Skip to content

Commit

Permalink
use block context provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ramtinms committed Oct 15, 2024
1 parent afdf41f commit cdc3555
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
35 changes: 5 additions & 30 deletions fvm/evm/offchain/query/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (

"github.com/onflow/flow-go/fvm/evm/emulator"
"github.com/onflow/flow-go/fvm/evm/emulator/state"
"github.com/onflow/flow-go/fvm/evm/offchain/blocks"
"github.com/onflow/flow-go/fvm/evm/offchain/storage"
"github.com/onflow/flow-go/fvm/evm/offchain/sync"
"github.com/onflow/flow-go/fvm/evm/types"
"github.com/onflow/flow-go/model/flow"
)
Expand All @@ -26,6 +24,7 @@ type View struct {
chainID flow.ChainID
rootAddr flow.Address
storage *storage.EphemeralStorage
blockSnapshot types.BlockSnapshot
tracer *gethTracers.Tracer
extraPCs []types.PrecompiledContract
maxCallGasLimit uint64
Expand All @@ -36,25 +35,18 @@ func NewView(
chainID flow.ChainID,
rootAddr flow.Address,
storage *storage.EphemeralStorage,
blockSnapshot types.BlockSnapshot,
maxCallGasLimit uint64,
) *View {
return &View{
chainID: chainID,
rootAddr: rootAddr,
storage: storage,
blockSnapshot: blockSnapshot,
maxCallGasLimit: maxCallGasLimit,
}
}

// GetBlockMeta return block meta data
func (v *View) GetBlockMeta() (*blocks.Meta, error) {
blks, err := blocks.NewBlocks(v.chainID, v.rootAddr, v.storage)
if err != nil {
return nil, err
}
return blks.LatestBlock()
}

// GetBalance returns the balance for the given address
// can be used for the `eth_getBalance` endpoint
func (v *View) GetBalance(addr gethCommon.Address) (*big.Int, error) {
Expand Down Expand Up @@ -131,16 +123,12 @@ func (v *View) DryCall(
}
}

blks, err := blocks.NewBlocks(v.chainID, v.rootAddr, v.storage)
if err != nil {
return nil, err
}

// create context
ctx, err := sync.CreateBlockContext(v.chainID, blks, v.tracer)
ctx, err := v.blockSnapshot.BlockContext()
if err != nil {
return nil, err
}
ctx.Tracer = v.tracer
ctx.ExtraPrecompiledContracts = v.extraPCs

// create emulator
Expand Down Expand Up @@ -333,19 +321,6 @@ func WithStateOverrideStateDiff(
}
}

// WithStorageOverrideBlocksMeta constructs a dry call option
// that overrides the value related to block meta
func WithStateOverrideBlocksMeta(meta *blocks.Meta) DryCallOption {
return func(v *View) error {
blks, err := blocks.NewBlocks(v.chainID, v.rootAddr, v.storage)
if err != nil {
return err
}
blks.PushBlockMeta(meta)
return nil
}
}

// WithTracer constructs a dry call option
// that allows running the dry call with the
// custom tracer.
Expand Down
8 changes: 8 additions & 0 deletions fvm/evm/offchain/query/viewProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ViewProvider struct {
chainID flow.ChainID
rootAddr flow.Address
storageProvider types.StorageProvider
blockProvider types.BlockSnapshotProvider
maxCallGasLimit uint64
}

Expand All @@ -20,11 +21,13 @@ func NewViewProvider(
chainID flow.ChainID,
rootAddr flow.Address,
sp types.StorageProvider,
bp types.BlockSnapshotProvider,
maxCallGasLimit uint64,
) *ViewProvider {
return &ViewProvider{
chainID: chainID,
storageProvider: sp,
blockProvider: bp,
rootAddr: rootAddr,
maxCallGasLimit: maxCallGasLimit,
}
Expand All @@ -36,12 +39,17 @@ func (evp *ViewProvider) GetBlockView(height uint64) (*View, error) {
if err != nil {
return nil, err
}
blockSnapshot, err := evp.blockProvider.GetSnapshotAt(height)
if err != nil {
return nil, err
}
return &View{
chainID: evp.chainID,
rootAddr: evp.rootAddr,
maxCallGasLimit: evp.maxCallGasLimit,
storage: storage.NewEphemeralStorage(
storage.NewReadOnlyStorage(readOnly),
),
blockSnapshot: blockSnapshot,
}, nil
}
5 changes: 5 additions & 0 deletions fvm/evm/offchain/query/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/fvm/evm/handler"
"github.com/onflow/flow-go/fvm/evm/offchain/blocks"
"github.com/onflow/flow-go/fvm/evm/offchain/query"
"github.com/onflow/flow-go/fvm/evm/offchain/storage"
"github.com/onflow/flow-go/fvm/evm/precompiles"
Expand All @@ -27,13 +28,17 @@ func TestView(t *testing.T) {

h := SetupHandler(chainID, backend, rootAddr)

blks, err := blocks.NewBlocks(chainID, rootAddr, backend)
require.NoError(t, err)

maxCallGasLimit := uint64(5_000_000)
view := query.NewView(
chainID,
rootAddr,
storage.NewEphemeralStorage(
backend,
),
blks,
maxCallGasLimit,
)

Expand Down

0 comments on commit cdc3555

Please sign in to comment.