arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/test/transakcio/lang_test.go
test/transakcio/lang_test.goRaw
  1package transakcio
  2
  3import (
  4	"fmt"
  5	"strings"
  6	"testing"
  7
  8	"github.com/stretchr/testify/assert"
  9	"github.com/stretchr/testify/require"
 10)
 11
 12var debug = false
 13
 14func TestParseBlockchainTxs(t *testing.T) {
 15	s := `
 16		Type: Blockchain
 17		// deposits
 18		Deposit(1) A: 10
 19		Deposit(2) A: 20
 20		Deposit(1) B: 5
 21		CreateAccountDeposit(1) C: 5
 22		CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
 23		CreateAccountDepositCoordinator(1) E
 24
 25		// L2 transactions
 26		Transfer(1) A-B: 6 (1)
 27		Transfer(1) B-D: 3 (1)
 28		Transfer(1) A-E: 1 (1)
 29
 30		// set new batch
 31		> batch
 32
 33		DepositTransfer(1) A-B: 15, 10 (1)
 34		Transfer(1) C-A : 3 (1)
 35		Transfer(2) A-B: 15 (1)
 36
 37		Deposit(1) User0: 20
 38		Deposit(3) User1: 20
 39		Transfer(1) User0-User1: 15 (1)
 40		Transfer(3) User1-User0: 15 (1)
 41
 42		> batch
 43
 44		Transfer(1) User1-User0: 1 (1)
 45
 46		> batch
 47		> block
 48
 49		// Exits
 50		Exit(1) A: 5
 51	`
 52
 53	parser := newParser(strings.NewReader(s))
 54	instructions, err := parser.parse()
 55	require.Nil(t, err)
 56	assert.Equal(t, 22, len(instructions.instructions))
 57	assert.Equal(t, 7, len(instructions.accounts))
 58	assert.Equal(t, 3, len(instructions.tokenIDs))
 59
 60	if debug {
 61		fmt.Println(instructions)
 62		for _, instruction := range instructions.instructions {
 63			fmt.Println(instruction.raw())
 64		}
 65	}
 66
 67	assert.Equal(t, txTypeCreateAccountDepositCoordinator, instructions.instructions[5].typ)
 68	assert.Equal(t, typeNewBatch, instructions.instructions[9].typ)
 69	assert.Equal(t, "Deposit(1)User0:20", instructions.instructions[13].raw())
 70	assert.Equal(t, "Type: DepositTransfer, From: A, To: B, LoadAmount: 15, Amount: 10, Fee: 1, TokenID: 1\n", instructions.instructions[10].String())
 71	assert.Equal(t, "Type: Transfer, From: User1, To: User0, Amount: 15, Fee: 1, TokenID: 3\n", instructions.instructions[16].String())
 72	assert.Equal(t, "Transfer(2)A-B:15(1)", instructions.instructions[12].raw())
 73	assert.Equal(t, "Type: Transfer, From: A, To: B, Amount: 15, Fee: 1, TokenID: 2\n", instructions.instructions[12].String())
 74	assert.Equal(t, "Exit(1)A:5", instructions.instructions[21].raw())
 75	assert.Equal(t, "Type: Exit, From: A, Amount: 5, TokenID: 1\n", instructions.instructions[21].String())
 76}
 77
 78func TestParsePoolTxs(t *testing.T) {
 79	s := `
 80		Type: PoolL2
 81		PoolTransfer(1) A-B: 6 (1)
 82		PoolTransfer(2) A-B: 3 (3)
 83		PoolTransfer(1) B-D: 3 (1)
 84		PoolTransfer(1) C-D: 3 (1)
 85		PoolExit(1) A: 5
 86	`
 87
 88	parser := newParser(strings.NewReader(s))
 89	instructions, err := parser.parse()
 90	require.Nil(t, err)
 91	assert.Equal(t, 5, len(instructions.instructions))
 92	assert.Equal(t, 4, len(instructions.accounts))
 93	assert.Equal(t, 2, len(instructions.tokenIDs))
 94
 95	if debug {
 96		fmt.Println(instructions)
 97		for _, instruction := range instructions.instructions {
 98			fmt.Println(instruction.raw())
 99		}
100	}
101
102	assert.Equal(t, "Transfer(1)A-B:6(1)", instructions.instructions[0].raw())
103	assert.Equal(t, "Transfer(2)A-B:3(3)", instructions.instructions[1].raw())
104	assert.Equal(t, "Transfer(1)B-D:3(1)", instructions.instructions[2].raw())
105	assert.Equal(t, "Transfer(1)C-D:3(1)", instructions.instructions[3].raw())
106	assert.Equal(t, "Exit(1)A:5", instructions.instructions[4].raw())
107}
108
109func TestParseErrors(t *testing.T) {
110	s := `
111		Type: Blockchain
112		Deposit(1) A:: 10
113	`
114	parser := newParser(strings.NewReader(s))
115	_, err := parser.parse()
116	assert.Equal(t, "error parsing line 1: Deposit(1)A:: 10\n, err: strconv.Atoi: parsing \":\": invalid syntax", err.Error())
117
118	s = `
119		Type: Blockchain
120		Deposit(1) A: 10 20
121	`
122	parser = newParser(strings.NewReader(s))
123	_, err = parser.parse()
124	assert.Equal(t, "error parsing line 2: 20, err: Unexpected Blockchain tx type: 20", err.Error())
125
126	s = `
127		Type: Blockchain
128		Transfer(1) A: 10
129	`
130	parser = newParser(strings.NewReader(s))
131	_, err = parser.parse()
132	assert.Equal(t, "error parsing line 1: Transfer(1)A:, err: Expected '-', found ':'", err.Error())
133
134	s = `
135		Type: Blockchain
136		Transfer(1) A B: 10
137	`
138	parser = newParser(strings.NewReader(s))
139	_, err = parser.parse()
140	assert.Equal(t, "error parsing line 1: Transfer(1)AB, err: Expected '-', found 'B'", err.Error())
141
142	s = `
143		Type: Blockchain
144		Transfer(1) A-B: 10 (255)
145	`
146	parser = newParser(strings.NewReader(s))
147	_, err = parser.parse()
148	assert.Nil(t, err)
149	s = `
150		Type: Blockchain
151		Transfer(1) A-B: 10 (256)
152	`
153	parser = newParser(strings.NewReader(s))
154	_, err = parser.parse()
155	assert.Equal(t, "error parsing line 1: Transfer(1)A-B:10(256)\n, err: Fee 256 can not be bigger than 255", err.Error())
156
157	// check that the PoolTransfer & Transfer are only accepted in the
158	// correct case case (PoolTxs/BlockchainTxs)
159	s = `
160		Type: PoolL2
161		Transfer(1) A-B: 10 (1)
162	`
163	parser = newParser(strings.NewReader(s))
164	_, err = parser.parse()
165	assert.Equal(t, "error parsing line 1: Transfer, err: Unexpected PoolL2 tx type: Transfer", err.Error())
166	s = `
167		Type: Blockchain
168		PoolTransfer(1) A-B: 10 (1)
169	`
170	parser = newParser(strings.NewReader(s))
171	_, err = parser.parse()
172	assert.Equal(t, "error parsing line 1: PoolTransfer, err: Unexpected Blockchain tx type: PoolTransfer", err.Error())
173
174	s = `
175		Type: Blockchain
176		> btch
177	`
178	parser = newParser(strings.NewReader(s))
179	_, err = parser.parse()
180	assert.Equal(t, "error parsing line 1: >, err: Unexpected '> btch', expected '> batch' or '> block'", err.Error())
181
182	// check definition of set Type
183	s = `PoolTransfer(1) A-B: 10 (1)`
184	parser = newParser(strings.NewReader(s))
185	_, err = parser.parse()
186	assert.Equal(t, "error parsing line 0: PoolTransfer, err: Set type not defined", err.Error())
187	s = `Type: PoolL1`
188	parser = newParser(strings.NewReader(s))
189	_, err = parser.parse()
190	assert.Equal(t, "error parsing line 0: Type:, err: Invalid set type: 'PoolL1'. Valid set types: 'Blockchain', 'PoolL2'", err.Error())
191	s = `Type: PoolL1
192		Type: Blockchain`
193	parser = newParser(strings.NewReader(s))
194	_, err = parser.parse()
195	assert.Equal(t, "error parsing line 0: Type:, err: Invalid set type: 'PoolL1'. Valid set types: 'Blockchain', 'PoolL2'", err.Error())
196}