mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Simplify eth code
This commit is contained in:
@@ -11,8 +11,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// L1TxBytesLen is the length of the byte array that represents the L1Tx
|
||||
L1TxBytesLen = 72
|
||||
// L1UserTxBytesLen is the length of the byte array that represents the L1Tx
|
||||
L1UserTxBytesLen = 72
|
||||
// L1CoordinatorTxBytesLen is the length of the byte array that represents the L1CoordinatorTx
|
||||
L1CoordinatorTxBytesLen = 101
|
||||
)
|
||||
@@ -27,20 +27,20 @@ type L1Tx struct {
|
||||
// where type:
|
||||
// - L1UserTx: 0
|
||||
// - L1CoordinatorTx: 1
|
||||
TxID TxID
|
||||
ToForgeL1TxsNum *int64 // toForgeL1TxsNum in which the tx was forged / will be forged
|
||||
Position int
|
||||
UserOrigin bool // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
|
||||
FromIdx Idx // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
|
||||
FromEthAddr ethCommon.Address
|
||||
FromBJJ *babyjub.PublicKey
|
||||
ToIdx Idx // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
|
||||
TokenID TokenID
|
||||
Amount *big.Int
|
||||
LoadAmount *big.Int
|
||||
EthBlockNum int64 // Ethereum Block Number in which this L1Tx was added to the queue
|
||||
Type TxType
|
||||
BatchNum *BatchNum
|
||||
TxID TxID `meddler:"id"`
|
||||
ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
|
||||
Position int `meddler:"position"`
|
||||
UserOrigin bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
|
||||
FromIdx Idx `meddler:"from_idx,zeroisnull"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
|
||||
FromEthAddr ethCommon.Address `meddler:"from_eth_addr,zeroisnull"`
|
||||
FromBJJ *babyjub.PublicKey `meddler:"from_bjj,zeroisnull"`
|
||||
ToIdx Idx `meddler:"to_idx"` // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
|
||||
TokenID TokenID `meddler:"token_id"`
|
||||
Amount *big.Int `meddler:"amount,bigint"`
|
||||
LoadAmount *big.Int `meddler:"load_amount,bigint"`
|
||||
EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
|
||||
Type TxType `meddler:"type"`
|
||||
BatchNum *BatchNum `meddler:"batch_num"`
|
||||
}
|
||||
|
||||
// NewL1Tx returns the given L1Tx with the TxId & Type parameters calculated
|
||||
@@ -146,9 +146,9 @@ func (tx L1Tx) Tx() Tx {
|
||||
return genericTx
|
||||
}
|
||||
|
||||
// Bytes encodes a L1Tx into []byte
|
||||
func (tx *L1Tx) Bytes() ([]byte, error) {
|
||||
var b [L1TxBytesLen]byte
|
||||
// BytesUser encodes a L1Tx into []byte
|
||||
func (tx *L1Tx) BytesUser() ([]byte, error) {
|
||||
var b [L1UserTxBytesLen]byte
|
||||
copy(b[0:20], tx.FromEthAddr.Bytes())
|
||||
pkCompL := tx.FromBJJ.Compress()
|
||||
pkCompB := SwapEndianness(pkCompL[:])
|
||||
@@ -193,13 +193,15 @@ func (tx *L1Tx) BytesCoordinatorTx(compressedSignatureBytes []byte) ([]byte, err
|
||||
return b[:], nil
|
||||
}
|
||||
|
||||
// L1TxFromBytes decodes a L1Tx from []byte
|
||||
func L1TxFromBytes(b []byte) (*L1Tx, error) {
|
||||
if len(b) != L1TxBytesLen {
|
||||
// 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))
|
||||
}
|
||||
|
||||
tx := &L1Tx{}
|
||||
tx := &L1Tx{
|
||||
UserOrigin: true,
|
||||
}
|
||||
var err error
|
||||
tx.FromEthAddr = ethCommon.BytesToAddress(b[0:20])
|
||||
|
||||
@@ -231,8 +233,8 @@ func L1TxFromBytes(b []byte) (*L1Tx, error) {
|
||||
return tx, nil
|
||||
}
|
||||
|
||||
// L1TxFromCoordinatorBytes decodes a L1Tx from []byte
|
||||
func L1TxFromCoordinatorBytes(b []byte) (*L1Tx, error) {
|
||||
// L1CoordinatorTxFromBytes decodes a L1Tx from []byte
|
||||
func L1CoordinatorTxFromBytes(b []byte) (*L1Tx, error) {
|
||||
if len(b) != L1CoordinatorTxBytesLen {
|
||||
return nil, fmt.Errorf("Can not parse L1CoordinatorTx bytes, expected length %d, current: %d", 101, len(b))
|
||||
}
|
||||
@@ -240,7 +242,9 @@ func L1TxFromCoordinatorBytes(b []byte) (*L1Tx, error) {
|
||||
bytesMessage1 := []byte("\x19Ethereum Signed Message:\n98")
|
||||
bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
|
||||
|
||||
tx := &L1Tx{}
|
||||
tx := &L1Tx{
|
||||
UserOrigin: false,
|
||||
}
|
||||
var err error
|
||||
// Ethereum adds 27 to v
|
||||
v := b[0] - byte(27) //nolint:gomnd
|
||||
|
||||
@@ -49,7 +49,7 @@ func TestNewL1CoordinatorTx(t *testing.T) {
|
||||
assert.Equal(t, "0x01000000000000cafe005800", l1Tx.TxID.String())
|
||||
}
|
||||
|
||||
func TestL1TxByteParsers(t *testing.T) {
|
||||
func TestL1userTxByteParsers(t *testing.T) {
|
||||
var pkComp babyjub.PublicKeyComp
|
||||
pkCompL := []byte("0x56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
|
||||
err := pkComp.UnmarshalText(pkCompL)
|
||||
@@ -59,6 +59,7 @@ func TestL1TxByteParsers(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
|
||||
l1Tx := &L1Tx{
|
||||
UserOrigin: true,
|
||||
ToIdx: 3,
|
||||
TokenID: 5,
|
||||
Amount: big.NewInt(1),
|
||||
@@ -68,21 +69,21 @@ func TestL1TxByteParsers(t *testing.T) {
|
||||
FromEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
|
||||
}
|
||||
|
||||
encodedData, err := l1Tx.Bytes()
|
||||
encodedData, err := l1Tx.BytesUser()
|
||||
require.Nil(t, err)
|
||||
decodedData, err := L1TxFromBytes(encodedData)
|
||||
decodedData, err := L1UserTxFromBytes(encodedData)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, l1Tx, decodedData)
|
||||
encodedData2, err := decodedData.Bytes()
|
||||
encodedData2, err := decodedData.BytesUser()
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, encodedData, encodedData2)
|
||||
|
||||
// expect error if length!=68
|
||||
_, err = L1TxFromBytes(encodedData[:66])
|
||||
_, err = L1UserTxFromBytes(encodedData[:66])
|
||||
require.NotNil(t, err)
|
||||
_, err = L1TxFromBytes([]byte{})
|
||||
_, err = L1UserTxFromBytes([]byte{})
|
||||
require.NotNil(t, err)
|
||||
_, err = L1TxFromBytes(nil)
|
||||
_, err = L1UserTxFromBytes(nil)
|
||||
require.NotNil(t, err)
|
||||
}
|
||||
|
||||
@@ -113,7 +114,7 @@ func TestL1TxByteParsersCompatibility(t *testing.T) {
|
||||
expected, err := utils.HexDecode("85dab5b9e2e361d0c208d77be90efcc0439b0a530dd02deb2c81068e7a0f7e327df80b4ab79ee1f41a7def613e73a20c32eece5a000001c638db8be880f00020039c0000053cb88d")
|
||||
require.Nil(t, err)
|
||||
|
||||
encodedData, err := l1Tx.Bytes()
|
||||
encodedData, err := l1Tx.BytesUser()
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, expected, encodedData)
|
||||
}
|
||||
@@ -161,7 +162,7 @@ func TestL1CoordinatorTxByteParsers(t *testing.T) {
|
||||
|
||||
bytesCoordinatorL1, err := l1Tx.BytesCoordinatorTx(signature)
|
||||
require.Nil(t, err)
|
||||
l1txDecoded, err := L1TxFromCoordinatorBytes(bytesCoordinatorL1)
|
||||
l1txDecoded, err := L1CoordinatorTxFromBytes(bytesCoordinatorL1)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, l1Tx, l1txDecoded)
|
||||
bytesCoordinatorL12, err := l1txDecoded.BytesCoordinatorTx(signature)
|
||||
@@ -169,11 +170,11 @@ func TestL1CoordinatorTxByteParsers(t *testing.T) {
|
||||
assert.Equal(t, bytesCoordinatorL1, bytesCoordinatorL12)
|
||||
|
||||
// expect error if length!=68
|
||||
_, err = L1TxFromCoordinatorBytes(bytesCoordinatorL1[:66])
|
||||
_, err = L1CoordinatorTxFromBytes(bytesCoordinatorL1[:66])
|
||||
require.NotNil(t, err)
|
||||
_, err = L1TxFromCoordinatorBytes([]byte{})
|
||||
_, err = L1CoordinatorTxFromBytes([]byte{})
|
||||
require.NotNil(t, err)
|
||||
_, err = L1TxFromCoordinatorBytes(nil)
|
||||
_, err = L1CoordinatorTxFromBytes(nil)
|
||||
require.NotNil(t, err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user