From 93ffc32632fb6e787012506056f394eb935e2859 Mon Sep 17 00:00:00 2001 From: envestcc Date: Thu, 2 Nov 2023 12:42:16 +0800 Subject: [PATCH] update tests --- action/protocol/execution/evm/evm_test.go | 7 +++++++ action/protocol/execution/protocol_test.go | 12 +++++++++--- action/protocol/poll/protocol_test.go | 3 +++ api/coreservice_test.go | 3 ++- api/serverV2_integrity_test.go | 4 ++-- blockchain/integrity/benchmark_test.go | 2 +- blockchain/integrity/integrity_test.go | 18 +++++++++++------- e2etest/bigint_test.go | 5 ++++- e2etest/contract_staking_test.go | 2 +- e2etest/sgd_registry_test.go | 2 +- e2etest/staking_test.go | 3 +++ gasstation/gasstattion_test.go | 5 +++-- state/factory/factory_test.go | 3 +++ .../internal/client/client_test.go | 3 ++- 14 files changed, 52 insertions(+), 20 deletions(-) diff --git a/action/protocol/execution/evm/evm_test.go b/action/protocol/execution/evm/evm_test.go index e8781b7ab2..4fc48c9492 100644 --- a/action/protocol/execution/evm/evm_test.go +++ b/action/protocol/execution/evm/evm_test.go @@ -11,6 +11,7 @@ import ( "math" "math/big" "testing" + "time" "github.com/ethereum/go-ethereum/core/vm" "github.com/golang/mock/gomock" @@ -64,6 +65,9 @@ func TestExecuteContractFailure(t *testing.T) { GetBlockHash: func(uint64) (hash.Hash256, error) { return hash.ZeroHash256, nil }, + GetBlockTime: func(uint64) (time.Time, error) { + return time.Time{}, nil + }, DepositGasFunc: func(context.Context, protocol.StateManager, address.Address, *big.Int, *big.Int) (*action.TransactionLog, error) { return nil, nil }, @@ -262,6 +266,9 @@ func TestConstantinople(t *testing.T) { GetBlockHash: func(uint64) (hash.Hash256, error) { return hash.ZeroHash256, nil }, + GetBlockTime: func(uint64) (time.Time, error) { + return time.Time{}, nil + }, }) stateDB, err := prepareStateDB(fCtx, sm) require.NoError(err) diff --git a/action/protocol/execution/protocol_test.go b/action/protocol/execution/protocol_test.go index d33fb28aa5..6f7e261913 100644 --- a/action/protocol/execution/protocol_test.go +++ b/action/protocol/execution/protocol_test.go @@ -15,6 +15,7 @@ import ( "math/big" "os" "testing" + "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -285,6 +286,9 @@ func readExecution( } ctx = evm.WithHelperCtx(ctx, evm.HelperContext{ GetBlockHash: dao.GetBlockHash, + GetBlockTime: func(uint64) (time.Time, error) { + return time.Time{}, nil + }, }) return sf.SimulateExecution(ctx, addr, exec) } @@ -457,7 +461,7 @@ func (sct *SmartContractTest) prepareBlockchain( r.NoError(reward.Register(registry)) r.NotNil(bc) - execution := NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + execution := NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, func(u uint64) (time.Time, error) { return time.Time{}, nil }) r.NoError(execution.Register(registry)) r.NoError(bc.Start(ctx)) @@ -607,7 +611,9 @@ func TestProtocol_Validate(t *testing.T) { require := require.New(t) p := NewProtocol(func(uint64) (hash.Hash256, error) { return hash.ZeroHash256, nil - }, rewarding.DepositGasWithSGD, nil) + }, rewarding.DepositGasWithSGD, nil, func(u uint64) (time.Time, error) { + return time.Time{}, nil + }) ex, err := action.NewExecution("2", uint64(1), big.NewInt(0), uint64(0), big.NewInt(0), make([]byte, 32684)) require.NoError(err) @@ -677,7 +683,7 @@ func TestProtocol_Handle(t *testing.T) { protocol.NewGenericValidator(sf, accountutil.AccountState), )), ) - exeProtocol := NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + exeProtocol := NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, func(u uint64) (time.Time, error) { return time.Time{}, nil }) require.NoError(exeProtocol.Register(registry)) require.NoError(bc.Start(ctx)) require.NotNil(bc) diff --git a/action/protocol/poll/protocol_test.go b/action/protocol/poll/protocol_test.go index 557ddd5e5a..67a130c913 100644 --- a/action/protocol/poll/protocol_test.go +++ b/action/protocol/poll/protocol_test.go @@ -45,6 +45,9 @@ func TestNewProtocol(t *testing.T) { func(uint64) (hash.Hash256, error) { return hash.ZeroHash256, nil }, + func(u uint64) (time.Time, error) { + return time.Time{}, nil + }, ) require.NoError(err) require.NotNil(p) diff --git a/api/coreservice_test.go b/api/coreservice_test.go index 6b1dd98384..61d55b1896 100644 --- a/api/coreservice_test.go +++ b/api/coreservice_test.go @@ -11,6 +11,7 @@ import ( "math/big" "strconv" "testing" + "time" "github.com/ethereum/go-ethereum/eth/tracers/logger" "github.com/golang/mock/gomock" @@ -174,7 +175,7 @@ func setupTestCoreService() (CoreService, blockchain.Blockchain, blockdao.BlockD opts := []Option{WithBroadcastOutbound(func(ctx context.Context, chainID uint32, msg proto.Message) error { return nil })} - svr, err := newCoreService(cfg.api, bc, nil, sf, dao, indexer, bfIndexer, ap, registry, opts...) + svr, err := newCoreService(cfg.api, bc, nil, sf, dao, indexer, bfIndexer, ap, registry, func(u uint64) (time.Time, error) { return time.Time{}, nil }, opts...) if err != nil { panic(err) } diff --git a/api/serverV2_integrity_test.go b/api/serverV2_integrity_test.go index ecb34ac756..589966709a 100644 --- a/api/serverV2_integrity_test.go +++ b/api/serverV2_integrity_test.go @@ -336,7 +336,7 @@ func setupChain(cfg testConfig) (blockchain.Blockchain, blockdao.BlockDAO, block }() acc := account.NewProtocol(rewarding.DepositGas) - evm := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + evm := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, func(u uint64) (time.Time, error) { return time.Time{}, nil }) p := poll.NewLifeLongDelegatesProtocol(cfg.genesis.Delegates) rolldposProtocol := rolldpos.NewProtocol( genesis.Default.NumCandidateDelegates, @@ -452,7 +452,7 @@ func createServerV2(cfg testConfig, needActPool bool) (*ServerV2, blockchain.Blo opts := []Option{WithBroadcastOutbound(func(ctx context.Context, chainID uint32, msg proto.Message) error { return nil })} - svr, err := NewServerV2(cfg.api, bc, nil, sf, dao, indexer, bfIndexer, ap, registry, opts...) + svr, err := NewServerV2(cfg.api, bc, nil, sf, dao, indexer, bfIndexer, ap, registry, func(u uint64) (time.Time, error) { return time.Time{}, nil }, opts...) if err != nil { return nil, nil, nil, nil, nil, nil, "", err } diff --git a/blockchain/integrity/benchmark_test.go b/blockchain/integrity/benchmark_test.go index a60fd41ad0..e884c7c4ce 100644 --- a/blockchain/integrity/benchmark_test.go +++ b/blockchain/integrity/benchmark_test.go @@ -277,7 +277,7 @@ func newChainInDB() (blockchain.Blockchain, actpool.ActPool, error) { if bc == nil { return nil, nil, errors.New("pointer is nil") } - ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, func(u uint64) (time.Time, error) { return time.Time{}, nil }) if err = ep.Register(registry); err != nil { return nil, nil, err } diff --git a/blockchain/integrity/integrity_test.go b/blockchain/integrity/integrity_test.go index dfb00c96e8..789fc1d10e 100644 --- a/blockchain/integrity/integrity_test.go +++ b/blockchain/integrity/integrity_test.go @@ -73,6 +73,10 @@ var ( _postGrPostStore *big.Int ) +func fakeGetBlockTime(height uint64) (time.Time, error) { + return time.Time{}, nil +} + func addTestingConstantinopleBlocks(bc blockchain.Blockchain, dao blockdao.BlockDAO, sf factory.Factory, ap actpool.ActPool) error { // Add block 1 priKey0 := identityset.PrivateKey(27) @@ -494,7 +498,7 @@ func TestCreateBlockchain(t *testing.T) { protocol.NewGenericValidator(sf, accountutil.AccountState), )), ) - ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, fakeGetBlockTime) require.NoError(ep.Register(registry)) rewardingProtocol := rewarding.NewProtocol(cfg.Genesis.Rewarding) require.NoError(rewardingProtocol.Register(registry)) @@ -547,7 +551,7 @@ func TestGetBlockHash(t *testing.T) { protocol.NewGenericValidator(sf, accountutil.AccountState), )), ) - ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, fakeGetBlockTime) require.NoError(ep.Register(registry)) rewardingProtocol := rewarding.NewProtocol(cfg.Genesis.Rewarding) require.NoError(rewardingProtocol.Register(registry)) @@ -710,7 +714,7 @@ func TestBlockchain_MintNewBlock(t *testing.T) { protocol.NewGenericValidator(sf, accountutil.AccountState), )), ) - ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, fakeGetBlockTime) require.NoError(t, ep.Register(registry)) rewardingProtocol := rewarding.NewProtocol(cfg.Genesis.Rewarding) require.NoError(t, rewardingProtocol.Register(registry)) @@ -786,7 +790,7 @@ func TestBlockchain_MintNewBlock_PopAccount(t *testing.T) { ) rp := rolldpos.NewProtocol(cfg.Genesis.NumCandidateDelegates, cfg.Genesis.NumDelegates, cfg.Genesis.NumSubEpochs) require.NoError(t, rp.Register(registry)) - ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, fakeGetBlockTime) require.NoError(t, ep.Register(registry)) rewardingProtocol := rewarding.NewProtocol(cfg.Genesis.Rewarding) require.NoError(t, rewardingProtocol.Register(registry)) @@ -885,7 +889,7 @@ func TestConstantinople(t *testing.T) { protocol.NewGenericValidator(sf, accountutil.AccountState), )), ) - ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, fakeGetBlockTime) require.NoError(ep.Register(registry)) rewardingProtocol := rewarding.NewProtocol(cfg.Genesis.Rewarding) require.NoError(rewardingProtocol.Register(registry)) @@ -1135,7 +1139,7 @@ func TestLoadBlockchainfromDB(t *testing.T) { protocol.NewGenericValidator(sf, accountutil.AccountState), )), ) - ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, fakeGetBlockTime) require.NoError(ep.Register(registry)) require.NoError(bc.Start(ctx)) @@ -1989,7 +1993,7 @@ func newChain(t *testing.T, stateTX bool) (blockchain.Blockchain, factory.Factor )), ) require.NotNil(bc) - ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, fakeGetBlockTime) require.NoError(ep.Register(registry)) require.NoError(bc.Start(context.Background())) diff --git a/e2etest/bigint_test.go b/e2etest/bigint_test.go index ead36d90a4..8bd5b8e1de 100644 --- a/e2etest/bigint_test.go +++ b/e2etest/bigint_test.go @@ -9,6 +9,7 @@ import ( "context" "math/big" "testing" + "time" "github.com/iotexproject/go-pkgs/crypto" "github.com/stretchr/testify/require" @@ -39,6 +40,8 @@ const ( _executorPriKey = "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1" ) +func fakeGetBlockTime(uint64) (time.Time, error) { return time.Time{}, nil } + func TestTransfer_Negative(t *testing.T) { r := require.New(t) ctx := context.Background() @@ -110,7 +113,7 @@ func prepareBlockchain(ctx context.Context, _executor string, r *require.Asserti reward := rewarding.NewProtocol(cfg.Genesis.Rewarding) r.NoError(reward.Register(registry)) - ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, fakeGetBlockTime) r.NoError(ep.Register(registry)) r.NoError(bc.Start(ctx)) ctx = genesis.WithGenesisContext(ctx, cfg.Genesis) diff --git a/e2etest/contract_staking_test.go b/e2etest/contract_staking_test.go index 3dbade68ea..765432680c 100644 --- a/e2etest/contract_staking_test.go +++ b/e2etest/contract_staking_test.go @@ -1985,7 +1985,7 @@ func prepareContractStakingBlockchain(ctx context.Context, cfg config.Config, r // r.NoError(reward.Register(registry)) r.NotNil(bc) - execution := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + execution := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, fakeGetBlockTime) r.NoError(execution.Register(registry)) r.NoError(bc.Start(ctx)) diff --git a/e2etest/sgd_registry_test.go b/e2etest/sgd_registry_test.go index efa2c05ebd..a2793d2bee 100644 --- a/e2etest/sgd_registry_test.go +++ b/e2etest/sgd_registry_test.go @@ -78,7 +78,7 @@ func TestSGDRegistry(t *testing.T) { reward := rewarding.NewProtocol(cfg.Genesis.Rewarding) r.NoError(reward.Register(registry)) - ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + ep := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGasWithSGD, nil, fakeGetBlockTime) r.NoError(ep.Register(registry)) r.NoError(bc.Start(ctx)) ctx = genesis.WithGenesisContext(ctx, cfg.Genesis) diff --git a/e2etest/staking_test.go b/e2etest/staking_test.go index 1c1a1b58bc..4eed0f2579 100644 --- a/e2etest/staking_test.go +++ b/e2etest/staking_test.go @@ -124,6 +124,9 @@ func TestStakingContract(t *testing.T) { ctx = evm.WithHelperCtx(ctx, evm.HelperContext{ GetBlockHash: dao.GetBlockHash, + GetBlockTime: func(u uint64) (time.Time, error) { + return time.Time{}, nil + }, }) data, _, err := sf.SimulateExecution(ctx, addr, ex) diff --git a/gasstation/gasstattion_test.go b/gasstation/gasstattion_test.go index e89bc99194..dd683e8e47 100644 --- a/gasstation/gasstattion_test.go +++ b/gasstation/gasstattion_test.go @@ -10,6 +10,7 @@ import ( "fmt" "math/big" "testing" + "time" "github.com/stretchr/testify/require" @@ -81,7 +82,7 @@ func TestSuggestGasPriceForUserAction(t *testing.T) { protocol.NewGenericValidator(sf, accountutil.AccountState), )), ) - ep := execution.NewProtocol(blkMemDao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + ep := execution.NewProtocol(blkMemDao.GetBlockHash, rewarding.DepositGasWithSGD, nil, func(u uint64) (time.Time, error) { return time.Time{}, nil }) require.NoError(t, ep.Register(registry)) rewardingProtocol := rewarding.NewProtocol(cfg.Genesis.Rewarding) require.NoError(t, rewardingProtocol.Register(registry)) @@ -157,7 +158,7 @@ func TestSuggestGasPriceForSystemAction(t *testing.T) { protocol.NewGenericValidator(sf, accountutil.AccountState), )), ) - ep := execution.NewProtocol(blkMemDao.GetBlockHash, rewarding.DepositGasWithSGD, nil) + ep := execution.NewProtocol(blkMemDao.GetBlockHash, rewarding.DepositGasWithSGD, nil, func(u uint64) (time.Time, error) { return time.Time{}, nil }) require.NoError(t, ep.Register(registry)) rewardingProtocol := rewarding.NewProtocol(cfg.Genesis.Rewarding) require.NoError(t, rewardingProtocol.Register(registry)) diff --git a/state/factory/factory_test.go b/state/factory/factory_test.go index 9f15e4f7b3..1e90eff463 100644 --- a/state/factory/factory_test.go +++ b/state/factory/factory_test.go @@ -1228,6 +1228,9 @@ func testSimulateExecution(ctx context.Context, sf Factory, t *testing.T) { GetBlockHash: func(uint64) (hash.Hash256, error) { return hash.ZeroHash256, nil }, + GetBlockTime: func(u uint64) (time.Time, error) { + return time.Time{}, nil + }, }) _, _, err = sf.SimulateExecution(ctx, addr, ex) require.NoError(err) diff --git a/tools/actioninjector.v2/internal/client/client_test.go b/tools/actioninjector.v2/internal/client/client_test.go index 575b3937d3..186ad37dac 100644 --- a/tools/actioninjector.v2/internal/client/client_test.go +++ b/tools/actioninjector.v2/internal/client/client_test.go @@ -5,6 +5,7 @@ import ( "fmt" "math/big" "testing" + "time" "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" @@ -84,7 +85,7 @@ func TestClient(t *testing.T) { require.NoError(err) bfIndexer, err := blockindex.NewBloomfilterIndexer(db.NewMemKVStore(), cfg.Indexer) require.NoError(err) - apiServer, err := api.NewServerV2(cfg.API, bc, nil, sf, nil, indexer, bfIndexer, ap, nil, newOption) + apiServer, err := api.NewServerV2(cfg.API, bc, nil, sf, nil, indexer, bfIndexer, ap, nil, func(u uint64) (time.Time, error) { return time.Time{}, nil }, newOption) require.NoError(err) require.NoError(apiServer.Start(ctx)) // test New()