From 7ebf37aeb21adb69b3e3b8ffdda33e4b22554ca2 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 5 Dec 2023 19:58:57 +0000 Subject: [PATCH 1/2] chore(deps): bump github.com/klauspost/compress from 1.17.3 to 1.17.4 (#3620) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 6e53725b84..da6fafebd6 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/gtank/merlin v0.1.1 github.com/ipfs/go-ds-badger2 v0.1.3 github.com/jpillora/ipfilter v1.2.9 - github.com/klauspost/compress v1.17.3 + github.com/klauspost/compress v1.17.4 github.com/libp2p/go-libp2p v0.31.0 github.com/libp2p/go-libp2p-kad-dht v0.25.2 github.com/minio/sha256-simd v1.0.1 diff --git a/go.sum b/go.sum index ef0703914d..e7401fcb5b 100644 --- a/go.sum +++ b/go.sum @@ -467,8 +467,8 @@ github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6 github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/klauspost/compress v1.9.7/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= -github.com/klauspost/compress v1.17.3 h1:qkRjuerhUU1EmXLYGkSH6EZL+vPSxIrYjLNAK4slzwA= -github.com/klauspost/compress v1.17.3/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= +github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= +github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= From b951cf34980e457a216796ace01206232ed73384 Mon Sep 17 00:00:00 2001 From: EclesioMeloJunior Date: Tue, 5 Dec 2023 16:28:02 -0400 Subject: [PATCH 2/2] fix: store raw authority and parse to authority only to verify signature --- dot/core/service.go | 19 +++++++++++ dot/state/epoch.go | 61 +++++++++++++---------------------- dot/types/consensus_digest.go | 13 +++----- lib/babe/babe.go | 12 +++---- lib/babe/crypto.go | 4 +-- lib/babe/epoch.go | 8 ++--- lib/babe/state.go | 5 ++- lib/babe/types.go | 2 +- lib/babe/verify.go | 30 +++++++++-------- 9 files changed, 75 insertions(+), 79 deletions(-) diff --git a/dot/core/service.go b/dot/core/service.go index a7dc445a3e..a9011730d4 100644 --- a/dot/core/service.go +++ b/dot/core/service.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "sync" + "time" "github.com/ChainSafe/gossamer/dot/network" "github.com/ChainSafe/gossamer/dot/types" @@ -104,6 +105,24 @@ func NewService(cfg *Config) (*Service, error) { // Start starts the core service func (s *Service) Start() error { go s.handleBlocksAsync() + + go func() { + time.Sleep(30 * time.Second) + + bb, err := s.blockState.BestBlockHeader() + if err != nil { + panic(err) + } + + msg, err := createBlockAnnounce(&types.Block{ + Header: *bb, + }, true) + if err != nil { + panic(err) + } + + s.net.GossipMessage(msg) + }() return nil } diff --git a/dot/state/epoch.go b/dot/state/epoch.go index c318c3874e..ad719d1d2f 100644 --- a/dot/state/epoch.go +++ b/dot/state/epoch.go @@ -95,15 +95,12 @@ func NewEpochStateFromGenesis(db database.Database, blockState *BlockState, nextConfigData: make(nextEpochMap[types.NextConfigDataV1]), } - auths, err := types.BABEAuthorityRawToAuthority(genesisConfig.GenesisAuthorities) - if err != nil { - return nil, err + epochDataRaw := &types.EpochDataRaw{ + Authorities: genesisConfig.GenesisAuthorities, + Randomness: genesisConfig.Randomness, } - err = s.SetEpochData(0, &types.EpochData{ - Authorities: auths, - Randomness: genesisConfig.Randomness, - }) + err = s.setEpochDataRaw(0, epochDataRaw) if err != nil { return nil, err } @@ -235,10 +232,8 @@ func (s *EpochState) GetEpochForBlock(header *types.Header) (uint64, error) { return 0, errNoPreRuntimeDigest } -// SetEpochData sets the epoch data for a given epoch -func (s *EpochState) SetEpochData(epoch uint64, info *types.EpochData) error { - raw := info.ToEpochDataRaw() - +// setEpochDataRaw sets the epoch data raw for a given epoch +func (s *EpochState) setEpochDataRaw(epoch uint64, raw *types.EpochDataRaw) error { enc, err := scale.Marshal(*raw) if err != nil { return err @@ -247,17 +242,17 @@ func (s *EpochState) SetEpochData(epoch uint64, info *types.EpochData) error { return s.db.Put(epochDataKey(epoch), enc) } -// GetEpochData returns the epoch data for a given epoch persisted in database +// GetEpochDataRaw returns the epoch data raw for a given epoch persisted in database // otherwise will try to get the data from the in-memory map using the header // if the header params is nil then it will search only in database -func (s *EpochState) GetEpochData(epoch uint64, header *types.Header) (*types.EpochData, error) { - epochData, err := s.getEpochDataFromDatabase(epoch) +func (s *EpochState) GetEpochDataRaw(epoch uint64, header *types.Header) (*types.EpochDataRaw, error) { + epochDataRaw, err := s.getEpochDataRawFromDatabase(epoch) if err != nil && !errors.Is(err, database.ErrNotFound) { return nil, fmt.Errorf("failed to retrieve epoch data from database: %w", err) } - if epochData != nil { - return epochData, nil + if epochDataRaw != nil { + return epochDataRaw, nil } if header == nil { @@ -272,38 +267,33 @@ func (s *EpochState) GetEpochData(epoch uint64, header *types.Header) (*types.Ep return nil, fmt.Errorf("failed to get epoch data from memory: %w", err) } - epochData, err = inMemoryEpochData.ToEpochData() - if err != nil { - return nil, fmt.Errorf("cannot transform into epoch data: %w", err) - } - - return epochData, nil + return inMemoryEpochData.ToEpochDataRaw(), nil } -// getEpochDataFromDatabase returns the epoch data for a given epoch persisted in database -func (s *EpochState) getEpochDataFromDatabase(epoch uint64) (*types.EpochData, error) { +// getEpochDataRawFromDatabase returns the epoch data for a given epoch persisted in database +func (s *EpochState) getEpochDataRawFromDatabase(epoch uint64) (*types.EpochDataRaw, error) { enc, err := s.db.Get(epochDataKey(epoch)) if err != nil { return nil, err } - raw := &types.EpochDataRaw{} + raw := new(types.EpochDataRaw) err = scale.Unmarshal(enc, raw) if err != nil { - return nil, err + return nil, fmt.Errorf("unmarshaling into epoch data raw: %w", err) } - return raw.ToEpochData() + return raw, nil } -// GetLatestEpochData returns the EpochData for the current epoch -func (s *EpochState) GetLatestEpochData() (*types.EpochData, error) { +// GetLatestEpochDataRaw returns the EpochData for the current epoch +func (s *EpochState) GetLatestEpochDataRaw() (*types.EpochDataRaw, error) { curr, err := s.GetCurrentEpoch() if err != nil { return nil, err } - return s.GetEpochData(curr, nil) + return s.GetEpochDataRaw(curr, nil) } // SetConfigData sets the BABE config data for a given epoch @@ -586,7 +576,7 @@ func (s *EpochState) FinalizeBABENextEpochData(finalizedHeader *types.Header) er nextEpoch = finalizedBlockEpoch + 1 } - epochInDatabase, err := s.getEpochDataFromDatabase(nextEpoch) + epochRawInDatabase, err := s.getEpochDataRawFromDatabase(nextEpoch) // if an error occurs and the error is database.ErrNotFound we ignore // since this error is what we will handle in the next lines @@ -595,7 +585,7 @@ func (s *EpochState) FinalizeBABENextEpochData(finalizedHeader *types.Header) er } // epoch data already defined we don't need to lookup in the map - if epochInDatabase != nil { + if epochRawInDatabase != nil { return nil } @@ -604,12 +594,7 @@ func (s *EpochState) FinalizeBABENextEpochData(finalizedHeader *types.Header) er return fmt.Errorf("cannot find next epoch data: %w", err) } - ed, err := finalizedNextEpochData.ToEpochData() - if err != nil { - return fmt.Errorf("cannot transform epoch data: %w", err) - } - - err = s.SetEpochData(nextEpoch, ed) + err = s.setEpochDataRaw(nextEpoch, finalizedNextEpochData.ToEpochDataRaw()) if err != nil { return fmt.Errorf("cannot set epoch data: %w", err) } diff --git a/dot/types/consensus_digest.go b/dot/types/consensus_digest.go index 9cb061e325..d6719c3b91 100644 --- a/dot/types/consensus_digest.go +++ b/dot/types/consensus_digest.go @@ -102,16 +102,11 @@ func (d NextEpochData) String() string { //skipcq: GO-W1029 } // ToEpochData returns the NextEpochData as EpochData -func (d *NextEpochData) ToEpochData() (*EpochData, error) { //skipcq: GO-W1029 - auths, err := BABEAuthorityRawToAuthority(d.Authorities) - if err != nil { - return nil, err - } - - return &EpochData{ - Authorities: auths, +func (d *NextEpochData) ToEpochDataRaw() *EpochDataRaw { + return &EpochDataRaw{ + Authorities: d.Authorities, Randomness: d.Randomness, - }, nil + } } // BABEOnDisabled represents a GRANDPA authority being disabled diff --git a/lib/babe/babe.go b/lib/babe/babe.go index 0ef35b8244..a7aa8d3c19 100644 --- a/lib/babe/babe.go +++ b/lib/babe/babe.go @@ -258,12 +258,8 @@ func (b *Service) Stop() error { } // Authorities returns the current BABE authorities -func (b *Service) Authorities() []types.Authority { - auths := make([]types.Authority, len(b.epochHandler.epochData.authorities)) - for i, auth := range b.epochHandler.epochData.authorities { - auths[i] = *auth.DeepCopy() - } - return auths +func (b *Service) AuthoritiesRaw() []types.AuthorityRaw { + return b.epochHandler.epochData.authorities } // IsStopped returns true if the service is stopped (ie not producing blocks) @@ -271,7 +267,7 @@ func (b *Service) IsStopped() bool { return b.ctx.Err() != nil } -func (b *Service) getAuthorityIndex(Authorities []types.Authority) (uint32, error) { +func (b *Service) getAuthorityIndex(Authorities []types.AuthorityRaw) (uint32, error) { if !b.authority { return 0, ErrNotAuthority } @@ -279,7 +275,7 @@ func (b *Service) getAuthorityIndex(Authorities []types.Authority) (uint32, erro pub := b.keypair.Public() for i, auth := range Authorities { - if bytes.Equal(pub.Encode(), auth.Key.Encode()) { + if bytes.Equal(pub.Encode(), auth.Key[:]) { return uint32(i), nil } } diff --git a/lib/babe/crypto.go b/lib/babe/crypto.go index 63830f149c..5c2829e12e 100644 --- a/lib/babe/crypto.go +++ b/lib/babe/crypto.go @@ -94,7 +94,7 @@ func checkPrimaryThreshold(randomness Randomness, func claimSecondarySlotVRF(randomness Randomness, slot, epoch uint64, - authorities []types.Authority, + authorities []types.AuthorityRaw, keypair *sr25519.Keypair, authorityIndex uint32, ) (*VrfOutputAndProof, error) { @@ -123,7 +123,7 @@ func claimSecondarySlotVRF(randomness Randomness, }, nil } -func claimSecondarySlotPlain(randomness Randomness, slot uint64, authorities []types.Authority, authorityIndex uint32, +func claimSecondarySlotPlain(randomness Randomness, slot uint64, authorities []types.AuthorityRaw, authorityIndex uint32, ) error { secondarySlotAuthor, err := getSecondarySlotAuthor(slot, len(authorities), randomness) if err != nil { diff --git a/lib/babe/epoch.go b/lib/babe/epoch.go index 7cf51873be..69b8493b2b 100644 --- a/lib/babe/epoch.go +++ b/lib/babe/epoch.go @@ -95,7 +95,7 @@ func (b *Service) getEpochData(epoch uint64, bestBlock *types.Header) (*epochDat return epochData, nil } - currEpochData, err := b.epochState.GetEpochData(epoch, bestBlock) + currEpochData, err := b.epochState.GetEpochDataRaw(epoch, bestBlock) if err != nil { return nil, fmt.Errorf("cannot get epoch data for epoch %d: %w", epoch, err) } @@ -127,13 +127,13 @@ func (b *Service) getEpochData(epoch uint64, bestBlock *types.Header) (*epochDat func (b *Service) getLatestEpochData() (resEpochData *epochData, error error) { resEpochData = &epochData{} - epochData, err := b.epochState.GetLatestEpochData() + epochDataRaw, err := b.epochState.GetLatestEpochDataRaw() if err != nil { return nil, fmt.Errorf("cannot get latest epoch data: %w", err) } - resEpochData.randomness = epochData.Randomness - resEpochData.authorities = epochData.Authorities + resEpochData.randomness = epochDataRaw.Randomness + resEpochData.authorities = epochDataRaw.Authorities configData, err := b.epochState.GetLatestConfigData() if err != nil { diff --git a/lib/babe/state.go b/lib/babe/state.go index 7652d0f2bc..47de36a930 100644 --- a/lib/babe/state.go +++ b/lib/babe/state.go @@ -61,16 +61,15 @@ type EpochState interface { GetSlotDuration() (time.Duration, error) SetCurrentEpoch(epoch uint64) error GetCurrentEpoch() (uint64, error) - SetEpochData(epoch uint64, info *types.EpochData) error - GetEpochData(epoch uint64, header *types.Header) (*types.EpochData, error) + GetEpochDataRaw(epoch uint64, header *types.Header) (*types.EpochDataRaw, error) GetConfigData(epoch uint64, header *types.Header) (*types.ConfigData, error) GetLatestConfigData() (*types.ConfigData, error) GetStartSlotForEpoch(epoch uint64) (uint64, error) GetEpochForBlock(header *types.Header) (uint64, error) SetFirstSlot(slot uint64) error - GetLatestEpochData() (*types.EpochData, error) + GetLatestEpochDataRaw() (*types.EpochDataRaw, error) SkipVerify(*types.Header) (bool, error) } diff --git a/lib/babe/types.go b/lib/babe/types.go index accea21c49..02457ea083 100644 --- a/lib/babe/types.go +++ b/lib/babe/types.go @@ -58,7 +58,7 @@ func (d Authorities) String() string { type epochData struct { randomness Randomness authorityIndex uint32 - authorities []types.Authority + authorities []types.AuthorityRaw threshold *scale.Uint128 allowedSlots types.AllowedSlots } diff --git a/lib/babe/verify.go b/lib/babe/verify.go index c66022e4ca..8f4841eb04 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -20,7 +20,7 @@ var errEmptyKeyOwnershipProof = errors.New("key ownership proof is nil") // verifierInfo contains the information needed to verify blocks // it remains the same for an epoch type verifierInfo struct { - authorities []types.Authority + authorities []types.AuthorityRaw randomness Randomness threshold *scale.Uint128 secondarySlots bool @@ -195,7 +195,7 @@ func (v *VerificationManager) VerifyBlock(header *types.Header) error { } func (v *VerificationManager) getVerifierInfo(epoch uint64, header *types.Header) (*verifierInfo, error) { - epochData, err := v.epochState.GetEpochData(epoch, header) + epochData, err := v.epochState.GetEpochDataRaw(epoch, header) if err != nil { return nil, fmt.Errorf("failed to get epoch data for epoch %d: %w", epoch, err) } @@ -223,7 +223,7 @@ type verifier struct { blockState BlockState slotState SlotState epoch uint64 - authorities []types.Authority + authorities []types.AuthorityRaw randomness Randomness threshold *scale.Uint128 secondarySlots bool @@ -294,7 +294,11 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { authIdx = d.AuthorityIndex } - authorPub := b.authorities[authIdx].Key + authority := new(types.Authority) + err = authority.FromRawSr25519(&b.authorities[authIdx]) + if err != nil { + return fmt.Errorf("from raw sr25519: %w", err) + } // remove seal before verifying signature h := types.NewDigest() @@ -331,7 +335,7 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error { return err } - ok, err = authorPub.Verify(hash[:], seal.Data) + ok, err = authority.Key.Verify(hash[:], seal.Data) if err != nil { return err } @@ -396,7 +400,7 @@ func (b *verifier) verifyBlockEquivocation(header *types.Header) (bool, error) { } slotNow := getCurrentSlot(b.slotDuration) - signer := types.AuthorityID(b.authorities[authorityIndex].ToRaw().Key) + signer := b.authorities[authorityIndex].Key equivocationProof, err := b.slotState.CheckEquivocation(slotNow, slotNumber, header, signer) if err != nil { @@ -449,9 +453,8 @@ func (b *verifier) verifyPreRuntimeDigest(digest *types.PreRuntimeDigest) (scale ok = false break } - pub := b.authorities[d.AuthorityIndex].Key - - pk, err := sr25519.NewPublicKey(pub.Encode()) + authority := b.authorities[d.AuthorityIndex] + pk, err := sr25519.NewPublicKey(authority.Key[:]) if err != nil { return nil, err } @@ -487,9 +490,8 @@ func (b *verifier) verifyPreRuntimeDigest(digest *types.PreRuntimeDigest) (scale func (b *verifier) verifyPrimarySlotWinner(authorityIndex uint32, slot uint64, vrfOutput [sr25519.VRFOutputLength]byte, vrfProof [sr25519.VRFProofLength]byte) (bool, error) { - pub := b.authorities[authorityIndex].Key - - pk, err := sr25519.NewPublicKey(pub.Encode()) + authority := b.authorities[authorityIndex] + pk, err := sr25519.NewPublicKey(authority.Key[:]) if err != nil { return false, err } @@ -511,10 +513,10 @@ func (b *verifier) verifyPrimarySlotWinner(authorityIndex uint32, // validate VRF proof logger.Tracef("verifyPrimarySlotWinner authority index %d, "+ - "public key %s, randomness 0x%x, slot %d, epoch %d, "+ + "public key 0x%x, randomness 0x%x, slot %d, epoch %d, "+ "output 0x%x and proof 0x%x", authorityIndex, - pub.Hex(), b.randomness, slot, b.epoch, + authority.Key[:], b.randomness, slot, b.epoch, vrfOutput, vrfProof[:]) t := makeTranscript(b.randomness, slot, b.epoch)