From 34de6412cf2c655445f4138f7a6aab864926e402 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Wed, 21 Oct 2020 17:02:20 +0200 Subject: [PATCH] Update Til with small fixes [...] Update Til with small fixes: - L2Tx TxID & Type correctness done after Idxs setted - Added PoolL2Tx signature for Exit type Common: - Add common.TxID Marshalers test --- common/tx_test.go | 18 ++++++++++++++++++ test/til/txs.go | 22 ++++++++++++++++------ test/til/txs_test.go | 3 +-- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/common/tx_test.go b/common/tx_test.go index e3c2c34..ff45af8 100644 --- a/common/tx_test.go +++ b/common/tx_test.go @@ -25,3 +25,21 @@ func TestTxIDScannerValue(t *testing.T) { assert.NoError(t, scan.Scan(fromDB)) assert.Equal(t, value, scan) } + +func TestTxIDMarshalers(t *testing.T) { + h := []byte("0x00000000000001e240004700") + var txid TxID + err := txid.UnmarshalText(h) + assert.Nil(t, err) + assert.Equal(t, h, []byte(txid.String())) + + h2, err := txid.MarshalText() + assert.Nil(t, err) + assert.Equal(t, h, h2) + + var txid2 TxID + err = txid2.UnmarshalText(h2) + assert.Nil(t, err) + assert.Equal(t, h2, []byte(txid2.String())) + assert.Equal(t, h, h2) +} diff --git a/test/til/txs.go b/test/til/txs.go index 4e7fac9..eb97906 100644 --- a/test/til/txs.go +++ b/test/til/txs.go @@ -198,11 +198,6 @@ func (tc *Context) GenerateBlocks(set string) ([]BlockData, error) { Fee: common.FeeSelector(inst.fee), Type: common.TxTypeTransfer, } - nTx, err := common.NewPoolL2Tx(tx.PoolL2Tx()) - if err != nil { - return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error()) - } - tx = nTx.L2Tx() tx.BatchNum = common.BatchNum(tc.currBatchNum) // when converted to PoolL2Tx BatchNum parameter is lost testTx := L2Tx{ lineNum: inst.lineNum, @@ -371,6 +366,9 @@ func (tc *Context) setIdxs() error { if testTx.L2Tx.Type == common.TxTypeTransfer { testTx.L2Tx.ToIdx = tc.Users[testTx.toIdxName].Accounts[testTx.tokenID].Idx } + // in case Type==Exit, ToIdx=1, already set at the + // GenerateBlocks main switch inside TxTypeExit case + nTx, err := common.NewL2Tx(&testTx.L2Tx) if err != nil { return fmt.Errorf("Line %d: %s", testTx.lineNum, err.Error()) @@ -466,7 +464,7 @@ func (tc *Context) GeneratePoolL2Txs(set string) ([]common.PoolL2Tx, error) { if err != nil { return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error()) } - sig := tc.Users[inst.to].BJJ.SignPoseidon(toSign) + sig := tc.Users[inst.from].BJJ.SignPoseidon(toSign) tx.Signature = sig.Compress() txs = append(txs, tx) @@ -480,6 +478,18 @@ func (tc *Context) GeneratePoolL2Txs(set string) ([]common.PoolL2Tx, error) { Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce, Type: common.TxTypeExit, } + nTx, err := common.NewPoolL2Tx(&tx) + if err != nil { + return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error()) + } + tx = *nTx + // perform signature and set it to tx.Signature + toSign, err := tx.HashToSign() + if err != nil { + return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error()) + } + sig := tc.Users[inst.from].BJJ.SignPoseidon(toSign) + tx.Signature = sig.Compress() txs = append(txs, tx) default: return nil, fmt.Errorf("Line %d: instruction type unrecognized: %s", inst.lineNum, inst.typ) diff --git a/test/til/txs_test.go b/test/til/txs_test.go index 7bab91c..66be038 100644 --- a/test/til/txs_test.go +++ b/test/til/txs_test.go @@ -148,8 +148,7 @@ func (tc *Context) checkL2TxParams(t *testing.T, tx common.L2Tx, typ common.TxTy assert.Equal(t, nonce, tx.Nonce) } -//nolint the test is broken, and this is done on purpose to avoid execution -func testGeneratePoolL2Txs(t *testing.T) { +func TestGeneratePoolL2Txs(t *testing.T) { set := ` Type: Blockchain RegisterToken(1)