mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
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:
@@ -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),
|
||||
|
||||
@@ -27,7 +27,7 @@ func CleanL2DB(db *sqlx.DB) {
|
||||
func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
|
||||
txs := make([]*common.PoolL2Tx, 0, n)
|
||||
privK := babyjub.NewRandPrivKey()
|
||||
for i := 0; i < n; i++ {
|
||||
for i := 256; i < 256+n; i++ {
|
||||
var state common.PoolL2TxState
|
||||
//nolint:gomnd
|
||||
if i%4 == 0 {
|
||||
@@ -54,7 +54,6 @@ func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
|
||||
*absFee = fee.Percentage() * *usd
|
||||
}
|
||||
tx := &common.PoolL2Tx{
|
||||
TxID: common.TxID(common.Hash([]byte(strconv.Itoa(i)))),
|
||||
FromIdx: common.Idx(i),
|
||||
ToIdx: common.Idx(i + 1),
|
||||
ToEthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
|
||||
@@ -72,6 +71,11 @@ func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
|
||||
AbsoluteFee: absFee,
|
||||
AbsoluteFeeUpdate: token.USDUpdate,
|
||||
}
|
||||
var err error
|
||||
tx, err = common.NewPoolL2Tx(tx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if i%2 == 0 { // Optional parameters: rq
|
||||
tx.RqFromIdx = common.Idx(i)
|
||||
tx.RqToIdx = common.Idx(i + 1)
|
||||
|
||||
10
test/txs.go
10
test/txs.go
@@ -62,8 +62,8 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]*common.L1Tx,
|
||||
var l1Txs [][]*common.L1Tx
|
||||
var coordinatorL1Txs [][]*common.L1Tx
|
||||
var poolL2Txs [][]*common.PoolL2Tx
|
||||
idx := 1
|
||||
for i, inst := range instructions.Instructions {
|
||||
idx := 256
|
||||
for _, inst := range instructions.Instructions {
|
||||
switch inst.Type {
|
||||
case common.TxTypeCreateAccountDeposit:
|
||||
tx := common.L1Tx{
|
||||
@@ -98,7 +98,6 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]*common.L1Tx,
|
||||
}
|
||||
|
||||
tx := common.PoolL2Tx{
|
||||
TxID: common.TxID([]byte{byte(i)}), // TODO this is for the moment, once TxID Hash is implemented use it
|
||||
FromIdx: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx,
|
||||
ToIdx: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Idx,
|
||||
ToEthAddr: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr,
|
||||
@@ -114,6 +113,11 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]*common.L1Tx,
|
||||
RqToBJJ: accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.Public(),
|
||||
Type: common.TxTypeTransfer,
|
||||
}
|
||||
nTx, err := common.NewPoolL2Tx(&tx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
tx = *nTx
|
||||
// perform signature and set it to tx.Signature
|
||||
toSign, err := tx.HashToSign()
|
||||
if err != nil {
|
||||
|
||||
@@ -38,19 +38,21 @@ func TestGenerateTestL2Txs(t *testing.T) {
|
||||
assert.Equal(t, 4, len(coordinatorL1txs[1]))
|
||||
assert.Equal(t, 6, len(l2txs[1]))
|
||||
|
||||
accounts := GenerateKeys(t, instructions.Accounts)
|
||||
|
||||
// l1txs
|
||||
assert.Equal(t, common.TxTypeCreateAccountDeposit, l1txs[0][0].Type)
|
||||
assert.Equal(t, "5bac784d938067d980a9d39bdd79bf84a0cbb296977c47cc30de2d5ce9229d2f", l1txs[0][0].FromBJJ.String())
|
||||
assert.Equal(t, "323ff10c28df37ecb787fe216e111db64aa7cfa2c517509fe0057ff08a10b30c", l1txs[0][1].FromBJJ.String())
|
||||
assert.Equal(t, "f3587ad5cc7414a47545770b6c75bc71930f63c491eb2294dde8b8a6670b8e96", l1txs[0][2].FromBJJ.String())
|
||||
assert.Equal(t, "750a24a874a81c6c6f8aaa168ff2ee88c58263fee9ddd96d9717bcffc809b027", l1txs[1][1].FromBJJ.String())
|
||||
assert.Equal(t, accounts["A1"].BJJ.Public().String(), l1txs[0][0].FromBJJ.String())
|
||||
assert.Equal(t, accounts["A2"].BJJ.Public().String(), l1txs[0][1].FromBJJ.String())
|
||||
assert.Equal(t, accounts["B1"].BJJ.Public().String(), l1txs[0][2].FromBJJ.String())
|
||||
assert.Equal(t, accounts["User13"].BJJ.Public().String(), l1txs[1][1].FromBJJ.String())
|
||||
|
||||
// l2txs
|
||||
assert.Equal(t, common.TxTypeTransfer, l2txs[0][0].Type)
|
||||
assert.Equal(t, common.Idx(1), l2txs[0][0].FromIdx)
|
||||
assert.Equal(t, common.Idx(3), l2txs[0][0].ToIdx)
|
||||
assert.Equal(t, "f3587ad5cc7414a47545770b6c75bc71930f63c491eb2294dde8b8a6670b8e96", l2txs[0][0].ToBJJ.String())
|
||||
assert.Equal(t, "0x6813Eb9362372EEF6200f3b1dbC3f819671cBA69", l2txs[0][0].ToEthAddr.Hex())
|
||||
assert.Equal(t, common.Idx(256), l2txs[0][0].FromIdx)
|
||||
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)
|
||||
assert.Equal(t, common.Nonce(1), l2txs[1][1].Nonce)
|
||||
assert.Equal(t, common.FeeSelector(1), l2txs[0][0].Fee)
|
||||
|
||||
Reference in New Issue
Block a user