arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/db/historydb/views.go
db/historydb/views.goRaw
  1package historydb
  2
  3import (
  4	"math/big"
  5	"time"
  6
  7	ethCommon "github.com/ethereum/go-ethereum/common"
  8	"github.com/hermeznetwork/hermez-node/apitypes"
  9	"github.com/hermeznetwork/hermez-node/common"
 10	"github.com/iden3/go-iden3-crypto/babyjub"
 11	"github.com/iden3/go-merkletree"
 12)
 13
 14// HistoryTx is a representation of a generic Tx with additional information
 15// required by the API, and extracted by joining block and token tables
 16type HistoryTx struct {
 17	// Generic
 18	IsL1        bool             `meddler:"is_l1"`
 19	TxID        common.TxID      `meddler:"id"`
 20	ItemID      int              `meddler:"item_id"`
 21	Type        common.TxType    `meddler:"type"`
 22	Position    int              `meddler:"position"`
 23	FromIdx     *common.Idx      `meddler:"from_idx"`
 24	ToIdx       common.Idx       `meddler:"to_idx"`
 25	Amount      *big.Int         `meddler:"amount,bigint"`
 26	HistoricUSD *float64         `meddler:"amount_usd"`
 27	BatchNum    *common.BatchNum `meddler:"batch_num"`     // batchNum in which this tx was forged. If the tx is L2, this must be != 0
 28	EthBlockNum int64            `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
 29	// L1
 30	ToForgeL1TxsNum *int64             `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
 31	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
 32	FromEthAddr     *ethCommon.Address `meddler:"from_eth_addr"`
 33	FromBJJ         *babyjub.PublicKey `meddler:"from_bjj"`
 34	LoadAmount      *big.Int           `meddler:"load_amount,bigintnull"`
 35	// LoadAmountFloat       *float64           `meddler:"load_amount_f"`
 36	HistoricLoadAmountUSD *float64 `meddler:"load_amount_usd"`
 37	// L2
 38	Fee            *common.FeeSelector `meddler:"fee"`
 39	HistoricFeeUSD *float64            `meddler:"fee_usd"`
 40	Nonce          *common.Nonce       `meddler:"nonce"`
 41	// API extras
 42	Timestamp        time.Time         `meddler:"timestamp,utctime"`
 43	TotalItems       int               `meddler:"total_items"`
 44	FirstItem        int               `meddler:"first_item"`
 45	LastItem         int               `meddler:"last_item"`
 46	TokenID          common.TokenID    `meddler:"token_id"`
 47	TokenEthBlockNum int64             `meddler:"token_block"`
 48	TokenEthAddr     ethCommon.Address `meddler:"eth_addr"`
 49	TokenName        string            `meddler:"name"`
 50	TokenSymbol      string            `meddler:"symbol"`
 51	TokenDecimals    uint64            `meddler:"decimals"`
 52	TokenUSD         *float64          `meddler:"usd"`
 53	TokenUSDUpdate   *time.Time        `meddler:"usd_update"`
 54}
 55
 56// txWrite is an representatiion that merges common.L1Tx and common.L2Tx
 57// in order to perform inserts into tx table
 58type txWrite struct {
 59	// Generic
 60	IsL1        bool             `meddler:"is_l1"`
 61	TxID        common.TxID      `meddler:"id"`
 62	Type        common.TxType    `meddler:"type"`
 63	Position    int              `meddler:"position"`
 64	FromIdx     *common.Idx      `meddler:"from_idx"`
 65	ToIdx       common.Idx       `meddler:"to_idx"`
 66	Amount      *big.Int         `meddler:"amount,bigint"`
 67	AmountFloat float64          `meddler:"amount_f"`
 68	TokenID     common.TokenID   `meddler:"token_id"`
 69	BatchNum    *common.BatchNum `meddler:"batch_num"`     // batchNum in which this tx was forged. If the tx is L2, this must be != 0
 70	EthBlockNum int64            `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
 71	// L1
 72	ToForgeL1TxsNum *int64             `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
 73	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
 74	FromEthAddr     *ethCommon.Address `meddler:"from_eth_addr"`
 75	FromBJJ         *babyjub.PublicKey `meddler:"from_bjj"`
 76	LoadAmount      *big.Int           `meddler:"load_amount,bigintnull"`
 77	LoadAmountFloat *float64           `meddler:"load_amount_f"`
 78	// L2
 79	Fee   *common.FeeSelector `meddler:"fee"`
 80	Nonce *common.Nonce       `meddler:"nonce"`
 81}
 82
 83// TokenWithUSD add USD info to common.Token
 84type TokenWithUSD struct {
 85	ItemID      int               `json:"itemId" meddler:"item_id"`
 86	TokenID     common.TokenID    `json:"id" meddler:"token_id"`
 87	EthBlockNum int64             `json:"ethereumBlockNum" meddler:"eth_block_num"` // Ethereum block number in which this token was registered
 88	EthAddr     ethCommon.Address `json:"ethereumAddress" meddler:"eth_addr"`
 89	Name        string            `json:"name" meddler:"name"`
 90	Symbol      string            `json:"symbol" meddler:"symbol"`
 91	Decimals    uint64            `json:"decimals" meddler:"decimals"`
 92	USD         *float64          `json:"USD" meddler:"usd"`
 93	USDUpdate   *time.Time        `json:"fiatUpdate" meddler:"usd_update,utctime"`
 94	TotalItems  int               `json:"-" meddler:"total_items"`
 95	FirstItem   int               `json:"-" meddler:"first_item"`
 96	LastItem    int               `json:"-" meddler:"last_item"`
 97}
 98
 99// HistoryExit is a representation of a exit with additional information
100// required by the API, and extracted by joining token table
101type HistoryExit struct {
102	ItemID                 int                             `meddler:"item_id"`
103	BatchNum               common.BatchNum                 `meddler:"batch_num"`
104	AccountIdx             common.Idx                      `meddler:"account_idx"`
105	MerkleProof            *merkletree.CircomVerifierProof `meddler:"merkle_proof,json"`
106	Balance                *big.Int                        `meddler:"balance,bigint"`
107	InstantWithdrawn       *int64                          `meddler:"instant_withdrawn"`
108	DelayedWithdrawRequest *int64                          `meddler:"delayed_withdraw_request"`
109	DelayedWithdrawn       *int64                          `meddler:"delayed_withdrawn"`
110	TotalItems             int                             `meddler:"total_items"`
111	FirstItem              int                             `meddler:"first_item"`
112	LastItem               int                             `meddler:"last_item"`
113	TokenID                common.TokenID                  `meddler:"token_id"`
114	TokenEthBlockNum       int64                           `meddler:"token_block"`
115	TokenEthAddr           ethCommon.Address               `meddler:"eth_addr"`
116	TokenName              string                          `meddler:"name"`
117	TokenSymbol            string                          `meddler:"symbol"`
118	TokenDecimals          uint64                          `meddler:"decimals"`
119	TokenUSD               *float64                        `meddler:"usd"`
120	TokenUSDUpdate         *time.Time                      `meddler:"usd_update"`
121}
122
123// CoordinatorAPI is a representation of a coordinator with additional information
124// required by the API
125type CoordinatorAPI struct {
126	ItemID      int               `json:"itemId" meddler:"item_id"`
127	Bidder      ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
128	Forger      ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
129	EthBlockNum int64             `json:"ethereumBlock" meddler:"eth_block_num"`
130	URL         string            `json:"URL" meddler:"url"`
131	TotalItems  int               `json:"-" meddler:"total_items"`
132	FirstItem   int               `json:"-" meddler:"first_item"`
133	LastItem    int               `json:"-" meddler:"last_item"`
134}
135
136// BatchAPI is a representation of a batch with additional information
137// required by the API, and extracted by joining block table
138type BatchAPI struct {
139	ItemID        int                    `json:"itemId" meddler:"item_id"`
140	BatchNum      common.BatchNum        `json:"batchNum" meddler:"batch_num"`
141	EthBlockNum   int64                  `json:"ethereumBlockNum" meddler:"eth_block_num"`
142	EthBlockHash  ethCommon.Hash         `json:"ethereumBlockHash" meddler:"hash"`
143	Timestamp     time.Time              `json:"timestamp" meddler:"timestamp,utctime"`
144	ForgerAddr    ethCommon.Address      `json:"forgerAddr" meddler:"forger_addr"`
145	CollectedFees apitypes.CollectedFees `json:"collectedFees" meddler:"fees_collected,json"`
146	// CollectedFees map[common.TokenID]*big.Int `json:"collectedFees" meddler:"fees_collected,json"`
147	TotalFeesUSD  *float64           `json:"historicTotalCollectedFeesUSD" meddler:"total_fees_usd"`
148	StateRoot     apitypes.BigIntStr `json:"stateRoot" meddler:"state_root"`
149	NumAccounts   int                `json:"numAccounts" meddler:"num_accounts"`
150	ExitRoot      apitypes.BigIntStr `json:"exitRoot" meddler:"exit_root"`
151	ForgeL1TxsNum *int64             `json:"forgeL1TransactionsNum" meddler:"forge_l1_txs_num"`
152	SlotNum       int64              `json:"slotNum" meddler:"slot_num"`
153	TotalItems    int                `json:"-" meddler:"total_items"`
154	FirstItem     int                `json:"-" meddler:"first_item"`
155	LastItem      int                `json:"-" meddler:"last_item"`
156}
157
158// Network define status of the network
159type Network struct {
160	LastBlock   int64            `json:"lastBlock"`
161	LastBatch   BatchAPI         `json:"lastBatch"`
162	CurrentSlot int64            `json:"currentSlot"`
163	NextForgers []CoordinatorAPI `json:"nextForgers"`
164}
165
166// Metrics define metrics of the network
167type Metrics struct {
168	TransactionsPerBatch  float64 `json:"transactionsPerBatch"`
169	BatchFrequency        float64 `json:"batchFrequency"`
170	TransactionsPerSecond float64 `json:"transactionsPerSecond"`
171	TotalAccounts         int64   `json:"totalAccounts"`
172	TotalBJJs             int64   `json:"totalBJJs"`
173	AvgTransactionFee     float64 `json:"avgTransactionFee"`
174}