-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6760 from onflow/leo/export-evm-state-from-gobs
Refactor Export EVM State and Compare State Diff
- Loading branch information
Showing
8 changed files
with
505 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package state | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
func AccountEqual(a, b *Account) bool { | ||
if a.Address != b.Address { | ||
return false | ||
} | ||
if !bytes.Equal(a.Balance.Bytes(), b.Balance.Bytes()) { | ||
return false | ||
} | ||
if a.Nonce != b.Nonce { | ||
return false | ||
} | ||
if a.CodeHash != b.CodeHash { | ||
return false | ||
} | ||
|
||
// CollectionID could be different | ||
return true | ||
} | ||
|
||
// find the difference and return as error | ||
func Diff(a *EVMState, b *EVMState) []error { | ||
var differences []error | ||
|
||
// Compare Accounts | ||
for addr, accA := range a.Accounts { | ||
if accB, exists := b.Accounts[addr]; exists { | ||
if !AccountEqual(accA, accB) { | ||
differences = append(differences, fmt.Errorf("account %s differs, accA %v, accB %v", addr.Hex(), accA, accB)) | ||
} | ||
} else { | ||
differences = append(differences, fmt.Errorf("account %s exists in a but not in b", addr.Hex())) | ||
} | ||
} | ||
for addr := range b.Accounts { | ||
if _, exists := a.Accounts[addr]; !exists { | ||
differences = append(differences, fmt.Errorf("account %s exists in b but not in a", addr.Hex())) | ||
} | ||
} | ||
|
||
// Compare Slots | ||
for addr, slotsA := range a.Slots { | ||
slotsB, exists := b.Slots[addr] | ||
if !exists { | ||
differences = append(differences, fmt.Errorf("slots for address %s exist in a but not in b", addr.Hex())) | ||
continue | ||
} | ||
for key, valueA := range slotsA { | ||
if valueB, exists := slotsB[key]; exists { | ||
if valueA.Value != valueB.Value { | ||
differences = append(differences, fmt.Errorf("slot value for address %s and key %s differs", addr.Hex(), key.Hex())) | ||
} | ||
} else { | ||
differences = append(differences, fmt.Errorf("slot with key %s for address %s exists in a but not in b", key.Hex(), addr.Hex())) | ||
} | ||
} | ||
for key := range slotsB { | ||
if _, exists := slotsA[key]; !exists { | ||
differences = append(differences, fmt.Errorf("slot with key %s for address %s exists in b but not in a", key.Hex(), addr.Hex())) | ||
} | ||
} | ||
} | ||
for addr := range b.Slots { | ||
if _, exists := a.Slots[addr]; !exists { | ||
differences = append(differences, fmt.Errorf("slots for address %s exist in b but not in a", addr.Hex())) | ||
} | ||
} | ||
|
||
// Compare Codes | ||
for hash, codeA := range a.Codes { | ||
if codeB, exists := b.Codes[hash]; exists { | ||
if !bytes.Equal(codeA.Code, codeB.Code) { | ||
differences = append(differences, fmt.Errorf("code for hash %s differs", hash.Hex())) | ||
} | ||
} else { | ||
differences = append(differences, fmt.Errorf("code with hash %s exists in a but not in b", hash.Hex())) | ||
} | ||
} | ||
for hash := range b.Codes { | ||
if _, exists := a.Codes[hash]; !exists { | ||
differences = append(differences, fmt.Errorf("code with hash %s exists in b but not in a", hash.Hex())) | ||
} | ||
} | ||
|
||
return differences | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package state_test | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/onflow/flow-go/fvm/evm" | ||
"github.com/onflow/flow-go/fvm/evm/emulator/state" | ||
"github.com/onflow/flow-go/fvm/evm/testutils" | ||
"github.com/onflow/flow-go/model/flow" | ||
) | ||
|
||
func StateDiff(t *testing.T) { | ||
offchainState, err := state.ImportEVMStateFromGob("/var/flow2/evm-state-from-gobs-218215348/") | ||
require.NoError(t, err) | ||
|
||
enState, err := state.ImportEVMStateFromGob("/var/flow2/evm-state-from-gobs-218215348/") | ||
require.NoError(t, err) | ||
|
||
differences := state.Diff(enState, offchainState) | ||
|
||
require.Len(t, differences, 0) | ||
} | ||
|
||
func EVMStateDiff(t *testing.T) { | ||
|
||
state1 := EVMStateFromReplayGobDir(t, "/var/flow2/evm-state-from-gobs-218215348/", uint64(218215348)) | ||
// state2 := EVMStateFromReplayGobDir(t, "/var/flow2/evm-state-from-gobs-218215348/", uint64(218215348)) | ||
state2 := EVMStateFromCheckpointExtract(t, "/var/flow2/evm-state-from-checkpoint-218215348/") | ||
|
||
differences := state.Diff(state1, state2) | ||
|
||
for i, diff := range differences { | ||
fmt.Printf("Difference %d: %v\n", i, diff) | ||
} | ||
|
||
require.Len(t, differences, 0) | ||
} | ||
|
||
func EVMStateFromCheckpointExtract(t *testing.T, dir string) *state.EVMState { | ||
enState, err := state.ImportEVMStateFromGob("/var/flow2/evm-state-from-gobs-218215348/") | ||
require.NoError(t, err) | ||
return enState | ||
} | ||
|
||
func EVMStateFromReplayGobDir(t *testing.T, gobDir string, flowHeight uint64) *state.EVMState { | ||
valueFileName, allocatorFileName := evmStateGobFileNamesByEndHeight(gobDir, flowHeight) | ||
chainID := flow.Testnet | ||
|
||
allocatorGobs, err := testutils.DeserializeAllocator(allocatorFileName) | ||
require.NoError(t, err) | ||
|
||
storageRoot := evm.StorageAccountAddress(chainID) | ||
valuesGob, err := testutils.DeserializeState(valueFileName) | ||
require.NoError(t, err) | ||
|
||
store := testutils.GetSimpleValueStorePopulated(valuesGob, allocatorGobs) | ||
|
||
bv, err := state.NewBaseView(store, storageRoot) | ||
require.NoError(t, err) | ||
|
||
evmState, err := state.Extract(storageRoot, bv) | ||
require.NoError(t, err) | ||
return evmState | ||
} | ||
|
||
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 | ||
} |
Oops, something went wrong.