-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.go
493 lines (453 loc) · 15.6 KB
/
utils.go
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package indexer
import (
"context"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"math/big"
"github.com/fxamacker/cbor/v2"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/oasisprotocol/oasis-core/go/roothash/api/block"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/core"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/evm"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
"github.com/oasisprotocol/emerald-web3-gateway/db/model"
"github.com/oasisprotocol/emerald-web3-gateway/filters"
"github.com/oasisprotocol/emerald-web3-gateway/storage"
)
var defaultValidatorAddr = "0x0000000000000000000000000000000088888888"
// Log is the Oasis Log.
type Log struct {
Address common.Address `json:"address"`
Topics []common.Hash `json:"topics"`
Data []byte `json:"data"`
}
// Logs2EthLogs converts logs in db to ethereum logs.
func Logs2EthLogs(logs []*Log, round uint64, blockHash, txHash common.Hash, startIndex uint, txIndex uint32) []*ethtypes.Log {
ethLogs := []*ethtypes.Log{}
for i, log := range logs {
ethLog := ðtypes.Log{
Address: log.Address,
Topics: log.Topics,
Data: log.Data,
BlockNumber: round,
TxHash: txHash,
TxIndex: uint(txIndex),
BlockHash: blockHash,
Index: startIndex + uint(i),
Removed: false,
}
ethLogs = append(ethLogs, ethLog)
}
return ethLogs
}
// ethToModelLogs converts ethereum logs to DB model logs.
func ethToModelLogs(ethLogs []*ethtypes.Log) ([]*model.Log, map[string][]*model.Log) {
res := make([]*model.Log, 0, len(ethLogs))
txLogs := make(map[string][]*model.Log)
for _, log := range ethLogs {
topics := make([]string, 0, len(log.Topics))
for _, tp := range log.Topics {
topics = append(topics, tp.Hex())
}
txHash := log.TxHash.Hex()
log := &model.Log{
Address: log.Address.Hex(),
Topics: topics,
Data: hex.EncodeToString(log.Data),
Round: log.BlockNumber,
BlockHash: log.BlockHash.Hex(),
TxHash: txHash,
TxIndex: log.TxIndex,
Index: log.Index,
Removed: log.Removed,
}
res = append(res, log)
txLogs[txHash] = append(txLogs[txHash], log)
}
return res, txLogs
}
// blockToModels converts block data to DB model objects.
func blockToModels(
block *block.Block,
transactions ethtypes.Transactions,
logs []*ethtypes.Log,
txsStatus []uint8,
txsGasUsed []uint64,
results []types.CallResult,
blockGasLimit uint64,
) (*model.Block, []*model.Transaction, []*model.Receipt, []*model.Log, error) {
dbLogs, txLogs := ethToModelLogs(logs)
encoded := block.Header.EncodedHash()
bhash := common.HexToHash(encoded.Hex()).String()
bprehash := block.Header.PreviousHash.Hex()
bshash := block.Header.StateRoot.Hex()
logsBloom := ethtypes.BytesToBloom(ethtypes.LogsBloom(logs))
bloomData, _ := logsBloom.MarshalText()
baseFee := big.NewInt(10)
number := big.NewInt(0)
number.SetUint64(block.Header.Round)
var btxHash string
if len(transactions) == 0 {
btxHash = ethtypes.EmptyRootHash.Hex()
} else {
btxHash = block.Header.IORoot.Hex()
}
innerHeader := &model.Header{
ParentHash: bprehash,
UncleHash: ethtypes.EmptyUncleHash.Hex(),
Coinbase: common.HexToAddress(defaultValidatorAddr).Hex(),
Root: bshash,
TxHash: btxHash,
ReceiptHash: ethtypes.EmptyRootHash.Hex(),
Bloom: string(bloomData),
Difficulty: big.NewInt(0).String(),
Number: number.Uint64(),
GasLimit: blockGasLimit,
Time: uint64(block.Header.Timestamp),
Extra: "",
MixDigest: "",
Nonce: ethtypes.BlockNonce{}.Uint64(),
BaseFee: baseFee.String(),
}
innerTxs := make([]*model.Transaction, 0, len(transactions))
innerReceipts := make([]*model.Receipt, 0, len(transactions))
cumulativeGasUsed := uint64(0)
for idx, ethTx := range transactions {
v, r, s := ethTx.RawSignatureValues()
signer := ethtypes.LatestSignerForChainID(ethTx.ChainId())
from, _ := signer.Sender(ethTx)
ethAccList := ethTx.AccessList()
accList := make([]model.AccessTuple, 0, len(ethAccList))
for _, ethAcc := range ethAccList {
keys := []string{}
for _, ethStorageKey := range ethAcc.StorageKeys {
keys = append(keys, ethStorageKey.Hex())
}
acc := model.AccessTuple{
Address: ethAcc.Address.Hex(),
StorageKeys: keys,
}
accList = append(accList, acc)
}
tx := &model.Transaction{
Hash: ethTx.Hash().Hex(),
Type: ethTx.Type(),
ChainID: ethTx.ChainId().String(),
Status: uint(txsStatus[idx]),
BlockHash: bhash,
Round: number.Uint64(),
Index: uint32(idx),
Gas: ethTx.Gas(),
GasPrice: ethTx.GasPrice().String(),
GasTipCap: ethTx.GasTipCap().String(),
GasFeeCap: ethTx.GasFeeCap().String(),
Nonce: ethTx.Nonce(),
FromAddr: from.Hex(),
Value: ethTx.Value().String(),
Data: hex.EncodeToString(ethTx.Data()),
AccessList: accList,
V: v.String(),
R: r.String(),
S: s.String(),
}
to := ethTx.To()
if to == nil {
tx.ToAddr = ""
} else {
tx.ToAddr = to.Hex()
}
innerTxs = append(innerTxs, tx)
// receipts
cumulativeGasUsed += txsGasUsed[idx]
receipt := &model.Receipt{
Status: uint(txsStatus[idx]),
CumulativeGasUsed: cumulativeGasUsed,
Logs: txLogs[ethTx.Hash().Hex()],
LogsBloom: hex.EncodeToString(bloomData),
TransactionHash: ethTx.Hash().Hex(),
BlockHash: bhash,
GasUsed: txsGasUsed[idx],
Type: uint(ethTx.Type()),
Round: block.Header.Round,
TransactionIndex: uint64(idx),
FromAddr: tx.FromAddr,
ToAddr: tx.ToAddr,
}
if len(receipt.Logs) == 0 {
receipt.Logs = []*model.Log{}
}
if tx.ToAddr == "" && results[idx].IsSuccess() {
var out []byte
if err := cbor.Unmarshal(results[idx].Ok, &out); err != nil {
return nil, nil, nil, nil, err
}
receipt.ContractAddress = common.BytesToAddress(out).Hex()
}
innerReceipts = append(innerReceipts, receipt)
}
innerHeader.GasUsed = cumulativeGasUsed
innerBlock := &model.Block{
Hash: bhash,
Round: number.Uint64(),
Header: innerHeader,
Transactions: innerTxs,
}
return innerBlock, innerTxs, innerReceipts, dbLogs, nil
}
// StoreBlockData parses oasis block and stores in db.
func (ib *indexBackend) StoreBlockData(ctx context.Context, oasisBlock *block.Block, txResults []*client.TransactionWithResults, blockGasLimit uint64) error { // nolint: gocyclo
encoded := oasisBlock.Header.EncodedHash()
bhash := common.HexToHash(encoded.Hex())
blockNum := oasisBlock.Header.Round
ethTxs := ethtypes.Transactions{}
logs := []*ethtypes.Log{}
txsStatus := []uint8{}
txsGasUsed := []uint64{}
results := []types.CallResult{}
// Decode tx and get logs
for txIndex, item := range txResults {
oasisTx := item.Tx
if len(oasisTx.AuthProofs) != 1 || oasisTx.AuthProofs[0].Module != "evm.ethereum.v0" {
// Skip non-Ethereum transactions.
continue
}
// Decode the Ethereum transaction.
ethTx := ðtypes.Transaction{}
err := rlp.DecodeBytes(oasisTx.Body, ethTx)
if err != nil {
ib.logger.Error("Failed to decode UnverifiedTransaction", "height", blockNum, "index", txIndex, "err", err)
return err
}
var status uint8
if txResults[txIndex].Result.IsSuccess() {
status = uint8(ethtypes.ReceiptStatusSuccessful)
} else {
status = uint8(ethtypes.ReceiptStatusFailed)
}
txsStatus = append(txsStatus, status)
results = append(results, item.Result)
ethTxs = append(ethTxs, ethTx)
var txGasUsed uint64
var oasisLogs []*Log
resEvents := item.Events
for eventIndex, event := range resEvents {
switch {
case event.Module == evm.ModuleName && event.Code == 1:
// Manually parse EVM logs to also handle the legacy format which is not supported by the sdk.
var logs []*Log
if err = cbor.Unmarshal(event.Value, &logs); err != nil {
ib.logger.Debug("Failed to unmarshal event value, trying legacy format next", "index", eventIndex, "err", err)
// Emerald events value format changed in https://github.com/oasisprotocol/oasis-sdk/pull/675.
// Try the legacy format in case the new format unmarshalling failed.
var log Log
if lerr := cbor.Unmarshal(event.Value, &log); lerr != nil {
ib.logger.Error("Failed to unmarshal event value", "index", eventIndex, "legacy_err", lerr, "err", err)
// The legacy fallback failed, likely the event not in legacy format, return the original unmarshal error.
return err
}
oasisLogs = append(oasisLogs, &log)
continue
}
oasisLogs = append(oasisLogs, logs...)
case event.Module == core.ModuleName:
events, err := core.DecodeEvent(event)
if err != nil {
ib.logger.Error("failed to decode core event", "index", eventIndex, "err", err)
return err
}
for _, ev := range events {
ce, ok := ev.(*core.Event)
if !ok {
ib.logger.Error("invalid core event", "event", ev, "index", eventIndex)
return fmt.Errorf("invalid core event: %v", ev)
}
if ce.GasUsed == nil {
continue
}
// Shouldn't happen unless something is terribly wrong.
if txGasUsed != 0 {
ib.logger.Error("multiple gas used events for a transaction", "prev_used", txGasUsed, "gas_used", ce, "index", eventIndex)
return fmt.Errorf("multiple GasUsedEvent for a transaction")
}
txGasUsed = ce.GasUsed.Amount
}
default:
// Ignore any other events.
}
}
logs = append(logs, Logs2EthLogs(oasisLogs, blockNum, bhash, ethTx.Hash(), uint(len(logs)), uint32(txIndex))...)
// Emerald GasUsed events were added in version 7.0.0.
// Default to using gas limit, which was the behaviour before.
if txGasUsed == 0 {
ib.logger.Debug("no GasUsedEvent for a transaction, defaulting to gas limit", "transaction_index", txIndex, "round", oasisBlock.Header.Round)
txGasUsed = ethTx.Gas()
}
txsGasUsed = append(txsGasUsed, txGasUsed)
}
// Convert to DB models.
blk, txs, receipts, dbLogs, err := blockToModels(oasisBlock, ethTxs, logs, txsStatus, txsGasUsed, results, blockGasLimit)
if err != nil {
ib.logger.Debug("Failed to ConvertToEthBlock", "height", blockNum, "err", err)
return err
}
// Store indexed models.
upsertedTxes := make([]*model.Transaction, 0, len(txs))
upsertedReceipts := make([]*model.Receipt, 0, len(receipts))
if err = ib.storage.RunInTransaction(ctx, func(s storage.Storage) error {
// Store txs.
for i, tx := range txs {
switch tx.Status {
case uint(ethtypes.ReceiptStatusFailed):
// In the Emerald Paratime it can happen that an already committed
// transaction is re-proposed at a later block. The duplicate transaction fails,
// but it is still included in the block. The gateway should drop these
// transactions to remain compatible with ETH semantics.
if err = s.InsertIfNotExists(ctx, tx); err != nil {
return err
}
default:
earlierTx, earlierTxErr := s.GetTransaction(ctx, tx.Hash)
if earlierTxErr != nil {
if !errors.Is(earlierTxErr, sql.ErrNoRows) {
return err
}
// First encounter of this tx, continue to upsert.
} else {
if earlierTx.Status != uint(ethtypes.ReceiptStatusFailed) {
ib.logger.Error("duplicate tx",
"earlier_tx", earlierTx,
"tx", tx,
)
return fmt.Errorf("duplicate tx hash %s in rounds %d and %d", tx.Hash, earlierTx.Round, tx.Round)
}
// Replacing a failed encounter of this tx, continue to upsert.
}
if upsertErr := s.Upsert(ctx, tx); upsertErr != nil {
return upsertErr
}
upsertedTxes = append(upsertedTxes, txs[i])
}
}
// Store receipts.
for i, receipt := range receipts {
switch receipt.Status {
case uint(ethtypes.ReceiptStatusFailed):
// In the Emerald Paratime it can happen that an already committed
// transaction is re-proposed at a later block. The duplicate transaction fails,
// but it is still included in the block. The gateway should drop these
// transactions to remain compatible with ETH semantics.
if err = s.InsertIfNotExists(ctx, receipt); err != nil {
return err
}
default:
earlierReceipt, earlierReceiptErr := s.GetTransactionReceipt(ctx, receipt.TransactionHash)
if earlierReceiptErr != nil {
if !errors.Is(earlierReceiptErr, sql.ErrNoRows) {
return err
}
// First encounter of this receipt, continue to upsert.
} else {
if earlierReceipt.Status != uint(ethtypes.ReceiptStatusFailed) {
ib.logger.Error("duplicate receipt",
"earlier_receipt", earlierReceipt,
"receipt", receipt,
)
return fmt.Errorf("duplicate receipt tx hash %s in rounds %d and %d", receipt.TransactionHash, earlierReceipt.Round, receipt.Round)
}
// Replacing a failed encounter of this receipt, continue to upsert.
}
if upsertErr := s.Upsert(ctx, receipt); upsertErr != nil {
return upsertErr
}
upsertedReceipts = append(upsertedReceipts, receipts[i])
}
}
// Store logs.
for _, log := range dbLogs {
if err = s.Insert(ctx, log); err != nil {
ib.logger.Error("Failed to store logs", "height", blockNum, "log", log, "err", err)
return err
}
}
// Store block.
if err = s.Insert(ctx, blk); err != nil {
return err
}
// Update last indexed round.
r := &model.IndexedRoundWithTip{
Tip: model.Continues,
Round: blk.Round,
}
return s.Upsert(ctx, r)
}); err != nil {
return err
}
// Send event to event system.
chainEvent := filters.ChainEvent{
Block: blk,
Hash: bhash,
Logs: dbLogs,
}
ib.subscribe.ChainChan() <- chainEvent
ib.logger.Debug("sent chain event to event system", "height", blockNum)
// Notify the observer if any.
if ib.observer != nil {
ib.observer.OnBlockIndexed(blk, upsertedTxes, upsertedReceipts)
}
return nil
}
// db2EthReceipt converts model.Receipt to the GetTransactipReceipt format.
func db2EthReceipt(dbReceipt *model.Receipt) map[string]interface{} {
ethLogs := make([]*ethtypes.Log, 0, len(dbReceipt.Logs))
for _, dbLog := range dbReceipt.Logs {
topics := make([]common.Hash, 0, len(dbLog.Topics))
for _, dbTopic := range dbLog.Topics {
tp := common.HexToHash(dbTopic)
topics = append(topics, tp)
}
data, _ := hex.DecodeString(dbLog.Data)
log := ðtypes.Log{
Address: common.HexToAddress(dbLog.Address),
Topics: topics,
Data: data,
BlockNumber: dbLog.Round,
TxHash: common.HexToHash(dbLog.TxHash),
TxIndex: dbLog.TxIndex,
BlockHash: common.HexToHash(dbLog.BlockHash),
Index: dbLog.Index,
Removed: dbLog.Removed,
}
ethLogs = append(ethLogs, log)
}
receipt := map[string]interface{}{
"status": hexutil.Uint(dbReceipt.Status),
"cumulativeGasUsed": hexutil.Uint64(dbReceipt.CumulativeGasUsed),
"logsBloom": ethtypes.BytesToBloom(ethtypes.LogsBloom(ethLogs)),
"logs": ethLogs,
"transactionHash": dbReceipt.TransactionHash,
"gasUsed": hexutil.Uint64(dbReceipt.GasUsed),
"type": hexutil.Uint64(dbReceipt.Type),
"blockHash": dbReceipt.BlockHash,
"blockNumber": hexutil.Uint64(dbReceipt.Round),
"transactionIndex": hexutil.Uint64(dbReceipt.TransactionIndex),
"from": nil,
"to": nil,
"contractAddress": nil,
}
if dbReceipt.FromAddr != "" {
receipt["from"] = dbReceipt.FromAddr
}
if dbReceipt.ToAddr != "" {
receipt["to"] = dbReceipt.ToAddr
}
if dbReceipt.ContractAddress != "" {
receipt["contractAddress"] = dbReceipt.ContractAddress
}
return receipt
}