hermez-node/db/historydb/views.go
db/historydb/views.goRaw
1package historydb
2
3import (
4 "encoding/json"
5 "math/big"
6 "time"
7
8 ethCommon "github.com/ethereum/go-ethereum/common"
9 "github.com/hermeznetwork/hermez-node/apitypes"
10 "github.com/hermeznetwork/hermez-node/common"
11 "github.com/iden3/go-iden3-crypto/babyjub"
12 "github.com/iden3/go-merkletree"
13)
14
15// TxAPI is a representation of a generic Tx with additional information
16// required by the API, and extracted by joining block and token tables
17type TxAPI struct {
18 // Generic
19 IsL1 bool `meddler:"is_l1"`
20 TxID common.TxID `meddler:"id"`
21 ItemID uint64 `meddler:"item_id"`
22 Type common.TxType `meddler:"type"`
23 Position int `meddler:"position"`
24 FromIdx *apitypes.HezIdx `meddler:"from_idx"`
25 FromEthAddr *apitypes.HezEthAddr `meddler:"from_eth_addr"`
26 FromBJJ *apitypes.HezBJJ `meddler:"from_bjj"`
27 ToIdx apitypes.HezIdx `meddler:"to_idx"`
28 ToEthAddr *apitypes.HezEthAddr `meddler:"to_eth_addr"`
29 ToBJJ *apitypes.HezBJJ `meddler:"to_bjj"`
30 Amount apitypes.BigIntStr `meddler:"amount"`
31 HistoricUSD *float64 `meddler:"amount_usd"`
32 BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
33 EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
34 // L1
35 ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
36 UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
37 DepositAmount *apitypes.BigIntStr `meddler:"deposit_amount"`
38 HistoricDepositAmountUSD *float64 `meddler:"deposit_amount_usd"`
39 AmountSuccess bool `meddler:"amount_success"`
40 DepositAmountSuccess bool `meddler:"deposit_amount_success"`
41 // L2
42 Fee *common.FeeSelector `meddler:"fee"`
43 HistoricFeeUSD *float64 `meddler:"fee_usd"`
44 Nonce *common.Nonce `meddler:"nonce"`
45 // API extras
46 Timestamp time.Time `meddler:"timestamp,utctime"`
47 TotalItems uint64 `meddler:"total_items"`
48 FirstItem uint64 `meddler:"first_item"`
49 LastItem uint64 `meddler:"last_item"`
50 TokenID common.TokenID `meddler:"token_id"`
51 TokenItemID uint64 `meddler:"token_item_id"`
52 TokenEthBlockNum int64 `meddler:"token_block"`
53 TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
54 TokenName string `meddler:"name"`
55 TokenSymbol string `meddler:"symbol"`
56 TokenDecimals uint64 `meddler:"decimals"`
57 TokenUSD *float64 `meddler:"usd"`
58 TokenUSDUpdate *time.Time `meddler:"usd_update"`
59}
60
61// MarshalJSON is used to neast some of the fields of TxAPI
62// without the need of auxiliar structs
63func (tx TxAPI) MarshalJSON() ([]byte, error) {
64 jsonTx := map[string]interface{}{
65 "id": tx.TxID,
66 "itemId": tx.ItemID,
67 "type": tx.Type,
68 "position": tx.Position,
69 "fromAccountIndex": tx.FromIdx,
70 "fromHezEthereumAddress": tx.FromEthAddr,
71 "fromBJJ": tx.FromBJJ,
72 "toAccountIndex": tx.ToIdx,
73 "toHezEthereumAddress": tx.ToEthAddr,
74 "toBJJ": tx.ToBJJ,
75 "amount": tx.Amount,
76 "batchNum": tx.BatchNum,
77 "historicUSD": tx.HistoricUSD,
78 "timestamp": tx.Timestamp,
79 "L1Info": nil,
80 "L2Info": nil,
81 "token": map[string]interface{}{
82 "id": tx.TokenID,
83 "itemId": tx.TokenItemID,
84 "ethereumBlockNum": tx.TokenEthBlockNum,
85 "ethereumAddress": tx.TokenEthAddr,
86 "name": tx.TokenName,
87 "symbol": tx.TokenSymbol,
88 "decimals": tx.TokenDecimals,
89 "USD": tx.TokenUSD,
90 "fiatUpdate": tx.TokenUSDUpdate,
91 },
92 }
93 if tx.IsL1 {
94 jsonTx["L1orL2"] = "L1"
95 amountSuccess := tx.AmountSuccess
96 depositAmountSuccess := tx.DepositAmountSuccess
97 if tx.BatchNum == nil {
98 amountSuccess = false
99 depositAmountSuccess = false
100 }
101 jsonTx["L1Info"] = map[string]interface{}{
102 "toForgeL1TransactionsNum": tx.ToForgeL1TxsNum,
103 "userOrigin": tx.UserOrigin,
104 "depositAmount": tx.DepositAmount,
105 "amountSuccess": amountSuccess,
106 "depositAmountSuccess": depositAmountSuccess,
107 "historicDepositAmountUSD": tx.HistoricDepositAmountUSD,
108 "ethereumBlockNum": tx.EthBlockNum,
109 }
110 } else {
111 jsonTx["L1orL2"] = "L2"
112 jsonTx["L2Info"] = map[string]interface{}{
113 "fee": tx.Fee,
114 "historicFeeUSD": tx.HistoricFeeUSD,
115 "nonce": tx.Nonce,
116 }
117 }
118 return json.Marshal(jsonTx)
119}
120
121// txWrite is an representatiion that merges common.L1Tx and common.L2Tx
122// in order to perform inserts into tx table
123// EffectiveAmount and LoadEffectiveAmount are not set since they have default values in the DB
124type txWrite struct {
125 // Generic
126 IsL1 bool `meddler:"is_l1"`
127 TxID common.TxID `meddler:"id"`
128 Type common.TxType `meddler:"type"`
129 Position int `meddler:"position"`
130 FromIdx *common.Idx `meddler:"from_idx"`
131 ToIdx common.Idx `meddler:"to_idx"`
132 Amount *big.Int `meddler:"amount,bigint"`
133 AmountFloat float64 `meddler:"amount_f"`
134 TokenID common.TokenID `meddler:"token_id"`
135 BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
136 EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
137 // L1
138 ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
139 UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
140 FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
141 FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
142 DepositAmount *big.Int `meddler:"deposit_amount,bigintnull"`
143 DepositAmountFloat *float64 `meddler:"deposit_amount_f"`
144 // L2
145 Fee *common.FeeSelector `meddler:"fee"`
146 Nonce *common.Nonce `meddler:"nonce"`
147}
148
149// TokenWithUSD add USD info to common.Token
150type TokenWithUSD struct {
151 ItemID uint64 `json:"itemId" meddler:"item_id"`
152 TokenID common.TokenID `json:"id" meddler:"token_id"`
153 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` // Ethereum block number in which this token was registered
154 EthAddr ethCommon.Address `json:"ethereumAddress" meddler:"eth_addr"`
155 Name string `json:"name" meddler:"name"`
156 Symbol string `json:"symbol" meddler:"symbol"`
157 Decimals uint64 `json:"decimals" meddler:"decimals"`
158 USD *float64 `json:"USD" meddler:"usd"`
159 USDUpdate *time.Time `json:"fiatUpdate" meddler:"usd_update,utctime"`
160 TotalItems uint64 `json:"-" meddler:"total_items"`
161 FirstItem uint64 `json:"-" meddler:"first_item"`
162 LastItem uint64 `json:"-" meddler:"last_item"`
163}
164
165// ExitAPI is a representation of a exit with additional information
166// required by the API, and extracted by joining token table
167type ExitAPI struct {
168 ItemID uint64 `meddler:"item_id"`
169 BatchNum common.BatchNum `meddler:"batch_num"`
170 AccountIdx apitypes.HezIdx `meddler:"account_idx"`
171 MerkleProof *merkletree.CircomVerifierProof `meddler:"merkle_proof,json"`
172 Balance apitypes.BigIntStr `meddler:"balance"`
173 InstantWithdrawn *int64 `meddler:"instant_withdrawn"`
174 DelayedWithdrawRequest *int64 `meddler:"delayed_withdraw_request"`
175 DelayedWithdrawn *int64 `meddler:"delayed_withdrawn"`
176 TotalItems uint64 `meddler:"total_items"`
177 FirstItem uint64 `meddler:"first_item"`
178 LastItem uint64 `meddler:"last_item"`
179 TokenID common.TokenID `meddler:"token_id"`
180 TokenItemID uint64 `meddler:"token_item_id"`
181 TokenEthBlockNum int64 `meddler:"token_block"`
182 TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
183 TokenName string `meddler:"name"`
184 TokenSymbol string `meddler:"symbol"`
185 TokenDecimals uint64 `meddler:"decimals"`
186 TokenUSD *float64 `meddler:"usd"`
187 TokenUSDUpdate *time.Time `meddler:"usd_update"`
188}
189
190// MarshalJSON is used to neast some of the fields of ExitAPI
191// without the need of auxiliar structs
192func (e ExitAPI) MarshalJSON() ([]byte, error) {
193 siblings := []string{}
194 for i := 0; i < len(e.MerkleProof.Siblings); i++ {
195 siblings = append(siblings, e.MerkleProof.Siblings[i].String())
196 }
197 return json.Marshal(map[string]interface{}{
198 "itemId": e.ItemID,
199 "batchNum": e.BatchNum,
200 "accountIndex": e.AccountIdx,
201 "merkleProof": map[string]interface{}{
202 "Root": e.MerkleProof.Root.String(),
203 "Siblings": siblings,
204 "OldKey": e.MerkleProof.OldKey.String(),
205 "OldValue": e.MerkleProof.OldValue.String(),
206 "IsOld0": e.MerkleProof.IsOld0,
207 "Key": e.MerkleProof.Key.String(),
208 "Value": e.MerkleProof.Value.String(),
209 "Fnc": e.MerkleProof.Fnc,
210 },
211 "balance": e.Balance,
212 "instantWithdrawn": e.InstantWithdrawn,
213 "delayedWithdrawRequest": e.DelayedWithdrawRequest,
214 "delayedWithdrawn": e.DelayedWithdrawn,
215 "token": map[string]interface{}{
216 "id": e.TokenID,
217 "itemId": e.TokenItemID,
218 "ethereumBlockNum": e.TokenEthBlockNum,
219 "ethereumAddress": e.TokenEthAddr,
220 "name": e.TokenName,
221 "symbol": e.TokenSymbol,
222 "decimals": e.TokenDecimals,
223 "USD": e.TokenUSD,
224 "fiatUpdate": e.TokenUSDUpdate,
225 },
226 })
227}
228
229// CoordinatorAPI is a representation of a coordinator with additional information
230// required by the API
231type CoordinatorAPI struct {
232 ItemID uint64 `json:"itemId" meddler:"item_id"`
233 Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
234 Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
235 EthBlockNum int64 `json:"ethereumBlock" meddler:"eth_block_num"`
236 URL string `json:"URL" meddler:"url"`
237 TotalItems uint64 `json:"-" meddler:"total_items"`
238 FirstItem uint64 `json:"-" meddler:"first_item"`
239 LastItem uint64 `json:"-" meddler:"last_item"`
240}
241
242// AccountAPI is a representation of a account with additional information
243// required by the API
244type AccountAPI struct {
245 ItemID uint64 `meddler:"item_id"`
246 Idx apitypes.HezIdx `meddler:"idx"`
247 BatchNum common.BatchNum `meddler:"batch_num"`
248 PublicKey apitypes.HezBJJ `meddler:"bjj"`
249 EthAddr apitypes.HezEthAddr `meddler:"eth_addr"`
250 Nonce common.Nonce `meddler:"-"` // max of 40 bits used
251 Balance *apitypes.BigIntStr `meddler:"-"` // max of 192 bits used
252 TotalItems uint64 `meddler:"total_items"`
253 FirstItem uint64 `meddler:"first_item"`
254 LastItem uint64 `meddler:"last_item"`
255 TokenID common.TokenID `meddler:"token_id"`
256 TokenItemID int `meddler:"token_item_id"`
257 TokenEthBlockNum int64 `meddler:"token_block"`
258 TokenEthAddr ethCommon.Address `meddler:"token_eth_addr"`
259 TokenName string `meddler:"name"`
260 TokenSymbol string `meddler:"symbol"`
261 TokenDecimals uint64 `meddler:"decimals"`
262 TokenUSD *float64 `meddler:"usd"`
263 TokenUSDUpdate *time.Time `meddler:"usd_update"`
264}
265
266// MarshalJSON is used to neast some of the fields of AccountAPI
267// without the need of auxiliar structs
268func (account AccountAPI) MarshalJSON() ([]byte, error) {
269 jsonAccount := map[string]interface{}{
270 "itemId": account.ItemID,
271 "accountIndex": account.Idx,
272 "nonce": account.Nonce,
273 "balance": account.Balance,
274 "bjj": account.PublicKey,
275 "hezEthereumAddress": account.EthAddr,
276 "token": map[string]interface{}{
277 "id": account.TokenID,
278 "itemId": account.TokenItemID,
279 "ethereumBlockNum": account.TokenEthBlockNum,
280 "ethereumAddress": account.TokenEthAddr,
281 "name": account.TokenName,
282 "symbol": account.TokenSymbol,
283 "decimals": account.TokenDecimals,
284 "USD": account.TokenUSD,
285 "fiatUpdate": account.TokenUSDUpdate,
286 },
287 }
288 return json.Marshal(jsonAccount)
289}
290
291// BatchAPI is a representation of a batch with additional information
292// required by the API, and extracted by joining block table
293type BatchAPI struct {
294 ItemID uint64 `json:"itemId" meddler:"item_id"`
295 BatchNum common.BatchNum `json:"batchNum" meddler:"batch_num"`
296 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
297 EthBlockHash ethCommon.Hash `json:"ethereumBlockHash" meddler:"hash"`
298 Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
299 ForgerAddr ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
300 CollectedFees apitypes.CollectedFees `json:"collectedFees" meddler:"fees_collected,json"`
301 TotalFeesUSD *float64 `json:"historicTotalCollectedFeesUSD" meddler:"total_fees_usd"`
302 StateRoot apitypes.BigIntStr `json:"stateRoot" meddler:"state_root"`
303 NumAccounts int `json:"numAccounts" meddler:"num_accounts"`
304 ExitRoot apitypes.BigIntStr `json:"exitRoot" meddler:"exit_root"`
305 ForgeL1TxsNum *int64 `json:"forgeL1TransactionsNum" meddler:"forge_l1_txs_num"`
306 SlotNum int64 `json:"slotNum" meddler:"slot_num"`
307 TotalItems uint64 `json:"-" meddler:"total_items"`
308 FirstItem uint64 `json:"-" meddler:"first_item"`
309 LastItem uint64 `json:"-" meddler:"last_item"`
310}
311
312// Metrics define metrics of the network
313type Metrics struct {
314 TransactionsPerBatch float64 `json:"transactionsPerBatch"`
315 BatchFrequency float64 `json:"batchFrequency"`
316 TransactionsPerSecond float64 `json:"transactionsPerSecond"`
317 TotalAccounts int64 `json:"totalAccounts" meddler:"total_accounts"`
318 TotalBJJs int64 `json:"totalBJJs" meddler:"total_bjjs"`
319 AvgTransactionFee float64 `json:"avgTransactionFee"`
320}
321
322// MetricsTotals is used to get temporal information from HistoryDB
323// to calculate data to be stored into the Metrics struct
324type MetricsTotals struct {
325 TotalTransactions uint64 `meddler:"total_txs"`
326 FirstBatchNum common.BatchNum `meddler:"batch_num"`
327 TotalBatches int64 `meddler:"total_batches"`
328 TotalFeesUSD float64 `meddler:"total_fees"`
329}
330
331// BidAPI is a representation of a bid with additional information
332// required by the API
333type BidAPI struct {
334 ItemID uint64 `json:"itemId" meddler:"item_id"`
335 SlotNum int64 `json:"slotNum" meddler:"slot_num"`
336 BidValue apitypes.BigIntStr `json:"bidValue" meddler:"bid_value"`
337 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
338 Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
339 Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
340 URL string `json:"URL" meddler:"url"`
341 Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
342 TotalItems uint64 `json:"-" meddler:"total_items"`
343 FirstItem uint64 `json:"-" meddler:"first_item"`
344 LastItem uint64 `json:"-" meddler:"last_item"`
345}