Skip to content

Commit

Permalink
Address sec3 audit feedbacks (sei-protocol#1766)
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen authored Jul 18, 2024
1 parent 656642e commit 7b9d809
Show file tree
Hide file tree
Showing 22 changed files with 84 additions and 188 deletions.
11 changes: 7 additions & 4 deletions app/receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app_test

import (
"crypto/sha256"
"embed"
"encoding/hex"
"fmt"
"math/big"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
pcommon "github.com/sei-protocol/sei-chain/precompiles/common"
"github.com/sei-protocol/sei-chain/precompiles/wasmd"
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
evmtypes "github.com/sei-protocol/sei-chain/x/evm/types"
Expand All @@ -28,6 +30,9 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
)

//go:embed wasm_abi.json
var f embed.FS

func TestEvmEventsForCw20(t *testing.T) {
k := testkeeper.EVMTestApp.EvmKeeper
wasmKeeper := k.WasmKeeper()
Expand Down Expand Up @@ -74,8 +79,7 @@ func TestEvmEventsForCw20(t *testing.T) {
require.True(t, found)

// calling from wasmd precompile
abi, err := wasmd.GetABI()
require.Nil(t, err)
abi := pcommon.MustGetABI(f, "wasm_abi.json")
emptyCoins, err := sdk.NewCoins().MarshalJSON()
require.Nil(t, err)
data, err := abi.Pack("execute", contractAddr.String(), payload, emptyCoins)
Expand Down Expand Up @@ -189,8 +193,7 @@ func TestEvmEventsForCw721(t *testing.T) {
require.True(t, found)

// calling from wasmd precompile
abi, err := wasmd.GetABI()
require.Nil(t, err)
abi := pcommon.MustGetABI(f, "wasm_abi.json")
emptyCoins, err := sdk.NewCoins().MarshalJSON()
require.Nil(t, err)
payload = []byte(fmt.Sprintf("{\"mint\":{\"token_id\":\"2\",\"owner\":\"%s\"}}", creator.String()))
Expand Down
1 change: 1 addition & 0 deletions app/wasm_abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"string","name":"contractAddress","type":"string"},{"internalType":"bytes","name":"msg","type":"bytes"},{"internalType":"bytes","name":"coins","type":"bytes"}],"name":"execute","outputs":[{"internalType":"bytes","name":"response","type":"bytes"}],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"string","name":"contractAddress","type":"string"},{"internalType":"bytes","name":"msg","type":"bytes"},{"internalType":"bytes","name":"coins","type":"bytes"}],"internalType":"struct IWasmd.ExecuteMsg[]","name":"executeMsgs","type":"tuple[]"}],"name":"execute_batch","outputs":[{"internalType":"bytes[]","name":"responses","type":"bytes[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint64","name":"codeID","type":"uint64"},{"internalType":"string","name":"admin","type":"string"},{"internalType":"bytes","name":"msg","type":"bytes"},{"internalType":"string","name":"label","type":"string"},{"internalType":"bytes","name":"coins","type":"bytes"}],"name":"instantiate","outputs":[{"internalType":"string","name":"contractAddr","type":"string"},{"internalType":"bytes","name":"data","type":"bytes"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"contractAddress","type":"string"},{"internalType":"bytes","name":"req","type":"bytes"}],"name":"query","outputs":[{"internalType":"bytes","name":"response","type":"bytes"}],"stateMutability":"view","type":"function"}]
11 changes: 1 addition & 10 deletions precompiles/addr/addr.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package addr

import (
"bytes"
"embed"
"fmt"
"math/big"
Expand Down Expand Up @@ -38,15 +37,7 @@ type PrecompileExecutor struct {
}

func NewPrecompile(evmKeeper pcommon.EVMKeeper) (*pcommon.Precompile, error) {
abiBz, err := f.ReadFile("abi.json")
if err != nil {
return nil, fmt.Errorf("error loading the addr ABI %s", err)
}

newAbi, err := abi.JSON(bytes.NewReader(abiBz))
if err != nil {
return nil, err
}
newAbi := pcommon.MustGetABI(f, "abi.json")

p := &PrecompileExecutor{
evmKeeper: evmKeeper,
Expand Down
16 changes: 1 addition & 15 deletions precompiles/bank/bank.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bank

import (
"bytes"
"embed"
"errors"
"fmt"
Expand Down Expand Up @@ -37,19 +36,6 @@ const (
//go:embed abi.json
var f embed.FS

func GetABI() abi.ABI {
abiBz, err := f.ReadFile("abi.json")
if err != nil {
panic(err)
}

newAbi, err := abi.JSON(bytes.NewReader(abiBz))
if err != nil {
panic(err)
}
return newAbi
}

type PrecompileExecutor struct {
accountKeeper pcommon.AccountKeeper
bankKeeper pcommon.BankKeeper
Expand All @@ -72,7 +58,7 @@ type CoinBalance struct {
}

func NewPrecompile(bankKeeper pcommon.BankKeeper, evmKeeper pcommon.EVMKeeper, accountKeeper pcommon.AccountKeeper) (*pcommon.Precompile, error) {
newAbi := GetABI()
newAbi := pcommon.MustGetABI(f, "abi.json")
p := &PrecompileExecutor{
bankKeeper: bankKeeper,
evmKeeper: evmKeeper,
Expand Down
7 changes: 6 additions & 1 deletion precompiles/bank/bank_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bank_test

import (
"embed"
"encoding/hex"
"fmt"
"math/big"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/sei-protocol/sei-chain/precompiles/bank"
pcommon "github.com/sei-protocol/sei-chain/precompiles/common"
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/sei-protocol/sei-chain/x/evm/ante"
"github.com/sei-protocol/sei-chain/x/evm/keeper"
Expand All @@ -26,6 +28,9 @@ import (
tmtypes "github.com/tendermint/tendermint/proto/tendermint/types"
)

//go:embed abi.json
var f embed.FS

type mockTx struct {
msgs []sdk.Msg
signers []sdk.AccAddress
Expand Down Expand Up @@ -99,7 +104,7 @@ func TestRun(t *testing.T) {

// Send native 10_000_000_000_100, split into 10 usei 100wei
// Test payable with eth LegacyTx
abi := bank.GetABI()
abi := pcommon.MustGetABI(f, "abi.json")
argsNative, err := abi.Pack(bank.SendNativeMethod, seiAddr.String())
require.Nil(t, err)
require.Nil(t, err)
Expand Down
15 changes: 15 additions & 0 deletions precompiles/common/precompiles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package common

import (
"bytes"
"embed"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -248,3 +250,16 @@ func DefaultGasCost(input []byte, isTransaction bool) uint64 {

return storetypes.KVGasConfig().ReadCostFlat + (storetypes.KVGasConfig().ReadCostPerByte * uint64(len(input)))
}

func MustGetABI(f embed.FS, filename string) abi.ABI {
abiBz, err := f.ReadFile(filename)
if err != nil {
panic(err)
}

newAbi, err := abi.JSON(bytes.NewReader(abiBz))
if err != nil {
panic(err)
}
return newAbi
}
20 changes: 1 addition & 19 deletions precompiles/distribution/distribution.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package distribution

import (
"bytes"
"embed"
"errors"
"fmt"
Expand Down Expand Up @@ -34,19 +33,6 @@ const (
//go:embed abi.json
var f embed.FS

func GetABI() abi.ABI {
abiBz, err := f.ReadFile("abi.json")
if err != nil {
panic(err)
}

newAbi, err := abi.JSON(bytes.NewReader(abiBz))
if err != nil {
panic(err)
}
return newAbi
}

type PrecompileExecutor struct {
distrKeeper pcommon.DistributionKeeper
evmKeeper pcommon.EVMKeeper
Expand All @@ -59,7 +45,7 @@ type PrecompileExecutor struct {
}

func NewPrecompile(distrKeeper pcommon.DistributionKeeper, evmKeeper pcommon.EVMKeeper) (*pcommon.DynamicGasPrecompile, error) {
newAbi := GetABI()
newAbi := pcommon.MustGetABI(f, "abi.json")

p := &PrecompileExecutor{
distrKeeper: distrKeeper,
Expand Down Expand Up @@ -113,10 +99,6 @@ func (p PrecompileExecutor) EVMKeeper() pcommon.EVMKeeper {
return p.evmKeeper
}

func (p PrecompileExecutor) GetName() string {
return "distribution"
}

func (p PrecompileExecutor) setWithdrawAddress(ctx sdk.Context, method *abi.Method, caller common.Address, args []interface{}, value *big.Int) (ret []byte, remainingGas uint64, rerr error) {
defer func() {
if err := recover(); err != nil {
Expand Down
16 changes: 11 additions & 5 deletions precompiles/distribution/distribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package distribution_test

import (
"context"
"embed"
"encoding/hex"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"math/big"
"reflect"
"testing"
"time"

distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
crptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -36,6 +38,10 @@ import (
tmtypes "github.com/tendermint/tendermint/proto/tendermint/types"
)

//go:embed abi.json
//go:embed staking_abi.json
var f embed.FS

func TestWithdraw(t *testing.T) {
testApp := testkeeper.EVMTestApp
ctx := testApp.NewContext(false, tmtypes.Header{}).WithBlockHeight(2)
Expand All @@ -47,7 +53,7 @@ func TestWithdraw(t *testing.T) {
val := setupValidator(t, ctx, testApp, stakingtypes.Unbonded, valPub1)

// delegate
abi := staking.GetABI()
abi := pcommon.MustGetABI(f, "staking_abi.json")
args, err := abi.Pack("delegate", val.String())
require.Nil(t, err)

Expand Down Expand Up @@ -95,7 +101,7 @@ func TestWithdraw(t *testing.T) {
// set withdraw addr
withdrawSeiAddr, withdrawAddr := testkeeper.MockAddressPair()
k.SetAddressMapping(ctx, withdrawSeiAddr, withdrawAddr)
abi = distribution.GetABI()
abi = pcommon.MustGetABI(f, "abi.json")
args, err = abi.Pack("setWithdrawAddress", withdrawAddr)
require.Nil(t, err)
addr = common.HexToAddress(distribution.DistrAddress)
Expand Down Expand Up @@ -161,7 +167,7 @@ func TestWithdrawMultipleDelegationRewards(t *testing.T) {
getValidator(t, ctx, testApp),
getValidator(t, ctx, testApp)}

abi := staking.GetABI()
abi := pcommon.MustGetABI(f, "staking_abi.json")
privKey := testkeeper.MockPrivateKey()
addr := common.HexToAddress(staking.StakingAddress)
chainID := k.ChainID(ctx)
Expand Down Expand Up @@ -241,7 +247,7 @@ func setWithdrawAddressAndWithdraw(
) {
withdrawSeiAddr, withdrawAddr := testkeeper.MockAddressPair()
k.SetAddressMapping(ctx, withdrawSeiAddr, withdrawAddr)
abi := distribution.GetABI()
abi := pcommon.MustGetABI(f, "abi.json")
args, err := abi.Pack("setWithdrawAddress", withdrawAddr)
require.Nil(t, err)
addr = common.HexToAddress(distribution.DistrAddress)
Expand Down
1 change: 1 addition & 0 deletions precompiles/distribution/staking_abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"string","name":"valAddress","type":"string"}],"name":"delegate","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"srcAddress","type":"string"},{"internalType":"string","name":"dstAddress","type":"string"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"redelegate","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"valAddress","type":"string"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"undelegate","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]
15 changes: 1 addition & 14 deletions precompiles/gov/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,6 @@ const (
//go:embed abi.json
var f embed.FS

func GetABI() abi.ABI {
abiBz, err := f.ReadFile("abi.json")
if err != nil {
panic(err)
}

newAbi, err := abi.JSON(bytes.NewReader(abiBz))
if err != nil {
panic(err)
}
return newAbi
}

type PrecompileExecutor struct {
govKeeper pcommon.GovKeeper
evmKeeper pcommon.EVMKeeper
Expand All @@ -53,7 +40,7 @@ type PrecompileExecutor struct {
}

func NewPrecompile(govKeeper pcommon.GovKeeper, evmKeeper pcommon.EVMKeeper, bankKeeper pcommon.BankKeeper) (*pcommon.Precompile, error) {
newAbi := GetABI()
newAbi := pcommon.MustGetABI(f, "abi.json")

p := &PrecompileExecutor{
govKeeper: govKeeper,
Expand Down
7 changes: 6 additions & 1 deletion precompiles/gov/gov_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gov_test

import (
"embed"
"encoding/hex"
"math/big"
"testing"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/stretchr/testify/require"
tmtypes "github.com/tendermint/tendermint/proto/tendermint/types"

pcommon "github.com/sei-protocol/sei-chain/precompiles/common"
"github.com/sei-protocol/sei-chain/precompiles/gov"
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/sei-protocol/sei-chain/x/evm/ante"
Expand All @@ -21,6 +23,9 @@ import (
"github.com/sei-protocol/sei-chain/x/evm/types/ethtx"
)

//go:embed abi.json
var f embed.FS

func TestGovPrecompile(t *testing.T) {
testApp := testkeeper.EVMTestApp
ctx := testApp.NewContext(false, tmtypes.Header{}).WithBlockHeight(2)
Expand All @@ -34,7 +39,7 @@ func TestGovPrecompile(t *testing.T) {
testApp.GovKeeper.ActivateVotingPeriod(ctx, proposal2)

k := &testApp.EvmKeeper
abi := gov.GetABI()
abi := pcommon.MustGetABI(f, "abi.json")

type args struct {
method string
Expand Down
16 changes: 1 addition & 15 deletions precompiles/ibc/ibc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ibc

import (
"bytes"
"embed"
"errors"
"fmt"
Expand Down Expand Up @@ -34,19 +33,6 @@ const (
//go:embed abi.json
var f embed.FS

func GetABI() abi.ABI {
abiBz, err := f.ReadFile("abi.json")
if err != nil {
panic(err)
}

newAbi, err := abi.JSON(bytes.NewReader(abiBz))
if err != nil {
panic(err)
}
return newAbi
}

type PrecompileExecutor struct {
transferKeeper pcommon.TransferKeeper
evmKeeper pcommon.EVMKeeper
Expand All @@ -64,7 +50,7 @@ func NewPrecompile(
clientKeeper pcommon.ClientKeeper,
connectionKeeper pcommon.ConnectionKeeper,
channelKeeper pcommon.ChannelKeeper) (*pcommon.DynamicGasPrecompile, error) {
newAbi := GetABI()
newAbi := pcommon.MustGetABI(f, "abi.json")

p := &PrecompileExecutor{
transferKeeper: transferKeeper,
Expand Down
Loading

0 comments on commit 7b9d809

Please sign in to comment.