From 7614a0ad9ab05fe26014e530e919884b59813a8e Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Mon, 3 Nov 2025 15:16:35 +0800 Subject: [PATCH] consensus/XDPoS: convert variables to const, close XFN-123 (#1688) --- consensus/XDPoS/engines/engine_v1/engine.go | 16 +++++----- consensus/XDPoS/engines/engine_v2/engine.go | 10 +++--- consensus/XDPoS/utils/constants.go | 35 +++++++++------------ 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/consensus/XDPoS/engines/engine_v1/engine.go b/consensus/XDPoS/engines/engine_v1/engine.go index 2e30722cd8..9e545ad59f 100644 --- a/consensus/XDPoS/engines/engine_v1/engine.go +++ b/consensus/XDPoS/engines/engine_v1/engine.go @@ -97,10 +97,10 @@ func New(chainConfig *params.ChainConfig, db ethdb.Database) *XDPoS_v1 { config: &conf, db: db, - recents: lru.NewCache[common.Hash, *SnapshotV1](utils.InmemorySnapshots), - signatures: lru.NewCache[common.Hash, common.Address](utils.InmemorySnapshots), - verifiedHeaders: lru.NewCache[common.Hash, struct{}](utils.InmemorySnapshots), - validatorSignatures: lru.NewCache[common.Hash, common.Address](utils.InmemorySnapshots), + recents: lru.NewCache[common.Hash, *SnapshotV1](utils.InMemorySnapshots), + signatures: lru.NewCache[common.Hash, common.Address](utils.InMemorySnapshots), + verifiedHeaders: lru.NewCache[common.Hash, struct{}](utils.InMemorySnapshots), + validatorSignatures: lru.NewCache[common.Hash, common.Address](utils.InMemorySnapshots), proposals: make(map[common.Address]bool), } } @@ -1040,10 +1040,10 @@ func NewFaker(db ethdb.Database, chainConfig *params.ChainConfig) *XDPoS_v1 { config: conf, db: db, - recents: lru.NewCache[common.Hash, *SnapshotV1](utils.InmemorySnapshots), - signatures: lru.NewCache[common.Hash, common.Address](utils.InmemorySnapshots), - verifiedHeaders: lru.NewCache[common.Hash, struct{}](utils.InmemorySnapshots), - validatorSignatures: lru.NewCache[common.Hash, common.Address](utils.InmemorySnapshots), + recents: lru.NewCache[common.Hash, *SnapshotV1](utils.InMemorySnapshots), + signatures: lru.NewCache[common.Hash, common.Address](utils.InMemorySnapshots), + verifiedHeaders: lru.NewCache[common.Hash, struct{}](utils.InMemorySnapshots), + validatorSignatures: lru.NewCache[common.Hash, common.Address](utils.InMemorySnapshots), proposals: make(map[common.Address]bool), } return fakeEngine diff --git a/consensus/XDPoS/engines/engine_v2/engine.go b/consensus/XDPoS/engines/engine_v2/engine.go index 33c7b0e9a7..32a9951c90 100644 --- a/consensus/XDPoS/engines/engine_v2/engine.go +++ b/consensus/XDPoS/engines/engine_v2/engine.go @@ -98,17 +98,17 @@ func New(chainConfig *params.ChainConfig, db ethdb.Database, minePeriodCh chan i db: db, isInitilised: false, - signatures: lru.NewCache[common.Hash, common.Address](utils.InmemorySnapshots), + signatures: lru.NewCache[common.Hash, common.Address](utils.InMemorySnapshots), - verifiedHeaders: lru.NewCache[common.Hash, struct{}](utils.InmemorySnapshots), - snapshots: lru.NewCache[common.Hash, *SnapshotV2](utils.InmemorySnapshots), - epochSwitches: lru.NewCache[common.Hash, *types.EpochSwitchInfo](int(utils.InmemoryEpochs)), + verifiedHeaders: lru.NewCache[common.Hash, struct{}](utils.InMemorySnapshots), + snapshots: lru.NewCache[common.Hash, *SnapshotV2](utils.InMemorySnapshots), + epochSwitches: lru.NewCache[common.Hash, *types.EpochSwitchInfo](int(utils.InMemoryEpochs)), timeoutWorker: timeoutTimer, BroadcastCh: make(chan interface{}), minePeriodCh: minePeriodCh, newRoundCh: newRoundCh, - round2epochBlockInfo: lru.NewCache[types.Round, *types.BlockInfo](utils.InmemoryRound2Epochs), + round2epochBlockInfo: lru.NewCache[types.Round, *types.BlockInfo](utils.InMemoryRound2Epochs), timeoutPool: timeoutPool, votePool: votePool, diff --git a/consensus/XDPoS/utils/constants.go b/consensus/XDPoS/utils/constants.go index 6cef0034a0..acf562375d 100644 --- a/consensus/XDPoS/utils/constants.go +++ b/consensus/XDPoS/utils/constants.go @@ -6,28 +6,21 @@ import ( ) // XDPoS delegated-proof-of-stake protocol constants. +const ( + BlockSignersCacheLimit = 9000 + EpochLength = uint64(900) // Default number of blocks after which to checkpoint and reset the pending votes + ExtraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity + ExtraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal + InMemoryEpochs = 5 * EpochLength // Number of mapping from block to epoch switch infos to keep in memory + InMemoryRound2Epochs = 65536 // Number of mapping of epoch switch blocks for quickly locating epoch switch block. One epoch ~ 0.5hours, so 65536 epochs ~ 3.7 years. And it uses ~ 10MB memory. + InMemorySnapshots = 128 // Number of recent vote snapshots to keep in memory + M2ByteLength = 4 + PeriodicJobPeriod = 60 + PoolHygieneRound = 10 +) + var ( - EpochLength = uint64(900) // Default number of blocks after which to checkpoint and reset the pending votes - - ExtraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity - ExtraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal - NonceAuthVote = hexutil.MustDecode("0xffffffffffffffff") // Magic nonce number to vote on adding a new signer NonceDropVote = hexutil.MustDecode("0x0000000000000000") // Magic nonce number to vote on removing a signer. - - UncleHash = types.CalcUncleHash(nil) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW. - InmemoryEpochs = 5 * EpochLength // Number of mapping from block to epoch switch infos to keep in memory - - InmemoryRound2Epochs = 65536 // Number of mapping of epoch switch blocks for quickly locating epoch switch block. One epoch ~ 0.5hours, so 65536 epochs ~ 3.7 years. And it uses ~ 10MB memory. -) - -const ( - InmemorySnapshots = 128 // Number of recent vote snapshots to keep in memory - BlockSignersCacheLimit = 9000 - M2ByteLength = 4 -) - -const ( - PeriodicJobPeriod = 60 - PoolHygieneRound = 10 + UncleHash = types.CalcUncleHash(nil) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW. )