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