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