-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a859d3a
commit fb448bb
Showing
8 changed files
with
256 additions
and
61 deletions.
There are no files selected for viewing
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
File renamed without changes.
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,60 @@ | ||
package post | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/terra-money/core/v2/x/smartaccount/ante" | ||
"github.com/terra-money/core/v2/x/smartaccount/types" | ||
) | ||
|
||
// PostTransactionHookDecorator does authentication for smart accounts | ||
type PostTransactionHookDecorator struct { | ||
smartaccountKeeper SmartAccountKeeper | ||
wasmKeeper WasmKeeper | ||
} | ||
|
||
func NewPostTransactionHookDecorator(sak SmartAccountKeeper, wk WasmKeeper) PostTransactionHookDecorator { | ||
return PostTransactionHookDecorator{ | ||
smartaccountKeeper: sak, | ||
wasmKeeper: wk, | ||
} | ||
} | ||
|
||
// FeeSharePostHandler if the smartaccount module is enabled | ||
// takes the total fees paid for each transaction and | ||
// split these fees equally between all the contacts | ||
// involved in the transaction based on the module params. | ||
func (pth PostTransactionHookDecorator) PostHandle( | ||
ctx sdk.Context, | ||
tx sdk.Tx, | ||
simulate bool, | ||
success bool, | ||
next sdk.PostHandler, | ||
) (newCtx sdk.Context, err error) { | ||
setting, ok := ctx.Value(types.ModuleName).(types.Setting) | ||
if !ok { | ||
return next(ctx, tx, simulate, success) | ||
} | ||
|
||
if setting.PostTransaction != nil && len(setting.PostTransaction) > 0 { | ||
for _, postTx := range setting.PostTransaction { | ||
contractAddr, err := sdk.AccAddressFromBech32(postTx) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
data, err := BuildPostTransactionHookMsg(tx) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
_, err = pth.wasmKeeper.Sudo(ctx, contractAddr, data) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
} | ||
} | ||
return next(ctx, tx, simulate, success) | ||
} | ||
|
||
func BuildPostTransactionHookMsg(tx sdk.Tx) ([]byte, error) { | ||
return ante.BuildPrePostTransactionHookMsg(tx, false) | ||
} |
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,162 @@ | ||
package post_test | ||
|
||
import ( | ||
"testing" | ||
|
||
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" | ||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/tx" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/tx/signing" | ||
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" | ||
"github.com/cosmos/cosmos-sdk/x/bank/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
"github.com/terra-money/core/v2/x/smartaccount/post" | ||
"github.com/terra-money/core/v2/x/smartaccount/test_helpers" | ||
smartaccounttypes "github.com/terra-money/core/v2/x/smartaccount/types" | ||
) | ||
|
||
type PostTxTestSuite struct { | ||
test_helpers.SmartAccountTestSuite | ||
|
||
PostTxDecorator post.PostTransactionHookDecorator | ||
WasmKeeper *wasmkeeper.PermissionedKeeper | ||
} | ||
|
||
func TestAnteSuite(t *testing.T) { | ||
suite.Run(t, new(PostTxTestSuite)) | ||
} | ||
|
||
func (s *PostTxTestSuite) Setup() { | ||
s.SmartAccountTestSuite.SetupTests() | ||
s.WasmKeeper = wasmkeeper.NewDefaultPermissionKeeper(s.App.Keepers.WasmKeeper) | ||
s.PostTxDecorator = post.NewPostTransactionHookDecorator(s.SmartAccountKeeper, s.WasmKeeper) | ||
s.Ctx = s.Ctx.WithChainID("test") | ||
} | ||
|
||
func (s *PostTxTestSuite) TestPostTransactionHookWithoutSmartAccount() { | ||
s.Setup() | ||
txBuilder := s.BuildDefaultMsgTx(0, &types.MsgSend{ | ||
FromAddress: s.TestAccs[0].String(), | ||
ToAddress: s.TestAccs[1].String(), | ||
Amount: sdk.NewCoins(sdk.NewInt64Coin("uluna", 100000000)), | ||
}) | ||
_, err := s.PostTxDecorator.PostHandle(s.Ctx, txBuilder.GetTx(), false, true, sdk.ChainPostDecorators(sdk.Terminator{})) | ||
require.NoError(s.T(), err) | ||
} | ||
|
||
func (s *PostTxTestSuite) TestPostTransactionHookWithEmptySmartAccount() { | ||
s.Setup() | ||
s.Ctx = s.Ctx.WithValue(smartaccounttypes.ModuleName, smartaccounttypes.Setting{}) | ||
txBuilder := s.BuildDefaultMsgTx(0, &types.MsgSend{ | ||
FromAddress: s.TestAccs[0].String(), | ||
ToAddress: s.TestAccs[1].String(), | ||
Amount: sdk.NewCoins(sdk.NewInt64Coin("uluna", 100000000)), | ||
}) | ||
_, err := s.PostTxDecorator.PostHandle(s.Ctx, txBuilder.GetTx(), false, true, sdk.ChainPostDecorators(sdk.Terminator{})) | ||
require.NoError(s.T(), err) | ||
} | ||
|
||
func (s *PostTxTestSuite) TestInvalidContractAddress() { | ||
s.Setup() | ||
s.Ctx = s.Ctx.WithValue(smartaccounttypes.ModuleName, smartaccounttypes.Setting{ | ||
PostTransaction: []string{s.TestAccs[0].String()}, | ||
}) | ||
txBuilder := s.BuildDefaultMsgTx(0, &types.MsgSend{ | ||
FromAddress: s.TestAccs[0].String(), | ||
ToAddress: s.TestAccs[1].String(), | ||
Amount: sdk.NewCoins(sdk.NewInt64Coin("uluna", 100000000)), | ||
}) | ||
_, err := s.PostTxDecorator.PostHandle(s.Ctx, txBuilder.GetTx(), false, true, sdk.ChainPostDecorators(sdk.Terminator{})) | ||
require.ErrorContainsf(s.T(), err, "no such contract", "error message: %s", err) | ||
} | ||
|
||
func (s *PostTxTestSuite) TestSendCoinsWithLimitSendHook() { | ||
s.Setup() | ||
|
||
acc := s.TestAccs[0] | ||
codeId, _, err := s.WasmKeeper.Create(s.Ctx, acc, test_helpers.LimitSendOnlyHookWasm, nil) | ||
require.NoError(s.T(), err) | ||
contractAddr, _, err := s.WasmKeeper.Instantiate(s.Ctx, codeId, acc, acc, []byte("{}"), "limit send", sdk.NewCoins()) | ||
require.NoError(s.T(), err) | ||
|
||
s.Ctx = s.Ctx.WithValue(smartaccounttypes.ModuleName, smartaccounttypes.Setting{ | ||
PostTransaction: []string{contractAddr.String()}, | ||
}) | ||
txBuilder := s.BuildDefaultMsgTx(0, &types.MsgSend{ | ||
FromAddress: acc.String(), | ||
ToAddress: acc.String(), | ||
Amount: sdk.NewCoins(sdk.NewInt64Coin("uluna", 100000000)), | ||
}) | ||
_, err = s.PostTxDecorator.PostHandle(s.Ctx, txBuilder.GetTx(), false, true, sdk.ChainPostDecorators(sdk.Terminator{})) | ||
require.NoError(s.T(), err) | ||
} | ||
|
||
func (s *PostTxTestSuite) TestStakingWithLimitSendHook() { | ||
s.Setup() | ||
|
||
acc := s.TestAccs[0] | ||
codeId, _, err := s.WasmKeeper.Create(s.Ctx, acc, test_helpers.LimitSendOnlyHookWasm, nil) | ||
require.NoError(s.T(), err) | ||
contractAddr, _, err := s.WasmKeeper.Instantiate(s.Ctx, codeId, acc, acc, []byte("{}"), "limit send", sdk.NewCoins()) | ||
require.NoError(s.T(), err) | ||
|
||
s.Ctx = s.Ctx.WithValue(smartaccounttypes.ModuleName, smartaccounttypes.Setting{ | ||
PostTransaction: []string{contractAddr.String()}, | ||
}) | ||
txBuilder := s.BuildDefaultMsgTx(0, &stakingtypes.MsgDelegate{ | ||
DelegatorAddress: acc.String(), | ||
ValidatorAddress: acc.String(), | ||
Amount: sdk.NewInt64Coin("uluna", 100000000), | ||
}) | ||
_, err = s.PostTxDecorator.PostHandle(s.Ctx, txBuilder.GetTx(), false, true, sdk.ChainPostDecorators(sdk.Terminator{})) | ||
require.ErrorContainsf(s.T(), err, "Unauthorized message type", "error message: %s", err) | ||
} | ||
|
||
func (s *PostTxTestSuite) BuildDefaultMsgTx(accountIndex int, msgs ...sdk.Msg) client.TxBuilder { | ||
pk := s.TestAccPrivs[accountIndex] | ||
sender := s.TestAccs[accountIndex] | ||
acc := s.App.Keepers.AccountKeeper.GetAccount(s.Ctx, msgs[0].GetSigners()[0]) | ||
txBuilder := s.EncodingConfig.TxConfig.NewTxBuilder() | ||
err := txBuilder.SetMsgs( | ||
msgs..., | ||
) | ||
require.NoError(s.T(), err) | ||
|
||
signer := authsigning.SignerData{ | ||
Address: sender.String(), | ||
ChainID: "test", | ||
AccountNumber: acc.GetAccountNumber(), | ||
Sequence: acc.GetSequence(), | ||
PubKey: pk.PubKey(), | ||
} | ||
|
||
emptySig := signing.SignatureV2{ | ||
PubKey: signer.PubKey, | ||
Data: &signing.SingleSignatureData{ | ||
SignMode: s.EncodingConfig.TxConfig.SignModeHandler().DefaultMode(), | ||
Signature: nil, | ||
}, | ||
Sequence: signer.Sequence, | ||
} | ||
|
||
err = txBuilder.SetSignatures(emptySig) | ||
require.NoError(s.T(), err) | ||
|
||
sigV2, err := tx.SignWithPrivKey( | ||
s.EncodingConfig.TxConfig.SignModeHandler().DefaultMode(), | ||
signer, | ||
txBuilder, | ||
pk, | ||
s.EncodingConfig.TxConfig, | ||
acc.GetSequence(), | ||
) | ||
require.NoError(s.T(), err) | ||
|
||
err = txBuilder.SetSignatures(sigV2) | ||
require.NoError(s.T(), err) | ||
|
||
return txBuilder | ||
} |
This file was deleted.
Oops, something went wrong.
Binary file modified
BIN
+55 Bytes
(100%)
x/smartaccount/test_helpers/test_data/limit_send_only_hooks.wasm
Binary file not shown.
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