Skip to content

Commit 856798f

Browse files
committed
Fix breaking changes due to BlockNumber special values
1 parent 0c3dbdf commit 856798f

3 files changed

Lines changed: 33 additions & 17 deletions

File tree

api/api.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ func (b *BlockChainAPI) GetBlockByHash(
387387
// - When blockNr is -2 the chain latest block is returned.
388388
// - When blockNr is -3 the chain finalized block is returned.
389389
// - When blockNr is -4 the chain safe block is returned.
390+
// - When blockNr is -5 the chain earliest block is returned.
390391
// - When fullTx is true all transactions in the block are returned, otherwise
391392
// only the transaction hash is returned.
392393
func (b *BlockChainAPI) GetBlockByNumber(
@@ -405,7 +406,9 @@ func (b *BlockChainAPI) GetBlockByNumber(
405406

406407
height := uint64(blockNumber)
407408
var err error
408-
if blockNumber < 0 {
409+
if blockNumber == rpc.EarliestBlockNumber {
410+
height = 0
411+
} else if blockNumber <= rpc.PendingBlockNumber {
409412
height, err = b.blocks.LatestEVMHeight()
410413
if err != nil {
411414
return handleError[*ethTypes.Block](err, l, b.collector)
@@ -510,15 +513,18 @@ func (b *BlockChainAPI) GetBlockTransactionCountByNumber(
510513
return nil, err
511514
}
512515

513-
if blockNumber < rpc.EarliestBlockNumber {
514-
latestBlockNumber, err := b.blocks.LatestEVMHeight()
516+
height := uint64(blockNumber)
517+
var err error
518+
if blockNumber == rpc.EarliestBlockNumber {
519+
height = 0
520+
} else if blockNumber <= rpc.PendingBlockNumber {
521+
height, err = b.blocks.LatestEVMHeight()
515522
if err != nil {
516523
return handleError[*hexutil.Uint](err, l, b.collector)
517524
}
518-
blockNumber = rpc.BlockNumber(latestBlockNumber)
519525
}
520526

521-
block, err := b.blocks.GetByHeight(uint64(blockNumber))
527+
block, err := b.blocks.GetByHeight(height)
522528
if err != nil {
523529
return handleError[*hexutil.Uint](err, l, b.collector)
524530
}
@@ -638,10 +644,15 @@ func (b *BlockChainAPI) GetLogs(
638644
latest := big.NewInt(int64(h))
639645

640646
// if special value, use latest block number
641-
if from.Cmp(models.EarliestBlockNumber) < 0 {
647+
if from.Cmp(models.EarliestBlockNumber) == 0 {
648+
from = big.NewInt(0)
649+
} else if from.Cmp(models.PendingBlockNumber) < 0 {
642650
from = latest
643651
}
644-
if to.Cmp(models.EarliestBlockNumber) < 0 {
652+
653+
if to.Cmp(models.EarliestBlockNumber) == 0 {
654+
to = big.NewInt(0)
655+
} else if to.Cmp(models.PendingBlockNumber) < 0 {
645656
to = latest
646657
}
647658

api/utils.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,31 @@ func resolveBlockTag(
5555
}
5656

5757
func resolveBlockNumber(
58-
number rpc.BlockNumber,
58+
blockNumber rpc.BlockNumber,
5959
blocksDB storage.BlockIndexer,
6060
) (uint64, error) {
61-
height := number.Int64()
62-
6361
// if special values (latest) we return latest executed height
6462
//
6563
// all the special values are:
64+
// EarliestBlockNumber = BlockNumber(-5)
6665
// SafeBlockNumber = BlockNumber(-4)
6766
// FinalizedBlockNumber = BlockNumber(-3)
6867
// LatestBlockNumber = BlockNumber(-2)
6968
// PendingBlockNumber = BlockNumber(-1)
7069
//
7170
// EVM on Flow does not have these concepts, but the latest block is the closest fit
72-
if height < 0 {
73-
executed, err := blocksDB.LatestEVMHeight()
71+
height := uint64(blockNumber)
72+
var err error
73+
if blockNumber == rpc.EarliestBlockNumber {
74+
height = 0
75+
} else if blockNumber <= rpc.PendingBlockNumber {
76+
height, err = blocksDB.LatestEVMHeight()
7477
if err != nil {
7578
return 0, err
7679
}
77-
height = int64(executed)
7880
}
7981

80-
return uint64(height), nil
82+
return height, nil
8183
}
8284

8385
// decodeHash parses a hex-encoded 32-byte hash. The input may optionally

models/block.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ import (
1414
)
1515

1616
var (
17-
LatestBlockNumber = big.NewInt(-2)
18-
EarliestBlockNumber = big.NewInt(0)
19-
zeroGethHash = gethCommon.HexToHash("0x0")
17+
EarliestBlockNumber = big.NewInt(-5)
18+
SafeBlockNumber = big.NewInt(-4)
19+
FinalizedBlockNumber = big.NewInt(-3)
20+
LatestBlockNumber = big.NewInt(-2)
21+
PendingBlockNumber = big.NewInt(-1)
22+
zeroGethHash = gethCommon.HexToHash("0x0")
2023
)
2124

2225
func GenesisBlock(chainID flow.ChainID) *Block {

0 commit comments

Comments
 (0)