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

@@ -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 {