Skip to content

Commit

Permalink
get execution version from snapshot instead of state
Browse files Browse the repository at this point in the history
move snapshot provider

move snapshot state provider

cleanup
  • Loading branch information
janezpodhostnik committed Jan 13, 2025
1 parent bbb02eb commit 3a40478
Show file tree
Hide file tree
Showing 40 changed files with 500 additions and 550 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
21 changes: 4 additions & 17 deletions engine/execution/computation/computer/computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/onflow/flow-go/module/executiondatasync/provider"
"github.com/onflow/flow-go/module/mempool/entity"
"github.com/onflow/flow-go/module/trace"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/utils/logging"
)

Expand Down Expand Up @@ -115,7 +114,7 @@ type blockComputer struct {
spockHasher hash.Hasher
receiptHasher hash.Hasher
colResCons []result.ExecutedCollectionConsumer
protocolState protocol.State
protocolState flow.ProtocolSnapshotExecutionSubsetProvider
maxConcurrency int
}

Expand Down Expand Up @@ -146,7 +145,7 @@ func NewBlockComputer(
signer module.Local,
executionDataProvider provider.Provider,
colResCons []result.ExecutedCollectionConsumer,
state protocol.State,
state flow.ProtocolSnapshotExecutionSubsetProvider,
maxConcurrency int,
) (BlockComputer, error) {
if maxConcurrency < 1 {
Expand Down Expand Up @@ -220,13 +219,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 +255,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
5 changes: 2 additions & 3 deletions engine/execution/computation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/executiondatasync/provider"
"github.com/onflow/flow-go/module/mempool/entity"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/utils/logging"
)

Expand Down Expand Up @@ -91,7 +90,7 @@ func New(
metrics module.ExecutionMetrics,
tracer module.Tracer,
me module.Local,
protoState protocol.State,
protoState flow.ProtocolSnapshotExecutionSubsetProvider,
vmCtx fvm.Context,
committer computer.ViewCommitter,
executionDataProvider provider.Provider,
Expand Down Expand Up @@ -140,7 +139,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.

36 changes: 18 additions & 18 deletions engine/execution/computation/query/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,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 flow.ProtocolSnapshotExecutionSubsetProvider
}

var _ Executor = &QueryExecutor{}
Expand All @@ -129,20 +129,20 @@ func NewQueryExecutor(
vm fvm.VM,
vmCtx fvm.Context,
derivedChainData *derived.DerivedChainData,
entropyPerBlock EntropyProviderPerBlock,
protocolStateSnapshot flow.ProtocolSnapshotExecutionSubsetProvider,
) *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 +215,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"
)

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

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

// protocolStateWrapper implements `EntropyProviderPerBlock`
var _ flow.ProtocolSnapshotExecutionSubsetProvider = (*protocolStateWrapper)(nil)

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

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

0 comments on commit 3a40478

Please sign in to comment.