Add TxID calculation & New{Layer}Tx Type

Add TxID calculation & New{Layer}Tx Type

New{Layer}Tx methods that compute the `TxID` & `TxType` values from the
transaction values:
- NewL1Tx
- NewL2Tx
- NewPoolL2Tx

Add TxID Scanner & Valuer for database/sql

HistoryDB & L2DB & API packages tests will need to be addapted to the
TestTransaction generation once is done.
This commit is contained in:
arnaucube
2020-09-30 14:05:51 +02:00
parent c6f70f3177
commit 9bb4a4ec1b
21 changed files with 422 additions and 131 deletions

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"math"
"math/big"
"strconv"
"time"
ethCommon "github.com/ethereum/go-ethereum/common"
@@ -88,10 +87,10 @@ func GenAccounts(totalAccounts, userAccounts int, tokens []common.Token, userAdd
panic("totalAccounts must be greater than userAccounts")
}
accs := []common.Account{}
for i := 0; i < totalAccounts; i++ {
for i := 256; i < 256+totalAccounts; i++ {
var addr ethCommon.Address
var pubK *babyjub.PublicKey
if i < userAccounts {
if i < 256+userAccounts {
addr = *userAddr
pubK = userBjj
} else {
@@ -126,7 +125,7 @@ func GenL1Txs(
userTxs := []common.L1Tx{}
othersTxs := []common.L1Tx{}
_, nextTxsNum := GetNextToForgeNumAndBatch(batches)
for i := 0; i < totalTxs; i++ {
for i := fromIdx; i < fromIdx+totalTxs; i++ {
token := tokens[i%len(tokens)]
var usd *float64
var lUSD *float64
@@ -142,8 +141,7 @@ func GenL1Txs(
*lUSD = noDecimalsUSD * af
}
tx := common.L1Tx{
TxID: common.TxID(common.Hash([]byte("L1_" + strconv.Itoa(fromIdx+i)))),
Position: i,
Position: i - fromIdx,
UserOrigin: i%2 == 0,
TokenID: token.TokenID,
Amount: amount,
@@ -151,17 +149,21 @@ func GenL1Txs(
LoadAmount: amount,
LoadAmountUSD: lUSD,
EthBlockNum: blocks[i%len(blocks)].EthBlockNum,
Type: randomTxType(i),
}
nTx, err := common.NewL1Tx(&tx)
if err != nil {
panic(err)
}
tx = *nTx
if batches[i%len(batches)].ForgeL1TxsNum != 0 {
// Add already forged txs
tx.BatchNum = &batches[i%len(batches)].BatchNum
setFromToAndAppend(tx, i, nUserTxs, userAddr, accounts, &userTxs, &othersTxs)
setFromToAndAppend(fromIdx, tx, i, nUserTxs, userAddr, accounts, &userTxs, &othersTxs)
} else {
// Add unforged txs
tx.ToForgeL1TxsNum = nextTxsNum
tx.UserOrigin = true
setFromToAndAppend(tx, i, nUserTxs, userAddr, accounts, &userTxs, &othersTxs)
setFromToAndAppend(fromIdx, tx, i, nUserTxs, userAddr, accounts, &userTxs, &othersTxs)
}
}
return userTxs, othersTxs
@@ -186,6 +188,7 @@ func GetNextToForgeNumAndBatch(batches []common.Batch) (common.BatchNum, int64)
}
func setFromToAndAppend(
fromIdx int,
tx common.L1Tx,
i, nUserTxs int,
userAddr *ethCommon.Address,
@@ -193,7 +196,7 @@ func setFromToAndAppend(
userTxs *[]common.L1Tx,
othersTxs *[]common.L1Tx,
) {
if i < nUserTxs {
if i < fromIdx+nUserTxs {
var from, to *common.Account
var err error
if i%2 == 0 {
@@ -252,13 +255,13 @@ func GenL2Txs(
}
userTxs := []common.L2Tx{}
othersTxs := []common.L2Tx{}
for i := 0; i < totalTxs; i++ {
for i := fromIdx; i < fromIdx+totalTxs; i++ {
amount := big.NewInt(int64(i + 1))
fee := common.FeeSelector(i % 256) //nolint:gomnd
tx := common.L2Tx{
TxID: common.TxID(common.Hash([]byte("L2_" + strconv.Itoa(fromIdx+i)))),
TxID: common.TxID([12]byte{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, byte(i)}), // only for testing purposes
BatchNum: batches[i%len(batches)].BatchNum,
Position: i,
Position: i - fromIdx,
Amount: amount,
Fee: fee,
Nonce: common.Nonce(i + 1),