Skip to content
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

Merged
merged 22 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c8b46b5
add check for types.ConfigData.SecondarySlots
edwardmack Oct 19, 2021
250986d
Merge branch 'development' into ed/use_types_configdata_secondarySlots
edwardmack Oct 20, 2021
7d6f8cd
Update lib/babe/verify.go
edwardmack Oct 22, 2021
3692fd0
Merge branch 'development' into ed/use_types_configdata_secondarySlots
edwardmack Oct 22, 2021
09e702c
Merge branch 'development' into ed/use_types_configdata_secondarySlots
edwardmack Oct 25, 2021
d25c781
update ok = false if !b.secondarySlots
edwardmack Oct 25, 2021
c26553d
Merge branch 'development' into ed/use_types_configdata_secondarySlots
edwardmack Oct 26, 2021
4e2f925
Merge branch 'development' into ed/use_types_configdata_secondarySlots
edwardmack Oct 26, 2021
4ef4652
stub test for secondary verification
edwardmack Oct 27, 2021
ee0900d
add test to verify secondary digest
edwardmack Oct 27, 2021
1e221c3
Merge branch 'development' into ed/use_types_configdata_secondarySlots
edwardmack Oct 29, 2021
b6930bb
address PR comments
edwardmack Oct 29, 2021
83a4591
add err check
edwardmack Oct 29, 2021
ac41b7a
update pointers to fix tests
edwardmack Nov 1, 2021
2ae9e0f
Merge branch 'development' into ed/use_types_configdata_secondarySlots
edwardmack Nov 1, 2021
b4f4fad
add tests for decoding
edwardmack Nov 1, 2021
da13828
my wip
timwu20 Nov 2, 2021
1c21820
fix VRF encoding
edwardmack Nov 2, 2021
73f08b5
Merge branch 'development' into ed/use_types_configdata_secondarySlots
edwardmack Nov 2, 2021
1ee9ffc
Merge branch 'development' into ed/use_types_configdata_secondarySlots
edwardmack Nov 3, 2021
1d70d13
update linter tags
edwardmack Nov 3, 2021
fb16b0b
Merge branch 'development' into ed/use_types_configdata_secondarySlots
edwardmack Nov 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dot/types/babe.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (d *EpochDataRaw) ToEpochData() (*EpochData, error) {
type ConfigData struct {
C1 uint64
C2 uint64
SecondarySlots byte // TODO: this is unused, will need to update BABE verifier to use this (#1863)
SecondarySlots byte
}

// GetSlotFromHeader returns the BABE slot from the given header
Expand Down
55 changes: 36 additions & 19 deletions lib/babe/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -409,15 +413,28 @@ func (b *verifier) verifyPreRuntimeDigest(digest *types.PreRuntimeDigest) (scale
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
Copy link
Contributor

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:

Suggested change
pk, err := sr25519.NewPublicKey(pub.Encode()) // nolint
pk, err := sr25519.NewPublicKey(pub.Encode()) //nolint:govet

Copy link
Member Author

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?

Copy link
Contributor

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.

if err != nil {
return nil, err
}

ok, err = verifySecondarySlotVRF(&d, pk, b.epoch, len(b.authorities), b.randomness) // nolint
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, let's not use // nolint anymore. You can specify the linter you want to be ignored with //nolint:lintername. Note there is no space between // and nolint.

Copy link
Member Author

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?

Copy link
Contributor

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, so maybe that was a false positive back months ago and now it doesn't detect it anymore. We should remove these // nolint comments sometime soon since they could be hiding potentially bad code too.

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)
}
Expand Down
69 changes: 65 additions & 4 deletions lib/babe/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ package babe
import (
"errors"
"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 {
Expand Down Expand Up @@ -162,6 +163,66 @@ 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{})

bd := types.NewBabeDigest()
err = bd.Set(dig)
require.NoError(t, err)

bdEnc, err := scale.Marshal(bd)
require.NoError(t, err)

// create pre-digest
preDigest := &types.PreRuntimeDigest{
ConsensusEngineID: types.BabeEngineID,
Data: bdEnc,
}

// 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 TestVerificationManager_VerifyBlock_MultipleEpochs(t *testing.T) {
babeService := createTestService(t, nil)
rt, err := babeService.blockState.GetRuntime(nil)
Expand Down