Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit 69ecbc9

Browse files
committed
block and log functions
1 parent de0d25b commit 69ecbc9

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

internal/ethapi/api.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,63 @@ func (s *PublicBlockChainAPI) GetBlockReceipts(ctx context.Context, hash common.
896896
return resultFields, nil
897897
}
898898

899+
func (s *PublicBlockChainAPI) GetMinimalBlockLogs(ctx context.Context, hash common.Hash) ([]interface{}, error) {
900+
receipts, err := s.b.GetReceipts(ctx, hash)
901+
if err != nil {
902+
return nil, err
903+
}
904+
resultLogs := []interface{}{}
905+
for _, receipt := range receipts {
906+
for _, log := range receipt.Logs {
907+
resultLogs = append(resultLogs, map[string]interface{}{
908+
"address": log.Address,
909+
"topics": log.Topics,
910+
"data": hexutil.Bytes(log.Data),
911+
})
912+
}
913+
}
914+
return resultLogs, nil
915+
}
916+
917+
// gets minimal block parameters
918+
// for 0x2's bots- you might or might not find this useful, but doesnt really hurt to make it public lol
919+
func (s *PublicBlockChainAPI) GetMinimalBlockByNumber(ctx context.Context, number rpc.BlockNumber) (map[string]interface{}, error) {
920+
block, err := s.b.BlockByNumber(ctx, number)
921+
if err != nil {
922+
return nil, err
923+
}
924+
return RPCMarshalMinimalBlock(block), nil
925+
}
926+
927+
// gets minimal block data along with their logs (this function is used to get absolute minimal data out of the node)
928+
// also for 0x2's bots- might or might not be useful to you
929+
func (s *PublicBlockChainAPI) GetBatchBlockDataByNumbers(ctx context.Context, blockNrs []rpc.BlockNumber) (map[string]interface{}, error) {
930+
resultFields := map[string]interface{}{}
931+
for _, blockNr := range blockNrs {
932+
block, err := s.b.BlockByNumber(ctx, blockNr)
933+
if err != nil {
934+
return nil, err
935+
}
936+
blockData := RPCMarshalMinimalBlock(block)
937+
blockData["logs"] = []map[string]interface{}{}
938+
receipts, err := s.b.GetReceipts(ctx, block.Hash())
939+
if err != nil {
940+
return nil, err
941+
}
942+
for _, receipt := range receipts { // fill in compact receipt data
943+
for _, log := range receipt.Logs {
944+
blockData["logs"] = append(blockData["logs"].([]map[string]interface{}), map[string]interface{}{
945+
"address": log.Address,
946+
"topics": log.Topics,
947+
"data": hexutil.Bytes(log.Data),
948+
})
949+
}
950+
}
951+
resultFields[block.Number().String()] = blockData
952+
}
953+
return resultFields, nil
954+
}
955+
899956
// OverrideAccount indicates the overriding fields of account during the execution
900957
// of a message call.
901958
// Note, state and stateDiff can't be specified at the same time. If state is
@@ -1346,6 +1403,17 @@ func RPCMarshalBlock(block *types.Block, inclTx bool, fullTx bool, config *param
13461403
return fields, nil
13471404
}
13481405

1406+
// RPCMatshalMinimalBlock converts the given block into JSONRPC output, but only returning select fields.
1407+
// for use with mevgeth++ custom rpc functions for efficient block data fetching
1408+
func RPCMarshalMinimalBlock(block *types.Block) map[string]interface{} {
1409+
return map[string]interface{}{
1410+
"number": block.Number(),
1411+
"hash": block.Hash(),
1412+
"parentHash": block.ParentHash(),
1413+
"difficulty": block.Difficulty(),
1414+
}
1415+
}
1416+
13491417
// rpcMarshalHeader uses the generalized output filler, then adds the total difficulty field, which requires
13501418
// a `PublicBlockchainAPI`.
13511419
func (s *PublicBlockChainAPI) rpcMarshalHeader(ctx context.Context, header *types.Header) map[string]interface{} {
@@ -2399,7 +2467,7 @@ type CallBundleArgs struct {
23992467
BaseFee *big.Int `json:"baseFee"`
24002468
SimulationLogs bool `json:"simulationLogs"`
24012469
CreateAccessList bool `json:"createAccessList"`
2402-
StateOverrides *StateOverride `json:"stateOverrides"`
2470+
StateOverrides *StateOverride `json:"stateOverrides"`
24032471
}
24042472

24052473
// CallBundle will simulate a bundle of transactions at the top of a given block

internal/web3ext/web3ext.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,24 @@ web3._extend({
571571
params: 1,
572572
inputFormatter: [null]
573573
}),
574+
new web3._extend.Method({
575+
name: 'getMinimalBlockLogs',
576+
call: 'eth_getMinimalBlockLogs',
577+
params: 1,
578+
inputFormatter: [null]
579+
}),
580+
new web3._extend.Method({
581+
name: 'getMinimalBlockByNumber',
582+
call: 'eth_getMinimalBlockByNumber',
583+
params: 1,
584+
inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter]
585+
}),
586+
new web3._extend.Method({
587+
name: 'getBatchBlockDataByNumbers',
588+
call: 'eth_getBatchBlockDataByNumbers',
589+
params: 1,
590+
inputFormatter: [function (val) { return val.map(web3._extend.formatters.inputBlockNumberFormatter); }]
591+
}),
574592
new web3._extend.Method({
575593
name: 'getRawTransaction',
576594
call: 'eth_getRawTransactionByHash',

0 commit comments

Comments
 (0)