diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index b129609e31..e955a18324 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -55,7 +55,9 @@ import ( // This nil assignment ensures compile time that SimulatedBackend implements bind.ContractBackend. var _ bind.ContractBackend = (*SimulatedBackend)(nil) -var errBlockNumberUnsupported = errors.New("SimulatedBackend cannot access blocks other than the latest block") +var ( + errBlockNumberUnsupported = errors.New("SimulatedBackend cannot access blocks other than the latest block") +) // SimulatedBackend implements bind.ContractBackend, simulating a blockchain in // the background. Its main purpose is to allow easily testing contract bindings. @@ -96,7 +98,7 @@ func SimulateWalletAddressAndSignFn() (common.Address, func(account accounts.Acc return a1.Address, ks.SignHash, nil } -// XDC simulated backend for testing purpose. +// NewXDCSimulatedBackend creates a new backend for testing purpose. func NewXDCSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64, chainConfig *params.ChainConfig) *SimulatedBackend { database := rawdb.NewMemoryDatabase() genesis := core.Genesis{ @@ -139,9 +141,8 @@ func NewXDCSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64, chainConfi return backend } -// NewSimulatedBackend creates a new binding backend using a simulated blockchain -// for testing purposes. -// +// NewSimulatedBackend creates a new binding backend based on the given database +// and uses a simulated blockchain for testing purposes. // A simulated backend always uses chainID 1337. func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend { database := rawdb.NewMemoryDatabase() @@ -625,7 +626,8 @@ func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error { return nil } -func (b *SimulatedBackend) GetBlockChain() *core.BlockChain { +// Blockchain returns the underlying blockchain. +func (b *SimulatedBackend) BlockChain() *core.BlockChain { return b.blockchain } diff --git a/accounts/abi/bind/bind_test.go b/accounts/abi/bind/bind_test.go index ed1d9f0c14..a4f7dc5af5 100644 --- a/accounts/abi/bind/bind_test.go +++ b/accounts/abi/bind/bind_test.go @@ -520,11 +520,12 @@ var bindTests = []struct { "github.com/XinFinOrg/XDPoSChain/accounts/abi/bind" "github.com/XinFinOrg/XDPoSChain/accounts/abi/bind/backends" "github.com/XinFinOrg/XDPoSChain/common" + "github.com/XinFinOrg/XDPoSChain/core" "github.com/XinFinOrg/XDPoSChain/params" `, ` // Create a simulator and wrap a non-deployed contract - sim := backends.NewXDCSimulatedBackend(nil, uint64(10000000000), params.TestXDPoSMockChainConfig) + sim := backends.NewXDCSimulatedBackend(core.GenesisAlloc{}, uint64(10000000000), params.TestXDPoSMockChainConfig) nonexistent, err := NewNonExistent(common.Address{}, sim) if err != nil { diff --git a/accounts/abi/bind/template.go b/accounts/abi/bind/template.go index f7b1ebb62a..e743d4064b 100644 --- a/accounts/abi/bind/template.go +++ b/accounts/abi/bind/template.go @@ -453,6 +453,18 @@ var ( } }), nil } + + // Parse{{.Normalized.Name}} is a log parse operation binding the contract event 0x{{printf "%x" .Original.Id}}. + // + // Solidity: {{.Original.String}} + func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Parse{{.Normalized.Name}}(log types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) { + event := new({{$contract.Type}}{{.Normalized.Name}}) + if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil { + return nil, err + } + return event, nil + } + {{end}} {{end}} ` diff --git a/accounts/abi/bind/util_test.go b/accounts/abi/bind/util_test.go index 5b52249209..b836165708 100644 --- a/accounts/abi/bind/util_test.go +++ b/accounts/abi/bind/util_test.go @@ -59,7 +59,9 @@ func TestWaitDeployed(t *testing.T) { backend := backends.NewXDCSimulatedBackend( core.GenesisAlloc{ crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(100000000000000000)}, - }, 10000000, &config, + }, + 10000000, + &config, ) // Create the transaction diff --git a/consensus/tests/api_test.go b/consensus/tests/api_test.go index ed3e0897d0..64cbf80431 100644 --- a/consensus/tests/api_test.go +++ b/consensus/tests/api_test.go @@ -22,9 +22,9 @@ func TestConfigApi(t *testing.T) { voterAddr: {Balance: new(big.Int).SetUint64(10000000000)}, }, 10000000, params.TestXDPoSMockChainConfig) - engine := bc.GetBlockChain().Engine().(*XDPoS.XDPoS) + engine := bc.BlockChain().Engine().(*XDPoS.XDPoS) - info := engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).NetworkInformation() + info := engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).NetworkInformation() assert.Equal(t, info.NetworkId, big.NewInt(1337)) assert.Equal(t, info.ConsensusConfigs.V2.CurrentConfig.MaxMasternodes, 18) diff --git a/consensus/tests/engine_v1_tests/helper.go b/consensus/tests/engine_v1_tests/helper.go index afc1a22cf0..e1a66d1308 100644 --- a/consensus/tests/engine_v1_tests/helper.go +++ b/consensus/tests/engine_v1_tests/helper.go @@ -244,7 +244,7 @@ func PrepareXDCTestBlockChain(t *testing.T, numOfBlocks int, chainConfig *params signer, signFn, err := backends.SimulateWalletAddressAndSignFn() backend := getCommonBackend(t, chainConfig) - blockchain := backend.GetBlockChain() + blockchain := backend.BlockChain() blockchain.Client = backend if err != nil { diff --git a/consensus/tests/engine_v2_tests/api_test.go b/consensus/tests/engine_v2_tests/api_test.go index 0b1258d2a4..d02790d894 100644 --- a/consensus/tests/engine_v2_tests/api_test.go +++ b/consensus/tests/engine_v2_tests/api_test.go @@ -15,10 +15,10 @@ import ( func TestGetMissedRoundsInEpochByBlockNumOnlyForV2Consensus(t *testing.T) { _, bc, _, _, _ := PrepareXDCTestBlockChainWith128Candidates(t, 1802, params.TestXDPoSMockChainConfig) - engine := bc.GetBlockChain().Engine().(*XDPoS.XDPoS) + engine := bc.BlockChain().Engine().(*XDPoS.XDPoS) blockNum := rpc.BlockNumber(123) - data, err := engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetMissedRoundsInEpochByBlockNum(&blockNum) + data, err := engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).GetMissedRoundsInEpochByBlockNum(&blockNum) assert.EqualError(t, err, "not supported in the v1 consensus") assert.Nil(t, data) @@ -27,10 +27,10 @@ func TestGetMissedRoundsInEpochByBlockNumOnlyForV2Consensus(t *testing.T) { func TestGetMissedRoundsInEpochByBlockNumReturnEmptyForV2(t *testing.T) { _, bc, cb, _, _ := PrepareXDCTestBlockChainWith128Candidates(t, 1802, params.TestXDPoSMockChainConfig) - engine := bc.GetBlockChain().Engine().(*XDPoS.XDPoS) + engine := bc.BlockChain().Engine().(*XDPoS.XDPoS) blockNum := rpc.BlockNumber(cb.NumberU64()) - data, err := engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetMissedRoundsInEpochByBlockNum(&blockNum) + data, err := engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).GetMissedRoundsInEpochByBlockNum(&blockNum) assert.Nil(t, err) assert.Equal(t, types.Round(900), data.EpochRound) @@ -39,7 +39,7 @@ func TestGetMissedRoundsInEpochByBlockNumReturnEmptyForV2(t *testing.T) { blockNum = rpc.BlockNumber(1800) - data, err = engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetMissedRoundsInEpochByBlockNum(&blockNum) + data, err = engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).GetMissedRoundsInEpochByBlockNum(&blockNum) assert.Nil(t, err) assert.Equal(t, types.Round(900), data.EpochRound) @@ -48,7 +48,7 @@ func TestGetMissedRoundsInEpochByBlockNumReturnEmptyForV2(t *testing.T) { blockNum = rpc.BlockNumber(1801) - data, err = engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetMissedRoundsInEpochByBlockNum(&blockNum) + data, err = engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).GetMissedRoundsInEpochByBlockNum(&blockNum) assert.Nil(t, err) assert.Equal(t, types.Round(900), data.EpochRound) @@ -59,10 +59,10 @@ func TestGetMissedRoundsInEpochByBlockNumReturnEmptyForV2(t *testing.T) { func TestGetMissedRoundsInEpochByBlockNumReturnEmptyForV2FistEpoch(t *testing.T) { _, bc, _, _, _ := PrepareXDCTestBlockChainWith128Candidates(t, 1802, params.TestXDPoSMockChainConfig) - engine := bc.GetBlockChain().Engine().(*XDPoS.XDPoS) + engine := bc.BlockChain().Engine().(*XDPoS.XDPoS) blockNum := rpc.BlockNumber(901) - data, err := engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetMissedRoundsInEpochByBlockNum(&blockNum) + data, err := engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).GetMissedRoundsInEpochByBlockNum(&blockNum) assert.Nil(t, err) assert.Equal(t, types.Round(1), data.EpochRound) @@ -73,7 +73,7 @@ func TestGetMissedRoundsInEpochByBlockNumReturnEmptyForV2FistEpoch(t *testing.T) func TestGetMissedRoundsInEpochByBlockNum(t *testing.T) { blockchain, bc, currentBlock, signer, signFn := PrepareXDCTestBlockChainWith128Candidates(t, 1802, params.TestXDPoSMockChainConfig) chainConfig := params.TestXDPoSMockChainConfig - engine := bc.GetBlockChain().Engine().(*XDPoS.XDPoS) + engine := bc.BlockChain().Engine().(*XDPoS.XDPoS) blockCoinBase := signer.Hex() startingBlockNum := currentBlock.Number().Int64() + 1 @@ -93,7 +93,7 @@ func TestGetMissedRoundsInEpochByBlockNum(t *testing.T) { blockNum := rpc.BlockNumber(1803) - data, err := engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetMissedRoundsInEpochByBlockNum(&blockNum) + data, err := engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).GetMissedRoundsInEpochByBlockNum(&blockNum) assert.Nil(t, err) assert.Equal(t, types.Round(900), data.EpochRound) @@ -114,32 +114,32 @@ func TestGetMissedRoundsInEpochByBlockNum(t *testing.T) { func TestGetEpochNumbersBetween(t *testing.T) { _, bc, _, _, _ := PrepareXDCTestBlockChainWith128Candidates(t, 1802, params.TestXDPoSMockChainConfig) - engine := bc.GetBlockChain().Engine().(*XDPoS.XDPoS) + engine := bc.BlockChain().Engine().(*XDPoS.XDPoS) begin := rpc.BlockNumber(1800) end := rpc.BlockNumber(1802) - numbers, err := engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) + numbers, err := engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) assert.True(t, reflect.DeepEqual([]uint64{1800}, numbers)) assert.Nil(t, err) begin = rpc.BlockNumber(1799) end = rpc.BlockNumber(1802) - numbers, err = engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) + numbers, err = engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) assert.True(t, reflect.DeepEqual([]uint64{1800}, numbers)) assert.Nil(t, err) begin = rpc.BlockNumber(1799) end = rpc.BlockNumber(1802) - numbers, err = engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) + numbers, err = engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) assert.True(t, reflect.DeepEqual([]uint64{1800}, numbers)) assert.Nil(t, err) begin = rpc.BlockNumber(901) end = rpc.BlockNumber(1802) - numbers, err = engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) + numbers, err = engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) assert.True(t, reflect.DeepEqual([]uint64{901, 1800}, numbers)) assert.Nil(t, err) @@ -147,7 +147,7 @@ func TestGetEpochNumbersBetween(t *testing.T) { // 900 is V1, not V2, so error begin = rpc.BlockNumber(900) end = rpc.BlockNumber(1802) - numbers, err = engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) + numbers, err = engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) assert.Nil(t, numbers) assert.EqualError(t, err, "not supported in the v1 consensus") @@ -155,7 +155,7 @@ func TestGetEpochNumbersBetween(t *testing.T) { // 1803 not exist begin = rpc.BlockNumber(901) end = rpc.BlockNumber(1803) - numbers, err = engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) + numbers, err = engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) assert.Nil(t, numbers) assert.EqualError(t, err, "illegal end block number") @@ -163,7 +163,7 @@ func TestGetEpochNumbersBetween(t *testing.T) { // 1803 not exist begin = rpc.BlockNumber(1803) end = rpc.BlockNumber(1803) - numbers, err = engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) + numbers, err = engine.APIs(bc.BlockChain())[0].Service.(*XDPoS.API).GetEpochNumbersBetween(&begin, &end) assert.Nil(t, numbers) assert.EqualError(t, err, "illegal begin block number") diff --git a/consensus/tests/engine_v2_tests/helper.go b/consensus/tests/engine_v2_tests/helper.go index 1e9b6d14b9..c13b289b34 100644 --- a/consensus/tests/engine_v2_tests/helper.go +++ b/consensus/tests/engine_v2_tests/helper.go @@ -373,7 +373,7 @@ func PrepareXDCTestBlockChainForV2Engine(t *testing.T, numOfBlocks int, chainCon panic(fmt.Errorf("error while creating simulated wallet for generating singer address and signer fn: %v", err)) } backend := getCommonBackend(t, chainConfig) - blockchain := backend.GetBlockChain() + blockchain := backend.BlockChain() blockchain.Client = backend engine := blockchain.Engine().(*XDPoS.XDPoS) @@ -463,7 +463,7 @@ func PrepareXDCTestBlockChainWithPenaltyForV2Engine(t *testing.T, numOfBlocks in t.Fatal("Error while creating simulated wallet for generating singer address and signer fn: ", err) } backend := getCommonBackend(t, chainConfig) - blockchain := backend.GetBlockChain() + blockchain := backend.BlockChain() blockchain.Client = backend // Authorise @@ -514,7 +514,7 @@ func PrepareXDCTestBlockChainWith128Candidates(t *testing.T, numOfBlocks int, ch t.Fatal("Error while creating simulated wallet for generating singer address and signer fn: ", err) } backend := getMultiCandidatesBackend(t, chainConfig, 128) - blockchain := backend.GetBlockChain() + blockchain := backend.BlockChain() blockchain.Client = backend engine := blockchain.Engine().(*XDPoS.XDPoS) diff --git a/contracts/validator/validator_test.go b/contracts/validator/validator_test.go index c2b5d0fde1..090d261770 100644 --- a/contracts/validator/validator_test.go +++ b/contracts/validator/validator_test.go @@ -320,7 +320,7 @@ func TestStatedbUtils(t *testing.T) { if err != nil { t.Fatalf("can't get validator object: %v", err) } - statedb, err := contractBackendForValidator.GetBlockChain().State() + statedb, err := contractBackendForValidator.BlockChain().State() if err != nil { t.Fatalf("can't get statedb: %v", err) }