Skip to content

Commit

Permalink
Merge pull request #6883 from onflow/janez/v0.37-execution-version-fr…
Browse files Browse the repository at this point in the history
…om-snapshot

Get execution version from snapshot instead of state - v0.37 port
  • Loading branch information
janezpodhostnik authored Jan 16, 2025
2 parents bbb02eb + ca6d246 commit a270553
Show file tree
Hide file tree
Showing 42 changed files with 641 additions and 545 deletions.
3 changes: 2 additions & 1 deletion cmd/access/node_builder/access_node_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
"github.com/onflow/flow-go/engine/common/stop"
synceng "github.com/onflow/flow-go/engine/common/synchronization"
"github.com/onflow/flow-go/engine/common/version"
"github.com/onflow/flow-go/engine/execution/computation"
"github.com/onflow/flow-go/engine/execution/computation/query"
"github.com/onflow/flow-go/fvm/storage/derived"
"github.com/onflow/flow-go/ledger"
Expand Down Expand Up @@ -979,7 +980,7 @@ func (builder *FlowAccessNodeBuilder) BuildExecutionSyncComponents() *FlowAccess
builder.Logger,
metrics.NewExecutionCollector(builder.Tracer),
builder.RootChainID,
query.NewProtocolStateWrapper(builder.State),
computation.NewProtocolStateWrapper(builder.State),
builder.Storage.Headers,
builder.ExecutionIndexerCore.RegisterValue,
builder.scriptExecutorConfig,
Expand Down
2 changes: 1 addition & 1 deletion cmd/execution_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ func (exeNode *ExecutionNode) LoadProviderEngine(
exeNode.collector,
node.Tracer,
node.Me,
node.State,
computation.NewProtocolStateWrapper(node.State),
vmCtx,
ledgerViewCommitter,
executionDataProvider,
Expand Down
3 changes: 2 additions & 1 deletion cmd/observer/node_builder/observer_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
"github.com/onflow/flow-go/engine/common/stop"
synceng "github.com/onflow/flow-go/engine/common/synchronization"
"github.com/onflow/flow-go/engine/common/version"
"github.com/onflow/flow-go/engine/execution/computation"
"github.com/onflow/flow-go/engine/execution/computation/query"
"github.com/onflow/flow-go/fvm/storage/derived"
"github.com/onflow/flow-go/ledger"
Expand Down Expand Up @@ -1519,7 +1520,7 @@ func (builder *ObserverServiceBuilder) BuildExecutionSyncComponents() *ObserverS
builder.Logger,
metrics.NewExecutionCollector(builder.Tracer),
builder.RootChainID,
query.NewProtocolStateWrapper(builder.State),
computation.NewProtocolStateWrapper(builder.State),
builder.Storage.Headers,
builder.ExecutionIndexerCore.RegisterValue,
builder.scriptExecutorConfig,
Expand Down
10 changes: 2 additions & 8 deletions engine/access/rpc/backend/script_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/onflow/flow-go/engine/access/index"
"github.com/onflow/flow-go/engine/common/version"
"github.com/onflow/flow-go/engine/execution/computation/query"
"github.com/onflow/flow-go/engine/execution/computation/query/mock"
"github.com/onflow/flow-go/engine/execution/testutil"
"github.com/onflow/flow-go/fvm"
"github.com/onflow/flow-go/fvm/storage/derived"
Expand Down Expand Up @@ -109,12 +108,7 @@ func (s *ScriptExecutorSuite) SetupTest() {
s.headers = newBlockHeadersStorage(blockchain)
s.height = blockchain[0].Header.Height

entropyProvider := testutil.EntropyProviderFixture(nil)
entropyBlock := mock.NewEntropyProviderPerBlock(s.T())
entropyBlock.
On("AtBlockID", testifyMock.AnythingOfType("flow.Identifier")).
Return(entropyProvider).
Maybe()
protocolState := testutil.ProtocolStateWithSourceFixture(nil)

s.snapshot = snapshot.NewSnapshotTree(nil)
s.vm = fvm.NewVirtualMachine()
Expand Down Expand Up @@ -153,7 +147,7 @@ func (s *ScriptExecutorSuite) SetupTest() {
s.log,
metrics.NewNoopCollector(),
s.chain.ChainID(),
entropyBlock,
protocolState,
s.headers,
indexerCore.RegisterValue,
query.NewDefaultConfig(),
Expand Down
20 changes: 4 additions & 16 deletions engine/execution/computation/computer/computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type blockComputer struct {
spockHasher hash.Hasher
receiptHasher hash.Hasher
colResCons []result.ExecutedCollectionConsumer
protocolState protocol.State
protocolState protocol.SnapshotExecutionSubsetProvider
maxConcurrency int
}

Expand Down Expand Up @@ -146,7 +146,7 @@ func NewBlockComputer(
signer module.Local,
executionDataProvider provider.Provider,
colResCons []result.ExecutedCollectionConsumer,
state protocol.State,
state protocol.SnapshotExecutionSubsetProvider,
maxConcurrency int,
) (BlockComputer, error) {
if maxConcurrency < 1 {
Expand Down Expand Up @@ -220,13 +220,7 @@ func (e *blockComputer) queueTransactionRequests(
collectionCtx := fvm.NewContextFromParent(
e.vmCtx,
fvm.WithBlockHeader(blockHeader),
// `protocol.Snapshot` implements `EntropyProvider` interface
// Note that `Snapshot` possible errors for RandomSource() are:
// - storage.ErrNotFound if the QC is unknown.
// - state.ErrUnknownSnapshotReference if the snapshot reference block is unknown
// However, at this stage, snapshot reference block should be known and the QC should also be known,
// so no error is expected in normal operations, as required by `EntropyProvider`.
fvm.WithEntropyProvider(e.protocolState.AtBlockID(blockId)),
fvm.WithProtocolStateSnapshot(e.protocolState.AtBlockID(blockId)),
)

for idx, collection := range rawCollections {
Expand Down Expand Up @@ -262,13 +256,7 @@ func (e *blockComputer) queueTransactionRequests(
systemCtx := fvm.NewContextFromParent(
e.systemChunkCtx,
fvm.WithBlockHeader(blockHeader),
// `protocol.Snapshot` implements `EntropyProvider` interface
// Note that `Snapshot` possible errors for RandomSource() are:
// - storage.ErrNotFound if the QC is unknown.
// - state.ErrUnknownSnapshotReference if the snapshot reference block is unknown
// However, at this stage, snapshot reference block should be known and the QC should also be known,
// so no error is expected in normal operations, as required by `EntropyProvider`.
fvm.WithEntropyProvider(e.protocolState.AtBlockID(blockId)),
fvm.WithProtocolStateSnapshot(e.protocolState.AtBlockID(blockId)),
)
systemCollectionLogger := systemCtx.Logger.With().
Str("block_id", blockIdStr).
Expand Down
3 changes: 0 additions & 3 deletions engine/execution/computation/computer/computer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,6 @@ func TestBlockExecutor_ExecuteBlock(t *testing.T) {
},
),
),
fvm.WithReadVersionFromNodeVersionBeacon(false),
)

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

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

vm := fvm.NewVirtualMachine()
Expand Down
4 changes: 2 additions & 2 deletions engine/execution/computation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func New(
metrics module.ExecutionMetrics,
tracer module.Tracer,
me module.Local,
protoState protocol.State,
protoState protocol.SnapshotExecutionSubsetProvider,
vmCtx fvm.Context,
committer computer.ViewCommitter,
executionDataProvider provider.Provider,
Expand Down Expand Up @@ -140,7 +140,7 @@ func New(
vm,
vmCtx,
derivedChainData,
query.NewProtocolStateWrapper(protoState),
protoState,
)

e := Manager{
Expand Down
2 changes: 1 addition & 1 deletion engine/execution/computation/manager_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
"time"

blockstore "github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/blockstore"
"github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
"github.com/onflow/cadence/runtime"
Expand Down
40 changes: 0 additions & 40 deletions engine/execution/computation/query/entropy_provider.go

This file was deleted.

37 changes: 19 additions & 18 deletions engine/execution/computation/query/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/onflow/flow-go/fvm/storage/snapshot"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/utils/debug"
"github.com/onflow/flow-go/utils/rand"
)
Expand Down Expand Up @@ -110,14 +111,14 @@ func NewDefaultConfig() QueryConfig {
}

type QueryExecutor struct {
config QueryConfig
logger zerolog.Logger
metrics module.ExecutionMetrics
vm fvm.VM
vmCtx fvm.Context
derivedChainData *derived.DerivedChainData
rngLock *sync.Mutex
entropyPerBlock EntropyProviderPerBlock
config QueryConfig
logger zerolog.Logger
metrics module.ExecutionMetrics
vm fvm.VM
vmCtx fvm.Context
derivedChainData *derived.DerivedChainData
rngLock *sync.Mutex
protocolStateSnapshot protocol.SnapshotExecutionSubsetProvider
}

var _ Executor = &QueryExecutor{}
Expand All @@ -129,20 +130,20 @@ func NewQueryExecutor(
vm fvm.VM,
vmCtx fvm.Context,
derivedChainData *derived.DerivedChainData,
entropyPerBlock EntropyProviderPerBlock,
protocolStateSnapshot protocol.SnapshotExecutionSubsetProvider,
) *QueryExecutor {
if config.ComputationLimit > 0 {
vmCtx = fvm.NewContextFromParent(vmCtx, fvm.WithComputationLimit(config.ComputationLimit))
}
return &QueryExecutor{
config: config,
logger: logger,
metrics: metrics,
vm: vm,
vmCtx: vmCtx,
derivedChainData: derivedChainData,
rngLock: &sync.Mutex{},
entropyPerBlock: entropyPerBlock,
config: config,
logger: logger,
metrics: metrics,
vm: vm,
vmCtx: vmCtx,
derivedChainData: derivedChainData,
rngLock: &sync.Mutex{},
protocolStateSnapshot: protocolStateSnapshot,
}
}

Expand Down Expand Up @@ -215,7 +216,7 @@ func (e *QueryExecutor) ExecuteScript(
fvm.NewContextFromParent(
e.vmCtx,
fvm.WithBlockHeader(blockHeader),
fvm.WithEntropyProvider(e.entropyPerBlock.AtBlockID(blockHeader.ID())),
fvm.WithProtocolStateSnapshot(e.protocolStateSnapshot.AtBlockID(blockHeader.ID())),
fvm.WithDerivedBlockData(
e.derivedChainData.NewDerivedBlockDataForScript(blockHeader.ID()))),
fvm.NewScriptWithContextAndArgs(script, requestCtx, arguments...),
Expand Down

This file was deleted.

29 changes: 29 additions & 0 deletions engine/execution/computation/snapshot_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package computation

import (
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol"
)

// SnapshotExecutionSubset is a subset of the protocol state snapshot that is needed by the FVM
var _ protocol.SnapshotExecutionSubset = (protocol.Snapshot)(nil)

// protocolStateWrapper just wraps the protocol.State and returns a SnapshotExecutionSubset
// from the AtBlockID method instead of the protocol.Snapshot interface.
type protocolStateWrapper struct {
protocol.State
}

// protocolStateWrapper implements `EntropyProviderPerBlock`
var _ protocol.SnapshotExecutionSubsetProvider = (*protocolStateWrapper)(nil)

func (p protocolStateWrapper) AtBlockID(blockID flow.Identifier) protocol.SnapshotExecutionSubset {
return p.State.AtBlockID(blockID)
}

// NewProtocolStateWrapper wraps the protocol.State input so that the AtBlockID method returns a
// SnapshotExecutionSubset instead of the protocol.Snapshot interface.
// This is used in the FVM for execution.
func NewProtocolStateWrapper(s protocol.State) protocol.SnapshotExecutionSubsetProvider {
return protocolStateWrapper{s}
}
Loading

0 comments on commit a270553

Please sign in to comment.