mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Avoid using pointers in common
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
@@ -136,6 +137,7 @@ func (hdb *HistoryDB) AddBatches(batches []common.Batch) error {
|
||||
return hdb.addBatches(hdb.db, batches)
|
||||
}
|
||||
func (hdb *HistoryDB) addBatches(d meddler.DB, batches []common.Batch) error {
|
||||
// TODO: Calculate and insert total_fees_usd
|
||||
return db.BulkInsert(
|
||||
d,
|
||||
`INSERT INTO batch (
|
||||
@@ -147,8 +149,7 @@ func (hdb *HistoryDB) addBatches(d meddler.DB, batches []common.Batch) error {
|
||||
num_accounts,
|
||||
exit_root,
|
||||
forge_l1_txs_num,
|
||||
slot_num,
|
||||
total_fees_usd
|
||||
slot_num
|
||||
) VALUES %s;`,
|
||||
batches[:],
|
||||
)
|
||||
@@ -265,9 +266,7 @@ func (hdb *HistoryDB) addTokens(d meddler.DB, tokens []common.Token) error {
|
||||
eth_addr,
|
||||
name,
|
||||
symbol,
|
||||
decimals,
|
||||
usd,
|
||||
usd_update
|
||||
decimals
|
||||
) VALUES %s;`,
|
||||
tokens[:],
|
||||
)
|
||||
@@ -283,13 +282,13 @@ func (hdb *HistoryDB) UpdateTokenValue(tokenSymbol string, value float64) error
|
||||
}
|
||||
|
||||
// GetTokens returns a list of tokens from the DB
|
||||
func (hdb *HistoryDB) GetTokens() ([]common.Token, error) {
|
||||
var tokens []*common.Token
|
||||
func (hdb *HistoryDB) GetTokens() ([]TokenRead, error) {
|
||||
var tokens []*TokenRead
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &tokens,
|
||||
"SELECT * FROM token ORDER BY token_id;",
|
||||
)
|
||||
return db.SlicePtrsToSlice(tokens).([]common.Token), err
|
||||
return db.SlicePtrsToSlice(tokens).([]TokenRead), err
|
||||
}
|
||||
|
||||
// GetTokenSymbols returns all the token symbols from the DB
|
||||
@@ -347,26 +346,67 @@ func (hdb *HistoryDB) AddL1Txs(l1txs []common.L1Tx) error { return hdb.addL1Txs(
|
||||
// If the tx is originated by a coordinator, BatchNum must be provided. If it's originated by a user,
|
||||
// BatchNum should be null, and the value will be setted by a trigger when a batch forges the tx.
|
||||
func (hdb *HistoryDB) addL1Txs(d meddler.DB, l1txs []common.L1Tx) error {
|
||||
txs := []common.Tx{}
|
||||
for _, tx := range l1txs {
|
||||
txs = append(txs, *(tx.Tx()))
|
||||
txs := []txWrite{}
|
||||
for i := 0; i < len(l1txs); i++ {
|
||||
af := new(big.Float).SetInt(l1txs[i].Amount)
|
||||
amountFloat, _ := af.Float64()
|
||||
laf := new(big.Float).SetInt(l1txs[i].LoadAmount)
|
||||
loadAmountFloat, _ := laf.Float64()
|
||||
txs = append(txs, txWrite{
|
||||
// Generic
|
||||
IsL1: true,
|
||||
TxID: l1txs[i].TxID,
|
||||
Type: l1txs[i].Type,
|
||||
Position: l1txs[i].Position,
|
||||
FromIdx: &l1txs[i].FromIdx,
|
||||
ToIdx: l1txs[i].ToIdx,
|
||||
Amount: l1txs[i].Amount,
|
||||
AmountFloat: amountFloat,
|
||||
TokenID: l1txs[i].TokenID,
|
||||
BatchNum: l1txs[i].BatchNum,
|
||||
EthBlockNum: l1txs[i].EthBlockNum,
|
||||
// L1
|
||||
ToForgeL1TxsNum: l1txs[i].ToForgeL1TxsNum,
|
||||
UserOrigin: &l1txs[i].UserOrigin,
|
||||
FromEthAddr: &l1txs[i].FromEthAddr,
|
||||
FromBJJ: l1txs[i].FromBJJ,
|
||||
LoadAmount: l1txs[i].LoadAmount,
|
||||
LoadAmountFloat: &loadAmountFloat,
|
||||
})
|
||||
}
|
||||
return hdb.addTxs(d, txs)
|
||||
}
|
||||
|
||||
// AddL2Txs inserts L2 txs to the DB. USD and FeeUSD will be set automatically before storing the tx.
|
||||
// AddL2Txs inserts L2 txs to the DB. TokenID, USD and FeeUSD will be set automatically before storing the tx.
|
||||
func (hdb *HistoryDB) AddL2Txs(l2txs []common.L2Tx) error { return hdb.addL2Txs(hdb.db, l2txs) }
|
||||
|
||||
// addL2Txs inserts L2 txs to the DB. USD and FeeUSD will be set automatically before storing the tx.
|
||||
// addL2Txs inserts L2 txs to the DB. TokenID, USD and FeeUSD will be set automatically before storing the tx.
|
||||
func (hdb *HistoryDB) addL2Txs(d meddler.DB, l2txs []common.L2Tx) error {
|
||||
txs := []common.Tx{}
|
||||
for _, tx := range l2txs {
|
||||
txs = append(txs, *(tx.Tx()))
|
||||
txs := []txWrite{}
|
||||
for i := 0; i < len(l2txs); i++ {
|
||||
f := new(big.Float).SetInt(l2txs[i].Amount)
|
||||
amountFloat, _ := f.Float64()
|
||||
txs = append(txs, txWrite{
|
||||
// Generic
|
||||
IsL1: false,
|
||||
TxID: l2txs[i].TxID,
|
||||
Type: l2txs[i].Type,
|
||||
Position: l2txs[i].Position,
|
||||
FromIdx: &l2txs[i].FromIdx,
|
||||
ToIdx: l2txs[i].ToIdx,
|
||||
Amount: l2txs[i].Amount,
|
||||
AmountFloat: amountFloat,
|
||||
BatchNum: &l2txs[i].BatchNum,
|
||||
EthBlockNum: l2txs[i].EthBlockNum,
|
||||
// L2
|
||||
Fee: &l2txs[i].Fee,
|
||||
Nonce: &l2txs[i].Nonce,
|
||||
})
|
||||
}
|
||||
return hdb.addTxs(d, txs)
|
||||
}
|
||||
|
||||
func (hdb *HistoryDB) addTxs(d meddler.DB, txs []common.Tx) error {
|
||||
func (hdb *HistoryDB) addTxs(d meddler.DB, txs []txWrite) error {
|
||||
return db.BulkInsert(
|
||||
d,
|
||||
`INSERT INTO tx (
|
||||
@@ -379,7 +419,6 @@ func (hdb *HistoryDB) addTxs(d meddler.DB, txs []common.Tx) error {
|
||||
amount,
|
||||
amount_f,
|
||||
token_id,
|
||||
amount_usd,
|
||||
batch_num,
|
||||
eth_block_num,
|
||||
to_forge_l1_txs_num,
|
||||
@@ -388,25 +427,23 @@ func (hdb *HistoryDB) addTxs(d meddler.DB, txs []common.Tx) error {
|
||||
from_bjj,
|
||||
load_amount,
|
||||
load_amount_f,
|
||||
load_amount_usd,
|
||||
fee,
|
||||
fee_usd,
|
||||
nonce
|
||||
) VALUES %s;`,
|
||||
txs[:],
|
||||
)
|
||||
}
|
||||
|
||||
// GetTxs returns a list of txs from the DB
|
||||
func (hdb *HistoryDB) GetTxs() ([]common.Tx, error) {
|
||||
var txs []*common.Tx
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &txs,
|
||||
`SELECT * FROM tx
|
||||
ORDER BY (batch_num, position) ASC`,
|
||||
)
|
||||
return db.SlicePtrsToSlice(txs).([]common.Tx), err
|
||||
}
|
||||
// // GetTxs returns a list of txs from the DB
|
||||
// func (hdb *HistoryDB) GetTxs() ([]common.Tx, error) {
|
||||
// var txs []*common.Tx
|
||||
// err := meddler.QueryAll(
|
||||
// hdb.db, &txs,
|
||||
// `SELECT * FROM tx
|
||||
// ORDER BY (batch_num, position) ASC`,
|
||||
// )
|
||||
// return db.SlicePtrsToSlice(txs).([]common.Tx), err
|
||||
// }
|
||||
|
||||
// GetHistoryTxs returns a list of txs from the DB using the HistoryTx struct
|
||||
func (hdb *HistoryDB) GetHistoryTxs(
|
||||
@@ -419,7 +456,10 @@ func (hdb *HistoryDB) GetHistoryTxs(
|
||||
}
|
||||
var query string
|
||||
var args []interface{}
|
||||
queryStr := `SELECT tx.*, token.token_id, token.eth_block_num AS token_block,
|
||||
queryStr := `SELECT tx.is_l1, tx.id, tx.type, tx.position, tx.from_idx, tx.to_idx,
|
||||
tx.amount, tx.token_id, tx.batch_num, tx.eth_block_num, tx.to_forge_l1_txs_num,
|
||||
tx.user_origin, tx.from_eth_addr, tx.from_bjj, tx.load_amount, tx.fee, tx.nonce,
|
||||
token.token_id, token.eth_block_num AS token_block,
|
||||
token.eth_addr, token.name, token.symbol, token.decimals, token.usd,
|
||||
token.usd_update, block.timestamp, count(*) OVER() AS total_items
|
||||
FROM tx
|
||||
@@ -512,15 +552,15 @@ func (hdb *HistoryDB) GetHistoryTxs(
|
||||
return txs, txs[0].TotalItems, nil
|
||||
}
|
||||
|
||||
// GetTx returns a tx from the DB
|
||||
func (hdb *HistoryDB) GetTx(txID common.TxID) (*common.Tx, error) {
|
||||
tx := new(common.Tx)
|
||||
return tx, meddler.QueryRow(
|
||||
hdb.db, tx,
|
||||
"SELECT * FROM tx WHERE id = $1;",
|
||||
txID,
|
||||
)
|
||||
}
|
||||
// // GetTx returns a tx from the DB
|
||||
// func (hdb *HistoryDB) GetTx(txID common.TxID) (*common.Tx, error) {
|
||||
// tx := new(common.Tx)
|
||||
// return tx, meddler.QueryRow(
|
||||
// hdb.db, tx,
|
||||
// "SELECT * FROM tx WHERE id = $1;",
|
||||
// txID,
|
||||
// )
|
||||
// }
|
||||
|
||||
// // GetL1UserTxs gets L1 User Txs to be forged in a batch that will create an account
|
||||
// // TODO: This is currently not used. Figure out if it should be used somewhere or removed.
|
||||
|
||||
@@ -3,7 +3,6 @@ package historydb
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
dbUtils "github.com/hermeznetwork/hermez-node/db"
|
||||
@@ -157,12 +156,8 @@ func TestTokens(t *testing.T) {
|
||||
assert.Equal(t, tokens[i].EthAddr, token.EthAddr)
|
||||
assert.Equal(t, tokens[i].Name, token.Name)
|
||||
assert.Equal(t, tokens[i].Symbol, token.Symbol)
|
||||
assert.Equal(t, tokens[i].USD, token.USD)
|
||||
if token.USDUpdate != nil {
|
||||
assert.Greater(t, int64(1*time.Second), int64(time.Since(*token.USDUpdate)))
|
||||
} else {
|
||||
assert.Equal(t, tokens[i].USDUpdate, token.USDUpdate)
|
||||
}
|
||||
assert.Nil(t, token.USD)
|
||||
assert.Nil(t, token.USDUpdate)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,6 +213,7 @@ func TestTxs(t *testing.T) {
|
||||
|
||||
/*
|
||||
Uncomment once the transaction generation is fixed
|
||||
!! Missing tests to check that historic USD is not set if USDUpdate is too old (24h) !!
|
||||
|
||||
// Generate fake L1 txs
|
||||
const nL1s = 64
|
||||
|
||||
@@ -20,18 +20,17 @@ type HistoryTx struct {
|
||||
FromIdx *common.Idx `meddler:"from_idx"`
|
||||
ToIdx common.Idx `meddler:"to_idx"`
|
||||
Amount *big.Int `meddler:"amount,bigint"`
|
||||
AmountFloat float64 `meddler:"amount_f"`
|
||||
HistoricUSD *float64 `meddler:"amount_usd"`
|
||||
BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
|
||||
EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
|
||||
// L1
|
||||
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
|
||||
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
|
||||
FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
|
||||
FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
|
||||
LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
|
||||
LoadAmountFloat *float64 `meddler:"load_amount_f"`
|
||||
HistoricLoadAmountUSD *float64 `meddler:"load_amount_usd"`
|
||||
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
|
||||
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
|
||||
FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
|
||||
FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
|
||||
LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
|
||||
// LoadAmountFloat *float64 `meddler:"load_amount_f"`
|
||||
HistoricLoadAmountUSD *float64 `meddler:"load_amount_usd"`
|
||||
// L2
|
||||
Fee *common.FeeSelector `meddler:"fee"`
|
||||
HistoricFeeUSD *float64 `meddler:"fee_usd"`
|
||||
@@ -48,3 +47,42 @@ type HistoryTx struct {
|
||||
TokenUSD *float64 `meddler:"usd"`
|
||||
TokenUSDUpdate *time.Time `meddler:"usd_update"`
|
||||
}
|
||||
|
||||
// txWrite is an representatiion that merges common.L1Tx and common.L2Tx
|
||||
// in order to perform inserts into tx table
|
||||
type txWrite struct {
|
||||
// Generic
|
||||
IsL1 bool `meddler:"is_l1"`
|
||||
TxID common.TxID `meddler:"id"`
|
||||
Type common.TxType `meddler:"type"`
|
||||
Position int `meddler:"position"`
|
||||
FromIdx *common.Idx `meddler:"from_idx"`
|
||||
ToIdx common.Idx `meddler:"to_idx"`
|
||||
Amount *big.Int `meddler:"amount,bigint"`
|
||||
AmountFloat float64 `meddler:"amount_f"`
|
||||
TokenID common.TokenID `meddler:"token_id"`
|
||||
BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
|
||||
EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
|
||||
// L1
|
||||
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
|
||||
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
|
||||
FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
|
||||
FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
|
||||
LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
|
||||
LoadAmountFloat *float64 `meddler:"load_amount_f"`
|
||||
// L2
|
||||
Fee *common.FeeSelector `meddler:"fee"`
|
||||
Nonce *common.Nonce `meddler:"nonce"`
|
||||
}
|
||||
|
||||
// TokenRead add USD info to common.Token
|
||||
type TokenRead struct {
|
||||
TokenID common.TokenID `json:"id" meddler:"token_id"`
|
||||
EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` // Ethereum block number in which this token was registered
|
||||
EthAddr ethCommon.Address `json:"ethereumAddress" meddler:"eth_addr"`
|
||||
Name string `json:"name" meddler:"name"`
|
||||
Symbol string `json:"symbol" meddler:"symbol"`
|
||||
Decimals uint64 `json:"decimals" meddler:"decimals"`
|
||||
USD *float64 `json:"USD" meddler:"usd"`
|
||||
USDUpdate *time.Time `json:"fiatUpdate" meddler:"usd_update,utctime"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user