Skip to content

Commit

Permalink
Implement GetAuthoritesChangesFromBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro committed Sep 23, 2024
1 parent 17ee317 commit 6fad840
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
21 changes: 18 additions & 3 deletions dot/state/block_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package state

import (
"github.com/ChainSafe/gossamer/lib/common"
"golang.org/x/exp/slices"
)

// prefixKey = prefix + hash
Expand Down Expand Up @@ -88,7 +89,21 @@ func (bs *BlockState) GetJustification(hash common.Hash) ([]byte, error) {
}

// GetAuthoritesChangesFromBlock retrieves blocks numbers where authority set changes happened
func (bs *BlockState) GetAuthoritesChangesFromBlock(blockNumber uint) ([]uint, error) {
// TODO: complete me
panic("not implemented")
func (bs *BlockState) GetAuthoritesChangesFromBlock(initialBlockNumber uint) ([]uint, error) {
blockNumbers := make([]uint, 0)
iter, err := bs.db.NewPrefixIterator(setIDChangePrefix)
if err != nil {
return nil, err
}

for iter.Next() {
blockNumber := common.BytesToUint(iter.Value())
if blockNumber >= initialBlockNumber {
blockNumbers = append(blockNumbers, blockNumber)
}
}

// To ensure the order of the blocks
slices.Sort(blockNumbers)
return blockNumbers, nil
}
1 change: 1 addition & 0 deletions dot/state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type BlockStateDatabase interface {
GetPutDeleter
Haser
NewBatcher
NewPrefixIterator(prefix []byte) (database.Iterator, error)
}

// GetPutter has methods to get and put key values.
Expand Down
15 changes: 15 additions & 0 deletions dot/state/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Table interface {
Path() string
NewBatch() Batch
NewIterator() (Iterator, error)
NewPrefixIterator(prefix []byte) (Iterator, error)
}

const DefaultDatabaseDir = "db"
Expand Down
4 changes: 4 additions & 0 deletions internal/database/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ func (t *table) NewBatch() Batch {
func (t *table) NewIterator() (Iterator, error) {
return t.db.NewPrefixIterator(t.prefix)
}

func (t *table) NewPrefixIterator(prefix []byte) (Iterator, error) {
return t.db.NewPrefixIterator(append(t.prefix, prefix...))
}
1 change: 0 additions & 1 deletion lib/grandpa/warp_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (np *NetworkProvider) Generate(start common.Hash) ([]byte, error) {
// the header should contains a standard scheduled change
// otherwise the set must have changed through a forced changed,
// in which case we stop collecting proofs as the chain of trust in authority handoffs was broken.

header, err := np.backend.GetHeaderByNumber(blockNumber)
if err != nil {
return nil, err
Expand Down

0 comments on commit 6fad840

Please sign in to comment.