Skip to content

Commit

Permalink
accounts: implement simple checkpoint syncing (ethereum#19543)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Jan 4, 2025
1 parent 015a56e commit 4c85ac5
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 38 deletions.
33 changes: 22 additions & 11 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -96,9 +98,7 @@ func SimulateWalletAddressAndSignFn() (common.Address, func(account accounts.Acc
return a1.Address, ks.SignHash, nil
}

// XDC simulated backend for testing purpose.
func NewXDCSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64, chainConfig *params.ChainConfig) *SimulatedBackend {
database := rawdb.NewMemoryDatabase()
func NewXDCSimulatedBackendWithDatabase(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64, chainConfig *params.ChainConfig) *SimulatedBackend {
genesis := core.Genesis{
GasLimit: gasLimit, // need this big, support initial smart contract
Config: chainConfig,
Expand Down Expand Up @@ -139,12 +139,14 @@ func NewXDCSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64, chainConfi
return backend
}

// NewSimulatedBackend creates a new binding backend using a simulated blockchain
// for testing purposes.
//
// A simulated backend always uses chainID 1337.
func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
database := rawdb.NewMemoryDatabase()
// NewXDCSimulatedBackend creates a new backend for testing purpose.
func NewXDCSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64, chainConfig *params.ChainConfig) *SimulatedBackend {
return NewXDCSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit, chainConfig)
}

// NewSimulatedBackendWithDatabase creates a new binding backend based on the given database
// and uses a simulated blockchain for testing purposes.
func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
genesis := core.Genesis{Config: params.AllEthashProtocolChanges, GasLimit: gasLimit, Alloc: alloc}
genesis.MustCommit(database)
blockchain, _ := core.NewBlockChain(database, nil, genesis.Config, ethash.NewFaker(), vm.Config{})
Expand All @@ -163,6 +165,14 @@ func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBac
return backend
}

// NewSimulatedBackend creates a new binding backend using a simulated blockchain
// for testing purposes.
//
// A simulated backend always uses chainID 1337.
func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit)
}

// Close terminates the underlying blockchain's update loop.
func (b *SimulatedBackend) Close() error {
b.blockchain.Stop()
Expand Down Expand Up @@ -625,7 +635,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
}

Expand Down
3 changes: 2 additions & 1 deletion accounts/abi/bind/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions accounts/abi/bind/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
`
4 changes: 3 additions & 1 deletion accounts/abi/bind/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions consensus/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion consensus/tests/engine_v1_tests/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 18 additions & 18 deletions consensus/tests/engine_v2_tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -114,56 +114,56 @@ 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)

// 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")

// 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")

// 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")
Expand Down
6 changes: 3 additions & 3 deletions consensus/tests/engine_v2_tests/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion contracts/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 4c85ac5

Please sign in to comment.