Merge pull request #128 from hermeznetwork/feature/l1txbytes

Update common.L1Tx parsers, add test checks
This commit is contained in:
Eduard S
2020-09-21 15:40:49 +02:00
committed by GitHub
11 changed files with 180 additions and 153 deletions

View File

@@ -37,7 +37,7 @@ 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))
binary.BigEndian.PutUint32(b[:], uint32(idx))
return b[:]
}
@@ -51,7 +51,7 @@ func IdxFromBytes(b []byte) (Idx, error) {
if len(b) != idxBytesLen {
return 0, fmt.Errorf("can not parse Idx, bytes len %d, expected 4", len(b))
}
idx := binary.LittleEndian.Uint32(b[:4])
idx := binary.BigEndian.Uint32(b[:4])
return Idx(idx), nil
}
@@ -84,7 +84,10 @@ func (a *Account) String() string {
return buf.String()
}
// Bytes returns the bytes representing the Account, in a way that each BigInt is represented by 32 bytes, in spite of the BigInt could be represented in less bytes (due a small big.Int), so in this way each BigInt is always 32 bytes and can be automatically parsed from a byte array.
// Bytes returns the bytes representing the Account, in a way that each BigInt
// is represented by 32 bytes, in spite of the BigInt could be represented in
// less bytes (due a small big.Int), so in this way each BigInt is always 32
// bytes and can be automatically parsed from a byte array.
func (a *Account) Bytes() ([32 * NLeafElems]byte, error) {
var b [32 * NLeafElems]byte
@@ -105,7 +108,7 @@ func (a *Account) Bytes() ([32 * NLeafElems]byte, error) {
if babyjub.PointCoordSign(a.PublicKey.X) {
b[10] = 1
}
copy(b[32:64], SwapEndianness(a.Balance.Bytes())) // SwapEndianness, as big.Int uses BigEndian
copy(b[32:64], SwapEndianness(a.Balance.Bytes()))
copy(b[64:96], SwapEndianness(a.PublicKey.Y.Bytes()))
copy(b[96:116], a.EthAddr.Bytes())
@@ -159,7 +162,10 @@ func AccountFromBigInts(e [NLeafElems]*big.Int) (*Account, error) {
// AccountFromBytes returns a Account from a byte array
func AccountFromBytes(b [32 * NLeafElems]byte) (*Account, error) {
tokenID := binary.LittleEndian.Uint32(b[0:4])
tokenID, err := TokenIDFromBytes(b[0:4])
if err != nil {
return nil, err
}
var nonceBytes5 [5]byte
copy(nonceBytes5[:], b[4:9])
nonce := NonceFromBytes(nonceBytes5)

View File

@@ -106,7 +106,7 @@ func TestAccountHashValue(t *testing.T) {
v, err := account.HashValue()
assert.Nil(t, err)
assert.Equal(t, "16085711911723375585301279875451049849443101031421093098714359651259271023730", v.String())
assert.Equal(t, "9478468711598093334066833736294178928569163287501434518121324135729106649559", v.String())
}
func TestAccountErrNotInFF(t *testing.T) {

129
common/float16.go Normal file
View File

@@ -0,0 +1,129 @@
// Package common Float16 provides methods to work with Hermez custom half float
// precision, 16 bits, codification internally called Float16 has been adopted
// to encode large integers. This is done in order to save bits when L2
// transactions are published.
//nolint:gomnd
package common
import (
"encoding/binary"
"errors"
"math/big"
)
var (
// ErrRoundingLoss is used when converted big.Int to Float16 causes rounding loss
ErrRoundingLoss = errors.New("input value causes rounding loss")
)
// Float16 represents a float in a 16 bit format
type Float16 uint16
// Bytes return a byte array of length 2 with the Float16 value encoded in BigEndian
func (f16 Float16) Bytes() []byte {
var b [2]byte
binary.BigEndian.PutUint16(b[:], uint16(f16))
return b[:]
}
// Float16FromBytes returns a Float16 from a byte array of 2 bytes.
func Float16FromBytes(b []byte) *Float16 {
f16 := Float16(binary.BigEndian.Uint16(b[:2]))
return &f16
}
// BigInt converts the Float16 to a *big.Int integer
func (f16 *Float16) BigInt() *big.Int {
fl := int64(*f16)
m := big.NewInt(fl & 0x3FF)
e := big.NewInt(fl >> 11)
e5 := (fl >> 10) & 0x01
exp := big.NewInt(0).Exp(big.NewInt(10), e, nil)
res := m.Mul(m, exp)
if e5 != 0 && e.Cmp(big.NewInt(0)) != 0 {
res.Add(res, exp.Div(exp, big.NewInt(2)))
}
return res
}
// floorFix2Float converts a fix to a float, always rounding down
func floorFix2Float(_f *big.Int) Float16 {
zero := big.NewInt(0)
ten := big.NewInt(10)
e := int64(0)
m := big.NewInt(0)
m.Set(_f)
if m.Cmp(zero) == 0 {
return 0
}
s := big.NewInt(0).Rsh(m, 10)
for s.Cmp(zero) != 0 {
m.Div(m, ten)
s.Rsh(m, 10)
e++
}
return Float16(m.Int64() | e<<11)
}
// NewFloat16 encodes a *big.Int integer as a Float16, returning error in
// case of loss during the encoding.
func NewFloat16(f *big.Int) (Float16, error) {
fl1 := floorFix2Float(f)
fi1 := fl1.BigInt()
fl2 := fl1 | 0x400
fi2 := fl2.BigInt()
m3 := (fl1 & 0x3FF) + 1
e3 := fl1 >> 11
if m3&0x400 == 0 {
m3 = 0x66
e3++
}
fl3 := m3 + e3<<11
fi3 := fl3.BigInt()
res := fl1
d := big.NewInt(0).Abs(fi1.Sub(fi1, f))
d2 := big.NewInt(0).Abs(fi2.Sub(fi2, f))
if d.Cmp(d2) == 1 {
res = fl2
d = d2
}
d3 := big.NewInt(0).Abs(fi3.Sub(fi3, f))
if d.Cmp(d3) == 1 {
res = fl3
}
// Do rounding check
if res.BigInt().Cmp(f) == 0 {
return res, nil
}
return res, ErrRoundingLoss
}
// NewFloat16Floor encodes a big.Int integer as a Float16, rounding down in
// case of loss during the encoding.
func NewFloat16Floor(f *big.Int) Float16 {
fl1 := floorFix2Float(f)
fl2 := fl1 | 0x400
fi2 := fl2.BigInt()
if fi2.Cmp(f) < 1 {
return fl2
}
return fl1
}

131
common/float16_test.go Normal file
View File

@@ -0,0 +1,131 @@
package common
import (
"math/big"
"testing"
"github.com/stretchr/testify/assert"
)
func TestConversions(t *testing.T) {
testVector := map[Float16]string{
0x307B: "123000000",
0x1DC6: "454500",
0xFFFF: "10235000000000000000000000000000000",
0x0000: "0",
0x0400: "0",
0x0001: "1",
0x0401: "1",
0x0800: "0",
0x0c00: "5",
0x0801: "10",
0x0c01: "15",
}
for test := range testVector {
fix := test.BigInt()
assert.Equal(t, fix.String(), testVector[test])
bi := big.NewInt(0)
bi.SetString(testVector[test], 10)
fl, err := NewFloat16(bi)
assert.Equal(t, nil, err)
fx2 := fl.BigInt()
assert.Equal(t, fx2.String(), testVector[test])
}
}
func TestFloorFix2Float(t *testing.T) {
testVector := map[string]Float16{
"87999990000000000": 0x776f,
"87950000000000001": 0x776f,
"87950000000000000": 0x776f,
"87949999999999999": 0x736f,
}
for test := range testVector {
bi := big.NewInt(0)
bi.SetString(test, 10)
testFloat := NewFloat16Floor(bi)
assert.Equal(t, testFloat, testVector[test])
}
}
func TestConversionLosses(t *testing.T) {
a := big.NewInt(1000)
b, err := NewFloat16(a)
assert.Equal(t, nil, err)
c := b.BigInt()
assert.Equal(t, c, a)
a = big.NewInt(1024)
b, err = NewFloat16(a)
assert.Equal(t, ErrRoundingLoss, err)
c = b.BigInt()
assert.NotEqual(t, c, a)
a = big.NewInt(32767)
b, err = NewFloat16(a)
assert.Equal(t, ErrRoundingLoss, err)
c = b.BigInt()
assert.NotEqual(t, c, a)
a = big.NewInt(32768)
b, err = NewFloat16(a)
assert.Equal(t, ErrRoundingLoss, err)
c = b.BigInt()
assert.NotEqual(t, c, a)
a = big.NewInt(65536000)
b, err = NewFloat16(a)
assert.Equal(t, ErrRoundingLoss, err)
c = b.BigInt()
assert.NotEqual(t, c, a)
}
func BenchmarkFloat16(b *testing.B) {
newBigInt := func(s string) *big.Int {
bigInt, ok := new(big.Int).SetString(s, 10)
if !ok {
panic("Bad big int")
}
return bigInt
}
type pair struct {
Float16 Float16
BigInt *big.Int
}
testVector := []pair{
{0x307B, newBigInt("123000000")},
{0x1DC6, newBigInt("454500")},
{0xFFFF, newBigInt("10235000000000000000000000000000000")},
{0x0000, newBigInt("0")},
{0x0400, newBigInt("0")},
{0x0001, newBigInt("1")},
{0x0401, newBigInt("1")},
{0x0800, newBigInt("0")},
{0x0c00, newBigInt("5")},
{0x0801, newBigInt("10")},
{0x0c01, newBigInt("15")},
}
b.Run("floorFix2Float()", func(b *testing.B) {
for i := 0; i < b.N; i++ {
NewFloat16Floor(testVector[i%len(testVector)].BigInt)
}
})
b.Run("NewFloat16()", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = NewFloat16(testVector[i%len(testVector)].BigInt)
}
})
b.Run("Float16.BigInt()", func(b *testing.B) {
for i := 0; i < b.N; i++ {
testVector[i%len(testVector)].Float16.BigInt()
}
})
}

View File

@@ -1,6 +1,7 @@
package common
import (
"fmt"
"math/big"
ethCommon "github.com/ethereum/go-ethereum/common"
@@ -8,11 +9,8 @@ import (
)
const (
fromBJJCompressedB = 256
fromEthAddrB = 160
f16B = 16
tokenIDB = 32
cidXB = 32
// L1TxBytesLen is the length of the byte array that represents the L1Tx
L1TxBytesLen = 68
)
// L1Tx is a struct that represents a L1 tx
@@ -64,69 +62,57 @@ func (tx *L1Tx) Tx() *Tx {
}
// Bytes encodes a L1Tx into []byte
func (tx *L1Tx) Bytes(nLevels int) []byte {
res := big.NewInt(0)
res = res.Add(res, big.NewInt(0).Or(big.NewInt(0), tx.ToIdx.BigInt()))
res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), big.NewInt(int64(tx.TokenID))), uint(nLevels)))
res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), tx.Amount), uint(nLevels+tokenIDB)))
res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), tx.LoadAmount), uint(nLevels+tokenIDB+f16B)))
res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), tx.FromIdx.BigInt()), uint(nLevels+tokenIDB+2*f16B)))
fromBJJ := big.NewInt(0)
fromBJJ.SetString(tx.FromBJJ.String(), 16)
fromBJJCompressed := big.NewInt(0).Or(big.NewInt(0), fromBJJ)
res = res.Add(res, big.NewInt(0).Lsh(fromBJJCompressed, uint(2*nLevels+tokenIDB+2*f16B)))
fromEthAddr := big.NewInt(0).Or(big.NewInt(0), tx.FromEthAddr.Hash().Big())
res = res.Add(res, big.NewInt(0).Lsh(fromEthAddr, uint(fromBJJCompressedB+2*nLevels+tokenIDB+2*f16B)))
return res.Bytes()
func (tx *L1Tx) Bytes(nLevels int) ([]byte, error) {
var b [68]byte
copy(b[0:20], tx.FromEthAddr.Bytes())
pkComp := tx.FromBJJ.Compress()
copy(b[20:52], pkComp[:])
copy(b[52:56], tx.FromIdx.Bytes())
loadAmountFloat16, err := NewFloat16(tx.LoadAmount)
if err != nil {
return nil, err
}
copy(b[56:58], loadAmountFloat16.Bytes())
amountFloat16, err := NewFloat16(tx.Amount)
if err != nil {
return nil, err
}
copy(b[58:60], amountFloat16.Bytes())
copy(b[60:64], tx.TokenID.Bytes())
copy(b[64:68], tx.ToIdx.Bytes())
return b[:], nil
}
// L1TxFromBytes decodes a L1Tx from []byte
func L1TxFromBytes(l1TxEncoded []byte) (*L1Tx, error) {
l1Tx := &L1Tx{}
var idxB uint = cidXB
l1TxEncodedBI := big.NewInt(0)
l1TxEncodedBI.SetBytes(l1TxEncoded)
toIdx, err := IdxFromBigInt(extract(l1TxEncodedBI, 0, idxB))
if err != nil {
return nil, err
func L1TxFromBytes(b []byte) (*L1Tx, error) {
if len(b) != L1TxBytesLen {
return nil, fmt.Errorf("Can not parse L1Tx bytes, expected length %d, current: %d", 68, len(b))
}
l1Tx.ToIdx = toIdx
l1Tx.TokenID = TokenID(extract(l1TxEncodedBI, idxB, tokenIDB).Uint64())
l1Tx.Amount = extract(l1TxEncodedBI, idxB+tokenIDB, f16B)
l1Tx.LoadAmount = extract(l1TxEncodedBI, idxB+tokenIDB+f16B, f16B)
fromIdx, err := IdxFromBigInt(extract(l1TxEncodedBI, idxB+tokenIDB+2*f16B, f16B))
if err != nil {
return nil, err
}
l1Tx.FromIdx = fromIdx
tx := &L1Tx{}
var err error
tx.FromEthAddr = ethCommon.BytesToAddress(b[0:20])
pkCompB := b[20:52]
var pkComp babyjub.PublicKeyComp
copy(pkComp[:], extract(l1TxEncodedBI, 2*idxB+tokenIDB+2*f16B, fromBJJCompressedB).Bytes())
pk, err := pkComp.Decompress()
copy(pkComp[:], pkCompB)
tx.FromBJJ, err = pkComp.Decompress()
if err != nil {
return nil, err
}
tx.FromIdx, err = IdxFromBytes(b[52:56])
if err != nil {
return nil, err
}
tx.LoadAmount = Float16FromBytes(b[56:58]).BigInt()
tx.Amount = Float16FromBytes(b[58:60]).BigInt()
tx.TokenID, err = TokenIDFromBytes(b[60:64])
if err != nil {
return nil, err
}
tx.ToIdx, err = IdxFromBytes(b[64:68])
if err != nil {
return nil, err
}
l1Tx.FromBJJ = pk
l1Tx.FromEthAddr = ethCommon.BigToAddress(extract(l1TxEncodedBI, fromBJJCompressedB+2*idxB+tokenIDB+2*f16B, fromEthAddrB))
return l1Tx, nil
}
// extract masks and shifts a bigInt
func extract(num *big.Int, origin uint, len uint) *big.Int {
mask := big.NewInt(0).Sub(big.NewInt(0).Lsh(big.NewInt(1), len), big.NewInt(1))
return big.NewInt(0).And(big.NewInt(0).Rsh(num, origin), mask)
return tx, nil
}

View File

@@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"
)
func TestL1TxCodec(t *testing.T) {
func TestL1TxByteParsers(t *testing.T) {
var pkComp babyjub.PublicKeyComp
err := pkComp.UnmarshalText([]byte("0x56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c"))
require.Nil(t, err)
@@ -19,7 +19,7 @@ func TestL1TxCodec(t *testing.T) {
pk, err := pkComp.Decompress()
require.Nil(t, err)
l1Tx := L1Tx{
l1Tx := &L1Tx{
ToIdx: 3,
TokenID: 5,
Amount: big.NewInt(1),
@@ -32,12 +32,23 @@ func TestL1TxCodec(t *testing.T) {
expected, err := utils.HexDecode("c58d29fa6e86e4fae04ddced660d45bcf3cb237056ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c00000002000200010000000500000003")
require.Nil(t, err)
encodedData := l1Tx.Bytes(32)
encodedData, err := l1Tx.Bytes(32)
require.Nil(t, err)
assert.Equal(t, expected, encodedData)
decodedData, err := L1TxFromBytes(encodedData)
require.Nil(t, err)
assert.Equal(t, l1Tx, decodedData)
encodedData2 := decodedData.Bytes(32)
encodedData2, err := decodedData.Bytes(32)
require.Nil(t, err)
assert.Equal(t, encodedData, encodedData2)
// expect error if length!=68
_, err = L1TxFromBytes(encodedData[:66])
require.NotNil(t, err)
_, err = L1TxFromBytes([]byte{})
require.NotNil(t, err)
_, err = L1TxFromBytes(nil)
require.NotNil(t, err)
}

View File

@@ -7,7 +7,6 @@ import (
"time"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-node/utils"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/iden3/go-iden3-crypto/poseidon"
)
@@ -21,9 +20,9 @@ func (n Nonce) Bytes() ([5]byte, error) {
return [5]byte{}, ErrNonceOverflow
}
var nonceBytes [8]byte
binary.LittleEndian.PutUint64(nonceBytes[:], uint64(n))
binary.BigEndian.PutUint64(nonceBytes[:], uint64(n))
var b [5]byte
copy(b[:], nonceBytes[:5])
copy(b[:], nonceBytes[3:])
return b, nil
}
@@ -35,8 +34,8 @@ func (n Nonce) BigInt() *big.Int {
// NonceFromBytes returns Nonce from a [5]byte
func NonceFromBytes(b [5]byte) Nonce {
var nonceBytes [8]byte
copy(nonceBytes[:], b[:5])
nonce := binary.LittleEndian.Uint64(nonceBytes[:])
copy(nonceBytes[3:], b[:])
nonce := binary.BigEndian.Uint64(nonceBytes[:])
return Nonce(nonce)
}
@@ -75,15 +74,15 @@ type PoolL2Tx struct {
}
// TxCompressedData spec:
// [ 32 bits ] signatureConstant // 4 bytes: [0:4]
// [ 16 bits ] chainId // 2 bytes: [4:6]
// [ 48 bits ] fromIdx // 6 bytes: [6:12]
// [ 48 bits ] toIdx // 6 bytes: [12:18]
// [ 16 bits ] amountFloat16 // 2 bytes: [18:20]
// [ 32 bits ] tokenID // 4 bytes: [20:24]
// [ 40 bits ] nonce // 5 bytes: [24:29]
// [ 8 bits ] userFee // 1 byte: [29:30]
// [ 1 bits ] toBJJSign // 1 byte: [30:31]
// [ 1 bits ] toBJJSign // 1 byte
// [ 8 bits ] userFee // 1 byte
// [ 40 bits ] nonce // 5 bytes
// [ 32 bits ] tokenID // 4 bytes
// [ 16 bits ] amountFloat16 // 2 bytes
// [ 48 bits ] toIdx // 6 bytes
// [ 48 bits ] fromIdx // 6 bytes
// [ 16 bits ] chainId // 2 bytes
// [ 32 bits ] signatureConstant // 4 bytes
// Total bits compressed data: 241 bits // 31 bytes in *big.Int representation
func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
// sigconstant
@@ -92,65 +91,65 @@ func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
return nil, fmt.Errorf("error parsing SignatureConstant")
}
amountFloat16, err := utils.NewFloat16(tx.Amount)
amountFloat16, err := NewFloat16(tx.Amount)
if err != nil {
return nil, err
}
var b [31]byte
copy(b[:4], SwapEndianness(sc.Bytes()))
copy(b[4:6], []byte{1, 0, 0, 0}) // LittleEndian representation of uint32(1) for Ethereum
copy(b[6:12], tx.FromIdx.Bytes())
copy(b[12:18], tx.ToIdx.Bytes())
copy(b[18:20], amountFloat16.Bytes())
copy(b[20:24], tx.TokenID.Bytes())
nonceBytes, err := tx.Nonce.Bytes()
if err != nil {
return nil, err
}
copy(b[24:29], nonceBytes[:])
b[29] = byte(tx.Fee)
toBJJSign := byte(0)
if babyjub.PointCoordSign(tx.ToBJJ.X) {
toBJJSign = byte(1)
}
b[30] = toBJJSign
bi := new(big.Int).SetBytes(SwapEndianness(b[:]))
b[0] = toBJJSign
b[1] = byte(tx.Fee)
nonceBytes, err := tx.Nonce.Bytes()
if err != nil {
return nil, err
}
copy(b[2:7], nonceBytes[:])
copy(b[7:11], tx.TokenID.Bytes())
copy(b[11:13], amountFloat16.Bytes())
copy(b[13+2:19], tx.ToIdx.Bytes())
copy(b[19+2:25], tx.FromIdx.Bytes())
copy(b[25:27], []byte{0, 1, 0, 0}) // TODO check js implementation (unexpected behaviour from test vector generated from js)
copy(b[27:31], sc.Bytes())
bi := new(big.Int).SetBytes(b[:])
return bi, nil
}
// TxCompressedDataV2 spec:
// [ 48 bits ] fromIdx // 6 bytes: [0:6]
// [ 48 bits ] toIdx // 6 bytes: [6:12]
// [ 16 bits ] amountFloat16 // 2 bytes: [12:14]
// [ 32 bits ] tokenID // 4 bytes: [14:18]
// [ 40 bits ] nonce // 5 bytes: [18:23]
// [ 8 bits ] userFee // 1 byte: [23:24]
// [ 1 bits ] toBJJSign // 1 byte: [24:25]
// [ 1 bits ] toBJJSign // 1 byte
// [ 8 bits ] userFee // 1 byte
// [ 40 bits ] nonce // 5 bytes
// [ 32 bits ] tokenID // 4 bytes
// [ 16 bits ] amountFloat16 // 2 bytes
// [ 48 bits ] toIdx // 6 bytes
// [ 48 bits ] fromIdx // 6 bytes
// Total bits compressed data: 193 bits // 25 bytes in *big.Int representation
func (tx *PoolL2Tx) TxCompressedDataV2() (*big.Int, error) {
amountFloat16, err := utils.NewFloat16(tx.Amount)
amountFloat16, err := NewFloat16(tx.Amount)
if err != nil {
return nil, err
}
var b [25]byte
copy(b[0:6], tx.FromIdx.Bytes())
copy(b[6:12], tx.ToIdx.Bytes())
copy(b[12:14], amountFloat16.Bytes())
copy(b[14:18], tx.TokenID.Bytes())
nonceBytes, err := tx.Nonce.Bytes()
if err != nil {
return nil, err
}
copy(b[18:23], nonceBytes[:])
b[23] = byte(tx.Fee)
toBJJSign := byte(0)
if babyjub.PointCoordSign(tx.ToBJJ.X) {
toBJJSign = byte(1)
}
b[24] = toBJJSign
b[0] = toBJJSign
b[1] = byte(tx.Fee)
nonceBytes, err := tx.Nonce.Bytes()
if err != nil {
return nil, err
}
copy(b[2:7], nonceBytes[:])
copy(b[7:11], tx.TokenID.Bytes())
copy(b[11:13], amountFloat16.Bytes())
copy(b[13+2:19], tx.ToIdx.Bytes())
copy(b[19+2:25], tx.FromIdx.Bytes())
bi := new(big.Int).SetBytes(SwapEndianness(b[:]))
bi := new(big.Int).SetBytes(b[:])
return bi, nil
}

View File

@@ -15,7 +15,7 @@ func TestNonceParser(t *testing.T) {
nBytes, err := n.Bytes()
assert.Nil(t, err)
assert.Equal(t, 5, len(nBytes))
assert.Equal(t, "0100000000", hex.EncodeToString(nBytes[:]))
assert.Equal(t, "0000000001", hex.EncodeToString(nBytes[:]))
n2 := NonceFromBytes(nBytes)
assert.Equal(t, n, n2)
@@ -51,7 +51,11 @@ func TestTxCompressedData(t *testing.T) {
txCompressedData, err := tx.TxCompressedData()
assert.Nil(t, err)
// test vector value generated from javascript implementation
assert.Equal(t, "1766847064778421992193717128424891165872736891548909569553540449389241871", txCompressedData.String())
expectedStr := "1766847064778421992193717128424891165872736891548909569553540449389241871"
assert.Equal(t, expectedStr, txCompressedData.String())
expected, ok := new(big.Int).SetString(expectedStr, 10)
assert.True(t, ok)
assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
assert.Equal(t, "10000000000060000000500040000000000030000000000020001c60be60f", hex.EncodeToString(txCompressedData.Bytes())[1:])
tx = PoolL2Tx{
@@ -66,7 +70,11 @@ func TestTxCompressedData(t *testing.T) {
txCompressedData, err = tx.TxCompressedDataV2()
assert.Nil(t, err)
// test vector value generated from javascript implementation
assert.Equal(t, "6571340879233176732837827812956721483162819083004853354503", txCompressedData.String())
expectedStr = "6571340879233176732837827812956721483162819083004853354503"
assert.Equal(t, expectedStr, txCompressedData.String())
expected, ok = new(big.Int).SetString(expectedStr, 10)
assert.True(t, ok)
assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
assert.Equal(t, "10c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes())[1:])
}

View File

@@ -2,12 +2,16 @@ package common
import (
"encoding/binary"
"fmt"
"math/big"
"time"
ethCommon "github.com/ethereum/go-ethereum/common"
)
// tokenIDBytesLen defines the length of the TokenID byte array representation
const tokenIDBytesLen = 4
// Token is a struct that represents an Ethereum token that is supported in Hermez network
type Token struct {
TokenID TokenID `meddler:"token_id"`
@@ -33,7 +37,7 @@ type TokenID uint32 // current implementation supports up to 2^32 tokens
// Bytes returns a byte array of length 4 representing the TokenID
func (t TokenID) Bytes() []byte {
var tokenIDBytes [4]byte
binary.LittleEndian.PutUint32(tokenIDBytes[:], uint32(t))
binary.BigEndian.PutUint32(tokenIDBytes[:], uint32(t))
return tokenIDBytes[:]
}
@@ -41,3 +45,12 @@ func (t TokenID) Bytes() []byte {
func (t TokenID) BigInt() *big.Int {
return big.NewInt(int64(t))
}
// TokenIDFromBytes returns TokenID from a byte array
func TokenIDFromBytes(b []byte) (TokenID, error) {
if len(b) != tokenIDBytesLen {
return 0, fmt.Errorf("can not parse TokenID, bytes len %d, expected 4", len(b))
}
tid := binary.BigEndian.Uint32(b[:4])
return TokenID(tid), nil
}