mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 21:31:37 +00:00
accounts: implement simple checkpoint syncing (#19543)
This commit is contained in:
parent
894bed5b7e
commit
f542e463d3
9 changed files with 50 additions and 33 deletions
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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}}
|
||||
`
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue