mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 19:36:44 +01:00
Feature/null refactor (#173)
* WIP: rebase * Make nullable fields use pointers
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package l2db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
@@ -49,7 +50,10 @@ func (l2db *L2DB) AddAccountCreationAuth(auth *common.AccountCreationAuth) error
|
||||
}
|
||||
|
||||
// GetAccountCreationAuth returns an account creation authorization into the DB
|
||||
func (l2db *L2DB) GetAccountCreationAuth(addr ethCommon.Address) (*common.AccountCreationAuth, error) {
|
||||
func (l2db *L2DB) GetAccountCreationAuth(addr *ethCommon.Address) (*common.AccountCreationAuth, error) {
|
||||
if addr == nil {
|
||||
return nil, errors.New("addr cannot be nil")
|
||||
}
|
||||
auth := new(common.AccountCreationAuth)
|
||||
return auth, meddler.QueryRow(
|
||||
l2db.db, auth,
|
||||
@@ -64,8 +68,8 @@ func (l2db *L2DB) AddTxTest(tx *common.PoolL2Tx) error {
|
||||
type withouUSD struct {
|
||||
TxID common.TxID `meddler:"tx_id"`
|
||||
FromIdx common.Idx `meddler:"from_idx"`
|
||||
ToIdx common.Idx `meddler:"to_idx"`
|
||||
ToEthAddr ethCommon.Address `meddler:"to_eth_addr"`
|
||||
ToIdx *common.Idx `meddler:"to_idx"`
|
||||
ToEthAddr *ethCommon.Address `meddler:"to_eth_addr"`
|
||||
ToBJJ *babyjub.PublicKey `meddler:"to_bjj"`
|
||||
TokenID common.TokenID `meddler:"token_id"`
|
||||
Amount *big.Int `meddler:"amount,bigint"`
|
||||
@@ -75,15 +79,15 @@ func (l2db *L2DB) AddTxTest(tx *common.PoolL2Tx) error {
|
||||
State common.PoolL2TxState `meddler:"state"`
|
||||
Signature *babyjub.Signature `meddler:"signature"`
|
||||
Timestamp time.Time `meddler:"timestamp,utctime"`
|
||||
BatchNum common.BatchNum `meddler:"batch_num,zeroisnull"`
|
||||
RqFromIdx common.Idx `meddler:"rq_from_idx,zeroisnull"`
|
||||
RqToIdx common.Idx `meddler:"rq_to_idx,zeroisnull"`
|
||||
RqToEthAddr ethCommon.Address `meddler:"rq_to_eth_addr"`
|
||||
BatchNum *common.BatchNum `meddler:"batch_num"`
|
||||
RqFromIdx *common.Idx `meddler:"rq_from_idx"`
|
||||
RqToIdx *common.Idx `meddler:"rq_to_idx"`
|
||||
RqToEthAddr *ethCommon.Address `meddler:"rq_to_eth_addr"`
|
||||
RqToBJJ *babyjub.PublicKey `meddler:"rq_to_bjj"`
|
||||
RqTokenID common.TokenID `meddler:"rq_token_id,zeroisnull"`
|
||||
RqTokenID *common.TokenID `meddler:"rq_token_id"`
|
||||
RqAmount *big.Int `meddler:"rq_amount,bigintnull"`
|
||||
RqFee common.FeeSelector `meddler:"rq_fee,zeroisnull"`
|
||||
RqNonce uint64 `meddler:"rq_nonce,zeroisnull"`
|
||||
RqFee *common.FeeSelector `meddler:"rq_fee"`
|
||||
RqNonce *uint64 `meddler:"rq_nonce"`
|
||||
Type common.TxType `meddler:"tx_type"`
|
||||
}
|
||||
return meddler.Insert(l2db.db, "tx_pool", &withouUSD{
|
||||
@@ -115,8 +119,8 @@ func (l2db *L2DB) AddTxTest(tx *common.PoolL2Tx) error {
|
||||
|
||||
// selectPoolTx select part of queries to get common.PoolL2Tx
|
||||
const selectPoolTx = `SELECT tx_pool.*, token.usd * tx_pool.amount_f AS value_usd,
|
||||
fee_percentage(tx_pool.fee::NUMERIC) * token.usd * tx_pool.amount_f AS fee_usd, token.usd_update,
|
||||
token.symbol AS token_symbol FROM tx_pool INNER JOIN token ON tx_pool.token_id = token.token_id `
|
||||
fee_percentage(tx_pool.fee::NUMERIC) * token.usd * tx_pool.amount_f AS fee_usd, token.usd_update
|
||||
FROM tx_pool INNER JOIN token ON tx_pool.token_id = token.token_id `
|
||||
|
||||
// GetTx return the specified Tx
|
||||
func (l2db *L2DB) GetTx(txID common.TxID) (*common.PoolL2Tx, error) {
|
||||
|
||||
@@ -141,7 +141,7 @@ func TestStartForging(t *testing.T) {
|
||||
fetchedTx, err := l2DB.GetTx(id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, common.PoolL2TxStateForging, fetchedTx.State)
|
||||
assert.Equal(t, fakeBatchNum, fetchedTx.BatchNum)
|
||||
assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ func TestDoneForging(t *testing.T) {
|
||||
fetchedTx, err := l2DB.GetTx(id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, common.PoolL2TxStateForged, fetchedTx.State)
|
||||
assert.Equal(t, fakeBatchNum, fetchedTx.BatchNum)
|
||||
assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ func TestInvalidate(t *testing.T) {
|
||||
fetchedTx, err := l2DB.GetTx(id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, common.PoolL2TxStateInvalid, fetchedTx.State)
|
||||
assert.Equal(t, fakeBatchNum, fetchedTx.BatchNum)
|
||||
assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,7 +242,7 @@ func TestCheckNonces(t *testing.T) {
|
||||
fetchedTx, err := l2DB.GetTx(id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, common.PoolL2TxStateInvalid, fetchedTx.State)
|
||||
assert.Equal(t, fakeBatchNum, fetchedTx.BatchNum)
|
||||
assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -257,11 +257,12 @@ func TestReorg(t *testing.T) {
|
||||
reorgedTxIDs := []common.TxID{}
|
||||
nonReorgedTxIDs := []common.TxID{}
|
||||
for i := 0; i < len(txs); i++ {
|
||||
txs[i].BatchNum = new(common.BatchNum)
|
||||
if txs[i].State == common.PoolL2TxStateForged || txs[i].State == common.PoolL2TxStateInvalid {
|
||||
txs[i].BatchNum = reorgBatch
|
||||
*txs[i].BatchNum = reorgBatch
|
||||
reorgedTxIDs = append(reorgedTxIDs, txs[i].TxID)
|
||||
} else {
|
||||
txs[i].BatchNum = lastValidBatch
|
||||
*txs[i].BatchNum = lastValidBatch
|
||||
nonReorgedTxIDs = append(nonReorgedTxIDs, txs[i].TxID)
|
||||
}
|
||||
err := l2DB.AddTxTest(txs[i])
|
||||
@@ -269,17 +270,16 @@ func TestReorg(t *testing.T) {
|
||||
}
|
||||
err := l2DB.Reorg(lastValidBatch)
|
||||
assert.NoError(t, err)
|
||||
var nullBatchNum common.BatchNum
|
||||
for _, id := range reorgedTxIDs {
|
||||
tx, err := l2DB.GetTx(id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, nullBatchNum, tx.BatchNum)
|
||||
assert.Nil(t, tx.BatchNum)
|
||||
assert.Equal(t, common.PoolL2TxStatePending, tx.State)
|
||||
}
|
||||
for _, id := range nonReorgedTxIDs {
|
||||
tx, err := l2DB.GetTx(id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, lastValidBatch, tx.BatchNum)
|
||||
assert.Equal(t, lastValidBatch, *tx.BatchNum)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,11 +294,12 @@ func TestPurge(t *testing.T) {
|
||||
safeBatchNum := toDeleteBatchNum + l2DB.safetyPeriod + 1
|
||||
// Add txs to the DB
|
||||
for i := 0; i < int(l2DB.maxTxs); i++ {
|
||||
txs[i].BatchNum = new(common.BatchNum)
|
||||
if i%1 == 0 { // keep tx
|
||||
txs[i].BatchNum = safeBatchNum
|
||||
*txs[i].BatchNum = safeBatchNum
|
||||
keepedIDs = append(keepedIDs, txs[i].TxID)
|
||||
} else if i%2 == 0 { // delete after safety period
|
||||
txs[i].BatchNum = toDeleteBatchNum
|
||||
*txs[i].BatchNum = toDeleteBatchNum
|
||||
if i%3 == 0 {
|
||||
txs[i].State = common.PoolL2TxStateForged
|
||||
} else {
|
||||
@@ -343,7 +344,7 @@ func TestAuth(t *testing.T) {
|
||||
err := l2DB.AddAccountCreationAuth(auths[i])
|
||||
assert.NoError(t, err)
|
||||
// Fetch from DB
|
||||
auth, err := l2DB.GetAccountCreationAuth(auths[i].EthAddr)
|
||||
auth, err := l2DB.GetAccountCreationAuth(&auths[i].EthAddr)
|
||||
assert.NoError(t, err)
|
||||
// Check fetched vs generated
|
||||
assert.Equal(t, auths[i].EthAddr, auth.EthAddr)
|
||||
|
||||
Reference in New Issue
Block a user