Skip to content

Commit

Permalink
chore(epoch): Solving deepsource issues and removing hyphen
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiroJCB committed Oct 1, 2024
1 parent 6588c9d commit 4e63588
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
14 changes: 7 additions & 7 deletions dot/state/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func configDataKey(epoch uint64) []byte {
}

func nextEpochDataKey(epoch uint64, hash common.Hash) []byte {
partialKey := fmt.Sprintf("-%d:%s", epoch, hash.String())
partialKey := fmt.Sprintf("%d:%s", epoch, hash.String())
return append(nextEpochDataPrefix, []byte(partialKey)...)
}

func nextConfigDataKey(epoch uint64, hash common.Hash) []byte {
partialKey := fmt.Sprintf("-%d:%s", epoch, hash.String())
partialKey := fmt.Sprintf("%d:%s", epoch, hash.String())
return append(nextConfigDataPrefix, []byte(partialKey)...)
}

Expand Down Expand Up @@ -188,7 +188,7 @@ func restoreMapFromDisk[T types.NextConfigDataV1 | types.NextEpochData](db datab
defer iter.Release()

for iter.First(); iter.Valid(); iter.Next() {
mapValue, epoch, fork, err := getNextEpochOrConfigData[T](iter)
mapValue, epoch, fork, err := getNextEpochOrConfigData[T](iter, prefix)

if err != nil {
return resMap, err
Expand All @@ -208,13 +208,13 @@ func restoreMapFromDisk[T types.NextConfigDataV1 | types.NextEpochData](db datab
}

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

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

// Split the key into epoch and fork
parts := strings.Split(keyWithoutPrefix, ":")
Expand Down Expand Up @@ -1052,8 +1052,8 @@ func getDataKeysFromDisk[T types.NextEpochData | types.NextConfigDataV1](
db database.Table, prefix []byte, currentEpoch uint64) (
[]string, error) {

dataKeys := []string{}
currentEpochPrefix := fmt.Sprintf("%s-%d", prefix, currentEpoch)
var dataKeys []string
currentEpochPrefix := fmt.Sprintf("%s%d", prefix, currentEpoch)

iter, err := db.NewPrefixIterator([]byte(currentEpochPrefix))
if err != nil {
Expand Down
30 changes: 15 additions & 15 deletions dot/state/epoch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/stretchr/testify/require"
)

func newEpochStateFromGenesis(t *testing.T) *EpochState {
func newTestEpochStateFromGenesis(t *testing.T) *EpochState {
db := NewInMemoryDB(t)
blockState := newTestBlockState(t, newTriesEmpty())
s, err := NewEpochStateFromGenesis(db, blockState, config.BABEConfigurationTestDefault)
Expand All @@ -28,11 +28,11 @@ func newEpochStateFromGenesis(t *testing.T) *EpochState {
}

func TestNewEpochStateFromGenesis(t *testing.T) {
_ = newEpochStateFromGenesis(t)
_ = newTestEpochStateFromGenesis(t)
}

func TestEpochState_CurrentEpoch(t *testing.T) {
s := newEpochStateFromGenesis(t)
s := newTestEpochStateFromGenesis(t)
epoch, err := s.GetCurrentEpoch()
require.NoError(t, err)
require.Equal(t, uint64(0), epoch)
Expand All @@ -45,7 +45,7 @@ func TestEpochState_CurrentEpoch(t *testing.T) {
}

func TestEpochState_EpochData(t *testing.T) {
s := newEpochStateFromGenesis(t)
s := newTestEpochStateFromGenesis(t)

keyring, err := keystore.NewSr25519Keyring()
require.NoError(t, err)
Expand All @@ -72,7 +72,7 @@ func TestEpochState_EpochData(t *testing.T) {
}

func TestEpochState_GetStartSlotForEpoch(t *testing.T) {
s := newEpochStateFromGenesis(t)
s := newTestEpochStateFromGenesis(t)

// let's say first slot is 1 second after January 1, 1970 UTC
startAtTime := time.Unix(1, 0)
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestEpochState_GetStartSlotForEpoch(t *testing.T) {
}

func TestEpochState_ConfigData(t *testing.T) {
s := newEpochStateFromGenesis(t)
s := newTestEpochStateFromGenesis(t)

data := &types.ConfigData{
C1: 1,
Expand Down Expand Up @@ -154,7 +154,7 @@ func createAndImportBlockOne(t *testing.T, slotNumber uint64, blockState *BlockS
}

func TestEpochState_GetEpochForBlock(t *testing.T) {
s := newEpochStateFromGenesis(t)
s := newTestEpochStateFromGenesis(t)

firstSlot := uint64(1)
blockOneHeader := createAndImportBlockOne(t, firstSlot, s.blockState)
Expand Down Expand Up @@ -211,7 +211,7 @@ func TestEpochState_GetEpochForBlock(t *testing.T) {
}

func TestEpochState_SetAndGetSlotDuration(t *testing.T) {
s := newEpochStateFromGenesis(t)
s := newTestEpochStateFromGenesis(t)
expected := time.Millisecond * time.Duration(config.BABEConfigurationTestDefault.SlotDuration)

ret, err := s.GetSlotDuration()
Expand Down Expand Up @@ -378,7 +378,7 @@ func TestStoreAndFinalizeBabeNextEpochData(t *testing.T) {

for testName, tt := range tests {
t.Run(testName, func(t *testing.T) {
epochState := newEpochStateFromGenesis(t)
epochState := newTestEpochStateFromGenesis(t)

for _, e := range tt.inMemoryEpoch {
for i, hash := range e.hashes {
Expand Down Expand Up @@ -561,7 +561,7 @@ func TestStoreAndFinalizeBabeNextConfigData(t *testing.T) {

for testName, tt := range tests {
t.Run(testName, func(t *testing.T) {
epochState := newEpochStateFromGenesis(t)
epochState := newTestEpochStateFromGenesis(t)

for _, finalized := range finalizedHeaders {
// mapping number #1 to the block hash
Expand Down Expand Up @@ -642,7 +642,7 @@ func TestRetrieveChainFirstSlot(t *testing.T) {
// epoch calculation, same for blocks on X
// when finalisation happens Gossamer should retrieve the chain first
// slot for the finalized chain, given that the other chain will be pruned
singleEpochState := newEpochStateFromGenesis(t)
singleEpochState := newTestEpochStateFromGenesis(t)

// calling without any block it must return error
_, err := singleEpochState.retrieveFirstNonOriginBlockSlot(common.Hash{})
Expand Down Expand Up @@ -734,7 +734,7 @@ func TestRetrieveChainFirstSlot(t *testing.T) {
}

func TestRetrieveAndUpdate(t *testing.T) {
epochState := newEpochStateFromGenesis(t)
epochState := newTestEpochStateFromGenesis(t)
blockState := epochState.blockState

nem := nextEpochMap[types.NextEpochData]{}
Expand Down Expand Up @@ -824,7 +824,7 @@ func TestRetrieveAndUpdate(t *testing.T) {

func TestFirstSlotNumberFromDb(t *testing.T) {
// test case to check whether we have the correct first slot number in the database
epochState := newEpochStateFromGenesis(t)
epochState := newTestEpochStateFromGenesis(t)
slotDuration, err := epochState.GetSlotDuration()
require.NoError(t, err)

Expand Down Expand Up @@ -858,7 +858,7 @@ func TestFirstSlotNumberFromDb(t *testing.T) {
}

func TestNextEpochDataAndConfigInDisk(t *testing.T) {
epochState := newEpochStateFromGenesis(t)
epochState := newTestEpochStateFromGenesis(t)
db := NewInMemoryDB(t)
dbTable := database.NewTable(db, epochPrefix)
epochState.db = dbTable
Expand Down Expand Up @@ -1008,7 +1008,7 @@ func createBABEConsensusDigest(t *testing.T, digestData any) types.ConsensusDige
}

func TestDeleteNextEpochDataAndConfig(t *testing.T) {
epochState := newEpochStateFromGenesis(t)
epochState := newTestEpochStateFromGenesis(t)
db := NewInMemoryDB(t)
// defining the db in the right context
dbTable := database.NewTable(db, epochPrefix)
Expand Down

0 comments on commit 4e63588

Please sign in to comment.