Skip to content

Commit 70e1e65

Browse files
authored
internal/ethapi: rename debug getters to match spec (#25176)
Some small fixes to get the existing debug methods to conform to the spec. Mainly dropping the encoding information from the method name as it should be deduced from the debug context and allowing the method to be invoked by either block number or block hash. It also adds the method debug_getTransaction which returns the raw tx bytes by tx hash. This is pretty much equivalent to the eth_getRawTransactionByHash method.
1 parent 6c40aed commit 70e1e65

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

internal/ethapi/api.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,25 +1888,45 @@ func NewDebugAPI(b Backend) *DebugAPI {
18881888
return &DebugAPI{b: b}
18891889
}
18901890

1891-
// GetHeaderRlp retrieves the RLP encoded for of a single header.
1892-
func (api *DebugAPI) GetHeaderRlp(ctx context.Context, number uint64) (hexutil.Bytes, error) {
1893-
header, _ := api.b.HeaderByNumber(ctx, rpc.BlockNumber(number))
1891+
// GetRawHeader retrieves the RLP encoding for a single header.
1892+
func (api *DebugAPI) GetRawHeader(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
1893+
var hash common.Hash
1894+
if h, ok := blockNrOrHash.Hash(); ok {
1895+
hash = h
1896+
} else {
1897+
block, err := api.b.BlockByNumberOrHash(ctx, blockNrOrHash)
1898+
if err != nil {
1899+
return nil, err
1900+
}
1901+
hash = block.Hash()
1902+
}
1903+
header, _ := api.b.HeaderByHash(ctx, hash)
18941904
if header == nil {
1895-
return nil, fmt.Errorf("header #%d not found", number)
1905+
return nil, fmt.Errorf("header #%d not found", hash)
18961906
}
18971907
return rlp.EncodeToBytes(header)
18981908
}
18991909

1900-
// GetBlockRlp retrieves the RLP encoded for of a single block.
1901-
func (api *DebugAPI) GetBlockRlp(ctx context.Context, number uint64) (hexutil.Bytes, error) {
1902-
block, _ := api.b.BlockByNumber(ctx, rpc.BlockNumber(number))
1910+
// GetRawBlock retrieves the RLP encoded for a single block.
1911+
func (api *DebugAPI) GetRawBlock(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
1912+
var hash common.Hash
1913+
if h, ok := blockNrOrHash.Hash(); ok {
1914+
hash = h
1915+
} else {
1916+
block, err := api.b.BlockByNumberOrHash(ctx, blockNrOrHash)
1917+
if err != nil {
1918+
return nil, err
1919+
}
1920+
hash = block.Hash()
1921+
}
1922+
block, _ := api.b.BlockByHash(ctx, hash)
19031923
if block == nil {
1904-
return nil, fmt.Errorf("block #%d not found", number)
1924+
return nil, fmt.Errorf("block #%d not found", hash)
19051925
}
19061926
return rlp.EncodeToBytes(block)
19071927
}
19081928

1909-
// GetRawReceipts retrieves the binary-encoded raw receipts of a single block.
1929+
// GetRawReceipts retrieves the binary-encoded receipts of a single block.
19101930
func (api *DebugAPI) GetRawReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]hexutil.Bytes, error) {
19111931
var hash common.Hash
19121932
if h, ok := blockNrOrHash.Hash(); ok {
@@ -1933,6 +1953,22 @@ func (api *DebugAPI) GetRawReceipts(ctx context.Context, blockNrOrHash rpc.Block
19331953
return result, nil
19341954
}
19351955

1956+
// GetRawTransaction returns the bytes of the transaction for the given hash.
1957+
func (s *DebugAPI) GetRawTransaction(ctx context.Context, hash common.Hash) (hexutil.Bytes, error) {
1958+
// Retrieve a finalized transaction, or a pooled otherwise
1959+
tx, _, _, _, err := s.b.GetTransaction(ctx, hash)
1960+
if err != nil {
1961+
return nil, err
1962+
}
1963+
if tx == nil {
1964+
if tx = s.b.GetPoolTransaction(hash); tx == nil {
1965+
// Transaction not found anywhere, abort
1966+
return nil, nil
1967+
}
1968+
}
1969+
return tx.MarshalBinary()
1970+
}
1971+
19361972
// PrintBlock retrieves a block and returns its pretty printed form.
19371973
func (api *DebugAPI) PrintBlock(ctx context.Context, number uint64) (string, error) {
19381974
block, _ := api.b.BlockByNumber(ctx, rpc.BlockNumber(number))

internal/web3ext/web3ext.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,25 @@ web3._extend({
224224
outputFormatter: console.log
225225
}),
226226
new web3._extend.Method({
227-
name: 'getHeaderRlp',
228-
call: 'debug_getHeaderRlp',
227+
name: 'getRawHeader',
228+
call: 'debug_getRawHeader',
229229
params: 1
230230
}),
231231
new web3._extend.Method({
232-
name: 'getBlockRlp',
233-
call: 'debug_getBlockRlp',
232+
name: 'getRawBlock',
233+
call: 'debug_getRawBlock',
234234
params: 1
235235
}),
236236
new web3._extend.Method({
237237
name: 'getRawReceipts',
238238
call: 'debug_getRawReceipts',
239239
params: 1
240240
}),
241+
new web3._extend.Method({
242+
name: 'getRawTransaction',
243+
call: 'debug_getRawTransaction',
244+
params: 1
245+
}),
241246
new web3._extend.Method({
242247
name: 'setHead',
243248
call: 'debug_setHead',

0 commit comments

Comments
 (0)