Skip to content

Commit

Permalink
refactor serailization with gob
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchiqing committed Nov 21, 2024
1 parent 257cb38 commit 016d009
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 90 deletions.
2 changes: 1 addition & 1 deletion cmd/util/cmd/verify-evm-offchain-replay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func run(*cobra.Command, []string) {
}

log.Info().Msgf("verifying range from %d to %d", from, to)
err = Verify(from, to, flow.Testnet, flagDatadir, flagExecutionDataDir)
err = Verify(from, to, flow.Testnet, flagDatadir, flagExecutionDataDir, flagEVMStateGobDir)
if err != nil {
log.Fatal().Err(err).Msg("could not verify last k height")
}
Expand Down
38 changes: 36 additions & 2 deletions cmd/util/cmd/verify-evm-offchain-replay/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,21 @@ func Verify(from uint64, to uint64, chainID flow.ChainID, dataDir string, execut
return err
}
} else {
// TODO: recover from gob
prev := from - 1
valueFileName, allocatorFileName := evmStateGobFileNamesByEndHeight(evmStateGobDir, prev)
values, err := testutils.DeserializeState(valueFileName)
if err != nil {
return err
}

allocators, err := testutils.DeserializeAllocator(allocatorFileName)
if err != nil {
return err
}
store = testutils.GetSimpleValueStorePopulated(values, allocators)
}

return utils.OffchainReplayBackwardCompatibilityTest(
err = utils.OffchainReplayBackwardCompatibilityTest(
chainID,
from,
to,
Expand All @@ -52,6 +63,23 @@ func Verify(from uint64, to uint64, chainID flow.ChainID, dataDir string, execut
executionDataStore,
store,
)

if err != nil {
return err
}

valueFileName, allocatorFileName := evmStateGobFileNamesByEndHeight(evmStateGobDir, to)
values, allocators := store.Dump()
err = testutils.SerializeState(valueFileName, values)
if err != nil {
return err
}
err = testutils.SerializeAllocator(allocatorFileName, allocators)
if err != nil {
return err
}

return nil
}

func initStorages(chainID flow.ChainID, dataDir string, executionDataDir string) (
Expand Down Expand Up @@ -91,3 +119,9 @@ func isEVMRootHeight(chainID flow.ChainID, flowHeight uint64) bool {
}
return flowHeight == 1
}

func evmStateGobFileNamesByEndHeight(evmStateGobDir string, endHeight uint64) (string, string) {
valueFileName := filepath.Join(evmStateGobDir, fmt.Sprintf("values-%d.gob", endHeight))
allocatorFileName := filepath.Join(evmStateGobDir, fmt.Sprintf("allocators-%d.gob", endHeight))
return valueFileName, allocatorFileName
}
91 changes: 4 additions & 87 deletions fvm/evm/offchain/utils/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils_test

import (
"bufio"
"encoding/gob"
"encoding/hex"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -127,9 +126,9 @@ func initStorageWithEVMStateGob(t *testing.T, chainID flow.ChainID, evmStateGob
}

valueFileName, allocatorFileName := evmStateGobFileNamesByEndHeight(evmStateGob, evmStateEndHeight)
values, err := deserialize(valueFileName)
values, err := DeserializeState(valueFileName)
require.NoError(t, err)
allocators, err := deserializeAllocator(allocatorFileName)
allocators, err := DeserializeAllocator(allocatorFileName)
require.NoError(t, err)
store := GetSimpleValueStorePopulated(values, allocators)
return store, evmStateEndHeight
Expand Down Expand Up @@ -203,8 +202,8 @@ func dumpEVMStateToGobFiles(t *testing.T, store *TestValueStore, dir string, evm
valuesFileName, allocatorsFileName := evmStateGobFileNamesByEndHeight(dir, evmStateEndHeight)
values, allocators := store.Dump()

require.NoError(t, serialize(valuesFileName, values))
require.NoError(t, serializeAllocator(allocatorsFileName, allocators))
require.NoError(t, SerializeState(valuesFileName, values))
require.NoError(t, SerializeAllocator(allocatorsFileName, allocators))
return valuesFileName, allocatorsFileName
}

Expand Down Expand Up @@ -257,85 +256,3 @@ func scanEventFilesAndRun(
t.Fatal(err)
}
}

// Serialize function: saves map data to a file
func serialize(filename string, data map[string][]byte) error {
// Create a file to save data
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()

// Use gob to encode data
encoder := gob.NewEncoder(file)
err = encoder.Encode(data)
if err != nil {
return err
}

return nil
}

// Deserialize function: reads map data from a file
func deserialize(filename string) (map[string][]byte, error) {
// Open the file for reading
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()

// Prepare the map to store decoded data
var data map[string][]byte

// Use gob to decode data
decoder := gob.NewDecoder(file)
err = decoder.Decode(&data)
if err != nil {
return nil, err
}

return data, nil
}

// Serialize function: saves map data to a file
func serializeAllocator(filename string, data map[string]uint64) error {
// Create a file to save data
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()

// Use gob to encode data
encoder := gob.NewEncoder(file)
err = encoder.Encode(data)
if err != nil {
return err
}

return nil
}

// Deserialize function: reads map data from a file
func deserializeAllocator(filename string) (map[string]uint64, error) {
// Open the file for reading
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()

// Prepare the map to store decoded data
var data map[string]uint64

// Use gob to decode data
decoder := gob.NewDecoder(file)
err = decoder.Decode(&data)
if err != nil {
return nil, err
}

return data, nil
}
88 changes: 88 additions & 0 deletions fvm/evm/testutils/gob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package testutils

import (
"encoding/gob"
"os"
)

// Serialize function: saves map data to a file
func SerializeState(filename string, data map[string][]byte) error {
// Create a file to save data
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()

// Use gob to encode data
encoder := gob.NewEncoder(file)
err = encoder.Encode(data)
if err != nil {
return err
}

return nil
}

// Deserialize function: reads map data from a file
func DeserializeState(filename string) (map[string][]byte, error) {
// Open the file for reading
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()

// Prepare the map to store decoded data
var data map[string][]byte

// Use gob to decode data
decoder := gob.NewDecoder(file)
err = decoder.Decode(&data)
if err != nil {
return nil, err
}

return data, nil
}

// Serialize function: saves map data to a file
func SerializeAllocator(filename string, data map[string]uint64) error {
// Create a file to save data
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()

// Use gob to encode data
encoder := gob.NewEncoder(file)
err = encoder.Encode(data)
if err != nil {
return err
}

return nil
}

// Deserialize function: reads map data from a file
func DeserializeAllocator(filename string) (map[string]uint64, error) {
// Open the file for reading
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()

// Prepare the map to store decoded data
var data map[string]uint64

// Use gob to decode data
decoder := gob.NewDecoder(file)
err = decoder.Decode(&data)
if err != nil {
return nil, err
}

return data, nil
}

0 comments on commit 016d009

Please sign in to comment.