From d8e422c9ffc13ecc71e0a1387e6299ab9385abc6 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Fri, 10 Jun 2022 20:23:44 +0530 Subject: [PATCH 01/20] Remove checks for equivocatory blocks --- lib/babe/errors.go | 3 -- lib/babe/verify.go | 29 ------------------ lib/babe/verify_integration_test.go | 47 ----------------------------- lib/babe/verify_test.go | 39 ------------------------ 4 files changed, 118 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 527a0a8915..521007187b 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -33,9 +33,6 @@ var ( // ErrBadSignature is returned when a seal is invalid ErrBadSignature = errors.New("could not verify signature") - // ErrProducerEquivocated is returned when a block producer has produced conflicting blocks - ErrProducerEquivocated = errors.New("block producer equivocated") - // ErrNotAuthorized is returned when the node is not authorized to produce a block ErrNotAuthorized = errors.New("not authorized to produce block") diff --git a/lib/babe/verify.go b/lib/babe/verify.go index add58392a4..d4b01d26b0 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -340,35 +340,6 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { return ErrBadSignature } - // check if the producer has equivocated, ie. have they produced a conflicting block? - hashes := b.blockState.GetAllBlocksAtDepth(header.ParentHash) - - for _, hash := range hashes { - currentHeader, err := b.blockState.GetHeader(hash) - if err != nil { - continue - } - - currentBlockProducerIndex, err := getAuthorityIndex(currentHeader) - if err != nil { - continue - } - - var existingBlockProducerIndex uint32 - switch d := babePreDigest.(type) { - case types.BabePrimaryPreDigest: - existingBlockProducerIndex = d.AuthorityIndex - case types.BabeSecondaryVRFPreDigest: - existingBlockProducerIndex = d.AuthorityIndex - case types.BabeSecondaryPlainPreDigest: - existingBlockProducerIndex = d.AuthorityIndex - } - - if currentBlockProducerIndex == existingBlockProducerIndex && hash != header.Hash() { - return ErrProducerEquivocated - } - } - return nil } diff --git a/lib/babe/verify_integration_test.go b/lib/babe/verify_integration_test.go index 48cdd864da..5e298ecfcc 100644 --- a/lib/babe/verify_integration_test.go +++ b/lib/babe/verify_integration_test.go @@ -386,53 +386,6 @@ func TestVerifyAuthorshipRight(t *testing.T) { require.NoError(t, err) } -func TestVerifyAuthorshipRight_Equivocation(t *testing.T) { - kp, err := sr25519.GenerateKeypair() - require.NoError(t, err) - - cfg := &ServiceConfig{ - Keypair: kp, - } - - babeService := createTestService(t, cfg) - epochData, err := babeService.initiateEpoch(testEpochIndex) - require.NoError(t, err) - - epochData.threshold = maxThreshold - epochData.authorities = []types.Authority{ - { - Key: kp.Public().(*sr25519.PublicKey), - }, - } - - // create and add first block - block := createTestBlock(t, babeService, genesisHeader, [][]byte{}, 1, testEpochIndex, epochData) - block.Header.Hash() - - err = babeService.blockState.AddBlock(block) - require.NoError(t, err) - - verifier, err := newVerifier(babeService.blockState, testEpochIndex, &verifierInfo{ - authorities: epochData.authorities, - threshold: epochData.threshold, - randomness: epochData.randomness, - }) - require.NoError(t, err) - - err = verifier.verifyAuthorshipRight(&block.Header) - require.NoError(t, err) - - // create new block - block2 := createTestBlock(t, babeService, genesisHeader, [][]byte{}, 1, testEpochIndex, epochData) - block2.Header.Hash() - - err = babeService.blockState.AddBlock(block2) - require.NoError(t, err) - - err = verifier.verifyAuthorshipRight(&block2.Header) - require.Equal(t, ErrProducerEquivocated, err) -} - func TestVerifyForkBlocksWithRespectiveEpochData(t *testing.T) { /* * Setup the services: StateService, DigestHandler, EpochState diff --git a/lib/babe/verify_test.go b/lib/babe/verify_test.go index f6c106ca7c..739584f03f 100644 --- a/lib/babe/verify_test.go +++ b/lib/babe/verify_test.go @@ -612,27 +612,6 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { //// Case 8: Get header error babeVerifier6 := newTestVerifier(t, kp, mockBlockStateErr, scale.MaxUint128, false) - // Case 9: Equivocate case primary - babeVerifier7 := newTestVerifier(t, kp, mockBlockStateEquiv1, scale.MaxUint128, false) - - // Case 10: Equivocate case secondary plain - babeSecPlainPrd2, err := testBabeSecondaryPlainPreDigest.ToPreRuntimeDigest() - assert.NoError(t, err) - header8 := newTestHeader(t, *babeSecPlainPrd2) - - hash2 := encodeAndHashHeader(t, header8) - signAndAddSeal(t, kp, header8, hash2[:]) - babeVerifier8 := newTestVerifier(t, kp, mockBlockStateEquiv2, scale.MaxUint128, true) - - // Case 11: equivocation case secondary VRF - encVrfDigest := newEncodedBabeDigest(t, testBabeSecondaryVRFPreDigest) - assert.NoError(t, err) - header9 := newTestHeader(t, *types.NewBABEPreRuntimeDigest(encVrfDigest)) - - hash3 := encodeAndHashHeader(t, header9) - signAndAddSeal(t, kp, header9, hash3[:]) - babeVerifier9 := newTestVerifier(t, kp, mockBlockStateEquiv3, scale.MaxUint128, true) - tests := []struct { name string verifier verifier @@ -697,24 +676,6 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { verifier: *babeVerifier6, header: header7, }, - { - name: "equivocate - primary", - verifier: *babeVerifier7, - header: header7, - expErr: ErrProducerEquivocated, - }, - { - name: "equivocate - secondary plain", - verifier: *babeVerifier8, - header: header8, - expErr: ErrProducerEquivocated, - }, - { - name: "equivocate - secondary vrf", - verifier: *babeVerifier9, - header: header9, - expErr: ErrProducerEquivocated, - }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 5cb4ccab40be597f3a8cc1020ed60ff27dabf175 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Thu, 16 Jun 2022 13:22:06 +0530 Subject: [PATCH 02/20] Revert "Remove checks for equivocatory blocks" This reverts commit d8e422c9ffc13ecc71e0a1387e6299ab9385abc6. --- lib/babe/errors.go | 3 ++ lib/babe/verify.go | 29 ++++++++++++++++++ lib/babe/verify_integration_test.go | 47 +++++++++++++++++++++++++++++ lib/babe/verify_test.go | 39 ++++++++++++++++++++++++ 4 files changed, 118 insertions(+) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 521007187b..527a0a8915 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -33,6 +33,9 @@ var ( // ErrBadSignature is returned when a seal is invalid ErrBadSignature = errors.New("could not verify signature") + // ErrProducerEquivocated is returned when a block producer has produced conflicting blocks + ErrProducerEquivocated = errors.New("block producer equivocated") + // ErrNotAuthorized is returned when the node is not authorized to produce a block ErrNotAuthorized = errors.New("not authorized to produce block") diff --git a/lib/babe/verify.go b/lib/babe/verify.go index d4b01d26b0..add58392a4 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -340,6 +340,35 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { return ErrBadSignature } + // check if the producer has equivocated, ie. have they produced a conflicting block? + hashes := b.blockState.GetAllBlocksAtDepth(header.ParentHash) + + for _, hash := range hashes { + currentHeader, err := b.blockState.GetHeader(hash) + if err != nil { + continue + } + + currentBlockProducerIndex, err := getAuthorityIndex(currentHeader) + if err != nil { + continue + } + + var existingBlockProducerIndex uint32 + switch d := babePreDigest.(type) { + case types.BabePrimaryPreDigest: + existingBlockProducerIndex = d.AuthorityIndex + case types.BabeSecondaryVRFPreDigest: + existingBlockProducerIndex = d.AuthorityIndex + case types.BabeSecondaryPlainPreDigest: + existingBlockProducerIndex = d.AuthorityIndex + } + + if currentBlockProducerIndex == existingBlockProducerIndex && hash != header.Hash() { + return ErrProducerEquivocated + } + } + return nil } diff --git a/lib/babe/verify_integration_test.go b/lib/babe/verify_integration_test.go index 5e298ecfcc..48cdd864da 100644 --- a/lib/babe/verify_integration_test.go +++ b/lib/babe/verify_integration_test.go @@ -386,6 +386,53 @@ func TestVerifyAuthorshipRight(t *testing.T) { require.NoError(t, err) } +func TestVerifyAuthorshipRight_Equivocation(t *testing.T) { + kp, err := sr25519.GenerateKeypair() + require.NoError(t, err) + + cfg := &ServiceConfig{ + Keypair: kp, + } + + babeService := createTestService(t, cfg) + epochData, err := babeService.initiateEpoch(testEpochIndex) + require.NoError(t, err) + + epochData.threshold = maxThreshold + epochData.authorities = []types.Authority{ + { + Key: kp.Public().(*sr25519.PublicKey), + }, + } + + // create and add first block + block := createTestBlock(t, babeService, genesisHeader, [][]byte{}, 1, testEpochIndex, epochData) + block.Header.Hash() + + err = babeService.blockState.AddBlock(block) + require.NoError(t, err) + + verifier, err := newVerifier(babeService.blockState, testEpochIndex, &verifierInfo{ + authorities: epochData.authorities, + threshold: epochData.threshold, + randomness: epochData.randomness, + }) + require.NoError(t, err) + + err = verifier.verifyAuthorshipRight(&block.Header) + require.NoError(t, err) + + // create new block + block2 := createTestBlock(t, babeService, genesisHeader, [][]byte{}, 1, testEpochIndex, epochData) + block2.Header.Hash() + + err = babeService.blockState.AddBlock(block2) + require.NoError(t, err) + + err = verifier.verifyAuthorshipRight(&block2.Header) + require.Equal(t, ErrProducerEquivocated, err) +} + func TestVerifyForkBlocksWithRespectiveEpochData(t *testing.T) { /* * Setup the services: StateService, DigestHandler, EpochState diff --git a/lib/babe/verify_test.go b/lib/babe/verify_test.go index 739584f03f..f6c106ca7c 100644 --- a/lib/babe/verify_test.go +++ b/lib/babe/verify_test.go @@ -612,6 +612,27 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { //// Case 8: Get header error babeVerifier6 := newTestVerifier(t, kp, mockBlockStateErr, scale.MaxUint128, false) + // Case 9: Equivocate case primary + babeVerifier7 := newTestVerifier(t, kp, mockBlockStateEquiv1, scale.MaxUint128, false) + + // Case 10: Equivocate case secondary plain + babeSecPlainPrd2, err := testBabeSecondaryPlainPreDigest.ToPreRuntimeDigest() + assert.NoError(t, err) + header8 := newTestHeader(t, *babeSecPlainPrd2) + + hash2 := encodeAndHashHeader(t, header8) + signAndAddSeal(t, kp, header8, hash2[:]) + babeVerifier8 := newTestVerifier(t, kp, mockBlockStateEquiv2, scale.MaxUint128, true) + + // Case 11: equivocation case secondary VRF + encVrfDigest := newEncodedBabeDigest(t, testBabeSecondaryVRFPreDigest) + assert.NoError(t, err) + header9 := newTestHeader(t, *types.NewBABEPreRuntimeDigest(encVrfDigest)) + + hash3 := encodeAndHashHeader(t, header9) + signAndAddSeal(t, kp, header9, hash3[:]) + babeVerifier9 := newTestVerifier(t, kp, mockBlockStateEquiv3, scale.MaxUint128, true) + tests := []struct { name string verifier verifier @@ -676,6 +697,24 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { verifier: *babeVerifier6, header: header7, }, + { + name: "equivocate - primary", + verifier: *babeVerifier7, + header: header7, + expErr: ErrProducerEquivocated, + }, + { + name: "equivocate - secondary plain", + verifier: *babeVerifier8, + header: header8, + expErr: ErrProducerEquivocated, + }, + { + name: "equivocate - secondary vrf", + verifier: *babeVerifier9, + header: header9, + expErr: ErrProducerEquivocated, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 2e1947e7a0ec78d5050bfe5e45f71eb9a39744e7 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Thu, 16 Jun 2022 15:01:45 +0530 Subject: [PATCH 03/20] throw ErrProducerEquivocated only when both equivocatory blocks are created by primary block producer --- lib/babe/verify.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/babe/verify.go b/lib/babe/verify.go index add58392a4..171c2281b7 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -354,17 +354,38 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { continue } + currentPreDigestItem := currentHeader.Digest.Types[0] + + currentPreDigest, ok := currentPreDigestItem.Value().(types.PreRuntimeDigest) + if !ok { + return fmt.Errorf("first digest item is not pre-digest") + } + + currentBabePreDigest, err := b.verifyPreRuntimeDigest(¤tPreDigest) + if err != nil { + return fmt.Errorf("failed to verify pre-runtime digest: %w", err) + } + + var isCurrentBlockProducerPrimary bool + switch currentBabePreDigest.(type) { + case types.BabePrimaryPreDigest: + isCurrentBlockProducerPrimary = true + default: + } + + var isExistingBlockProducerPrimary bool var existingBlockProducerIndex uint32 switch d := babePreDigest.(type) { case types.BabePrimaryPreDigest: existingBlockProducerIndex = d.AuthorityIndex + isExistingBlockProducerPrimary = true case types.BabeSecondaryVRFPreDigest: existingBlockProducerIndex = d.AuthorityIndex case types.BabeSecondaryPlainPreDigest: existingBlockProducerIndex = d.AuthorityIndex } - if currentBlockProducerIndex == existingBlockProducerIndex && hash != header.Hash() { + if currentBlockProducerIndex == existingBlockProducerIndex && hash != header.Hash() && isCurrentBlockProducerPrimary == isExistingBlockProducerPrimary { return ErrProducerEquivocated } } From 1862997016dec9f84b69830975a3c953ea94fa71 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Thu, 16 Jun 2022 15:06:37 +0530 Subject: [PATCH 04/20] lint --- lib/babe/verify.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/babe/verify.go b/lib/babe/verify.go index 171c2281b7..5f0d2c86dc 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -385,7 +385,9 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { existingBlockProducerIndex = d.AuthorityIndex } - if currentBlockProducerIndex == existingBlockProducerIndex && hash != header.Hash() && isCurrentBlockProducerPrimary == isExistingBlockProducerPrimary { + if currentBlockProducerIndex == existingBlockProducerIndex && + hash != header.Hash() && + isCurrentBlockProducerPrimary == isExistingBlockProducerPrimary { return ErrProducerEquivocated } } From fcb1eaae1836371945a84b899c41e740954783d0 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Mon, 20 Jun 2022 14:45:29 +0530 Subject: [PATCH 05/20] addressed reviews --- dot/types/babe.go | 7 +++++-- lib/babe/verify.go | 10 ++++++++-- lib/babe/verify_test.go | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/dot/types/babe.go b/dot/types/babe.go index e0ebd30cf1..026a5f439c 100644 --- a/dot/types/babe.go +++ b/dot/types/babe.go @@ -4,9 +4,12 @@ package types import ( + "errors" "fmt" ) +var ErrNoFirstPreDigest = errors.New("first digest item is not pre-digest") + // RandomnessLength is the length of the epoch randomness (32 bytes) const RandomnessLength = 32 @@ -107,7 +110,7 @@ func GetSlotFromHeader(header *Header) (uint64, error) { preDigest, ok := header.Digest.Types[0].Value().(PreRuntimeDigest) if !ok { - return 0, fmt.Errorf("first digest item is not pre-digest") + return 0, ErrNoFirstPreDigest } digest, err := DecodeBabePreDigest(preDigest.Data) @@ -140,7 +143,7 @@ func IsPrimary(header *Header) (bool, error) { preDigest, ok := header.Digest.Types[0].Value().(PreRuntimeDigest) if !ok { - return false, fmt.Errorf("first digest item is not pre-digest: type=%T", header.Digest.Types[0].Value()) + return false, fmt.Errorf("%w: type=%T", ErrNoFirstPreDigest, header.Digest.Types[0].Value()) } digest, err := DecodeBabePreDigest(preDigest.Data) diff --git a/lib/babe/verify.go b/lib/babe/verify.go index 5f0d2c86dc..9ad49745a3 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -272,12 +272,13 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { logger.Tracef("beginning BABE authorship right verification for block %s", header.Hash()) // check for valid seal by verifying signature + preDigestItem := header.Digest.Types[0] sealItem := header.Digest.Types[len(header.Digest.Types)-1] preDigest, ok := preDigestItem.Value().(types.PreRuntimeDigest) if !ok { - return fmt.Errorf("first digest item is not pre-digest") + return fmt.Errorf("%w: got %T", types.ErrNoFirstPreDigest, preDigestItem.Value()) } seal, ok := sealItem.Value().(types.SealDigest) @@ -354,11 +355,16 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { continue } + if len(currentHeader.Digest.Types) == 0 { + return fmt.Errorf("current header missing digest") + } + currentPreDigestItem := currentHeader.Digest.Types[0] currentPreDigest, ok := currentPreDigestItem.Value().(types.PreRuntimeDigest) if !ok { - return fmt.Errorf("first digest item is not pre-digest") + return fmt.Errorf("%w: got %T", types.ErrNoFirstPreDigest, currentPreDigestItem.Value()) + } currentBabePreDigest, err := b.verifyPreRuntimeDigest(¤tPreDigest) diff --git a/lib/babe/verify_test.go b/lib/babe/verify_test.go index f6c106ca7c..b7b8ead7ed 100644 --- a/lib/babe/verify_test.go +++ b/lib/babe/verify_test.go @@ -649,7 +649,7 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { name: "first digest invalid", verifier: verifier{}, header: header0, - expErr: errors.New("first digest item is not pre-digest"), + expErr: types.ErrNoFirstPreDigest, }, { name: "last digest invalid", From 7e49eeb9919bf12ef4f00cf5eeb222243f7d290b Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Tue, 28 Jun 2022 17:55:47 +0530 Subject: [PATCH 06/20] some fixes --- lib/babe/errors.go | 1 + lib/babe/verify.go | 2 +- lib/babe/verify_test.go | 6 +++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/babe/errors.go b/lib/babe/errors.go index 527a0a8915..af41b73d58 100644 --- a/lib/babe/errors.go +++ b/lib/babe/errors.go @@ -86,6 +86,7 @@ var ( errServicePaused = errors.New("service paused") errInvalidSlotTechnique = errors.New("invalid slot claiming technique") errNoBABEAuthorityKeyProvided = errors.New("cannot create BABE service as authority; no keypair provided") + errLastDigestItemNotSeal = errors.New("last digest item is not seal") other Other invalidCustom InvalidCustom diff --git a/lib/babe/verify.go b/lib/babe/verify.go index 6a9374ab7d..bc03ac3b6b 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -283,7 +283,7 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { seal, ok := sealItem.Value().(types.SealDigest) if !ok { - return fmt.Errorf("last digest item is not seal") + return errLastDigestItemNotSeal } babePreDigest, err := b.verifyPreRuntimeDigest(&preDigest) diff --git a/lib/babe/verify_test.go b/lib/babe/verify_test.go index b7b8ead7ed..56f505d4e1 100644 --- a/lib/babe/verify_test.go +++ b/lib/babe/verify_test.go @@ -643,19 +643,19 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { name: "missing digest", verifier: verifier{}, header: types.NewEmptyHeader(), - expErr: errors.New("block header is missing digest items"), + expErr: errMissingDigestItems, }, { name: "first digest invalid", verifier: verifier{}, header: header0, - expErr: types.ErrNoFirstPreDigest, + expErr: fmt.Errorf("%w: got types.SealDigest", types.ErrNoFirstPreDigest), }, { name: "last digest invalid", verifier: verifier{}, header: header1, - expErr: errors.New("last digest item is not seal"), + expErr: errLastDigestItemNotSeal, }, { name: "invalid preruntime digest data", From cb1cf9cf075d10cf4c95921f539d1d990493a8c7 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Wed, 29 Jun 2022 10:41:05 +0530 Subject: [PATCH 07/20] temp --- lib/babe/verify.go | 10 ++++- lib/babe/verify_test.go | 86 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/lib/babe/verify.go b/lib/babe/verify.go index bc03ac3b6b..a1de257dc5 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -344,15 +344,18 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { // check if the producer has equivocated, ie. have they produced a conflicting block? // hashes is hashes of all blocks with same block number as header.Number hashes := b.blockState.GetAllBlocksAtDepth(header.ParentHash) - + fmt.Println("hashes") + fmt.Println(hashes) for _, hash := range hashes { currentHeader, err := b.blockState.GetHeader(hash) if err != nil { + logger.Errorf("GetHeader %s", err.Error()) continue } currentBlockProducerIndex, err := getAuthorityIndex(currentHeader) if err != nil { + logger.Errorf("getAuthorityIndex %s", err.Error()) continue } @@ -370,6 +373,7 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { currentBabePreDigest, err := b.verifyPreRuntimeDigest(¤tPreDigest) if err != nil { + fmt.Println("there u go home boy") return fmt.Errorf("failed to verify pre-runtime digest: %w", err) } @@ -432,6 +436,10 @@ func (b *verifier) verifyPreRuntimeDigest(digest *types.PreRuntimeDigest) (scale switch d := babePreDigest.(type) { case types.BabePrimaryPreDigest: ok, err = b.verifyPrimarySlotWinner(d.AuthorityIndex, d.SlotNumber, d.VRFOutput, d.VRFProof) + if !ok { + fmt.Println("types.BabePrimaryPreDigest") + } + fmt.Println(err) case types.BabeSecondaryVRFPreDigest: if !b.secondarySlots { ok = false diff --git a/lib/babe/verify_test.go b/lib/babe/verify_test.go index 56f505d4e1..59d5903d95 100644 --- a/lib/babe/verify_test.go +++ b/lib/babe/verify_test.go @@ -730,6 +730,92 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { } } +func Test_verifier_verifyAuthorshipRight2(t *testing.T) { + ctrl := gomock.NewController(t) + + mockBlockStateEquiv1 := NewMockBlockState(ctrl) + + //Generate keys + kp, err := sr25519.GenerateKeypair() + assert.NoError(t, err) + + // Create a VRF output and proof + randomness := common.MustHexToHash("0x0123") + output, proof, err := kp.VrfSign(makeTranscript(Randomness(randomness), uint64(1), 1)) + assert.NoError(t, err) + + testBabePrimaryPreDigest := types.BabePrimaryPreDigest{ + AuthorityIndex: 0, + SlotNumber: 1, + VRFOutput: output, + VRFProof: proof, + } + + // Primary Test Header + encTestDigest := newEncodedBabeDigest(t, types.BabePrimaryPreDigest{AuthorityIndex: 0}) + + testDigestPrimary := types.NewDigest() + err = testDigestPrimary.Add(types.PreRuntimeDigest{ + ConsensusEngineID: types.BabeEngineID, + Data: encTestDigest, + }) + assert.NoError(t, err) + testHeaderPrimary := types.NewEmptyHeader() + testHeaderPrimary.Digest = testDigestPrimary + + h := common.MustHexToHash("0x01") + h1 := []common.Hash{h} + + // h2 := common.MustHexToHash("0x02") + // hArray := []common.Hash{h, h2} + + mockBlockStateEquiv1.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return(h1) + mockBlockStateEquiv1.EXPECT().GetHeader(h).Return(testHeaderPrimary, nil) + + // Case 7: GetAuthorityIndex Err + babeParentPrd, err := testBabePrimaryPreDigest.ToPreRuntimeDigest() + assert.NoError(t, err) + babeParentHeader := newTestHeader(t, *babeParentPrd) + + parentHash := encodeAndHashHeader(t, babeParentHeader) + babePrd3, err := testBabePrimaryPreDigest.ToPreRuntimeDigest() + assert.NoError(t, err) + + header7 := newTestHeader(t, *babePrd3) + header7.ParentHash = parentHash + + hash := encodeAndHashHeader(t, header7) + signAndAddSeal(t, kp, header7, hash[:]) + + // Case 9: Equivocate case primary + babeVerifier7 := newTestVerifier(t, kp, mockBlockStateEquiv1, scale.MaxUint128, false) + + tests := []struct { + name string + verifier verifier + header *types.Header + expErr error + }{ + { + name: "equivocate - primary", + verifier: *babeVerifier7, + header: header7, + expErr: ErrProducerEquivocated, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := &tt.verifier + err := b.verifyAuthorshipRight(tt.header) + if tt.expErr != nil { + assert.EqualError(t, err, tt.expErr.Error()) + } else { + assert.NoError(t, err) + } + + }) + } +} func TestVerificationManager_getConfigData(t *testing.T) { ctrl := gomock.NewController(t) mockBlockState := NewMockBlockState(ctrl) From 78317598f470bbcd91b61f671f3e8f808b3264e0 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Wed, 29 Jun 2022 11:40:40 +0530 Subject: [PATCH 08/20] fix tests --- lib/babe/verify_test.go | 78 +++++++++++++---------------------------- 1 file changed, 24 insertions(+), 54 deletions(-) diff --git a/lib/babe/verify_test.go b/lib/babe/verify_test.go index 59d5903d95..bd294927fa 100644 --- a/lib/babe/verify_test.go +++ b/lib/babe/verify_test.go @@ -469,7 +469,7 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { ctrl := gomock.NewController(t) mockBlockState := NewMockBlockState(ctrl) mockBlockStateErr := NewMockBlockState(ctrl) - mockBlockStateEquiv1 := NewMockBlockState(ctrl) + // mockBlockStateEquiv1 := NewMockBlockState(ctrl) mockBlockStateEquiv2 := NewMockBlockState(ctrl) mockBlockStateEquiv3 := NewMockBlockState(ctrl) @@ -546,8 +546,8 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { mockBlockStateErr.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return(h1) mockBlockStateErr.EXPECT().GetHeader(h).Return(nil, errors.New("get header error")) - mockBlockStateEquiv1.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return(h1) - mockBlockStateEquiv1.EXPECT().GetHeader(h).Return(testHeaderPrimary, nil) + // mockBlockStateEquiv1.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return(h1) + // mockBlockStateEquiv1.EXPECT().GetHeader(h).Return(testHeaderPrimary, nil) mockBlockStateEquiv2.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return(h1) mockBlockStateEquiv2.EXPECT().GetHeader(h).Return(testSecPlainHeader, nil) @@ -613,7 +613,6 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { babeVerifier6 := newTestVerifier(t, kp, mockBlockStateErr, scale.MaxUint128, false) // Case 9: Equivocate case primary - babeVerifier7 := newTestVerifier(t, kp, mockBlockStateEquiv1, scale.MaxUint128, false) // Case 10: Equivocate case secondary plain babeSecPlainPrd2, err := testBabeSecondaryPlainPreDigest.ToPreRuntimeDigest() @@ -697,12 +696,6 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { verifier: *babeVerifier6, header: header7, }, - { - name: "equivocate - primary", - verifier: *babeVerifier7, - header: header7, - expErr: ErrProducerEquivocated, - }, { name: "equivocate - secondary plain", verifier: *babeVerifier8, @@ -730,7 +723,7 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { } } -func Test_verifier_verifyAuthorshipRight2(t *testing.T) { +func Test_verifier_verifyAuthorshipRightEquivocatory(t *testing.T) { ctrl := gomock.NewController(t) mockBlockStateEquiv1 := NewMockBlockState(ctrl) @@ -739,56 +732,33 @@ func Test_verifier_verifyAuthorshipRight2(t *testing.T) { kp, err := sr25519.GenerateKeypair() assert.NoError(t, err) - // Create a VRF output and proof - randomness := common.MustHexToHash("0x0123") - output, proof, err := kp.VrfSign(makeTranscript(Randomness(randomness), uint64(1), 1)) + //BabePrimaryPreDigest case + output, proof, err := kp.VrfSign(makeTranscript(Randomness{}, uint64(1), 1)) assert.NoError(t, err) - testBabePrimaryPreDigest := types.BabePrimaryPreDigest{ - AuthorityIndex: 0, - SlotNumber: 1, - VRFOutput: output, - VRFProof: proof, + secDigest1 := types.BabePrimaryPreDigest{ + SlotNumber: 1, + VRFOutput: output, + VRFProof: proof, } - - // Primary Test Header - encTestDigest := newEncodedBabeDigest(t, types.BabePrimaryPreDigest{AuthorityIndex: 0}) - - testDigestPrimary := types.NewDigest() - err = testDigestPrimary.Add(types.PreRuntimeDigest{ - ConsensusEngineID: types.BabeEngineID, - Data: encTestDigest, - }) + prd1, err := secDigest1.ToPreRuntimeDigest() assert.NoError(t, err) - testHeaderPrimary := types.NewEmptyHeader() - testHeaderPrimary.Digest = testDigestPrimary - - h := common.MustHexToHash("0x01") - h1 := []common.Hash{h} - // h2 := common.MustHexToHash("0x02") - // hArray := []common.Hash{h, h2} - - mockBlockStateEquiv1.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return(h1) - mockBlockStateEquiv1.EXPECT().GetHeader(h).Return(testHeaderPrimary, nil) - - // Case 7: GetAuthorityIndex Err - babeParentPrd, err := testBabePrimaryPreDigest.ToPreRuntimeDigest() - assert.NoError(t, err) - babeParentHeader := newTestHeader(t, *babeParentPrd) + auth := types.NewAuthority(kp.Public(), uint64(1)) + vi := &verifierInfo{ + authorities: []types.Authority{*auth, *auth}, + threshold: scale.MaxUint128, + } - parentHash := encodeAndHashHeader(t, babeParentHeader) - babePrd3, err := testBabePrimaryPreDigest.ToPreRuntimeDigest() + v, err := newVerifier(mockBlockStateEquiv1, 1, vi) assert.NoError(t, err) - header7 := newTestHeader(t, *babePrd3) - header7.ParentHash = parentHash - - hash := encodeAndHashHeader(t, header7) - signAndAddSeal(t, kp, header7, hash[:]) + headerEquivocatory := newTestHeader(t, *prd1) + hashEquivocatory := encodeAndHashHeader(t, headerEquivocatory) + signAndAddSeal(t, kp, headerEquivocatory, hashEquivocatory[:]) - // Case 9: Equivocate case primary - babeVerifier7 := newTestVerifier(t, kp, mockBlockStateEquiv1, scale.MaxUint128, false) + mockBlockStateEquiv1.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return([]common.Hash{hashEquivocatory}) + mockBlockStateEquiv1.EXPECT().GetHeader(hashEquivocatory).Return(headerEquivocatory, nil) tests := []struct { name string @@ -798,8 +768,8 @@ func Test_verifier_verifyAuthorshipRight2(t *testing.T) { }{ { name: "equivocate - primary", - verifier: *babeVerifier7, - header: header7, + verifier: *v, + header: headerEquivocatory, expErr: ErrProducerEquivocated, }, } From 344f5fc3e0bb9ea0cf370cf2ec9c45ebc846857b Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Wed, 29 Jun 2022 11:44:58 +0530 Subject: [PATCH 09/20] some cleanup --- lib/babe/verify.go | 10 +++------- lib/babe/verify_test.go | 6 ------ 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/lib/babe/verify.go b/lib/babe/verify.go index a1de257dc5..f446bc658d 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -272,7 +272,6 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { logger.Tracef("beginning BABE authorship right verification for block %s", header.Hash()) // check for valid seal by verifying signature - preDigestItem := header.Digest.Types[0] sealItem := header.Digest.Types[len(header.Digest.Types)-1] @@ -344,18 +343,17 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { // check if the producer has equivocated, ie. have they produced a conflicting block? // hashes is hashes of all blocks with same block number as header.Number hashes := b.blockState.GetAllBlocksAtDepth(header.ParentHash) - fmt.Println("hashes") - fmt.Println(hashes) + for _, hash := range hashes { currentHeader, err := b.blockState.GetHeader(hash) if err != nil { - logger.Errorf("GetHeader %s", err.Error()) + logger.Errorf("failed get header %s", err) continue } currentBlockProducerIndex, err := getAuthorityIndex(currentHeader) if err != nil { - logger.Errorf("getAuthorityIndex %s", err.Error()) + logger.Errorf("failed to get authority index %s", err) continue } @@ -364,7 +362,6 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { } currentPreDigestItem := currentHeader.Digest.Types[0] - currentPreDigest, ok := currentPreDigestItem.Value().(types.PreRuntimeDigest) if !ok { return fmt.Errorf("%w: got %T", types.ErrNoFirstPreDigest, currentPreDigestItem.Value()) @@ -373,7 +370,6 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { currentBabePreDigest, err := b.verifyPreRuntimeDigest(¤tPreDigest) if err != nil { - fmt.Println("there u go home boy") return fmt.Errorf("failed to verify pre-runtime digest: %w", err) } diff --git a/lib/babe/verify_test.go b/lib/babe/verify_test.go index bd294927fa..f0ca6fb2ad 100644 --- a/lib/babe/verify_test.go +++ b/lib/babe/verify_test.go @@ -469,7 +469,6 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { ctrl := gomock.NewController(t) mockBlockState := NewMockBlockState(ctrl) mockBlockStateErr := NewMockBlockState(ctrl) - // mockBlockStateEquiv1 := NewMockBlockState(ctrl) mockBlockStateEquiv2 := NewMockBlockState(ctrl) mockBlockStateEquiv3 := NewMockBlockState(ctrl) @@ -546,9 +545,6 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { mockBlockStateErr.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return(h1) mockBlockStateErr.EXPECT().GetHeader(h).Return(nil, errors.New("get header error")) - // mockBlockStateEquiv1.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return(h1) - // mockBlockStateEquiv1.EXPECT().GetHeader(h).Return(testHeaderPrimary, nil) - mockBlockStateEquiv2.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return(h1) mockBlockStateEquiv2.EXPECT().GetHeader(h).Return(testSecPlainHeader, nil) mockBlockStateEquiv3.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return(h1) @@ -612,8 +608,6 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { //// Case 8: Get header error babeVerifier6 := newTestVerifier(t, kp, mockBlockStateErr, scale.MaxUint128, false) - // Case 9: Equivocate case primary - // Case 10: Equivocate case secondary plain babeSecPlainPrd2, err := testBabeSecondaryPlainPreDigest.ToPreRuntimeDigest() assert.NoError(t, err) From 78b70c7d83eb9dc55e0467f792043877f67dfece Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Wed, 29 Jun 2022 12:30:52 +0530 Subject: [PATCH 10/20] more test fix --- lib/babe/verify.go | 4 -- lib/babe/verify_test.go | 105 +++++++++++++++++++++++++--------------- 2 files changed, 67 insertions(+), 42 deletions(-) diff --git a/lib/babe/verify.go b/lib/babe/verify.go index f446bc658d..ca48dd7a8f 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -432,10 +432,6 @@ func (b *verifier) verifyPreRuntimeDigest(digest *types.PreRuntimeDigest) (scale switch d := babePreDigest.(type) { case types.BabePrimaryPreDigest: ok, err = b.verifyPrimarySlotWinner(d.AuthorityIndex, d.SlotNumber, d.VRFOutput, d.VRFProof) - if !ok { - fmt.Println("types.BabePrimaryPreDigest") - } - fmt.Println(err) case types.BabeSecondaryVRFPreDigest: if !b.secondarySlots { ok = false diff --git a/lib/babe/verify_test.go b/lib/babe/verify_test.go index f0ca6fb2ad..503917089b 100644 --- a/lib/babe/verify_test.go +++ b/lib/babe/verify_test.go @@ -469,8 +469,6 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { ctrl := gomock.NewController(t) mockBlockState := NewMockBlockState(ctrl) mockBlockStateErr := NewMockBlockState(ctrl) - mockBlockStateEquiv2 := NewMockBlockState(ctrl) - mockBlockStateEquiv3 := NewMockBlockState(ctrl) //Generate keys kp, err := sr25519.GenerateKeypair() @@ -545,11 +543,6 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { mockBlockStateErr.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return(h1) mockBlockStateErr.EXPECT().GetHeader(h).Return(nil, errors.New("get header error")) - mockBlockStateEquiv2.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return(h1) - mockBlockStateEquiv2.EXPECT().GetHeader(h).Return(testSecPlainHeader, nil) - mockBlockStateEquiv3.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return(h1) - mockBlockStateEquiv3.EXPECT().GetHeader(h).Return(testSecVrfHeader, nil) - // Case 0: First element not preruntime digest header0 := newTestHeader(t, testInvalidSeal, testInvalidSeal) @@ -608,24 +601,6 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { //// Case 8: Get header error babeVerifier6 := newTestVerifier(t, kp, mockBlockStateErr, scale.MaxUint128, false) - // Case 10: Equivocate case secondary plain - babeSecPlainPrd2, err := testBabeSecondaryPlainPreDigest.ToPreRuntimeDigest() - assert.NoError(t, err) - header8 := newTestHeader(t, *babeSecPlainPrd2) - - hash2 := encodeAndHashHeader(t, header8) - signAndAddSeal(t, kp, header8, hash2[:]) - babeVerifier8 := newTestVerifier(t, kp, mockBlockStateEquiv2, scale.MaxUint128, true) - - // Case 11: equivocation case secondary VRF - encVrfDigest := newEncodedBabeDigest(t, testBabeSecondaryVRFPreDigest) - assert.NoError(t, err) - header9 := newTestHeader(t, *types.NewBABEPreRuntimeDigest(encVrfDigest)) - - hash3 := encodeAndHashHeader(t, header9) - signAndAddSeal(t, kp, header9, hash3[:]) - babeVerifier9 := newTestVerifier(t, kp, mockBlockStateEquiv3, scale.MaxUint128, true) - tests := []struct { name string verifier verifier @@ -690,18 +665,6 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { verifier: *babeVerifier6, header: header7, }, - { - name: "equivocate - secondary plain", - verifier: *babeVerifier8, - header: header8, - expErr: ErrProducerEquivocated, - }, - { - name: "equivocate - secondary vrf", - verifier: *babeVerifier9, - header: header9, - expErr: ErrProducerEquivocated, - }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -721,15 +684,28 @@ func Test_verifier_verifyAuthorshipRightEquivocatory(t *testing.T) { ctrl := gomock.NewController(t) mockBlockStateEquiv1 := NewMockBlockState(ctrl) + mockBlockStateEquiv2 := NewMockBlockState(ctrl) + mockBlockStateEquiv3 := NewMockBlockState(ctrl) //Generate keys kp, err := sr25519.GenerateKeypair() assert.NoError(t, err) - //BabePrimaryPreDigest case output, proof, err := kp.VrfSign(makeTranscript(Randomness{}, uint64(1), 1)) assert.NoError(t, err) + testBabeSecondaryPlainPreDigest := types.BabeSecondaryPlainPreDigest{ + AuthorityIndex: 1, + SlotNumber: 1, + } + testBabeSecondaryVRFPreDigest := types.BabeSecondaryVRFPreDigest{ + AuthorityIndex: 1, + SlotNumber: 1, + VrfOutput: output, + VrfProof: proof, + } + + //BabePrimaryPreDigest case secDigest1 := types.BabePrimaryPreDigest{ SlotNumber: 1, VRFOutput: output, @@ -754,6 +730,47 @@ func Test_verifier_verifyAuthorshipRightEquivocatory(t *testing.T) { mockBlockStateEquiv1.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return([]common.Hash{hashEquivocatory}) mockBlockStateEquiv1.EXPECT().GetHeader(hashEquivocatory).Return(headerEquivocatory, nil) + // Secondary Plain Test Header + testParentPrd, err := testBabeSecondaryPlainPreDigest.ToPreRuntimeDigest() + assert.NoError(t, err) + testParentHeader := newTestHeader(t, *testParentPrd) + + testParentHash := encodeAndHashHeader(t, testParentHeader) + testSecondaryPrd, err := testBabeSecondaryPlainPreDigest.ToPreRuntimeDigest() + assert.NoError(t, err) + testSecPlainHeader := newTestHeader(t, *testSecondaryPrd) + testSecPlainHeader.ParentHash = testParentHash + + babeSecPlainPrd2, err := testBabeSecondaryPlainPreDigest.ToPreRuntimeDigest() + assert.NoError(t, err) + headerEquivocatorySecondaryPlain := newTestHeader(t, *babeSecPlainPrd2) + + hashEquivocatorySecondaryPlain := encodeAndHashHeader(t, headerEquivocatorySecondaryPlain) + signAndAddSeal(t, kp, headerEquivocatorySecondaryPlain, hashEquivocatorySecondaryPlain[:]) + babeVerifier8 := newTestVerifier(t, kp, mockBlockStateEquiv2, scale.MaxUint128, true) + + mockBlockStateEquiv2.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return([]common.Hash{hashEquivocatorySecondaryPlain}) + mockBlockStateEquiv2.EXPECT().GetHeader(hashEquivocatorySecondaryPlain).Return(headerEquivocatorySecondaryPlain, nil) + + // Secondary Vrf Test Header + encParentVrfDigest := newEncodedBabeDigest(t, testBabeSecondaryVRFPreDigest) + testParentVrfHeader := newTestHeader(t, *types.NewBABEPreRuntimeDigest(encParentVrfDigest)) + + testVrfParentHash := encodeAndHashHeader(t, testParentVrfHeader) + encVrfHeader := newEncodedBabeDigest(t, testBabeSecondaryVRFPreDigest) + testSecVrfHeader := newTestHeader(t, *types.NewBABEPreRuntimeDigest(encVrfHeader)) + testSecVrfHeader.ParentHash = testVrfParentHash + + encVrfDigest := newEncodedBabeDigest(t, testBabeSecondaryVRFPreDigest) + assert.NoError(t, err) + headerEquivocatorySecondaryVRF := newTestHeader(t, *types.NewBABEPreRuntimeDigest(encVrfDigest)) + + hashEquivocatorySecondaryVRF := encodeAndHashHeader(t, headerEquivocatorySecondaryVRF) + signAndAddSeal(t, kp, headerEquivocatorySecondaryVRF, hashEquivocatorySecondaryVRF[:]) + babeVerifierEquivocatorySecondaryVRF := newTestVerifier(t, kp, mockBlockStateEquiv3, scale.MaxUint128, true) + mockBlockStateEquiv3.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return([]common.Hash{hashEquivocatorySecondaryVRF}) + mockBlockStateEquiv3.EXPECT().GetHeader(hashEquivocatorySecondaryVRF).Return(headerEquivocatorySecondaryVRF, nil) + tests := []struct { name string verifier verifier @@ -766,6 +783,18 @@ func Test_verifier_verifyAuthorshipRightEquivocatory(t *testing.T) { header: headerEquivocatory, expErr: ErrProducerEquivocated, }, + { + name: "equivocate - secondary plain", + verifier: *babeVerifier8, + header: headerEquivocatorySecondaryPlain, + expErr: ErrProducerEquivocated, + }, + { + name: "equivocate - secondary vrf", + verifier: *babeVerifierEquivocatorySecondaryVRF, + header: headerEquivocatorySecondaryVRF, + expErr: ErrProducerEquivocated, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 34b90ee5d454baebf7a3555c84c1a50ae70b825f Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Wed, 29 Jun 2022 13:30:51 +0530 Subject: [PATCH 11/20] fix more things --- lib/babe/verify_test.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/babe/verify_test.go b/lib/babe/verify_test.go index 503917089b..88dbfd787d 100644 --- a/lib/babe/verify_test.go +++ b/lib/babe/verify_test.go @@ -720,15 +720,15 @@ func Test_verifier_verifyAuthorshipRightEquivocatory(t *testing.T) { threshold: scale.MaxUint128, } - v, err := newVerifier(mockBlockStateEquiv1, 1, vi) + verifierEquivocatoryPrimary, err := newVerifier(mockBlockStateEquiv1, 1, vi) assert.NoError(t, err) - headerEquivocatory := newTestHeader(t, *prd1) - hashEquivocatory := encodeAndHashHeader(t, headerEquivocatory) - signAndAddSeal(t, kp, headerEquivocatory, hashEquivocatory[:]) + headerEquivocatoryPrimary := newTestHeader(t, *prd1) + hashEquivocatoryPrimary := encodeAndHashHeader(t, headerEquivocatoryPrimary) + signAndAddSeal(t, kp, headerEquivocatoryPrimary, hashEquivocatoryPrimary[:]) - mockBlockStateEquiv1.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return([]common.Hash{hashEquivocatory}) - mockBlockStateEquiv1.EXPECT().GetHeader(hashEquivocatory).Return(headerEquivocatory, nil) + mockBlockStateEquiv1.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return([]common.Hash{hashEquivocatoryPrimary}) + mockBlockStateEquiv1.EXPECT().GetHeader(hashEquivocatoryPrimary).Return(headerEquivocatoryPrimary, nil) // Secondary Plain Test Header testParentPrd, err := testBabeSecondaryPlainPreDigest.ToPreRuntimeDigest() @@ -779,8 +779,8 @@ func Test_verifier_verifyAuthorshipRightEquivocatory(t *testing.T) { }{ { name: "equivocate - primary", - verifier: *v, - header: headerEquivocatory, + verifier: *verifierEquivocatoryPrimary, + header: headerEquivocatoryPrimary, expErr: ErrProducerEquivocated, }, { @@ -809,6 +809,7 @@ func Test_verifier_verifyAuthorshipRightEquivocatory(t *testing.T) { }) } } + func TestVerificationManager_getConfigData(t *testing.T) { ctrl := gomock.NewController(t) mockBlockState := NewMockBlockState(ctrl) From db33dcf7f622ea8152831486339e10738891f223 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Thu, 30 Jun 2022 11:39:23 +0530 Subject: [PATCH 12/20] Update lib/babe/verify.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Eclésio Junior --- lib/babe/verify.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/babe/verify.go b/lib/babe/verify.go index ca48dd7a8f..c3a9fbb8b5 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -394,7 +394,7 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { // same authority won't produce two different blocks at the same block number as primary block producer if currentBlockProducerIndex == existingBlockProducerIndex && - hash != header.Hash() && + hash.Equals(header.Hash()) && isCurrentBlockProducerPrimary == isExistingBlockProducerPrimary { return ErrProducerEquivocated } From d5989013cc50c4aee620324ab34d6eff3e176cd8 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Mon, 4 Jul 2022 20:40:06 +0530 Subject: [PATCH 13/20] addressed review --- lib/babe/verify.go | 8 ++++---- lib/babe/verify_test.go | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/babe/verify.go b/lib/babe/verify.go index ca48dd7a8f..b08591f933 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -344,8 +344,8 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { // hashes is hashes of all blocks with same block number as header.Number hashes := b.blockState.GetAllBlocksAtDepth(header.ParentHash) - for _, hash := range hashes { - currentHeader, err := b.blockState.GetHeader(hash) + for _, currentHash := range hashes { + currentHeader, err := b.blockState.GetHeader(currentHash) if err != nil { logger.Errorf("failed get header %s", err) continue @@ -358,7 +358,7 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { } if len(currentHeader.Digest.Types) == 0 { - return fmt.Errorf("current header missing digest") + logger.Errorf("current header missing digest") } currentPreDigestItem := currentHeader.Digest.Types[0] @@ -394,7 +394,7 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { // same authority won't produce two different blocks at the same block number as primary block producer if currentBlockProducerIndex == existingBlockProducerIndex && - hash != header.Hash() && + currentHash != header.Hash() && isCurrentBlockProducerPrimary == isExistingBlockProducerPrimary { return ErrProducerEquivocated } diff --git a/lib/babe/verify_test.go b/lib/babe/verify_test.go index 88dbfd787d..ff089bd1e8 100644 --- a/lib/babe/verify_test.go +++ b/lib/babe/verify_test.go @@ -727,7 +727,7 @@ func Test_verifier_verifyAuthorshipRightEquivocatory(t *testing.T) { hashEquivocatoryPrimary := encodeAndHashHeader(t, headerEquivocatoryPrimary) signAndAddSeal(t, kp, headerEquivocatoryPrimary, hashEquivocatoryPrimary[:]) - mockBlockStateEquiv1.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return([]common.Hash{hashEquivocatoryPrimary}) + mockBlockStateEquiv1.EXPECT().GetAllBlocksAtDepth(headerEquivocatoryPrimary.ParentHash).Return([]common.Hash{hashEquivocatoryPrimary}) mockBlockStateEquiv1.EXPECT().GetHeader(hashEquivocatoryPrimary).Return(headerEquivocatoryPrimary, nil) // Secondary Plain Test Header @@ -749,7 +749,7 @@ func Test_verifier_verifyAuthorshipRightEquivocatory(t *testing.T) { signAndAddSeal(t, kp, headerEquivocatorySecondaryPlain, hashEquivocatorySecondaryPlain[:]) babeVerifier8 := newTestVerifier(t, kp, mockBlockStateEquiv2, scale.MaxUint128, true) - mockBlockStateEquiv2.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return([]common.Hash{hashEquivocatorySecondaryPlain}) + mockBlockStateEquiv2.EXPECT().GetAllBlocksAtDepth(headerEquivocatorySecondaryPlain.ParentHash).Return([]common.Hash{hashEquivocatorySecondaryPlain}) mockBlockStateEquiv2.EXPECT().GetHeader(hashEquivocatorySecondaryPlain).Return(headerEquivocatorySecondaryPlain, nil) // Secondary Vrf Test Header @@ -768,7 +768,7 @@ func Test_verifier_verifyAuthorshipRightEquivocatory(t *testing.T) { hashEquivocatorySecondaryVRF := encodeAndHashHeader(t, headerEquivocatorySecondaryVRF) signAndAddSeal(t, kp, headerEquivocatorySecondaryVRF, hashEquivocatorySecondaryVRF[:]) babeVerifierEquivocatorySecondaryVRF := newTestVerifier(t, kp, mockBlockStateEquiv3, scale.MaxUint128, true) - mockBlockStateEquiv3.EXPECT().GetAllBlocksAtDepth(gomock.Any()).Return([]common.Hash{hashEquivocatorySecondaryVRF}) + mockBlockStateEquiv3.EXPECT().GetAllBlocksAtDepth(headerEquivocatorySecondaryVRF.ParentHash).Return([]common.Hash{hashEquivocatorySecondaryVRF}) mockBlockStateEquiv3.EXPECT().GetHeader(hashEquivocatorySecondaryVRF).Return(headerEquivocatorySecondaryVRF, nil) tests := []struct { From b27f088b4bec1e5c79af4f69391cdeb159dac7aa Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Mon, 4 Jul 2022 20:48:12 +0530 Subject: [PATCH 14/20] fix lint --- lib/babe/verify_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/babe/verify_test.go b/lib/babe/verify_test.go index ff089bd1e8..63bb920da2 100644 --- a/lib/babe/verify_test.go +++ b/lib/babe/verify_test.go @@ -727,7 +727,8 @@ func Test_verifier_verifyAuthorshipRightEquivocatory(t *testing.T) { hashEquivocatoryPrimary := encodeAndHashHeader(t, headerEquivocatoryPrimary) signAndAddSeal(t, kp, headerEquivocatoryPrimary, hashEquivocatoryPrimary[:]) - mockBlockStateEquiv1.EXPECT().GetAllBlocksAtDepth(headerEquivocatoryPrimary.ParentHash).Return([]common.Hash{hashEquivocatoryPrimary}) + mockBlockStateEquiv1.EXPECT().GetAllBlocksAtDepth(headerEquivocatoryPrimary.ParentHash).Return( + []common.Hash{hashEquivocatoryPrimary}) mockBlockStateEquiv1.EXPECT().GetHeader(hashEquivocatoryPrimary).Return(headerEquivocatoryPrimary, nil) // Secondary Plain Test Header @@ -749,7 +750,8 @@ func Test_verifier_verifyAuthorshipRightEquivocatory(t *testing.T) { signAndAddSeal(t, kp, headerEquivocatorySecondaryPlain, hashEquivocatorySecondaryPlain[:]) babeVerifier8 := newTestVerifier(t, kp, mockBlockStateEquiv2, scale.MaxUint128, true) - mockBlockStateEquiv2.EXPECT().GetAllBlocksAtDepth(headerEquivocatorySecondaryPlain.ParentHash).Return([]common.Hash{hashEquivocatorySecondaryPlain}) + mockBlockStateEquiv2.EXPECT().GetAllBlocksAtDepth(headerEquivocatorySecondaryPlain.ParentHash).Return( + []common.Hash{hashEquivocatorySecondaryPlain}) mockBlockStateEquiv2.EXPECT().GetHeader(hashEquivocatorySecondaryPlain).Return(headerEquivocatorySecondaryPlain, nil) // Secondary Vrf Test Header @@ -768,7 +770,8 @@ func Test_verifier_verifyAuthorshipRightEquivocatory(t *testing.T) { hashEquivocatorySecondaryVRF := encodeAndHashHeader(t, headerEquivocatorySecondaryVRF) signAndAddSeal(t, kp, headerEquivocatorySecondaryVRF, hashEquivocatorySecondaryVRF[:]) babeVerifierEquivocatorySecondaryVRF := newTestVerifier(t, kp, mockBlockStateEquiv3, scale.MaxUint128, true) - mockBlockStateEquiv3.EXPECT().GetAllBlocksAtDepth(headerEquivocatorySecondaryVRF.ParentHash).Return([]common.Hash{hashEquivocatorySecondaryVRF}) + mockBlockStateEquiv3.EXPECT().GetAllBlocksAtDepth(headerEquivocatorySecondaryVRF.ParentHash).Return( + []common.Hash{hashEquivocatorySecondaryVRF}) mockBlockStateEquiv3.EXPECT().GetHeader(hashEquivocatorySecondaryVRF).Return(headerEquivocatorySecondaryVRF, nil) tests := []struct { From 387074af5cf6c43be76a02126fdad07111508a1d Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Tue, 5 Jul 2022 20:11:10 +0530 Subject: [PATCH 15/20] addressed some more reviews --- lib/babe/verify.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/babe/verify.go b/lib/babe/verify.go index bd400e799c..ed4509c5a3 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -282,7 +282,7 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { seal, ok := sealItem.Value().(types.SealDigest) if !ok { - return errLastDigestItemNotSeal + return fmt.Errorf("%w: got %T", errLastDigestItemNotSeal, sealItem.Value()) } babePreDigest, err := b.verifyPreRuntimeDigest(&preDigest) @@ -394,7 +394,7 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { // same authority won't produce two different blocks at the same block number as primary block producer if currentBlockProducerIndex == existingBlockProducerIndex && - currentHash.Equal(header.Hash()) && + !currentHash.Equal(header.Hash()) && isCurrentBlockProducerPrimary == isExistingBlockProducerPrimary { return ErrProducerEquivocated } From e039a26e39d80aa7dbdbeb196dfd1641a441a2e8 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Thu, 7 Jul 2022 10:06:18 +0530 Subject: [PATCH 16/20] Update lib/babe/verify.go Co-authored-by: Timothy Wu --- lib/babe/verify.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/babe/verify.go b/lib/babe/verify.go index ed4509c5a3..20d0f4f67e 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -373,12 +373,7 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { return fmt.Errorf("failed to verify pre-runtime digest: %w", err) } - var isCurrentBlockProducerPrimary bool - switch currentBabePreDigest.(type) { - case types.BabePrimaryPreDigest: - isCurrentBlockProducerPrimary = true - default: - } + _, isCurrentBlockProducerPrimary := currentBabePreDigest.(types.BabePrimaryPreDigest) var isExistingBlockProducerPrimary bool var existingBlockProducerIndex uint32 From e0572ce6199905f379112eff71dbe832cb3040aa Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Thu, 7 Jul 2022 10:08:34 +0530 Subject: [PATCH 17/20] addressed more reviews --- dot/types/babe.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dot/types/babe.go b/dot/types/babe.go index 026a5f439c..ab30b0f119 100644 --- a/dot/types/babe.go +++ b/dot/types/babe.go @@ -110,7 +110,7 @@ func GetSlotFromHeader(header *Header) (uint64, error) { preDigest, ok := header.Digest.Types[0].Value().(PreRuntimeDigest) if !ok { - return 0, ErrNoFirstPreDigest + return 0, fmt.Errorf("%w: got %T", ErrNoFirstPreDigest, header.Digest.Types[0].Value()) } digest, err := DecodeBabePreDigest(preDigest.Data) @@ -143,7 +143,7 @@ func IsPrimary(header *Header) (bool, error) { preDigest, ok := header.Digest.Types[0].Value().(PreRuntimeDigest) if !ok { - return false, fmt.Errorf("%w: type=%T", ErrNoFirstPreDigest, header.Digest.Types[0].Value()) + return false, fmt.Errorf("%w: got %T", ErrNoFirstPreDigest, header.Digest.Types[0].Value()) } digest, err := DecodeBabePreDigest(preDigest.Data) From e2149be6ab5a4dda2debbf90817aacb6b5ebe66b Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Thu, 7 Jul 2022 11:10:11 +0530 Subject: [PATCH 18/20] fixing test --- lib/babe/verify_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/babe/verify_test.go b/lib/babe/verify_test.go index 63bb920da2..3c0b9634e8 100644 --- a/lib/babe/verify_test.go +++ b/lib/babe/verify_test.go @@ -623,7 +623,7 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { name: "last digest invalid", verifier: verifier{}, header: header1, - expErr: errLastDigestItemNotSeal, + expErr: fmt.Errorf("%w: got types.PreRuntimeDigest", errLastDigestItemNotSeal), }, { name: "invalid preruntime digest data", From 8c59cefc91ca44eb03a45951e49ad66ecad28980 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Mon, 11 Jul 2022 08:36:40 +0530 Subject: [PATCH 19/20] fixed more things --- lib/babe/verify.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/babe/verify.go b/lib/babe/verify.go index 20d0f4f67e..174c93aac7 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -347,25 +347,22 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { for _, currentHash := range hashes { currentHeader, err := b.blockState.GetHeader(currentHash) if err != nil { - logger.Errorf("failed get header %s", err) - continue + return fmt.Errorf("failed get header %s", err) } currentBlockProducerIndex, err := getAuthorityIndex(currentHeader) if err != nil { - logger.Errorf("failed to get authority index %s", err) - continue + return fmt.Errorf("failed to get authority index %s", err) } if len(currentHeader.Digest.Types) == 0 { - logger.Errorf("current header missing digest") + return fmt.Errorf("current header missing digest") } currentPreDigestItem := currentHeader.Digest.Types[0] currentPreDigest, ok := currentPreDigestItem.Value().(types.PreRuntimeDigest) if !ok { return fmt.Errorf("%w: got %T", types.ErrNoFirstPreDigest, currentPreDigestItem.Value()) - } currentBabePreDigest, err := b.verifyPreRuntimeDigest(¤tPreDigest) From 1a44c28f908604c38c21ffb06074c83ffd5d59b5 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Tue, 12 Jul 2022 09:47:14 +0530 Subject: [PATCH 20/20] fixed some tests --- lib/babe/verify_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/babe/verify_test.go b/lib/babe/verify_test.go index 71cf7f534d..9b2adf9e8b 100644 --- a/lib/babe/verify_test.go +++ b/lib/babe/verify_test.go @@ -660,11 +660,13 @@ func Test_verifier_verifyAuthorshipRight(t *testing.T) { name: "valid digest items, getAuthorityIndex error", verifier: *babeVerifier5, header: header7, + expErr: errors.New("failed to get authority index no digest provided"), }, { name: "get header err", verifier: *babeVerifier6, header: header7, + expErr: errors.New("failed get header get header error"), }, } for _, tt := range tests {