From a5109e244ada5b5940e018f8b052d84f8326cf3b Mon Sep 17 00:00:00 2001 From: LeoGuo621 <335209779@qq.com> Date: Mon, 10 Jul 2023 18:44:31 +0800 Subject: [PATCH] optimize ut --- .../app_parallel_test.go | 373 +++++++++++------- app/app_test.go | 31 +- app/test_helpers.go | 39 ++ libs/tendermint/types/milestone.go | 4 + 4 files changed, 279 insertions(+), 168 deletions(-) rename libs/cosmos-sdk/baseapp/baseapp_paralled_test.go => app/app_parallel_test.go (59%) diff --git a/libs/cosmos-sdk/baseapp/baseapp_paralled_test.go b/app/app_parallel_test.go similarity index 59% rename from libs/cosmos-sdk/baseapp/baseapp_paralled_test.go rename to app/app_parallel_test.go index d2ae3e815b..bf79d934de 100644 --- a/libs/cosmos-sdk/baseapp/baseapp_paralled_test.go +++ b/app/app_parallel_test.go @@ -1,9 +1,10 @@ -package baseapp_test +package app import ( "encoding/json" "math/big" "reflect" + "strings" "testing" "github.com/ethereum/go-ethereum/accounts/abi" @@ -12,27 +13,29 @@ import ( "github.com/stretchr/testify/require" "github.com/okex/exchain/app/crypto/ethsecp256k1" - types3 "github.com/okex/exchain/app/types" + apptypes "github.com/okex/exchain/app/types" + "github.com/okex/exchain/libs/cosmos-sdk/codec" "github.com/okex/exchain/libs/cosmos-sdk/simapp/helpers" sdk "github.com/okex/exchain/libs/cosmos-sdk/types" "github.com/okex/exchain/libs/cosmos-sdk/x/auth" authexported "github.com/okex/exchain/libs/cosmos-sdk/x/auth/exported" - simapp2 "github.com/okex/exchain/libs/ibc-go/testing/simapp" abci "github.com/okex/exchain/libs/tendermint/abci/types" - types2 "github.com/okex/exchain/libs/tendermint/types" - "github.com/okex/exchain/x/evm/types" - types4 "github.com/okex/exchain/x/token/types" + tmtypes "github.com/okex/exchain/libs/tendermint/types" + evmtypes "github.com/okex/exchain/x/evm/types" + tokentypes "github.com/okex/exchain/x/token/types" ) type Env struct { priv []ethsecp256k1.PrivKey addr []sdk.AccAddress } + type Chain struct { - app *simapp2.SimApp + app *OKExChainApp + codec *codec.Codec priv []ethsecp256k1.PrivKey addr []sdk.AccAddress - acc []*types3.EthAccount + acc []apptypes.EthAccount seq []uint64 num []uint64 chainIdStr string @@ -41,17 +44,23 @@ type Chain struct { } func NewChain(env *Env) *Chain { - types2.UnittestOnlySetMilestoneVenusHeight(-1) - types2.UnittestOnlySetMilestoneVenus1Height(-1) + tmtypes.UnittestOnlySetMilestoneVenusHeight(-1) + tmtypes.UnittestOnlySetMilestoneVenus1Height(1) + tmtypes.UnittestOnlySetMilestoneVenus2Height(1) + tmtypes.UnittestOnlySetMilestoneEarthHeight(1) + tmtypes.UnittestOnlySetMilestoneVenus6Height(1) chain := new(Chain) - chain.acc = make([]*types3.EthAccount, 10) + chain.acc = make([]apptypes.EthAccount, 10) chain.priv = make([]ethsecp256k1.PrivKey, 10) chain.addr = make([]sdk.AccAddress, 10) chain.seq = make([]uint64, 10) chain.num = make([]uint64, 10) - genAccs := []authexported.GenesisAccount{} + chain.chainIdStr = "ethermint-3" + chain.chainIdInt = big.NewInt(3) + // initialize account + genAccs := make([]authexported.GenesisAccount, 0) for i := 0; i < 10; i++ { - chain.acc[i] = &types3.EthAccount{ + chain.acc[i] = apptypes.EthAccount{ BaseAccount: &auth.BaseAccount{ Address: env.addr[i], Coins: sdk.Coins{sdk.NewInt64Coin("okt", 1000000)}, @@ -64,101 +73,68 @@ func NewChain(env *Env) *Chain { chain.seq[i] = 0 chain.num[i] = uint64(i) } - chain.chainIdStr = "ethermint-3" - chain.chainIdInt = big.NewInt(3) - chain.app = simapp2.SetupWithGenesisAccounts(genAccs, sdk.NewDecCoins(sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.NewDecWithPrec(1000000, 0)))) - //header := abci.Header{Height: app.LastBlockHeight() + 1, ChainID: chainIdStr} + chain.app = SetupWithGenesisAccounts(false, genAccs, WithChainId(chain.chainIdStr)) + chain.codec = chain.app.Codec() + + params := evmtypes.DefaultParams() + params.EnableCreate = true + params.EnableCall = true + chain.app.EvmKeeper.SetParams(chain.Ctx(), params) chain.app.BaseApp.Commit(abci.RequestCommit{}) return chain } -func createEthTx(t *testing.T, chain *Chain, i int) []byte { - amount, gasPrice, gasLimit := int64(1024), int64(2048), uint64(100000) - addrTo := ethcmn.BytesToAddress(chain.priv[i+1].PubKey().Address().Bytes()) - msg := types.NewMsgEthereumTx(chain.seq[i], &addrTo, big.NewInt(amount), gasLimit, big.NewInt(gasPrice), []byte("test")) - chain.seq[i]++ - err := msg.Sign(chain.chainIdInt, chain.priv[i].ToECDSA()) - require.NoError(t, err) - rawtx, err := rlp.EncodeToBytes(&msg) - require.NoError(t, err) - - return rawtx +func (chain *Chain) Ctx() sdk.Context { + return chain.app.BaseApp.GetDeliverStateCtx() } -//contract Storage { -//uint256 number; -///** -// * @dev Store value in variable -// * @param num value to store -// */ -//function store(uint256 num) public { -//number = num; -//} -//function add() public { -//number += 1; -//} -///** -// * @dev Return value -// * @return value of 'number' -// */ -//function retrieve() public view returns (uint256){ -//return number; -//} -//} -var abiStr = `[{"inputs":[{"internalType":"uint256","name":"num","type":"uint256"}],"name":"add","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"retrieve","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"num","type":"uint256"}],"name":"store","outputs":[],"stateMutability":"nonpayable","type":"function"}]` +func DeployContractAndGetContractAddress(t *testing.T, chain *Chain) { + var rawTxs [][]byte + rawTxs = append(rawTxs, deployContract(t, chain, 0)) + r := runTxs(chain, rawTxs, false) -func deployContract(t *testing.T, chain *Chain, i int) []byte { - // Deploy contract - Owner.sol - gasLimit := uint64(10000000000000) - gasPrice := big.NewInt(10000) + log := r[0].Log[1 : len(r[0].Log)-1] + logMap := make(map[string]interface{}) + err := json.Unmarshal([]byte(log), &logMap) + require.NoError(t, err) - sender := ethcmn.HexToAddress(chain.priv[i].PubKey().Address().String()) + logs := strings.Split(logMap["log"].(string), ";") + require.True(t, len(logs) == 3) + contractLog := strings.Split(logs[2], " ") + require.True(t, len(contractLog) == 4) + chain.ContractAddr = []byte(contractLog[3]) +} - bytecode := ethcmn.FromHex("608060405234801561001057600080fd5b50610217806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80631003e2d2146100465780632e64cec1146100625780636057361d14610080575b600080fd5b610060600480360381019061005b9190610105565b61009c565b005b61006a6100b7565b6040516100779190610141565b60405180910390f35b61009a60048036038101906100959190610105565b6100c0565b005b806000808282546100ad919061018b565b9250508190555050565b60008054905090565b8060008190555050565b600080fd5b6000819050919050565b6100e2816100cf565b81146100ed57600080fd5b50565b6000813590506100ff816100d9565b92915050565b60006020828403121561011b5761011a6100ca565b5b6000610129848285016100f0565b91505092915050565b61013b816100cf565b82525050565b60006020820190506101566000830184610132565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610196826100cf565b91506101a1836100cf565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156101d6576101d561015c565b5b82820190509291505056fea2646970667358221220318e29d6b4806f219eedd0cc861e82c13e28eb7f42161f2c780dc539b0e32b4e64736f6c634300080a0033") - msg := types.NewMsgEthereumTx(chain.seq[i], &sender, big.NewInt(0), gasLimit, gasPrice, bytecode) - err := msg.Sign(big.NewInt(3), chain.priv[i].ToECDSA()) +func createEthTx(t *testing.T, chain *Chain, addressIdx int) []byte { + amount, gasPrice, gasLimit := int64(1024), int64(2048), uint64(100000) + addrTo := ethcmn.BytesToAddress(chain.priv[addressIdx+1].PubKey().Address().Bytes()) + msg := evmtypes.NewMsgEthereumTx(chain.seq[addressIdx], &addrTo, big.NewInt(amount), gasLimit, big.NewInt(gasPrice), []byte{}) + chain.seq[addressIdx]++ + err := msg.Sign(chain.chainIdInt, chain.priv[addressIdx].ToECDSA()) require.NoError(t, err) - chain.seq[i]++ rawTx, err := rlp.EncodeToBytes(&msg) require.NoError(t, err) - return rawTx -} - -var contractJson = `{"abi":[{"inputs":[],"name":"add","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"retrieve","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"num","type":"uint256"}],"name":"store","outputs":[],"stateMutability":"nonpayable","type":"function"}],"bin":"608060405234801561001057600080fd5b50610205806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80632e64cec1146100465780634f2be91f146100645780636057361d1461006e575b600080fd5b61004e61008a565b60405161005b91906100d1565b60405180910390f35b61006c610093565b005b6100886004803603810190610083919061011d565b6100ae565b005b60008054905090565b60016000808282546100a59190610179565b92505081905550565b8060008190555050565b6000819050919050565b6100cb816100b8565b82525050565b60006020820190506100e660008301846100c2565b92915050565b600080fd5b6100fa816100b8565b811461010557600080fd5b50565b600081359050610117816100f1565b92915050565b600060208284031215610133576101326100ec565b5b600061014184828501610108565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610184826100b8565b915061018f836100b8565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156101c4576101c361014a565b5b82820190509291505056fea2646970667358221220742b7232e733bee3592cb9e558bdae3fbd0006bcbdba76abc47b6020744037b364736f6c634300080a0033"}` -type CompiledContract struct { - ABI abi.ABI - Bin string -} - -func UnmarshalContract(t *testing.T) *CompiledContract { - cc := new(CompiledContract) - err := json.Unmarshal([]byte(contractJson), cc) - require.NoError(t, err) - return cc + return rawTx } -func callContract(t *testing.T, chain *Chain, i int) []byte { - gasLimit := uint64(10000000000000) - gasPrice := big.NewInt(10000) - //to := ethcmn.HexToAddress(chain.priv[i].PubKey().Address().String()) - to := ethcmn.BytesToAddress(chain.ContractAddr) - cc := UnmarshalContract(t) - data, err := cc.ABI.Pack("add") - require.NoError(t, err) - msg := types.NewMsgEthereumTx(chain.seq[i], &to, big.NewInt(0), gasLimit, gasPrice, data) - err = msg.Sign(big.NewInt(3), chain.priv[i].ToECDSA()) +func createFailedEthTx(t *testing.T, chain *Chain, addressIdx int) []byte { + amount, gasPrice, gasLimit := int64(1024), int64(2048), uint64(1) + addrTo := ethcmn.BytesToAddress(chain.priv[addressIdx+1].PubKey().Address().Bytes()) + msg := evmtypes.NewMsgEthereumTx(chain.seq[addressIdx], &addrTo, big.NewInt(amount), gasLimit, big.NewInt(gasPrice), []byte{}) + chain.seq[addressIdx]++ + err := msg.Sign(chain.chainIdInt, chain.priv[addressIdx].ToECDSA()) require.NoError(t, err) - chain.seq[i]++ rawTx, err := rlp.EncodeToBytes(&msg) require.NoError(t, err) + return rawTx } -func createCosmosTx(t *testing.T, chain *Chain, i int) []byte { - msg := types4.NewMsgTokenSend(chain.addr[i], chain.addr[i+1], sdk.Coins{sdk.NewInt64Coin("okt", 10)}) +func createTokenSendTx(t *testing.T, chain *Chain, i int) []byte { + msg := tokentypes.NewMsgTokenSend(chain.addr[i], chain.addr[i+1], sdk.Coins{sdk.NewInt64Coin("okt", 10)}) tx := helpers.GenTx( []sdk.Msg{msg}, @@ -176,11 +152,11 @@ func createCosmosTx(t *testing.T, chain *Chain, i int) []byte { return txBytes } -func runtxs(chain *Chain, rawTxs [][]byte, isParalle bool) []*abci.ResponseDeliverTx { +func runTxs(chain *Chain, rawTxs [][]byte, isParallel bool) []*abci.ResponseDeliverTx { header := abci.Header{Height: chain.app.LastBlockHeight() + 1, ChainID: chain.chainIdStr} chain.app.BaseApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - ret := []*abci.ResponseDeliverTx{} - if isParalle { + var ret []*abci.ResponseDeliverTx + if isParallel { ret = chain.app.BaseApp.ParallelTxs(rawTxs, false) } else { for _, tx := range rawTxs { @@ -194,59 +170,52 @@ func runtxs(chain *Chain, rawTxs [][]byte, isParalle bool) []*abci.ResponseDeliv return ret } -func DeployContractAndGetContractAddress(t *testing.T, chain *Chain) { - rawTxs := [][]byte{} - rawTxs = append(rawTxs, deployContract(t, chain, 0)) - r := runtxs(chain, rawTxs, false) - - for _, e := range r[0].Events { - for _, v := range e.Attributes { - if string(v.Key) == "recipient" { - chain.ContractAddr = v.Value - } - } - } -} +func TestParallelTxs(t *testing.T) { -func TestParalledTxs(t *testing.T) { env := new(Env) - accountNum := 10 env.priv = make([]ethsecp256k1.PrivKey, 10) env.addr = make([]sdk.AccAddress, 10) - for i := 0; i < accountNum; i++ { + for i := 0; i < 10; i++ { priv, _ := ethsecp256k1.GenerateKey() addr := sdk.AccAddress(priv.PubKey().Address()) env.priv[i] = priv env.addr[i] = addr } - chainA, chainB := NewChain(env), NewChain(env) - //deploy contract on chainA and chainB DeployContractAndGetContractAddress(t, chainA) DeployContractAndGetContractAddress(t, chainB) testCases := []struct { - name string - malleate func(t *testing.T, chain *Chain, isParallel bool) ([]byte, []byte) + title string + executeTxs func(t *testing.T, chain *Chain, isParallel bool) ([]byte, []byte) }{ { - "five txs one group:a->b b->c c->d d->e e->f", - func(t *testing.T, chain *Chain, isParalled bool) ([]byte, []byte) { + "five evm txs, one group:a->b b->c c->d d->e e->f", + func(t *testing.T, chain *Chain, isParallel bool) ([]byte, []byte) { - rawTxs := [][]byte{} + var rawTxs [][]byte for i := 0; i < 5; i++ { rawTxs = append(rawTxs, createEthTx(t, chain, i)) } + ret := runTxs(chain, rawTxs, isParallel) + return resultHash(ret), chain.app.BaseApp.LastCommitID().Hash + }, + }, + { + "five failed evm txs, one group:a->b b->c c->d d->e e->f", + func(t *testing.T, chain *Chain, isParallel bool) ([]byte, []byte) { - header := abci.Header{Height: chain.app.LastBlockHeight() + 1, ChainID: chain.chainIdStr} - chain.app.BaseApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - ret := runtxs(chain, rawTxs, isParalled) + var rawTxs [][]byte + for i := 0; i < 5; i++ { + rawTxs = append(rawTxs, createFailedEthTx(t, chain, i)) + } + ret := runTxs(chain, rawTxs, isParallel) return resultHash(ret), chain.app.BaseApp.LastCommitID().Hash }, }, { - "five txs two group, no conflict:a->b b->c / d->e e->f f->g", - func(t *testing.T, chain *Chain, isParalled bool) ([]byte, []byte) { + "five evm txs, two group:a->b b->c / d->e e->f f->g", + func(t *testing.T, chain *Chain, isParallel bool) ([]byte, []byte) { rawTxs := [][]byte{} //one group 3txs for i := 0; i < 3; i++ { @@ -256,97 +225,201 @@ func TestParalledTxs(t *testing.T) { for i := 8; i > 6; i-- { rawTxs = append(rawTxs, createEthTx(t, chain, i)) } - ret := runtxs(chain, rawTxs, isParalled) + ret := runTxs(chain, rawTxs, isParallel) return resultHash(ret), chain.app.BaseApp.LastCommitID().Hash }, }, { - "five txs two group, has conflict", - func(t *testing.T, chain *Chain, isParalled bool) ([]byte, []byte) { + "five failed evm txs, two group:a->b b->c / d->e e->f f->g", + func(t *testing.T, chain *Chain, isParallel bool) ([]byte, []byte) { rawTxs := [][]byte{} + //one group 3txs + for i := 0; i < 3; i++ { + rawTxs = append(rawTxs, createFailedEthTx(t, chain, i)) + } + //one group 2txs + for i := 8; i > 6; i-- { + rawTxs = append(rawTxs, createFailedEthTx(t, chain, i)) + } + ret := runTxs(chain, rawTxs, isParallel) + return resultHash(ret), chain.app.BaseApp.LastCommitID().Hash + }, + }, + { + "three evm txs and two failed evm txs, two group:a->b b->c / d->e e->f f->g", + func(t *testing.T, chain *Chain, isParallel bool) ([]byte, []byte) { + rawTxs := [][]byte{} //one group 3txs for i := 0; i < 3; i++ { - rawTxs = append(rawTxs, callContract(t, chain, i)) + rawTxs = append(rawTxs, createFailedEthTx(t, chain, i)) } - ////one group 2txs + //one group 2txs for i := 8; i > 6; i-- { rawTxs = append(rawTxs, createEthTx(t, chain, i)) } - ret := runtxs(chain, rawTxs, isParalled) + ret := runTxs(chain, rawTxs, isParallel) return resultHash(ret), chain.app.BaseApp.LastCommitID().Hash }, }, { - "five txs one group with cosmos tx", - func(t *testing.T, chain *Chain, isParalled bool) ([]byte, []byte) { + "three evm txs and two failed evm txs, two group:a->b b->c / d->e e->f f->g", + func(t *testing.T, chain *Chain, isParallel bool) ([]byte, []byte) { rawTxs := [][]byte{} //one group 3txs - for i := 0; i < 2; i++ { - rawTxs = append(rawTxs, createEthTx(t, chain, i)) + for i := 0; i < 3; i++ { + rawTxs = append(rawTxs, createFailedEthTx(t, chain, i)) } - //cosmostx - rawTxs = append(rawTxs, createCosmosTx(t, chain, 2)) //one group 2txs - for i := 3; i < 5; i++ { + for i := 8; i > 6; i-- { + rawTxs = append(rawTxs, createEthTx(t, chain, i)) + } + ret := runTxs(chain, rawTxs, isParallel) + + return resultHash(ret), chain.app.BaseApp.LastCommitID().Hash + }, + }, + { + "three contract txs and two normal evm txs, two group", + func(t *testing.T, chain *Chain, isParallel bool) ([]byte, []byte) { + var rawTxs [][]byte + + //one group 3txs + for i := 0; i < 3; i++ { + rawTxs = append(rawTxs, callContract(t, chain, i)) + } + ////one group 2txs + for i := 8; i > 6; i-- { rawTxs = append(rawTxs, createEthTx(t, chain, i)) } - ret := runtxs(chain, rawTxs, isParalled) + ret := runTxs(chain, rawTxs, isParallel) return resultHash(ret), chain.app.BaseApp.LastCommitID().Hash }, }, { - "five txs two group, no conflict with cosmos tx", - func(t *testing.T, chain *Chain, isParalle bool) ([]byte, []byte) { + "five txs one group with cosmos tx", + func(t *testing.T, chain *Chain, isParallel bool) ([]byte, []byte) { rawTxs := [][]byte{} - //one group 3txs(2eth and 1 cosmos) + //one group 3txs for i := 0; i < 2; i++ { rawTxs = append(rawTxs, createEthTx(t, chain, i)) } - //cosmos tx - rawTxs = append(rawTxs, createCosmosTx(t, chain, 2)) + //cosmostx + rawTxs = append(rawTxs, createTokenSendTx(t, chain, 7)) //one group 2txs - for i := 8; i > 6; i-- { + for i := 3; i < 5; i++ { rawTxs = append(rawTxs, createEthTx(t, chain, i)) } - ret := runtxs(chain, rawTxs, isParalle) + ret := runTxs(chain, rawTxs, isParallel) return resultHash(ret), chain.app.BaseApp.LastCommitID().Hash }, }, { "five txs two group, has conflict with cosmos tx", - func(t *testing.T, chain *Chain, isParalled bool) ([]byte, []byte) { + func(t *testing.T, chain *Chain, isParallel bool) ([]byte, []byte) { rawTxs := [][]byte{} - //one group 3txs:2 evm tx with conflict, one cosmos tx + //one group 2txs for i := 0; i < 2; i++ { rawTxs = append(rawTxs, callContract(t, chain, i)) } - rawTxs = append(rawTxs, createCosmosTx(t, chain, 2)) - ////one group 2txs - for i := 8; i > 6; i-- { - rawTxs = append(rawTxs, createCosmosTx(t, chain, i)) + ////one group 3txs + rawTxs = append(rawTxs, createTokenSendTx(t, chain, 8)) + for i := 8; i < 6; i++ { + rawTxs = append(rawTxs, createEthTx(t, chain, i)) } - ret := runtxs(chain, rawTxs, isParalled) + ret := runTxs(chain, rawTxs, isParallel) return resultHash(ret), chain.app.BaseApp.LastCommitID().Hash }, }, } + for _, tc := range testCases { - resultHashA, appHashA := tc.malleate(t, chainA, true) - resultHashB, appHashB := tc.malleate(t, chainB, false) - require.True(t, reflect.DeepEqual(resultHashA, resultHashB)) - require.True(t, reflect.DeepEqual(appHashA, appHashB)) + t.Run(tc.title, func(t *testing.T) { + resultHashA, appHashA := tc.executeTxs(t, chainA, true) + resultHashB, appHashB := tc.executeTxs(t, chainB, false) + require.True(t, reflect.DeepEqual(resultHashA, resultHashB)) + require.True(t, reflect.DeepEqual(appHashA, appHashB)) + }) } - } func resultHash(txs []*abci.ResponseDeliverTx) []byte { - results := types2.NewResults(txs) + results := tmtypes.NewResults(txs) return results.Hash() } + +//contract Storage { +//uint256 number; +///** +// * @dev Store value in variable +// * @param num value to store +// */ +//function store(uint256 num) public { +//number = num; +//} +//function add() public { +//number += 1; +//} +///** +// * @dev Return value +// * @return value of 'number' +// */ +//function retrieve() public view returns (uint256){ +//return number; +//} +//} +var abiStr = `[{"inputs":[{"internalType":"uint256","name":"num","type":"uint256"}],"name":"add","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"retrieve","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"num","type":"uint256"}],"name":"store","outputs":[],"stateMutability":"nonpayable","type":"function"}]` + +func deployContract(t *testing.T, chain *Chain, i int) []byte { + // Deploy contract - Owner.sol + gasLimit := uint64(30000000) + gasPrice := big.NewInt(100000000) + + //sender := ethcmn.HexToAddress(chain.priv[i].PubKey().Address().String()) + + bytecode := ethcmn.FromHex("608060405234801561001057600080fd5b50610217806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80631003e2d2146100465780632e64cec1146100625780636057361d14610080575b600080fd5b610060600480360381019061005b9190610105565b61009c565b005b61006a6100b7565b6040516100779190610141565b60405180910390f35b61009a60048036038101906100959190610105565b6100c0565b005b806000808282546100ad919061018b565b9250508190555050565b60008054905090565b8060008190555050565b600080fd5b6000819050919050565b6100e2816100cf565b81146100ed57600080fd5b50565b6000813590506100ff816100d9565b92915050565b60006020828403121561011b5761011a6100ca565b5b6000610129848285016100f0565b91505092915050565b61013b816100cf565b82525050565b60006020820190506101566000830184610132565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610196826100cf565b91506101a1836100cf565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156101d6576101d561015c565b5b82820190509291505056fea2646970667358221220318e29d6b4806f219eedd0cc861e82c13e28eb7f42161f2c780dc539b0e32b4e64736f6c634300080a0033") + msg := evmtypes.NewMsgEthereumTx(chain.seq[i], nil, big.NewInt(0), gasLimit, gasPrice, bytecode) + err := msg.Sign(big.NewInt(3), chain.priv[i].ToECDSA()) + require.NoError(t, err) + chain.seq[i]++ + rawTx, err := rlp.EncodeToBytes(&msg) + require.NoError(t, err) + return rawTx +} + +var contractJson = `{"abi":[{"inputs":[],"name":"add","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"retrieve","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"num","type":"uint256"}],"name":"store","outputs":[],"stateMutability":"nonpayable","type":"function"}],"bin":"608060405234801561001057600080fd5b50610205806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80632e64cec1146100465780634f2be91f146100645780636057361d1461006e575b600080fd5b61004e61008a565b60405161005b91906100d1565b60405180910390f35b61006c610093565b005b6100886004803603810190610083919061011d565b6100ae565b005b60008054905090565b60016000808282546100a59190610179565b92505081905550565b8060008190555050565b6000819050919050565b6100cb816100b8565b82525050565b60006020820190506100e660008301846100c2565b92915050565b600080fd5b6100fa816100b8565b811461010557600080fd5b50565b600081359050610117816100f1565b92915050565b600060208284031215610133576101326100ec565b5b600061014184828501610108565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610184826100b8565b915061018f836100b8565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156101c4576101c361014a565b5b82820190509291505056fea2646970667358221220742b7232e733bee3592cb9e558bdae3fbd0006bcbdba76abc47b6020744037b364736f6c634300080a0033"}` + +type CompiledContract struct { + ABI abi.ABI + Bin string +} + +func UnmarshalContract(t *testing.T) *CompiledContract { + cc := new(CompiledContract) + err := json.Unmarshal([]byte(contractJson), cc) + require.NoError(t, err) + return cc +} + +func callContract(t *testing.T, chain *Chain, i int) []byte { + gasLimit := uint64(30000000) + gasPrice := big.NewInt(100000000) + //to := ethcmn.HexToAddress(chain.priv[i].PubKey().Address().String()) + to := ethcmn.BytesToAddress(chain.ContractAddr) + cc := UnmarshalContract(t) + data, err := cc.ABI.Pack("add") + require.NoError(t, err) + msg := evmtypes.NewMsgEthereumTx(chain.seq[i], &to, big.NewInt(0), gasLimit, gasPrice, data) + err = msg.Sign(big.NewInt(3), chain.priv[i].ToECDSA()) + require.NoError(t, err) + chain.seq[i]++ + rawTx, err := rlp.EncodeToBytes(&msg) + require.NoError(t, err) + return rawTx +} diff --git a/app/app_test.go b/app/app_test.go index cdbc799926..898301b443 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -7,35 +7,30 @@ import ( ethcommon "github.com/ethereum/go-ethereum/common" ethcrypto "github.com/ethereum/go-ethereum/crypto" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "github.com/okex/exchain/app/crypto/ethsecp256k1" + "github.com/okex/exchain/libs/cosmos-sdk/codec" cosmossdk "github.com/okex/exchain/libs/cosmos-sdk/types" + "github.com/okex/exchain/libs/cosmos-sdk/x/auth" authclient "github.com/okex/exchain/libs/cosmos-sdk/x/auth/client/utils" + authtypes "github.com/okex/exchain/libs/cosmos-sdk/x/auth/types" + "github.com/okex/exchain/libs/cosmos-sdk/x/upgrade" + abci "github.com/okex/exchain/libs/tendermint/abci/types" + abcitypes "github.com/okex/exchain/libs/tendermint/abci/types" + "github.com/okex/exchain/libs/tendermint/crypto" "github.com/okex/exchain/libs/tendermint/global" + "github.com/okex/exchain/libs/tendermint/libs/log" tendertypes "github.com/okex/exchain/libs/tendermint/types" - "github.com/okex/exchain/x/distribution/keeper" - evmtypes "github.com/okex/exchain/x/evm/types" - - "github.com/okex/exchain/libs/cosmos-sdk/x/upgrade" + dbm "github.com/okex/exchain/libs/tm-db" "github.com/okex/exchain/x/dex" distr "github.com/okex/exchain/x/distribution" + "github.com/okex/exchain/x/distribution/keeper" + evmtypes "github.com/okex/exchain/x/evm/types" "github.com/okex/exchain/x/farm" - "github.com/okex/exchain/x/params" - - "github.com/stretchr/testify/require" - - abci "github.com/okex/exchain/libs/tendermint/abci/types" - "github.com/okex/exchain/libs/tendermint/libs/log" - dbm "github.com/okex/exchain/libs/tm-db" - - "github.com/okex/exchain/libs/cosmos-sdk/codec" - - "github.com/okex/exchain/libs/cosmos-sdk/x/auth" - authtypes "github.com/okex/exchain/libs/cosmos-sdk/x/auth/types" - abcitypes "github.com/okex/exchain/libs/tendermint/abci/types" - "github.com/okex/exchain/libs/tendermint/crypto" "github.com/okex/exchain/x/gov" + "github.com/okex/exchain/x/params" ) var ( diff --git a/app/test_helpers.go b/app/test_helpers.go index 989c903ea7..b5eecfa31e 100644 --- a/app/test_helpers.go +++ b/app/test_helpers.go @@ -5,7 +5,10 @@ import ( "github.com/okex/exchain/libs/cosmos-sdk/codec" sdk "github.com/okex/exchain/libs/cosmos-sdk/types" + authtypes "github.com/okex/exchain/libs/cosmos-sdk/x/auth" + authexported "github.com/okex/exchain/libs/cosmos-sdk/x/auth/exported" abci "github.com/okex/exchain/libs/tendermint/abci/types" + abcitypes "github.com/okex/exchain/libs/tendermint/abci/types" "github.com/okex/exchain/libs/tendermint/libs/log" "github.com/okex/exchain/libs/tendermint/types" dbm "github.com/okex/exchain/libs/tm-db" @@ -54,3 +57,39 @@ func Setup(isCheckTx bool, options ...Option) *OKExChainApp { return app } + +func SetupWithGenesisAccounts(isCheckTx bool, genAccs []authexported.GenesisAccount, options ...Option) *OKExChainApp { + viper.Set(sdk.FlagDBBackend, string(dbm.MemDBBackend)) + types.DBBackend = string(dbm.MemDBBackend) + db := dbm.NewMemDB() + app := NewOKExChainApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, 0) + + if !isCheckTx { + setupOption := &SetupOption{chainId: ""} + for _, opt := range options { + opt(setupOption) + } + // init chain must be called to stop deliverState from being nil + genesisState := NewDefaultGenesisState() + authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) + genesisState[authtypes.ModuleName] = app.Codec().MustMarshalJSON(authGenesis) + stateBytes, err := codec.MarshalJSONIndent(app.Codec(), genesisState) + if err != nil { + panic(err) + } + + // Initialize the chain + app.InitChain( + abci.RequestInitChain{ + Validators: []abci.ValidatorUpdate{}, + AppStateBytes: stateBytes, + ChainId: setupOption.chainId, + }, + ) + + app.Commit(abcitypes.RequestCommit{}) + app.BeginBlock(abci.RequestBeginBlock{Header: abcitypes.Header{Height: app.LastBlockHeight() + 1}}) + } + + return app +} diff --git a/libs/tendermint/types/milestone.go b/libs/tendermint/types/milestone.go index 0b6762761c..85e7f78195 100644 --- a/libs/tendermint/types/milestone.go +++ b/libs/tendermint/types/milestone.go @@ -201,6 +201,10 @@ func UnittestOnlySetMilestoneVenus1Height(h int64) { milestoneVenus1Height = h } +func UnittestOnlySetMilestoneVenus6Height(h int64) { + milestoneVenus6Height = h +} + func GetVenus1Height() int64 { return milestoneVenus1Height }