Skip to content

Commit

Permalink
chore(epoch): Changing to more generic functions and approaches
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiroJCB committed Aug 20, 2024
1 parent 2cf996f commit 82730de
Showing 1 changed file with 37 additions and 59 deletions.
96 changes: 37 additions & 59 deletions dot/state/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ func configDataKey(epoch uint64) []byte {
}

func nextEpochDataKey(epoch uint64, hash common.Hash) []byte {
// we add a "-" to the key to avoid conflicts with composite keys
key := fmt.Sprintf("-%d:%v", epoch, hash)
return append(nextEpochDataPrefix, []byte(key)...)
partialKey := fmt.Sprintf("-%d:%v", epoch, hash)
return append(nextEpochDataPrefix, []byte(partialKey)...)
}

func nextConfigDataKey(epoch uint64, hash common.Hash) []byte {
// we add a "-" to the key to avoid conflicts with composite keys
key := fmt.Sprintf("-%d:%v", epoch, hash)
return append(nextConfigDataPrefix, []byte(key)...)
partialKey := fmt.Sprintf("-%d:%v", epoch, hash)
return append(nextConfigDataPrefix, []byte(partialKey)...)
}

// GenesisEpochDescriptor is the informations provided by calling
Expand Down Expand Up @@ -144,7 +142,12 @@ func NewEpochState(db database.Database, blockState *BlockState,
return nil, err
}

nextEpochData, nextConfigData, err := getNextEpochAndConfigDataFromDisk(db)
nextEpochData, err := restoreMapFromDisk[types.NextEpochData](db, nextEpochDataPrefix)
if err != nil {
return nil, err
}

nextConfigData, err := restoreMapFromDisk[types.NextConfigDataV1](db, nextConfigDataPrefix)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -172,68 +175,45 @@ func NewEpochState(db database.Database, blockState *BlockState,
}, nil
}

// getNextEpochAndConfigDataFromDisk retrieves the next epoch and config data maps from the database
func getNextEpochAndConfigDataFromDisk(db database.Database) (
nextEpochMap[types.NextEpochData], nextEpochMap[types.NextConfigDataV1], error) {

nextConfigData := make(nextEpochMap[types.NextConfigDataV1])
nextEpochData := make(nextEpochMap[types.NextEpochData])
// restoreMapFromDisk retrieves the next epoch and config data maps from the database
func restoreMapFromDisk[T types.NextConfigDataV1 | types.NextEpochData](db database.Database, prefix []byte) (
nextEpochMap[T], error) {

configIter, err := db.NewPrefixIterator(nextConfigDataPrefix)
resMap := make(nextEpochMap[T])
iter, err := db.NewPrefixIterator(prefix)
if err != nil {
return nextEpochMap[types.NextEpochData]{}, nextEpochMap[types.NextConfigDataV1]{}, err
return resMap, err
}

defer configIter.Release()
defer iter.Release()

for configIter.First(); configIter.Valid(); configIter.Next() {
nexEpochvalue := new(types.NextConfigDataV1)
for iter.First(); iter.Valid(); iter.Next() {
mapValue, epoch, fork, err := getNextEpochOrConfigData[T](iter)

nexEpochvalue, epoch, fork, err := getNextEpochOrConfigData(nexEpochvalue, nextConfigDataPrefix, configIter)
if err != nil {
return nextEpochMap[types.NextEpochData]{}, nextEpochMap[types.NextConfigDataV1]{}, err
return resMap, err
}

if _, ok := nextConfigData[epoch]; !ok {
nextConfigData[epoch] = make(map[common.Hash]types.NextConfigDataV1)
if _, ok := resMap[epoch]; !ok {
resMap[epoch] = make(map[common.Hash]T)
}
// Add data to the map
nextConfigData[epoch][fork] = *nexEpochvalue
}

epochIter, err := db.NewPrefixIterator(nextEpochDataPrefix)
if err != nil {
return nextEpochMap[types.NextEpochData]{}, nextEpochMap[types.NextConfigDataV1]{}, err
resMap[epoch][fork] = *mapValue
}

defer epochIter.Release()

for epochIter.First(); epochIter.Valid(); epochIter.Next() {

nexEpochvalue := new(types.NextEpochData)
nexEpochvalue, epoch, fork, err := getNextEpochOrConfigData(nexEpochvalue, nextEpochDataPrefix, epochIter)
if err != nil {
return nextEpochMap[types.NextEpochData]{}, nextEpochMap[types.NextConfigDataV1]{}, err
}
// Add data to the map
if _, ok := nextEpochData[epoch]; !ok {
nextEpochData[epoch] = make(map[common.Hash]types.NextEpochData)
}

nextEpochData[epoch][fork] = *nexEpochvalue

if err = iter.Close(); err != nil {
return resMap, err
}

return nextEpochData, nextConfigData, nil
return resMap, nil
}

// getNextEpochOrConfigData retrieves the next epoch or config data from the iterator
func getNextEpochOrConfigData[T *types.NextConfigDataV1 | *types.NextEpochData](
NextData T, nextDataPrefix []byte, iter database.Iterator) (T, uint64, common.Hash, error) {
func getNextEpochOrConfigData[T types.NextConfigDataV1 | types.NextEpochData](iter database.Iterator) (*T, uint64, common.Hash, error) {
nextData := new(T)
key := string(iter.Key())
value := iter.Value()

keyWithoutPrefix := strings.TrimPrefix(key, string(nextDataPrefix)+"-")
keyWithoutPrefix := strings.Split(key, "-")[1]

// Split the key into epoch and fork
parts := strings.Split(keyWithoutPrefix, ":")
Expand All @@ -250,11 +230,11 @@ func getNextEpochOrConfigData[T *types.NextConfigDataV1 | *types.NextEpochData](

copy(fork[:], part1)

if err = scale.Unmarshal(value, NextData); err != nil {
if err = scale.Unmarshal(value, nextData); err != nil {
return nil, 0, common.Hash{}, err
}

return NextData, epoch, fork, nil
return nextData, epoch, fork, nil
}

// GetEpochLength returns the length of an epoch in slots
Expand Down Expand Up @@ -1036,8 +1016,7 @@ func (s *EpochState) FinalizeBABENextEpochData(finalizedHeader *types.Header) er
}

func (s *EpochState) deleteEpochDataFromDisk(epoch uint64) error {
nexEpochvalue := new(types.NextEpochData)
configKeysToDelete, err := getNextEpochOrConfigDataKeysFromDisk(s.db, nexEpochvalue, nextEpochDataPrefix, epoch)
configKeysToDelete, err := getDataKeysFromDisk[types.NextEpochData](s.db, nextEpochDataPrefix, epoch)
if err != nil {
return fmt.Errorf("cannot get next config data keys from disk: %w", err)
}
Expand All @@ -1051,10 +1030,10 @@ func (s *EpochState) deleteEpochDataFromDisk(epoch uint64) error {
return nil
}

// getNextEpochOrConfigDataKeysFromDisk is a generic function that returns all the nextEpochData or nextConfigData keys
// getDataKeysFromDisk is a generic function that returns all the nextEpochData or nextConfigData keys
// for a given epoch from the database
func getNextEpochOrConfigDataKeysFromDisk[T *types.NextEpochData | *types.NextConfigDataV1](
db GetterPutterNewBatcherPrefixIter, value T, prefix []byte, currentEpoch uint64) (
func getDataKeysFromDisk[T types.NextEpochData | types.NextConfigDataV1](
db GetterPutterNewBatcherPrefixIter, prefix []byte, currentEpoch uint64) (
[]string, error) {

dataKeys := []string{}
Expand All @@ -1067,7 +1046,7 @@ func getNextEpochOrConfigDataKeysFromDisk[T *types.NextEpochData | *types.NextCo
defer iter.Release()

for iter.First(); iter.Valid(); iter.Next() {
_, epoch, fork, err := getNextEpochOrConfigData(value, prefix, iter)
_, epoch, fork, err := getNextEpochOrConfigData[T](iter)
if err != nil {
return dataKeys, err
}
Expand Down Expand Up @@ -1149,8 +1128,7 @@ func (s *EpochState) FinalizeBABENextConfigData(finalizedHeader *types.Header) e
}

func (s *EpochState) deleteNextConfigDataFromDisk(epoch uint64) error {
nextConfigValue := new(types.NextConfigDataV1)
configKeysToDelete, err := getNextEpochOrConfigDataKeysFromDisk(s.db, nextConfigValue, nextConfigDataPrefix, epoch)
configKeysToDelete, err := getDataKeysFromDisk[types.NextConfigDataV1](s.db, nextConfigDataPrefix, epoch)
if err != nil {
return fmt.Errorf("cannot get next config data keys from disk: %w", err)
}
Expand Down

0 comments on commit 82730de

Please sign in to comment.