Skip to content

Commit

Permalink
refactor: move AllowUnfinalizedQueries out of vm.Config (#1052)
Browse files Browse the repository at this point in the history
* remove ethdb

* refactor: move AllowUnfinalizedQueries out of vm.Config
  • Loading branch information
darioush authored Jan 23, 2024
1 parent b14bbb2 commit 955377b
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,8 +894,8 @@ func (fb *filterBackend) SubscribeAcceptedTransactionEvent(ch chan<- core.NewTxs
return fb.bc.SubscribeAcceptedTransactionEvent(ch)
}

func (fb *filterBackend) GetVMConfig() *vm.Config {
return fb.bc.GetVMConfig()
func (fb *filterBackend) IsAllowUnfinalizedQueries() bool {
return false
}

func (fb *filterBackend) LastAcceptedBlock() *types.Block {
Expand Down
3 changes: 0 additions & 3 deletions core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ type Config struct {
NoBaseFee bool // Forces the EIP-1559 baseFee to 0 (needed for 0 price calls)
EnablePreimageRecording bool // Enables recording of SHA3/keccak preimages
ExtraEips []int // Additional EIPS that are to be enabled

// AllowUnfinalizedQueries allow unfinalized queries
AllowUnfinalizedQueries bool
}

// ScopeContext contains the things that are per-call, such as stack and memory,
Expand Down
19 changes: 12 additions & 7 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type EthAPIBackend struct {
extRPCEnabled bool
allowUnprotectedTxs bool
allowUnprotectedTxHashes map[common.Hash]struct{} // Invariant: read-only after creation.
allowUnfinalizedQueries bool
eth *Ethereum
gpo *gasprice.Oracle
}
Expand All @@ -67,8 +68,12 @@ func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
return b.eth.blockchain.Config()
}

func (b *EthAPIBackend) GetVMConfig() *vm.Config {
return b.eth.blockchain.GetVMConfig()
func (b *EthAPIBackend) IsAllowUnfinalizedQueries() bool {
return b.allowUnfinalizedQueries
}

func (b *EthAPIBackend) SetAllowUnfinalizedQueries(allow bool) {
b.allowUnfinalizedQueries = allow
}

func (b *EthAPIBackend) CurrentBlock() *types.Header {
Expand All @@ -90,7 +95,7 @@ func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumb
return acceptedBlock.Header(), nil
}

if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil {
if !b.IsAllowUnfinalizedQueries() && acceptedBlock != nil {
if number.Int64() > acceptedBlock.Number().Int64() {
return nil, ErrUnfinalizedData
}
Expand Down Expand Up @@ -118,7 +123,7 @@ func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*ty
}

acceptedBlock := b.eth.LastAcceptedBlock()
if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil {
if !b.IsAllowUnfinalizedQueries() && acceptedBlock != nil {
if header.Number.Cmp(acceptedBlock.Number()) > 0 {
return nil, ErrUnfinalizedData
}
Expand Down Expand Up @@ -154,7 +159,7 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe
return acceptedBlock, nil
}

if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil {
if !b.IsAllowUnfinalizedQueries() && acceptedBlock != nil {
if number.Int64() > acceptedBlock.Number().Int64() {
return nil, ErrUnfinalizedData
}
Expand All @@ -179,7 +184,7 @@ func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*typ
}

acceptedBlock := b.eth.LastAcceptedBlock()
if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil {
if !b.IsAllowUnfinalizedQueries() && acceptedBlock != nil {
if number.Cmp(acceptedBlock.Number()) > 0 {
return nil, ErrUnfinalizedData
}
Expand Down Expand Up @@ -350,7 +355,7 @@ func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash)
// expectations with clients (expect an empty response when a transaction
// does not exist).
acceptedBlock := b.eth.LastAcceptedBlock()
if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil && tx != nil {
if !b.IsAllowUnfinalizedQueries() && acceptedBlock != nil && tx != nil {
if blockNumber > acceptedBlock.NumberU64() {
return nil, common.Hash{}, 0, 0, nil
}
Expand Down
2 changes: 1 addition & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ func New(
var (
vmConfig = vm.Config{
EnablePreimageRecording: config.EnablePreimageRecording,
AllowUnfinalizedQueries: config.AllowUnfinalizedQueries,
}
cacheConfig = &core.CacheConfig{
TrieCleanLimit: config.TrieCleanCache,
Expand Down Expand Up @@ -241,6 +240,7 @@ func New(
extRPCEnabled: stack.Config().ExtRPCEnabled(),
allowUnprotectedTxs: config.AllowUnprotectedTxs,
allowUnprotectedTxHashes: allowUnprotectedTxHashes,
allowUnfinalizedQueries: config.AllowUnfinalizedQueries,
eth: eth,
}
if config.AllowUnprotectedTxs {
Expand Down
8 changes: 4 additions & 4 deletions eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (api *FilterAPI) NewBlockFilter() rpc.ID {
headerSub *Subscription
)

if api.sys.backend.GetVMConfig().AllowUnfinalizedQueries {
if api.sys.backend.IsAllowUnfinalizedQueries() {
headerSub = api.events.SubscribeNewHeads(headers)
} else {
headerSub = api.events.SubscribeAcceptedHeads(headers)
Expand Down Expand Up @@ -291,7 +291,7 @@ func (api *FilterAPI) NewHeads(ctx context.Context) (*rpc.Subscription, error) {
headersSub event.Subscription
)

if api.sys.backend.GetVMConfig().AllowUnfinalizedQueries {
if api.sys.backend.IsAllowUnfinalizedQueries() {
headersSub = api.events.SubscribeNewHeads(headers)
} else {
headersSub = api.events.SubscribeAcceptedHeads(headers)
Expand Down Expand Up @@ -328,7 +328,7 @@ func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subsc
err error
)

if api.sys.backend.GetVMConfig().AllowUnfinalizedQueries {
if api.sys.backend.IsAllowUnfinalizedQueries() {
logsSub, err = api.events.SubscribeLogs(interfaces.FilterQuery(crit), matchedLogs)
if err != nil {
return nil, err
Expand Down Expand Up @@ -383,7 +383,7 @@ func (api *FilterAPI) NewFilter(crit FilterCriteria) (rpc.ID, error) {
err error
)

if api.sys.backend.GetVMConfig().AllowUnfinalizedQueries {
if api.sys.backend.IsAllowUnfinalizedQueries() {
logsSub, err = api.events.SubscribeLogs(interfaces.FilterQuery(crit), logs)
if err != nil {
return rpc.ID(""), err
Expand Down
2 changes: 1 addition & 1 deletion eth/filters/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Filter struct {
// NewRangeFilter creates a new filter which uses a bloom filter on blocks to
// figure out whether a particular block is interesting or not.
func (sys *FilterSystem) NewRangeFilter(begin, end int64, addresses []common.Address, topics [][]common.Hash) (*Filter, error) {
allowUnfinalizedQueries := sys.backend.GetVMConfig().AllowUnfinalizedQueries
allowUnfinalizedQueries := sys.backend.IsAllowUnfinalizedQueries()
acceptedBlock := sys.backend.LastAcceptedBlock()

// Flatten the address and topic filter clauses into a single bloombits filter
Expand Down
3 changes: 1 addition & 2 deletions eth/filters/filter_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"github.com/ava-labs/subnet-evm/core"
"github.com/ava-labs/subnet-evm/core/bloombits"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/core/vm"
"github.com/ava-labs/subnet-evm/interfaces"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/rpc"
Expand Down Expand Up @@ -85,7 +84,7 @@ type Backend interface {
ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)

// Added to the backend interface to support limiting of logs requests
GetVMConfig() *vm.Config
IsAllowUnfinalizedQueries() bool
LastAcceptedBlock() *types.Block
GetMaxBlocksPerRequest() int64
}
Expand Down
5 changes: 2 additions & 3 deletions eth/filters/filter_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"github.com/ava-labs/subnet-evm/core/bloombits"
"github.com/ava-labs/subnet-evm/core/rawdb"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/core/vm"
"github.com/ava-labs/subnet-evm/interfaces"
"github.com/ava-labs/subnet-evm/internal/ethapi"
"github.com/ava-labs/subnet-evm/params"
Expand Down Expand Up @@ -78,8 +77,8 @@ func (b *testBackend) ChainDb() ethdb.Database {
return b.db
}

func (b *testBackend) GetVMConfig() *vm.Config {
return &vm.Config{AllowUnfinalizedQueries: true}
func (b *testBackend) IsAllowUnfinalizedQueries() bool {
return true
}

func (b *testBackend) GetMaxBlocksPerRequest() int64 {
Expand Down
8 changes: 4 additions & 4 deletions plugin/evm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ func TestNonCanonicalAccept(t *testing.T) {
t.Fatal(err)
}

vm1.blockChain.GetVMConfig().AllowUnfinalizedQueries = true
vm1.eth.APIBackend.SetAllowUnfinalizedQueries(true)

blkBHeight := vm1BlkB.Height()
blkBHash := vm1BlkB.(*chain.BlockWrapper).Block.(*Block).ethBlock.Hash()
Expand Down Expand Up @@ -1241,7 +1241,7 @@ func TestStickyPreference(t *testing.T) {
t.Fatal(err)
}

vm1.blockChain.GetVMConfig().AllowUnfinalizedQueries = true
vm1.eth.APIBackend.SetAllowUnfinalizedQueries(true)

blkBHeight := vm1BlkB.Height()
blkBHash := vm1BlkB.(*chain.BlockWrapper).Block.(*Block).ethBlock.Hash()
Expand Down Expand Up @@ -1940,7 +1940,7 @@ func TestLastAcceptedBlockNumberAllow(t *testing.T) {
blkHeight := blk.Height()
blkHash := blk.(*chain.BlockWrapper).Block.(*Block).ethBlock.Hash()

vm.blockChain.GetVMConfig().AllowUnfinalizedQueries = true
vm.eth.APIBackend.SetAllowUnfinalizedQueries(true)

ctx := context.Background()
b, err := vm.eth.APIBackend.BlockByNumber(ctx, rpc.BlockNumber(blkHeight))
Expand All @@ -1951,7 +1951,7 @@ func TestLastAcceptedBlockNumberAllow(t *testing.T) {
t.Fatalf("expected block at %d to have hash %s but got %s", blkHeight, blkHash.Hex(), b.Hash().Hex())
}

vm.blockChain.GetVMConfig().AllowUnfinalizedQueries = false
vm.eth.APIBackend.SetAllowUnfinalizedQueries(false)

_, err = vm.eth.APIBackend.BlockByNumber(ctx, rpc.BlockNumber(blkHeight))
if !errors.Is(err, eth.ErrUnfinalizedData) {
Expand Down

0 comments on commit 955377b

Please sign in to comment.