Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
avalkov committed Oct 23, 2024
1 parent 01e40f2 commit 5f6f0e3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 125 deletions.
40 changes: 0 additions & 40 deletions bor/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,43 +349,3 @@ func (k *Keeper) IterateSpansAndApplyFn(ctx sdk.Context, f func(span hmTypes.Spa
}
}
}

// IterateSpansAndCollect iterates over spans, collects up to 'max' entries,
// and returns a slice containing the collected spans.
// It continues from the last key processed in the previous batch.
func (k *Keeper) IterateSpansAndCollect(ctx sdk.Context, nextKey []byte, max int) ([]*hmTypes.Span, []byte, error) {
store := ctx.KVStore(k.storeKey)

var startKey []byte
if nextKey != nil {
startKey = nextKey
} else {
startKey = SpanPrefixKey
}

endKey := sdk.PrefixEndBytes(SpanPrefixKey)

iterator := store.Iterator(startKey, endKey)
defer iterator.Close()

collectedSpans := make([]*hmTypes.Span, 0, max)
entriesCollected := 0

for ; iterator.Valid() && entriesCollected < max; iterator.Next() {
var span hmTypes.Span
if err := k.cdc.UnmarshalBinaryBare(iterator.Value(), &span); err != nil {
k.Logger(ctx).Error("IterateSpansAndCollect | UnmarshalBinaryBare", "error", err)
return nil, nil, err
}

collectedSpans = append(collectedSpans, &span)
entriesCollected++
}

// We want to return the key after last processed key because the iterator is inclusive for the start key
if iterator.Valid() {
return collectedSpans, iterator.Key(), nil
}

return collectedSpans, nil, nil
}
72 changes: 0 additions & 72 deletions cmd/heimdallcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ func fetchModuleStreamedData(sdkCtx sdk.Context, module hmModule.StreamedGenesis
var lastKey []byte
allData := []json.RawMessage{}
allDataLength := 0
// var currAppendingPath string

for {
data, err := module.NextGenesisData(sdkCtx, lastKey, maxNextGenesisItems)
Expand All @@ -398,22 +397,6 @@ func fetchModuleStreamedData(sdkCtx sdk.Context, module hmModule.StreamedGenesis
return data.Path, combinedData, nil
}

// if currAppendingPath != "" && currAppendingPath != data.Path {
// combinedData, err := combineJSONArrays(allData, allDataLength)
// if err != nil {
// return err
// }

// if err := AddProperty(appData, currAppendingPath, combinedData); err != nil {
// return err
// }

// allData = []json.RawMessage{}
// allDataLength = 0
// }

// currAppendingPath = data.Path

allData = append(allData, data.Data)
allDataLength += len(data.Data)
}
Expand Down Expand Up @@ -454,44 +437,6 @@ func combineJSONArrays(arrays []json.RawMessage, allArraysLength int) (json.RawM
return json.RawMessage(combinedJSON), nil
}

// AddProperty adds a property to the data map.
func AddProperty(data map[string]interface{}, path string, value json.RawMessage) error {
if path == "" {
return fmt.Errorf("path cannot be empty")
}

keys := strings.Split(path, ".")
lastKey := keys[len(keys)-1]
parentPath := strings.Join(keys[:len(keys)-1], ".")

current, err := traversePath(data, parentPath)
if err != nil {
return err
}
current[lastKey] = value
return nil
}

// traversePath traverses the path in the data map.
func traversePath(data map[string]interface{}, path string) (map[string]interface{}, error) {
if path == "." {
return data, nil
}

keys := strings.Split(path, ".")
current := data

for _, key := range keys {
if next, ok := current[key].(map[string]interface{}); ok {
current = next
continue
}
return nil, fmt.Errorf("invalid path: %s", path)
}

return current, nil
}

// generateKeystore generate keystore file from private key
func generateKeystore(_ *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -577,23 +522,6 @@ func generateValidatorKey(cdc *codec.Codec) *cobra.Command {
return client.GetCommands(cmd)[0]
}

//
// Internal functions
//

func writeGenesisFile(genesisFile, chainID string, appState json.RawMessage) error {
genDoc := tmTypes.GenesisDoc{
ChainID: chainID,
AppState: appState,
}

if err := genDoc.ValidateAndComplete(); err != nil {
return err
}

return genDoc.SaveAs(genesisFile)
}

// keyFileName implements the naming convention for keyfiles:
// UTC--<created_at UTC ISO8601>-<address hex>
func keyFileName(keyAddr ethCommon.Address) string {
Expand Down
35 changes: 22 additions & 13 deletions cmd/heimdallcli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"testing"

heimdallApp "github.com/maticnetwork/heimdall/app"
Expand Down Expand Up @@ -33,34 +35,23 @@ func TestModulesStreamedGenesisExport(t *testing.T) {
ctx := happ.NewContext(true, abci.Header{Height: 1})
happ.GetModuleManager().InitGenesis(ctx, genesisState)

// Create a buffer to capture the output
var buf bytes.Buffer

// Call the refactored generateMarshalledAppState with the buffer
err = generateMarshalledAppState(happ, "test-chain", 2, &buf)
require.NoError(t, err)

// Write buf to file
err = os.WriteFile("./testdata/dump-genesis-streamed.json", buf.Bytes(), 0644)
require.NoError(t, err)

// Get the bytes from the buffer
marshaledAppState := buf.Bytes()

// Unmarshal the output JSON into a map
var unmarshaledAppState map[string]interface{}
err = json.Unmarshal(marshaledAppState, &unmarshaledAppState)
require.NoError(t, err)

// Access the "app_state" field
appState, ok := unmarshaledAppState["app_state"].(map[string]interface{})
require.True(t, ok, "app_state should be a map")

// Traverse to "clerk" module data
clerk, err := traversePath(appState, "clerk")
require.NoError(t, err)

// Validate "event_records" in the "clerk" module
eventRecords, ok := clerk["event_records"].([]interface{})
require.True(t, ok, "event_records should be an array")
require.Len(t, eventRecords, 6)
Expand All @@ -81,11 +72,9 @@ func TestModulesStreamedGenesisExport(t *testing.T) {
require.NotEmpty(t, eventRecord["record_time"])
}

// Traverse to "bor" module data
bor, err := traversePath(appState, "bor")
require.NoError(t, err)

// Validate "spans" in the "bor" module
spans, ok := bor["spans"].([]interface{})
require.True(t, ok, "spans should be an array")
require.Len(t, spans, 5)
Expand All @@ -105,3 +94,23 @@ func TestModulesStreamedGenesisExport(t *testing.T) {
require.NotEmpty(t, spanMap["bor_chain_id"])
}
}

// traversePath traverses the path in the data map.
func traversePath(data map[string]interface{}, path string) (map[string]interface{}, error) {
if path == "." {
return data, nil
}

keys := strings.Split(path, ".")
current := data

for _, key := range keys {
if next, ok := current[key].(map[string]interface{}); ok {
current = next
continue
}
return nil, fmt.Errorf("invalid path: %s", path)
}

return current, nil
}

0 comments on commit 5f6f0e3

Please sign in to comment.