Skip to content

Commit 79969a3

Browse files
committed
refat: added coinbase transaction/events in backfill script
1 parent a6865ff commit 79969a3

File tree

11 files changed

+216
-34
lines changed

11 files changed

+216
-34
lines changed

backfill/config/env.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,58 @@ func getEnvAsInt(key string) int {
7575
}
7676
return value
7777
}
78+
79+
func GetMinHeights(network string) map[int]int {
80+
minHeights := make(map[int]int)
81+
if network == "mainnet01" {
82+
minHeights = map[int]int{
83+
0: 0,
84+
1: 0,
85+
2: 0,
86+
3: 0,
87+
4: 0,
88+
5: 0,
89+
6: 0,
90+
7: 0,
91+
8: 0,
92+
9: 0,
93+
10: 852054,
94+
11: 852054,
95+
12: 852054,
96+
13: 852054,
97+
14: 852054,
98+
15: 852054,
99+
16: 852054,
100+
17: 852054,
101+
18: 852054,
102+
19: 852054,
103+
}
104+
}
105+
106+
if network == "testnet04" {
107+
minHeights = map[int]int{
108+
0: 0,
109+
1: 0,
110+
2: 0,
111+
3: 0,
112+
4: 0,
113+
5: 0,
114+
6: 0,
115+
7: 0,
116+
8: 0,
117+
9: 0,
118+
10: 332604,
119+
11: 332604,
120+
12: 332604,
121+
13: 332604,
122+
14: 332604,
123+
15: 332604,
124+
16: 332604,
125+
17: 332604,
126+
18: 332604,
127+
19: 332604,
128+
}
129+
}
130+
131+
return minHeights
132+
}

backfill/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func main() {
1919
go config.StartMemoryMonitoring()
2020
cut := fetch.FetchCut()
2121
ChainId := env.ChainId
22-
SyncMinHeight := env.SyncMinHeight
22+
minHeights := config.GetMinHeights(env.Network)
23+
SyncMinHeight := minHeights[ChainId]
2324
process.StartBackfill(cut.Height, cut.Hash, ChainId, SyncMinHeight, pool)
2425
}

backfill/middle-backfill/middle-backfill.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,5 @@ func FetchMaxHeightByChain(pool *pgxpool.Pool) ([]BlockHeight, error) {
6969
return nil, fmt.Errorf("error occurred during row iteration: %w", err)
7070
}
7171

72-
// const maxHeight = 5444446
73-
// var blockHeights = []BlockHeight{}
74-
75-
// for i := 0; i < 20; i++ {
76-
// blockHeights = append(blockHeights, BlockHeight{
77-
// ChainID: int64(i),
78-
// MaxHeight: maxHeight,
79-
// })
80-
// }
81-
8272
return results, nil
8373
}

backfill/process/coinbase.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package process
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"go-backfill/fetch"
7+
"go-backfill/repository"
8+
"strconv"
9+
)
10+
11+
type Coinbase struct {
12+
ReqKey string `json:"reqKey"`
13+
TxID int `json:"txId"`
14+
Events []fetch.Event `json:"events"`
15+
Result json.RawMessage `json:"result"`
16+
Logs string `json:"logs"`
17+
}
18+
19+
func decodeCoinbase(jsonStr string) (*Coinbase, error) {
20+
var coinbase Coinbase
21+
err := json.Unmarshal([]byte(jsonStr), &coinbase)
22+
if err != nil {
23+
return nil, err
24+
}
25+
return &coinbase, nil
26+
}
27+
28+
func processCoinbaseTransaction(coinbase string, blockId int64, creationTime int64, chainId int64) (repository.TransactionAttributes, error) {
29+
30+
coinbaseDecoded, err := decodeCoinbase(coinbase)
31+
if err != nil {
32+
return repository.TransactionAttributes{}, fmt.Errorf("decoding Coinbase JSON of block %d: %w", blockId, err)
33+
}
34+
35+
emptyJSON, _ := json.Marshal(map[string]interface{}{})
36+
emptyArray, _ := json.Marshal([]interface{}{})
37+
38+
txAttribute := repository.TransactionAttributes{
39+
BlockId: blockId,
40+
Code: emptyJSON,
41+
Data: emptyJSON,
42+
ChainId: int(chainId),
43+
CreationTime: strconv.FormatInt(creationTime, 10),
44+
GasLimit: "0",
45+
GasPrice: "0",
46+
Hash: coinbaseDecoded.ReqKey,
47+
Nonce: "",
48+
PactId: nil,
49+
Continuation: emptyJSON,
50+
Gas: "0",
51+
Result: coinbaseDecoded.Result,
52+
Logs: coinbaseDecoded.Logs,
53+
NumEvents: len(coinbaseDecoded.Events),
54+
RequestKey: coinbaseDecoded.ReqKey,
55+
Rollback: false,
56+
Sender: "coinbase",
57+
Sigs: emptyArray,
58+
Step: 0,
59+
Proof: nil,
60+
TTL: "0",
61+
TxId: fmt.Sprintf("%d", coinbaseDecoded.TxID),
62+
}
63+
64+
return txAttribute, nil
65+
}

backfill/process/process_blocks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func PrepareBlocks(network string, chainId int, payloads []fetch.ProcessedPayloa
2929
TransactionsHash: payload.TransactionsHash,
3030
OutputsHash: payload.OutputsHash,
3131
Coinbase: string(payload.Coinbase),
32-
TransactionsCount: len(payload.Transactions),
32+
TransactionsCount: len(payload.Transactions) + 1, // txs + coinbase tx
3333
}
3434

3535
blocks = append(blocks, block)

backfill/process/process_events.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import (
1010
func PrepareEvents(network string, payload fetch.ProcessedPayload, transactionsId []int64) ([]repository.EventAttributes, error) {
1111
transactions := payload.Transactions
1212

13-
if len(transactions) == 0 {
14-
return []repository.EventAttributes{}, nil
15-
}
16-
1713
const avgEventsPerTransaction = 80
1814
events := make([]repository.EventAttributes, 0, len(transactions)*avgEventsPerTransaction)
1915

@@ -42,7 +38,34 @@ func PrepareEvents(network string, payload fetch.ProcessedPayload, transactionsI
4238
}
4339
events = append(events, eventRecord)
4440
}
41+
}
42+
43+
coinbaseDecoded, err := decodeCoinbase(string(payload.Coinbase))
44+
if err != nil {
45+
return nil, fmt.Errorf("decoding Coinbase JSON of block: %w", err)
46+
}
4547

48+
var coinbaseTxId = transactionsId[len(transactionsId)-1]
49+
for eventIndex, event := range coinbaseDecoded.Events {
50+
51+
module := buildModuleName(event.Module.Namespace, event.Module.Name)
52+
qualName := buildModuleName(event.Module.Namespace, event.Module.Name)
53+
paramsJSON, err := json.Marshal(event.Params)
54+
if err != nil {
55+
return []repository.EventAttributes{}, fmt.Errorf("marshaling params for event %s: %w", event.Name, err)
56+
}
57+
58+
eventRecord := repository.EventAttributes{
59+
TransactionId: coinbaseTxId,
60+
ChainId: payload.Header.ChainId,
61+
Module: module,
62+
Name: event.Name,
63+
Params: paramsJSON,
64+
QualName: qualName,
65+
RequestKey: coinbaseDecoded.ReqKey,
66+
OrderIndex: eventIndex,
67+
}
68+
events = append(events, eventRecord)
4669
}
4770

4871
return events, nil

backfill/process/process_transactions.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,9 @@ type CmdData struct {
3131
} `json:"payload"`
3232
}
3333

34-
func PrepareTransactions(network string, blockId int64, payload fetch.ProcessedPayload) ([]repository.TransactionAttributes, error) {
34+
func PrepareTransactions(network string, blockId int64, payload fetch.ProcessedPayload, block repository.BlockAttributes) ([]repository.TransactionAttributes, error) {
3535
transactions := payload.Transactions
3636

37-
if len(transactions) == 0 {
38-
return []repository.TransactionAttributes{}, nil
39-
}
40-
4137
transactionRecords := make([]repository.TransactionAttributes, 0, len(transactions))
4238

4339
var cmdData CmdData
@@ -91,9 +87,14 @@ func PrepareTransactions(network string, blockId int64, payload fetch.ProcessedP
9187
}
9288
}
9389

94-
chainId, err := strconv.Atoi(cmdData.Meta.ChainId)
95-
if err != nil {
96-
return nil, fmt.Errorf("converting ChainId for transaction %s: %w", t.Hash, err)
90+
var chainId int
91+
if cmdData.Meta.ChainId != "" {
92+
chainId, err = strconv.Atoi(cmdData.Meta.ChainId)
93+
if err != nil {
94+
return nil, fmt.Errorf("converting ChainId for transaction %s: %w", t.Hash, err)
95+
}
96+
} else {
97+
chainId = block.ChainId
9798
}
9899

99100
txId := strconv.Itoa(t.TxId)
@@ -137,6 +138,14 @@ func PrepareTransactions(network string, blockId int64, payload fetch.ProcessedP
137138
transactionRecords = append(transactionRecords, transactionRecord)
138139
}
139140

141+
coinbaseTx, err := processCoinbaseTransaction(string(payload.Coinbase), blockId, block.CreationTime, int64(block.ChainId))
142+
143+
if err != nil {
144+
return nil, fmt.Errorf("processing coinbase transaction %d: %w", blockId, err)
145+
}
146+
147+
transactionRecords = append(transactionRecords, coinbaseTx)
148+
140149
return transactionRecords, nil
141150
}
142151

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package process
22

33
import (
4+
"fmt"
45
"go-backfill/fetch"
56
"go-backfill/repository"
67
)
78

8-
func PrepareTransfers(network string, payload fetch.ProcessedPayload, transactionsId []int64) []repository.TransferAttributes {
9+
func PrepareTransfers(network string, payload fetch.ProcessedPayload, transactionsId []int64) ([]repository.TransferAttributes, error) {
910
transactions := payload.Transactions
1011

11-
if len(transactions) == 0 {
12-
return []repository.TransferAttributes{}
13-
}
14-
1512
const avgTransfersPerTransaction = 80
1613
transfers := make([]repository.TransferAttributes, 0, len(transactions)*avgTransfersPerTransaction)
1714

@@ -23,5 +20,14 @@ func PrepareTransfers(network string, payload fetch.ProcessedPayload, transactio
2320
transfers = append(transfers, nftTransfers...)
2421
}
2522

26-
return transfers
23+
coinbaseDecoded, err := decodeCoinbase(string(payload.Coinbase))
24+
if err != nil {
25+
return nil, fmt.Errorf("decoding Coinbase JSON of block: %w", err)
26+
}
27+
28+
var coinbaseTxId = transactionsId[len(transactionsId)-1]
29+
coinTransfers := GetCoinTransfers(coinbaseDecoded.Events, payload.Header.ChainId, coinbaseDecoded.ReqKey, coinbaseTxId)
30+
transfers = append(transfers, coinTransfers...)
31+
32+
return transfers, nil
2733
}

backfill/process/save_payloads.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ func savePayloads(network string, chainId int, processedPayloads []fetch.Process
6868

6969
var transactionIdsToSave [][]int64
7070
for index, processedPayload := range processedPayloads {
71-
txs, err := PrepareTransactions(network, blockIds[index], processedPayload)
71+
var blockId = blockIds[index]
72+
var currBlock = blocks[index]
73+
txs, err := PrepareTransactions(network, blockId, processedPayload, currBlock)
7274
if err != nil {
7375
return Counters{}, DataSizeTracker{}, fmt.Errorf("saving transactions -> %w", err)
7476
}
@@ -98,7 +100,10 @@ func savePayloads(network string, chainId int, processedPayloads []fetch.Process
98100
}
99101

100102
for index, processedPayload := range processedPayloads {
101-
transfers := PrepareTransfers(network, processedPayload, transactionIdsToSave[index])
103+
transfers, err := PrepareTransfers(network, processedPayload, transactionIdsToSave[index])
104+
if err != nil {
105+
return Counters{}, DataSizeTracker{}, fmt.Errorf("preparing transfers -> %w", err)
106+
}
102107
if err := repository.SaveTransfersToDatabase(transfers, tx); err != nil {
103108
return Counters{}, DataSizeTracker{}, fmt.Errorf("saving transfers: %w", err)
104109
}

backfill/process/utils.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,30 @@ func (g *GasLimit) UnmarshalJSON(data []byte) error {
8686
return nil
8787
}
8888

89+
// Attempt to unmarshal the data as a float
90+
var floatValue float64
91+
if err := json.Unmarshal(data, &floatValue); err == nil {
92+
*g = GasLimit(fmt.Sprintf("%.0f", floatValue)) // Convert float to int-like string
93+
return nil
94+
}
95+
8996
// Attempt to unmarshal the data as a string
9097
var stringValue string
9198
if err := json.Unmarshal(data, &stringValue); err == nil {
9299
*g = GasLimit(stringValue) // Assign the string value directly
93100
return nil
94101
}
95102

103+
var tempStruct struct {
104+
Int int `json:"int"`
105+
}
106+
if err := json.Unmarshal(data, &tempStruct); err == nil {
107+
*g = GasLimit(strconv.Itoa(tempStruct.Int)) // Convert int to string and assign
108+
return nil
109+
}
110+
96111
// If neither, return an error
97-
return fmt.Errorf("data is neither int nor string: %s", string(data))
112+
return fmt.Errorf("data is neither int nor string nor float not { int: <some_number> }: %s", string(data))
98113
}
99114

100115
func convertToFloat64(event fetch.Event, index int) (float64, bool) {

0 commit comments

Comments
 (0)