-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #264 from terra-money/feat/pre-transaction-hooks
- Loading branch information
Showing
13 changed files
with
301 additions
and
59 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package ante | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
sdkerrors "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrortypes "github.com/cosmos/cosmos-sdk/types/errors" | ||
tx2 "github.com/cosmos/cosmos-sdk/types/tx" | ||
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" | ||
|
||
wasmvmtypes "github.com/CosmWasm/wasmvm/types" | ||
smartaccounttypes "github.com/terra-money/core/v2/x/smartaccount/types" | ||
) | ||
|
||
// SmartAccountCheckDecorator does authentication for smart accounts | ||
type PreTransactionHookDecorator struct { | ||
smartaccountKeeper SmartAccountKeeper | ||
wasmKeeper WasmKeeper | ||
} | ||
|
||
func NewPreTransactionHookDecorator(sak SmartAccountKeeper, wk WasmKeeper) PreTransactionHookDecorator { | ||
return PreTransactionHookDecorator{ | ||
smartaccountKeeper: sak, | ||
wasmKeeper: wk, | ||
} | ||
} | ||
|
||
// AnteHandle checks if the tx provides sufficient fee to cover the required fee from the fee market. | ||
func (pth PreTransactionHookDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { | ||
setting, ok := ctx.Value(smartaccounttypes.ModuleName).(smartaccounttypes.Setting) | ||
if !ok { | ||
return next(ctx, tx, simulate) | ||
} | ||
|
||
if setting.PreTransaction != nil && len(setting.PreTransaction) > 0 { | ||
for _, preTx := range setting.PreTransaction { | ||
contractAddr, err := sdk.AccAddressFromBech32(preTx) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
data, err := BuildPreTransactionHookMsg(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) | ||
} | ||
|
||
// TODO: to refactor | ||
func BuildPreTransactionHookMsg(tx sdk.Tx) ([]byte, error) { | ||
sigTx, ok := tx.(authsigning.SigVerifiableTx) | ||
if !ok { | ||
return nil, sdkerrors.Wrap(sdkerrortypes.ErrInvalidType, "expected SigVerifiableTx") | ||
} | ||
|
||
// Signer here is the account that the state transition is affecting | ||
// e.g. Account that is transferring some Coins | ||
signers := sigTx.GetSigners() | ||
// Current only supports one signer (TODO review in the future) | ||
if len(signers) != 1 { | ||
return nil, sdkerrors.Wrap(sdkerrortypes.ErrorInvalidSigner, "only one signer is supported") | ||
} | ||
|
||
// Sender here is the account that signed the transaction | ||
// Could be different from the account above (confusingly named signer) | ||
signatures, _ := sigTx.GetSignaturesV2() | ||
if len(signatures) == 0 { | ||
return nil, sdkerrors.Wrap(sdkerrortypes.ErrNoSignatures, "no signatures found") | ||
} | ||
senderAddr, err := sdk.AccAddressFromHexUnsafe(signatures[0].PubKey.Address().String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
msgs := sigTx.GetMsgs() | ||
anyMsgs, err := tx2.SetMsgs(msgs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var stargateMsgs []wasmvmtypes.CosmosMsg | ||
for _, msg := range anyMsgs { | ||
stargateMsg := wasmvmtypes.StargateMsg{ | ||
TypeURL: msg.TypeUrl, | ||
Value: msg.Value, | ||
} | ||
stargateMsgs = append(stargateMsgs, wasmvmtypes.CosmosMsg{ | ||
Stargate: &stargateMsg, | ||
}) | ||
} | ||
preTx := smartaccounttypes.PreTransaction{ | ||
Sender: senderAddr.String(), | ||
Account: signers[0].String(), | ||
Messages: stargateMsgs, | ||
} | ||
msg := smartaccounttypes.SudoMsg{PreTransaction: &preTx} | ||
return json.Marshal(msg) | ||
} |
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 was deleted.
Oops, something went wrong.
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,55 @@ | ||
package tests | ||
|
||
import ( | ||
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" | ||
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
"github.com/terra-money/core/v2/x/smartaccount/ante" | ||
"github.com/terra-money/core/v2/x/smartaccount/test_helpers" | ||
"testing" | ||
) | ||
|
||
type AnteTestSuite struct { | ||
test_helpers.SmartAccountTestSuite | ||
|
||
Decorator ante.PreTransactionHookDecorator | ||
WasmKeeper *wasmkeeper.PermissionedKeeper | ||
} | ||
|
||
func TestAnteSuite(t *testing.T) { | ||
suite.Run(t, new(AnteTestSuite)) | ||
} | ||
|
||
func (s *AnteTestSuite) Setup() { | ||
s.SmartAccountTestSuite.Setup() | ||
s.WasmKeeper = wasmkeeper.NewDefaultPermissionKeeper(s.App.Keepers.WasmKeeper) | ||
s.Decorator = ante.NewPreTransactionHookDecorator(s.SmartAccountKeeper, s.WasmKeeper) | ||
} | ||
|
||
func (s *AnteTestSuite) BuildDefaultMsgTx(accountIndex int, msgs ...sdk.Msg) client.TxBuilder { | ||
pk := s.TestAccPrivs[accountIndex] | ||
txBuilder := s.EncodingConfig.TxConfig.NewTxBuilder() | ||
err := txBuilder.SetMsgs( | ||
msgs..., | ||
) | ||
require.NoError(s.T(), err) | ||
signer := authsigning.SignerData{ | ||
ChainID: "test", | ||
AccountNumber: 0, | ||
Sequence: 0, | ||
} | ||
sig, err := tx.SignWithPrivKey( | ||
s.EncodingConfig.TxConfig.SignModeHandler().DefaultMode(), | ||
signer, | ||
txBuilder, | ||
pk, | ||
s.EncodingConfig.TxConfig, | ||
0, | ||
) | ||
txBuilder.SetSignatures(sig) | ||
return txBuilder | ||
} |
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,89 @@ | ||
package tests | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/bank/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
"github.com/stretchr/testify/require" | ||
"github.com/terra-money/core/v2/x/smartaccount/test_helpers" | ||
smartaccounttypes "github.com/terra-money/core/v2/x/smartaccount/types" | ||
) | ||
|
||
func (s *AnteTestSuite) TestPreTransactionHookWithoutSmartAccount() { | ||
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.Decorator.AnteHandle(s.Ctx, txBuilder.GetTx(), false, sdk.ChainAnteDecorators(sdk.Terminator{})) | ||
require.NoError(s.T(), err) | ||
} | ||
|
||
func (s *AnteTestSuite) TestPreTransactionHookWithEmptySmartAccount() { | ||
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.Decorator.AnteHandle(s.Ctx, txBuilder.GetTx(), false, sdk.ChainAnteDecorators(sdk.Terminator{})) | ||
require.NoError(s.T(), err) | ||
} | ||
|
||
func (s *AnteTestSuite) TestInvalidContractAddress() { | ||
s.Setup() | ||
s.Ctx = s.Ctx.WithValue(smartaccounttypes.ModuleName, smartaccounttypes.Setting{ | ||
PreTransaction: []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.Decorator.AnteHandle(s.Ctx, txBuilder.GetTx(), false, sdk.ChainAnteDecorators(sdk.Terminator{})) | ||
require.ErrorContainsf(s.T(), err, "no such contract", "error message: %s", err) | ||
} | ||
|
||
func (s *AnteTestSuite) 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{ | ||
PreTransaction: []string{contractAddr.String()}, | ||
}) | ||
txBuilder := s.BuildDefaultMsgTx(0, &types.MsgSend{ | ||
FromAddress: acc.String(), | ||
ToAddress: acc.String(), | ||
Amount: sdk.NewCoins(sdk.NewInt64Coin("uluna", 100000000)), | ||
}) | ||
_, err = s.Decorator.AnteHandle(s.Ctx, txBuilder.GetTx(), false, sdk.ChainAnteDecorators(sdk.Terminator{})) | ||
require.NoError(s.T(), err) | ||
} | ||
|
||
func (s *AnteTestSuite) 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{ | ||
PreTransaction: []string{contractAddr.String()}, | ||
}) | ||
txBuilder := s.BuildDefaultMsgTx(0, &stakingtypes.MsgDelegate{ | ||
DelegatorAddress: acc.String(), | ||
ValidatorAddress: acc.String(), | ||
Amount: sdk.NewInt64Coin("uluna", 100000000), | ||
}) | ||
_, err = s.Decorator.AnteHandle(s.Ctx, txBuilder.GetTx(), false, sdk.ChainAnteDecorators(sdk.Terminator{})) | ||
require.ErrorContainsf(s.T(), err, "Unauthorized message type", "error message: %s", err) | ||
} |
Oops, something went wrong.