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