-
Notifications
You must be signed in to change notification settings - Fork 589
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into adam/e2e-upgrade
- Loading branch information
Showing
57 changed files
with
3,418 additions
and
277 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# CODEOWNERS: https://help.github.com/articles/about-codeowners/ | ||
|
||
# NOTE: Order is important; the last matching pattern takes the | ||
# most precedence. | ||
|
||
# People who get pinged on every PR, request if you'd like to be added | ||
* @osmosis-labs/chain-engineering-core-reviewers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package ante | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
bank "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
"github.com/spf13/cast" | ||
|
||
servertypes "github.com/cosmos/cosmos-sdk/server/types" | ||
) | ||
|
||
type SendBlockOptions struct { | ||
PermittedOnlySendTo map[string]string | ||
} | ||
|
||
func NewSendBlockOptions(appOpts servertypes.AppOptions) SendBlockOptions { | ||
return SendBlockOptions{ | ||
PermittedOnlySendTo: parsePermittedOnlySendTo(appOpts), | ||
} | ||
} | ||
|
||
func parsePermittedOnlySendTo(opts servertypes.AppOptions) map[string]string { | ||
valueInterface := opts.Get("permitted-only-send-to") | ||
if valueInterface == nil { | ||
return make(map[string]string) | ||
} | ||
return cast.ToStringMapString(valueInterface) // equal with viper.GetStringMapString | ||
} | ||
|
||
type SendBlockDecorator struct { | ||
Options SendBlockOptions | ||
} | ||
|
||
func NewSendBlockDecorator(options SendBlockOptions) *SendBlockDecorator { | ||
return &SendBlockDecorator{ | ||
Options: options, // TODO: hydrate from configuration | ||
} | ||
} | ||
|
||
func (decorator *SendBlockDecorator) AnteHandle( | ||
ctx sdk.Context, | ||
tx sdk.Tx, | ||
simulate bool, | ||
next sdk.AnteHandler, | ||
) (newCtx sdk.Context, err error) { | ||
if ctx.IsReCheckTx() { | ||
return next(ctx, tx, simulate) | ||
} | ||
|
||
if ctx.IsCheckTx() && !simulate { | ||
if err := decorator.CheckIfBlocked(tx.GetMsgs()); err != nil { | ||
return ctx, err | ||
} | ||
} | ||
|
||
return next(ctx, tx, simulate) | ||
} | ||
|
||
// CheckIfBlocked returns error if following are true: | ||
// 1. decorator.permittedOnlySendTo has msg.GetSigners() has its key, and | ||
// 2-1. msg is not a SendMsg, or | ||
// 2-2. msg is SendMsg and the destination is not decorator.permittedOnlySendTo[msg.Sender] | ||
func (decorator *SendBlockDecorator) CheckIfBlocked(msgs []sdk.Msg) error { | ||
if len(decorator.Options.PermittedOnlySendTo) == 0 { | ||
return nil | ||
} | ||
for _, msg := range msgs { | ||
signers := msg.GetSigners() | ||
for _, signer := range signers { | ||
if permittedTo, ok := decorator.Options.PermittedOnlySendTo[signer.String()]; ok { | ||
sendmsg, ok := msg.(*bank.MsgSend) | ||
if !ok { | ||
return fmt.Errorf("signer is not allowed to send transactions: %s", signer) | ||
} | ||
if sendmsg.ToAddress != permittedTo { | ||
return fmt.Errorf("signer is not allowed to send tokens: %s", signer) | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ante | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
bank "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSendBlockDecorator(t *testing.T) { | ||
testCases := []struct { | ||
from sdk.AccAddress | ||
to sdk.AccAddress | ||
expectPass bool | ||
}{ | ||
{sdk.AccAddress("honest-sender"), sdk.AccAddress("honest-address"), true}, | ||
{sdk.AccAddress("honest-sender"), sdk.AccAddress("recovery-address"), true}, | ||
{sdk.AccAddress("malicious-sender"), sdk.AccAddress("recovery-address"), true}, | ||
{sdk.AccAddress("malicious-sender"), sdk.AccAddress("random-address"), false}, | ||
} | ||
|
||
permittedOnlySendTo := map[string]string{ | ||
sdk.AccAddress("malicious-sender").String(): sdk.AccAddress("recovery-address").String(), | ||
} | ||
decorator := NewSendBlockDecorator(SendBlockOptions{permittedOnlySendTo}) | ||
|
||
for _, testCase := range testCases { | ||
err := decorator.CheckIfBlocked([]sdk.Msg{bank.NewMsgSend(testCase.from, testCase.to, sdk.NewCoins(sdk.NewInt64Coin("test", 1)))}) | ||
if testCase.expectPass { | ||
require.NoError(t, err) | ||
} else { | ||
require.Error(t, err) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.