|
| 1 | +package verify |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/dgraph-io/badger/v2" |
| 10 | + badgerds "github.com/ipfs/go-ds-badger2" |
| 11 | + "github.com/rs/zerolog" |
| 12 | + "github.com/rs/zerolog/log" |
| 13 | + |
| 14 | + "github.com/onflow/flow-go/cmd/util/cmd/common" |
| 15 | + "github.com/onflow/flow-go/fvm/evm/offchain/utils" |
| 16 | + "github.com/onflow/flow-go/fvm/evm/testutils" |
| 17 | + "github.com/onflow/flow-go/model/flow" |
| 18 | + "github.com/onflow/flow-go/module/blobs" |
| 19 | + "github.com/onflow/flow-go/module/executiondatasync/execution_data" |
| 20 | + "github.com/onflow/flow-go/storage" |
| 21 | +) |
| 22 | + |
| 23 | +// Verify verifies the offchain replay of EVM blocks from the given height range |
| 24 | +// and updates the EVM state gob files with the latest state |
| 25 | +func Verify( |
| 26 | + log zerolog.Logger, |
| 27 | + from uint64, |
| 28 | + to uint64, |
| 29 | + chainID flow.ChainID, |
| 30 | + dataDir string, |
| 31 | + executionDataDir string, |
| 32 | + evmStateGobDir string, |
| 33 | + saveEveryNBlocks uint64, |
| 34 | +) error { |
| 35 | + lg := log.With(). |
| 36 | + Uint64("from", from).Uint64("to", to). |
| 37 | + Str("chain", chainID.String()). |
| 38 | + Str("dataDir", dataDir). |
| 39 | + Str("executionDataDir", executionDataDir). |
| 40 | + Str("evmStateGobDir", evmStateGobDir). |
| 41 | + Uint64("saveEveryNBlocks", saveEveryNBlocks). |
| 42 | + Logger() |
| 43 | + |
| 44 | + lg.Info().Msgf("verifying range from %d to %d", from, to) |
| 45 | + |
| 46 | + db, storages, executionDataStore, dsStore, err := initStorages(dataDir, executionDataDir) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("could not initialize storages: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + defer db.Close() |
| 52 | + defer dsStore.Close() |
| 53 | + |
| 54 | + var store *testutils.TestValueStore |
| 55 | + |
| 56 | + // root block require the account status registers to be saved |
| 57 | + isRoot := utils.IsEVMRootHeight(chainID, from) |
| 58 | + if isRoot { |
| 59 | + store = testutils.GetSimpleValueStore() |
| 60 | + } else { |
| 61 | + prev := from - 1 |
| 62 | + store, err = loadState(prev, evmStateGobDir) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("could not load EVM state from previous height %d: %w", prev, err) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // save state every N blocks |
| 69 | + onHeightReplayed := func(height uint64) error { |
| 70 | + log.Info().Msgf("replayed height %d", height) |
| 71 | + if height%saveEveryNBlocks == 0 { |
| 72 | + err := saveState(store, height, evmStateGobDir) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + } |
| 77 | + return nil |
| 78 | + } |
| 79 | + |
| 80 | + // replay blocks |
| 81 | + err = utils.OffchainReplayBackwardCompatibilityTest( |
| 82 | + log, |
| 83 | + chainID, |
| 84 | + from, |
| 85 | + to, |
| 86 | + storages.Headers, |
| 87 | + storages.Results, |
| 88 | + executionDataStore, |
| 89 | + store, |
| 90 | + onHeightReplayed, |
| 91 | + ) |
| 92 | + |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + err = saveState(store, to, evmStateGobDir) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + lg.Info().Msgf("successfully verified range from %d to %d", from, to) |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func saveState(store *testutils.TestValueStore, height uint64, gobDir string) error { |
| 108 | + valueFileName, allocatorFileName := evmStateGobFileNamesByEndHeight(gobDir, height) |
| 109 | + values, allocators := store.Dump() |
| 110 | + err := testutils.SerializeState(valueFileName, values) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + err = testutils.SerializeAllocator(allocatorFileName, allocators) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + log.Info().Msgf("saved EVM state to %s and %s", valueFileName, allocatorFileName) |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func loadState(height uint64, gobDir string) (*testutils.TestValueStore, error) { |
| 125 | + valueFileName, allocatorFileName := evmStateGobFileNamesByEndHeight(gobDir, height) |
| 126 | + values, err := testutils.DeserializeState(valueFileName) |
| 127 | + if err != nil { |
| 128 | + return nil, fmt.Errorf("could not deserialize state %v: %w", valueFileName, err) |
| 129 | + } |
| 130 | + |
| 131 | + allocators, err := testutils.DeserializeAllocator(allocatorFileName) |
| 132 | + if err != nil { |
| 133 | + return nil, fmt.Errorf("could not deserialize allocator %v: %w", allocatorFileName, err) |
| 134 | + } |
| 135 | + store := testutils.GetSimpleValueStorePopulated(values, allocators) |
| 136 | + |
| 137 | + log.Info().Msgf("loaded EVM state for height %d from gob file %v", height, valueFileName) |
| 138 | + return store, nil |
| 139 | +} |
| 140 | + |
| 141 | +func initStorages(dataDir string, executionDataDir string) ( |
| 142 | + *badger.DB, |
| 143 | + *storage.All, |
| 144 | + execution_data.ExecutionDataGetter, |
| 145 | + io.Closer, |
| 146 | + error, |
| 147 | +) { |
| 148 | + db := common.InitStorage(dataDir) |
| 149 | + |
| 150 | + storages := common.InitStorages(db) |
| 151 | + |
| 152 | + datastoreDir := filepath.Join(executionDataDir, "blobstore") |
| 153 | + err := os.MkdirAll(datastoreDir, 0700) |
| 154 | + if err != nil { |
| 155 | + return nil, nil, nil, nil, err |
| 156 | + } |
| 157 | + dsOpts := &badgerds.DefaultOptions |
| 158 | + ds, err := badgerds.NewDatastore(datastoreDir, dsOpts) |
| 159 | + if err != nil { |
| 160 | + return nil, nil, nil, nil, err |
| 161 | + } |
| 162 | + |
| 163 | + executionDataBlobstore := blobs.NewBlobstore(ds) |
| 164 | + executionDataStore := execution_data.NewExecutionDataStore(executionDataBlobstore, execution_data.DefaultSerializer) |
| 165 | + |
| 166 | + return db, storages, executionDataStore, ds, nil |
| 167 | +} |
| 168 | + |
| 169 | +func evmStateGobFileNamesByEndHeight(evmStateGobDir string, endHeight uint64) (string, string) { |
| 170 | + valueFileName := filepath.Join(evmStateGobDir, fmt.Sprintf("values-%d.gob", endHeight)) |
| 171 | + allocatorFileName := filepath.Join(evmStateGobDir, fmt.Sprintf("allocators-%d.gob", endHeight)) |
| 172 | + return valueFileName, allocatorFileName |
| 173 | +} |
0 commit comments