From 5af51eae9493aac09aa05d247973bca93eef2b8f Mon Sep 17 00:00:00 2001 From: yumiel Date: Thu, 19 Dec 2024 12:25:38 +0800 Subject: [PATCH] add mixHash length check at selectRandomCommittee --- consensus/istanbul/backend/snapshot.go | 2 +- kaiax/valset/impl/committee.go | 2 +- kaiax/valset/impl/getter_test.go | 4 ++-- kaiax/valset/impl/init_test.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/consensus/istanbul/backend/snapshot.go b/consensus/istanbul/backend/snapshot.go index 612f77316..540eea6a1 100644 --- a/consensus/istanbul/backend/snapshot.go +++ b/consensus/istanbul/backend/snapshot.go @@ -538,4 +538,4 @@ func (sb *backend) regen(chain consensus.ChainReader, headers []*types.Header) { if commitTried { // This prevents pushing too many logs by potential DoS attack logger.Trace("[Snapshot] Snapshot restoring completed", "len(headers)", len(headers), "from", from, "to", to, "elapsed", time.Since(start)) } -} \ No newline at end of file +} diff --git a/kaiax/valset/impl/committee.go b/kaiax/valset/impl/committee.go index 0e52e6854..08c38fcec 100644 --- a/kaiax/valset/impl/committee.go +++ b/kaiax/valset/impl/committee.go @@ -193,7 +193,7 @@ func (c *committeeContext) selectRandomCommittee(round uint64, proposer, nextDis // return shuffled[:min(committee_size, len(validators))] func (c *committeeContext) selectRandaoCommittee() ([]common.Address, error) { prevMixHash := c.prevHeader.MixHash - if prevMixHash == nil { + if prevMixHash == nil || len(prevMixHash) != 32 { prevMixHash = params.ZeroMixHash } diff --git a/kaiax/valset/impl/getter_test.go b/kaiax/valset/impl/getter_test.go index a9087a614..7d5bb0f36 100644 --- a/kaiax/valset/impl/getter_test.go +++ b/kaiax/valset/impl/getter_test.go @@ -77,7 +77,7 @@ func TestGetCommittee(t *testing.T) { // per committeeSize {"committeesize is zero", defaultBN, defaultRound, testProposerPolicy, 0, tgn, nil, errInvalidCommitteeSize}, {"committeesize is one", defaultBN, defaultRound, testProposerPolicy, 1, tgn, []common.Address{tgn}, nil}, - {"committeesize is three", defaultBN, defaultRound, testProposerPolicy, testSubGroupSize, tgn, []common.Address{n[0], n[1], n[2]}, nil}, + {"committeesize is three", defaultBN, defaultRound, testProposerPolicy, testSubGroupSize, tgn, []common.Address{n[0], n[3], n[1]}, nil}, {"committeesize is six", defaultBN, defaultRound, testProposerPolicy, testSubGroupSize + 3, tgn, []common.Address{tgn, n[1], n[3], n[2]}, nil}, {"committeesize is seven", defaultBN, defaultRound, testProposerPolicy, testSubGroupSize + 4, tgn, []common.Address{tgn, n[1], n[3], n[2]}, nil}, // per proposerPolicy @@ -88,7 +88,7 @@ func TestGetCommittee(t *testing.T) { {"block 1", 1, defaultRound, testProposerPolicy, testSubGroupSize, tgn, []common.Address{n[1], n[2], tgn}, nil}, {"istanbul hf activated", testIstanbulCompatibleNumber.Uint64() + 1, defaultRound, testProposerPolicy, testSubGroupSize, tgn, []common.Address{n[1], n[2], n[3]}, nil}, {"kore hf activated", testKoreCompatibleBlock.Uint64() + 1, defaultRound, testProposerPolicy, testSubGroupSize, tgn, []common.Address{n[3], tgn, n[1]}, nil}, - {"randao hf activated", testRandaoCompatibleBlock.Uint64() + 1, defaultRound, testProposerPolicy, testSubGroupSize, tgn, []common.Address{tgn, n[1], n[2]}, nil}, + {"randao hf activated", testRandaoCompatibleBlock.Uint64() + 1, defaultRound, testProposerPolicy, testSubGroupSize, tgn, []common.Address{n[0], n[3], n[1]}, nil}, // TODO-kaia-valset: add mainnet,testnet testcases } { t.Run(tc.name, func(t *testing.T) { diff --git a/kaiax/valset/impl/init_test.go b/kaiax/valset/impl/init_test.go index 92ca57b91..c2664a6c2 100644 --- a/kaiax/valset/impl/init_test.go +++ b/kaiax/valset/impl/init_test.go @@ -69,7 +69,7 @@ var ( testRandaoCompatibleBlock = big.NewInt(int64(testEpoch) + 30) testKaiaCompatibleBlock = big.NewInt(int64(testEpoch) + 40) - testMixHash = common.Hex2Bytes("0x365643b31592079285c66bfbf9215bccc5d977210ec0b28fb2f19b6b92773625") + testMixHash = common.Hex2Bytes("365643b31592079285c66bfbf9215bccc5d977210ec0b28fb2f19b6b92773625") testPrevHash = common.HexToHash("0x2e43e99fe04c8664f93f21b255356cd2279b0e993cc367db8d3e629d8c745635") )