Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local state index #553

Open
wants to merge 187 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
187 commits
Select commit Hold shift + click to select a range
920da4b
add atree ledger implementation in pebble
sideninja Aug 15, 2024
e2d86ce
simple state init implementation
sideninja Aug 15, 2024
6e2bc20
creating custom block context wip
sideninja Aug 19, 2024
a4d7ff3
add precompiled calls to receipt
sideninja Aug 19, 2024
d45f771
Merge branch 'gregor/local-state/database' into gregor/local-state/init
sideninja Aug 19, 2024
5dac5b0
use precompiles in the block context
sideninja Aug 19, 2024
d81d50c
Merge branch 'gregor/local-state/database' into gregor/local-state/init
sideninja Aug 19, 2024
e59015d
add precompiled calls to receipt factory
sideninja Aug 19, 2024
6f4894f
update to specific flow-go with extended events
sideninja Aug 20, 2024
70ae5f1
add new fields to receipt
sideninja Aug 20, 2024
91a6c32
Merge branch 'main' into gregor/local-state/tx-event-extended
sideninja Aug 20, 2024
12e8d3a
Merge branch 'gregor/local-state/tx-event-extended' into gregor/local…
sideninja Aug 20, 2024
c4be2fa
refactor the state context
sideninja Aug 20, 2024
d82d0c4
refactor state factory
sideninja Aug 21, 2024
527acba
add transaction to geth transaction api
sideninja Aug 21, 2024
a848b19
use geth transaction
sideninja Aug 21, 2024
1ec177a
update state execute
sideninja Aug 21, 2024
0bc496e
remove unneeded api from transaction
sideninja Aug 22, 2024
bfc26a4
update state db for direct call and tx
sideninja Aug 22, 2024
d62a3f2
go tidy
sideninja Aug 22, 2024
49e7851
add state engine to bootstrap
sideninja Aug 22, 2024
11aca89
add state engine
sideninja Aug 26, 2024
2cb48b5
add integration state test
sideninja Aug 26, 2024
9aaec7e
skip tx validation on emulator
sideninja Aug 26, 2024
9642317
Merge branch 'main' into gregor/local-state/main
sideninja Sep 4, 2024
265dfe1
add bootstrap state index
sideninja Sep 4, 2024
8d42a5f
update changed type
sideninja Sep 4, 2024
496a0ef
export engines and storage
sideninja Sep 4, 2024
ea5d95a
expose client
sideninja Sep 4, 2024
0877c0d
remove transaction random and coinbase
sideninja Sep 4, 2024
07a8aec
update coinbase and rand
sideninja Sep 4, 2024
b96000d
expose requester
sideninja Sep 4, 2024
c7ba551
formatting
sideninja Sep 4, 2024
daab1f8
temp debug
sideninja Sep 4, 2024
27e885d
improve test with new bootstrap
sideninja Sep 4, 2024
b5f95cb
add compare receipts
sideninja Sep 5, 2024
c3e4265
handle compare of results
sideninja Sep 5, 2024
df2b6e3
fix test bootstrap
sideninja Sep 5, 2024
a2fc6e9
add error message on equal compare
sideninja Sep 5, 2024
471c799
add todo
sideninja Sep 5, 2024
ee2988a
wip test state
sideninja Sep 5, 2024
c084452
cleanup state
sideninja Sep 6, 2024
6803cad
temp disable hash check
sideninja Sep 6, 2024
d57da16
bugfix index usage
sideninja Sep 6, 2024
f7f6305
add logger
sideninja Sep 6, 2024
107cdf1
update nonce in test
sideninja Sep 6, 2024
5f3ff04
Merge branch 'main' into gregor/local-state/main
sideninja Sep 6, 2024
bb91214
add state index engine to bootstrap
sideninja Sep 6, 2024
2b92d4f
use chain id
sideninja Sep 6, 2024
20565a0
fix bug with index
sideninja Sep 10, 2024
c2af01b
refactor the state and engine
sideninja Sep 10, 2024
42708f0
remove old comment
sideninja Sep 10, 2024
5f3fd1c
update api
sideninja Sep 10, 2024
9504e97
update gas used
sideninja Sep 10, 2024
e64b3ca
add height to key id
sideninja Sep 10, 2024
71986cd
refactor ledger id
sideninja Sep 10, 2024
4bcad89
rename ledger to register index
sideninja Sep 10, 2024
736a841
lock set height
sideninja Sep 10, 2024
1fd3e20
add basic register test
sideninja Sep 10, 2024
38aed72
change creation of register storage
sideninja Sep 10, 2024
78df44b
remove logs
sideninja Sep 10, 2024
fa46656
multiple registers test
sideninja Sep 10, 2024
c5f3fb5
add register test at later heights
sideninja Sep 10, 2024
374d56d
test historic balance
sideninja Sep 10, 2024
c67a2de
remove todo
sideninja Sep 11, 2024
4eb1229
nicer syntax
sideninja Sep 11, 2024
27ea12e
Merge branch 'gregor/local-state/engine' into gregor/local-state/hist…
sideninja Sep 11, 2024
d2b8c85
rename evm and remote
sideninja Sep 11, 2024
4554f46
move evm interface and rename file
sideninja Sep 11, 2024
e800a25
move validation and context inside the state
sideninja Sep 11, 2024
e039861
add local client
sideninja Sep 11, 2024
3ba5ba2
add support for calls and estimate
sideninja Sep 11, 2024
460cde6
implement call on state
sideninja Sep 11, 2024
584029f
add client handler
sideninja Sep 11, 2024
edf82ef
add client handler balancing remote and local
sideninja Sep 11, 2024
b2bb7b0
change local client creation
sideninja Sep 11, 2024
e3490c3
update local client syntax
sideninja Sep 11, 2024
f851e83
bootstrap client handler
sideninja Sep 11, 2024
8613437
update test apis
sideninja Sep 11, 2024
23533df
don't use height with slab indexes
sideninja Sep 11, 2024
56e4bca
Merge branch 'gregor/local-state/main' into gregor/local-state/integrate
sideninja Sep 11, 2024
aded8e3
handle failed results
sideninja Sep 12, 2024
ec6d724
handle failed results
sideninja Sep 12, 2024
6cf64e4
check errors by value
sideninja Sep 12, 2024
cf9c41d
add state re-execution height
sideninja Sep 12, 2024
8ed258d
add evm height
sideninja Sep 12, 2024
2a73273
add pebble implementation for block state
sideninja Sep 12, 2024
d912624
update block mock
sideninja Sep 12, 2024
4a7ca08
fend for receipt status
sideninja Sep 12, 2024
0280909
update executed block height
sideninja Sep 12, 2024
80e698f
wip api changes executed height
sideninja Sep 12, 2024
550151c
rename block latest executed and indexed heights
sideninja Sep 13, 2024
7d2d5d1
use latest executed height
sideninja Sep 13, 2024
6829472
return data nil handle
sideninja Sep 13, 2024
a4e896f
add ms response time
sideninja Sep 13, 2024
0ddcaa6
use latest executed height on client init
sideninja Sep 13, 2024
dd9f945
handle estimate failures
sideninja Sep 13, 2024
832c39d
use specific height, don't use latest height since the local and remo…
sideninja Sep 13, 2024
e8ef351
fix tests
sideninja Sep 13, 2024
f4c0b83
Merge branch 'main' into gregor/local-state/main
sideninja Sep 13, 2024
2ca4b19
Merge branch 'gregor/local-state/main' into gregor/local-state/state
sideninja Sep 13, 2024
4e7aab7
change evm client height from int to uint
sideninja Sep 13, 2024
d1651c4
update block hash resolver change
sideninja Sep 13, 2024
935f68e
client handler api updates
sideninja Sep 13, 2024
8584fa7
update remote client changes
sideninja Sep 13, 2024
1aa026c
return remote values
sideninja Sep 13, 2024
f6c4f74
improve time output to ms
sideninja Sep 13, 2024
14ba53b
added cadence arch and environment tests
sideninja Sep 13, 2024
2150fb5
update flow-go with fixed replayer
sideninja Sep 13, 2024
675c7e5
update flow-go
sideninja Sep 14, 2024
cf582e6
add todo comments
sideninja Sep 14, 2024
a4e500c
extend storage contract to have logs emitted and improve the test
sideninja Sep 15, 2024
c20e766
fix test issues and add comment
sideninja Sep 15, 2024
d07b8ff
update comment
sideninja Sep 15, 2024
d0b5da8
update flow-go to specific version
sideninja Sep 15, 2024
ce4eb30
update test with changes
sideninja Sep 15, 2024
5e4201f
remove legacy decode done in flow-go
sideninja Sep 15, 2024
5fb3080
Update README.md
sideninja Sep 15, 2024
4960b86
Merge branch 'gregor/local-state/integrate' into gregor/local-state/main
sideninja Sep 15, 2024
8c319c2
Merge branch 'gregor/local-state/state' into gregor/local-state/main
sideninja Sep 15, 2024
2d6477c
comment out
sideninja Sep 15, 2024
fde99b6
comment out
sideninja Sep 15, 2024
0d6af5e
return errs
sideninja Sep 15, 2024
da2f408
Merge remote-tracking branch 'origin/gregor/local-state/state' into g…
sideninja Sep 15, 2024
05b4f64
fix test changes in contract storage.sol
sideninja Sep 15, 2024
846283c
fix test changes in contract storage.sol
sideninja Sep 15, 2024
24243a3
Merge branch 'gregor/local-state/state' into gregor/local-state/main
sideninja Sep 16, 2024
20d48a3
add register validator
sideninja Sep 16, 2024
703e692
remove uneeded block fetch
sideninja Sep 16, 2024
b1d62e0
Merge branch 'gregor/local-state/integrate' into gregor/local-state/main
sideninja Sep 16, 2024
8723c83
Merge branch 'gregor/local-state/main' into gregor/local-state/check
sideninja Sep 16, 2024
a0881de
Merge branch 'gregor/local-state/main' into feature/state
sideninja Sep 16, 2024
6afb1db
Merge branch 'main' into gregor/local-state/state
sideninja Sep 16, 2024
dc3a030
Merge branch 'main' into gregor/local-state/integrate
sideninja Sep 16, 2024
65707be
Merge branch 'main' into gregor/local-state/historic
sideninja Sep 16, 2024
169c245
Merge branch 'main' into gregor/local-state/engine
sideninja Sep 16, 2024
f44b425
Merge branch 'main' into gregor/local-state/check
sideninja Sep 16, 2024
1d9e57f
add typed error and comments
sideninja Sep 16, 2024
a959e64
add option to check register to config
sideninja Sep 16, 2024
3f0f483
use atree type
sideninja Sep 16, 2024
0d58353
log wrong register
sideninja Sep 16, 2024
18e14c0
clear data in map after block validation
sideninja Sep 16, 2024
2ba6449
Merge branch 'gregor/local-state/engine' into feature/state
sideninja Sep 16, 2024
1059dfb
Merge branch 'main' into gregor/local-state/historic
sideninja Sep 16, 2024
4cb9036
Merge remote-tracking branch 'origin/gregor/local-state/historic' int…
sideninja Sep 16, 2024
41c5a0c
add log
sideninja Sep 16, 2024
d5874db
Merge branch 'gregor/local-state/historic' into feature/state
sideninja Sep 16, 2024
728923e
use remote client for height and add comment
sideninja Sep 16, 2024
61b9e0e
restrict calling latest evm height
sideninja Sep 16, 2024
f75c2c9
remove receipt match
sideninja Sep 16, 2024
73ea0a5
Merge branch 'gregor/local-state/integrate' into feature/state
sideninja Sep 16, 2024
ee67501
Merge branch 'gregor/local-state/integrate' into gregor/local-state/s…
sideninja Sep 16, 2024
cbb422d
Merge branch 'gregor/local-state/check' into feature/state
sideninja Sep 16, 2024
19cfad7
fix errors handling
sideninja Sep 16, 2024
4830bf1
fix error
sideninja Sep 16, 2024
8a217bb
move validator
sideninja Sep 16, 2024
b8e29d3
fix usage of exe client
sideninja Sep 16, 2024
dacc700
update new state
sideninja Sep 16, 2024
4d501de
fix wrong balance encoding
sideninja Sep 17, 2024
661c705
use execution client
sideninja Sep 17, 2024
5d8701c
update validation logic
sideninja Sep 17, 2024
59b21ae
fix bootstrap logic
sideninja Sep 18, 2024
ac776dd
sync up to missed blocks
sideninja Sep 18, 2024
3138a82
better handle batch close
sideninja Sep 18, 2024
252d699
use batch in the handling of blocks exe
sideninja Sep 18, 2024
a3c623d
support usage of batch
sideninja Sep 18, 2024
d56cc9d
update api usage
sideninja Sep 18, 2024
b97ce67
update api usage
sideninja Sep 18, 2024
14ab876
fix state test
sideninja Sep 18, 2024
02c5e42
fix state test
sideninja Sep 18, 2024
6e24eb6
update flow-go
sideninja Sep 18, 2024
015f11a
fix test api change
sideninja Sep 18, 2024
57f7469
Merge branch 'feature/state' into gregor/local-state/progress
sideninja Sep 19, 2024
35f011a
add init latest executed height
sideninja Sep 19, 2024
c83ed0d
Merge remote-tracking branch 'origin/gregor/local-state/progress' int…
sideninja Sep 19, 2024
01ee9ef
revert flow-go ver
sideninja Sep 19, 2024
84b6d1c
revert flow-go ver
sideninja Sep 19, 2024
f02a989
add client handler test
sideninja Sep 19, 2024
fa1c64d
parse errors
sideninja Sep 19, 2024
89f8fca
handle known errors in client
sideninja Sep 19, 2024
11833eb
Merge pull request #559 from onflow/gregor/local-state/error-handle-fix
sideninja Sep 19, 2024
4e33340
improve comparing res
sideninja Sep 19, 2024
972e2ed
Merge pull request #561 from onflow/gregor/local-state/check-values
sideninja Sep 19, 2024
096e754
patch if
sideninja Sep 20, 2024
c8b0815
rename remote ledger
sideninja Sep 20, 2024
f96dcb7
add more details to logs
sideninja Sep 20, 2024
16798f2
Merge pull request #556 from onflow/gregor/local-state/progress
sideninja Sep 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ EVM Gateway has public RPC endpoints available for the following environments:

| Name | Value |
|-----------------|----------------------------------------|
| Network Name | EVM on Flow Testnet |
| Network Name | Testnet |
| Description | The public RPC URL for Flow Testnet |
| RPC Endpoint | https://testnet.evm.nodes.onflow.org |
| Chain ID | 545 |
Expand All @@ -226,7 +226,7 @@ EVM Gateway has public RPC endpoints available for the following environments:

| Name | Value |
|-----------------|----------------------------------------|
| Network Name | EVM on Flow |
| Network Name | Mainnet |
| Description | The public RPC URL for Flow Mainnet |
| RPC Endpoint | https://mainnet.evm.nodes.onflow.org |
| Chain ID | 747 |
Expand Down
150 changes: 80 additions & 70 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func SupportedAPIs(
type BlockChainAPI struct {
logger zerolog.Logger
config *config.Config
evm requester.Requester
evm requester.EVMClient
blocks storage.BlockIndexer
transactions storage.TransactionIndexer
receipts storage.ReceiptIndexer
Expand All @@ -96,7 +96,7 @@ type BlockChainAPI struct {
func NewBlockChainAPI(
logger zerolog.Logger,
config *config.Config,
evm requester.Requester,
evm requester.EVMClient,
blocks storage.BlockIndexer,
transactions storage.TransactionIndexer,
receipts storage.ReceiptIndexer,
Expand All @@ -105,7 +105,7 @@ func NewBlockChainAPI(
collector metrics.Collector,
) (*BlockChainAPI, error) {
// get the height from which the indexing resumed since the last restart, this is needed for syncing status.
indexingResumedHeight, err := blocks.LatestEVMHeight()
indexingResumedHeight, err := blocks.LatestIndexedHeight()
if err != nil {
return nil, fmt.Errorf("failed to retrieve the indexing resumed height: %w", err)
}
Expand All @@ -130,12 +130,12 @@ func (b *BlockChainAPI) BlockNumber(ctx context.Context) (hexutil.Uint64, error)
return 0, err
}

latestBlockHeight, err := b.blocks.LatestEVMHeight()
latest, err := b.blocks.LatestExecutedHeight()
if err != nil {
return handleError[hexutil.Uint64](err, b.logger, b.collector)
return hexutil.Uint64(0), err
}

return hexutil.Uint64(latestBlockHeight), nil
return hexutil.Uint64(latest), nil
}

// Syncing returns false in case the node is currently not syncing with the network.
Expand All @@ -149,7 +149,7 @@ func (b *BlockChainAPI) Syncing(ctx context.Context) (interface{}, error) {
return nil, err
}

currentBlock, err := b.blocks.LatestEVMHeight()
currentBlock, err := b.blocks.LatestExecutedHeight()
if err != nil {
return handleError[any](err, b.logger, b.collector)
}
Expand Down Expand Up @@ -214,7 +214,7 @@ func (b *BlockChainAPI) GetBalance(
return nil, err
}

evmHeight, err := b.getBlockNumber(&blockNumberOrHash)
evmHeight, err := b.resolveBlockNumberOrHash(&blockNumberOrHash)
if err != nil {
return handleError[*hexutil.Big](err, l, b.collector)
}
Expand Down Expand Up @@ -246,6 +246,7 @@ func (b *BlockChainAPI) GetTransactionByHash(
return handleError[*Transaction](err, l, b.collector)
}

// todo what if there's no receipt yet? but tx exists
rcp, err := b.receipts.GetByTransactionID(hash)
if err != nil {
return handleError[*Transaction](err, l, b.collector)
Expand Down Expand Up @@ -305,15 +306,12 @@ func (b *BlockChainAPI) GetTransactionByBlockNumberAndIndex(
return nil, err
}

if blockNumber < rpc.EarliestBlockNumber {
latestBlockNumber, err := b.blocks.LatestEVMHeight()
if err != nil {
return handleError[*Transaction](err, l, b.collector)
}
blockNumber = rpc.BlockNumber(latestBlockNumber)
height, err := b.resolveBlockNumber(blockNumber)
if err != nil {
return nil, err
}

block, err := b.blocks.GetByHeight(uint64(blockNumber))
block, err := b.blocks.GetByHeight(uint64(height))
if err != nil {
return handleError[*Transaction](err, l, b.collector)
}
Expand All @@ -335,7 +333,7 @@ func (b *BlockChainAPI) GetTransactionByBlockNumberAndIndex(
func (b *BlockChainAPI) GetTransactionReceipt(
ctx context.Context,
hash common.Hash,
) (map[string]interface{}, error) {
) (map[string]any, error) {
l := b.logger.With().
Str("endpoint", "getTransactionReceipt").
Str("hash", hash.String()).
Expand All @@ -347,17 +345,27 @@ func (b *BlockChainAPI) GetTransactionReceipt(

tx, err := b.transactions.Get(hash)
if err != nil {
return handleError[map[string]interface{}](err, l, b.collector)
return handleError[map[string]any](err, l, b.collector)
}

receipt, err := b.receipts.GetByTransactionID(hash)
if err != nil {
return handleError[map[string]interface{}](err, l, b.collector)
return handleError[map[string]any](err, l, b.collector)
}

// we don't return receipts until local state index
// recreated the state by executing the transaction
latestExecutedHeight, err := b.blocks.LatestExecutedHeight()
if err != nil {
return handleError[map[string]any](err, l, b.collector)
}
if receipt.BlockNumber.Uint64() > latestExecutedHeight {
return nil, nil
}

txReceipt, err := MarshalReceipt(receipt, tx)
if err != nil {
return handleError[map[string]interface{}](err, l, b.collector)
return handleError[map[string]any](err, l, b.collector)
}

return txReceipt, nil
Expand Down Expand Up @@ -413,17 +421,12 @@ func (b *BlockChainAPI) GetBlockByNumber(
return nil, err
}

height := uint64(blockNumber)
var err error
if blockNumber < 0 {
height, err = b.blocks.LatestEVMHeight()
if err != nil {
return handleError[*Block](err, l, b.collector)
}
height, err := b.resolveBlockNumber(blockNumber)
if err != nil {
return handleError[*Block](err, l, b.collector)
}

block, err := b.blocks.GetByHeight(height)

block, err := b.blocks.GetByHeight(uint64(height))
if err != nil {
return handleError[*Block](err, l, b.collector)
}
Expand All @@ -439,51 +442,42 @@ func (b *BlockChainAPI) GetBlockByNumber(
// GetBlockReceipts returns the block receipts for the given block hash or number or tag.
func (b *BlockChainAPI) GetBlockReceipts(
ctx context.Context,
blockNumberOrHash rpc.BlockNumberOrHash,
) ([]map[string]interface{}, error) {
numHash rpc.BlockNumberOrHash,
) ([]map[string]any, error) {
l := b.logger.With().
Str("endpoint", "getBlockReceipts").
Str("hash", blockNumberOrHash.String()).
Str("hash", numHash.String()).
Logger()

if err := rateLimit(ctx, b.limiter, l); err != nil {
return nil, err
}

var (
block *models.Block
err error
)
if blockNumberOrHash.BlockHash != nil {
block, err = b.blocks.GetByID(*blockNumberOrHash.BlockHash)
} else if blockNumberOrHash.BlockNumber != nil {
block, err = b.blocks.GetByHeight(uint64(blockNumberOrHash.BlockNumber.Int64()))
} else {
return handleError[[]map[string]interface{}](
fmt.Errorf("%w: block number or hash not provided", errs.ErrInvalid),
l,
b.collector,
)
height, err := b.resolveBlockNumberOrHash(&numHash)
if err != nil {
return handleError[[]map[string]any](err, l, b.collector)
}

block, err := b.blocks.GetByHeight(uint64(height))
if err != nil {
return handleError[[]map[string]interface{}](err, l, b.collector)
return handleError[[]map[string]any](err, l, b.collector)
}

receipts := make([]map[string]interface{}, len(block.TransactionHashes))
for i, hash := range block.TransactionHashes {
tx, err := b.transactions.Get(hash)
if err != nil {
return handleError[[]map[string]interface{}](err, l, b.collector)
return handleError[[]map[string]any](err, l, b.collector)
}

receipt, err := b.receipts.GetByTransactionID(hash)
if err != nil {
return handleError[[]map[string]interface{}](err, l, b.collector)
return handleError[[]map[string]any](err, l, b.collector)
}

receipts[i], err = MarshalReceipt(receipt, tx)
if err != nil {
return handleError[[]map[string]interface{}](err, l, b.collector)
return handleError[[]map[string]any](err, l, b.collector)
}
}

Expand Down Expand Up @@ -529,15 +523,12 @@ func (b *BlockChainAPI) GetBlockTransactionCountByNumber(
return nil, err
}

if blockNumber < rpc.EarliestBlockNumber {
latestBlockNumber, err := b.blocks.LatestEVMHeight()
if err != nil {
return handleError[*hexutil.Uint](err, l, b.collector)
}
blockNumber = rpc.BlockNumber(latestBlockNumber)
height, err := b.resolveBlockNumber(blockNumber)
if err != nil {
return handleError[*hexutil.Uint](err, l, b.collector)
}

block, err := b.blocks.GetByHeight(uint64(blockNumber))
block, err := b.blocks.GetByHeight(uint64(height))
if err != nil {
return handleError[*hexutil.Uint](err, l, b.collector)
}
Expand Down Expand Up @@ -576,7 +567,7 @@ func (b *BlockChainAPI) Call(
blockNumberOrHash = &latestBlockNumberOrHash
}

evmHeight, err := b.getBlockNumber(blockNumberOrHash)
evmHeight, err := b.resolveBlockNumberOrHash(blockNumberOrHash)
if err != nil {
return handleError[hexutil.Bytes](err, l, b.collector)
}
Expand Down Expand Up @@ -637,6 +628,7 @@ func (b *BlockChainAPI) GetLogs(
// otherwise we use the block range as the filter

// assign default values to latest block number, unless provided
// todo should we resolve latest to specific height
from := models.LatestBlockNumber
if criteria.FromBlock != nil {
from = criteria.FromBlock
Expand All @@ -646,7 +638,7 @@ func (b *BlockChainAPI) GetLogs(
to = criteria.ToBlock
}

h, err := b.blocks.LatestEVMHeight()
h, err := b.blocks.LatestIndexedHeight()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have seen b.blocks.LatestEVMHeight() being replaced sometimes by b.blocks.LatestIndexedHeight() and sometimes by b.blocks.LatestExecutedHeight(). Which should be used in which situation?

if err != nil {
return handleError[[]*types.Log](err, l, b.collector)
}
Expand Down Expand Up @@ -694,7 +686,7 @@ func (b *BlockChainAPI) GetTransactionCount(
return nil, err
}

evmHeight, err := b.getBlockNumber(&blockNumberOrHash)
evmHeight, err := b.resolveBlockNumberOrHash(&blockNumberOrHash)
if err != nil {
return handleError[*hexutil.Uint64](err, l, b.collector)
}
Expand Down Expand Up @@ -760,7 +752,7 @@ func (b *BlockChainAPI) EstimateGas(
blockNumberOrHash = &latestBlockNumberOrHash
}

evmHeight, err := b.getBlockNumber(blockNumberOrHash)
evmHeight, err := b.resolveBlockNumberOrHash(blockNumberOrHash)
if err != nil {
return handleError[hexutil.Uint64](err, l, b.collector)
}
Expand Down Expand Up @@ -789,7 +781,7 @@ func (b *BlockChainAPI) GetCode(
return nil, err
}

evmHeight, err := b.getBlockNumber(&blockNumberOrHash)
evmHeight, err := b.resolveBlockNumberOrHash(&blockNumberOrHash)
if err != nil {
return handleError[hexutil.Bytes](err, l, b.collector)
}
Expand Down Expand Up @@ -834,7 +826,7 @@ func (b *BlockChainAPI) FeeHistory(
var err error
if lastBlock < 0 {
// From the special block tags, we only support "latest".
lastBlockNumber, err = b.blocks.LatestEVMHeight()
lastBlockNumber, err = b.blocks.LatestIndexedHeight()
if err != nil {
return handleError[*FeeHistoryResult](err, l, b.collector)
}
Expand Down Expand Up @@ -914,7 +906,7 @@ func (b *BlockChainAPI) GetStorageAt(
)
}

evmHeight, err := b.getBlockNumber(&blockNumberOrHash)
evmHeight, err := b.resolveBlockNumberOrHash(&blockNumberOrHash)
if err != nil {
return handleError[hexutil.Bytes](err, l, b.collector)
}
Expand Down Expand Up @@ -1029,27 +1021,45 @@ func (b *BlockChainAPI) prepareBlockResponse(
return blockResponse, nil
}

func (b *BlockChainAPI) getBlockNumber(blockNumberOrHash *rpc.BlockNumberOrHash) (int64, error) {
// resolveBlockNumberOrHash resolves the block number or hash into the evm block number.
// If block number is negative we resolve to latest executed height.
func (b *BlockChainAPI) resolveBlockNumberOrHash(block *rpc.BlockNumberOrHash) (uint64, error) {
err := fmt.Errorf("%w: neither block number nor hash specified", errs.ErrInvalid)
if blockNumberOrHash == nil {
if block == nil {
return 0, err
}
if number, ok := blockNumberOrHash.Number(); ok {
return number.Int64(), nil
if number, ok := block.Number(); ok {
return b.resolveBlockNumber(number)
}

if hash, ok := blockNumberOrHash.Hash(); ok {
if hash, ok := block.Hash(); ok {
evmHeight, err := b.blocks.GetHeightByID(hash)
if err != nil {
b.logger.Error().Err(err).Msg("failed to get block by hash")
return 0, err
}
return int64(evmHeight), nil
return evmHeight, nil
}

return 0, err
}

// resolveBlockNumber resolves the block number into the evm block number.
// If block number is negative we resolve to latest executed height.
func (b *BlockChainAPI) resolveBlockNumber(number rpc.BlockNumber) (uint64, error) {
height := number.Int64()

// if special values (latest) we return latest executed height
if height < 0 {
executed, err := b.blocks.LatestExecutedHeight()
if err != nil {
return 0, err
}
height = int64(executed)
}

return uint64(height), nil
}

// handleError takes in an error and in case the error is of type ErrEntityNotFound
// it returns nil instead of an error since that is according to the API spec,
// if the error is not of type ErrEntityNotFound it will return the error and the generic
Expand Down
Loading