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