From 37cefb00579402b2217f3b57f200ef4b7c4cd44a Mon Sep 17 00:00:00 2001 From: arnaucube Date: Tue, 4 Aug 2020 17:53:19 +0200 Subject: [PATCH] Add common> Idx parsers --- batchbuilder/batchbuilder.go | 11 ++++++++--- common/leaf.go | 9 ++++++--- common/tx.go | 28 ++++++++++++++++++++++++++-- common/tx_test.go | 23 +++++++++++++++++++++++ 4 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 common/tx_test.go diff --git a/batchbuilder/batchbuilder.go b/batchbuilder/batchbuilder.go index 26b4e41..1f37124 100644 --- a/batchbuilder/batchbuilder.go +++ b/batchbuilder/batchbuilder.go @@ -55,7 +55,9 @@ func (bb *BatchBuilder) Reset(batchNum int, idx uint64, fromSynchronizer bool) e return nil } -func (bb *BatchBuilder) BuildBatch(configBatch ConfigBatch, l1usertxs, l1coordinatortxs []common.L1Tx, l2txs []common.L2Tx, tokenIDs []common.TokenID) (*common.ZKInputs, error) { +type ZKInputs struct{} // TMP + +func (bb *BatchBuilder) BuildBatch(configBatch ConfigBatch, l1usertxs, l1coordinatortxs []common.L1Tx, l2txs []common.L2Tx, tokenIDs []common.TokenID) (*ZKInputs, error) { for _, tx := range l1usertxs { bb.processL1Tx(tx) @@ -123,7 +125,7 @@ func (bb *BatchBuilder) applyCreateLeaf(tx common.L1Tx) error { EthAddr: tx.FromEthAddr, } - v, err := leaf.Value() + v, err := leaf.HashValue() if err != nil { return err } @@ -133,7 +135,10 @@ func (bb *BatchBuilder) applyCreateLeaf(tx common.L1Tx) error { if err != nil { return err } - leafBytes := leaf.Bytes() + leafBytes, err := leaf.Bytes() + if err != nil { + return err + } dbTx.Put(v.Bytes(), leafBytes[:]) // Add k & v into the MT diff --git a/common/leaf.go b/common/leaf.go index 7bf60d6..b880ad5 100644 --- a/common/leaf.go +++ b/common/leaf.go @@ -66,10 +66,13 @@ func (l *Leaf) BigInts() ([5]*big.Int, error) { return e, nil } -// Value returns the value of the Leaf, which is the Poseidon hash of its *big.Int representation -func (l *Leaf) Value() (*big.Int, error) { +// HashValue returns the value of the Leaf, which is the Poseidon hash of its *big.Int representation +func (l *Leaf) HashValue() (*big.Int, error) { toHash := [poseidon.T]*big.Int{} - lBI := l.BigInts() + lBI, err := l.BigInts() + if err != nil { + return nil, err + } copy(toHash[:], lBI[:]) v, err := poseidon.Hash(toHash) diff --git a/common/tx.go b/common/tx.go index 58b2e20..393b9ef 100644 --- a/common/tx.go +++ b/common/tx.go @@ -1,14 +1,38 @@ package common import ( + "encoding/binary" "math/big" ) +// Idx represents the account Index in the MerkleTree +type Idx uint32 + +// Bytes returns a byte array representing the Idx +func (idx Idx) Bytes() []byte { + var b [4]byte + binary.LittleEndian.PutUint32(b[:], uint32(idx)) + return b[:] +} + +// BigInt returns a *big.Int representing the Idx +func (idx Idx) BigInt() *big.Int { + return big.NewInt(int64(idx)) +} + +// IdxFromBigInt converts a *big.Int to Idx type +func IdxFromBigInt(b *big.Int) (Idx, error) { + if b.Int64() > 4294967295 { // 2**32-1 + return 0, ErrNumOverflow + } + return Idx(uint32(b.Int64())), nil +} + // Tx is a struct that represents a Hermez network transaction type Tx struct { TxID TxID - FromIdx uint32 - ToIdx uint32 + FromIdx Idx // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit) + ToIdx Idx // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer TokenID TokenID Amount *big.Int Nonce uint64 // effective 48 bits used diff --git a/common/tx_test.go b/common/tx_test.go new file mode 100644 index 0000000..4851e5d --- /dev/null +++ b/common/tx_test.go @@ -0,0 +1,23 @@ +package common + +import ( + "math/big" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestIdx(t *testing.T) { + i := Idx(100) + assert.Equal(t, big.NewInt(100), i.BigInt()) + + i = Idx(uint32(4294967295)) + assert.Equal(t, "4294967295", i.BigInt().String()) + + b := big.NewInt(4294967296) + i, err := IdxFromBigInt(b) + assert.NotNil(t, err) + assert.Equal(t, ErrNumOverflow, err) + assert.Equal(t, Idx(0), i) + +}