arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/common/l2tx_test.go
common/l2tx_test.goRaw
 1package common
 2
 3import (
 4	"encoding/hex"
 5	"math/big"
 6	"testing"
 7
 8	"github.com/stretchr/testify/assert"
 9	"github.com/stretchr/testify/require"
10)
11
12func TestNewL2Tx(t *testing.T) {
13	l2Tx := &L2Tx{
14		FromIdx: 87654,
15		ToIdx:   300,
16		TokenID: 5,
17		Amount:  big.NewInt(4),
18		Nonce:   144,
19	}
20	l2Tx, err := NewL2Tx(l2Tx)
21	assert.NoError(t, err)
22	assert.Equal(t, "0x024f67ec893467419cdfacfc9152e55dc7ce16088952bf2b3442732fd046bd031c", l2Tx.TxID.String())
23}
24
25func TestL2TxByteParsers(t *testing.T) {
26	amount := new(big.Int)
27	amount.SetString("79000000", 10)
28	l2Tx := &L2Tx{
29		ToIdx:   256,
30		Amount:  amount,
31		FromIdx: 257,
32		Fee:     201,
33	}
34	// Data from the compatibility test
35	expected := "00000101000001002b16c9"
36	encodedData, err := l2Tx.BytesDataAvailability(32)
37	require.NoError(t, err)
38	assert.Equal(t, expected, hex.EncodeToString(encodedData))
39
40	decodedData, err := L2TxFromBytesDataAvailability(encodedData, 32)
41	require.NoError(t, err)
42	assert.Equal(t, l2Tx, decodedData)
43}