Avoid using pointers in common

This commit is contained in:
Arnau B
2020-10-07 18:43:48 +02:00
parent 5ec18f0378
commit 5097939b12
30 changed files with 673 additions and 564 deletions

View File

@@ -1,7 +1,6 @@
package l2db
import (
"errors"
"math/big"
"time"
@@ -9,7 +8,6 @@ import (
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/hermez-node/db"
"github.com/hermeznetwork/hermez-node/log"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/jmoiron/sqlx"
//nolint:errcheck // driver for postgres DB
@@ -52,10 +50,7 @@ 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) {
if addr == nil {
return nil, errors.New("addr cannot be nil")
}
func (l2db *L2DB) GetAccountCreationAuth(addr ethCommon.Address) (*common.AccountCreationAuth, error) {
auth := new(common.AccountCreationAuth)
return auth, meddler.QueryRow(
l2db.db, auth,
@@ -67,69 +62,71 @@ func (l2db *L2DB) GetAccountCreationAuth(addr *ethCommon.Address) (*common.Accou
// AddTxTest inserts a tx into the L2DB. This is useful for test purposes,
// but in production txs will only be inserted through the API (method TBD)
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"`
ToBJJ *babyjub.PublicKey `meddler:"to_bjj"`
TokenID common.TokenID `meddler:"token_id"`
Amount *big.Int `meddler:"amount,bigint"`
AmountFloat float64 `meddler:"amount_f"`
Fee common.FeeSelector `meddler:"fee"`
Nonce common.Nonce `meddler:"nonce"`
State common.PoolL2TxState `meddler:"state"`
Signature *babyjub.Signature `meddler:"signature"`
Timestamp time.Time `meddler:"timestamp,utctime"`
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"`
RqAmount *big.Int `meddler:"rq_amount,bigintnull"`
RqFee *common.FeeSelector `meddler:"rq_fee"`
RqNonce *uint64 `meddler:"rq_nonce"`
Type common.TxType `meddler:"tx_type"`
// transform tx from *common.PoolL2Tx to PoolL2TxWrite
insertTx := &PoolL2TxWrite{
TxID: tx.TxID,
FromIdx: tx.FromIdx,
ToBJJ: tx.ToBJJ,
TokenID: tx.TokenID,
Amount: tx.Amount,
Fee: tx.Fee,
Nonce: tx.Nonce,
State: tx.State,
Signature: tx.Signature,
RqToBJJ: tx.RqToBJJ,
RqAmount: tx.RqAmount,
Type: tx.Type,
}
return meddler.Insert(l2db.db, "tx_pool", &withouUSD{
TxID: tx.TxID,
FromIdx: tx.FromIdx,
ToIdx: tx.ToIdx,
ToEthAddr: tx.ToEthAddr,
ToBJJ: tx.ToBJJ,
TokenID: tx.TokenID,
Amount: tx.Amount,
AmountFloat: tx.AmountFloat,
Fee: tx.Fee,
Nonce: tx.Nonce,
State: tx.State,
Signature: tx.Signature,
Timestamp: tx.Timestamp,
BatchNum: tx.BatchNum,
RqFromIdx: tx.RqFromIdx,
RqToIdx: tx.RqToIdx,
RqToEthAddr: tx.RqToEthAddr,
RqToBJJ: tx.RqToBJJ,
RqTokenID: tx.RqTokenID,
RqAmount: tx.RqAmount,
RqFee: tx.RqFee,
RqNonce: tx.RqNonce,
Type: tx.Type,
})
if tx.ToIdx != 0 {
insertTx.ToIdx = &tx.ToIdx
}
nilAddr := ethCommon.BigToAddress(big.NewInt(0))
if tx.ToEthAddr != nilAddr {
insertTx.ToEthAddr = &tx.ToEthAddr
}
if tx.RqFromIdx != 0 {
insertTx.RqFromIdx = &tx.RqFromIdx
}
if tx.RqToIdx != 0 { // if true, all Rq... fields must be different to nil
insertTx.RqToIdx = &tx.RqToIdx
insertTx.RqTokenID = &tx.RqTokenID
insertTx.RqFee = &tx.RqFee
insertTx.RqNonce = &tx.RqNonce
}
if tx.RqToEthAddr != nilAddr {
insertTx.RqToEthAddr = &tx.RqToEthAddr
}
f := new(big.Float).SetInt(tx.Amount)
amountF, _ := f.Float64()
insertTx.AmountFloat = amountF
// insert tx
return meddler.Insert(l2db.db, "tx_pool", insertTx)
}
// selectPoolTx select part of queries to get common.PoolL2Tx
const selectPoolTx = `SELECT tx_pool.*, token.usd * tx_pool.amount_f AS value_usd,
// selectPoolTxRead select part of queries to get PoolL2TxRead
const selectPoolTxRead = `SELECT tx_pool.tx_id, tx_pool.from_idx, tx_pool.to_idx, tx_pool.to_eth_addr,
tx_pool.to_bjj, tx_pool.token_id, tx_pool.amount, tx_pool.fee, tx_pool.nonce,
tx_pool.state, tx_pool.signature, tx_pool.timestamp, tx_pool.batch_num, tx_pool.rq_from_idx,
tx_pool.rq_to_idx, tx_pool.rq_to_eth_addr, tx_pool.rq_to_bjj, tx_pool.rq_token_id, tx_pool.rq_amount,
tx_pool.rq_fee, tx_pool.rq_nonce, tx_pool.tx_type,
token.eth_block_num, token.eth_addr, token.name, token.symbol, token.decimals, token.usd, token.usd_update
FROM tx_pool INNER JOIN token ON tx_pool.token_id = token.token_id `
// selectPoolTxCommon select part of queries to get common.PoolL2Tx
const selectPoolTxCommon = `SELECT tx_pool.tx_id, tx_pool.from_idx, tx_pool.to_idx, tx_pool.to_eth_addr,
tx_pool.to_bjj, tx_pool.token_id, tx_pool.amount, tx_pool.fee, tx_pool.nonce,
tx_pool.state, tx_pool.signature, tx_pool.timestamp, tx_pool.rq_from_idx,
tx_pool.rq_to_idx, tx_pool.rq_to_eth_addr, tx_pool.rq_to_bjj, tx_pool.rq_token_id, tx_pool.rq_amount,
tx_pool.rq_fee, tx_pool.rq_nonce, tx_pool.tx_type,
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) {
tx := new(common.PoolL2Tx)
func (l2db *L2DB) GetTx(txID common.TxID) (*PoolL2TxRead, error) {
tx := new(PoolL2TxRead)
return tx, meddler.QueryRow(
l2db.db, tx,
selectPoolTx+"WHERE tx_id = $1;",
selectPoolTxRead+"WHERE tx_id = $1;",
txID,
)
}
@@ -139,7 +136,7 @@ func (l2db *L2DB) GetPendingTxs() ([]common.PoolL2Tx, error) {
var txs []*common.PoolL2Tx
err := meddler.QueryAll(
l2db.db, &txs,
selectPoolTx+"WHERE state = $1 AND token.usd IS NOT NULL",
selectPoolTxCommon+"WHERE state = $1",
common.PoolL2TxStatePending,
)
return db.SlicePtrsToSlice(txs).([]common.PoolL2Tx), err

View File

@@ -1,10 +1,12 @@
package l2db
import (
"math/big"
"os"
"testing"
"time"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-node/common"
dbUtils "github.com/hermeznetwork/hermez-node/db"
"github.com/hermeznetwork/hermez-node/db/historydb"
@@ -16,6 +18,7 @@ import (
var l2DB *L2DB
var tokens []common.Token
var tokensUSD []historydb.TokenRead
func TestMain(m *testing.M) {
// init DB
@@ -25,7 +28,7 @@ func TestMain(m *testing.M) {
panic(err)
}
l2DB = NewL2DB(db, 10, 100, 24*time.Hour)
tokens, err = prepareHistoryDB(db)
tokens, tokensUSD = prepareHistoryDB(db)
if err != nil {
panic(err)
}
@@ -38,7 +41,7 @@ func TestMain(m *testing.M) {
os.Exit(result)
}
func prepareHistoryDB(db *sqlx.DB) ([]common.Token, error) {
func prepareHistoryDB(db *sqlx.DB) ([]common.Token, []historydb.TokenRead) {
historyDB := historydb.NewHistoryDB(db)
const fromBlock int64 = 1
const toBlock int64 = 5
@@ -54,7 +57,31 @@ func prepareHistoryDB(db *sqlx.DB) ([]common.Token, error) {
// Store tokens to historyDB
const nTokens = 5
tokens := test.GenTokens(nTokens, blocks)
return tokens, historyDB.AddTokens(tokens)
if err := historyDB.AddTokens(tokens); err != nil {
panic(err)
}
readTokens := []historydb.TokenRead{}
for i, token := range tokens {
readToken := historydb.TokenRead{
TokenID: token.TokenID,
EthBlockNum: token.EthBlockNum,
EthAddr: token.EthAddr,
Name: token.Name,
Symbol: token.Symbol,
Decimals: token.Decimals,
}
if i%2 != 0 {
value := float64(i) * 5.4321
if err := historyDB.UpdateTokenValue(token.Symbol, value); err != nil {
panic(err)
}
now := time.Now().UTC()
readToken.USDUpdate = &now
readToken.USD = &value
}
readTokens = append(readTokens, readToken)
}
return tokens, readTokens
}
func TestAddTxTest(t *testing.T) {
@@ -67,20 +94,105 @@ func TestAddTxTest(t *testing.T) {
assert.NoError(t, err)
fetchedTx, err := l2DB.GetTx(tx.TxID)
assert.NoError(t, err)
assertTx(t, tx, fetchedTx)
assertReadTx(t, commonToRead(tx, tokens), fetchedTx)
}
}
func assertTx(t *testing.T, expected, actual *common.PoolL2Tx) {
assert.Equal(t, expected.Timestamp.Unix(), actual.Timestamp.Unix())
expected.Timestamp = actual.Timestamp
if expected.AbsoluteFeeUpdate != nil {
assert.Equal(t, expected.AbsoluteFeeUpdate.Unix(), actual.AbsoluteFeeUpdate.Unix())
expected.AbsoluteFeeUpdate = actual.AbsoluteFeeUpdate
} else {
assert.Equal(t, expected.AbsoluteFeeUpdate, actual.AbsoluteFeeUpdate)
func commonToRead(commonTx *common.PoolL2Tx, tokens []common.Token) *PoolL2TxRead {
readTx := &PoolL2TxRead{
TxID: commonTx.TxID,
FromIdx: commonTx.FromIdx,
ToBJJ: commonTx.ToBJJ,
Amount: commonTx.Amount,
Fee: commonTx.Fee,
Nonce: commonTx.Nonce,
State: commonTx.State,
Signature: commonTx.Signature,
RqToBJJ: commonTx.RqToBJJ,
RqAmount: commonTx.RqAmount,
Type: commonTx.Type,
Timestamp: commonTx.Timestamp,
TokenID: commonTx.TokenID,
}
// token related fields
// find token
token := historydb.TokenRead{}
for _, tkn := range tokensUSD {
if tkn.TokenID == readTx.TokenID {
token = tkn
break
}
}
// set token related fields
readTx.TokenEthBlockNum = token.EthBlockNum
readTx.TokenEthAddr = token.EthAddr
readTx.TokenName = token.Name
readTx.TokenSymbol = token.Symbol
readTx.TokenDecimals = token.Decimals
readTx.TokenUSD = token.USD
readTx.TokenUSDUpdate = token.USDUpdate
// nullable fields
if commonTx.ToIdx != 0 {
readTx.ToIdx = &commonTx.ToIdx
}
nilAddr := ethCommon.BigToAddress(big.NewInt(0))
if commonTx.ToEthAddr != nilAddr {
readTx.ToEthAddr = &commonTx.ToEthAddr
}
if commonTx.RqFromIdx != 0 {
readTx.RqFromIdx = &commonTx.RqFromIdx
}
if commonTx.RqToIdx != 0 { // if true, all Rq... fields must be different to nil
readTx.RqToIdx = &commonTx.RqToIdx
readTx.RqTokenID = &commonTx.RqTokenID
readTx.RqFee = &commonTx.RqFee
readTx.RqNonce = &commonTx.RqNonce
}
if commonTx.RqToEthAddr != nilAddr {
readTx.RqToEthAddr = &commonTx.RqToEthAddr
}
return readTx
}
func assertReadTx(t *testing.T, expected, actual *PoolL2TxRead) {
// Check that timestamp has been set within the last 3 seconds
assert.Less(t, time.Now().UTC().Unix()-3, actual.Timestamp.Unix())
assert.GreaterOrEqual(t, time.Now().UTC().Unix(), actual.Timestamp.Unix())
expected.Timestamp = actual.Timestamp
// Check token related stuff
if expected.TokenUSDUpdate != nil {
// Check that TokenUSDUpdate has been set within the last 3 seconds
assert.Less(t, time.Now().UTC().Unix()-3, actual.TokenUSDUpdate.Unix())
assert.GreaterOrEqual(t, time.Now().UTC().Unix(), actual.TokenUSDUpdate.Unix())
expected.TokenUSDUpdate = actual.TokenUSDUpdate
}
assert.Equal(t, expected, actual)
}
func assertTx(t *testing.T, expected, actual *common.PoolL2Tx) {
// Check that timestamp has been set within the last 3 seconds
assert.Less(t, time.Now().UTC().Unix()-3, actual.Timestamp.Unix())
assert.GreaterOrEqual(t, time.Now().UTC().Unix(), actual.Timestamp.Unix())
expected.Timestamp = actual.Timestamp
// Check absolute fee
// find token
token := historydb.TokenRead{}
for _, tkn := range tokensUSD {
if expected.TokenID == tkn.TokenID {
token = tkn
break
}
}
// If the token has value in USD setted
if token.USDUpdate != nil {
assert.Equal(t, token.USDUpdate.Unix(), actual.AbsoluteFeeUpdate.Unix())
expected.AbsoluteFeeUpdate = actual.AbsoluteFeeUpdate
// Set expected fee
f := new(big.Float).SetInt(expected.Amount)
amountF, _ := f.Float64()
expected.AbsoluteFee = *token.USD * amountF * expected.Fee.Percentage()
test.AssertUSD(t, &expected.AbsoluteFee, &actual.AbsoluteFee)
}
test.AssertUSD(t, expected.AbsoluteFee, actual.AbsoluteFee)
assert.Equal(t, expected, actual)
}
@@ -104,7 +216,7 @@ func TestGetPending(t *testing.T) {
for _, tx := range txs {
err := l2DB.AddTxTest(tx)
assert.NoError(t, err)
if tx.State == common.PoolL2TxStatePending && tx.AbsoluteFee != nil {
if tx.State == common.PoolL2TxStatePending {
pendingTxs = append(pendingTxs, tx)
}
}
@@ -257,16 +369,21 @@ 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
reorgedTxIDs = append(reorgedTxIDs, txs[i].TxID)
} else {
*txs[i].BatchNum = lastValidBatch
nonReorgedTxIDs = append(nonReorgedTxIDs, txs[i].TxID)
}
err := l2DB.AddTxTest(txs[i])
assert.NoError(t, err)
var batchNum common.BatchNum
if txs[i].State == common.PoolL2TxStateForged || txs[i].State == common.PoolL2TxStateInvalid {
reorgedTxIDs = append(reorgedTxIDs, txs[i].TxID)
batchNum = reorgBatch
} else {
nonReorgedTxIDs = append(nonReorgedTxIDs, txs[i].TxID)
batchNum = lastValidBatch
}
_, err = l2DB.db.Exec(
"UPDATE tx_pool SET batch_num = $1 WHERE tx_id = $2;",
batchNum, txs[i].TxID,
)
assert.NoError(t, err)
}
err := l2DB.Reorg(lastValidBatch)
assert.NoError(t, err)
@@ -277,9 +394,9 @@ func TestReorg(t *testing.T) {
assert.Equal(t, common.PoolL2TxStatePending, tx.State)
}
for _, id := range nonReorgedTxIDs {
tx, err := l2DB.GetTx(id)
fetchedTx, err := l2DB.GetTx(id)
assert.NoError(t, err)
assert.Equal(t, lastValidBatch, *tx.BatchNum)
assert.Equal(t, lastValidBatch, *fetchedTx.BatchNum)
}
}
@@ -294,12 +411,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
var batchNum common.BatchNum
if i%2 == 0 { // keep tx
batchNum = safeBatchNum
keepedIDs = append(keepedIDs, txs[i].TxID)
} else if i%2 == 0 { // delete after safety period
*txs[i].BatchNum = toDeleteBatchNum
} else { // delete after safety period
batchNum = toDeleteBatchNum
if i%3 == 0 {
txs[i].State = common.PoolL2TxStateForged
} else {
@@ -309,16 +426,28 @@ func TestPurge(t *testing.T) {
}
err := l2DB.AddTxTest(txs[i])
assert.NoError(t, err)
// Set batchNum
_, err = l2DB.db.Exec(
"UPDATE tx_pool SET batch_num = $1 WHERE tx_id = $2;",
batchNum, txs[i].TxID,
)
assert.NoError(t, err)
}
for i := int(l2DB.maxTxs); i < len(txs); i++ {
// Delete after TTL
txs[i].Timestamp = time.Unix(time.Now().UTC().Unix()-int64(l2DB.ttl.Seconds()+float64(4*time.Second)), 0)
deletedIDs = append(deletedIDs, txs[i].TxID)
err := l2DB.AddTxTest(txs[i])
assert.NoError(t, err)
// Set timestamp
deleteTimestamp := time.Unix(time.Now().UTC().Unix()-int64(l2DB.ttl.Seconds()+float64(4*time.Second)), 0)
_, err = l2DB.db.Exec(
"UPDATE tx_pool SET timestamp = $1 WHERE tx_id = $2;",
deleteTimestamp, txs[i].TxID,
)
assert.NoError(t, err)
}
// Purge txs
err := l2DB.Purge(safeBatchNum - 1)
err := l2DB.Purge(safeBatchNum)
assert.NoError(t, err)
// Check results
for _, id := range deletedIDs {
@@ -344,7 +473,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)

70
db/l2db/views.go Normal file
View File

@@ -0,0 +1,70 @@
package l2db
import (
"math/big"
"time"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-node/common"
"github.com/iden3/go-iden3-crypto/babyjub"
)
// PoolL2TxWrite holds the necessary data to perform inserts in tx_pool
type PoolL2TxWrite 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"`
ToBJJ *babyjub.PublicKey `meddler:"to_bjj"`
TokenID common.TokenID `meddler:"token_id"`
Amount *big.Int `meddler:"amount,bigint"`
AmountFloat float64 `meddler:"amount_f"`
Fee common.FeeSelector `meddler:"fee"`
Nonce common.Nonce `meddler:"nonce"`
State common.PoolL2TxState `meddler:"state"`
Signature *babyjub.Signature `meddler:"signature"`
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"`
RqAmount *big.Int `meddler:"rq_amount,bigintnull"`
RqFee *common.FeeSelector `meddler:"rq_fee"`
RqNonce *uint64 `meddler:"rq_nonce"`
Type common.TxType `meddler:"tx_type"`
}
// PoolL2TxRead represents a L2 Tx pool with extra metadata used by the API
type PoolL2TxRead 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"`
ToBJJ *babyjub.PublicKey `meddler:"to_bjj"`
Amount *big.Int `meddler:"amount,bigint"`
Fee common.FeeSelector `meddler:"fee"`
Nonce common.Nonce `meddler:"nonce"`
State common.PoolL2TxState `meddler:"state"`
Signature *babyjub.Signature `meddler:"signature"`
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"`
RqAmount *big.Int `meddler:"rq_amount,bigintnull"`
RqFee *common.FeeSelector `meddler:"rq_fee"`
RqNonce *uint64 `meddler:"rq_nonce"`
Type common.TxType `meddler:"tx_type"`
// Extra read fileds
BatchNum *common.BatchNum `meddler:"batch_num"`
Timestamp time.Time `meddler:"timestamp,utctime"`
TotalItems int `meddler:"total_items"`
TokenID common.TokenID `meddler:"token_id"`
TokenEthBlockNum int64 `meddler:"eth_block_num"`
TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
TokenName string `meddler:"name"`
TokenSymbol string `meddler:"symbol"`
TokenDecimals uint64 `meddler:"decimals"`
TokenUSD *float64 `meddler:"usd"`
TokenUSDUpdate *time.Time `meddler:"usd_update"`
}