Skip to content

Commit

Permalink
R4R: optimize getBlockByNumber api (#23)
Browse files Browse the repository at this point in the history
* optimize getBlockByNumber api

* optimize getBlockByNumber api
  • Loading branch information
byteflyfunny authored Dec 27, 2023
1 parent 6571f93 commit 353122d
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1430,21 +1430,18 @@ func RPCMarshalBlock(ctx context.Context, block *types.Block, inclTx bool, fullT
fields["size"] = hexutil.Uint64(block.Size())

if inclTx {
formatTx := func(tx *types.Transaction) (interface{}, error) {
return tx.Hash(), nil
formatTx := func(idx int, tx *types.Transaction) interface{} {
return tx.Hash()
}
if fullTx {
formatTx = func(tx *types.Transaction) (interface{}, error) {
return newRPCTransactionFromBlockHash(ctx, block, tx.Hash(), backend), nil
formatTx = func(idx int, tx *types.Transaction) interface{} {
return newRPCTransactionFromBlockIndex(ctx, block, uint64(idx), backend)
}
}
txs := block.Transactions()
transactions := make([]interface{}, len(txs))
var err error
for i, tx := range txs {
if transactions[i], err = formatTx(tx); err != nil {
return nil, err
}
transactions[i] = formatTx(i, tx)
}
fields["transactions"] = transactions
}
Expand Down Expand Up @@ -1627,16 +1624,6 @@ func newRPCRawTransactionFromBlockIndex(b *types.Block, index uint64) hexutil.By
return blob
}

// newRPCTransactionFromBlockHash returns a transaction that will serialize to the RPC representation.
func newRPCTransactionFromBlockHash(ctx context.Context, b *types.Block, hash common.Hash, backend Backend) *RPCTransaction {
for idx, tx := range b.Transactions() {
if tx.Hash() == hash {
return newRPCTransactionFromBlockIndex(ctx, b, uint64(idx), backend)
}
}
return nil
}

// accessListResult returns an optional accesslist
// Its the result of the `debug_createAccessList` RPC call.
// It contains an error if the transaction itself failed.
Expand Down

0 comments on commit 353122d

Please sign in to comment.