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:
@@ -591,7 +591,7 @@ func (c *Client) CtlAddL1TxUser(l1Tx *common.L1Tx) {
|
||||
r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum] = eth.NewQueueStruct()
|
||||
queue = r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum]
|
||||
}
|
||||
if l1Tx.FromIdx != nil && int64(*l1Tx.FromIdx) > r.State.CurrentIdx {
|
||||
if int64(l1Tx.FromIdx) > r.State.CurrentIdx {
|
||||
panic("l1Tx.FromIdx > r.State.CurrentIdx")
|
||||
}
|
||||
if int(l1Tx.TokenID)+1 > len(r.State.TokenList) {
|
||||
|
||||
@@ -155,7 +155,7 @@ func TestClientRollup(t *testing.T) {
|
||||
for i := 0; i < N; i++ {
|
||||
keys[i] = genKeys(int64(i))
|
||||
l1UserTx := common.L1Tx{
|
||||
FromIdx: nil,
|
||||
FromIdx: 0,
|
||||
FromEthAddr: keys[i].Addr,
|
||||
FromBJJ: keys[i].BJJPublicKey,
|
||||
TokenID: common.TokenID(0),
|
||||
|
||||
@@ -3,7 +3,6 @@ package test
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
@@ -42,12 +41,6 @@ func GenTokens(nTokens int, blocks []common.Block) []common.Token {
|
||||
EthBlockNum: blocks[i%len(blocks)].EthBlockNum,
|
||||
EthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
|
||||
}
|
||||
if i%2 == 0 {
|
||||
usd := 3.0
|
||||
token.USD = &usd
|
||||
now := time.Now()
|
||||
token.USDUpdate = &now
|
||||
}
|
||||
tokens = append(tokens, token)
|
||||
}
|
||||
return tokens
|
||||
@@ -129,28 +122,14 @@ func GenL1Txs(
|
||||
_, nextTxsNum := GetNextToForgeNumAndBatch(batches)
|
||||
for i := fromIdx; i < fromIdx+totalTxs; i++ {
|
||||
token := tokens[i%len(tokens)]
|
||||
var usd *float64
|
||||
var lUSD *float64
|
||||
amount := big.NewInt(int64(i + 1))
|
||||
if token.USD != nil {
|
||||
//nolint:gomnd
|
||||
noDecimalsUSD := *token.USD / math.Pow(10, float64(token.Decimals))
|
||||
f := new(big.Float).SetInt(amount)
|
||||
af, _ := f.Float64()
|
||||
usd = new(float64)
|
||||
*usd = noDecimalsUSD * af
|
||||
lUSD = new(float64)
|
||||
*lUSD = noDecimalsUSD * af
|
||||
}
|
||||
tx := common.L1Tx{
|
||||
Position: i - fromIdx,
|
||||
UserOrigin: i%2 == 0,
|
||||
TokenID: token.TokenID,
|
||||
Amount: amount,
|
||||
USD: usd,
|
||||
LoadAmount: amount,
|
||||
LoadAmountUSD: lUSD,
|
||||
EthBlockNum: blocks[i%len(blocks)].EthBlockNum,
|
||||
Position: i - fromIdx,
|
||||
UserOrigin: i%2 == 0,
|
||||
TokenID: token.TokenID,
|
||||
Amount: amount,
|
||||
LoadAmount: amount,
|
||||
EthBlockNum: blocks[i%len(blocks)].EthBlockNum,
|
||||
}
|
||||
if tx.UserOrigin {
|
||||
n := nextTxsNum
|
||||
@@ -227,9 +206,7 @@ func setFromToAndAppend(
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
fromIdx := new(common.Idx)
|
||||
*fromIdx = from.Idx
|
||||
tx.FromIdx = fromIdx
|
||||
tx.FromIdx = from.Idx
|
||||
tx.FromEthAddr = from.EthAddr
|
||||
tx.FromBJJ = from.PublicKey
|
||||
tx.ToIdx = to.Idx
|
||||
@@ -243,9 +220,7 @@ func setFromToAndAppend(
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fromIdx := new(common.Idx)
|
||||
*fromIdx = from.Idx
|
||||
tx.FromIdx = fromIdx
|
||||
tx.FromIdx = from.Idx
|
||||
tx.FromEthAddr = from.EthAddr
|
||||
tx.FromBJJ = from.PublicKey
|
||||
tx.ToIdx = to.Idx
|
||||
@@ -318,21 +293,6 @@ func GenL2Txs(
|
||||
tx.ToIdx = to.Idx
|
||||
}
|
||||
|
||||
var usd *float64
|
||||
var fUSD *float64
|
||||
token := GetToken(tx.FromIdx, accounts, tokens)
|
||||
if token.USD != nil {
|
||||
//nolint:gomnd
|
||||
noDecimalsUSD := *token.USD / math.Pow(10, float64(token.Decimals))
|
||||
f := new(big.Float).SetInt(amount)
|
||||
af, _ := f.Float64()
|
||||
usd = new(float64)
|
||||
fUSD = new(float64)
|
||||
*usd = noDecimalsUSD * af
|
||||
*fUSD = *usd * fee.Percentage()
|
||||
}
|
||||
tx.USD = usd
|
||||
tx.FeeUSD = fUSD
|
||||
if i < nUserTxs {
|
||||
userTxs = append(userTxs, tx)
|
||||
} else {
|
||||
@@ -342,29 +302,6 @@ func GenL2Txs(
|
||||
return userTxs, othersTxs
|
||||
}
|
||||
|
||||
// GetToken returns the Token associated to an Idx given a list of tokens and accounts.
|
||||
// It panics when not found, intended for testing only.
|
||||
func GetToken(idx common.Idx, accs []common.Account, tokens []common.Token) common.Token {
|
||||
var id common.TokenID
|
||||
found := false
|
||||
for _, acc := range accs {
|
||||
if acc.Idx == idx {
|
||||
found = true
|
||||
id = acc.TokenID
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
panic("tokenID not found")
|
||||
}
|
||||
for i := 0; i < len(tokens); i++ {
|
||||
if tokens[i].TokenID == id {
|
||||
return tokens[i]
|
||||
}
|
||||
}
|
||||
panic("token not found")
|
||||
}
|
||||
|
||||
// GenCoordinators generates coordinators. WARNING: This is meant for DB/API testing, and may not be fully consistent with the protocol.
|
||||
func GenCoordinators(nCoords int, blocks []common.Block) []common.Coordinator {
|
||||
coords := []common.Coordinator{}
|
||||
|
||||
67
test/l2db.go
67
test/l2db.go
@@ -42,37 +42,19 @@ func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
|
||||
} else if i%4 == 3 {
|
||||
state = common.PoolL2TxStateForged
|
||||
}
|
||||
f := new(big.Float).SetInt(big.NewInt(int64(i)))
|
||||
amountF, _ := f.Float64()
|
||||
var usd, absFee *float64
|
||||
fee := common.FeeSelector(i % 255) //nolint:gomnd
|
||||
token := tokens[i%len(tokens)]
|
||||
if token.USD != nil {
|
||||
usd = new(float64)
|
||||
absFee = new(float64)
|
||||
*usd = *token.USD * amountF
|
||||
*absFee = fee.Percentage() * *usd
|
||||
}
|
||||
toIdx := new(common.Idx)
|
||||
*toIdx = common.Idx(i + 1)
|
||||
toEthAddr := new(ethCommon.Address)
|
||||
*toEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
|
||||
tx := &common.PoolL2Tx{
|
||||
FromIdx: common.Idx(i),
|
||||
ToIdx: toIdx,
|
||||
ToEthAddr: toEthAddr,
|
||||
ToBJJ: privK.Public(),
|
||||
TokenID: token.TokenID,
|
||||
Amount: big.NewInt(int64(i)),
|
||||
AmountFloat: amountF,
|
||||
USD: usd,
|
||||
Fee: fee,
|
||||
Nonce: common.Nonce(i),
|
||||
State: state,
|
||||
Signature: privK.SignPoseidon(big.NewInt(int64(i))),
|
||||
Timestamp: time.Now().UTC(),
|
||||
AbsoluteFee: absFee,
|
||||
AbsoluteFeeUpdate: token.USDUpdate,
|
||||
FromIdx: common.Idx(i),
|
||||
ToIdx: common.Idx(i + 1),
|
||||
ToEthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
|
||||
ToBJJ: privK.Public(),
|
||||
TokenID: token.TokenID,
|
||||
Amount: big.NewInt(int64(i)),
|
||||
Fee: fee,
|
||||
Nonce: common.Nonce(i),
|
||||
State: state,
|
||||
Signature: privK.SignPoseidon(big.NewInt(int64(i))),
|
||||
}
|
||||
var err error
|
||||
tx, err = common.NewPoolL2Tx(tx)
|
||||
@@ -80,31 +62,14 @@ func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
|
||||
panic(err)
|
||||
}
|
||||
if i%2 == 0 { // Optional parameters: rq
|
||||
rqFromIdx := new(common.Idx)
|
||||
*rqFromIdx = common.Idx(i)
|
||||
tx.RqFromIdx = rqFromIdx
|
||||
rqToIdx := new(common.Idx)
|
||||
*rqToIdx = common.Idx(i + 1)
|
||||
tx.RqToIdx = rqToIdx
|
||||
rqToEthAddr := new(ethCommon.Address)
|
||||
*rqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
|
||||
tx.RqToEthAddr = rqToEthAddr
|
||||
tx.RqFromIdx = common.Idx(i)
|
||||
tx.RqToIdx = common.Idx(i + 1)
|
||||
tx.RqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
|
||||
tx.RqToBJJ = privK.Public()
|
||||
rqTokenID := new(common.TokenID)
|
||||
*rqTokenID = common.TokenID(i)
|
||||
tx.RqTokenID = rqTokenID
|
||||
tx.RqTokenID = common.TokenID(i)
|
||||
tx.RqAmount = big.NewInt(int64(i))
|
||||
rqFee := new(common.FeeSelector)
|
||||
*rqFee = common.FeeSelector(i)
|
||||
tx.RqFee = rqFee
|
||||
rqNonce := new(uint64)
|
||||
*rqNonce = uint64(i)
|
||||
tx.RqNonce = rqNonce
|
||||
}
|
||||
if i%3 == 0 { // Optional parameters: things that get updated "a posteriori"
|
||||
batchNum := new(common.BatchNum)
|
||||
*batchNum = 489
|
||||
tx.BatchNum = batchNum
|
||||
tx.RqFee = common.FeeSelector(i)
|
||||
tx.RqNonce = uint64(i)
|
||||
}
|
||||
txs = append(txs, tx)
|
||||
}
|
||||
|
||||
19
test/txs.go
19
test/txs.go
@@ -6,7 +6,6 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
ethCrypto "github.com/ethereum/go-ethereum/crypto"
|
||||
@@ -97,25 +96,17 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]common.L1Tx,
|
||||
batchCoordinatorL1Txs = append(batchCoordinatorL1Txs, tx)
|
||||
idx++
|
||||
}
|
||||
toIdx := new(common.Idx)
|
||||
*toIdx = accounts[idxTokenIDToString(inst.To, inst.TokenID)].Idx
|
||||
toEthAddr := new(ethCommon.Address)
|
||||
*toEthAddr = accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr
|
||||
rqToEthAddr := new(ethCommon.Address)
|
||||
*rqToEthAddr = accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr
|
||||
tx := common.PoolL2Tx{
|
||||
FromIdx: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx,
|
||||
ToIdx: toIdx,
|
||||
ToEthAddr: toEthAddr,
|
||||
ToIdx: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Idx,
|
||||
ToEthAddr: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr,
|
||||
ToBJJ: accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.Public(),
|
||||
TokenID: inst.TokenID,
|
||||
Amount: big.NewInt(int64(inst.Amount)),
|
||||
Fee: common.FeeSelector(inst.Fee),
|
||||
Nonce: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Nonce,
|
||||
State: common.PoolL2TxStatePending,
|
||||
Timestamp: time.Now(),
|
||||
BatchNum: nil,
|
||||
RqToEthAddr: rqToEthAddr,
|
||||
RqToEthAddr: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr,
|
||||
RqToBJJ: accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.Public(),
|
||||
Type: common.TxTypeTransfer,
|
||||
}
|
||||
@@ -136,10 +127,8 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]common.L1Tx,
|
||||
batchPoolL2Txs = append(batchPoolL2Txs, tx)
|
||||
|
||||
case common.TxTypeExit, common.TxTypeForceExit:
|
||||
fromIdx := new(common.Idx)
|
||||
*fromIdx = accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx
|
||||
tx := common.L1Tx{
|
||||
FromIdx: fromIdx,
|
||||
FromIdx: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx,
|
||||
ToIdx: common.Idx(1), // as is an Exit
|
||||
TokenID: inst.TokenID,
|
||||
Amount: big.NewInt(int64(inst.Amount)),
|
||||
|
||||
@@ -50,7 +50,7 @@ func TestGenerateTestL2Txs(t *testing.T) {
|
||||
// l2txs
|
||||
assert.Equal(t, common.TxTypeTransfer, l2txs[0][0].Type)
|
||||
assert.Equal(t, common.Idx(256), l2txs[0][0].FromIdx)
|
||||
assert.Equal(t, common.Idx(258), *l2txs[0][0].ToIdx)
|
||||
assert.Equal(t, common.Idx(258), l2txs[0][0].ToIdx)
|
||||
assert.Equal(t, accounts["B1"].BJJ.Public().String(), l2txs[0][0].ToBJJ.String())
|
||||
assert.Equal(t, accounts["B1"].Addr.Hex(), l2txs[0][0].ToEthAddr.Hex())
|
||||
assert.Equal(t, common.Nonce(0), l2txs[0][0].Nonce)
|
||||
|
||||
Reference in New Issue
Block a user