From fa2b341c9aa5cab26953a7d4c3f079756853eb50 Mon Sep 17 00:00:00 2001 From: Jianrong Date: Sun, 29 Aug 2021 17:14:10 +1000 Subject: [PATCH] resolving broken eth tests --- accounts/abi/bind/backends/simulated.go | 5 +++-- accounts/abi/bind/bind_test.go | 22 +++++++++++----------- accounts/abi/bind/util_test.go | 2 +- cmd/puppeth/wizard_genesis.go | 2 +- consensus/XDPoS/XDPoS.go | 8 ++++---- contracts/blocksigner/blocksigner_test.go | 2 +- contracts/chequebook/cheque_test.go | 2 +- contracts/ens/ens_test.go | 2 +- contracts/randomize/randomize_test.go | 4 ++-- contracts/utils_test.go | 2 +- contracts/validator/validator_test.go | 4 ++-- core/blockchain.go | 3 ++- params/config.go | 7 +++---- tests/end_to_end/block_signer_test.go | 6 +++--- 14 files changed, 36 insertions(+), 35 deletions(-) diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 9d8889c3e7..228b184ebe 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -65,10 +65,10 @@ type SimulatedBackend struct { } // XDC simulated backend for testing purpose. -func NewXDCSimulatedBackend(alloc core.GenesisAlloc) *SimulatedBackend { +func NewXDCSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend { database := ethdb.NewMemDatabase() genesis := core.Genesis{ - GasLimit: 10000000, // need this big, support initial smart contract + GasLimit: gasLimit, // need this big, support initial smart contract Config: params.TestXDPoSMockChainConfig, Alloc: alloc, ExtraData: append(make([]byte, 32), make([]byte, 65)...), @@ -82,6 +82,7 @@ func NewXDCSimulatedBackend(alloc core.GenesisAlloc) *SimulatedBackend { config: genesis.Config, events: filters.NewEventSystem(new(event.TypeMux), &filterBackend{database, blockchain}, false), } + blockchain.Client = backend backend.rollback() return backend } diff --git a/accounts/abi/bind/bind_test.go b/accounts/abi/bind/bind_test.go index 46e0b38d00..55b1f3259e 100644 --- a/accounts/abi/bind/bind_test.go +++ b/accounts/abi/bind/bind_test.go @@ -258,7 +258,7 @@ var bindTests = []struct { // Generate a new random account and a funded simulator key, _ := crypto.GenerateKey() auth := bind.NewKeyedTransactor(key) - sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) + sim := backends.NewXDCSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) // Deploy an interaction tester contract and call a transaction on it _, _, interactor, err := DeployInteractor(auth, sim, "Deploy string") @@ -307,7 +307,7 @@ var bindTests = []struct { // Generate a new random account and a funded simulator key, _ := crypto.GenerateKey() auth := bind.NewKeyedTransactor(key) - sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) + sim := backends.NewXDCSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) // Deploy a tuple tester contract and execute a structured call on it _, _, getter, err := DeployGetter(auth, sim) @@ -347,7 +347,7 @@ var bindTests = []struct { // Generate a new random account and a funded simulator key, _ := crypto.GenerateKey() auth := bind.NewKeyedTransactor(key) - sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) + sim := backends.NewXDCSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) // Deploy a tuple tester contract and execute a structured call on it _, _, tupler, err := DeployTupler(auth, sim) @@ -399,7 +399,7 @@ var bindTests = []struct { // Generate a new random account and a funded simulator key, _ := crypto.GenerateKey() auth := bind.NewKeyedTransactor(key) - sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) + sim := backends.NewXDCSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) // Deploy a slice tester contract and execute a n array call on it _, _, slicer, err := DeploySlicer(auth, sim) @@ -441,7 +441,7 @@ var bindTests = []struct { // Generate a new random account and a funded simulator key, _ := crypto.GenerateKey() auth := bind.NewKeyedTransactor(key) - sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) + sim := backends.NewXDCSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) // Deploy a default method invoker contract and execute its default method _, _, defaulter, err := DeployDefaulter(auth, sim) @@ -479,7 +479,7 @@ var bindTests = []struct { `, ` // Create a simulator and wrap a non-deployed contract - sim := backends.NewSimulatedBackend(nil, uint64(10000000000)) + sim := backends.NewXDCSimulatedBackend(nil, uint64(10000000000)) nonexistent, err := NewNonExistent(common.Address{}, sim) if err != nil { @@ -523,7 +523,7 @@ var bindTests = []struct { // Generate a new random account and a funded simulator key, _ := crypto.GenerateKey() auth := bind.NewKeyedTransactor(key) - sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) + sim := backends.NewXDCSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) // Deploy a funky gas pattern contract _, _, limiter, err := DeployFunkyGasPattern(auth, sim) @@ -567,7 +567,7 @@ var bindTests = []struct { // Generate a new random account and a funded simulator key, _ := crypto.GenerateKey() auth := bind.NewKeyedTransactor(key) - sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) + sim := backends.NewXDCSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) // Deploy a sender tester contract and execute a structured call on it _, _, callfrom, err := DeployCallFrom(auth, sim) @@ -636,7 +636,7 @@ var bindTests = []struct { // Generate a new random account and a funded simulator key, _ := crypto.GenerateKey() auth := bind.NewKeyedTransactor(key) - sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) + sim := backends.NewXDCSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) // Deploy a underscorer tester contract and execute a structured call on it _, _, underscorer, err := DeployUnderscorer(auth, sim) @@ -716,7 +716,7 @@ var bindTests = []struct { // Generate a new random account and a funded simulator key, _ := crypto.GenerateKey() auth := bind.NewKeyedTransactor(key) - sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) + sim := backends.NewXDCSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) // Deploy an eventer contract _, _, eventer, err := DeployEventer(auth, sim) @@ -873,7 +873,7 @@ var bindTests = []struct { // Generate a new random account and a funded simulator key, _ := crypto.GenerateKey() auth := bind.NewKeyedTransactor(key) - sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) + sim := backends.NewXDCSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000) //deploy the test contract _, _, testContract, err := DeployDeeplyNestedArray(auth, sim) diff --git a/accounts/abi/bind/util_test.go b/accounts/abi/bind/util_test.go index 8f4092971f..4d72676b3c 100644 --- a/accounts/abi/bind/util_test.go +++ b/accounts/abi/bind/util_test.go @@ -53,7 +53,7 @@ var waitDeployedTests = map[string]struct { func TestWaitDeployed(t *testing.T) { for name, test := range waitDeployedTests { - backend := backends.NewSimulatedBackend( + backend := backends.NewXDCSimulatedBackend( core.GenesisAlloc{ crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)}, }, 10000000, diff --git a/cmd/puppeth/wizard_genesis.go b/cmd/puppeth/wizard_genesis.go index ea77881634..8b14562235 100644 --- a/cmd/puppeth/wizard_genesis.go +++ b/cmd/puppeth/wizard_genesis.go @@ -182,7 +182,7 @@ func (w *wizard) makeGenesis() { // Validator Smart Contract Code pKey, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") addr := crypto.PubkeyToAddress(pKey.PublicKey) - contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000) + contractBackend := backends.NewXDCSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000) transactOpts := bind.NewKeyedTransactor(pKey) validatorAddress, _, err := validatorContract.DeployValidator(transactOpts, contractBackend, signers, validatorCaps, owner) diff --git a/consensus/XDPoS/XDPoS.go b/consensus/XDPoS/XDPoS.go index e370c86d62..4caf7c3b7e 100644 --- a/consensus/XDPoS/XDPoS.go +++ b/consensus/XDPoS/XDPoS.go @@ -264,7 +264,7 @@ func New(config *params.XDPoSConfig, db ethdb.Database) *XDPoS { } } -var engine *XDPoS +var fakeEngine *XDPoS // NewFullFaker creates an ethash consensus engine with a full fake scheme that // accepts all blocks as valid, without checking any consensus rules whatsoever. @@ -278,7 +278,7 @@ func NewFaker(db ethdb.Database) *XDPoS { signatures, _ := lru.NewARC(inmemorySnapshots) validatorSignatures, _ := lru.NewARC(inmemorySnapshots) verifiedHeaders, _ := lru.NewARC(inmemorySnapshots) - engine = &XDPoS{ + fakeEngine = &XDPoS{ config: conf, db: db, BlockSigners: BlockSigners, @@ -288,11 +288,11 @@ func NewFaker(db ethdb.Database) *XDPoS { validatorSignatures: validatorSignatures, proposals: make(map[common.Address]bool), } - return engine + return fakeEngine } func GetFaker() *XDPoS { - return engine + return fakeEngine } // Author implements consensus.Engine, returning the Ethereum address recovered diff --git a/contracts/blocksigner/blocksigner_test.go b/contracts/blocksigner/blocksigner_test.go index 8ca1140f28..54e19f6a37 100644 --- a/contracts/blocksigner/blocksigner_test.go +++ b/contracts/blocksigner/blocksigner_test.go @@ -36,7 +36,7 @@ var ( ) func TestBlockSigner(t *testing.T) { - contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000) + contractBackend := backends.NewXDCSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000) transactOpts := bind.NewKeyedTransactor(key) blockSignerAddress, blockSigner, err := DeployBlockSigner(transactOpts, contractBackend, big.NewInt(99)) diff --git a/contracts/chequebook/cheque_test.go b/contracts/chequebook/cheque_test.go index 4bd2e176b1..c3ddb157a1 100644 --- a/contracts/chequebook/cheque_test.go +++ b/contracts/chequebook/cheque_test.go @@ -42,7 +42,7 @@ var ( ) func newTestBackend() *backends.SimulatedBackend { - return backends.NewSimulatedBackend(core.GenesisAlloc{ + return backends.NewXDCSimulatedBackend(core.GenesisAlloc{ addr0: {Balance: big.NewInt(1000000000)}, addr1: {Balance: big.NewInt(1000000000)}, addr2: {Balance: big.NewInt(1000000000)}, diff --git a/contracts/ens/ens_test.go b/contracts/ens/ens_test.go index cd64fbf15f..5fda443eac 100644 --- a/contracts/ens/ens_test.go +++ b/contracts/ens/ens_test.go @@ -37,7 +37,7 @@ var ( ) func TestENS(t *testing.T) { - contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000) + contractBackend := backends.NewXDCSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000) transactOpts := bind.NewKeyedTransactor(key) ensAddr, ens, err := DeployENS(transactOpts, contractBackend) diff --git a/contracts/randomize/randomize_test.go b/contracts/randomize/randomize_test.go index 0b5db4021e..e873798d53 100644 --- a/contracts/randomize/randomize_test.go +++ b/contracts/randomize/randomize_test.go @@ -40,7 +40,7 @@ var ( ) func TestRandomize(t *testing.T) { - contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(100000000000000)}}, 10000000) + contractBackend := backends.NewXDCSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(100000000000000)}}, 10000000) transactOpts := bind.NewKeyedTransactor(key) transactOpts.GasLimit = 1000000 @@ -71,7 +71,7 @@ func TestRandomize(t *testing.T) { func TestSendTxRandomizeSecretAndOpening(t *testing.T) { genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000000)}} - backend := backends.NewSimulatedBackend(genesis, 10000000) + backend := backends.NewXDCSimulatedBackend(genesis, 10000000) backend.Commit() signer := types.HomesteadSigner{} ctx := context.Background() diff --git a/contracts/utils_test.go b/contracts/utils_test.go index 34e7d1279e..db02156395 100644 --- a/contracts/utils_test.go +++ b/contracts/utils_test.go @@ -47,7 +47,7 @@ var ( func getCommonBackend() *backends.SimulatedBackend { genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000000)}} - backend := backends.NewSimulatedBackend(genesis, 10000000) + backend := backends.NewXDCSimulatedBackend(genesis, 10000000) backend.Commit() return backend diff --git a/contracts/validator/validator_test.go b/contracts/validator/validator_test.go index ae16681a77..6b7f505fd7 100644 --- a/contracts/validator/validator_test.go +++ b/contracts/validator/validator_test.go @@ -46,7 +46,7 @@ var ( ) func TestValidator(t *testing.T) { - contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000) + contractBackend := backends.NewXDCSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000) transactOpts := bind.NewKeyedTransactor(key) validatorCap := new(big.Int) @@ -82,7 +82,7 @@ func TestValidator(t *testing.T) { } func TestRewardBalance(t *testing.T) { - contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{ + contractBackend := backends.NewXDCSimulatedBackend(core.GenesisAlloc{ acc1Addr: {Balance: new(big.Int).SetUint64(10000000)}, acc2Addr: {Balance: new(big.Int).SetUint64(10000000)}, acc4Addr: {Balance: new(big.Int).SetUint64(10000000)}, diff --git a/core/blockchain.go b/core/blockchain.go index e3b3c1b1e8..b28666b348 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1071,7 +1071,8 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. if (block.NumberU64() % bc.chainConfig.XDPoS.Epoch) == (bc.chainConfig.XDPoS.Epoch - bc.chainConfig.XDPoS.Gap) { err := bc.UpdateM1() if err != nil { - log.Crit("Error when update masternodes set. Stopping node", "err", err) + log.Error("Error when update masternodes set. Stopping node", "err", err) + os.Exit(1) } } } diff --git a/params/config.go b/params/config.go index 8418201c5d..4ea1886532 100644 --- a/params/config.go +++ b/params/config.go @@ -157,12 +157,11 @@ var ( // This configuration is intentionally not using keyed fields to force anyone // adding flags to the config to also have to set these fields. AllXDPoSProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, &XDPoSConfig{Period: 0, Epoch: 30000}} + TestXDPoSMockChainConfig = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, &XDPoSConfig{Epoch: 900, Gap: 890, SkipValidation: true}} AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, nil} - TestXDPoSMockChainConfig = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, &XDPoSConfig{Epoch: 900, Gap: 890, SkipValidation: true}} - TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, nil} - TestRules = TestChainConfig.Rules(new(big.Int)) - TestXDPoSChainConfig = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, &XDPoSConfig{Period: 2, Epoch: 900, Reward: 250, RewardCheckpoint: 900, Gap: 890, FoudationWalletAddr: common.HexToAddress("0x0000000000000000000000000000000000000068")}} + TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, nil} + TestRules = TestChainConfig.Rules(new(big.Int)) ) // TrustedCheckpoint represents a set of post-processed trie roots (CHT and diff --git a/tests/end_to_end/block_signer_test.go b/tests/end_to_end/block_signer_test.go index 0c75568b1c..102614861b 100644 --- a/tests/end_to_end/block_signer_test.go +++ b/tests/end_to_end/block_signer_test.go @@ -60,7 +60,7 @@ func getCommonBackend(t *testing.T) *backends.SimulatedBackend { // initial helper backend contractBackendForSC := backends.NewXDCSimulatedBackend(core.GenesisAlloc{ voterAddr: {Balance: new(big.Int).SetUint64(10000000000)}, - }) + }, 10000000) transactOpts := bind.NewKeyedTransactor(voterKey) @@ -71,7 +71,7 @@ func getCommonBackend(t *testing.T) *backends.SimulatedBackend { for i := 1; i <= 16; i++ { addr := fmt.Sprintf("%02d", i) - candidates = append(candidates, common.StringToAddress(addr)) + candidates = append(candidates, common.StringToAddress(addr)) // StringToAddress does not exist caps = append(caps, defalutCap) } @@ -127,7 +127,7 @@ func getCommonBackend(t *testing.T) *backends.SimulatedBackend { acc3Addr: {Balance: new(big.Int).SetUint64(10000000000)}, voterAddr: {Balance: new(big.Int).SetUint64(10000000000)}, common.HexToAddress(common.MasternodeVotingSMC): {Balance: new(big.Int).SetUint64(1), Code: code, Storage: storage}, // Binding the MasternodeVotingSMC with newly created 'code' for SC execution - }) + }, 10000000) return contractBackend2