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 EffectiveDepositAmount 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 EffectiveFromIdx *common.Idx `meddler:"effective_from_idx"`
132 ToIdx common.Idx `meddler:"to_idx"`
133 Amount *big.Int `meddler:"amount,bigint"`
134 AmountFloat float64 `meddler:"amount_f"`
135 TokenID common.TokenID `meddler:"token_id"`
136 BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
137 EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
138 // L1
139 ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
140 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
141 FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
142 FromBJJ *babyjub.PublicKeyComp `meddler:"from_bjj"`
143 DepositAmount *big.Int `meddler:"deposit_amount,bigintnull"`
144 DepositAmountFloat *float64 `meddler:"deposit_amount_f"`
145 // L2
146 Fee *common.FeeSelector `meddler:"fee"`
147 Nonce *common.Nonce `meddler:"nonce"`
148}
149
150// TokenWithUSD add USD info to common.Token
151type TokenWithUSD struct {
152 ItemID uint64 `json:"itemId" meddler:"item_id"`
153 TokenID common.TokenID `json:"id" meddler:"token_id"`
154 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` // Ethereum block number in which this token was registered
155 EthAddr ethCommon.Address `json:"ethereumAddress" meddler:"eth_addr"`
156 Name string `json:"name" meddler:"name"`
157 Symbol string `json:"symbol" meddler:"symbol"`
158 Decimals uint64 `json:"decimals" meddler:"decimals"`
159 USD *float64 `json:"USD" meddler:"usd"`
160 USDUpdate *time.Time `json:"fiatUpdate" meddler:"usd_update,utctime"`
161 TotalItems uint64 `json:"-" meddler:"total_items"`
162 FirstItem uint64 `json:"-" meddler:"first_item"`
163 LastItem uint64 `json:"-" meddler:"last_item"`
164}
165
166// ExitAPI is a representation of a exit with additional information
167// required by the API, and extracted by joining token table
168type ExitAPI struct {
169 ItemID uint64 `meddler:"item_id"`
170 BatchNum common.BatchNum `meddler:"batch_num"`
171 AccountIdx apitypes.HezIdx `meddler:"account_idx"`
172 EthAddr *apitypes.HezEthAddr `meddler:"eth_addr"`
173 BJJ *apitypes.HezBJJ `meddler:"bjj"`
174 MerkleProof *merkletree.CircomVerifierProof `meddler:"merkle_proof,json"`
175 Balance apitypes.BigIntStr `meddler:"balance"`
176 InstantWithdrawn *int64 `meddler:"instant_withdrawn"`
177 DelayedWithdrawRequest *int64 `meddler:"delayed_withdraw_request"`
178 DelayedWithdrawn *int64 `meddler:"delayed_withdrawn"`
179 TotalItems uint64 `meddler:"total_items"`
180 FirstItem uint64 `meddler:"first_item"`
181 LastItem uint64 `meddler:"last_item"`
182 TokenID common.TokenID `meddler:"token_id"`
183 TokenItemID uint64 `meddler:"token_item_id"`
184 TokenEthBlockNum int64 `meddler:"token_block"`
185 TokenEthAddr ethCommon.Address `meddler:"token_eth_addr"`
186 TokenName string `meddler:"name"`
187 TokenSymbol string `meddler:"symbol"`
188 TokenDecimals uint64 `meddler:"decimals"`
189 TokenUSD *float64 `meddler:"usd"`
190 TokenUSDUpdate *time.Time `meddler:"usd_update"`
191}
192
193// MarshalJSON is used to neast some of the fields of ExitAPI
194// without the need of auxiliar structs
195func (e ExitAPI) MarshalJSON() ([]byte, error) {
196 return json.Marshal(map[string]interface{}{
197 "itemId": e.ItemID,
198 "batchNum": e.BatchNum,
199 "accountIndex": e.AccountIdx,
200 "bjj": e.BJJ,
201 "hezEthereumAddress": e.EthAddr,
202 "merkleProof": e.MerkleProof,
203 "balance": e.Balance,
204 "instantWithdraw": e.InstantWithdrawn,
205 "delayedWithdrawRequest": e.DelayedWithdrawRequest,
206 "delayedWithdraw": e.DelayedWithdrawn,
207 "token": map[string]interface{}{
208 "id": e.TokenID,
209 "itemId": e.TokenItemID,
210 "ethereumBlockNum": e.TokenEthBlockNum,
211 "ethereumAddress": e.TokenEthAddr,
212 "name": e.TokenName,
213 "symbol": e.TokenSymbol,
214 "decimals": e.TokenDecimals,
215 "USD": e.TokenUSD,
216 "fiatUpdate": e.TokenUSDUpdate,
217 },
218 })
219}
220
221// CoordinatorAPI is a representation of a coordinator with additional information
222// required by the API
223type CoordinatorAPI struct {
224 ItemID uint64 `json:"itemId" meddler:"item_id"`
225 Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
226 Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
227 EthBlockNum int64 `json:"ethereumBlock" meddler:"eth_block_num"`
228 URL string `json:"URL" meddler:"url"`
229 TotalItems uint64 `json:"-" meddler:"total_items"`
230 FirstItem uint64 `json:"-" meddler:"first_item"`
231 LastItem uint64 `json:"-" meddler:"last_item"`
232}
233
234// AccountAPI is a representation of a account with additional information
235// required by the API
236type AccountAPI struct {
237 ItemID uint64 `meddler:"item_id"`
238 Idx apitypes.HezIdx `meddler:"idx"`
239 BatchNum common.BatchNum `meddler:"batch_num"`
240 PublicKey apitypes.HezBJJ `meddler:"bjj"`
241 EthAddr apitypes.HezEthAddr `meddler:"eth_addr"`
242 Nonce common.Nonce `meddler:"nonce"` // max of 40 bits used
243 Balance *apitypes.BigIntStr `meddler:"balance"` // max of 192 bits used
244 TotalItems uint64 `meddler:"total_items"`
245 FirstItem uint64 `meddler:"first_item"`
246 LastItem uint64 `meddler:"last_item"`
247 TokenID common.TokenID `meddler:"token_id"`
248 TokenItemID int `meddler:"token_item_id"`
249 TokenEthBlockNum int64 `meddler:"token_block"`
250 TokenEthAddr ethCommon.Address `meddler:"token_eth_addr"`
251 TokenName string `meddler:"name"`
252 TokenSymbol string `meddler:"symbol"`
253 TokenDecimals uint64 `meddler:"decimals"`
254 TokenUSD *float64 `meddler:"usd"`
255 TokenUSDUpdate *time.Time `meddler:"usd_update"`
256}
257
258// MarshalJSON is used to neast some of the fields of AccountAPI
259// without the need of auxiliar structs
260func (account AccountAPI) MarshalJSON() ([]byte, error) {
261 jsonAccount := map[string]interface{}{
262 "itemId": account.ItemID,
263 "accountIndex": account.Idx,
264 "nonce": account.Nonce,
265 "balance": account.Balance,
266 "bjj": account.PublicKey,
267 "hezEthereumAddress": account.EthAddr,
268 "token": map[string]interface{}{
269 "id": account.TokenID,
270 "itemId": account.TokenItemID,
271 "ethereumBlockNum": account.TokenEthBlockNum,
272 "ethereumAddress": account.TokenEthAddr,
273 "name": account.TokenName,
274 "symbol": account.TokenSymbol,
275 "decimals": account.TokenDecimals,
276 "USD": account.TokenUSD,
277 "fiatUpdate": account.TokenUSDUpdate,
278 },
279 }
280 return json.Marshal(jsonAccount)
281}
282
283// BatchAPI is a representation of a batch with additional information
284// required by the API, and extracted by joining block table
285type BatchAPI struct {
286 ItemID uint64 `json:"itemId" meddler:"item_id"`
287 BatchNum common.BatchNum `json:"batchNum" meddler:"batch_num"`
288 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
289 EthBlockHash ethCommon.Hash `json:"ethereumBlockHash" meddler:"hash"`
290 Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
291 ForgerAddr ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
292 CollectedFees apitypes.CollectedFees `json:"collectedFees" meddler:"fees_collected,json"`
293 TotalFeesUSD *float64 `json:"historicTotalCollectedFeesUSD" meddler:"total_fees_usd"`
294 StateRoot apitypes.BigIntStr `json:"stateRoot" meddler:"state_root"`
295 NumAccounts int `json:"numAccounts" meddler:"num_accounts"`
296 ExitRoot apitypes.BigIntStr `json:"exitRoot" meddler:"exit_root"`
297 ForgeL1TxsNum *int64 `json:"forgeL1TransactionsNum" meddler:"forge_l1_txs_num"`
298 SlotNum int64 `json:"slotNum" meddler:"slot_num"`
299 ForgedTxs int `json:"forgedTransactions" meddler:"forged_txs"`
300 TotalItems uint64 `json:"-" meddler:"total_items"`
301 FirstItem uint64 `json:"-" meddler:"first_item"`
302 LastItem uint64 `json:"-" meddler:"last_item"`
303}
304
305// Metrics define metrics of the network
306type Metrics struct {
307 TransactionsPerBatch float64 `json:"transactionsPerBatch"`
308 BatchFrequency float64 `json:"batchFrequency"`
309 TransactionsPerSecond float64 `json:"transactionsPerSecond"`
310 TotalAccounts int64 `json:"totalAccounts" meddler:"total_accounts"`
311 TotalBJJs int64 `json:"totalBJJs" meddler:"total_bjjs"`
312 AvgTransactionFee float64 `json:"avgTransactionFee"`
313 EstimatedTimeToForgeL1 float64 `json:"estimatedTimeToForgeL1" meddler:"estimated_time_to_forge_l1"`
314}
315
316// MetricsTotals is used to get temporal information from HistoryDB
317// to calculate data to be stored into the Metrics struct
318type MetricsTotals struct {
319 TotalTransactions uint64 `meddler:"total_txs"`
320 FirstBatchNum common.BatchNum `meddler:"batch_num"`
321 TotalBatches int64 `meddler:"total_batches"`
322 TotalFeesUSD float64 `meddler:"total_fees"`
323 MinTimestamp time.Time `meddler:"min_timestamp,utctime"`
324 MaxTimestamp time.Time `meddler:"max_timestamp,utctime"`
325}
326
327// BidAPI is a representation of a bid with additional information
328// required by the API
329type BidAPI struct {
330 ItemID uint64 `json:"itemId" meddler:"item_id"`
331 SlotNum int64 `json:"slotNum" meddler:"slot_num"`
332 BidValue apitypes.BigIntStr `json:"bidValue" meddler:"bid_value"`
333 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
334 Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
335 Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
336 URL string `json:"URL" meddler:"url"`
337 Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
338 TotalItems uint64 `json:"-" meddler:"total_items"`
339 FirstItem uint64 `json:"-" meddler:"first_item"`
340 LastItem uint64 `json:"-" meddler:"last_item"`
341}
342
343// MinBidInfo gives information of the minum bid for specific slot(s)
344type MinBidInfo struct {
345 DefaultSlotSetBid [6]*big.Int `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"`
346 DefaultSlotSetBidSlotNum int64 `json:"-" meddler:"default_slot_set_bid_slot_num"`
347}
348
349// BucketUpdateAPI are the bucket updates (tracking the withdrawals value changes)
350// in Rollup Smart Contract
351type BucketUpdateAPI struct {
352 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
353 NumBucket int `json:"numBucket" meddler:"num_bucket"`
354 BlockStamp int64 `json:"blockStamp" meddler:"block_stamp"`
355 Withdrawals *apitypes.BigIntStr `json:"withdrawals" meddler:"withdrawals"`
356}
357
358// BucketParamsAPI are the parameter variables of each Bucket of Rollup Smart
359// Contract
360type BucketParamsAPI struct {
361 CeilUSD *apitypes.BigIntStr `json:"ceilUSD"`
362 Withdrawals *apitypes.BigIntStr `json:"withdrawals"`
363 BlockWithdrawalRate *apitypes.BigIntStr `json:"blockWithdrawalRate"`
364 MaxWithdrawals *apitypes.BigIntStr `json:"maxWithdrawals"`
365}
366
367// RollupVariablesAPI are the variables of the Rollup Smart Contract
368type RollupVariablesAPI struct {
369 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
370 FeeAddToken *apitypes.BigIntStr `json:"feeAddToken" meddler:"fee_add_token" validate:"required"`
371 ForgeL1L2BatchTimeout int64 `json:"forgeL1L2BatchTimeout" meddler:"forge_l1_timeout" validate:"required"`
372 WithdrawalDelay uint64 `json:"withdrawalDelay" meddler:"withdrawal_delay" validate:"required"`
373 Buckets [common.RollupConstNumBuckets]BucketParamsAPI `json:"buckets" meddler:"buckets,json"`
374 SafeMode bool `json:"safeMode" meddler:"safe_mode"`
375}
376
377// AuctionVariablesAPI are the variables of the Auction Smart Contract
378type AuctionVariablesAPI struct {
379 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
380 // DonationAddress Address where the donations will be sent
381 DonationAddress ethCommon.Address `json:"donationAddress" meddler:"donation_address" validate:"required"`
382 // BootCoordinator Address of the boot coordinator
383 BootCoordinator ethCommon.Address `json:"bootCoordinator" meddler:"boot_coordinator" validate:"required"`
384 // BootCoordinatorURL URL of the boot coordinator
385 BootCoordinatorURL string `json:"bootCoordinatorUrl" meddler:"boot_coordinator_url" validate:"required"`
386 // DefaultSlotSetBid The minimum bid value in a series of 6 slots
387 DefaultSlotSetBid [6]*apitypes.BigIntStr `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"`
388 // DefaultSlotSetBidSlotNum SlotNum at which the new default_slot_set_bid applies
389 DefaultSlotSetBidSlotNum int64 `json:"defaultSlotSetBidSlotNum" meddler:"default_slot_set_bid_slot_num"`
390 // ClosedAuctionSlots Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min )
391 ClosedAuctionSlots uint16 `json:"closedAuctionSlots" meddler:"closed_auction_slots" validate:"required"`
392 // OpenAuctionSlots Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots )
393 OpenAuctionSlots uint16 `json:"openAuctionSlots" meddler:"open_auction_slots" validate:"required"`
394 // AllocationRatio How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%)
395 AllocationRatio [3]uint16 `json:"allocationRatio" meddler:"allocation_ratio,json" validate:"required"`
396 // Outbidding Minimum outbid (percentage) over the previous one to consider it valid
397 Outbidding uint16 `json:"outbidding" meddler:"outbidding" validate:"required"`
398 // SlotDeadline Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before
399 SlotDeadline uint8 `json:"slotDeadline" meddler:"slot_deadline" validate:"required"`
400}