mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Add transakcio set type define, add set load [...]
Add transakcio set type definition, add set loading, move transakcio to package, adapt branch to last master updates (fix compile due new common types & git conflicts). Update tests to pass the test, pending to adapt to new Transakcio interface.
This commit is contained in:
@@ -334,7 +334,9 @@ func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2
|
||||
s.zki.AuxToIdx[s.i] = auxToIdx.BigInt()
|
||||
}
|
||||
|
||||
s.zki.ToBJJAy[s.i] = tx.ToBJJ.Y
|
||||
if tx.ToBJJ != nil {
|
||||
s.zki.ToBJJAy[s.i] = tx.ToBJJ.Y
|
||||
}
|
||||
s.zki.ToEthAddr[s.i] = common.EthAddrToBigInt(tx.ToEthAddr)
|
||||
|
||||
s.zki.OnChain[s.i] = big.NewInt(0)
|
||||
@@ -353,7 +355,7 @@ func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2
|
||||
// if StateDB type==TypeSynchronizer, will need to add Nonce and
|
||||
// TokenID to the transaction
|
||||
if s.typ == TypeSynchronizer {
|
||||
acc, err := s.GetAccount(tx.ToIdx)
|
||||
acc, err := s.GetAccount(tx.FromIdx)
|
||||
if err != nil {
|
||||
return nil, nil, false, err
|
||||
}
|
||||
|
||||
@@ -1,50 +1,15 @@
|
||||
package statedb
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/hermez-node/test"
|
||||
"github.com/hermeznetwork/hermez-node/test/transakcio"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var debug = false
|
||||
|
||||
func TestProcessTxs(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", "tmpdb")
|
||||
require.Nil(t, err)
|
||||
|
||||
sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// generate test transactions from test.SetTest0 code
|
||||
parser := test.NewParser(strings.NewReader(test.SetTest0))
|
||||
instructions, err := parser.Parse()
|
||||
assert.Nil(t, err)
|
||||
|
||||
l1Txs, coordinatorL1Txs, poolL2Txs, _ := test.GenerateTestTxs(t, instructions)
|
||||
assert.Equal(t, 29, len(l1Txs[0]))
|
||||
assert.Equal(t, 0, len(coordinatorL1Txs[0]))
|
||||
assert.Equal(t, 21, len(poolL2Txs[0]))
|
||||
|
||||
// iterate for each batch
|
||||
for i := 0; i < len(l1Txs); i++ {
|
||||
// l2Txs := common.PoolL2TxsToL2Txs(poolL2Txs[i])
|
||||
|
||||
_, _, err := sdb.ProcessTxs(l1Txs[i], coordinatorL1Txs[i], poolL2Txs[i])
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
acc, err := sdb.GetAccount(common.Idx(256))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "23", acc.Balance.String())
|
||||
}
|
||||
|
||||
func TestProcessTxsSynchronizer(t *testing.T) {
|
||||
// TODO once TTGL is updated, use the blockchain L2Tx (not PoolL2Tx) for
|
||||
// the Synchronizer tests
|
||||
@@ -56,24 +21,22 @@ func TestProcessTxsSynchronizer(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
// generate test transactions from test.SetTest0 code
|
||||
parser := test.NewParser(strings.NewReader(test.SetTest0))
|
||||
instructions, err := parser.Parse()
|
||||
assert.Nil(t, err)
|
||||
tc := transakcio.NewTestContext(t)
|
||||
blocks := tc.GenerateBlocks(transakcio.SetBlockchain0)
|
||||
|
||||
l1Txs, coordinatorL1Txs, poolL2Txs, _ := test.GenerateTestTxs(t, instructions)
|
||||
assert.Equal(t, 29, len(l1Txs[0]))
|
||||
assert.Equal(t, 0, len(coordinatorL1Txs[0]))
|
||||
assert.Equal(t, 21, len(poolL2Txs[0]))
|
||||
assert.Equal(t, 5, len(l1Txs[1]))
|
||||
assert.Equal(t, 1, len(coordinatorL1Txs[1]))
|
||||
assert.Equal(t, 55, len(poolL2Txs[1]))
|
||||
assert.Equal(t, 10, len(l1Txs[2]))
|
||||
assert.Equal(t, 0, len(coordinatorL1Txs[2]))
|
||||
assert.Equal(t, 7, len(poolL2Txs[2]))
|
||||
assert.Equal(t, 29, len(blocks[0].Batches[0].L1UserTxs))
|
||||
assert.Equal(t, 0, len(blocks[0].Batches[0].L1CoordinatorTxs))
|
||||
assert.Equal(t, 21, len(blocks[0].Batches[0].L2Txs))
|
||||
assert.Equal(t, 1, len(blocks[0].Batches[1].L1UserTxs))
|
||||
assert.Equal(t, 1, len(blocks[0].Batches[1].L1CoordinatorTxs))
|
||||
assert.Equal(t, 59, len(blocks[0].Batches[1].L2Txs))
|
||||
assert.Equal(t, 9, len(blocks[0].Batches[2].L1UserTxs))
|
||||
assert.Equal(t, 0, len(blocks[0].Batches[2].L1CoordinatorTxs))
|
||||
assert.Equal(t, 8, len(blocks[0].Batches[2].L2Txs))
|
||||
|
||||
// use first batch
|
||||
// l2txs := common.PoolL2TxsToL2Txs(poolL2Txs[0])
|
||||
_, exitInfos, err := sdb.ProcessTxs(l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0])
|
||||
l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Batches[0].L2Txs)
|
||||
_, exitInfos, err := sdb.ProcessTxs(blocks[0].Batches[0].L1UserTxs, blocks[0].Batches[0].L1CoordinatorTxs, l2Txs)
|
||||
require.Nil(t, err)
|
||||
// TODO once TTGL is updated, add a check that a input poolL2Tx with
|
||||
// Nonce & TokenID =0, after ProcessTxs call has the expected value
|
||||
@@ -84,8 +47,8 @@ func TestProcessTxsSynchronizer(t *testing.T) {
|
||||
assert.Equal(t, "28", acc.Balance.String())
|
||||
|
||||
// use second batch
|
||||
// l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[1])
|
||||
_, exitInfos, err = sdb.ProcessTxs(l1Txs[1], coordinatorL1Txs[1], poolL2Txs[1])
|
||||
l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Batches[1].L2Txs)
|
||||
_, exitInfos, err = sdb.ProcessTxs(blocks[0].Batches[1].L1UserTxs, blocks[0].Batches[1].L1CoordinatorTxs, l2Txs)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 5, len(exitInfos))
|
||||
acc, err = sdb.GetAccount(common.Idx(256))
|
||||
@@ -93,15 +56,18 @@ func TestProcessTxsSynchronizer(t *testing.T) {
|
||||
assert.Equal(t, "48", acc.Balance.String())
|
||||
|
||||
// use third batch
|
||||
// l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[2])
|
||||
_, exitInfos, err = sdb.ProcessTxs(l1Txs[2], coordinatorL1Txs[2], poolL2Txs[2])
|
||||
l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Batches[2].L2Txs)
|
||||
_, exitInfos, err = sdb.ProcessTxs(blocks[0].Batches[2].L1UserTxs, blocks[0].Batches[2].L1CoordinatorTxs, l2Txs)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 1, len(exitInfos))
|
||||
acc, err = sdb.GetAccount(common.Idx(256))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "23", acc.Balance.String())
|
||||
assert.Equal(t, "73", acc.Balance.String())
|
||||
}
|
||||
|
||||
/*
|
||||
WIP
|
||||
|
||||
func TestProcessTxsBatchBuilder(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", "tmpdb")
|
||||
require.Nil(t, err)
|
||||
@@ -110,24 +76,22 @@ func TestProcessTxsBatchBuilder(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
// generate test transactions from test.SetTest0 code
|
||||
parser := test.NewParser(strings.NewReader(test.SetTest0))
|
||||
instructions, err := parser.Parse()
|
||||
assert.Nil(t, err)
|
||||
tc := transakcio.NewTestContext(t)
|
||||
blocks := tc.GenerateBlocks(transakcio.SetBlockchain0)
|
||||
|
||||
l1Txs, coordinatorL1Txs, poolL2Txs, _ := test.GenerateTestTxs(t, instructions)
|
||||
assert.Equal(t, 29, len(l1Txs[0]))
|
||||
assert.Equal(t, 0, len(coordinatorL1Txs[0]))
|
||||
assert.Equal(t, 21, len(poolL2Txs[0]))
|
||||
assert.Equal(t, 5, len(l1Txs[1]))
|
||||
assert.Equal(t, 1, len(coordinatorL1Txs[1]))
|
||||
assert.Equal(t, 55, len(poolL2Txs[1]))
|
||||
assert.Equal(t, 10, len(l1Txs[2]))
|
||||
assert.Equal(t, 0, len(coordinatorL1Txs[2]))
|
||||
assert.Equal(t, 7, len(poolL2Txs[2]))
|
||||
assert.Equal(t, 29, len(blocks[0].Batches[0].L1UserTxs))
|
||||
assert.Equal(t, 0, len(blocks[0].Batches[0].L1CoordinatorTxs))
|
||||
assert.Equal(t, 21, len(blocks[0].Batches[0].L2Txs))
|
||||
assert.Equal(t, 1, len(blocks[0].Batches[1].L1UserTxs))
|
||||
assert.Equal(t, 1, len(blocks[0].Batches[1].L1CoordinatorTxs))
|
||||
assert.Equal(t, 59, len(blocks[0].Batches[1].L2Txs))
|
||||
assert.Equal(t, 9, len(blocks[0].Batches[2].L1UserTxs))
|
||||
assert.Equal(t, 0, len(blocks[0].Batches[2].L1CoordinatorTxs))
|
||||
assert.Equal(t, 8, len(blocks[0].Batches[2].L2Txs))
|
||||
|
||||
// use first batch
|
||||
// l2txs := common.PoolL2TxsToL2Txs(poolL2Txs[0])
|
||||
_, exitInfos, err := sdb.ProcessTxs(l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0])
|
||||
l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Batches[0].L2Txs)
|
||||
_, exitInfos, err := sdb.ProcessTxs(blocks[0].Batches[0].L1UserTxs, blocks[0].Batches[0].L1CoordinatorTxs, l2Txs)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 0, len(exitInfos))
|
||||
acc, err := sdb.GetAccount(common.Idx(256))
|
||||
@@ -135,8 +99,8 @@ func TestProcessTxsBatchBuilder(t *testing.T) {
|
||||
assert.Equal(t, "28", acc.Balance.String())
|
||||
|
||||
// use second batch
|
||||
// l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[1])
|
||||
_, exitInfos, err = sdb.ProcessTxs(l1Txs[1], coordinatorL1Txs[1], poolL2Txs[1])
|
||||
l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Batches[1].L2Txs)
|
||||
_, exitInfos, err = sdb.ProcessTxs(blocks[0].Batches[1].L1UserTxs, blocks[0].Batches[1].L1CoordinatorTxs, l2Txs)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 5, len(exitInfos))
|
||||
acc, err = sdb.GetAccount(common.Idx(256))
|
||||
@@ -144,8 +108,8 @@ func TestProcessTxsBatchBuilder(t *testing.T) {
|
||||
assert.Equal(t, "48", acc.Balance.String())
|
||||
|
||||
// use third batch
|
||||
// l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[2])
|
||||
_, exitInfos, err = sdb.ProcessTxs(l1Txs[2], coordinatorL1Txs[2], poolL2Txs[2])
|
||||
l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Batches[2].L2Txs)
|
||||
_, exitInfos, err = sdb.ProcessTxs(blocks[0].Batches[2].L1UserTxs, blocks[0].Batches[2].L1CoordinatorTxs, l2Txs)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 1, len(exitInfos))
|
||||
acc, err = sdb.GetAccount(common.Idx(256))
|
||||
@@ -161,21 +125,21 @@ func TestZKInputsGeneration(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
// generate test transactions from test.SetTest0 code
|
||||
parser := test.NewParser(strings.NewReader(test.SetTest0))
|
||||
instructions, err := parser.Parse()
|
||||
assert.Nil(t, err)
|
||||
tc := transakcio.NewTestContext(t)
|
||||
blocks := tc.GenerateBlocks(transakcio.SetBlockchain0)
|
||||
assert.Equal(t, 29, len(blocks[0].Batches[0].L1UserTxs))
|
||||
assert.Equal(t, 0, len(blocks[0].Batches[0].L1CoordinatorTxs))
|
||||
assert.Equal(t, 21, len(blocks[0].Batches[0].L2Txs))
|
||||
|
||||
l1Txs, coordinatorL1Txs, poolL2Txs, _ := test.GenerateTestTxs(t, instructions)
|
||||
assert.Equal(t, 29, len(l1Txs[0]))
|
||||
assert.Equal(t, 0, len(coordinatorL1Txs[0]))
|
||||
assert.Equal(t, 21, len(poolL2Txs[0]))
|
||||
|
||||
zki, _, err := sdb.ProcessTxs(l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0])
|
||||
l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Batches[0].L2Txs)
|
||||
zki, _, err := sdb.ProcessTxs(blocks[0].Batches[0].L1UserTxs, blocks[0].Batches[0].L1CoordinatorTxs, l2Txs)
|
||||
require.Nil(t, err)
|
||||
|
||||
s, err := json.Marshal(zki)
|
||||
require.Nil(t, err)
|
||||
debug:=true
|
||||
if debug {
|
||||
fmt.Println(string(s))
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user