hermez-node/test/lang_test.go
test/lang_test.goRaw
1package test
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 // deposits
17 Deposit(1) A: 10
18 Deposit(2) A: 20
19 Deposit(1) B: 5
20 CreateAccountDeposit(1) C: 5
21 CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
22
23 // L2 transactions
24 Transfer(1) A-B: 6 (1)
25 Transfer(1) B-D: 3 (1)
26
27 // set new batch
28 > batch
29
30 DepositTransfer(1) A-B: 15, 10 (1)
31 Transfer(1) C-A : 3 (1)
32 Transfer(2) A-B: 15 (1)
33
34 Deposit(1) User0: 20
35 Deposit(3) User1: 20
36 Transfer(1) User0-User1: 15 (1)
37 Transfer(3) User1-User0: 15 (1)
38
39 > batch
40
41 Transfer(1) User1-User0: 1 (1)
42
43 > batch
44 > block
45
46 // Exits
47 Exit(1) A: 5
48 `
49
50 parser := NewParser(strings.NewReader(s))
51 instructions, err := parser.Parse(false)
52 require.Nil(t, err)
53 assert.Equal(t, 20, len(instructions.Instructions))
54 assert.Equal(t, 10, len(instructions.Accounts))
55 assert.Equal(t, 3, len(instructions.TokenIDs))
56
57 if debug {
58 fmt.Println(instructions)
59 for _, instruction := range instructions.Instructions {
60 fmt.Println(instruction.Raw())
61 }
62 }
63
64 assert.Equal(t, TypeNewBatch, instructions.Instructions[7].Type)
65 assert.Equal(t, "Deposit(1)User0:20", instructions.Instructions[11].Raw())
66 assert.Equal(t, "Type: DepositTransfer, From: A, To: B, LoadAmount: 15, Amount: 10, Fee: 1, TokenID: 1\n", instructions.Instructions[8].String())
67 assert.Equal(t, "Type: Transfer, From: User1, To: User0, Amount: 15, Fee: 1, TokenID: 3\n", instructions.Instructions[14].String())
68 assert.Equal(t, "Transfer(2)A-B:15(1)", instructions.Instructions[10].Raw())
69 assert.Equal(t, "Type: Transfer, From: A, To: B, Amount: 15, Fee: 1, TokenID: 2\n", instructions.Instructions[10].String())
70 assert.Equal(t, "Exit(1)A:5", instructions.Instructions[19].Raw())
71 assert.Equal(t, "Type: Exit, From: A, Amount: 5, TokenID: 1\n", instructions.Instructions[19].String())
72}
73
74func TestParsePoolTxs(t *testing.T) {
75 s := `
76 PoolTransfer(1) A-B: 6 (1)
77 PoolTransfer(2) A-B: 3 (3)
78 PoolTransfer(1) B-D: 3 (1)
79 PoolTransfer(1) C-D: 3 (1)
80 PoolExit(1) A: 5
81 `
82
83 parser := NewParser(strings.NewReader(s))
84 instructions, err := parser.Parse(true)
85 require.Nil(t, err)
86 assert.Equal(t, 5, len(instructions.Instructions))
87 assert.Equal(t, 6, len(instructions.Accounts))
88 assert.Equal(t, 2, len(instructions.TokenIDs))
89
90 if debug {
91 fmt.Println(instructions)
92 for _, instruction := range instructions.Instructions {
93 fmt.Println(instruction.Raw())
94 }
95 }
96
97 assert.Equal(t, "Transfer(1)A-B:6(1)", instructions.Instructions[0].Raw())
98 assert.Equal(t, "Transfer(2)A-B:3(3)", instructions.Instructions[1].Raw())
99 assert.Equal(t, "Transfer(1)B-D:3(1)", instructions.Instructions[2].Raw())
100 assert.Equal(t, "Transfer(1)C-D:3(1)", instructions.Instructions[3].Raw())
101 assert.Equal(t, "Exit(1)A:5", instructions.Instructions[4].Raw())
102}
103
104func TestParseErrors(t *testing.T) {
105 s := "Deposit(1) A:: 10"
106 parser := NewParser(strings.NewReader(s))
107 _, err := parser.Parse(false)
108 assert.Equal(t, "error parsing line 0: Deposit(1)A:: 10, err: strconv.Atoi: parsing \":\": invalid syntax", err.Error())
109
110 s = "Deposit(1) A: 10 20"
111 parser = NewParser(strings.NewReader(s))
112 _, err = parser.Parse(false)
113 assert.Equal(t, "error parsing line 1: 20, err: Unexpected tx type: 20", err.Error())
114
115 s = "Transfer(1) A: 10"
116 parser = NewParser(strings.NewReader(s))
117 _, err = parser.Parse(false)
118 assert.Equal(t, "error parsing line 0: Transfer(1)A:, err: Expected '-', found ':'", err.Error())
119
120 s = "Transfer(1) A B: 10"
121 parser = NewParser(strings.NewReader(s))
122 _, err = parser.Parse(false)
123 assert.Equal(t, "error parsing line 0: Transfer(1)AB, err: Expected '-', found 'B'", err.Error())
124
125 s = "Transfer(1) A-B: 10 (255)"
126 parser = NewParser(strings.NewReader(s))
127 _, err = parser.Parse(false)
128 assert.Nil(t, err)
129 s = "Transfer(1) A-B: 10 (256)"
130 parser = NewParser(strings.NewReader(s))
131 _, err = parser.Parse(false)
132 assert.Equal(t, "error parsing line 0: Transfer(1)A-B:10(256), err: Fee 256 can not be bigger than 255", err.Error())
133
134 // check that the PoolTransfer & Transfer are only accepted in the
135 // correct case case (PoolTxs/BlockchainTxs)
136 s = "Transfer(1) A-B: 10 (1)"
137 parser = NewParser(strings.NewReader(s))
138 _, err = parser.Parse(true)
139 assert.Equal(t, "error parsing line 0: Transfer, err: Unexpected 'Transfer' at PoolL2Txs set", err.Error())
140 s = "PoolTransfer(1) A-B: 10 (1)"
141 parser = NewParser(strings.NewReader(s))
142 _, err = parser.Parse(false)
143 assert.Equal(t, "error parsing line 0: PoolTransfer, err: Unexpected 'PoolTransfer' at BlockchainTxs set", err.Error())
144
145 s = "> btch"
146 parser = NewParser(strings.NewReader(s))
147 _, err = parser.Parse(false)
148 assert.Equal(t, "error parsing line 0: >, err: Unexpected '> btch', expected '> batch' or '> block'", err.Error())
149}