-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathotterscan_api.go
More file actions
429 lines (371 loc) · 14 KB
/
otterscan_api.go
File metadata and controls
429 lines (371 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.
package jsonrpc
import (
"context"
"errors"
"fmt"
"math/big"
"github.com/holiman/uint256"
"github.com/erigontech/erigon/common"
"github.com/erigontech/erigon/common/hexutil"
"github.com/erigontech/erigon/db/kv"
"github.com/erigontech/erigon/execution/chain"
"github.com/erigontech/erigon/execution/protocol"
"github.com/erigontech/erigon/execution/protocol/rules"
"github.com/erigontech/erigon/execution/tracing/tracers"
"github.com/erigontech/erigon/execution/types"
"github.com/erigontech/erigon/execution/types/accounts"
"github.com/erigontech/erigon/execution/types/ethutils"
"github.com/erigontech/erigon/execution/vm"
"github.com/erigontech/erigon/execution/vm/evmtypes"
"github.com/erigontech/erigon/rpc"
"github.com/erigontech/erigon/rpc/ethapi"
"github.com/erigontech/erigon/rpc/rpchelper"
"github.com/erigontech/erigon/rpc/transactions"
)
// API_LEVEL Must be incremented every time new additions are made
const API_LEVEL = 8
type TransactionsWithReceipts struct {
Txs []*ethapi.RPCTransaction `json:"txs"`
Receipts []map[string]any `json:"receipts"`
FirstPage bool `json:"firstPage"`
LastPage bool `json:"lastPage"`
}
type OtterscanAPI interface {
GetApiLevel() uint8
GetInternalOperations(ctx context.Context, hash common.Hash) ([]*InternalOperation, error)
SearchTransactionsBefore(ctx context.Context, addr common.Address, blockNum uint64, pageSize uint16) (*TransactionsWithReceipts, error)
SearchTransactionsAfter(ctx context.Context, addr common.Address, blockNum uint64, pageSize uint16) (*TransactionsWithReceipts, error)
GetBlockDetails(ctx context.Context, number rpc.BlockNumber) (map[string]any, error)
GetBlockDetailsByHash(ctx context.Context, hash common.Hash) (map[string]any, error)
GetBlockTransactions(ctx context.Context, number rpc.BlockNumber, pageNumber uint8, pageSize uint8) (map[string]any, error)
HasCode(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (bool, error)
TraceTransaction(ctx context.Context, hash common.Hash) ([]*TraceEntry, error)
GetTransactionError(ctx context.Context, hash common.Hash) (hexutil.Bytes, error)
GetTransactionBySenderAndNonce(ctx context.Context, addr common.Address, nonce uint64) (*common.Hash, error)
GetContractCreator(ctx context.Context, addr common.Address) (*ContractCreatorData, error)
}
type OtterscanAPIImpl struct {
*BaseAPI
db kv.TemporalRoDB
maxPageSize uint64
}
func NewOtterscanAPI(base *BaseAPI, db kv.TemporalRoDB, maxPageSize uint64) *OtterscanAPIImpl {
return &OtterscanAPIImpl{
BaseAPI: base,
db: db,
maxPageSize: maxPageSize,
}
}
func (api *OtterscanAPIImpl) GetApiLevel() uint8 {
return API_LEVEL
}
// TODO: dedup from eth_txs.go#GetTransactionByHash
func (api *OtterscanAPIImpl) getTransactionByHash(ctx context.Context, tx kv.Tx, hash common.Hash) (types.Transaction, *types.Block, common.Hash, uint64, uint64, error) {
// https://www.quicknode.com/docs/ethereum/eth_getTransactionByHash
blockNum, _, ok, err := api.txnLookup(ctx, tx, hash)
if err != nil {
return nil, nil, common.Hash{}, 0, 0, err
}
if !ok {
return nil, nil, common.Hash{}, 0, 0, nil
}
err = api.BaseAPI.checkPruneHistory(ctx, tx, blockNum)
if err != nil {
return nil, nil, common.Hash{}, 0, 0, err
}
block, err := api.blockByNumberWithSenders(ctx, tx, blockNum)
if err != nil {
return nil, nil, common.Hash{}, 0, 0, err
}
if block == nil {
return nil, nil, common.Hash{}, 0, 0, fmt.Errorf("block not found: %d", blockNum)
}
blockHash := block.Hash()
var txnIndex uint64
var txn types.Transaction
for i, transaction := range block.Transactions() {
if transaction.Hash() == hash {
txn = transaction
txnIndex = uint64(i)
break
}
}
// Add GasPrice for the DynamicFeeTransaction
// var baseFee *big.Int
// if chainConfig.IsLondon(blockNum) && blockHash != (common.Hash{}) {
// baseFee = block.BaseFee()
// }
// if no transaction was found then we return nil
if txn == nil {
return nil, nil, common.Hash{}, 0, 0, nil
}
return txn, block, blockHash, blockNum, txnIndex, nil
}
func (api *OtterscanAPIImpl) runTracer(ctx context.Context, tx kv.TemporalTx, hash common.Hash, tracer *tracers.Tracer) (*evmtypes.ExecutionResult, error) {
txn, block, _, _, txIndex, err := api.getTransactionByHash(ctx, tx, hash)
if err != nil {
return nil, err
}
if txn == nil {
return nil, fmt.Errorf("transaction %#x not found", hash)
}
chainConfig, err := api.chainConfig(ctx, tx)
if err != nil {
return nil, err
}
engine := api.engine()
ibs, blockCtx, _, rules, signer, err := transactions.ComputeBlockContext(ctx, engine, block.HeaderNoCopy(), chainConfig, api._blockReader, api.stateCache, api._txNumReader, tx, int(txIndex))
if err != nil {
return nil, err
}
msg, txCtx, err := transactions.ComputeTxContext(ibs, engine, rules, signer, block, chainConfig, int(txIndex))
if err != nil {
return nil, err
}
if tracer != nil {
ibs.SetHooks(tracer.Hooks)
}
var vmConfig vm.Config
if tracer == nil {
vmConfig = vm.Config{}
} else {
vmConfig = vm.Config{Tracer: tracer.Hooks}
}
vmenv := vm.NewEVM(blockCtx, txCtx, ibs, chainConfig, vmConfig)
if tracer != nil && tracer.Hooks.OnTxStart != nil {
tracer.Hooks.OnTxStart(vmenv.GetVMContext(), txn, msg.From())
}
result, err := protocol.ApplyMessage(vmenv, msg, new(protocol.GasPool).AddGas(msg.Gas()).AddBlobGas(msg.BlobGas()), true, false /* gasBailout */, engine)
if err != nil {
if tracer != nil && tracer.Hooks.OnTxEnd != nil {
tracer.Hooks.OnTxEnd(nil, err)
}
return nil, fmt.Errorf("tracing failed: %v", err)
}
if tracer != nil && tracer.Hooks.OnTxEnd != nil {
tracer.Hooks.OnTxEnd(&types.Receipt{GasUsed: result.ReceiptGasUsed}, nil)
}
return result, nil
}
func (api *OtterscanAPIImpl) GetInternalOperations(ctx context.Context, hash common.Hash) ([]*InternalOperation, error) {
tx, err := api.db.BeginTemporalRo(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
tracer := NewOperationsTracer(ctx)
if _, err := api.runTracer(ctx, tx, hash, tracer.Tracer()); err != nil {
return nil, err
}
return tracer.Results, nil
}
// Search transactions that touch a certain address.
//
// It searches back a certain block (excluding); the results are sorted descending.
//
// The pageSize indicates how many txs may be returned. If there are less txs than pageSize,
// they are just returned. But it may return a little more than pageSize if there are more txs
// than the necessary to fill pageSize in the last found block, i.e., let's say you want pageSize == 25,
// you already found 24 txs, the next block contains 4 matches, then this function will return 28 txs.
func (api *OtterscanAPIImpl) SearchTransactionsBefore(ctx context.Context, addr common.Address, blockNum uint64, pageSize uint16) (*TransactionsWithReceipts, error) {
if uint64(pageSize) > api.maxPageSize {
return nil, fmt.Errorf("max allowed page size: %v", api.maxPageSize)
}
dbtx, err := api.db.BeginTemporalRo(ctx)
if err != nil {
return nil, err
}
defer dbtx.Rollback()
err = api.BaseAPI.checkPruneHistory(ctx, dbtx, blockNum)
if err != nil {
return nil, err
}
return api.searchTransactionsBeforeV3(dbtx, ctx, addr, blockNum, pageSize)
}
// Search transactions that touch a certain address.
//
// It searches forward a certain block (excluding); the results are sorted descending.
//
// The pageSize indicates how many txs may be returned. If there are less txs than pageSize,
// they are just returned. But it may return a little more than pageSize if there are more txs
// than the necessary to fill pageSize in the last found block, i.e., let's say you want pageSize == 25,
// you already found 24 txs, the next block contains 4 matches, then this function will return 28 txs.
func (api *OtterscanAPIImpl) SearchTransactionsAfter(ctx context.Context, addr common.Address, blockNum uint64, pageSize uint16) (*TransactionsWithReceipts, error) {
if uint64(pageSize) > api.maxPageSize {
return nil, fmt.Errorf("max allowed page size: %v", api.maxPageSize)
}
dbtx, err := api.db.BeginTemporalRo(ctx)
if err != nil {
return nil, err
}
defer dbtx.Rollback()
err = api.BaseAPI.checkPruneHistory(ctx, dbtx, blockNum)
if err != nil {
return nil, err
}
return api.searchTransactionsAfterV3(dbtx, ctx, addr, blockNum, pageSize)
}
func delegateGetBlockByNumber(tx kv.Tx, b *types.Block, number rpc.BlockNumber, inclTx bool) (map[string]any, error) {
response, err := ethapi.RPCMarshalBlock(b, inclTx, inclTx, nil)
if !inclTx {
delete(response, "transactions") // workaround for https://github.com/erigontech/erigon/issues/4989#issuecomment-1218415666
}
response["transactionCount"] = b.Transactions().Len()
if err == nil && number == rpc.PendingBlockNumber {
// Pending blocks need to nil out a few fields
for _, field := range []string{"hash", "nonce", "miner"} {
response[field] = nil
}
}
// Explicitly drop unwanted fields
response["logsBloom"] = nil
return response, err
}
// TODO: temporary workaround due to API breakage from watch_the_burn
type internalIssuance struct {
BlockReward string `json:"blockReward,omitempty"`
UncleReward string `json:"uncleReward,omitempty"`
Issuance string `json:"issuance,omitempty"`
}
func delegateIssuance(tx kv.Tx, block *types.Block, chainConfig *chain.Config, engine rules.EngineReader) (internalIssuance, error) {
// TODO: aura seems to be already broken in the original version of this RPC method
rewards, err := engine.CalculateRewards(chainConfig, block.HeaderNoCopy(), block.Uncles(), func(contract accounts.Address, data []byte) ([]byte, error) {
return nil, nil
})
if err != nil {
return internalIssuance{}, err
}
blockReward := uint256.NewInt(0)
uncleReward := uint256.NewInt(0)
for _, r := range rewards {
if r.Kind == rules.RewardAuthor {
blockReward.Add(blockReward, &r.Amount)
}
if r.Kind == rules.RewardUncle {
uncleReward.Add(uncleReward, &r.Amount)
}
}
var ret internalIssuance
ret.BlockReward = hexutil.EncodeBig(blockReward.ToBig())
ret.UncleReward = hexutil.EncodeBig(uncleReward.ToBig())
blockReward.Add(blockReward, uncleReward)
ret.Issuance = hexutil.EncodeBig(blockReward.ToBig())
return ret, nil
}
func delegateBlockFees(ctx context.Context, tx kv.Tx, block *types.Block, senders []common.Address, chainConfig *chain.Config, receipts types.Receipts) (*big.Int, error) {
var fee, gasUsed, totalFees uint256.Int
isLondon := chainConfig.IsLondon(block.NumberU64())
baseFee := block.BaseFee()
for _, receipt := range receipts {
txn := block.Transactions()[receipt.TransactionIndex]
if !isLondon {
fee.Set(txn.GetTipCap())
} else {
effectiveTip := txn.GetEffectiveGasTip(baseFee)
fee.Add(baseFee, &effectiveTip)
}
gasUsed.SetUint64(receipt.GasUsed)
fee.Mul(&fee, &gasUsed)
totalFees.Add(&totalFees, &fee)
}
return totalFees.ToBig(), nil
}
func (api *OtterscanAPIImpl) getBlockWithSenders(ctx context.Context, number rpc.BlockNumber, tx kv.Tx) (*types.Block, []common.Address, error) {
if number == rpc.PendingBlockNumber {
return api.pendingBlock(), nil, nil
}
n, hash, _, err := rpchelper.GetBlockNumber(ctx, rpc.BlockNumberOrHashWithNumber(number), tx, api._blockReader, api.filters)
if err != nil {
if errors.As(err, &rpc.BlockNotFoundErr{}) {
return nil, nil, nil // not error, see also other cases https://github.com/erigontech/erigon/issues/1645
}
return nil, nil, err
}
block, err := api.blockWithSenders(ctx, tx, hash, n)
if err != nil {
return nil, nil, err
}
if block == nil {
return nil, nil, nil
}
return block, block.Body().SendersFromTxs(), nil
}
func (api *OtterscanAPIImpl) GetBlockTransactions(ctx context.Context, number rpc.BlockNumber, pageNumber uint8, pageSize uint8) (map[string]any, error) {
tx, err := api.db.BeginTemporalRo(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
err = api.BaseAPI.checkPruneHistory(ctx, tx, number.Uint64())
if err != nil {
return nil, err
}
b, _, err := api.getBlockWithSenders(ctx, number, tx)
if err != nil {
return nil, err
}
if b == nil {
return nil, nil
}
chainConfig, err := api.chainConfig(ctx, tx)
if err != nil {
return nil, err
}
getBlockRes, err := delegateGetBlockByNumber(tx, b, number, true)
if err != nil {
return nil, err
}
// Receipts
receipts, err := api.getReceipts(ctx, tx, b)
if err != nil {
return nil, err
}
result := make([]map[string]any, 0, len(receipts))
for _, receipt := range receipts {
txn := b.Transactions()[receipt.TransactionIndex]
marshalledRcpt := ethutils.MarshalReceipt(receipt, txn, chainConfig, b.HeaderNoCopy(), txn.Hash(), true, false)
marshalledRcpt["logs"] = nil
marshalledRcpt["logsBloom"] = nil
result = append(result, marshalledRcpt)
}
// Crop txn input to 4bytes
var txs = getBlockRes["transactions"].([]any)
for _, rawTx := range txs {
rpcTx := rawTx.(*ethapi.RPCTransaction)
if len(rpcTx.Input) >= 4 {
rpcTx.Input = rpcTx.Input[:4]
}
}
// Crop page
pageEnd := b.Transactions().Len() - int(pageNumber)*int(pageSize)
pageStart := pageEnd - int(pageSize)
if pageEnd < 0 {
pageEnd = 0
}
if pageStart < 0 {
pageStart = 0
}
if pageEnd > len(result) {
return nil, fmt.Errorf("receipts count mismatch: got %d, need %d", len(result), pageEnd)
}
response := map[string]any{}
getBlockRes["transactions"] = getBlockRes["transactions"].([]any)[pageStart:pageEnd]
response["fullblock"] = getBlockRes
response["receipts"] = result[pageStart:pageEnd]
return response, nil
}