Wrap all errors with tracerr

This commit is contained in:
Eduard S
2020-11-30 12:58:36 +01:00
parent 879af66a2a
commit ced42634da
59 changed files with 1324 additions and 1268 deletions

View File

@@ -8,6 +8,7 @@ import (
"strconv"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/tracerr"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/iden3/go-iden3-crypto/poseidon"
cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
@@ -51,7 +52,7 @@ func (idx Idx) String() string {
// Bytes returns a byte array representing the Idx
func (idx Idx) Bytes() ([6]byte, error) {
if idx > maxIdxValue {
return [6]byte{}, ErrIdxOverflow
return [6]byte{}, tracerr.Wrap(ErrIdxOverflow)
}
var idxBytes [8]byte
binary.BigEndian.PutUint64(idxBytes[:], uint64(idx))
@@ -68,7 +69,7 @@ func (idx Idx) BigInt() *big.Int {
// IdxFromBytes returns Idx from a byte array
func IdxFromBytes(b []byte) (Idx, error) {
if len(b) != IdxBytesLen {
return 0, fmt.Errorf("can not parse Idx, bytes len %d, expected %d", len(b), IdxBytesLen)
return 0, tracerr.Wrap(fmt.Errorf("can not parse Idx, bytes len %d, expected %d", len(b), IdxBytesLen))
}
var idxBytes [8]byte
copy(idxBytes[2:], b[:])
@@ -79,7 +80,7 @@ func IdxFromBytes(b []byte) (Idx, error) {
// IdxFromBigInt converts a *big.Int to Idx type
func IdxFromBigInt(b *big.Int) (Idx, error) {
if b.Int64() > maxIdxValue {
return 0, ErrNumOverflow
return 0, tracerr.Wrap(ErrNumOverflow)
}
return Idx(uint64(b.Int64())), nil
}
@@ -90,7 +91,7 @@ type Nonce uint64
// Bytes returns a byte array of length 5 representing the Nonce
func (n Nonce) Bytes() ([5]byte, error) {
if n > maxNonceValue {
return [5]byte{}, ErrNonceOverflow
return [5]byte{}, tracerr.Wrap(ErrNonceOverflow)
}
var nonceBytes [8]byte
binary.BigEndian.PutUint64(nonceBytes[:], uint64(n))
@@ -143,22 +144,22 @@ func (a *Account) Bytes() ([32 * NLeafElems]byte, error) {
var b [32 * NLeafElems]byte
if a.Nonce > maxNonceValue {
return b, fmt.Errorf("%s Nonce", ErrNumOverflow)
return b, tracerr.Wrap(fmt.Errorf("%s Nonce", ErrNumOverflow))
}
if len(a.Balance.Bytes()) > maxBalanceBytes {
return b, fmt.Errorf("%s Balance", ErrNumOverflow)
return b, tracerr.Wrap(fmt.Errorf("%s Balance", ErrNumOverflow))
}
nonceBytes, err := a.Nonce.Bytes()
if err != nil {
return b, err
return b, tracerr.Wrap(err)
}
copy(b[28:32], a.TokenID.Bytes())
copy(b[23:28], nonceBytes[:])
if a.PublicKey == nil {
return b, fmt.Errorf("Account.PublicKey can not be nil")
return b, tracerr.Wrap(fmt.Errorf("Account.PublicKey can not be nil"))
}
if babyjub.PointCoordSign(a.PublicKey.X) {
b[22] = 1
@@ -178,7 +179,7 @@ func (a *Account) BigInts() ([NLeafElems]*big.Int, error) {
b, err := a.Bytes()
if err != nil {
return e, err
return e, tracerr.Wrap(err)
}
e[0] = new(big.Int).SetBytes(b[0:32])
@@ -193,7 +194,7 @@ func (a *Account) BigInts() ([NLeafElems]*big.Int, error) {
func (a *Account) HashValue() (*big.Int, error) {
bi, err := a.BigInts()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
return poseidon.Hash(bi[:])
}
@@ -201,7 +202,7 @@ func (a *Account) HashValue() (*big.Int, error) {
// AccountFromBigInts returns a Account from a [5]*big.Int
func AccountFromBigInts(e [NLeafElems]*big.Int) (*Account, error) {
if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
return nil, ErrNotInFF
return nil, tracerr.Wrap(ErrNotInFF)
}
e0B := e[0].Bytes()
e1B := e[1].Bytes()
@@ -220,7 +221,7 @@ func AccountFromBigInts(e [NLeafElems]*big.Int) (*Account, error) {
func AccountFromBytes(b [32 * NLeafElems]byte) (*Account, error) {
tokenID, err := TokenIDFromBytes(b[28:32])
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
var nonceBytes5 [5]byte
copy(nonceBytes5[:], b[23:28])
@@ -230,21 +231,21 @@ func AccountFromBytes(b [32 * NLeafElems]byte) (*Account, error) {
balance := new(big.Int).SetBytes(b[40:64])
// Balance is max of 192 bits (24 bytes)
if !bytes.Equal(b[32:40], []byte{0, 0, 0, 0, 0, 0, 0, 0}) {
return nil, fmt.Errorf("%s Balance", ErrNumOverflow)
return nil, tracerr.Wrap(fmt.Errorf("%s Balance", ErrNumOverflow))
}
ay := new(big.Int).SetBytes(b[64:96])
pkPoint, err := babyjub.PointFromSignAndY(sign, ay)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
publicKey := babyjub.PublicKey(*pkPoint)
ethAddr := ethCommon.BytesToAddress(b[108:128])
if !cryptoUtils.CheckBigIntInField(balance) {
return nil, ErrNotInFF
return nil, tracerr.Wrap(ErrNotInFF)
}
if !cryptoUtils.CheckBigIntInField(ay) {
return nil, ErrNotInFF
return nil, tracerr.Wrap(ErrNotInFF)
}
a := Account{

View File

@@ -10,6 +10,7 @@ import (
ethCommon "github.com/ethereum/go-ethereum/common"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/hermeznetwork/tracerr"
"github.com/iden3/go-iden3-crypto/babyjub"
cryptoConstants "github.com/iden3/go-iden3-crypto/constants"
"github.com/iden3/go-iden3-crypto/poseidon"
@@ -45,7 +46,7 @@ func TestIdxParser(t *testing.T) {
i = Idx(281474976710656)
iBytes, err = i.Bytes()
assert.NotNil(t, err)
assert.Equal(t, ErrIdxOverflow, err)
assert.Equal(t, ErrIdxOverflow, tracerr.Unwrap(err))
}
func TestNonceParser(t *testing.T) {
@@ -70,7 +71,7 @@ func TestNonceParser(t *testing.T) {
n = Nonce(1099511627776)
nBytes, err = n.Bytes()
assert.NotNil(t, err)
assert.Equal(t, ErrNonceOverflow, err)
assert.Equal(t, ErrNonceOverflow, tracerr.Unwrap(err))
}
func TestAccount(t *testing.T) {
@@ -296,14 +297,14 @@ func TestAccountErrNotInFF(t *testing.T) {
e = [NLeafElems]*big.Int{z, z, r, r}
_, err = AccountFromBigInts(e)
assert.NotNil(t, err)
assert.Equal(t, ErrNotInFF, err)
assert.Equal(t, ErrNotInFF, tracerr.Unwrap(err))
// Q+1 should give error
r = new(big.Int).Add(cryptoConstants.Q, big.NewInt(1))
e = [NLeafElems]*big.Int{z, z, r, r}
_, err = AccountFromBigInts(e)
assert.NotNil(t, err)
assert.Equal(t, ErrNotInFF, err)
assert.Equal(t, ErrNotInFF, tracerr.Unwrap(err))
}
func TestAccountErrNumOverflowNonce(t *testing.T) {
@@ -327,7 +328,7 @@ func TestAccountErrNumOverflowNonce(t *testing.T) {
account.Nonce = Nonce(math.Pow(2, 40))
b, err := account.Bytes()
assert.NotNil(t, err)
assert.Equal(t, fmt.Errorf("%s Nonce", ErrNumOverflow), err)
assert.Equal(t, fmt.Errorf("%s Nonce", ErrNumOverflow), tracerr.Unwrap(err))
_, err = AccountFromBytes(b)
assert.Nil(t, err)
@@ -357,7 +358,7 @@ func TestAccountErrNumOverflowBalance(t *testing.T) {
assert.Equal(t, "6277101735386680763835789423207666416102355444464034512896", account.Balance.String())
b, err := account.Bytes()
assert.NotNil(t, err)
assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), tracerr.Unwrap(err))
_, err = AccountFromBytes(b)
assert.Nil(t, err)
@@ -365,5 +366,5 @@ func TestAccountErrNumOverflowBalance(t *testing.T) {
b[39] = 1
_, err = AccountFromBytes(b)
assert.NotNil(t, err)
assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), tracerr.Unwrap(err))
}

View File

@@ -5,6 +5,7 @@ import (
ethCommon "github.com/ethereum/go-ethereum/common"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/hermeznetwork/tracerr"
"github.com/iden3/go-iden3-crypto/babyjub"
)
@@ -22,7 +23,7 @@ func (a *AccountCreationAuth) HashToSign() ([]byte, error) {
const msg = "I authorize this babyjubjub key for hermez rollup account creation"
comp, err := a.BJJ.Compress().MarshalText()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
// Hash message (msg || compressed-bjj)
return ethCrypto.Keccak256Hash([]byte(msg), comp).Bytes(), nil

View File

@@ -6,6 +6,7 @@ import (
"math/big"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/tracerr"
)
const batchNumBytesLen = 8
@@ -44,7 +45,7 @@ func (bn BatchNum) BigInt() *big.Int {
// BatchNumFromBytes returns BatchNum from a []byte
func BatchNumFromBytes(b []byte) (BatchNum, error) {
if len(b) != batchNumBytesLen {
return 0, fmt.Errorf("can not parse BatchNumFromBytes, bytes len %d, expected %d", len(b), batchNumBytesLen)
return 0, tracerr.Wrap(fmt.Errorf("can not parse BatchNumFromBytes, bytes len %d, expected %d", len(b), batchNumBytesLen))
}
batchNum := binary.BigEndian.Uint64(b[:batchNumBytesLen])
return BatchNum(batchNum), nil

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"math"
"math/big"
"github.com/hermeznetwork/tracerr"
)
// MaxFeePlan is the maximum value of the FeePlan
@@ -49,7 +51,7 @@ func CalcFeeAmount(amount *big.Int, feeSel FeeSelector) (*big.Int, error) {
feeAmount.Rsh(feeAmount, 60)
}
if feeAmount.BitLen() > 128 { //nolint:gomnd
return nil, fmt.Errorf("FeeAmount overflow (feeAmount doesn't fit in 128 bits)")
return nil, tracerr.Wrap(fmt.Errorf("FeeAmount overflow (feeAmount doesn't fit in 128 bits)"))
}
return feeAmount, nil
}

View File

@@ -9,6 +9,8 @@ import (
"encoding/binary"
"errors"
"math/big"
"github.com/hermeznetwork/tracerr"
)
var (
@@ -112,7 +114,7 @@ func NewFloat16(f *big.Int) (Float16, error) {
if res.BigInt().Cmp(f) == 0 {
return res, nil
}
return res, ErrRoundingLoss
return res, tracerr.Wrap(ErrRoundingLoss)
}
// NewFloat16Floor encodes a big.Int integer as a Float16, rounding down in

View File

@@ -4,6 +4,7 @@ import (
"math/big"
"testing"
"github.com/hermeznetwork/tracerr"
"github.com/stretchr/testify/assert"
)
@@ -65,25 +66,25 @@ func TestConversionLosses(t *testing.T) {
a = big.NewInt(1024)
b, err = NewFloat16(a)
assert.Equal(t, ErrRoundingLoss, err)
assert.Equal(t, ErrRoundingLoss, tracerr.Unwrap(err))
c = b.BigInt()
assert.NotEqual(t, c, a)
a = big.NewInt(32767)
b, err = NewFloat16(a)
assert.Equal(t, ErrRoundingLoss, err)
assert.Equal(t, ErrRoundingLoss, tracerr.Unwrap(err))
c = b.BigInt()
assert.NotEqual(t, c, a)
a = big.NewInt(32768)
b, err = NewFloat16(a)
assert.Equal(t, ErrRoundingLoss, err)
assert.Equal(t, ErrRoundingLoss, tracerr.Unwrap(err))
c = b.BigInt()
assert.NotEqual(t, c, a)
a = big.NewInt(65536000)
b, err = NewFloat16(a)
assert.Equal(t, ErrRoundingLoss, err)
assert.Equal(t, ErrRoundingLoss, tracerr.Unwrap(err))
c = b.BigInt()
assert.NotEqual(t, c, a)
}

View File

@@ -7,6 +7,7 @@ import (
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/hermeznetwork/tracerr"
"github.com/iden3/go-iden3-crypto/babyjub"
)
@@ -58,7 +59,7 @@ func NewL1Tx(l1Tx *L1Tx) (*L1Tx, error) {
} else if l1Tx.ToIdx >= IdxUserThreshold {
txType = TxTypeCreateAccountDepositTransfer
} else {
return l1Tx, fmt.Errorf("Can not determine type of L1Tx, invalid ToIdx value: %d", l1Tx.ToIdx)
return l1Tx, tracerr.Wrap(fmt.Errorf("Can not determine type of L1Tx, invalid ToIdx value: %d", l1Tx.ToIdx))
}
} else if l1Tx.FromIdx >= IdxUserThreshold {
if l1Tx.ToIdx == Idx(0) {
@@ -72,20 +73,20 @@ func NewL1Tx(l1Tx *L1Tx) (*L1Tx, error) {
txType = TxTypeDepositTransfer
}
} else {
return l1Tx, fmt.Errorf("Can not determine type of L1Tx, invalid ToIdx value: %d", l1Tx.ToIdx)
return l1Tx, tracerr.Wrap(fmt.Errorf("Can not determine type of L1Tx, invalid ToIdx value: %d", l1Tx.ToIdx))
}
} else {
return l1Tx, fmt.Errorf("Can not determine type of L1Tx, invalid FromIdx value: %d", l1Tx.FromIdx)
return l1Tx, tracerr.Wrap(fmt.Errorf("Can not determine type of L1Tx, invalid FromIdx value: %d", l1Tx.FromIdx))
}
if l1Tx.Type != "" && l1Tx.Type != txType {
return l1Tx, fmt.Errorf("L1Tx.Type: %s, should be: %s", l1Tx.Type, txType)
return l1Tx, tracerr.Wrap(fmt.Errorf("L1Tx.Type: %s, should be: %s", l1Tx.Type, txType))
}
l1Tx.Type = txType
txID, err := l1Tx.CalcTxID()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
l1Tx.TxID = *txID
@@ -97,7 +98,7 @@ func (tx *L1Tx) CalcTxID() (*TxID, error) {
var txID TxID
if tx.UserOrigin {
if tx.ToForgeL1TxsNum == nil {
return nil, fmt.Errorf("L1Tx.UserOrigin == true && L1Tx.ToForgeL1TxsNum == nil")
return nil, tracerr.Wrap(fmt.Errorf("L1Tx.UserOrigin == true && L1Tx.ToForgeL1TxsNum == nil"))
}
txID[0] = TxIDPrefixL1UserTx
var toForgeL1TxsNumBytes [8]byte
@@ -105,7 +106,7 @@ func (tx *L1Tx) CalcTxID() (*TxID, error) {
copy(txID[1:9], toForgeL1TxsNumBytes[:])
} else {
if tx.BatchNum == nil {
return nil, fmt.Errorf("L1Tx.UserOrigin == false && L1Tx.BatchNum == nil")
return nil, tracerr.Wrap(fmt.Errorf("L1Tx.UserOrigin == false && L1Tx.BatchNum == nil"))
}
txID[0] = TxIDPrefixL1CoordTx
var batchNumBytes [8]byte
@@ -164,7 +165,7 @@ func (tx L1Tx) Tx() Tx {
func (tx L1Tx) TxCompressedData() (*big.Int, error) {
amountFloat16, err := NewFloat16(tx.Amount)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
var b [31]byte
// b[0:7] empty: no fee neither nonce
@@ -172,12 +173,12 @@ func (tx L1Tx) TxCompressedData() (*big.Int, error) {
copy(b[11:13], amountFloat16.Bytes())
toIdxBytes, err := tx.ToIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[13:19], toIdxBytes[:])
fromIdxBytes, err := tx.FromIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[19:25], fromIdxBytes[:])
copy(b[25:27], []byte{0, 1}) // TODO this will be generated by the ChainID config parameter
@@ -195,19 +196,19 @@ func (tx *L1Tx) BytesDataAvailability(nLevels uint32) ([]byte, error) {
fromIdxBytes, err := tx.FromIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[0:idxLen], fromIdxBytes[6-idxLen:])
toIdxBytes, err := tx.ToIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[idxLen:idxLen*2], toIdxBytes[6-idxLen:])
if tx.EffectiveAmount != nil {
amountFloat16, err := NewFloat16(tx.EffectiveAmount)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[idxLen*2:idxLen*2+2], amountFloat16.Bytes())
}
@@ -229,23 +230,23 @@ func (tx *L1Tx) BytesGeneric() ([]byte, error) {
}
fromIdxBytes, err := tx.FromIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[52:58], fromIdxBytes[:])
loadAmountFloat16, err := NewFloat16(tx.LoadAmount)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[58:60], loadAmountFloat16.Bytes())
amountFloat16, err := NewFloat16(tx.Amount)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[60:62], amountFloat16.Bytes())
copy(b[62:66], tx.TokenID.Bytes())
toIdxBytes, err := tx.ToIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[66:72], toIdxBytes[:])
return b[:], nil
@@ -254,7 +255,7 @@ func (tx *L1Tx) BytesGeneric() ([]byte, error) {
// BytesUser encodes a L1UserTx into []byte
func (tx *L1Tx) BytesUser() ([]byte, error) {
if !tx.UserOrigin {
return nil, fmt.Errorf("Can not calculate BytesUser() for a L1CoordinatorTx")
return nil, tracerr.Wrap(fmt.Errorf("Can not calculate BytesUser() for a L1CoordinatorTx"))
}
return tx.BytesGeneric()
}
@@ -262,7 +263,7 @@ func (tx *L1Tx) BytesUser() ([]byte, error) {
// BytesCoordinatorTx encodes a L1CoordinatorTx into []byte
func (tx *L1Tx) BytesCoordinatorTx(compressedSignatureBytes []byte) ([]byte, error) {
if tx.UserOrigin {
return nil, fmt.Errorf("Can not calculate BytesCoordinatorTx() for a L1UserTx")
return nil, tracerr.Wrap(fmt.Errorf("Can not calculate BytesCoordinatorTx() for a L1UserTx"))
}
var b [L1CoordinatorTxBytesLen]byte
v := compressedSignatureBytes[64]
@@ -281,7 +282,7 @@ func (tx *L1Tx) BytesCoordinatorTx(compressedSignatureBytes []byte) ([]byte, err
// L1UserTxFromBytes decodes a L1Tx from []byte
func L1UserTxFromBytes(b []byte) (*L1Tx, error) {
if len(b) != L1UserTxBytesLen {
return nil, fmt.Errorf("Can not parse L1Tx bytes, expected length %d, current: %d", 68, len(b))
return nil, tracerr.Wrap(fmt.Errorf("Can not parse L1Tx bytes, expected length %d, current: %d", 68, len(b)))
}
tx := &L1Tx{
@@ -296,22 +297,22 @@ func L1UserTxFromBytes(b []byte) (*L1Tx, error) {
copy(pkComp[:], pkCompL)
tx.FromBJJ, err = pkComp.Decompress()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
fromIdx, err := IdxFromBytes(b[52:58])
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
tx.FromIdx = fromIdx
tx.LoadAmount = Float16FromBytes(b[58:60]).BigInt()
tx.Amount = Float16FromBytes(b[60:62]).BigInt()
tx.TokenID, err = TokenIDFromBytes(b[62:66])
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
tx.ToIdx, err = IdxFromBytes(b[66:72])
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
return tx, nil
@@ -325,7 +326,7 @@ func signHash(data []byte) []byte {
// L1CoordinatorTxFromBytes decodes a L1Tx from []byte
func L1CoordinatorTxFromBytes(b []byte, chainID *big.Int, hermezAddress ethCommon.Address) (*L1Tx, error) {
if len(b) != L1CoordinatorTxBytesLen {
return nil, fmt.Errorf("Can not parse L1CoordinatorTx bytes, expected length %d, current: %d", 101, len(b))
return nil, tracerr.Wrap(fmt.Errorf("Can not parse L1CoordinatorTx bytes, expected length %d, current: %d", 101, len(b)))
}
bytesMessage := []byte("I authorize this babyjubjub key for hermez rollup account creation")
@@ -343,11 +344,11 @@ func L1CoordinatorTxFromBytes(b []byte, chainID *big.Int, hermezAddress ethCommo
copy(pkComp[:], pkCompL)
tx.FromBJJ, err = pkComp.Decompress()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
tx.TokenID, err = TokenIDFromBytes(b[97:101])
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
tx.Amount = big.NewInt(0)
tx.LoadAmount = big.NewInt(0)
@@ -368,11 +369,11 @@ func L1CoordinatorTxFromBytes(b []byte, chainID *big.Int, hermezAddress ethCommo
hash := signHash(data)
pubKeyBytes, err := crypto.Ecrecover(hash, signature)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
pubKey, err := crypto.UnmarshalPubkey(pubKeyBytes)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
tx.FromEthAddr = crypto.PubkeyToAddress(*pubKey)
} else {

View File

@@ -3,6 +3,8 @@ package common
import (
"fmt"
"math/big"
"github.com/hermeznetwork/tracerr"
)
// L2Tx is a struct that represents an already forged L2 tx
@@ -30,12 +32,12 @@ func NewL2Tx(l2Tx *L2Tx) (*L2Tx, error) {
} else if l2Tx.ToIdx >= IdxUserThreshold {
txType = TxTypeTransfer
} else {
return l2Tx, fmt.Errorf("Can not determine type of L2Tx, invalid ToIdx value: %d", l2Tx.ToIdx)
return l2Tx, tracerr.Wrap(fmt.Errorf("Can not determine type of L2Tx, invalid ToIdx value: %d", l2Tx.ToIdx))
}
// if TxType!=l2Tx.TxType return error
if l2Tx.Type != "" && l2Tx.Type != txType {
return l2Tx, fmt.Errorf("L2Tx.Type: %s, should be: %s", l2Tx.Type, txType)
return l2Tx, tracerr.Wrap(fmt.Errorf("L2Tx.Type: %s, should be: %s", l2Tx.Type, txType))
}
l2Tx.Type = txType
@@ -43,12 +45,12 @@ func NewL2Tx(l2Tx *L2Tx) (*L2Tx, error) {
txid[0] = TxIDPrefixL2Tx
fromIdxBytes, err := l2Tx.FromIdx.Bytes()
if err != nil {
return l2Tx, err
return l2Tx, tracerr.Wrap(err)
}
copy(txid[1:7], fromIdxBytes[:])
nonceBytes, err := l2Tx.Nonce.Bytes()
if err != nil {
return l2Tx, err
return l2Tx, tracerr.Wrap(err)
}
copy(txid[7:12], nonceBytes[:])
l2Tx.TxID = TxID(txid)
@@ -111,19 +113,19 @@ func (tx L2Tx) BytesDataAvailability(nLevels uint32) ([]byte, error) {
fromIdxBytes, err := tx.FromIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[0:idxLen], fromIdxBytes[6-idxLen:]) // [6-idxLen:] as is BigEndian
toIdxBytes, err := tx.ToIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[idxLen:idxLen*2], toIdxBytes[6-idxLen:])
amountFloat16, err := NewFloat16(tx.Amount)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[idxLen*2:idxLen*2+2], amountFloat16.Bytes())
@@ -142,14 +144,14 @@ func L2TxFromBytes(b []byte, nLevels int) (*L2Tx, error) {
copy(paddedFromIdxBytes[6-idxLen:], b[0:idxLen])
tx.FromIdx, err = IdxFromBytes(paddedFromIdxBytes[:])
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
var paddedToIdxBytes [6]byte
copy(paddedToIdxBytes[6-idxLen:6], b[idxLen:idxLen*2])
tx.ToIdx, err = IdxFromBytes(paddedToIdxBytes[:])
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
tx.Amount = Float16FromBytes(b[idxLen*2 : idxLen*2+2]).BigInt()

View File

@@ -7,6 +7,7 @@ import (
"time"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/tracerr"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/iden3/go-iden3-crypto/poseidon"
)
@@ -63,12 +64,12 @@ func NewPoolL2Tx(poolL2Tx *PoolL2Tx) (*PoolL2Tx, error) {
txType = TxTypeTransferToEthAddr
}
} else {
return nil, errors.New("malformed transaction")
return nil, tracerr.Wrap(errors.New("malformed transaction"))
}
// if TxType!=poolL2Tx.TxType return error
if poolL2Tx.Type != "" && poolL2Tx.Type != txType {
return poolL2Tx, fmt.Errorf("type: %s, should be: %s", poolL2Tx.Type, txType)
return poolL2Tx, tracerr.Wrap(fmt.Errorf("type: %s, should be: %s", poolL2Tx.Type, txType))
}
poolL2Tx.Type = txType
@@ -76,19 +77,19 @@ func NewPoolL2Tx(poolL2Tx *PoolL2Tx) (*PoolL2Tx, error) {
txid[0] = TxIDPrefixL2Tx
fromIdxBytes, err := poolL2Tx.FromIdx.Bytes()
if err != nil {
return poolL2Tx, err
return poolL2Tx, tracerr.Wrap(err)
}
copy(txid[1:7], fromIdxBytes[:])
nonceBytes, err := poolL2Tx.Nonce.Bytes()
if err != nil {
return poolL2Tx, err
return poolL2Tx, tracerr.Wrap(err)
}
copy(txid[7:12], nonceBytes[:])
txID := TxID(txid)
// if TxID!=poolL2Tx.TxID return error
if poolL2Tx.TxID != (TxID{}) && poolL2Tx.TxID != txID {
return poolL2Tx, fmt.Errorf("id: %s, should be: %s", poolL2Tx.TxID.String(), txID.String())
return poolL2Tx, tracerr.Wrap(fmt.Errorf("id: %s, should be: %s", poolL2Tx.TxID.String(), txID.String()))
}
poolL2Tx.TxID = txID
return poolL2Tx, nil
@@ -109,12 +110,12 @@ func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
// sigconstant
sc, ok := new(big.Int).SetString("3322668559", 10)
if !ok {
return nil, fmt.Errorf("error parsing SignatureConstant")
return nil, tracerr.Wrap(fmt.Errorf("error parsing SignatureConstant"))
}
amountFloat16, err := NewFloat16(tx.Amount)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
var b [31]byte
toBJJSign := byte(0)
@@ -125,19 +126,19 @@ func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
b[1] = byte(tx.Fee)
nonceBytes, err := tx.Nonce.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[2:7], nonceBytes[:])
copy(b[7:11], tx.TokenID.Bytes())
copy(b[11:13], amountFloat16.Bytes())
toIdxBytes, err := tx.ToIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[13:19], toIdxBytes[:])
fromIdxBytes, err := tx.FromIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[19:25], fromIdxBytes[:])
copy(b[25:27], []byte{0, 1}) // TODO this will be generated by the ChainID config parameter
@@ -162,7 +163,7 @@ func (tx *PoolL2Tx) TxCompressedDataV2() (*big.Int, error) {
}
amountFloat16, err := NewFloat16(tx.Amount)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
var b [25]byte
toBJJSign := byte(0)
@@ -173,19 +174,19 @@ func (tx *PoolL2Tx) TxCompressedDataV2() (*big.Int, error) {
b[1] = byte(tx.Fee)
nonceBytes, err := tx.Nonce.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[2:7], nonceBytes[:])
copy(b[7:11], tx.TokenID.Bytes())
copy(b[11:13], amountFloat16.Bytes())
toIdxBytes, err := tx.ToIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[13:19], toIdxBytes[:])
fromIdxBytes, err := tx.FromIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[19:25], fromIdxBytes[:])
@@ -213,7 +214,7 @@ func (tx *PoolL2Tx) RqTxCompressedDataV2() (*big.Int, error) {
}
amountFloat16, err := NewFloat16(tx.RqAmount)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
var b [25]byte
toBJJSign := byte(0)
@@ -224,19 +225,19 @@ func (tx *PoolL2Tx) RqTxCompressedDataV2() (*big.Int, error) {
b[1] = byte(tx.RqFee)
nonceBytes, err := tx.RqNonce.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[2:7], nonceBytes[:])
copy(b[7:11], tx.RqTokenID.Bytes())
copy(b[11:13], amountFloat16.Bytes())
toIdxBytes, err := tx.RqToIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[13:19], toIdxBytes[:])
fromIdxBytes, err := tx.RqFromIdx.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(b[19:25], fromIdxBytes[:])
@@ -248,7 +249,7 @@ func (tx *PoolL2Tx) RqTxCompressedDataV2() (*big.Int, error) {
func (tx *PoolL2Tx) HashToSign() (*big.Int, error) {
toCompressedData, err := tx.TxCompressedData()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
toEthAddr := EthAddrToBigInt(tx.ToEthAddr)
rqToEthAddr := EthAddrToBigInt(tx.RqToEthAddr)
@@ -258,7 +259,7 @@ func (tx *PoolL2Tx) HashToSign() (*big.Int, error) {
}
rqTxCompressedDataV2, err := tx.RqTxCompressedDataV2()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
rqToBJJY := big.NewInt(0)
if tx.RqToBJJ != nil {

View File

@@ -7,6 +7,7 @@ import (
"time"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/tracerr"
)
// tokenIDBytesLen defines the length of the TokenID byte array representation
@@ -47,7 +48,7 @@ func (t TokenID) BigInt() *big.Int {
// 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))
return 0, tracerr.Wrap(fmt.Errorf("can not parse TokenID, bytes len %d, expected 4", len(b)))
}
tid := binary.BigEndian.Uint32(b[:4])
return TokenID(tid), nil

View File

@@ -10,6 +10,7 @@ import (
"strings"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/tracerr"
"github.com/iden3/go-iden3-crypto/babyjub"
)
@@ -40,10 +41,10 @@ type TxID [TxIDLen]byte
func (txid *TxID) Scan(src interface{}) error {
srcB, ok := src.([]byte)
if !ok {
return fmt.Errorf("can't scan %T into TxID", src)
return tracerr.Wrap(fmt.Errorf("can't scan %T into TxID", src))
}
if len(srcB) != TxIDLen {
return fmt.Errorf("can't scan []byte of len %d into TxID, need %d", len(srcB), TxIDLen)
return tracerr.Wrap(fmt.Errorf("can't scan []byte of len %d into TxID, need %d", len(srcB), TxIDLen))
}
copy(txid[:], srcB)
return nil
@@ -65,10 +66,10 @@ func NewTxIDFromString(idStr string) (TxID, error) {
idStr = strings.TrimPrefix(idStr, "0x")
decoded, err := hex.DecodeString(idStr)
if err != nil {
return TxID{}, err
return TxID{}, tracerr.Wrap(err)
}
if len(decoded) != TxIDLen {
return txid, errors.New("Invalid idStr")
return txid, tracerr.Wrap(errors.New("Invalid idStr"))
}
copy(txid[:], decoded)
return txid, nil
@@ -84,7 +85,7 @@ func (txid *TxID) UnmarshalText(data []byte) error {
idStr := string(data)
id, err := NewTxIDFromString(idStr)
if err != nil {
return err
return tracerr.Wrap(err)
}
*txid = id
return nil

View File

@@ -5,6 +5,7 @@ import (
"math/big"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/tracerr"
"github.com/iden3/go-iden3-crypto/babyjub"
)
@@ -29,7 +30,7 @@ func EthAddrToBigInt(a ethCommon.Address) *big.Int {
func BJJFromStringWithChecksum(s string) (*babyjub.PublicKey, error) {
b, err := hex.DecodeString(s)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
pkBytes := SwapEndianness(b)
var pkComp babyjub.PublicKeyComp

View File

@@ -10,6 +10,7 @@ import (
"math/big"
"github.com/hermeznetwork/hermez-node/log"
"github.com/hermeznetwork/tracerr"
cryptoConstants "github.com/iden3/go-iden3-crypto/constants"
"github.com/iden3/go-merkletree"
"github.com/mitchellh/mapstructure"
@@ -258,11 +259,11 @@ func (z ZKInputs) MarshalJSON() ([]byte, error) {
Result: &m,
})
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
err = dec.Decode(z)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
for k, v := range m {
@@ -398,13 +399,13 @@ func newSlice(n uint32) []*big.Int {
func (z ZKInputs) HashGlobalData() (*big.Int, error) {
b, err := z.ToHashGlobalData()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
h := sha256.New()
_, err = h.Write(b)
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
r := new(big.Int).SetBytes(h.Sum(nil))
@@ -427,7 +428,7 @@ func (z ZKInputs) ToHashGlobalData() ([]byte, error) {
newLastIdx := make([]byte, bytesMaxLevels)
newLastIdxBytes, err := z.Metadata.NewLastIdxRaw.Bytes()
if err != nil {
return nil, err
return nil, tracerr.Wrap(err)
}
copy(newLastIdx, newLastIdxBytes[len(newLastIdxBytes)-bytesMaxLevels:])
b = append(b, newLastIdx...)
@@ -474,7 +475,7 @@ func (z ZKInputs) ToHashGlobalData() ([]byte, error) {
l2TxsData = append(l2TxsData, z.Metadata.L2TxsData[i]...)
}
if len(l2TxsData) > int(expectedL2TxsDataLen) {
return nil, fmt.Errorf("len(l2TxsData): %d, expected: %d", len(l2TxsData), expectedL2TxsDataLen)
return nil, tracerr.Wrap(fmt.Errorf("len(l2TxsData): %d, expected: %d", len(l2TxsData), expectedL2TxsDataLen))
}
b = append(b, l2TxsData...)