Skip to content

Commit c6ae29a

Browse files
authored
Merge pull request #805 from onflow/feature/pectra-upgrade
Update to EVM Pectra hard-fork
2 parents 0cc36f1 + 039fd1c commit c6ae29a

32 files changed

Lines changed: 1657 additions & 540 deletions

api/api.go

Lines changed: 39 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,12 @@ func (b *BlockChainAPI) GetTransactionByBlockNumberAndIndex(
295295
return nil, err
296296
}
297297

298-
if blockNumber < rpc.EarliestBlockNumber {
299-
latestBlockNumber, err := b.blocks.LatestEVMHeight()
300-
if err != nil {
301-
return handleError[*ethTypes.Transaction](err, l, b.collector)
302-
}
303-
blockNumber = rpc.BlockNumber(latestBlockNumber)
298+
height, err := resolveBlockNumber(blockNumber, b.blocks)
299+
if err != nil {
300+
return handleError[*ethTypes.Transaction](err, l, b.collector)
304301
}
305302

306-
block, err := b.blocks.GetByHeight(uint64(blockNumber))
303+
block, err := b.blocks.GetByHeight(height)
307304
if err != nil {
308305
return handleError[*ethTypes.Transaction](err, l, b.collector)
309306
}
@@ -387,6 +384,7 @@ func (b *BlockChainAPI) GetBlockByHash(
387384
// - When blockNr is -2 the chain latest block is returned.
388385
// - When blockNr is -3 the chain finalized block is returned.
389386
// - When blockNr is -4 the chain safe block is returned.
387+
// - When blockNr is -5 the chain earliest block is returned.
390388
// - When fullTx is true all transactions in the block are returned, otherwise
391389
// only the transaction hash is returned.
392390
func (b *BlockChainAPI) GetBlockByNumber(
@@ -403,13 +401,9 @@ func (b *BlockChainAPI) GetBlockByNumber(
403401
return nil, err
404402
}
405403

406-
height := uint64(blockNumber)
407-
var err error
408-
if blockNumber < 0 {
409-
height, err = b.blocks.LatestEVMHeight()
410-
if err != nil {
411-
return handleError[*ethTypes.Block](err, l, b.collector)
412-
}
404+
height, err := resolveBlockNumber(blockNumber, b.blocks)
405+
if err != nil {
406+
return handleError[*ethTypes.Block](err, l, b.collector)
413407
}
414408

415409
block, err := b.blocks.GetByHeight(height)
@@ -430,7 +424,7 @@ func (b *BlockChainAPI) GetBlockByNumber(
430424
func (b *BlockChainAPI) GetBlockReceipts(
431425
ctx context.Context,
432426
blockNumberOrHash rpc.BlockNumberOrHash,
433-
) ([]map[string]interface{}, error) {
427+
) ([]map[string]any, error) {
434428
l := b.logger.With().
435429
Str("endpoint", "getBlockReceipts").
436430
Str("hash", blockNumberOrHash.String()).
@@ -442,29 +436,29 @@ func (b *BlockChainAPI) GetBlockReceipts(
442436

443437
height, err := resolveBlockTag(&blockNumberOrHash, b.blocks, b.logger)
444438
if err != nil {
445-
return handleError[[]map[string]interface{}](err, l, b.collector)
439+
return handleError[[]map[string]any](err, l, b.collector)
446440
}
447441

448442
block, err := b.blocks.GetByHeight(height)
449443
if err != nil {
450-
return handleError[[]map[string]interface{}](err, l, b.collector)
444+
return handleError[[]map[string]any](err, l, b.collector)
451445
}
452446

453-
receipts := make([]map[string]interface{}, len(block.TransactionHashes))
447+
receipts := make([]map[string]any, len(block.TransactionHashes))
454448
for i, hash := range block.TransactionHashes {
455449
tx, err := b.transactions.Get(hash)
456450
if err != nil {
457-
return handleError[[]map[string]interface{}](err, l, b.collector)
451+
return handleError[[]map[string]any](err, l, b.collector)
458452
}
459453

460454
receipt, err := b.receipts.GetByTransactionID(hash)
461455
if err != nil {
462-
return handleError[[]map[string]interface{}](err, l, b.collector)
456+
return handleError[[]map[string]any](err, l, b.collector)
463457
}
464458

465459
receipts[i], err = ethTypes.MarshalReceipt(receipt, tx)
466460
if err != nil {
467-
return handleError[[]map[string]interface{}](err, l, b.collector)
461+
return handleError[[]map[string]any](err, l, b.collector)
468462
}
469463
}
470464

@@ -510,15 +504,12 @@ func (b *BlockChainAPI) GetBlockTransactionCountByNumber(
510504
return nil, err
511505
}
512506

513-
if blockNumber < rpc.EarliestBlockNumber {
514-
latestBlockNumber, err := b.blocks.LatestEVMHeight()
515-
if err != nil {
516-
return handleError[*hexutil.Uint](err, l, b.collector)
517-
}
518-
blockNumber = rpc.BlockNumber(latestBlockNumber)
507+
height, err := resolveBlockNumber(blockNumber, b.blocks)
508+
if err != nil {
509+
return handleError[*hexutil.Uint](err, l, b.collector)
519510
}
520511

521-
block, err := b.blocks.GetByHeight(uint64(blockNumber))
512+
block, err := b.blocks.GetByHeight(height)
522513
if err != nil {
523514
return handleError[*hexutil.Uint](err, l, b.collector)
524515
}
@@ -562,18 +553,13 @@ func (b *BlockChainAPI) Call(
562553
return handleError[hexutil.Bytes](err, l, b.collector)
563554
}
564555

565-
tx, err := encodeTxFromArgs(args)
566-
if err != nil {
567-
return handleError[hexutil.Bytes](err, l, b.collector)
568-
}
569-
570556
// Default address in case user does not provide one
571557
from := b.config.Coinbase
572558
if args.From != nil {
573559
from = *args.From
574560
}
575561

576-
res, err := b.evm.Call(tx, from, height, stateOverrides, blockOverrides)
562+
res, err := b.evm.Call(args, from, height, stateOverrides, blockOverrides)
577563
if err != nil {
578564
return handleError[hexutil.Bytes](err, l, b.collector)
579565
}
@@ -643,10 +629,15 @@ func (b *BlockChainAPI) GetLogs(
643629
latest := big.NewInt(int64(h))
644630

645631
// if special value, use latest block number
646-
if from.Cmp(models.EarliestBlockNumber) < 0 {
632+
if from.Cmp(models.EarliestBlockNumber) == 0 {
633+
from = big.NewInt(0)
634+
} else if from.Cmp(models.PendingBlockNumber) < 0 {
647635
from = latest
648636
}
649-
if to.Cmp(models.EarliestBlockNumber) < 0 {
637+
638+
if to.Cmp(models.EarliestBlockNumber) == 0 {
639+
to = big.NewInt(0)
640+
} else if to.Cmp(models.PendingBlockNumber) < 0 {
650641
to = latest
651642
}
652643

@@ -722,11 +713,6 @@ func (b *BlockChainAPI) EstimateGas(
722713
return handleError[hexutil.Uint64](err, l, b.collector)
723714
}
724715

725-
tx, err := encodeTxFromArgs(args)
726-
if err != nil {
727-
return hexutil.Uint64(BlockGasLimit), nil // return block gas limit
728-
}
729-
730716
// Default address in case user does not provide one
731717
from := b.config.Coinbase
732718
if args.From != nil {
@@ -742,7 +728,7 @@ func (b *BlockChainAPI) EstimateGas(
742728
return handleError[hexutil.Uint64](err, l, b.collector)
743729
}
744730

745-
estimatedGas, err := b.evm.EstimateGas(tx, from, height, stateOverrides)
731+
estimatedGas, err := b.evm.EstimateGas(args, from, height, stateOverrides)
746732
if err != nil {
747733
return handleError[hexutil.Uint64](err, l, b.collector)
748734
}
@@ -807,14 +793,9 @@ func (b *BlockChainAPI) FeeHistory(
807793
)
808794
}
809795

810-
lastBlockNumber := uint64(lastBlock)
811-
var err error
812-
if lastBlock < 0 {
813-
// From the special block tags, we only support "latest".
814-
lastBlockNumber, err = b.blocks.LatestEVMHeight()
815-
if err != nil {
816-
return handleError[*ethTypes.FeeHistoryResult](err, l, b.collector)
817-
}
796+
lastBlockNumber, err := resolveBlockNumber(lastBlock, b.blocks)
797+
if err != nil {
798+
return handleError[*ethTypes.FeeHistoryResult](err, l, b.collector)
818799
}
819800

820801
var (
@@ -965,7 +946,7 @@ func (b *BlockChainAPI) prepareBlockResponse(
965946
Nonce: types.BlockNonce{0x1},
966947
Timestamp: hexutil.Uint64(block.Timestamp),
967948
BaseFeePerGas: hexutil.Big(*models.BaseFeePerGas),
968-
LogsBloom: types.LogsBloom([]*types.Log{}),
949+
LogsBloom: types.CreateBloom(&types.Receipt{}).Bytes(),
969950
Miner: evmTypes.CoinbaseAddress.ToCommon(),
970951
Sha3Uncles: types.EmptyUncleHash,
971952
}
@@ -983,19 +964,19 @@ func (b *BlockChainAPI) prepareBlockResponse(
983964

984965
if len(transactions) > 0 {
985966
totalGasUsed := hexutil.Uint64(0)
986-
logs := make([]*types.Log, 0)
967+
receipts := types.Receipts{}
987968
for _, tx := range transactions {
988969
txReceipt, err := b.receipts.GetByTransactionID(tx.Hash)
989970
if err != nil {
990971
return nil, err
991972
}
992973
totalGasUsed += hexutil.Uint64(txReceipt.GasUsed)
993-
logs = append(logs, txReceipt.Logs...)
974+
receipts = append(receipts, txReceipt.ToGethReceipt())
994975
blockSize += tx.Size()
995976
}
996977
blockResponse.GasUsed = totalGasUsed
997978
// TODO(m-Peter): Consider if its worthwhile to move this in storage.
998-
blockResponse.LogsBloom = types.LogsBloom(logs)
979+
blockResponse.LogsBloom = types.MergeBloom(receipts).Bytes()
999980
}
1000981
blockResponse.Size = hexutil.Uint64(rlp.ListSize(blockSize))
1001982

@@ -1056,17 +1037,17 @@ func (b *BlockChainAPI) GetUncleByBlockHashAndIndex(
10561037
ctx context.Context,
10571038
blockHash common.Hash,
10581039
index hexutil.Uint,
1059-
) (map[string]interface{}, error) {
1060-
return map[string]interface{}{}, nil
1040+
) (map[string]any, error) {
1041+
return map[string]any{}, nil
10611042
}
10621043

10631044
// GetUncleByBlockNumberAndIndex returns the uncle block for the given block hash and index.
10641045
func (b *BlockChainAPI) GetUncleByBlockNumberAndIndex(
10651046
ctx context.Context,
10661047
blockNumber rpc.BlockNumber,
10671048
index hexutil.Uint,
1068-
) (map[string]interface{}, error) {
1069-
return map[string]interface{}{}, nil
1049+
) (map[string]any, error) {
1050+
return map[string]any{}, nil
10701051
}
10711052

10721053
// MaxPriorityFeePerGas returns a suggestion for a gas tip cap for dynamic fee transactions.

0 commit comments

Comments
 (0)