Skip to content

Commit

Permalink
Merge branch 'master' into change-send-and-subscribe-endoints-msg-ind…
Browse files Browse the repository at this point in the history
…ex-start-with-zero
  • Loading branch information
AndriiDiachuk authored Oct 29, 2024
2 parents 30d861c + 395afb9 commit 2fb29f2
Show file tree
Hide file tree
Showing 31 changed files with 1,123 additions and 174 deletions.
109 changes: 109 additions & 0 deletions cmd/util/cmd/export-evm-state/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package evm_exporter

import (
"fmt"
"os"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

"github.com/onflow/flow-go/cmd/util/ledger/util"
"github.com/onflow/flow-go/fvm/evm"
"github.com/onflow/flow-go/fvm/evm/emulator/state"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/ledger/common/convert"
"github.com/onflow/flow-go/model/flow"
)

var (
flagChain string
flagExecutionStateDir string
flagOutputDir string
flagStateCommitment string
)

var Cmd = &cobra.Command{
Use: "export-evm-state",
Short: "exports evm state into a several binary files",
Run: run,
}

func init() {
Cmd.Flags().StringVar(&flagChain, "chain", "", "Chain name")
_ = Cmd.MarkFlagRequired("chain")

Cmd.Flags().StringVar(&flagExecutionStateDir, "execution-state-dir", "",
"Execution Node state dir (where WAL logs are written")
_ = Cmd.MarkFlagRequired("execution-state-dir")

Cmd.Flags().StringVar(&flagOutputDir, "output-dir", "",
"Directory to write new Execution State to")
_ = Cmd.MarkFlagRequired("output-dir")

Cmd.Flags().StringVar(&flagStateCommitment, "state-commitment", "",
"State commitment (hex-encoded, 64 characters)")
}

func run(*cobra.Command, []string) {
log.Info().Msg("start exporting evm state")
err := ExportEVMState(flagChain, flagExecutionStateDir, flagStateCommitment, flagOutputDir)
if err != nil {
log.Fatal().Err(err).Msg("cannot get export evm state")
}
}

// ExportEVMState evm state
func ExportEVMState(
chainName string,
ledgerPath string,
targetState string,
outputPath string) error {

chainID := flow.ChainID(chainName)

storageRoot := evm.StorageAccountAddress(chainID)
rootOwner := string(storageRoot.Bytes())

payloads, err := util.ReadTrie(ledgerPath, util.ParseStateCommitment(targetState))
if err != nil {
return err
}

// filter payloads of evm storage
filteredPayloads := make(map[flow.RegisterID]*ledger.Payload, 0)
for _, payload := range payloads {
registerID, _, err := convert.PayloadToRegister(payload)
if err != nil {
return fmt.Errorf("failed to convert payload to register: %w", err)
}
if registerID.Owner == rootOwner {
filteredPayloads[registerID] = payload
}
}

payloadsLedger := util.NewPayloadsLedger(filteredPayloads)

exporter, err := state.NewExporter(payloadsLedger, storageRoot)
if err != nil {
return fmt.Errorf("failed to create exporter: %w", err)
}

if _, err := os.Stat(outputPath); os.IsNotExist(err) {
err := os.Mkdir(outputPath, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create path: %w", err)
}
}

fi, err := os.Create(outputPath)
if err != nil {
return err
}
defer fi.Close()

err = exporter.Export(outputPath)
if err != nil {
return fmt.Errorf("failed to export: %w", err)
}
return nil
}
2 changes: 2 additions & 0 deletions cmd/util/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
export "github.com/onflow/flow-go/cmd/util/cmd/exec-data-json-export"
edbs "github.com/onflow/flow-go/cmd/util/cmd/execution-data-blobstore/cmd"
extract "github.com/onflow/flow-go/cmd/util/cmd/execution-state-extract"
evm_state_exporter "github.com/onflow/flow-go/cmd/util/cmd/export-evm-state"
ledger_json_exporter "github.com/onflow/flow-go/cmd/util/cmd/export-json-execution-state"
export_json_transactions "github.com/onflow/flow-go/cmd/util/cmd/export-json-transactions"
extractpayloads "github.com/onflow/flow-go/cmd/util/cmd/extract-payloads-by-address"
Expand Down Expand Up @@ -124,6 +125,7 @@ func addCommands() {
rootCmd.AddCommand(debug_tx.Cmd)
rootCmd.AddCommand(debug_script.Cmd)
rootCmd.AddCommand(generate_authorization_fixes.Cmd)
rootCmd.AddCommand(evm_state_exporter.Cmd)
}

func initConfig() {
Expand Down
9 changes: 7 additions & 2 deletions engine/execution/computation/computer/computer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ func TestBlockExecutor_ExecuteBlock(t *testing.T) {
},
),
),
fvm.WithReadVersionFromNodeVersionBeacon(false),
)

vm := fvm.NewVirtualMachine()
Expand Down Expand Up @@ -816,7 +817,9 @@ func TestBlockExecutor_ExecuteBlock(t *testing.T) {
runtime.Config{},
func(_ runtime.Config) runtime.Runtime {
return rt
})))
})),
fvm.WithReadVersionFromNodeVersionBeacon(false),
)

vm := fvm.NewVirtualMachine()

Expand Down Expand Up @@ -929,7 +932,9 @@ func TestBlockExecutor_ExecuteBlock(t *testing.T) {
runtime.Config{},
func(_ runtime.Config) runtime.Runtime {
return rt
})))
})),
fvm.WithReadVersionFromNodeVersionBeacon(false),
)

vm := fvm.NewVirtualMachine()

Expand Down
9 changes: 9 additions & 0 deletions fvm/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,12 @@ func WithEVMTracer(tracer debug.EVMTracer) Option {
return ctx
}
}

// WithReadVersionFromNodeVersionBeacon sets whether the version from the node version beacon should be read
// this should only be disabled for testing
func WithReadVersionFromNodeVersionBeacon(enabled bool) Option {
return func(ctx Context) Context {
ctx.ReadVersionFromNodeVersionBeacon = enabled
return ctx
}
}
28 changes: 14 additions & 14 deletions fvm/environment/derived_data_invalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (u ContractUpdates) Any() bool {
type DerivedDataInvalidator struct {
ContractUpdates

MeterParamOverridesUpdated bool
ExecutionParametersUpdated bool
}

var _ derived.TransactionInvalidator = DerivedDataInvalidator{}
Expand All @@ -37,16 +37,16 @@ func NewDerivedDataInvalidator(
) DerivedDataInvalidator {
return DerivedDataInvalidator{
ContractUpdates: contractUpdates,
MeterParamOverridesUpdated: meterParamOverridesUpdated(
ExecutionParametersUpdated: executionParametersUpdated(
executionSnapshot,
meterStateRead),
}
}

// meterParamOverridesUpdated returns true if the meter param overrides have been updated
// executionParametersUpdated returns true if the meter param overrides have been updated
// this is done by checking if the registers needed to compute the meter param overrides
// have been touched in the execution snapshot
func meterParamOverridesUpdated(
func executionParametersUpdated(
executionSnapshot *snapshot.ExecutionSnapshot,
meterStateRead *snapshot.ExecutionSnapshot,
) bool {
Expand All @@ -73,16 +73,16 @@ func (invalidator DerivedDataInvalidator) ProgramInvalidator() derived.ProgramIn
return ProgramInvalidator{invalidator}
}

func (invalidator DerivedDataInvalidator) MeterParamOverridesInvalidator() derived.MeterParamOverridesInvalidator {
return MeterParamOverridesInvalidator{invalidator}
func (invalidator DerivedDataInvalidator) ExecutionParametersInvalidator() derived.ExecutionParametersInvalidator {
return ExecutionParametersInvalidator{invalidator}
}

type ProgramInvalidator struct {
DerivedDataInvalidator
}

func (invalidator ProgramInvalidator) ShouldInvalidateEntries() bool {
return invalidator.MeterParamOverridesUpdated ||
return invalidator.ExecutionParametersUpdated ||
invalidator.ContractUpdates.Any()
}

Expand All @@ -91,7 +91,7 @@ func (invalidator ProgramInvalidator) ShouldInvalidateEntry(
program *derived.Program,
_ *snapshot.ExecutionSnapshot,
) bool {
if invalidator.MeterParamOverridesUpdated {
if invalidator.ExecutionParametersUpdated {
// if meter parameters changed we need to invalidate all programs
return true
}
Expand Down Expand Up @@ -124,18 +124,18 @@ func (invalidator ProgramInvalidator) ShouldInvalidateEntry(
return false
}

type MeterParamOverridesInvalidator struct {
type ExecutionParametersInvalidator struct {
DerivedDataInvalidator
}

func (invalidator MeterParamOverridesInvalidator) ShouldInvalidateEntries() bool {
return invalidator.MeterParamOverridesUpdated
func (invalidator ExecutionParametersInvalidator) ShouldInvalidateEntries() bool {
return invalidator.ExecutionParametersUpdated
}

func (invalidator MeterParamOverridesInvalidator) ShouldInvalidateEntry(
func (invalidator ExecutionParametersInvalidator) ShouldInvalidateEntry(
_ struct{},
_ derived.MeterParamOverrides,
_ derived.StateExecutionParameters,
_ *snapshot.ExecutionSnapshot,
) bool {
return invalidator.MeterParamOverridesUpdated
return invalidator.ExecutionParametersUpdated
}
20 changes: 12 additions & 8 deletions fvm/environment/derived_data_invalidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestDerivedDataProgramInvalidator(t *testing.T) {
})
t.Run("meter parameters invalidator invalidates all entries", func(t *testing.T) {
invalidator := environment.DerivedDataInvalidator{
MeterParamOverridesUpdated: true,
ExecutionParametersUpdated: true,
}.ProgramInvalidator()

require.True(t, invalidator.ShouldInvalidateEntries())
Expand Down Expand Up @@ -207,23 +207,23 @@ func TestDerivedDataProgramInvalidator(t *testing.T) {

func TestMeterParamOverridesInvalidator(t *testing.T) {
invalidator := environment.DerivedDataInvalidator{}.
MeterParamOverridesInvalidator()
ExecutionParametersInvalidator()

require.False(t, invalidator.ShouldInvalidateEntries())
require.False(t, invalidator.ShouldInvalidateEntry(
struct{}{},
derived.MeterParamOverrides{},
derived.StateExecutionParameters{},
nil))

invalidator = environment.DerivedDataInvalidator{
ContractUpdates: environment.ContractUpdates{},
MeterParamOverridesUpdated: true,
}.MeterParamOverridesInvalidator()
ExecutionParametersUpdated: true,
}.ExecutionParametersInvalidator()

require.True(t, invalidator.ShouldInvalidateEntries())
require.True(t, invalidator.ShouldInvalidateEntry(
struct{}{},
derived.MeterParamOverrides{},
derived.StateExecutionParameters{},
nil))
}

Expand Down Expand Up @@ -265,7 +265,11 @@ func TestMeterParamOverridesUpdated(t *testing.T) {
txnState, err := blockDatabase.NewTransaction(0, state.DefaultParameters())
require.NoError(t, err)

computer := fvm.NewMeterParamOverridesComputer(ctx, txnState)
computer := fvm.NewExecutionParametersComputer(
ctx.Logger,
ctx,
txnState,
)

overrides, err := computer.Compute(txnState, struct{}{})
require.NoError(t, err)
Expand Down Expand Up @@ -300,7 +304,7 @@ func TestMeterParamOverridesUpdated(t *testing.T) {
environment.ContractUpdates{},
snapshot,
meterStateRead)
require.Equal(t, expected, invalidator.MeterParamOverridesUpdated)
require.Equal(t, expected, invalidator.ExecutionParametersUpdated)
}

executionSnapshot, err = txnState.FinalizeMainTransaction()
Expand Down
6 changes: 6 additions & 0 deletions fvm/environment/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ type Environment interface {
error,
)

// GetCurrentVersionBoundary executes the getCurrentVersionBoundary function on the NodeVersionBeacon contract.
// the function will return the version boundary (version, block height) that is currently in effect.
// the version boundary currently in effect is the highest one not above the current block height.
// if there is no existing version boundary lower than the current block height, the function will return version 0 and block height 0.
GetCurrentVersionBoundary() (cadence.Value, error)

// AccountInfo
GetAccount(address flow.Address) (*flow.Account, error)
GetAccountKeys(address flow.Address) ([]flow.AccountPublicKey, error)
Expand Down
4 changes: 4 additions & 0 deletions fvm/environment/facade_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type facadeEnvironment struct {
ValueStore

*SystemContracts
MinimumCadenceRequiredVersion

UUIDGenerator
AccountLocalIDGenerator
Expand Down Expand Up @@ -107,6 +108,9 @@ func newFacadeEnvironment(
),

SystemContracts: systemContracts,
MinimumCadenceRequiredVersion: NewMinimumCadenceRequiredVersion(
txnState,
),

UUIDGenerator: NewUUIDGenerator(
tracer,
Expand Down
Loading

0 comments on commit 2fb29f2

Please sign in to comment.