-
Notifications
You must be signed in to change notification settings - Fork 129
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
feat(lib/babe): add check of types.ConfigData.SecondarySlots for disabling secondary verification #1910
feat(lib/babe): add check of types.ConfigData.SecondarySlots for disabling secondary verification #1910
Changes from 16 commits
c8b46b5
250986d
7d6f8cd
3692fd0
09e702c
d25c781
c26553d
4e2f925
4ef4652
ee0900d
1e221c3
b6930bb
83a4591
ac41b7a
2ae9e0f
b4f4fad
da13828
1c21820
73f08b5
1ee9ffc
1d70d13
fb16b0b
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 |
---|---|---|
|
@@ -31,9 +31,10 @@ import ( | |
// verifierInfo contains the information needed to verify blocks | ||
// it remains the same for an epoch | ||
type verifierInfo struct { | ||
authorities []types.Authority | ||
randomness Randomness | ||
threshold *common.Uint128 | ||
authorities []types.Authority | ||
randomness Randomness | ||
threshold *common.Uint128 | ||
secondarySlots bool | ||
} | ||
|
||
// onDisabledInfo contains information about an authority that's been disabled at a certain | ||
|
@@ -224,9 +225,10 @@ func (v *VerificationManager) getVerifierInfo(epoch uint64) (*verifierInfo, erro | |
} | ||
|
||
return &verifierInfo{ | ||
authorities: epochData.Authorities, | ||
randomness: epochData.Randomness, | ||
threshold: threshold, | ||
authorities: epochData.Authorities, | ||
randomness: epochData.Randomness, | ||
threshold: threshold, | ||
secondarySlots: configData.SecondarySlots > 0, | ||
}, nil | ||
} | ||
|
||
|
@@ -247,11 +249,12 @@ func (v *VerificationManager) getConfigData(epoch uint64) (*types.ConfigData, er | |
|
||
// verifier is a BABE verifier for a specific authority set, randomness, and threshold | ||
type verifier struct { | ||
blockState BlockState | ||
epoch uint64 | ||
authorities []types.Authority | ||
randomness Randomness | ||
threshold *common.Uint128 | ||
blockState BlockState | ||
epoch uint64 | ||
authorities []types.Authority | ||
randomness Randomness | ||
threshold *common.Uint128 | ||
secondarySlots bool | ||
} | ||
|
||
// newVerifier returns a Verifier for the epoch described by the given descriptor | ||
|
@@ -261,11 +264,12 @@ func newVerifier(blockState BlockState, epoch uint64, info *verifierInfo) (*veri | |
} | ||
|
||
return &verifier{ | ||
blockState: blockState, | ||
epoch: epoch, | ||
authorities: info.authorities, | ||
randomness: info.randomness, | ||
threshold: info.threshold, | ||
blockState: blockState, | ||
epoch: epoch, | ||
authorities: info.authorities, | ||
randomness: info.randomness, | ||
threshold: info.threshold, | ||
secondarySlots: info.secondarySlots, | ||
}, nil | ||
} | ||
|
||
|
@@ -404,20 +408,33 @@ func (b *verifier) verifyPreRuntimeDigest(digest *types.PreRuntimeDigest) (scale | |
var ( | ||
ok bool | ||
) | ||
|
||
fmt.Printf("d type %T\n", babePreDigest) | ||
switch d := babePreDigest.(type) { | ||
case types.BabePrimaryPreDigest: | ||
ok, err = b.verifyPrimarySlotWinner(d.AuthorityIndex, d.SlotNumber, d.VRFOutput, d.VRFProof) | ||
case types.BabeSecondaryVRFPreDigest: | ||
if !b.secondarySlots { | ||
ok = false | ||
break | ||
} | ||
pub := b.authorities[d.AuthorityIndex].Key | ||
var pk *sr25519.PublicKey | ||
pk, err = sr25519.NewPublicKey(pub.Encode()) | ||
|
||
pk, err := sr25519.NewPublicKey(pub.Encode()) // nolint | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ok, err = verifySecondarySlotVRF(&d, pk, b.epoch, len(b.authorities), b.randomness) // nolint | ||
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. Same here, let's not use 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. I removed //nolint tag completely, and linter no longer complains, has something changed? 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. Well yes, |
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ok, err = verifySecondarySlotVRF(&d, pk, b.epoch, len(b.authorities), b.randomness) | ||
case types.BabeSecondaryPlainPreDigest: | ||
if !b.secondarySlots { | ||
ok = false | ||
break | ||
} | ||
|
||
ok = true | ||
err = verifySecondarySlotPlain(d.AuthorityIndex, d.SlotNumber, len(b.authorities), b.randomness) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,19 +18,21 @@ package babe | |
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"math/big" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ChainSafe/gossamer/lib/genesis" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ChainSafe/gossamer/dot/state" | ||
"github.com/ChainSafe/gossamer/dot/types" | ||
"github.com/ChainSafe/gossamer/lib/common" | ||
"github.com/ChainSafe/gossamer/lib/crypto/sr25519" | ||
|
||
"github.com/ChainSafe/gossamer/lib/genesis" | ||
"github.com/ChainSafe/gossamer/pkg/scale" | ||
log "github.com/ChainSafe/log15" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func newTestVerificationManager(t *testing.T, genCfg *types.BabeConfiguration) *VerificationManager { | ||
|
@@ -162,6 +164,94 @@ func TestVerificationManager_VerifyBlock_Ok(t *testing.T) { | |
require.NoError(t, err) | ||
} | ||
|
||
func TestVerificationManager_VerifyBlock_Secondary(t *testing.T) { | ||
babeService := createTestService(t, nil) | ||
rt, err := babeService.blockState.GetRuntime(nil) | ||
require.NoError(t, err) | ||
|
||
cfg, err := rt.BabeConfiguration() | ||
require.NoError(t, err) | ||
|
||
cfg.GenesisAuthorities = types.AuthoritiesToRaw(babeService.epochData.authorities) | ||
cfg.C1 = 1 | ||
cfg.C2 = 1 | ||
cfg.SecondarySlots = 0 | ||
|
||
vm := newTestVerificationManager(t, cfg) | ||
|
||
kp, err := sr25519.GenerateKeypair() | ||
require.NoError(t, err) | ||
|
||
dig := createSecondaryVRFPreDigest(t, kp, 0, uint64(0), uint64(0), Randomness{}) | ||
|
||
// todo (ed) remove after fix (see test below TestCreateSecondaryVRFPreDigestMarshal | ||
fmt.Printf("dig %T\n", dig) | ||
|
||
digEnc, err := scale.Marshal(dig) | ||
require.NoError(t, err) | ||
|
||
babePreDigest, err := types.DecodeBabePreDigest(digEnc) | ||
// todo (ed) remove after fix (see test below TestCreateSecondaryVRFPreDigestMarshal | ||
fmt.Printf("decoded %T\n", babePreDigest) | ||
|
||
// create pre-digest | ||
preDigest := &types.PreRuntimeDigest{ | ||
ConsensusEngineID: types.BabeEngineID, | ||
Data: digEnc, | ||
} | ||
|
||
// create new block header | ||
number := big.NewInt(1) | ||
digest := types.NewDigest() | ||
err = digest.Add(*preDigest) | ||
require.NoError(t, err) | ||
|
||
// create seal and add to digest | ||
seal := &types.SealDigest{ | ||
ConsensusEngineID: types.BabeEngineID, | ||
Data: []byte{0}, | ||
} | ||
require.NoError(t, err) | ||
|
||
err = digest.Add(*seal) | ||
require.NoError(t, err) | ||
|
||
header, err := types.NewHeader(common.Hash{}, common.Hash{}, common.Hash{}, number, digest) | ||
require.NoError(t, err) | ||
|
||
block := types.Block{ | ||
Header: *header, | ||
Body: nil, | ||
} | ||
err = vm.VerifyBlock(&block.Header) | ||
require.EqualError(t, err, "failed to verify pre-runtime digest: could not verify slot claim VRF proof") | ||
} | ||
|
||
func TestCreateSecondaryVRFPreDigestMarshal(t *testing.T) { | ||
kp, err := sr25519.GenerateKeypair() | ||
require.NoError(t, err) | ||
|
||
dig := createSecondaryVRFPreDigest(t, kp, 0, uint64(0), uint64(0), Randomness{}) | ||
fmt.Printf("dig %T\n", dig) | ||
|
||
require.IsType(t, types.BabeSecondaryVRFPreDigest{}, *dig) | ||
|
||
// This used to be done with dig.Encode() | ||
digEnc, err := scale.Marshal(dig) | ||
require.NoError(t, err) | ||
|
||
babeDigest := types.NewBabeDigest() | ||
err = scale.Unmarshal(digEnc, &babeDigest) | ||
require.NoError(t, err) | ||
// Why isn't this type types.BabeSecondaryVRFPreDigest? | ||
require.IsType(t, types.BabePrimaryPreDigest{}, babeDigest) | ||
|
||
babePreDigest, err := types.DecodeBabePreDigest(digEnc) | ||
require.NoError(t, err) | ||
// Why isn't this type types.BabeSecondaryVRFPreDigest? | ||
require.IsType(t, types.BabePrimaryPreDigest{}, babePreDigest) | ||
} | ||
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. @jimjbrettj @timwu20 I had old code that was encoding with 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.
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. I'm trying to setup conditions to test digest type However, now that there is no longer an That's what I'm trying to accomplish, @timwu20 yes I'm happy to sync to discuss this. 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. I think like TIm said you need to use the 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. Yes, I've updated this, it's working now. |
||
|
||
func TestVerificationManager_VerifyBlock_MultipleEpochs(t *testing.T) { | ||
babeService := createTestService(t, nil) | ||
rt, err := babeService.blockState.GetRuntime(nil) | ||
|
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.
Assuming this is because of govet's funny shadowing detection feature, use this instead:
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.
I removed //nolint tag completely, and linter no longer complains, has something changed?
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.
Well yes,
golangci-lint
version wasn't pinned in the CI (see #1979), so maybe that was a false positive back months ago and now it doesn't detect it anymore.