hermez-node/db/l2db/l2db.go
db/l2db/l2db.goRaw
1package l2db
2
3import (
4 "math/big"
5 "time"
6
7 ethCommon "github.com/ethereum/go-ethereum/common"
8 "github.com/hermeznetwork/hermez-node/common"
9 "github.com/iden3/go-iden3-crypto/babyjub"
10 "github.com/jmoiron/sqlx"
11
12 //nolint:errcheck // driver for postgres DB
13 _ "github.com/lib/pq"
14 "github.com/russross/meddler"
15)
16
17// TODO(Edu): Check DB consistency while there's concurrent use from Coordinator/TxSelector & API
18
19// L2DB stores L2 txs and authorization registers received by the coordinator and keeps them until they are no longer relevant
20// due to them being forged or invalid after a safety period
21type L2DB struct {
22 db *sqlx.DB
23 safetyPeriod common.BatchNum
24 ttl time.Duration
25 maxTxs uint32
26}
27
28// NewL2DB creates a L2DB.
29// To create it, it's needed db connection, safety period expressed in batches,
30// maxTxs that the DB should have and TTL (time to live) for pending txs.
31func NewL2DB(db *sqlx.DB, safetyPeriod common.BatchNum, maxTxs uint32, TTL time.Duration) *L2DB {
32 return &L2DB{
33 db: db,
34 safetyPeriod: safetyPeriod,
35 ttl: TTL,
36 maxTxs: maxTxs,
37 }
38}
39
40// DB returns a pointer to the L2DB.db. This method should be used only for
41// internal testing purposes.
42func (l2db *L2DB) DB() *sqlx.DB {
43 return l2db.db
44}
45
46// AddAccountCreationAuth inserts an account creation authorization into the DB
47func (l2db *L2DB) AddAccountCreationAuth(auth *common.AccountCreationAuth) error {
48 return meddler.Insert(l2db.db, "account_creation_auth", auth)
49}
50
51// GetAccountCreationAuth returns an account creation authorization into the DB
52func (l2db *L2DB) GetAccountCreationAuth(addr ethCommon.Address) (*common.AccountCreationAuth, error) {
53 auth := new(common.AccountCreationAuth)
54 return auth, meddler.QueryRow(
55 l2db.db, auth,
56 "SELECT * FROM account_creation_auth WHERE eth_addr = $1;",
57 addr,
58 )
59}
60
61// AddTxTest inserts a tx into the L2DB. This is useful for test purposes,
62// but in production txs will only be inserted through the API (method TBD)
63func (l2db *L2DB) AddTxTest(tx *common.PoolL2Tx) error {
64 type withouUSD struct {
65 TxID common.TxID `meddler:"tx_id"`
66 FromIdx common.Idx `meddler:"from_idx"`
67 ToIdx common.Idx `meddler:"to_idx"`
68 ToEthAddr ethCommon.Address `meddler:"to_eth_addr"`
69 ToBJJ *babyjub.PublicKey `meddler:"to_bjj"`
70 TokenID common.TokenID `meddler:"token_id"`
71 Amount *big.Int `meddler:"amount,bigint"`
72 AmountFloat float64 `meddler:"amount_f"`
73 Fee common.FeeSelector `meddler:"fee"`
74 Nonce common.Nonce `meddler:"nonce"`
75 State common.PoolL2TxState `meddler:"state"`
76 Signature *babyjub.Signature `meddler:"signature"`
77 Timestamp time.Time `meddler:"timestamp,utctime"`
78 BatchNum common.BatchNum `meddler:"batch_num,zeroisnull"`
79 RqFromIdx common.Idx `meddler:"rq_from_idx,zeroisnull"`
80 RqToIdx common.Idx `meddler:"rq_to_idx,zeroisnull"`
81 RqToEthAddr ethCommon.Address `meddler:"rq_to_eth_addr"`
82 RqToBJJ *babyjub.PublicKey `meddler:"rq_to_bjj"`
83 RqTokenID common.TokenID `meddler:"rq_token_id,zeroisnull"`
84 RqAmount *big.Int `meddler:"rq_amount,bigintnull"`
85 RqFee common.FeeSelector `meddler:"rq_fee,zeroisnull"`
86 RqNonce uint64 `meddler:"rq_nonce,zeroisnull"`
87 Type common.TxType `meddler:"tx_type"`
88 }
89 return meddler.Insert(l2db.db, "tx_pool", &withouUSD{
90 TxID: tx.TxID,
91 FromIdx: tx.FromIdx,
92 ToIdx: tx.ToIdx,
93 ToEthAddr: tx.ToEthAddr,
94 ToBJJ: tx.ToBJJ,
95 TokenID: tx.TokenID,
96 Amount: tx.Amount,
97 AmountFloat: tx.AmountFloat,
98 Fee: tx.Fee,
99 Nonce: tx.Nonce,
100 State: tx.State,
101 Signature: tx.Signature,
102 Timestamp: tx.Timestamp,
103 BatchNum: tx.BatchNum,
104 RqFromIdx: tx.RqFromIdx,
105 RqToIdx: tx.RqToIdx,
106 RqToEthAddr: tx.RqToEthAddr,
107 RqToBJJ: tx.RqToBJJ,
108 RqTokenID: tx.RqTokenID,
109 RqAmount: tx.RqAmount,
110 RqFee: tx.RqFee,
111 RqNonce: tx.RqNonce,
112 Type: tx.Type,
113 })
114}
115
116// selectPoolTx select part of queries to get common.PoolL2Tx
117const selectPoolTx = `SELECT tx_pool.*, token.usd * tx_pool.amount_f AS value_usd,
118fee_percentage(tx_pool.fee::NUMERIC) * token.usd * tx_pool.amount_f AS fee_usd, token.usd_update,
119token.symbol AS token_symbol FROM tx_pool INNER JOIN token ON tx_pool.token_id = token.token_id `
120
121// GetTx return the specified Tx
122func (l2db *L2DB) GetTx(txID common.TxID) (*common.PoolL2Tx, error) {
123 tx := new(common.PoolL2Tx)
124 return tx, meddler.QueryRow(
125 l2db.db, tx,
126 selectPoolTx+"WHERE tx_id = $1;",
127 txID,
128 )
129}
130
131// GetPendingTxs return all the pending txs of the L2DB, that have a non NULL AbsoluteFee
132func (l2db *L2DB) GetPendingTxs() ([]*common.PoolL2Tx, error) {
133 var txs []*common.PoolL2Tx
134 err := meddler.QueryAll(
135 l2db.db, &txs,
136 selectPoolTx+"WHERE state = $1 AND token.usd IS NOT NULL",
137 common.PoolL2TxStatePending,
138 )
139 return txs, err
140}
141
142// StartForging updates the state of the transactions that will begin the forging process.
143// The state of the txs referenced by txIDs will be changed from Pending -> Forging
144func (l2db *L2DB) StartForging(txIDs []common.TxID, batchNum common.BatchNum) error {
145 query, args, err := sqlx.In(
146 `UPDATE tx_pool
147 SET state = ?, batch_num = ?
148 WHERE state = ? AND tx_id IN (?);`,
149 common.PoolL2TxStateForging,
150 batchNum,
151 common.PoolL2TxStatePending,
152 txIDs,
153 )
154 if err != nil {
155 return err
156 }
157 query = l2db.db.Rebind(query)
158 _, err = l2db.db.Exec(query, args...)
159 return err
160}
161
162// DoneForging updates the state of the transactions that have been forged
163// so the state of the txs referenced by txIDs will be changed from Forging -> Forged
164func (l2db *L2DB) DoneForging(txIDs []common.TxID, batchNum common.BatchNum) error {
165 query, args, err := sqlx.In(
166 `UPDATE tx_pool
167 SET state = ?, batch_num = ?
168 WHERE state = ? AND tx_id IN (?);`,
169 common.PoolL2TxStateForged,
170 batchNum,
171 common.PoolL2TxStateForging,
172 txIDs,
173 )
174 if err != nil {
175 return err
176 }
177 query = l2db.db.Rebind(query)
178 _, err = l2db.db.Exec(query, args...)
179 return err
180}
181
182// InvalidateTxs updates the state of the transactions that are invalid.
183// The state of the txs referenced by txIDs will be changed from * -> Invalid
184func (l2db *L2DB) InvalidateTxs(txIDs []common.TxID, batchNum common.BatchNum) error {
185 query, args, err := sqlx.In(
186 `UPDATE tx_pool
187 SET state = ?, batch_num = ?
188 WHERE tx_id IN (?);`,
189 common.PoolL2TxStateInvalid,
190 batchNum,
191 txIDs,
192 )
193 if err != nil {
194 return err
195 }
196 query = l2db.db.Rebind(query)
197 _, err = l2db.db.Exec(query, args...)
198 return err
199}
200
201// CheckNonces invalidate txs with nonces that are smaller or equal than their respective accounts nonces.
202// The state of the affected txs will be changed from Pending -> Invalid
203func (l2db *L2DB) CheckNonces(updatedAccounts []common.Account, batchNum common.BatchNum) (err error) {
204 txn, err := l2db.db.Begin()
205 if err != nil {
206 return err
207 }
208 defer func() {
209 // Rollback the transaction if there was an error.
210 if err != nil {
211 err = txn.Rollback()
212 }
213 }()
214 for i := 0; i < len(updatedAccounts); i++ {
215 _, err = txn.Exec(
216 `UPDATE tx_pool
217 SET state = $1, batch_num = $2
218 WHERE state = $3 AND from_idx = $4 AND nonce <= $5;`,
219 common.PoolL2TxStateInvalid,
220 batchNum,
221 common.PoolL2TxStatePending,
222 updatedAccounts[i].Idx,
223 updatedAccounts[i].Nonce,
224 )
225 if err != nil {
226 return err
227 }
228 }
229 return txn.Commit()
230}
231
232// Reorg updates the state of txs that were updated in a batch that has been discarted due to a blockchain reorg.
233// The state of the affected txs can change form Forged -> Pending or from Invalid -> Pending
234func (l2db *L2DB) Reorg(lastValidBatch common.BatchNum) error {
235 _, err := l2db.db.Exec(
236 `UPDATE tx_pool SET batch_num = NULL, state = $1
237 WHERE (state = $2 OR state = $3) AND batch_num > $4`,
238 common.PoolL2TxStatePending,
239 common.PoolL2TxStateForged,
240 common.PoolL2TxStateInvalid,
241 lastValidBatch,
242 )
243 return err
244}
245
246// Purge deletes transactions that have been forged or marked as invalid for longer than the safety period
247// it also deletes txs that has been in the L2DB for longer than the ttl if maxTxs has been exceeded
248func (l2db *L2DB) Purge(currentBatchNum common.BatchNum) (err error) {
249 txn, err := l2db.db.Begin()
250 if err != nil {
251 return err
252 }
253 defer func() {
254 // Rollback the transaction if there was an error.
255 if err != nil {
256 err = txn.Rollback()
257 }
258 }()
259 // Delete pending txs that have been in the pool after the TTL if maxTxs is reached
260 now := time.Now().UTC().Unix()
261 _, err = txn.Exec(
262 `DELETE FROM tx_pool WHERE (SELECT count(*) FROM tx_pool) > $1 AND timestamp < $2`,
263 l2db.maxTxs,
264 time.Unix(now-int64(l2db.ttl.Seconds()), 0),
265 )
266 if err != nil {
267 return err
268 }
269 // Delete txs that have been marked as forged / invalid after the safety period
270 _, err = txn.Exec(
271 `DELETE FROM tx_pool
272 WHERE batch_num < $1 AND (state = $2 OR state = $3)`,
273 currentBatchNum-l2db.safetyPeriod,
274 common.PoolL2TxStateForged,
275 common.PoolL2TxStateInvalid,
276 )
277 if err != nil {
278 return err
279 }
280 return txn.Commit()
281}