Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get sa settings using ctx #278

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,228 changes: 15 additions & 1,213 deletions integration-tests/package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"description": "Integration tests for Core using feather.js",
"main": "index.ts",
"scripts": {
"test:init" : "bash src/setup/init-test-framework.sh",
"test:init": "bash src/setup/init-test-framework.sh",
"test:relayer": "bash src/setup/relayer/init-relayer.sh",
"test:chain:upgrade" : "bash src/setup/chain-upgrade/chain-upgrade.sh",
"test:start" : "jest --runInBand --detectOpenHandles",
"test:chain:upgrade": "bash src/setup/chain-upgrade/chain-upgrade.sh",
"test:start": "jest --runInBand --detectOpenHandles",
"start": "npm run test:init && npm run test:relayer && npm run test:start",
"test:clean": "rm -rf src/test-data chain-upgrade-data && pkill terrad && pkill terrad && pkill relayer"
},
Expand Down Expand Up @@ -35,7 +35,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@terra-money/feather.js": "^2.0.0-beta.16",
"@terra-money/feather.js": "^2.1.0-beta.4",
"moment": "^2.29.4"
}
}
}
36 changes: 31 additions & 5 deletions integration-tests/src/modules/smartaccount/pretx.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Coin, Coins, MsgDelegate, MsgInstantiateContract, MsgStoreCode, ValAddress } from "@terra-money/feather.js";
import { Coin, Coins, MsgDelegate, MsgInstantiateContract, MsgSend, MsgStoreCode, ValAddress } from "@terra-money/feather.js";
import { MsgCreateSmartAccount, MsgUpdateTransactionHooks } from "@terra-money/feather.js/dist/core/smartaccount";
import fs from "fs";
import path from 'path';
Expand Down Expand Up @@ -113,8 +113,10 @@ describe("Smartaccount Module (https://github.com/terra-money/core/tree/release/
}
});

test('Transaction should fail for limit', async () => {
test('Transaction should fail for delegation', async () => {
try {
let setting = await LCD.chain1.smartaccount.setting(controlledAccountAddress);
expect(setting.preTransaction).toEqual([limitContractAddress]);
// signing with the controlledAccountAddress should now fail
let tx = await wallet.createAndSignTx({
msgs: [
Expand All @@ -127,9 +129,33 @@ describe("Smartaccount Module (https://github.com/terra-money/core/tree/release/
chainID: "test-1",
gas: '400000',
});
let result = await LCD.chain1.tx.broadcastSync(tx, "test-1");
console.log(result)
expect(result.raw_log).toEqual("Unauthorized: Unauthorized message type: execute wasm contract failed");
expect.assertions(1);
await LCD.chain1.tx.broadcastSync(tx, "test-1");
await blockInclusion();
} catch (e:any) {
console.log(e)
expect(e).toEqual("Unauthorized: Unauthorized message type: execute wasm contract failed");
}
});

test('Transaction should pass for send', async () => {
try {
// signing with the controlledAccountAddress should now fail
let tx = await wallet.createAndSignTx({
msgs: [
new MsgSend(
controlledAccountAddress,
controlledAccountAddress,
Coins.fromString("100000000uluna"),
),
],
chainID: "test-1",
gas: '400000',
});
let res = await LCD.chain1.tx.broadcastSync(tx, "test-1");
await blockInclusion();
let txResult = await LCD.chain1.tx.txInfo(res.txhash, "test-1") as any;
expect(txResult);
} catch (e:any) {
console.log(e)
expect(e).toBeUndefined();
Expand Down
13 changes: 7 additions & 6 deletions x/smartaccount/ante/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"

"github.com/terra-money/core/v2/x/smartaccount/types"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -12,7 +14,6 @@ import (
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/terra-money/core/v2/x/smartaccount/types"
)

// SmartAccountAuthDecorator does authentication for smart accounts
Expand Down Expand Up @@ -51,11 +52,6 @@ func NewSmartAccountAuthDecorator(

// AnteHandle checks if the tx provides sufficient fee to cover the required fee from the fee market.
func (sad SmartAccountAuthDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
// skip the smartaccount auth decorator if the tx is a simulation
if simulate {
return next(ctx, tx, simulate)
}

sigTx, ok := tx.(authsigning.SigVerifiableTx)
if !ok {
return ctx, sdkerrors.ErrInvalidType.Wrap("expected SigVerifiableTx")
Expand All @@ -80,6 +76,11 @@ func (sad SmartAccountAuthDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu
} else if err != nil {
return ctx, err
}
ctx = ctx.WithValue(types.ModuleName, setting)
// skip authorization checks if simulate since no signatures will be provided
if simulate {
return next(ctx, tx, simulate)
}

// Current smartaccount only supports one signer (TODO review in the future)
if len(signers) != 1 {
Expand Down
15 changes: 1 addition & 14 deletions x/smartaccount/ante/pretransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,9 @@ func NewPreTransactionHookDecorator(sak SmartAccountKeeper, wk WasmKeeper) PreTr
}

func (pth PreTransactionHookDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
sigTx, ok := tx.(authsigning.SigVerifiableTx)
setting, ok := ctx.Value(types.ModuleName).(*types.Setting)
if !ok {
return ctx, sdkerrors.ErrInvalidType.Wrap("expected SigVerifiableTx")
}

// Signer here is the account that the state transition is affecting
// e.g. Account that is transferring some Coins
signers := sigTx.GetSigners()
account := signers[0]
accountStr := account.String()

setting, err := pth.smartAccountKeeper.GetSetting(ctx, accountStr)
if sdkerrors.ErrKeyNotFound.Is(err) {
return next(ctx, tx, simulate)
} else if err != nil {
return ctx, err
}

if setting.PreTransaction != nil && len(setting.PreTransaction) > 0 {
Expand Down
11 changes: 6 additions & 5 deletions x/smartaccount/ante/tests/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import (
"testing"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
"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"
smartaccounttypes "github.com/terra-money/core/v2/x/smartaccount/types"

"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"
"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"
smartaccounttypes "github.com/terra-money/core/v2/x/smartaccount/types"
)

type AnteTestSuite struct {
Expand Down
29 changes: 10 additions & 19 deletions x/smartaccount/ante/tests/pretransaction_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

func (s *AnteTestSuite) TestPreTransactionHookWithoutSmartAccount() {
Expand All @@ -22,39 +23,32 @@ func (s *AnteTestSuite) TestPreTransactionHookWithoutSmartAccount() {

func (s *AnteTestSuite) TestPreTransactionHookWithEmptySmartAccount() {
s.Setup()

// set settings
err := s.SmartAccountKeeper.SetSetting(s.Ctx, smartaccounttypes.Setting{
s.Ctx = s.Ctx.WithValue(smartaccounttypes.ModuleName, &smartaccounttypes.Setting{
Owner: s.TestAccs[0].String(),
})
require.NoError(s.T(), err)

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.PreTxDecorator.AnteHandle(s.Ctx, txBuilder.GetTx(), false, sdk.ChainAnteDecorators(sdk.Terminator{}))
_, err := s.PreTxDecorator.AnteHandle(s.Ctx, txBuilder.GetTx(), false, sdk.ChainAnteDecorators(sdk.Terminator{}))
require.NoError(s.T(), err)
}

func (s *AnteTestSuite) TestInvalidContractAddress() {
s.Setup()

// set settings
err := s.SmartAccountKeeper.SetSetting(s.Ctx, smartaccounttypes.Setting{
s.Ctx = s.Ctx.WithValue(smartaccounttypes.ModuleName, &smartaccounttypes.Setting{
Owner: s.TestAccs[0].String(),
PreTransaction: []string{s.TestAccs[0].String()},
})
require.NoError(s.T(), err)

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.PreTxDecorator.AnteHandle(s.Ctx, txBuilder.GetTx(), false, sdk.ChainAnteDecorators(sdk.Terminator{}))
_, err := s.PreTxDecorator.AnteHandle(s.Ctx, txBuilder.GetTx(), false, sdk.ChainAnteDecorators(sdk.Terminator{}))
require.ErrorContainsf(s.T(), err, "no such contract", "error message: %s", err)
}

Expand All @@ -67,12 +61,10 @@ func (s *AnteTestSuite) TestSendCoinsWithLimitSendHook() {
contractAddr, _, err := s.WasmKeeper.Instantiate(s.Ctx, codeId, acc, acc, []byte("{}"), "limit send", sdk.NewCoins())
require.NoError(s.T(), err)

// set settings
err = s.SmartAccountKeeper.SetSetting(s.Ctx, smartaccounttypes.Setting{
s.Ctx = s.Ctx.WithValue(smartaccounttypes.ModuleName, &smartaccounttypes.Setting{
Owner: acc.String(),
PreTransaction: []string{contractAddr.String()},
})
require.NoError(s.T(), err)

txBuilder := s.BuildDefaultMsgTx(0, &types.MsgSend{
FromAddress: acc.String(),
Expand All @@ -93,11 +85,10 @@ func (s *AnteTestSuite) TestStakingWithLimitSendHook() {
require.NoError(s.T(), err)

// set settings
err = s.SmartAccountKeeper.SetSetting(s.Ctx, smartaccounttypes.Setting{
s.Ctx = s.Ctx.WithValue(smartaccounttypes.ModuleName, &smartaccounttypes.Setting{
Owner: acc.String(),
PreTransaction: []string{contractAddr.String()},
})
require.NoError(s.T(), err)

txBuilder := s.BuildDefaultMsgTx(0, &stakingtypes.MsgDelegate{
DelegatorAddress: acc.String(),
Expand Down
3 changes: 2 additions & 1 deletion x/smartaccount/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package keeper_test
import (
"encoding/base64"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/terra-money/core/v2/x/smartaccount/test_helpers"
"github.com/terra-money/core/v2/x/smartaccount/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func (s *IntegrationTestSuite) TestMsgCreateAndDisableSmartAccount() {
Expand Down
2 changes: 1 addition & 1 deletion x/smartaccount/post/posttransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (pth PostTransactionHookDecorator) PostHandle(
success bool,
next sdk.PostHandler,
) (newCtx sdk.Context, err error) {
setting, ok := ctx.Value(types.ModuleName).(types.Setting)
setting, ok := ctx.Value(types.ModuleName).(*types.Setting)
if !ok {
return next(ctx, tx, simulate, success)
}
Expand Down
11 changes: 6 additions & 5 deletions x/smartaccount/post/posttransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import (
"testing"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
"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"

"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 {
Expand Down
Loading