From 75f068baa8d1b2b9149fb5a3d8311cf5f20c351e Mon Sep 17 00:00:00 2001 From: Eduard S Date: Tue, 12 Jan 2021 18:46:33 +0100 Subject: [PATCH 1/2] Fix ChainID in zkInputs empty TxCmpData Test TxProcessor zkInputs against a real proof server --- common/pooll2tx.go | 10 ++ prover/prover.go | 2 +- test/zkproof/zkproof_test.go | 182 +++++++++++++++++++++++++++++++++++ txprocessor/txprocessor.go | 3 +- 4 files changed, 195 insertions(+), 2 deletions(-) create mode 100644 test/zkproof/zkproof_test.go diff --git a/common/pooll2tx.go b/common/pooll2tx.go index 89133cb..e675228 100644 --- a/common/pooll2tx.go +++ b/common/pooll2tx.go @@ -168,6 +168,16 @@ func (tx *PoolL2Tx) TxCompressedData(chainID uint16) (*big.Int, error) { return bi, nil } +// TxCompressedDataEmpty calculates the TxCompressedData of an empty +// transaction +func TxCompressedDataEmpty(chainID uint16) *big.Int { + var b [31]byte + binary.BigEndian.PutUint16(b[25:27], chainID) + copy(b[27:31], SignatureConstantBytes[:]) + bi := new(big.Int).SetBytes(b[:]) + return bi +} + // TxCompressedDataV2 spec: // [ 1 bits ] toBJJSign // 1 byte // [ 8 bits ] userFee // 1 byte diff --git a/prover/prover.go b/prover/prover.go index 51e75bb..5e12a0f 100644 --- a/prover/prover.go +++ b/prover/prover.go @@ -256,7 +256,7 @@ func (p *ProofServerClient) GetProof(ctx context.Context) (*Proof, []*big.Int, e } return &proof, pubInputs, nil } - return nil, nil, tracerr.Wrap(fmt.Errorf("status != StatusCodeSuccess, status = %v", status.Status)) + return nil, nil, tracerr.Wrap(fmt.Errorf("status != %v, status = %v", StatusCodeSuccess, status.Status)) } // Cancel cancels any current proof computation diff --git a/test/zkproof/zkproof_test.go b/test/zkproof/zkproof_test.go new file mode 100644 index 0000000..a5003d2 --- /dev/null +++ b/test/zkproof/zkproof_test.go @@ -0,0 +1,182 @@ +package zkproof + +import ( + "context" + "encoding/hex" + "encoding/json" + "fmt" + "io/ioutil" + "math/big" + "os" + "strconv" + "testing" + "time" + + ethCommon "github.com/ethereum/go-ethereum/common" + "github.com/hermeznetwork/hermez-node/common" + "github.com/hermeznetwork/hermez-node/db/statedb" + "github.com/hermeznetwork/hermez-node/prover" + "github.com/hermeznetwork/hermez-node/test/til" + "github.com/hermeznetwork/hermez-node/txprocessor" + "github.com/iden3/go-iden3-crypto/babyjub" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var proofServerURL string + +const pollInterval = 200 * time.Millisecond + +func TestMain(m *testing.M) { + exitVal := 0 + proofServerURL = os.Getenv("PROOF_SERVER_URL") + if proofServerURL != "" { + exitVal = m.Run() + } + os.Exit(exitVal) +} + +func generateJsUsers(t *testing.T) []til.User { + // same values than in the js test + // skJsHex is equivalent to the 0000...000i js private key in commonjs + skJsHex := []string{"7eb258e61862aae75c6c1d1f7efae5006ffc9e4d5596a6ff95f3df4ea209ea7f", "c005700f76f4b4cec710805c21595688648524df0a9d467afae537b7a7118819", "b373d14c67fb2a517bf4ac831c93341eec8e1b38dbc14e7d725b292a7cf84707", "2064b68d04a7aaae0ac3b36bf6f1850b380f1423be94a506c531940bd4a48b76"} + addrHex := []string{"0x7e5f4552091a69125d5dfcb7b8c2659029395bdf", "0x2b5ad5c4795c026514f8317c7a215e218dccd6cf", "0x6813eb9362372eef6200f3b1dbc3f819671cba69", "0x1eff47bc3a10a45d4b230b5d10e37751fe6aa718"} + var users []til.User + for i := 0; i < len(skJsHex); i++ { + skJs, err := hex.DecodeString(skJsHex[i]) + require.NoError(t, err) + var sk babyjub.PrivateKey + copy(sk[:], skJs) + // bjj := sk.Public() + user := til.User{ + Name: strconv.Itoa(i), + BJJ: &sk, + Addr: ethCommon.HexToAddress(addrHex[i]), + } + users = append(users, user) + } + assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", users[0].BJJ.Public().String()) + assert.Equal(t, "4d05c307400c65795f02db96b1b81c60386fd53e947d9d3f749f3d99b1853909", users[1].BJJ.Public().String()) + assert.Equal(t, "38ffa002724562eb2a952a2503e206248962406cf16392ff32759b6f2a41fe11", users[2].BJJ.Public().String()) + assert.Equal(t, "c719e6401190be7fa7fbfcd3448fe2755233c01575341a3b09edadf5454f760b", users[3].BJJ.Public().String()) + + return users +} + +func signL2Tx(t *testing.T, chainID uint16, user til.User, l2Tx common.PoolL2Tx) common.PoolL2Tx { + toSign, err := l2Tx.HashToSign(chainID) + require.NoError(t, err) + sig := user.BJJ.SignPoseidon(toSign) + l2Tx.Signature = sig.Compress() + return l2Tx +} + +const MaxTx = 376 +const NLevels = 32 +const MaxL1Tx = 256 +const MaxFeeTx = 64 +const ChainID uint16 = 1 + +func TestTxProcessor(t *testing.T) { + dir, err := ioutil.TempDir("", "tmpdb") + require.NoError(t, err) + defer assert.Nil(t, os.RemoveAll(dir)) + + sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeBatchBuilder, NLevels) + require.NoError(t, err) + + // same values than in the js test + users := generateJsUsers(t) + + l1Txs := []common.L1Tx{ + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[0].BJJ.Public().Compress(), + FromEthAddr: users[0].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[1].BJJ.Public().Compress(), + FromEthAddr: users[1].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 257, + DepositAmount: big.NewInt(0), + Amount: big.NewInt(1000), + TokenID: 1, + FromBJJ: users[1].BJJ.Public().Compress(), + FromEthAddr: users[1].Addr, + ToIdx: 1, + Type: common.TxTypeForceExit, + UserOrigin: true, + }, + } + l2Txs := []common.PoolL2Tx{ + { + FromIdx: 256, + ToIdx: 257, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 0, + Fee: 126, + Type: common.TxTypeTransfer, + }, + { + FromIdx: 256, + ToIdx: 1, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 1, + Fee: 126, + Type: common.TxTypeExit, + }, + } + + l2Txs[0] = signL2Tx(t, ChainID, users[0], l2Txs[0]) + l2Txs[1] = signL2Tx(t, ChainID, users[0], l2Txs[1]) + + config := txprocessor.Config{ + NLevels: uint32(NLevels), + MaxTx: MaxTx, + MaxL1Tx: MaxL1Tx, + MaxFeeTx: MaxFeeTx, + ChainID: ChainID, + } + tp := txprocessor.NewTxProcessor(sdb, config) + + // skip first batch to do the test with BatchNum=1 + _, err = tp.ProcessTxs(nil, nil, nil, nil) + require.NoError(t, err) + + coordIdxs := []common.Idx{257} + ptOut, err := tp.ProcessTxs(coordIdxs, l1Txs, nil, l2Txs) + require.NoError(t, err) + + // Store zkinputs json for debugging purposes + zkInputsJSON, err := json.Marshal(ptOut.ZKInputs) + require.NoError(t, err) + err = ioutil.WriteFile("/tmp/dbgZKInputs.json", zkInputsJSON, 0640) //nolint:gosec + require.NoError(t, err) + + proofServerClient := prover.NewProofServerClient(proofServerURL, pollInterval) + err = proofServerClient.WaitReady(context.Background()) + require.NoError(t, err) + err = proofServerClient.CalculateProof(context.Background(), ptOut.ZKInputs) + require.NoError(t, err) + proof, pubInputs, err := proofServerClient.GetProof(context.Background()) + require.NoError(t, err) + fmt.Printf("proof: %#v\n", proof) + fmt.Printf("pubInputs: %#v\n", pubInputs) +} diff --git a/txprocessor/txprocessor.go b/txprocessor/txprocessor.go index 49ceb77..59ba023 100644 --- a/txprocessor/txprocessor.go +++ b/txprocessor/txprocessor.go @@ -294,6 +294,7 @@ func (tp *TxProcessor) ProcessTxs(coordIdxs []common.Idx, l1usertxs, l1coordinat } } + txCompressedDataEmpty := common.TxCompressedDataEmpty(tp.config.ChainID) if tp.zki != nil { last := tp.i - 1 if tp.i == 0 { @@ -307,7 +308,7 @@ func (tp *TxProcessor) ProcessTxs(coordIdxs []common.Idx, l1usertxs, l1coordinat tp.zki.ISExitRoot[i] = exitTree.Root().BigInt() } if i >= tp.i { - tp.zki.TxCompressedData[i] = new(big.Int).SetBytes(common.SignatureConstantBytes) + tp.zki.TxCompressedData[i] = txCompressedDataEmpty } } isFinalAccFee := formatAccumulatedFees(collectedFees, tp.zki.FeePlanTokens, coordIdxs) From a16ec1750e789025ce9acd70bd0ee34893a41bc5 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Wed, 13 Jan 2021 14:01:13 +0100 Subject: [PATCH 2/2] Abstract txs generation for ZKInputs tests - TxProcessor move txCompressedDataEmpty inside the if of tp.zki!=nil - Abstract generation of transactions for ZKInput tests to avoid code repetition - used at txprocessor & test/zkproof tests --- api/api_test.go | 3 +- test/{til/sets.go => txsets/tilsets.go} | 2 +- .../sets_test.go => txsets/tilsets_test.go} | 17 +- test/txsets/txhelpers.go | 535 +++++++++++++++++ test/zkproof/zkproof_test.go | 110 +--- txprocessor/txprocessor.go | 2 +- txprocessor/txprocessor_test.go | 11 +- txprocessor/zkinputsgen_test.go | 537 +----------------- txselector/txselector_test.go | 5 +- 9 files changed, 580 insertions(+), 642 deletions(-) rename test/{til/sets.go => txsets/tilsets.go} (99%) rename test/{til/sets_test.go => txsets/tilsets_test.go} (55%) create mode 100644 test/txsets/txhelpers.go diff --git a/api/api_test.go b/api/api_test.go index 3e0ddd9..1c54916 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -25,6 +25,7 @@ import ( "github.com/hermeznetwork/hermez-node/log" "github.com/hermeznetwork/hermez-node/test" "github.com/hermeznetwork/hermez-node/test/til" + "github.com/hermeznetwork/hermez-node/test/txsets" "github.com/hermeznetwork/tracerr" ) @@ -269,7 +270,7 @@ func TestMain(m *testing.M) { } AddAditionalInformation(blocksData) // Generate L2 Txs with til - commonPoolTxs, err := tcc.GeneratePoolL2Txs(til.SetPoolL2MinimumFlow0) + commonPoolTxs, err := tcc.GeneratePoolL2Txs(txsets.SetPoolL2MinimumFlow0) if err != nil { panic(err) } diff --git a/test/til/sets.go b/test/txsets/tilsets.go similarity index 99% rename from test/til/sets.go rename to test/txsets/tilsets.go index 9dbf342..3a80ad8 100644 --- a/test/til/sets.go +++ b/test/txsets/tilsets.go @@ -1,4 +1,4 @@ -package til +package txsets // sets of instructions to be used in tests of other packages diff --git a/test/til/sets_test.go b/test/txsets/tilsets_test.go similarity index 55% rename from test/til/sets_test.go rename to test/txsets/tilsets_test.go index b735c00..1663451 100644 --- a/test/til/sets_test.go +++ b/test/txsets/tilsets_test.go @@ -1,23 +1,16 @@ -package til +package txsets import ( - "strings" "testing" "github.com/hermeznetwork/hermez-node/common" + "github.com/hermeznetwork/hermez-node/test/til" "github.com/stretchr/testify/assert" ) func TestCompileSetsBase(t *testing.T) { - parser := newParser(strings.NewReader(SetBlockchain0)) - _, err := parser.parse() - assert.NoError(t, err) - parser = newParser(strings.NewReader(SetPool0)) - _, err = parser.parse() - assert.NoError(t, err) - - tc := NewContext(0, common.RollupConstMaxL1UserTx) - _, err = tc.GenerateBlocks(SetBlockchain0) + tc := til.NewContext(0, common.RollupConstMaxL1UserTx) + _, err := tc.GenerateBlocks(SetBlockchain0) assert.NoError(t, err) _, err = tc.GeneratePoolL2Txs(SetPool0) assert.NoError(t, err) @@ -25,7 +18,7 @@ func TestCompileSetsBase(t *testing.T) { func TestCompileSetsMinimumFlow(t *testing.T) { // minimum flow - tc := NewContext(0, common.RollupConstMaxL1UserTx) + tc := til.NewContext(0, common.RollupConstMaxL1UserTx) _, err := tc.GenerateBlocks(SetBlockchainMinimumFlow0) assert.NoError(t, err) _, err = tc.GeneratePoolL2Txs(SetPoolL2MinimumFlow0) diff --git a/test/txsets/txhelpers.go b/test/txsets/txhelpers.go new file mode 100644 index 0000000..46a8755 --- /dev/null +++ b/test/txsets/txhelpers.go @@ -0,0 +1,535 @@ +// Package txsets contains Til sets of transactions & Transactions generation +// that are used at tests of other packages of hermez-node +//nolint:gomnd +package txsets + +import ( + "encoding/hex" + "math/big" + "strconv" + "testing" + + ethCommon "github.com/ethereum/go-ethereum/common" + "github.com/hermeznetwork/hermez-node/common" + "github.com/hermeznetwork/hermez-node/test/til" + "github.com/iden3/go-iden3-crypto/babyjub" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// The methods from this file are used at txprocessor package to test the +// ZKInputs generation & at tests of the test/zkproof to test the integration +// of the ZKInputs generation with the proof server + +// GenerateJsUsers generates the same values than in the js test +func GenerateJsUsers(t *testing.T) []til.User { + // same values than in the js test + // skJsHex is equivalent to the 0000...000i js private key in commonjs + skJsHex := []string{"7eb258e61862aae75c6c1d1f7efae5006ffc9e4d5596a6ff95f3df4ea209ea7f", "c005700f76f4b4cec710805c21595688648524df0a9d467afae537b7a7118819", "b373d14c67fb2a517bf4ac831c93341eec8e1b38dbc14e7d725b292a7cf84707", "2064b68d04a7aaae0ac3b36bf6f1850b380f1423be94a506c531940bd4a48b76"} + addrHex := []string{"0x7e5f4552091a69125d5dfcb7b8c2659029395bdf", "0x2b5ad5c4795c026514f8317c7a215e218dccd6cf", "0x6813eb9362372eef6200f3b1dbc3f819671cba69", "0x1eff47bc3a10a45d4b230b5d10e37751fe6aa718"} + var users []til.User + for i := 0; i < len(skJsHex); i++ { + skJs, err := hex.DecodeString(skJsHex[i]) + require.NoError(t, err) + var sk babyjub.PrivateKey + copy(sk[:], skJs) + // bjj := sk.Public() + user := til.User{ + Name: strconv.Itoa(i), + BJJ: &sk, + Addr: ethCommon.HexToAddress(addrHex[i]), + } + users = append(users, user) + } + assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", users[0].BJJ.Public().String()) + assert.Equal(t, "4d05c307400c65795f02db96b1b81c60386fd53e947d9d3f749f3d99b1853909", users[1].BJJ.Public().String()) + assert.Equal(t, "38ffa002724562eb2a952a2503e206248962406cf16392ff32759b6f2a41fe11", users[2].BJJ.Public().String()) + assert.Equal(t, "c719e6401190be7fa7fbfcd3448fe2755233c01575341a3b09edadf5454f760b", users[3].BJJ.Public().String()) + + return users +} + +func signL2Tx(t *testing.T, chainID uint16, user til.User, l2Tx common.PoolL2Tx) common.PoolL2Tx { + toSign, err := l2Tx.HashToSign(chainID) + require.NoError(t, err) + sig := user.BJJ.SignPoseidon(toSign) + l2Tx.Signature = sig.Compress() + return l2Tx +} + +// GenerateTxsZKInputsHash0 generates the transactions for the TestZKInputsHash0 +func GenerateTxsZKInputsHash0(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) { + // same values than in the js test + users = GenerateJsUsers(t) + + l1UserTxs = []common.L1Tx{ + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[0].BJJ.Public().Compress(), + FromEthAddr: users[0].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + } + l2Txs = []common.PoolL2Tx{ + { + FromIdx: 256, + ToIdx: 256, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 0, + Fee: 126, + Type: common.TxTypeTransfer, + }, + } + + l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0]) + + return users, []common.Idx{}, l1UserTxs, []common.L1Tx{}, l2Txs +} + +// GenerateTxsZKInputsHash1 generates the transactions for the TestZKInputsHash1 +func GenerateTxsZKInputsHash1(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) { + // same values than in the js test + users = GenerateJsUsers(t) + l1UserTxs = []common.L1Tx{ + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[0].BJJ.Public().Compress(), + FromEthAddr: users[0].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[1].BJJ.Public().Compress(), + FromEthAddr: users[1].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + } + l2Txs = []common.PoolL2Tx{ + { + FromIdx: 257, + ToIdx: 256, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 0, + Fee: 137, + Type: common.TxTypeTransfer, + }, + } + + l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0]) + + return users, []common.Idx{}, l1UserTxs, []common.L1Tx{}, l2Txs +} + +// GenerateTxsZKInputs0 generates the transactions for the TestZKInputs0 +func GenerateTxsZKInputs0(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) { + // same values than in the js test + users = GenerateJsUsers(t) + + l1UserTxs = []common.L1Tx{ + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[0].BJJ.Public().Compress(), + FromEthAddr: users[0].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + } + l2Txs = []common.PoolL2Tx{ + { + FromIdx: 256, + ToIdx: 256, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 0, + Fee: 0, + Type: common.TxTypeTransfer, + }, + } + + l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0]) + + return users, []common.Idx{}, l1UserTxs, []common.L1Tx{}, l2Txs +} + +// GenerateTxsZKInputs1 generates the transactions for the TestZKInputs1 +func GenerateTxsZKInputs1(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) { + // same values than in the js test + users = GenerateJsUsers(t) + + l1UserTxs = []common.L1Tx{ + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[0].BJJ.Public().Compress(), + FromEthAddr: users[0].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[1].BJJ.Public().Compress(), + FromEthAddr: users[1].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + } + l2Txs = []common.PoolL2Tx{ + { + FromIdx: 256, + ToIdx: 256, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 0, + Fee: 126, + Type: common.TxTypeTransfer, + }, + } + + l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0]) + + coordIdxs = []common.Idx{257} + return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs +} + +// GenerateTxsZKInputs2 generates the transactions for the TestZKInputs2 +func GenerateTxsZKInputs2(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) { + // same values than in the js test + users = GenerateJsUsers(t) + + l1UserTxs = []common.L1Tx{ + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[0].BJJ.Public().Compress(), + FromEthAddr: users[0].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[1].BJJ.Public().Compress(), + FromEthAddr: users[1].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[2].BJJ.Public().Compress(), + FromEthAddr: users[2].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[3].BJJ.Public().Compress(), + FromEthAddr: users[3].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + } + l2Txs = []common.PoolL2Tx{ + { + FromIdx: 256, + ToIdx: 258, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 0, + Fee: 126, + Type: common.TxTypeTransfer, + }, + { + FromIdx: 256, + ToIdx: 259, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 1, + Fee: 126, + Type: common.TxTypeTransfer, + }, + } + + l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0]) + l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1]) + + coordIdxs = []common.Idx{257} + return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs +} + +// GenerateTxsZKInputs3 generates the transactions for the TestZKInputs3 +func GenerateTxsZKInputs3(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) { + // same values than in the js test + users = GenerateJsUsers(t) + + l1UserTxs = []common.L1Tx{ + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[0].BJJ.Public().Compress(), + FromEthAddr: users[0].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[1].BJJ.Public().Compress(), + FromEthAddr: users[1].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[2].BJJ.Public().Compress(), + FromEthAddr: users[2].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(1000), + TokenID: 1, + FromBJJ: users[3].BJJ.Public().Compress(), + FromEthAddr: users[3].Addr, + ToIdx: 258, + Type: common.TxTypeCreateAccountDepositTransfer, + UserOrigin: true, + }, + } + l2Txs = []common.PoolL2Tx{ + { + FromIdx: 256, + ToIdx: 258, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 0, + Fee: 126, + Type: common.TxTypeTransfer, + }, + { + FromIdx: 256, + ToIdx: 259, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 1, + Fee: 126, + Type: common.TxTypeTransfer, + }, + } + + l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0]) + l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1]) + + coordIdxs = []common.Idx{257} + return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs +} + +// GenerateTxsZKInputs4 generates the transactions for the TestZKInputs4 +func GenerateTxsZKInputs4(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) { + // same values than in the js test + users = GenerateJsUsers(t) + + l1UserTxs = []common.L1Tx{ + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[0].BJJ.Public().Compress(), + FromEthAddr: users[0].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[1].BJJ.Public().Compress(), + FromEthAddr: users[1].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[2].BJJ.Public().Compress(), + FromEthAddr: users[2].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(1000), + TokenID: 1, + FromBJJ: users[3].BJJ.Public().Compress(), + FromEthAddr: users[3].Addr, + ToIdx: 258, + Type: common.TxTypeCreateAccountDepositTransfer, + UserOrigin: true, + }, + { + FromIdx: 258, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(1000), + TokenID: 1, + FromEthAddr: users[2].Addr, + ToIdx: 259, + Type: common.TxTypeDepositTransfer, + UserOrigin: true, + }, + } + l2Txs = []common.PoolL2Tx{ + { + FromIdx: 256, + ToIdx: 258, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 0, + Fee: 126, + Type: common.TxTypeTransfer, + }, + { + FromIdx: 256, + ToIdx: 259, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 1, + Fee: 126, + Type: common.TxTypeTransfer, + }, + } + + l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0]) + l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1]) + + coordIdxs = []common.Idx{257} + return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs +} + +// GenerateTxsZKInputs5 generates the transactions for the TestZKInputs5 +func GenerateTxsZKInputs5(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) { + // same values than in the js test + users = GenerateJsUsers(t) + + l1UserTxs = []common.L1Tx{ + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[0].BJJ.Public().Compress(), + FromEthAddr: users[0].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 0, + DepositAmount: big.NewInt(16000000), + Amount: big.NewInt(0), + TokenID: 1, + FromBJJ: users[1].BJJ.Public().Compress(), + FromEthAddr: users[1].Addr, + ToIdx: 0, + Type: common.TxTypeCreateAccountDeposit, + UserOrigin: true, + }, + { + FromIdx: 257, + DepositAmount: big.NewInt(0), + Amount: big.NewInt(1000), + TokenID: 1, + FromBJJ: users[1].BJJ.Public().Compress(), + FromEthAddr: users[1].Addr, + ToIdx: 1, + Type: common.TxTypeForceExit, + UserOrigin: true, + }, + } + l2Txs = []common.PoolL2Tx{ + { + FromIdx: 256, + ToIdx: 257, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 0, + Fee: 126, + Type: common.TxTypeTransfer, + }, + { + FromIdx: 256, + ToIdx: 1, + TokenID: 1, + Amount: big.NewInt(1000), + Nonce: 1, + Fee: 126, + Type: common.TxTypeExit, + }, + } + + l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0]) + l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1]) + + coordIdxs = []common.Idx{257} + return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs +} diff --git a/test/zkproof/zkproof_test.go b/test/zkproof/zkproof_test.go index a5003d2..b18e97c 100644 --- a/test/zkproof/zkproof_test.go +++ b/test/zkproof/zkproof_test.go @@ -2,23 +2,17 @@ package zkproof import ( "context" - "encoding/hex" "encoding/json" "fmt" "io/ioutil" - "math/big" "os" - "strconv" "testing" "time" - ethCommon "github.com/ethereum/go-ethereum/common" - "github.com/hermeznetwork/hermez-node/common" "github.com/hermeznetwork/hermez-node/db/statedb" "github.com/hermeznetwork/hermez-node/prover" - "github.com/hermeznetwork/hermez-node/test/til" + "github.com/hermeznetwork/hermez-node/test/txsets" "github.com/hermeznetwork/hermez-node/txprocessor" - "github.com/iden3/go-iden3-crypto/babyjub" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -36,48 +30,13 @@ func TestMain(m *testing.M) { os.Exit(exitVal) } -func generateJsUsers(t *testing.T) []til.User { - // same values than in the js test - // skJsHex is equivalent to the 0000...000i js private key in commonjs - skJsHex := []string{"7eb258e61862aae75c6c1d1f7efae5006ffc9e4d5596a6ff95f3df4ea209ea7f", "c005700f76f4b4cec710805c21595688648524df0a9d467afae537b7a7118819", "b373d14c67fb2a517bf4ac831c93341eec8e1b38dbc14e7d725b292a7cf84707", "2064b68d04a7aaae0ac3b36bf6f1850b380f1423be94a506c531940bd4a48b76"} - addrHex := []string{"0x7e5f4552091a69125d5dfcb7b8c2659029395bdf", "0x2b5ad5c4795c026514f8317c7a215e218dccd6cf", "0x6813eb9362372eef6200f3b1dbc3f819671cba69", "0x1eff47bc3a10a45d4b230b5d10e37751fe6aa718"} - var users []til.User - for i := 0; i < len(skJsHex); i++ { - skJs, err := hex.DecodeString(skJsHex[i]) - require.NoError(t, err) - var sk babyjub.PrivateKey - copy(sk[:], skJs) - // bjj := sk.Public() - user := til.User{ - Name: strconv.Itoa(i), - BJJ: &sk, - Addr: ethCommon.HexToAddress(addrHex[i]), - } - users = append(users, user) - } - assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", users[0].BJJ.Public().String()) - assert.Equal(t, "4d05c307400c65795f02db96b1b81c60386fd53e947d9d3f749f3d99b1853909", users[1].BJJ.Public().String()) - assert.Equal(t, "38ffa002724562eb2a952a2503e206248962406cf16392ff32759b6f2a41fe11", users[2].BJJ.Public().String()) - assert.Equal(t, "c719e6401190be7fa7fbfcd3448fe2755233c01575341a3b09edadf5454f760b", users[3].BJJ.Public().String()) - - return users -} - -func signL2Tx(t *testing.T, chainID uint16, user til.User, l2Tx common.PoolL2Tx) common.PoolL2Tx { - toSign, err := l2Tx.HashToSign(chainID) - require.NoError(t, err) - sig := user.BJJ.SignPoseidon(toSign) - l2Tx.Signature = sig.Compress() - return l2Tx -} - const MaxTx = 376 const NLevels = 32 const MaxL1Tx = 256 const MaxFeeTx = 64 const ChainID uint16 = 1 -func TestTxProcessor(t *testing.T) { +func TestZKInputs5(t *testing.T) { dir, err := ioutil.TempDir("", "tmpdb") require.NoError(t, err) defer assert.Nil(t, os.RemoveAll(dir)) @@ -85,67 +44,7 @@ func TestTxProcessor(t *testing.T) { sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeBatchBuilder, NLevels) require.NoError(t, err) - // same values than in the js test - users := generateJsUsers(t) - - l1Txs := []common.L1Tx{ - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[0].BJJ.Public().Compress(), - FromEthAddr: users[0].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[1].BJJ.Public().Compress(), - FromEthAddr: users[1].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 257, - DepositAmount: big.NewInt(0), - Amount: big.NewInt(1000), - TokenID: 1, - FromBJJ: users[1].BJJ.Public().Compress(), - FromEthAddr: users[1].Addr, - ToIdx: 1, - Type: common.TxTypeForceExit, - UserOrigin: true, - }, - } - l2Txs := []common.PoolL2Tx{ - { - FromIdx: 256, - ToIdx: 257, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 0, - Fee: 126, - Type: common.TxTypeTransfer, - }, - { - FromIdx: 256, - ToIdx: 1, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 1, - Fee: 126, - Type: common.TxTypeExit, - }, - } - - l2Txs[0] = signL2Tx(t, ChainID, users[0], l2Txs[0]) - l2Txs[1] = signL2Tx(t, ChainID, users[0], l2Txs[1]) + _, coordIdxs, l1UserTxs, l1CoordTxs, l2Txs := txsets.GenerateTxsZKInputs5(t, ChainID) config := txprocessor.Config{ NLevels: uint32(NLevels), @@ -160,8 +59,7 @@ func TestTxProcessor(t *testing.T) { _, err = tp.ProcessTxs(nil, nil, nil, nil) require.NoError(t, err) - coordIdxs := []common.Idx{257} - ptOut, err := tp.ProcessTxs(coordIdxs, l1Txs, nil, l2Txs) + ptOut, err := tp.ProcessTxs(coordIdxs, l1UserTxs, l1CoordTxs, l2Txs) require.NoError(t, err) // Store zkinputs json for debugging purposes diff --git a/txprocessor/txprocessor.go b/txprocessor/txprocessor.go index 59ba023..56abdf6 100644 --- a/txprocessor/txprocessor.go +++ b/txprocessor/txprocessor.go @@ -294,8 +294,8 @@ func (tp *TxProcessor) ProcessTxs(coordIdxs []common.Idx, l1usertxs, l1coordinat } } - txCompressedDataEmpty := common.TxCompressedDataEmpty(tp.config.ChainID) if tp.zki != nil { + txCompressedDataEmpty := common.TxCompressedDataEmpty(tp.config.ChainID) last := tp.i - 1 if tp.i == 0 { last = 0 diff --git a/txprocessor/txprocessor_test.go b/txprocessor/txprocessor_test.go index 049b618..1386849 100644 --- a/txprocessor/txprocessor_test.go +++ b/txprocessor/txprocessor_test.go @@ -13,6 +13,7 @@ import ( "github.com/hermeznetwork/hermez-node/db/statedb" "github.com/hermeznetwork/hermez-node/log" "github.com/hermeznetwork/hermez-node/test/til" + "github.com/hermeznetwork/hermez-node/test/txsets" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -211,7 +212,7 @@ func TestProcessTxsBalances(t *testing.T) { chainID := uint16(0) // generate test transactions from test.SetBlockchain0 code tc := til.NewContext(chainID, common.RollupConstMaxL1UserTx) - blocks, err := tc.GenerateBlocks(til.SetBlockchainMinimumFlow0) + blocks, err := tc.GenerateBlocks(txsets.SetBlockchainMinimumFlow0) require.NoError(t, err) config := Config{ @@ -329,7 +330,7 @@ func TestProcessTxsBalances(t *testing.T) { assert.Equal(t, "10342681351319338354912862547249967104198317571995055517008223832276478908482", tp.s.MT.Root().BigInt().String()) // use Set of PoolL2 txs - poolL2Txs, err := tc.GeneratePoolL2Txs(til.SetPoolL2MinimumFlow1) + poolL2Txs, err := tc.GeneratePoolL2Txs(txsets.SetPoolL2MinimumFlow1) assert.NoError(t, err) _, err = tp.ProcessTxs(coordIdxs, []common.L1Tx{}, []common.L1Tx{}, poolL2Txs) @@ -357,7 +358,7 @@ func TestProcessTxsSynchronizer(t *testing.T) { chainID := uint16(0) // generate test transactions from test.SetBlockchain0 code tc := til.NewContext(chainID, common.RollupConstMaxL1UserTx) - blocks, err := tc.GenerateBlocks(til.SetBlockchain0) + blocks, err := tc.GenerateBlocks(txsets.SetBlockchain0) require.NoError(t, err) assert.Equal(t, 31, len(blocks[0].Rollup.L1UserTxs)) @@ -488,7 +489,7 @@ func TestProcessTxsBatchBuilder(t *testing.T) { chainID := uint16(0) // generate test transactions from test.SetBlockchain0 code tc := til.NewContext(chainID, common.RollupConstMaxL1UserTx) - blocks, err := tc.GenerateBlocks(til.SetBlockchain0) + blocks, err := tc.GenerateBlocks(txsets.SetBlockchain0) require.NoError(t, err) // Coordinator Idx where to send the fees @@ -627,7 +628,7 @@ func TestCreateAccountDepositMaxValue(t *testing.T) { sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeBatchBuilder, nLevels) assert.NoError(t, err) - users := generateJsUsers(t) + users := txsets.GenerateJsUsers(t) daMaxHex, err := hex.DecodeString("FFFF") require.NoError(t, err) diff --git a/txprocessor/zkinputsgen_test.go b/txprocessor/zkinputsgen_test.go index 319e331..b7b0968 100644 --- a/txprocessor/zkinputsgen_test.go +++ b/txprocessor/zkinputsgen_test.go @@ -5,17 +5,14 @@ import ( "encoding/json" "fmt" "io/ioutil" - "math/big" "os" - "strconv" "testing" - ethCommon "github.com/ethereum/go-ethereum/common" "github.com/hermeznetwork/hermez-node/common" "github.com/hermeznetwork/hermez-node/db/statedb" "github.com/hermeznetwork/hermez-node/log" "github.com/hermeznetwork/hermez-node/test/til" - "github.com/iden3/go-iden3-crypto/babyjub" + "github.com/hermeznetwork/hermez-node/test/txsets" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -39,41 +36,6 @@ func printZKInputs(t *testing.T, zki *common.ZKInputs) { fmt.Println("") } -func generateJsUsers(t *testing.T) []til.User { - // same values than in the js test - // skJsHex is equivalent to the 0000...000i js private key in commonjs - skJsHex := []string{"7eb258e61862aae75c6c1d1f7efae5006ffc9e4d5596a6ff95f3df4ea209ea7f", "c005700f76f4b4cec710805c21595688648524df0a9d467afae537b7a7118819", "b373d14c67fb2a517bf4ac831c93341eec8e1b38dbc14e7d725b292a7cf84707", "2064b68d04a7aaae0ac3b36bf6f1850b380f1423be94a506c531940bd4a48b76"} - addrHex := []string{"0x7e5f4552091a69125d5dfcb7b8c2659029395bdf", "0x2b5ad5c4795c026514f8317c7a215e218dccd6cf", "0x6813eb9362372eef6200f3b1dbc3f819671cba69", "0x1eff47bc3a10a45d4b230b5d10e37751fe6aa718"} - var users []til.User - for i := 0; i < len(skJsHex); i++ { - skJs, err := hex.DecodeString(skJsHex[i]) - require.NoError(t, err) - var sk babyjub.PrivateKey - copy(sk[:], skJs) - // bjj := sk.Public() - user := til.User{ - Name: strconv.Itoa(i), - BJJ: &sk, - Addr: ethCommon.HexToAddress(addrHex[i]), - } - users = append(users, user) - } - assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", users[0].BJJ.Public().String()) - assert.Equal(t, "4d05c307400c65795f02db96b1b81c60386fd53e947d9d3f749f3d99b1853909", users[1].BJJ.Public().String()) - assert.Equal(t, "38ffa002724562eb2a952a2503e206248962406cf16392ff32759b6f2a41fe11", users[2].BJJ.Public().String()) - assert.Equal(t, "c719e6401190be7fa7fbfcd3448fe2755233c01575341a3b09edadf5454f760b", users[3].BJJ.Public().String()) - - return users -} - -func signL2Tx(t *testing.T, chainID uint16, user til.User, l2Tx common.PoolL2Tx) common.PoolL2Tx { - toSign, err := l2Tx.HashToSign(chainID) - require.NoError(t, err) - sig := user.BJJ.SignPoseidon(toSign) - l2Tx.Signature = sig.Compress() - return l2Tx -} - func TestZKInputsHashTestVector0(t *testing.T) { dir, err := ioutil.TempDir("", "tmpdb") require.NoError(t, err) @@ -82,34 +44,9 @@ func TestZKInputsHashTestVector0(t *testing.T) { sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeBatchBuilder, 32) require.NoError(t, err) - // same values than in the js test - users := generateJsUsers(t) - l1Txs := []common.L1Tx{ - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[0].BJJ.Public().Compress(), - FromEthAddr: users[0].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - } - l2Txs := []common.PoolL2Tx{ - { - FromIdx: 256, - ToIdx: 256, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 0, - Fee: 126, - Type: common.TxTypeTransfer, - }, - } - chainID := uint16(0) + users, coordIdxs, l1UserTxs, l1CoordTxs, l2Txs := txsets.GenerateTxsZKInputsHash0(t, chainID) + config := Config{ NLevels: 32, MaxFeeTx: 8, @@ -119,7 +56,7 @@ func TestZKInputsHashTestVector0(t *testing.T) { } tp := NewTxProcessor(sdb, config) - ptOut, err := tp.ProcessTxs(nil, l1Txs, nil, l2Txs) + ptOut, err := tp.ProcessTxs(coordIdxs, l1UserTxs, l1CoordTxs, l2Txs) require.NoError(t, err) // check expected account keys values from tx inputs @@ -156,45 +93,9 @@ func TestZKInputsHashTestVector1(t *testing.T) { sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeBatchBuilder, 32) require.NoError(t, err) - // same values than in the js test - users := generateJsUsers(t) - l1Txs := []common.L1Tx{ - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[0].BJJ.Public().Compress(), - FromEthAddr: users[0].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[1].BJJ.Public().Compress(), - FromEthAddr: users[1].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - } - l2Txs := []common.PoolL2Tx{ - { - FromIdx: 257, - ToIdx: 256, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 0, - Fee: 137, - Type: common.TxTypeTransfer, - }, - } - chainID := uint16(0) + users, coordIdxs, l1UserTxs, l1CoordTxs, l2Txs := txsets.GenerateTxsZKInputsHash1(t, chainID) + config := Config{ NLevels: 32, MaxFeeTx: 8, @@ -204,7 +105,7 @@ func TestZKInputsHashTestVector1(t *testing.T) { } tp := NewTxProcessor(sdb, config) - ptOut, err := tp.ProcessTxs(nil, l1Txs, nil, l2Txs) + ptOut, err := tp.ProcessTxs(coordIdxs, l1UserTxs, l1CoordTxs, l2Txs) require.NoError(t, err) // check expected account keys values from tx inputs @@ -299,40 +200,9 @@ func TestZKInputsEmpty(t *testing.T) { // so that the state tree is not empty (same transactions as // TestZKInputs0) - // same values than in the js test - users := generateJsUsers(t) - - l1UserTxs = []common.L1Tx{ - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[0].BJJ.Public().Compress(), - FromEthAddr: users[0].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - } - l2Txs = []common.PoolL2Tx{ - { - FromIdx: 256, - ToIdx: 256, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 0, - Fee: 0, - Type: common.TxTypeTransfer, - }, - } - - toSign, err := l2Txs[0].HashToSign(tp.config.ChainID) - require.NoError(t, err) - sig := users[0].BJJ.SignPoseidon(toSign) - l2Txs[0].Signature = sig.Compress() + _, coordIdxs, l1UserTxs, l1CoordTxs, l2Txs = txsets.GenerateTxsZKInputs0(t, chainID) - ptOut, err = tp.ProcessTxs(nil, l1UserTxs, nil, l2Txs) + ptOut, err = tp.ProcessTxs(coordIdxs, l1UserTxs, l1CoordTxs, l2Txs) require.NoError(t, err) rootNonZero := sdb.MT.Root() @@ -399,39 +269,8 @@ func TestZKInputs0(t *testing.T) { sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeBatchBuilder, nLevels) require.NoError(t, err) - // same values than in the js test - users := generateJsUsers(t) - - l1Txs := []common.L1Tx{ - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[0].BJJ.Public().Compress(), - FromEthAddr: users[0].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - } - l2Txs := []common.PoolL2Tx{ - { - FromIdx: 256, - ToIdx: 256, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 0, - Fee: 0, - Type: common.TxTypeTransfer, - }, - } - chainID := uint16(0) - toSign, err := l2Txs[0].HashToSign(chainID) - require.NoError(t, err) - sig := users[0].BJJ.SignPoseidon(toSign) - l2Txs[0].Signature = sig.Compress() + users, coordIdxs, l1UserTxs, l1CoordTxs, l2Txs := txsets.GenerateTxsZKInputs0(t, chainID) config := Config{ NLevels: uint32(nLevels), @@ -442,7 +281,7 @@ func TestZKInputs0(t *testing.T) { } tp := NewTxProcessor(sdb, config) - ptOut, err := tp.ProcessTxs(nil, l1Txs, nil, l2Txs) + ptOut, err := tp.ProcessTxs(coordIdxs, l1UserTxs, l1CoordTxs, l2Txs) require.NoError(t, err) // check expected account keys values from tx inputs @@ -486,50 +325,8 @@ func TestZKInputs1(t *testing.T) { sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeBatchBuilder, nLevels) require.NoError(t, err) - // same values than in the js test - users := generateJsUsers(t) - - l1Txs := []common.L1Tx{ - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[0].BJJ.Public().Compress(), - FromEthAddr: users[0].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[1].BJJ.Public().Compress(), - FromEthAddr: users[1].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - } - l2Txs := []common.PoolL2Tx{ - { - FromIdx: 256, - ToIdx: 256, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 0, - Fee: 126, - Type: common.TxTypeTransfer, - }, - } - chainID := uint16(0) - toSign, err := l2Txs[0].HashToSign(chainID) - require.NoError(t, err) - sig := users[0].BJJ.SignPoseidon(toSign) - l2Txs[0].Signature = sig.Compress() + users, coordIdxs, l1UserTxs, l1CoordTxs, l2Txs := txsets.GenerateTxsZKInputs1(t, chainID) config := Config{ NLevels: uint32(nLevels), @@ -540,8 +337,7 @@ func TestZKInputs1(t *testing.T) { } tp := NewTxProcessor(sdb, config) - coordIdxs := []common.Idx{257} - ptOut, err := tp.ProcessTxs(coordIdxs, l1Txs, nil, l2Txs) + ptOut, err := tp.ProcessTxs(coordIdxs, l1UserTxs, l1CoordTxs, l2Txs) require.NoError(t, err) // check expected account keys values from tx inputs @@ -592,79 +388,8 @@ func TestZKInputs2(t *testing.T) { sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeBatchBuilder, nLevels) require.NoError(t, err) - // same values than in the js test - users := generateJsUsers(t) - - l1Txs := []common.L1Tx{ - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[0].BJJ.Public().Compress(), - FromEthAddr: users[0].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[1].BJJ.Public().Compress(), - FromEthAddr: users[1].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[2].BJJ.Public().Compress(), - FromEthAddr: users[2].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[3].BJJ.Public().Compress(), - FromEthAddr: users[3].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - } - l2Txs := []common.PoolL2Tx{ - { - FromIdx: 256, - ToIdx: 258, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 0, - Fee: 126, - Type: common.TxTypeTransfer, - }, - { - FromIdx: 256, - ToIdx: 259, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 1, - Fee: 126, - Type: common.TxTypeTransfer, - }, - } - chainID := uint16(0) - l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0]) - l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1]) + users, coordIdxs, l1UserTxs, l1CoordTxs, l2Txs := txsets.GenerateTxsZKInputs2(t, chainID) config := Config{ NLevels: uint32(nLevels), @@ -675,8 +400,7 @@ func TestZKInputs2(t *testing.T) { } tp := NewTxProcessor(sdb, config) - coordIdxs := []common.Idx{257} - ptOut, err := tp.ProcessTxs(coordIdxs, l1Txs, nil, l2Txs) + ptOut, err := tp.ProcessTxs(coordIdxs, l1UserTxs, l1CoordTxs, l2Txs) require.NoError(t, err) // check expected account keys values from tx inputs @@ -735,79 +459,8 @@ func TestZKInputs3(t *testing.T) { sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeBatchBuilder, nLevels) require.NoError(t, err) - // same values than in the js test - users := generateJsUsers(t) - - l1Txs := []common.L1Tx{ - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[0].BJJ.Public().Compress(), - FromEthAddr: users[0].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[1].BJJ.Public().Compress(), - FromEthAddr: users[1].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[2].BJJ.Public().Compress(), - FromEthAddr: users[2].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(1000), - TokenID: 1, - FromBJJ: users[3].BJJ.Public().Compress(), - FromEthAddr: users[3].Addr, - ToIdx: 258, - Type: common.TxTypeCreateAccountDepositTransfer, - UserOrigin: true, - }, - } - l2Txs := []common.PoolL2Tx{ - { - FromIdx: 256, - ToIdx: 258, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 0, - Fee: 126, - Type: common.TxTypeTransfer, - }, - { - FromIdx: 256, - ToIdx: 259, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 1, - Fee: 126, - Type: common.TxTypeTransfer, - }, - } - chainID := uint16(0) - l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0]) - l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1]) + users, coordIdxs, l1UserTxs, l1CoordTxs, l2Txs := txsets.GenerateTxsZKInputs3(t, chainID) config := Config{ NLevels: uint32(nLevels), @@ -818,8 +471,7 @@ func TestZKInputs3(t *testing.T) { } tp := NewTxProcessor(sdb, config) - coordIdxs := []common.Idx{257} - ptOut, err := tp.ProcessTxs(coordIdxs, l1Txs, nil, l2Txs) + ptOut, err := tp.ProcessTxs(coordIdxs, l1UserTxs, l1CoordTxs, l2Txs) require.NoError(t, err) // check expected account keys values from tx inputs @@ -878,89 +530,8 @@ func TestZKInputs4(t *testing.T) { sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeBatchBuilder, nLevels) require.NoError(t, err) - // same values than in the js test - users := generateJsUsers(t) - - l1Txs := []common.L1Tx{ - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[0].BJJ.Public().Compress(), - FromEthAddr: users[0].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[1].BJJ.Public().Compress(), - FromEthAddr: users[1].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[2].BJJ.Public().Compress(), - FromEthAddr: users[2].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(1000), - TokenID: 1, - FromBJJ: users[3].BJJ.Public().Compress(), - FromEthAddr: users[3].Addr, - ToIdx: 258, - Type: common.TxTypeCreateAccountDepositTransfer, - UserOrigin: true, - }, - { - FromIdx: 258, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(1000), - TokenID: 1, - FromEthAddr: users[2].Addr, - ToIdx: 259, - Type: common.TxTypeDepositTransfer, - UserOrigin: true, - }, - } - l2Txs := []common.PoolL2Tx{ - { - FromIdx: 256, - ToIdx: 258, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 0, - Fee: 126, - Type: common.TxTypeTransfer, - }, - { - FromIdx: 256, - ToIdx: 259, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 1, - Fee: 126, - Type: common.TxTypeTransfer, - }, - } - chainID := uint16(0) - l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0]) - l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1]) + users, coordIdxs, l1UserTxs, l1CoordTxs, l2Txs := txsets.GenerateTxsZKInputs4(t, chainID) config := Config{ NLevels: uint32(nLevels), @@ -971,8 +542,7 @@ func TestZKInputs4(t *testing.T) { } tp := NewTxProcessor(sdb, config) - coordIdxs := []common.Idx{257} - ptOut, err := tp.ProcessTxs(coordIdxs, l1Txs, nil, l2Txs) + ptOut, err := tp.ProcessTxs(coordIdxs, l1UserTxs, l1CoordTxs, l2Txs) require.NoError(t, err) // check expected account keys values from tx inputs @@ -1031,68 +601,8 @@ func TestZKInputs5(t *testing.T) { sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeBatchBuilder, nLevels) require.NoError(t, err) - // same values than in the js test - users := generateJsUsers(t) - - l1Txs := []common.L1Tx{ - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[0].BJJ.Public().Compress(), - FromEthAddr: users[0].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 0, - DepositAmount: big.NewInt(16000000), - Amount: big.NewInt(0), - TokenID: 1, - FromBJJ: users[1].BJJ.Public().Compress(), - FromEthAddr: users[1].Addr, - ToIdx: 0, - Type: common.TxTypeCreateAccountDeposit, - UserOrigin: true, - }, - { - FromIdx: 257, - DepositAmount: big.NewInt(0), - Amount: big.NewInt(1000), - TokenID: 1, - FromBJJ: users[1].BJJ.Public().Compress(), - FromEthAddr: users[1].Addr, - ToIdx: 1, - Type: common.TxTypeForceExit, - UserOrigin: true, - }, - } - l2Txs := []common.PoolL2Tx{ - { - FromIdx: 256, - ToIdx: 257, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 0, - Fee: 126, - Type: common.TxTypeTransfer, - }, - { - FromIdx: 256, - ToIdx: 1, - TokenID: 1, - Amount: big.NewInt(1000), - Nonce: 1, - Fee: 126, - Type: common.TxTypeExit, - }, - } - chainID := uint16(0) - l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0]) - l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1]) + users, coordIdxs, l1UserTxs, l1CoordTxs, l2Txs := txsets.GenerateTxsZKInputs5(t, chainID) config := Config{ NLevels: uint32(nLevels), @@ -1103,8 +613,7 @@ func TestZKInputs5(t *testing.T) { } tp := NewTxProcessor(sdb, config) - coordIdxs := []common.Idx{257} - ptOut, err := tp.ProcessTxs(coordIdxs, l1Txs, nil, l2Txs) + ptOut, err := tp.ProcessTxs(coordIdxs, l1UserTxs, l1CoordTxs, l2Txs) require.NoError(t, err) assert.Equal(t, "9004936174112171168716185012590576631374182232656264130522697453639057968430", sdb.MT.Root().BigInt().String()) @@ -1145,7 +654,7 @@ func TestZKInputs5(t *testing.T) { // TestZKInputs6: // Tests ZKInputs generated by the batches generated by -// til.SetBlockchainMinimumFlow0 +// txsets.SetBlockchainMinimumFlow0 func TestZKInputs6(t *testing.T) { dir, err := ioutil.TempDir("", "tmpdb") require.NoError(t, err) @@ -1166,7 +675,7 @@ func TestZKInputs6(t *testing.T) { tp := NewTxProcessor(sdb, config) tc := til.NewContext(0, common.RollupConstMaxL1UserTx) - blocks, err := tc.GenerateBlocks(til.SetBlockchainMinimumFlow0) + blocks, err := tc.GenerateBlocks(txsets.SetBlockchainMinimumFlow0) require.NoError(t, err) // restart nonces of TilContext, as will be set by generating directly diff --git a/txselector/txselector_test.go b/txselector/txselector_test.go index 8243590..9f1d443 100644 --- a/txselector/txselector_test.go +++ b/txselector/txselector_test.go @@ -20,6 +20,7 @@ import ( "github.com/hermeznetwork/hermez-node/log" "github.com/hermeznetwork/hermez-node/test" "github.com/hermeznetwork/hermez-node/test/til" + "github.com/hermeznetwork/hermez-node/test/txsets" "github.com/hermeznetwork/hermez-node/txprocessor" "github.com/iden3/go-iden3-crypto/babyjub" "github.com/jmoiron/sqlx" @@ -155,12 +156,12 @@ func checkSortedByNonce(t *testing.T, testAccNonces map[common.Idx]common.Nonce, func TestGetL2TxSelectionMinimumFlow0(t *testing.T) { chainID := uint16(0) hermezContractAddr := ethCommon.HexToAddress("0xc344E203a046Da13b0B4467EB7B3629D0C99F6E6") - txsel, tc := initTest(t, chainID, hermezContractAddr, til.SetPool0) + txsel, tc := initTest(t, chainID, hermezContractAddr, txsets.SetPool0) // generate test transactions, the L1CoordinatorTxs generated by Til // will be ignored at this test, as will be the TxSelector who // generates them when needed - blocks, err := tc.GenerateBlocks(til.SetBlockchainMinimumFlow0) + blocks, err := tc.GenerateBlocks(txsets.SetBlockchainMinimumFlow0) assert.NoError(t, err) // restart nonces of TilContext, as will be set by generating directly