mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 11:26:44 +01:00
Refactor api txs
This commit is contained in:
@@ -114,30 +114,41 @@ func (l2db *L2DB) AddTxTest(tx *common.PoolL2Tx) error {
|
||||
return meddler.Insert(l2db.db, "tx_pool", insertTx)
|
||||
}
|
||||
|
||||
// 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,
|
||||
// selectPoolTxAPI select part of queries to get PoolL2TxRead
|
||||
const selectPoolTxAPI = `SELECT tx_pool.tx_id, hez_idx(tx_pool.from_idx, token.symbol) AS from_idx, tx_pool.from_eth_addr,
|
||||
tx_pool.from_bjj, hez_idx(tx_pool.to_idx, token.symbol) AS 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.state, tx_pool.signature, tx_pool.timestamp, tx_pool.batch_num, hez_idx(tx_pool.rq_from_idx, token.symbol) AS rq_from_idx,
|
||||
hez_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,
|
||||
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
|
||||
token.item_id AS token_item_id, 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,
|
||||
const selectPoolTxCommon = `SELECT tx_pool.tx_id, from_idx, 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.state, tx_pool.signature, tx_pool.timestamp, rq_from_idx,
|
||||
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) (*PoolL2TxRead, error) {
|
||||
tx := new(PoolL2TxRead)
|
||||
// GetTx return the specified Tx in common.PoolL2Tx format
|
||||
func (l2db *L2DB) GetTx(txID common.TxID) (*common.PoolL2Tx, error) {
|
||||
tx := new(common.PoolL2Tx)
|
||||
return tx, meddler.QueryRow(
|
||||
l2db.db, tx,
|
||||
selectPoolTxRead+"WHERE tx_id = $1;",
|
||||
selectPoolTxCommon+"WHERE tx_id = $1;",
|
||||
txID,
|
||||
)
|
||||
}
|
||||
|
||||
// GetTxAPI return the specified Tx in PoolTxAPI format
|
||||
func (l2db *L2DB) GetTxAPI(txID common.TxID) (*PoolTxAPI, error) {
|
||||
tx := new(PoolTxAPI)
|
||||
return tx, meddler.QueryRow(
|
||||
l2db.db, tx,
|
||||
selectPoolTxAPI+"WHERE tx_id = $1;",
|
||||
txID,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"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"
|
||||
@@ -94,81 +93,11 @@ func TestAddTxTest(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
fetchedTx, err := l2DB.GetTx(tx.TxID)
|
||||
assert.NoError(t, err)
|
||||
assertReadTx(t, commonToRead(tx, tokens), fetchedTx)
|
||||
// assertReadTx(t, commonToRead(tx, tokens), fetchedTx)
|
||||
assertTx(t, tx, fetchedTx)
|
||||
}
|
||||
}
|
||||
|
||||
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.TokenWithUSD{}
|
||||
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())
|
||||
@@ -400,13 +329,13 @@ func TestReorg(t *testing.T) {
|
||||
err := l2DB.Reorg(lastValidBatch)
|
||||
assert.NoError(t, err)
|
||||
for _, id := range reorgedTxIDs {
|
||||
tx, err := l2DB.GetTx(id)
|
||||
tx, err := l2DB.GetTxAPI(id)
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, tx.BatchNum)
|
||||
assert.Equal(t, common.PoolL2TxStatePending, tx.State)
|
||||
}
|
||||
for _, id := range nonReorgedTxIDs {
|
||||
fetchedTx, err := l2DB.GetTx(id)
|
||||
fetchedTx, err := l2DB.GetTxAPI(id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, lastValidBatch, *fetchedTx.BatchNum)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package l2db
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/hermez-node/apitypes"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
)
|
||||
@@ -34,24 +36,26 @@ type PoolL2TxWrite struct {
|
||||
Type common.TxType `meddler:"tx_type"`
|
||||
}
|
||||
|
||||
// PoolL2TxRead represents a L2 Tx pool with extra metadata used by the API
|
||||
type PoolL2TxRead struct {
|
||||
// PoolTxAPI represents a L2 Tx pool with extra metadata used by the API
|
||||
type PoolTxAPI 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"`
|
||||
FromIdx apitypes.HezIdx `meddler:"from_idx"`
|
||||
FromEthAddr *apitypes.HezEthAddr `meddler:"from_eth_addr"`
|
||||
FromBJJ *apitypes.HezBJJ `meddler:"from_bjj"`
|
||||
ToIdx *apitypes.HezIdx `meddler:"to_idx"`
|
||||
ToEthAddr *apitypes.HezEthAddr `meddler:"to_eth_addr"`
|
||||
ToBJJ *apitypes.HezBJJ `meddler:"to_bjj"`
|
||||
Amount apitypes.BigIntStr `meddler:"amount"`
|
||||
Fee common.FeeSelector `meddler:"fee"`
|
||||
Nonce common.Nonce `meddler:"nonce"`
|
||||
State common.PoolL2TxState `meddler:"state"`
|
||||
Signature babyjub.SignatureComp `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"`
|
||||
RqFromIdx *apitypes.HezIdx `meddler:"rq_from_idx"`
|
||||
RqToIdx *apitypes.HezIdx `meddler:"rq_to_idx"`
|
||||
RqToEthAddr *apitypes.HezEthAddr `meddler:"rq_to_eth_addr"`
|
||||
RqToBJJ *apitypes.HezBJJ `meddler:"rq_to_bjj"`
|
||||
RqTokenID *common.TokenID `meddler:"rq_token_id"`
|
||||
RqAmount *big.Int `meddler:"rq_amount,bigintnull"`
|
||||
RqAmount *apitypes.BigIntStr `meddler:"rq_amount"`
|
||||
RqFee *common.FeeSelector `meddler:"rq_fee"`
|
||||
RqNonce *common.Nonce `meddler:"rq_nonce"`
|
||||
Type common.TxType `meddler:"tx_type"`
|
||||
@@ -60,6 +64,7 @@ type PoolL2TxRead struct {
|
||||
Timestamp time.Time `meddler:"timestamp,utctime"`
|
||||
TotalItems int `meddler:"total_items"`
|
||||
TokenID common.TokenID `meddler:"token_id"`
|
||||
TokenItemID int `meddler:"token_item_id"`
|
||||
TokenEthBlockNum int64 `meddler:"eth_block_num"`
|
||||
TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
|
||||
TokenName string `meddler:"name"`
|
||||
@@ -68,3 +73,44 @@ type PoolL2TxRead struct {
|
||||
TokenUSD *float64 `meddler:"usd"`
|
||||
TokenUSDUpdate *time.Time `meddler:"usd_update"`
|
||||
}
|
||||
|
||||
// MarshalJSON is used to neast some of the fields of PoolTxAPI
|
||||
// without the need of auxiliar structs
|
||||
func (tx PoolTxAPI) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(map[string]interface{}{
|
||||
"id": tx.TxID,
|
||||
"type": tx.Type,
|
||||
"fromAccountIndex": tx.FromIdx,
|
||||
"fromHezEthereumAddress": tx.FromEthAddr,
|
||||
"fromBJJ": tx.FromBJJ,
|
||||
"toAccountIndex": tx.ToIdx,
|
||||
"toHezEthereumAddress": tx.ToEthAddr,
|
||||
"toBjj": tx.ToBJJ,
|
||||
"amount": tx.Amount,
|
||||
"fee": tx.Fee,
|
||||
"nonce": tx.Nonce,
|
||||
"state": tx.State,
|
||||
"signature": tx.Signature,
|
||||
"timestamp": tx.Timestamp,
|
||||
"batchNum": tx.BatchNum,
|
||||
"requestFromAccountIndex": tx.RqFromIdx,
|
||||
"requestToAccountIndex": tx.RqToIdx,
|
||||
"requestToHezEthereumAddress": tx.RqToEthAddr,
|
||||
"requestToBJJ": tx.RqToBJJ,
|
||||
"requestTokenId": tx.RqTokenID,
|
||||
"requestAmount": tx.RqAmount,
|
||||
"requestFee": tx.RqFee,
|
||||
"requestNonce": tx.RqNonce,
|
||||
"token": map[string]interface{}{
|
||||
"id": tx.TokenID,
|
||||
"itemId": tx.TokenItemID,
|
||||
"ethereumBlockNum": tx.TokenEthBlockNum,
|
||||
"ethereumAddress": tx.TokenEthAddr,
|
||||
"name": tx.TokenName,
|
||||
"symbol": tx.TokenSymbol,
|
||||
"decimals": tx.TokenDecimals,
|
||||
"USD": tx.TokenUSD,
|
||||
"fiatUpdate": tx.TokenUSDUpdate,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user