Repository navigation
fix(consensus/XDPoS): adjust epoch switch block iteration, close XDC-30 #3085
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev-upgrade
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -195,6 +195,23 @@ func (x *XDPoS_v2) IsEpochSwitch(header *types.Header) (bool, uint64, error) { | |
| // Search backwardly from end number to begin number | ||
| func (x *XDPoS_v2) GetEpochSwitchInfoBetween(chain consensus.ChainReader, begin, end *types.Header) ([]*types.EpochSwitchInfo, error) { | ||
| infos := make([]*types.EpochSwitchInfo, 0) | ||
| // A single-block range never enters the strict loop below, so decide it from | ||
| // the end header alone: report it only if it is itself an epoch switch. | ||
| if begin.Number.Cmp(end.Number) == 0 { | ||
| isEpochSwitch, _, err := x.IsEpochSwitch(end) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spec — informational, no action needed. |
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if !isEpochSwitch { | ||
| return infos, nil | ||
| } | ||
| epochSwitchInfo, err := x.getEpochSwitchInfo(chain, []*types.Header{end}, end.Hash()) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Standards — Duplicated Code (judgement call, no change requested). Lines 208-212 repeat the exact four-line shape of the loop body at 222-226 ( |
||
| if err != nil { | ||
| log.Error("[GetEpochSwitchInfoBetween] Adaptor v2 getEpochSwitchInfo has error, potentially bug", "err", err) | ||
| return nil, err | ||
| } | ||
| return append(infos, epochSwitchInfo), nil | ||
| } | ||
| // after the first iteration, it becomes nil since epoch switch info does not have header info | ||
| iteratorHeader := end | ||
| // after the first iteration, it becomes the parent hash of the epoch switch block | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package engine_v2_tests | |
| import ( | ||
| "math/big" | ||
| "reflect" | ||
| "strconv" | ||
| "testing" | ||
|
|
||
| "github.com/XinFinOrg/XDPoSChain/consensus/XDPoS" | ||
|
|
@@ -167,6 +168,36 @@ func TestGetEpochNumbersBetween(t *testing.T) { | |
| assert.Nil(t, numbers) | ||
| assert.EqualError(t, err, "illegal begin block number 1803") | ||
| } | ||
|
|
||
| // A range whose begin and end are the same block reports that block only if it | ||
| // is an epoch switch; a block inside an epoch reports nothing. | ||
| func TestGetEpochNumbersBetweenEqualSwitchBlock(t *testing.T) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Standards — Mysterious Name (nit). The name says |
||
| t.Parallel() | ||
| _, bc, _, _, _ := PrepareXDCTestBlockChainWith128Candidates(t, 1802, params.TestXDPoSMockChainConfig) | ||
|
|
||
| api := bc.BlockChain().Engine().(*XDPoS.XDPoS).APIs(bc.BlockChain())[0].Service.(*XDPoS.API) | ||
|
|
||
| cases := []struct { | ||
| block rpc.BlockNumber | ||
| want []uint64 | ||
| }{ | ||
| {901, []uint64{901}}, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spec — coverage nit (non-blocking). XDC-30 cites |
||
| {1800, []uint64{1800}}, | ||
| {1799, []uint64{}}, // inside an epoch, not a switch block | ||
| {1801, []uint64{}}, | ||
| {1802, []uint64{}}, | ||
| } | ||
| for _, c := range cases { | ||
| t.Run(strconv.FormatInt(c.block.Int64(), 10), func(t *testing.T) { | ||
| begin, end := c.block, c.block | ||
| numbers, err := api.GetEpochNumbersBetween(&begin, &end) | ||
|
|
||
| assert.Nil(t, err) | ||
| assert.Equal(t, c.want, numbers) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetBlockByEpochNumber(t *testing.T) { | ||
| t.Parallel() | ||
| blockchain, _, currentBlock, signer, signFn := PrepareXDCTestBlockChainWithPenaltyForV2Engine(t, 1802, params.TestXDPoSMockChainConfig) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebuild the round cache write for the single-block switch path so a canonical-at-write check guards it.
The equal-height branch in
GetEpochSwitchInfoBetweencallsIsEpochSwitch(end), which cachesend.Hash()by round. If a reorg replacesendafter the RPC resolves it, the cache can store a noncanonical hash.getBlockByEpochNumberInCachereturns a single cached entry without checking canonicality (utils.go:371-372), soGetBlockByEpochNumberandGetBlockInfoByV2EpochNumcan return that stale block. The exposure is narrow and overlaps the existing fork-import write path. Require that a cached entry be checked against the canonical header before it is returned, or that this RPC path skip the cache write.