Feature/transakciofeature/sql-semaphore1
@ -1,381 +0,0 @@ |
|||||
package test |
|
||||
|
|
||||
import ( |
|
||||
"bufio" |
|
||||
"bytes" |
|
||||
"fmt" |
|
||||
"io" |
|
||||
"sort" |
|
||||
"strconv" |
|
||||
|
|
||||
"github.com/hermeznetwork/hermez-node/common" |
|
||||
) |
|
||||
|
|
||||
var eof = rune(0) |
|
||||
var errof = fmt.Errorf("eof in parseline") |
|
||||
var errComment = fmt.Errorf("comment in parseline") |
|
||||
var errNewBatch = fmt.Errorf("newbatch") |
|
||||
|
|
||||
// TypeNewBatch is used for testing purposes only, and represents the
|
|
||||
// common.TxType of a new batch
|
|
||||
var TypeNewBatch common.TxType = "TxTypeNewBatch" |
|
||||
|
|
||||
//nolint
|
|
||||
const ( |
|
||||
ILLEGAL token = iota |
|
||||
WS |
|
||||
EOF |
|
||||
|
|
||||
IDENT // val
|
|
||||
) |
|
||||
|
|
||||
// Instruction is the data structure that represents one line of code
|
|
||||
type Instruction struct { |
|
||||
Literal string |
|
||||
From string |
|
||||
To string |
|
||||
Amount uint64 |
|
||||
Fee uint8 |
|
||||
TokenID common.TokenID |
|
||||
Type common.TxType // D: Deposit, T: Transfer, E: ForceExit
|
|
||||
} |
|
||||
|
|
||||
// Instructions contains the full Set of Instructions representing a full code
|
|
||||
type Instructions struct { |
|
||||
Instructions []*Instruction |
|
||||
Accounts []string |
|
||||
TokenIDs []common.TokenID |
|
||||
} |
|
||||
|
|
||||
func (i Instruction) String() string { |
|
||||
buf := bytes.NewBufferString("") |
|
||||
switch i.Type { |
|
||||
case common.TxTypeCreateAccountDeposit: |
|
||||
fmt.Fprintf(buf, "Type: Create&Deposit, ") |
|
||||
case common.TxTypeTransfer: |
|
||||
fmt.Fprintf(buf, "Type: Transfer, ") |
|
||||
case common.TxTypeForceExit: |
|
||||
fmt.Fprintf(buf, "Type: ForceExit, ") |
|
||||
default: |
|
||||
} |
|
||||
fmt.Fprintf(buf, "From: %s, ", i.From) |
|
||||
if i.Type == common.TxTypeTransfer { |
|
||||
fmt.Fprintf(buf, "To: %s, ", i.To) |
|
||||
} |
|
||||
fmt.Fprintf(buf, "Amount: %d, ", i.Amount) |
|
||||
if i.Type == common.TxTypeTransfer { |
|
||||
fmt.Fprintf(buf, "Fee: %d, ", i.Fee) |
|
||||
} |
|
||||
fmt.Fprintf(buf, "TokenID: %d,\n", i.TokenID) |
|
||||
return buf.String() |
|
||||
} |
|
||||
|
|
||||
// Raw returns a string with the raw representation of the Instruction
|
|
||||
func (i Instruction) Raw() string { |
|
||||
buf := bytes.NewBufferString("") |
|
||||
fmt.Fprintf(buf, "%s", i.From) |
|
||||
if i.Type == common.TxTypeTransfer { |
|
||||
fmt.Fprintf(buf, "-%s", i.To) |
|
||||
} |
|
||||
fmt.Fprintf(buf, " (%d)", i.TokenID) |
|
||||
if i.Type == common.TxTypeForceExit { |
|
||||
fmt.Fprintf(buf, "E") |
|
||||
} |
|
||||
fmt.Fprintf(buf, ":") |
|
||||
fmt.Fprintf(buf, " %d", i.Amount) |
|
||||
if i.Type == common.TxTypeTransfer { |
|
||||
fmt.Fprintf(buf, " %d", i.Fee) |
|
||||
} |
|
||||
return buf.String() |
|
||||
} |
|
||||
|
|
||||
type token int |
|
||||
|
|
||||
type scanner struct { |
|
||||
r *bufio.Reader |
|
||||
} |
|
||||
|
|
||||
func isWhitespace(ch rune) bool { |
|
||||
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\v' || ch == '\f' |
|
||||
} |
|
||||
|
|
||||
func isLetter(ch rune) bool { |
|
||||
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') |
|
||||
} |
|
||||
|
|
||||
func isComment(ch rune) bool { |
|
||||
return ch == '/' |
|
||||
} |
|
||||
|
|
||||
func isDigit(ch rune) bool { |
|
||||
return (ch >= '0' && ch <= '9') |
|
||||
} |
|
||||
|
|
||||
// newScanner creates a new scanner with the given io.Reader
|
|
||||
func newScanner(r io.Reader) *scanner { |
|
||||
return &scanner{r: bufio.NewReader(r)} |
|
||||
} |
|
||||
|
|
||||
func (s *scanner) read() rune { |
|
||||
ch, _, err := s.r.ReadRune() |
|
||||
if err != nil { |
|
||||
return eof |
|
||||
} |
|
||||
return ch |
|
||||
} |
|
||||
|
|
||||
func (s *scanner) unread() { |
|
||||
_ = s.r.UnreadRune() |
|
||||
} |
|
||||
|
|
||||
// scan returns the token and literal string of the current value
|
|
||||
func (s *scanner) scan() (tok token, lit string) { |
|
||||
ch := s.read() |
|
||||
|
|
||||
if isWhitespace(ch) { |
|
||||
// space
|
|
||||
s.unread() |
|
||||
return s.scanWhitespace() |
|
||||
} else if isLetter(ch) || isDigit(ch) { |
|
||||
// letter/digit
|
|
||||
s.unread() |
|
||||
return s.scanIndent() |
|
||||
} else if isComment(ch) { |
|
||||
// comment
|
|
||||
s.unread() |
|
||||
return s.scanIndent() |
|
||||
} |
|
||||
|
|
||||
if ch == eof { |
|
||||
return EOF, "" |
|
||||
} |
|
||||
|
|
||||
return ILLEGAL, string(ch) |
|
||||
} |
|
||||
|
|
||||
func (s *scanner) scanWhitespace() (token token, lit string) { |
|
||||
var buf bytes.Buffer |
|
||||
buf.WriteRune(s.read()) |
|
||||
|
|
||||
for { |
|
||||
if ch := s.read(); ch == eof { |
|
||||
break |
|
||||
} else if !isWhitespace(ch) { |
|
||||
s.unread() |
|
||||
break |
|
||||
} else { |
|
||||
_, _ = buf.WriteRune(ch) |
|
||||
} |
|
||||
} |
|
||||
return WS, buf.String() |
|
||||
} |
|
||||
|
|
||||
func (s *scanner) scanIndent() (tok token, lit string) { |
|
||||
var buf bytes.Buffer |
|
||||
buf.WriteRune(s.read()) |
|
||||
|
|
||||
for { |
|
||||
if ch := s.read(); ch == eof { |
|
||||
break |
|
||||
} else if !isLetter(ch) && !isDigit(ch) { |
|
||||
s.unread() |
|
||||
break |
|
||||
} else { |
|
||||
_, _ = buf.WriteRune(ch) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
if len(buf.String()) == 1 { |
|
||||
return token(rune(buf.String()[0])), buf.String() |
|
||||
} |
|
||||
return IDENT, buf.String() |
|
||||
} |
|
||||
|
|
||||
// Parser defines the parser
|
|
||||
type Parser struct { |
|
||||
s *scanner |
|
||||
buf struct { |
|
||||
tok token |
|
||||
lit string |
|
||||
n int |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// NewParser creates a new parser from a io.Reader
|
|
||||
func NewParser(r io.Reader) *Parser { |
|
||||
return &Parser{s: newScanner(r)} |
|
||||
} |
|
||||
|
|
||||
func (p *Parser) scan() (tok token, lit string) { |
|
||||
// if there is a token in the buffer return it
|
|
||||
if p.buf.n != 0 { |
|
||||
p.buf.n = 0 |
|
||||
return p.buf.tok, p.buf.lit |
|
||||
} |
|
||||
tok, lit = p.s.scan() |
|
||||
|
|
||||
p.buf.tok, p.buf.lit = tok, lit |
|
||||
|
|
||||
return |
|
||||
} |
|
||||
|
|
||||
func (p *Parser) scanIgnoreWhitespace() (tok token, lit string) { |
|
||||
tok, lit = p.scan() |
|
||||
if tok == WS { |
|
||||
tok, lit = p.scan() |
|
||||
} |
|
||||
return |
|
||||
} |
|
||||
|
|
||||
// parseLine parses the current line
|
|
||||
func (p *Parser) parseLine() (*Instruction, error) { |
|
||||
// line can be Deposit:
|
|
||||
// A (1): 10
|
|
||||
// or Transfer:
|
|
||||
// A-B (1): 6
|
|
||||
// or Withdraw:
|
|
||||
// A (1) E: 4
|
|
||||
// or NextBatch:
|
|
||||
// > and here the comment
|
|
||||
|
|
||||
c := &Instruction{} |
|
||||
tok, lit := p.scanIgnoreWhitespace() |
|
||||
if tok == EOF { |
|
||||
return nil, errof |
|
||||
} |
|
||||
c.Literal += lit |
|
||||
if lit == "/" { |
|
||||
_, _ = p.s.r.ReadString('\n') |
|
||||
return nil, errComment |
|
||||
} else if lit == ">" { |
|
||||
_, _ = p.s.r.ReadString('\n') |
|
||||
return nil, errNewBatch |
|
||||
} |
|
||||
c.From = lit |
|
||||
|
|
||||
_, lit = p.scanIgnoreWhitespace() |
|
||||
c.Literal += lit |
|
||||
if lit == "-" { |
|
||||
// transfer
|
|
||||
_, lit = p.scanIgnoreWhitespace() |
|
||||
c.Literal += lit |
|
||||
c.To = lit |
|
||||
c.Type = common.TxTypeTransfer |
|
||||
_, lit = p.scanIgnoreWhitespace() // expect (
|
|
||||
c.Literal += lit |
|
||||
if lit != "(" { |
|
||||
line, _ := p.s.r.ReadString('\n') |
|
||||
c.Literal += line |
|
||||
return c, fmt.Errorf("Expected '(', found '%s'", lit) |
|
||||
} |
|
||||
} else { |
|
||||
c.Type = common.TxTypeCreateAccountDeposit |
|
||||
} |
|
||||
|
|
||||
_, lit = p.scanIgnoreWhitespace() |
|
||||
c.Literal += lit |
|
||||
tidI, err := strconv.Atoi(lit) |
|
||||
if err != nil { |
|
||||
line, _ := p.s.r.ReadString('\n') |
|
||||
c.Literal += line |
|
||||
return c, err |
|
||||
} |
|
||||
c.TokenID = common.TokenID(tidI) |
|
||||
_, lit = p.scanIgnoreWhitespace() // expect )
|
|
||||
c.Literal += lit |
|
||||
if lit != ")" { |
|
||||
line, _ := p.s.r.ReadString('\n') |
|
||||
c.Literal += line |
|
||||
return c, fmt.Errorf("Expected ')', found '%s'", lit) |
|
||||
} |
|
||||
|
|
||||
_, lit = p.scanIgnoreWhitespace() // expect ':' or 'E' (Exit type)
|
|
||||
c.Literal += lit |
|
||||
if lit == "E" { |
|
||||
c.Type = common.TxTypeForceExit |
|
||||
_, lit = p.scanIgnoreWhitespace() // expect ':'
|
|
||||
c.Literal += lit |
|
||||
} |
|
||||
if lit != ":" { |
|
||||
line, _ := p.s.r.ReadString('\n') |
|
||||
c.Literal += line |
|
||||
return c, fmt.Errorf("Expected ':', found '%s'", lit) |
|
||||
} |
|
||||
tok, lit = p.scanIgnoreWhitespace() |
|
||||
c.Literal += lit |
|
||||
amount, err := strconv.Atoi(lit) |
|
||||
if err != nil { |
|
||||
line, _ := p.s.r.ReadString('\n') |
|
||||
c.Literal += line |
|
||||
return c, err |
|
||||
} |
|
||||
c.Amount = uint64(amount) |
|
||||
|
|
||||
if c.Type == common.TxTypeTransfer { |
|
||||
tok, lit = p.scanIgnoreWhitespace() |
|
||||
c.Literal += lit |
|
||||
fee, err := strconv.Atoi(lit) |
|
||||
if err != nil { |
|
||||
line, _ := p.s.r.ReadString('\n') |
|
||||
c.Literal += line |
|
||||
return c, err |
|
||||
} |
|
||||
if fee > common.MaxFeePlan-1 { |
|
||||
line, _ := p.s.r.ReadString('\n') |
|
||||
c.Literal += line |
|
||||
return c, fmt.Errorf("Fee %d can not be bigger than 255", fee) |
|
||||
} |
|
||||
c.Fee = uint8(fee) |
|
||||
} |
|
||||
|
|
||||
if tok == EOF { |
|
||||
return nil, errof |
|
||||
} |
|
||||
return c, nil |
|
||||
} |
|
||||
|
|
||||
func idxTokenIDToString(idx string, tid common.TokenID) string { |
|
||||
return idx + strconv.Itoa(int(tid)) |
|
||||
} |
|
||||
|
|
||||
// Parse parses through reader
|
|
||||
func (p *Parser) Parse() (Instructions, error) { |
|
||||
var instructions Instructions |
|
||||
i := 0 |
|
||||
accounts := make(map[string]bool) |
|
||||
tokenids := make(map[common.TokenID]bool) |
|
||||
for { |
|
||||
instruction, err := p.parseLine() |
|
||||
if err == errof { |
|
||||
break |
|
||||
} |
|
||||
if err == errComment { |
|
||||
i++ |
|
||||
continue |
|
||||
} |
|
||||
if err == errNewBatch { |
|
||||
i++ |
|
||||
inst := &Instruction{Type: TypeNewBatch} |
|
||||
instructions.Instructions = append(instructions.Instructions, inst) |
|
||||
continue |
|
||||
} |
|
||||
if err != nil { |
|
||||
return instructions, fmt.Errorf("error parsing line %d: %s, err: %s", i, instruction.Literal, err.Error()) |
|
||||
} |
|
||||
instructions.Instructions = append(instructions.Instructions, instruction) |
|
||||
accounts[idxTokenIDToString(instruction.From, instruction.TokenID)] = true |
|
||||
if instruction.Type == common.TxTypeTransfer { // type: Transfer
|
|
||||
accounts[idxTokenIDToString(instruction.To, instruction.TokenID)] = true |
|
||||
} |
|
||||
tokenids[instruction.TokenID] = true |
|
||||
i++ |
|
||||
} |
|
||||
for a := range accounts { |
|
||||
instructions.Accounts = append(instructions.Accounts, a) |
|
||||
} |
|
||||
sort.Strings(instructions.Accounts) |
|
||||
for tid := range tokenids { |
|
||||
instructions.TokenIDs = append(instructions.TokenIDs, tid) |
|
||||
} |
|
||||
return instructions, nil |
|
||||
} |
|
@ -1,86 +0,0 @@ |
|||||
package test |
|
||||
|
|
||||
import ( |
|
||||
"fmt" |
|
||||
"strings" |
|
||||
"testing" |
|
||||
|
|
||||
"github.com/stretchr/testify/assert" |
|
||||
) |
|
||||
|
|
||||
var debug = false |
|
||||
|
|
||||
func TestParse(t *testing.T) { |
|
||||
s := ` |
|
||||
// deposits
|
|
||||
A (1): 10 |
|
||||
A (2): 20 |
|
||||
B (1): 5 |
|
||||
|
|
||||
// L2 transactions
|
|
||||
A-B (1): 6 1 |
|
||||
B-C (1): 3 1 |
|
||||
|
|
||||
// set new batch, label does not affect
|
|
||||
> batch1 |
|
||||
|
|
||||
C-A (1): 3 1 |
|
||||
A-B (2): 15 1 |
|
||||
|
|
||||
User0 (1): 20 |
|
||||
User1 (3) : 20 |
|
||||
User0-User1 (1): 15 1 |
|
||||
User1-User0 (3): 15 1 |
|
||||
|
|
||||
// Exits
|
|
||||
A (1) E: 5 |
|
||||
` |
|
||||
parser := NewParser(strings.NewReader(s)) |
|
||||
instructions, err := parser.Parse() |
|
||||
assert.Nil(t, err) |
|
||||
assert.Equal(t, 13, len(instructions.Instructions)) |
|
||||
// assert.Equal(t, 5, len(instructions.Accounts))
|
|
||||
fmt.Println(instructions.Accounts) |
|
||||
assert.Equal(t, 3, len(instructions.TokenIDs)) |
|
||||
|
|
||||
if debug { |
|
||||
fmt.Println(instructions) |
|
||||
for _, instruction := range instructions.Instructions { |
|
||||
fmt.Println(instruction.Raw()) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
assert.Equal(t, TypeNewBatch, instructions.Instructions[5].Type) |
|
||||
assert.Equal(t, "User0 (1): 20", instructions.Instructions[8].Raw()) |
|
||||
assert.Equal(t, "Type: Create&Deposit, From: User0, Amount: 20, TokenID: 1,\n", instructions.Instructions[8].String()) |
|
||||
assert.Equal(t, "User0-User1 (1): 15 1", instructions.Instructions[10].Raw()) |
|
||||
assert.Equal(t, "Type: Transfer, From: User0, To: User1, Amount: 15, Fee: 1, TokenID: 1,\n", instructions.Instructions[10].String()) |
|
||||
assert.Equal(t, "A (1)E: 5", instructions.Instructions[12].Raw()) |
|
||||
assert.Equal(t, "Type: ForceExit, From: A, Amount: 5, TokenID: 1,\n", instructions.Instructions[12].String()) |
|
||||
} |
|
||||
|
|
||||
func TestParseErrors(t *testing.T) { |
|
||||
s := "A (1):: 10" |
|
||||
parser := NewParser(strings.NewReader(s)) |
|
||||
_, err := parser.Parse() |
|
||||
assert.Equal(t, "error parsing line 0: A(1):: 10, err: strconv.Atoi: parsing \":\": invalid syntax", err.Error()) |
|
||||
|
|
||||
s = "A (1): 10 20" |
|
||||
parser = NewParser(strings.NewReader(s)) |
|
||||
_, err = parser.Parse() |
|
||||
assert.Equal(t, "error parsing line 1: 20, err: strconv.Atoi: parsing \"\": invalid syntax", err.Error()) |
|
||||
|
|
||||
s = "A B (1): 10" |
|
||||
parser = NewParser(strings.NewReader(s)) |
|
||||
_, err = parser.Parse() |
|
||||
assert.Equal(t, "error parsing line 0: AB(1): 10, err: strconv.Atoi: parsing \"(\": invalid syntax", err.Error()) |
|
||||
|
|
||||
s = "A-B (1): 10 255" |
|
||||
parser = NewParser(strings.NewReader(s)) |
|
||||
_, err = parser.Parse() |
|
||||
assert.Nil(t, err) |
|
||||
s = "A-B (1): 10 256" |
|
||||
parser = NewParser(strings.NewReader(s)) |
|
||||
_, err = parser.Parse() |
|
||||
assert.Equal(t, "error parsing line 0: A-B(1):10256, err: Fee 256 can not be bigger than 255", err.Error()) |
|
||||
} |
|
@ -1,166 +0,0 @@ |
|||||
package test |
|
||||
|
|
||||
// sets of instructions to use in tests of other packages
|
|
||||
|
|
||||
// line can be Deposit:
|
|
||||
// A (1): 10
|
|
||||
// (deposit to A, TokenID 1, 10 units)
|
|
||||
// or Transfer:
|
|
||||
// A-B (1): 6 1
|
|
||||
// (transfer from A to B, TokenID 1, 6 units, with fee 1)
|
|
||||
// or Withdraw:
|
|
||||
// A (1) E: 4
|
|
||||
// exit to A, TokenID 1, 4 units)
|
|
||||
// or NextBatch:
|
|
||||
// > and here the comment
|
|
||||
// move one batch forward
|
|
||||
|
|
||||
// SetTest0 has 3 batches, 29 different accounts, with:
|
|
||||
// - 3 TokenIDs
|
|
||||
// - 29+5+10 L1 txs (deposits & exits)
|
|
||||
// - 21+53+7 L2 transactions
|
|
||||
var SetTest0 = ` |
|
||||
// deposits TokenID: 1
|
|
||||
A (1): 50 |
|
||||
B (1): 5 |
|
||||
C (1): 20 |
|
||||
D (1): 25 |
|
||||
E (1): 25 |
|
||||
F (1): 25 |
|
||||
G (1): 25 |
|
||||
H (1): 25 |
|
||||
I (1): 25 |
|
||||
J (1): 25 |
|
||||
K (1): 25 |
|
||||
L (1): 25 |
|
||||
M (1): 25 |
|
||||
N (1): 25 |
|
||||
O (1): 25 |
|
||||
P (1): 25 |
|
||||
Q (1): 25 |
|
||||
R (1): 25 |
|
||||
S (1): 25 |
|
||||
T (1): 25 |
|
||||
U (1): 25 |
|
||||
V (1): 25 |
|
||||
W (1): 25 |
|
||||
X (1): 25 |
|
||||
Y (1): 25 |
|
||||
Z (1): 25 |
|
||||
|
|
||||
// deposits TokenID: 2
|
|
||||
B (2): 5 |
|
||||
A (2): 20 |
|
||||
|
|
||||
// deposits TokenID: 3
|
|
||||
B (3): 100 |
|
||||
|
|
||||
// transactions TokenID: 1
|
|
||||
A-B (1): 5 1 |
|
||||
A-L (1): 10 1 |
|
||||
A-M (1): 5 1 |
|
||||
A-N (1): 5 1 |
|
||||
A-O (1): 5 1 |
|
||||
B-C (1): 3 1 |
|
||||
C-A (1): 3 255 |
|
||||
D-A (1): 5 1 |
|
||||
D-Z (1): 5 1 |
|
||||
D-Y (1): 5 1 |
|
||||
D-X (1): 5 1 |
|
||||
E-Z (1): 5 2 |
|
||||
E-Y (1): 5 1 |
|
||||
E-X (1): 5 1 |
|
||||
F-Z (1): 5 1 |
|
||||
G-K (1): 3 1 |
|
||||
G-K (1): 3 1 |
|
||||
G-K (1): 3 1 |
|
||||
H-K (1): 3 2 |
|
||||
H-K (1): 3 1 |
|
||||
H-K (1): 3 1 |
|
||||
|
|
||||
> batch1 |
|
||||
|
|
||||
// A (3) still does not exist, coordinator should create new L1Tx to create the account
|
|
||||
B-A (3): 5 1 |
|
||||
|
|
||||
A-B (2): 5 1 |
|
||||
I-K (1): 3 1 |
|
||||
I-K (1): 3 1 |
|
||||
I-K (1): 3 1 |
|
||||
J-K (1): 3 1 |
|
||||
J-K (1): 3 1 |
|
||||
J-K (1): 3 1 |
|
||||
K-J (1): 3 1 |
|
||||
L-A (1): 5 1 |
|
||||
L-Z (1): 5 1 |
|
||||
L-Y (1): 5 1 |
|
||||
L-X (1): 5 1 |
|
||||
M-A (1): 5 1 |
|
||||
M-Z (1): 5 1 |
|
||||
M-Y (1): 5 1 |
|
||||
N-A (1): 5 1 |
|
||||
N-Z (1): 5 2 |
|
||||
N-Y (1): 5 1 |
|
||||
O-T (1): 3 1 |
|
||||
O-U (1): 3 1 |
|
||||
O-V (1): 3 1 |
|
||||
P-T (1): 3 1 |
|
||||
P-U (1): 3 1 |
|
||||
P-V (1): 3 5 |
|
||||
Q-O (1): 3 1 |
|
||||
Q-P (1): 3 1 |
|
||||
R-O (1): 3 1 |
|
||||
R-P (1): 3 1 |
|
||||
R-Q (1): 3 1 |
|
||||
S-O (1): 3 1 |
|
||||
S-P (1): 3 1 |
|
||||
S-Q (1): 3 1 |
|
||||
T-O (1): 3 1 |
|
||||
T-P (1): 3 1 |
|
||||
T-Q (1): 3 1 |
|
||||
U-Z (1): 5 3 |
|
||||
U-Y (1): 5 1 |
|
||||
U-T (1): 3 1 |
|
||||
V-Z (1): 5 0 |
|
||||
V-Y (1): 6 1 |
|
||||
V-T (1): 3 1 |
|
||||
W-K (1): 3 1 |
|
||||
W-J (1): 3 1 |
|
||||
W-A (1): 5 1 |
|
||||
W-Z (1): 5 1 |
|
||||
X-B (1): 5 1 |
|
||||
X-C (1): 5 50 |
|
||||
X-D (1): 5 1 |
|
||||
X-E (1): 5 1 |
|
||||
Y-B (1): 5 1 |
|
||||
Y-C (1): 5 1 |
|
||||
Y-D (1): 5 1 |
|
||||
Y-E (1): 5 1 |
|
||||
Z-A (1): 5 1 |
|
||||
|
|
||||
// exits
|
|
||||
A (1) E: 5 |
|
||||
K (1) E: 5 |
|
||||
X (1) E: 5 |
|
||||
Y (1) E: 5 |
|
||||
Z (1) E: 5 |
|
||||
|
|
||||
> batch2 |
|
||||
A (1): 50 |
|
||||
B (1): 5 |
|
||||
C (1): 20 |
|
||||
D (1): 25 |
|
||||
E (1): 25 |
|
||||
F (1): 25 |
|
||||
G (1): 25 |
|
||||
H (1): 25 |
|
||||
I (1): 25 |
|
||||
A-B (1): 5 1 |
|
||||
A-L (1): 10 1 |
|
||||
A-M (1): 5 1 |
|
||||
B-N (1): 5 1 |
|
||||
C-O (1): 5 1 |
|
||||
H-O (1): 5 1 |
|
||||
I-H (1): 5 1 |
|
||||
A (1) E: 5 |
|
||||
` |
|
@ -1,14 +0,0 @@ |
|||||
package test |
|
||||
|
|
||||
import ( |
|
||||
"strings" |
|
||||
"testing" |
|
||||
|
|
||||
"github.com/stretchr/testify/assert" |
|
||||
) |
|
||||
|
|
||||
func TestCompileSets(t *testing.T) { |
|
||||
parser := NewParser(strings.NewReader(SetTest0)) |
|
||||
_, err := parser.Parse() |
|
||||
assert.Nil(t, err) |
|
||||
} |
|
@ -0,0 +1,523 @@ |
|||||
|
package transakcio |
||||
|
|
||||
|
import ( |
||||
|
"bufio" |
||||
|
"bytes" |
||||
|
"fmt" |
||||
|
"io" |
||||
|
"log" |
||||
|
"sort" |
||||
|
"strconv" |
||||
|
|
||||
|
"github.com/hermeznetwork/hermez-node/common" |
||||
|
) |
||||
|
|
||||
|
var eof = rune(0) |
||||
|
var errof = fmt.Errorf("eof in parseline") |
||||
|
var commentLine = fmt.Errorf("comment in parseline") //nolint:golint
|
||||
|
var newEventLine = fmt.Errorf("newEventLine") //nolint:golint
|
||||
|
var setTypeLine = fmt.Errorf("setTypeLine") //nolint:golint
|
||||
|
|
||||
|
// setType defines the type of the set
|
||||
|
type setType string |
||||
|
|
||||
|
// setTypeBlockchain defines the type 'Blockchain' of the set
|
||||
|
var setTypeBlockchain = setType("Blockchain") |
||||
|
|
||||
|
// setTypePoolL2 defines the type 'PoolL2' of the set
|
||||
|
var setTypePoolL2 = setType("PoolL2") |
||||
|
|
||||
|
// typeNewBatch is used for testing purposes only, and represents the
|
||||
|
// common.TxType of a new batch
|
||||
|
var typeNewBatch common.TxType = "TxTypeNewBatch" |
||||
|
|
||||
|
// typeNewBlock is used for testing purposes only, and represents the
|
||||
|
// common.TxType of a new ethereum block
|
||||
|
var typeNewBlock common.TxType = "TxTypeNewBlock" |
||||
|
|
||||
|
//nolint
|
||||
|
const ( |
||||
|
ILLEGAL token = iota |
||||
|
WS |
||||
|
EOF |
||||
|
|
||||
|
IDENT // val
|
||||
|
) |
||||
|
|
||||
|
// instruction is the data structure that represents one line of code
|
||||
|
type instruction struct { |
||||
|
literal string |
||||
|
from string |
||||
|
to string |
||||
|
amount uint64 |
||||
|
loadAmount uint64 |
||||
|
fee uint8 |
||||
|
tokenID common.TokenID |
||||
|
typ common.TxType // D: Deposit, T: Transfer, E: ForceExit
|
||||
|
} |
||||
|
|
||||
|
// parsedSet contains the full Set of Instructions representing a full code
|
||||
|
type parsedSet struct { |
||||
|
// type string
|
||||
|
instructions []instruction |
||||
|
accounts []string |
||||
|
tokenIDs []common.TokenID |
||||
|
} |
||||
|
|
||||
|
func (i instruction) String() string { |
||||
|
buf := bytes.NewBufferString("") |
||||
|
fmt.Fprintf(buf, "Type: %s, ", i.typ) |
||||
|
fmt.Fprintf(buf, "From: %s, ", i.from) |
||||
|
if i.typ == common.TxTypeTransfer || |
||||
|
i.typ == common.TxTypeDepositTransfer || |
||||
|
i.typ == common.TxTypeCreateAccountDepositTransfer { |
||||
|
fmt.Fprintf(buf, "To: %s, ", i.to) |
||||
|
} |
||||
|
|
||||
|
if i.typ == common.TxTypeDeposit || |
||||
|
i.typ == common.TxTypeDepositTransfer || |
||||
|
i.typ == common.TxTypeCreateAccountDepositTransfer { |
||||
|
fmt.Fprintf(buf, "LoadAmount: %d, ", i.loadAmount) |
||||
|
} |
||||
|
if i.typ != common.TxTypeDeposit { |
||||
|
fmt.Fprintf(buf, "Amount: %d, ", i.amount) |
||||
|
} |
||||
|
if i.typ == common.TxTypeTransfer || |
||||
|
i.typ == common.TxTypeDepositTransfer || |
||||
|
i.typ == common.TxTypeCreateAccountDepositTransfer { |
||||
|
fmt.Fprintf(buf, "Fee: %d, ", i.fee) |
||||
|
} |
||||
|
fmt.Fprintf(buf, "TokenID: %d\n", i.tokenID) |
||||
|
return buf.String() |
||||
|
} |
||||
|
|
||||
|
// Raw returns a string with the raw representation of the Instruction
|
||||
|
func (i instruction) raw() string { |
||||
|
buf := bytes.NewBufferString("") |
||||
|
fmt.Fprintf(buf, "%s", i.typ) |
||||
|
fmt.Fprintf(buf, "(%d)", i.tokenID) |
||||
|
fmt.Fprintf(buf, "%s", i.from) |
||||
|
if i.typ == common.TxTypeTransfer || |
||||
|
i.typ == common.TxTypeDepositTransfer || |
||||
|
i.typ == common.TxTypeCreateAccountDepositTransfer { |
||||
|
fmt.Fprintf(buf, "-%s", i.to) |
||||
|
} |
||||
|
fmt.Fprintf(buf, ":") |
||||
|
if i.typ == common.TxTypeDeposit || |
||||
|
i.typ == common.TxTypeDepositTransfer || |
||||
|
i.typ == common.TxTypeCreateAccountDepositTransfer { |
||||
|
fmt.Fprintf(buf, "%d", i.loadAmount) |
||||
|
} |
||||
|
if i.typ != common.TxTypeDeposit { |
||||
|
fmt.Fprintf(buf, "%d", i.amount) |
||||
|
} |
||||
|
if i.typ == common.TxTypeTransfer { |
||||
|
fmt.Fprintf(buf, "(%d)", i.fee) |
||||
|
} |
||||
|
return buf.String() |
||||
|
} |
||||
|
|
||||
|
type token int |
||||
|
|
||||
|
type scanner struct { |
||||
|
r *bufio.Reader |
||||
|
} |
||||
|
|
||||
|
func isWhitespace(ch rune) bool { |
||||
|
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\v' || ch == '\f' |
||||
|
} |
||||
|
|
||||
|
func isLetter(ch rune) bool { |
||||
|
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') |
||||
|
} |
||||
|
|
||||
|
func isComment(ch rune) bool { |
||||
|
return ch == '/' |
||||
|
} |
||||
|
|
||||
|
func isDigit(ch rune) bool { |
||||
|
return (ch >= '0' && ch <= '9') |
||||
|
} |
||||
|
|
||||
|
// newScanner creates a new scanner with the given io.Reader
|
||||
|
func newScanner(r io.Reader) *scanner { |
||||
|
return &scanner{r: bufio.NewReader(r)} |
||||
|
} |
||||
|
|
||||
|
func (s *scanner) read() rune { |
||||
|
ch, _, err := s.r.ReadRune() |
||||
|
if err != nil { |
||||
|
return eof |
||||
|
} |
||||
|
return ch |
||||
|
} |
||||
|
|
||||
|
func (s *scanner) unread() { |
||||
|
_ = s.r.UnreadRune() |
||||
|
} |
||||
|
|
||||
|
// scan returns the token and literal string of the current value
|
||||
|
func (s *scanner) scan() (tok token, lit string) { |
||||
|
ch := s.read() |
||||
|
|
||||
|
if isWhitespace(ch) { |
||||
|
// space
|
||||
|
s.unread() |
||||
|
return s.scanWhitespace() |
||||
|
} else if isLetter(ch) || isDigit(ch) { |
||||
|
// letter/digit
|
||||
|
s.unread() |
||||
|
return s.scanIndent() |
||||
|
} else if isComment(ch) { |
||||
|
// comment
|
||||
|
s.unread() |
||||
|
return s.scanIndent() |
||||
|
} |
||||
|
|
||||
|
if ch == eof { |
||||
|
return EOF, "" |
||||
|
} |
||||
|
|
||||
|
return ILLEGAL, string(ch) |
||||
|
} |
||||
|
|
||||
|
func (s *scanner) scanWhitespace() (token token, lit string) { |
||||
|
var buf bytes.Buffer |
||||
|
buf.WriteRune(s.read()) |
||||
|
|
||||
|
for { |
||||
|
if ch := s.read(); ch == eof { |
||||
|
break |
||||
|
} else if !isWhitespace(ch) { |
||||
|
s.unread() |
||||
|
break |
||||
|
} else { |
||||
|
_, _ = buf.WriteRune(ch) |
||||
|
} |
||||
|
} |
||||
|
return WS, buf.String() |
||||
|
} |
||||
|
|
||||
|
func (s *scanner) scanIndent() (tok token, lit string) { |
||||
|
var buf bytes.Buffer |
||||
|
buf.WriteRune(s.read()) |
||||
|
|
||||
|
for { |
||||
|
if ch := s.read(); ch == eof { |
||||
|
break |
||||
|
} else if !isLetter(ch) && !isDigit(ch) { |
||||
|
s.unread() |
||||
|
break |
||||
|
} else { |
||||
|
_, _ = buf.WriteRune(ch) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if len(buf.String()) == 1 { |
||||
|
return token(rune(buf.String()[0])), buf.String() |
||||
|
} |
||||
|
return IDENT, buf.String() |
||||
|
} |
||||
|
|
||||
|
// parser defines the parser
|
||||
|
type parser struct { |
||||
|
s *scanner |
||||
|
buf struct { |
||||
|
tok token |
||||
|
lit string |
||||
|
n int |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// newParser creates a new parser from a io.Reader
|
||||
|
func newParser(r io.Reader) *parser { |
||||
|
return &parser{s: newScanner(r)} |
||||
|
} |
||||
|
|
||||
|
func (p *parser) scan() (tok token, lit string) { |
||||
|
// if there is a token in the buffer return it
|
||||
|
if p.buf.n != 0 { |
||||
|
p.buf.n = 0 |
||||
|
return p.buf.tok, p.buf.lit |
||||
|
} |
||||
|
tok, lit = p.s.scan() |
||||
|
|
||||
|
p.buf.tok, p.buf.lit = tok, lit |
||||
|
|
||||
|
return |
||||
|
} |
||||
|
|
||||
|
func (p *parser) scanIgnoreWhitespace() (tok token, lit string) { |
||||
|
tok, lit = p.scan() |
||||
|
if tok == WS { |
||||
|
tok, lit = p.scan() |
||||
|
} |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
// parseLine parses the current line
|
||||
|
func (p *parser) parseLine(setType setType) (*instruction, error) { |
||||
|
c := &instruction{} |
||||
|
tok, lit := p.scanIgnoreWhitespace() |
||||
|
if tok == EOF { |
||||
|
return nil, errof |
||||
|
} |
||||
|
c.literal += lit |
||||
|
if lit == "/" { |
||||
|
_, _ = p.s.r.ReadString('\n') |
||||
|
return nil, commentLine |
||||
|
} else if lit == ">" { |
||||
|
if setType == setTypePoolL2 { |
||||
|
return c, fmt.Errorf("Unexpected '>' at PoolL2Txs set") |
||||
|
} |
||||
|
_, lit = p.scanIgnoreWhitespace() |
||||
|
if lit == "batch" { |
||||
|
_, _ = p.s.r.ReadString('\n') |
||||
|
return &instruction{typ: typeNewBatch}, newEventLine |
||||
|
} else if lit == "block" { |
||||
|
_, _ = p.s.r.ReadString('\n') |
||||
|
return &instruction{typ: typeNewBlock}, newEventLine |
||||
|
} else { |
||||
|
return c, fmt.Errorf("Unexpected '> %s', expected '> batch' or '> block'", lit) |
||||
|
} |
||||
|
} else if lit == "Type" { |
||||
|
if err := p.expectChar(c, ":"); err != nil { |
||||
|
return c, err |
||||
|
} |
||||
|
_, lit = p.scanIgnoreWhitespace() |
||||
|
if lit == "Blockchain" { |
||||
|
return &instruction{typ: "Blockchain"}, setTypeLine |
||||
|
} else if lit == "PoolL2" { |
||||
|
return &instruction{typ: "PoolL2"}, setTypeLine |
||||
|
} else { |
||||
|
return c, fmt.Errorf("Invalid set type: '%s'. Valid set types: 'Blockchain', 'PoolL2'", lit) |
||||
|
} |
||||
|
} |
||||
|
if setType == "" { |
||||
|
return c, fmt.Errorf("Set type not defined") |
||||
|
} |
||||
|
transferring := false |
||||
|
switch lit { |
||||
|
case "Deposit": |
||||
|
if setType != setTypeBlockchain { |
||||
|
return c, fmt.Errorf("Unexpected '%s' in a non %s set", lit, setTypeBlockchain) |
||||
|
} |
||||
|
c.typ = common.TxTypeDeposit |
||||
|
case "Exit": |
||||
|
if setType != setTypeBlockchain { |
||||
|
return c, fmt.Errorf("Unexpected '%s' in a non %s set", lit, setTypeBlockchain) |
||||
|
} |
||||
|
c.typ = common.TxTypeExit |
||||
|
case "PoolExit": |
||||
|
if setType != setTypePoolL2 { |
||||
|
return c, fmt.Errorf("Unexpected '%s' in a non %s set", lit, setTypePoolL2) |
||||
|
} |
||||
|
c.typ = common.TxTypeExit |
||||
|
case "Transfer": |
||||
|
if setType != setTypeBlockchain { |
||||
|
return c, fmt.Errorf("Unexpected '%s' in a non %s set", lit, setTypeBlockchain) |
||||
|
} |
||||
|
c.typ = common.TxTypeTransfer |
||||
|
transferring = true |
||||
|
case "PoolTransfer": |
||||
|
if setType != setTypePoolL2 { |
||||
|
return c, fmt.Errorf("Unexpected '%s' in a non %s set", lit, setTypePoolL2) |
||||
|
} |
||||
|
c.typ = common.TxTypeTransfer |
||||
|
transferring = true |
||||
|
case "CreateAccountDeposit": |
||||
|
if setType != setTypeBlockchain { |
||||
|
return c, fmt.Errorf("Unexpected '%s' in a non %s set", lit, setTypeBlockchain) |
||||
|
} |
||||
|
c.typ = common.TxTypeCreateAccountDeposit |
||||
|
case "CreateAccountDepositTransfer": |
||||
|
if setType != setTypeBlockchain { |
||||
|
return c, fmt.Errorf("Unexpected '%s' in a non %s set", lit, setTypeBlockchain) |
||||
|
} |
||||
|
c.typ = common.TxTypeCreateAccountDepositTransfer |
||||
|
transferring = true |
||||
|
case "DepositTransfer": |
||||
|
if setType != setTypeBlockchain { |
||||
|
return c, fmt.Errorf("Unexpected '%s' in a non %s set", lit, setTypeBlockchain) |
||||
|
} |
||||
|
c.typ = common.TxTypeDepositTransfer |
||||
|
transferring = true |
||||
|
case "ForceTransfer": |
||||
|
if setType != setTypeBlockchain { |
||||
|
return c, fmt.Errorf("Unexpected '%s' in a non %s set", lit, setTypeBlockchain) |
||||
|
} |
||||
|
c.typ = common.TxTypeForceTransfer |
||||
|
case "ForceExit": |
||||
|
if setType != setTypeBlockchain { |
||||
|
return c, fmt.Errorf("Unexpected '%s' in a non %s set", lit, setTypeBlockchain) |
||||
|
} |
||||
|
c.typ = common.TxTypeForceExit |
||||
|
default: |
||||
|
return c, fmt.Errorf("Unexpected tx type: %s", lit) |
||||
|
} |
||||
|
|
||||
|
if err := p.expectChar(c, "("); err != nil { |
||||
|
return c, err |
||||
|
} |
||||
|
_, lit = p.scanIgnoreWhitespace() |
||||
|
c.literal += lit |
||||
|
tidI, err := strconv.Atoi(lit) |
||||
|
if err != nil { |
||||
|
line, _ := p.s.r.ReadString('\n') |
||||
|
c.literal += line |
||||
|
return c, err |
||||
|
} |
||||
|
c.tokenID = common.TokenID(tidI) |
||||
|
if err := p.expectChar(c, ")"); err != nil { |
||||
|
return c, err |
||||
|
} |
||||
|
_, lit = p.scanIgnoreWhitespace() |
||||
|
c.literal += lit |
||||
|
c.from = lit |
||||
|
_, lit = p.scanIgnoreWhitespace() |
||||
|
c.literal += lit |
||||
|
if transferring { |
||||
|
if lit != "-" { |
||||
|
return c, fmt.Errorf("Expected '-', found '%s'", lit) |
||||
|
} |
||||
|
_, lit = p.scanIgnoreWhitespace() |
||||
|
c.literal += lit |
||||
|
c.to = lit |
||||
|
_, lit = p.scanIgnoreWhitespace() |
||||
|
c.literal += lit |
||||
|
} |
||||
|
if lit != ":" { |
||||
|
line, _ := p.s.r.ReadString('\n') |
||||
|
c.literal += line |
||||
|
return c, fmt.Errorf("Expected ':', found '%s'", lit) |
||||
|
} |
||||
|
if c.typ == common.TxTypeDepositTransfer || |
||||
|
c.typ == common.TxTypeCreateAccountDepositTransfer { |
||||
|
// deposit case
|
||||
|
_, lit = p.scanIgnoreWhitespace() |
||||
|
c.literal += lit |
||||
|
loadAmount, err := strconv.Atoi(lit) |
||||
|
if err != nil { |
||||
|
line, _ := p.s.r.ReadString('\n') |
||||
|
c.literal += line |
||||
|
return c, err |
||||
|
} |
||||
|
c.loadAmount = uint64(loadAmount) |
||||
|
if err := p.expectChar(c, ","); err != nil { |
||||
|
return c, err |
||||
|
} |
||||
|
} |
||||
|
_, lit = p.scanIgnoreWhitespace() |
||||
|
c.literal += lit |
||||
|
amount, err := strconv.Atoi(lit) |
||||
|
if err != nil { |
||||
|
line, _ := p.s.r.ReadString('\n') |
||||
|
c.literal += line |
||||
|
return c, err |
||||
|
} |
||||
|
if c.typ == common.TxTypeDeposit || |
||||
|
c.typ == common.TxTypeCreateAccountDeposit { |
||||
|
c.loadAmount = uint64(amount) |
||||
|
} else { |
||||
|
c.amount = uint64(amount) |
||||
|
} |
||||
|
if transferring { |
||||
|
if err := p.expectChar(c, "("); err != nil { |
||||
|
return c, err |
||||
|
} |
||||
|
_, lit = p.scanIgnoreWhitespace() |
||||
|
c.literal += lit |
||||
|
fee, err := strconv.Atoi(lit) |
||||
|
if err != nil { |
||||
|
line, _ := p.s.r.ReadString('\n') |
||||
|
c.literal += line |
||||
|
return c, err |
||||
|
} |
||||
|
if fee > common.MaxFeePlan-1 { |
||||
|
line, _ := p.s.r.ReadString('\n') |
||||
|
c.literal += line |
||||
|
return c, fmt.Errorf("Fee %d can not be bigger than 255", fee) |
||||
|
} |
||||
|
c.fee = uint8(fee) |
||||
|
|
||||
|
if err := p.expectChar(c, ")"); err != nil { |
||||
|
return c, err |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if tok == EOF { |
||||
|
return nil, errof |
||||
|
} |
||||
|
return c, nil |
||||
|
} |
||||
|
|
||||
|
func (p *parser) expectChar(c *instruction, ch string) error { |
||||
|
_, lit := p.scanIgnoreWhitespace() |
||||
|
c.literal += lit |
||||
|
if lit != ch { |
||||
|
line, _ := p.s.r.ReadString('\n') |
||||
|
c.literal += line |
||||
|
return fmt.Errorf("Expected '%s', found '%s'", ch, lit) |
||||
|
} |
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
func idxTokenIDToString(idx string, tid common.TokenID) string { |
||||
|
return idx + strconv.Itoa(int(tid)) |
||||
|
} |
||||
|
|
||||
|
// parse parses through reader
|
||||
|
func (p *parser) parse() (*parsedSet, error) { |
||||
|
instructions := &parsedSet{} |
||||
|
i := 0 |
||||
|
accounts := make(map[string]bool) |
||||
|
tokenids := make(map[common.TokenID]bool) |
||||
|
var setTypeOfSet setType |
||||
|
for { |
||||
|
instruction, err := p.parseLine(setTypeOfSet) |
||||
|
if err == errof { |
||||
|
break |
||||
|
} |
||||
|
if err == setTypeLine { |
||||
|
if instruction.typ == "PoolL2" { |
||||
|
setTypeOfSet = setTypePoolL2 |
||||
|
} else if instruction.typ == "Blockchain" { |
||||
|
setTypeOfSet = setTypeBlockchain |
||||
|
} else { |
||||
|
log.Fatalf("Invalid set type: '%s'. Valid set types: 'Blockchain', 'PoolL2'", instruction.typ) |
||||
|
} |
||||
|
i++ |
||||
|
continue |
||||
|
} |
||||
|
if err == commentLine { |
||||
|
i++ |
||||
|
continue |
||||
|
} |
||||
|
if err == newEventLine { |
||||
|
i++ |
||||
|
instructions.instructions = append(instructions.instructions, *instruction) |
||||
|
continue |
||||
|
} |
||||
|
if err != nil { |
||||
|
return instructions, fmt.Errorf("error parsing line %d: %s, err: %s", i, instruction.literal, err.Error()) |
||||
|
} |
||||
|
if setTypeOfSet == "" { |
||||
|
return instructions, fmt.Errorf("Set type not defined") |
||||
|
} |
||||
|
instructions.instructions = append(instructions.instructions, *instruction) |
||||
|
accounts[instruction.from] = true |
||||
|
if instruction.typ == common.TxTypeTransfer { // type: Transfer
|
||||
|
accounts[instruction.to] = true |
||||
|
} |
||||
|
tokenids[instruction.tokenID] = true |
||||
|
i++ |
||||
|
} |
||||
|
for a := range accounts { |
||||
|
instructions.accounts = append(instructions.accounts, a) |
||||
|
} |
||||
|
sort.Strings(instructions.accounts) |
||||
|
for tid := range tokenids { |
||||
|
instructions.tokenIDs = append(instructions.tokenIDs, tid) |
||||
|
} |
||||
|
return instructions, nil |
||||
|
} |
@ -0,0 +1,193 @@ |
|||||
|
package transakcio |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"strings" |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/stretchr/testify/assert" |
||||
|
"github.com/stretchr/testify/require" |
||||
|
) |
||||
|
|
||||
|
var debug = false |
||||
|
|
||||
|
func TestParseBlockchainTxs(t *testing.T) { |
||||
|
s := ` |
||||
|
Type: Blockchain |
||||
|
// deposits
|
||||
|
Deposit(1) A: 10 |
||||
|
Deposit(2) A: 20 |
||||
|
Deposit(1) B: 5 |
||||
|
CreateAccountDeposit(1) C: 5 |
||||
|
CreateAccountDepositTransfer(1) D-A: 15, 10 (3) |
||||
|
|
||||
|
// L2 transactions
|
||||
|
Transfer(1) A-B: 6 (1) |
||||
|
Transfer(1) B-D: 3 (1) |
||||
|
|
||||
|
// set new batch
|
||||
|
> batch |
||||
|
|
||||
|
DepositTransfer(1) A-B: 15, 10 (1) |
||||
|
Transfer(1) C-A : 3 (1) |
||||
|
Transfer(2) A-B: 15 (1) |
||||
|
|
||||
|
Deposit(1) User0: 20 |
||||
|
Deposit(3) User1: 20 |
||||
|
Transfer(1) User0-User1: 15 (1) |
||||
|
Transfer(3) User1-User0: 15 (1) |
||||
|
|
||||
|
> batch |
||||
|
|
||||
|
Transfer(1) User1-User0: 1 (1) |
||||
|
|
||||
|
> batch |
||||
|
> block |
||||
|
|
||||
|
// Exits
|
||||
|
Exit(1) A: 5 |
||||
|
` |
||||
|
|
||||
|
parser := newParser(strings.NewReader(s)) |
||||
|
instructions, err := parser.parse() |
||||
|
require.Nil(t, err) |
||||
|
assert.Equal(t, 20, len(instructions.instructions)) |
||||
|
assert.Equal(t, 6, len(instructions.accounts)) |
||||
|
assert.Equal(t, 3, len(instructions.tokenIDs)) |
||||
|
|
||||
|
if debug { |
||||
|
fmt.Println(instructions) |
||||
|
for _, instruction := range instructions.instructions { |
||||
|
fmt.Println(instruction.raw()) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
assert.Equal(t, typeNewBatch, instructions.instructions[7].typ) |
||||
|
assert.Equal(t, "Deposit(1)User0:20", instructions.instructions[11].raw()) |
||||
|
assert.Equal(t, "Type: DepositTransfer, From: A, To: B, LoadAmount: 15, Amount: 10, Fee: 1, TokenID: 1\n", instructions.instructions[8].String()) |
||||
|
assert.Equal(t, "Type: Transfer, From: User1, To: User0, Amount: 15, Fee: 1, TokenID: 3\n", instructions.instructions[14].String()) |
||||
|
assert.Equal(t, "Transfer(2)A-B:15(1)", instructions.instructions[10].raw()) |
||||
|
assert.Equal(t, "Type: Transfer, From: A, To: B, Amount: 15, Fee: 1, TokenID: 2\n", instructions.instructions[10].String()) |
||||
|
assert.Equal(t, "Exit(1)A:5", instructions.instructions[19].raw()) |
||||
|
assert.Equal(t, "Type: Exit, From: A, Amount: 5, TokenID: 1\n", instructions.instructions[19].String()) |
||||
|
} |
||||
|
|
||||
|
func TestParsePoolTxs(t *testing.T) { |
||||
|
s := ` |
||||
|
Type: PoolL2 |
||||
|
PoolTransfer(1) A-B: 6 (1) |
||||
|
PoolTransfer(2) A-B: 3 (3) |
||||
|
PoolTransfer(1) B-D: 3 (1) |
||||
|
PoolTransfer(1) C-D: 3 (1) |
||||
|
PoolExit(1) A: 5 |
||||
|
` |
||||
|
|
||||
|
parser := newParser(strings.NewReader(s)) |
||||
|
instructions, err := parser.parse() |
||||
|
require.Nil(t, err) |
||||
|
assert.Equal(t, 5, len(instructions.instructions)) |
||||
|
assert.Equal(t, 4, len(instructions.accounts)) |
||||
|
assert.Equal(t, 2, len(instructions.tokenIDs)) |
||||
|
|
||||
|
if debug { |
||||
|
fmt.Println(instructions) |
||||
|
for _, instruction := range instructions.instructions { |
||||
|
fmt.Println(instruction.raw()) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
assert.Equal(t, "Transfer(1)A-B:6(1)", instructions.instructions[0].raw()) |
||||
|
assert.Equal(t, "Transfer(2)A-B:3(3)", instructions.instructions[1].raw()) |
||||
|
assert.Equal(t, "Transfer(1)B-D:3(1)", instructions.instructions[2].raw()) |
||||
|
assert.Equal(t, "Transfer(1)C-D:3(1)", instructions.instructions[3].raw()) |
||||
|
assert.Equal(t, "Exit(1)A:5", instructions.instructions[4].raw()) |
||||
|
} |
||||
|
|
||||
|
func TestParseErrors(t *testing.T) { |
||||
|
s := ` |
||||
|
Type: Blockchain |
||||
|
Deposit(1) A:: 10 |
||||
|
` |
||||
|
parser := newParser(strings.NewReader(s)) |
||||
|
_, err := parser.parse() |
||||
|
assert.Equal(t, "error parsing line 1: Deposit(1)A:: 10\n, err: strconv.Atoi: parsing \":\": invalid syntax", err.Error()) |
||||
|
|
||||
|
s = ` |
||||
|
Type: Blockchain |
||||
|
Deposit(1) A: 10 20 |
||||
|
` |
||||
|
parser = newParser(strings.NewReader(s)) |
||||
|
_, err = parser.parse() |
||||
|
assert.Equal(t, "error parsing line 2: 20, err: Unexpected tx type: 20", err.Error()) |
||||
|
|
||||
|
s = ` |
||||
|
Type: Blockchain |
||||
|
Transfer(1) A: 10 |
||||
|
` |
||||
|
parser = newParser(strings.NewReader(s)) |
||||
|
_, err = parser.parse() |
||||
|
assert.Equal(t, "error parsing line 1: Transfer(1)A:, err: Expected '-', found ':'", err.Error()) |
||||
|
|
||||
|
s = ` |
||||
|
Type: Blockchain |
||||
|
Transfer(1) A B: 10 |
||||
|
` |
||||
|
parser = newParser(strings.NewReader(s)) |
||||
|
_, err = parser.parse() |
||||
|
assert.Equal(t, "error parsing line 1: Transfer(1)AB, err: Expected '-', found 'B'", err.Error()) |
||||
|
|
||||
|
s = ` |
||||
|
Type: Blockchain |
||||
|
Transfer(1) A-B: 10 (255) |
||||
|
` |
||||
|
parser = newParser(strings.NewReader(s)) |
||||
|
_, err = parser.parse() |
||||
|
assert.Nil(t, err) |
||||
|
s = ` |
||||
|
Type: Blockchain |
||||
|
Transfer(1) A-B: 10 (256) |
||||
|
` |
||||
|
parser = newParser(strings.NewReader(s)) |
||||
|
_, err = parser.parse() |
||||
|
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()) |
||||
|
|
||||
|
// check that the PoolTransfer & Transfer are only accepted in the
|
||||
|
// correct case case (PoolTxs/BlockchainTxs)
|
||||
|
s = ` |
||||
|
Type: PoolL2 |
||||
|
Transfer(1) A-B: 10 (1) |
||||
|
` |
||||
|
parser = newParser(strings.NewReader(s)) |
||||
|
_, err = parser.parse() |
||||
|
assert.Equal(t, "error parsing line 1: Transfer, err: Unexpected 'Transfer' in a non Blockchain set", err.Error()) |
||||
|
s = ` |
||||
|
Type: Blockchain |
||||
|
PoolTransfer(1) A-B: 10 (1) |
||||
|
` |
||||
|
parser = newParser(strings.NewReader(s)) |
||||
|
_, err = parser.parse() |
||||
|
assert.Equal(t, "error parsing line 1: PoolTransfer, err: Unexpected 'PoolTransfer' in a non PoolL2 set", err.Error()) |
||||
|
|
||||
|
s = ` |
||||
|
Type: Blockchain |
||||
|
> btch |
||||
|
` |
||||
|
parser = newParser(strings.NewReader(s)) |
||||
|
_, err = parser.parse() |
||||
|
assert.Equal(t, "error parsing line 1: >, err: Unexpected '> btch', expected '> batch' or '> block'", err.Error()) |
||||
|
|
||||
|
// check definition of set Type
|
||||
|
s = `PoolTransfer(1) A-B: 10 (1)` |
||||
|
parser = newParser(strings.NewReader(s)) |
||||
|
_, err = parser.parse() |
||||
|
assert.Equal(t, "error parsing line 0: PoolTransfer, err: Set type not defined", err.Error()) |
||||
|
s = `Type: PoolL1` |
||||
|
parser = newParser(strings.NewReader(s)) |
||||
|
_, err = parser.parse() |
||||
|
assert.Equal(t, "error parsing line 0: Type:, err: Invalid set type: 'PoolL1'. Valid set types: 'Blockchain', 'PoolL2'", err.Error()) |
||||
|
s = `Type: PoolL1 |
||||
|
Type: Blockchain` |
||||
|
parser = newParser(strings.NewReader(s)) |
||||
|
_, err = parser.parse() |
||||
|
assert.Equal(t, "error parsing line 0: Type:, err: Invalid set type: 'PoolL1'. Valid set types: 'Blockchain', 'PoolL2'", err.Error()) |
||||
|
} |
@ -0,0 +1,162 @@ |
|||||
|
package transakcio |
||||
|
|
||||
|
// sets of instructions to be used in tests of other packages
|
||||
|
|
||||
|
// SetBlockchain0 contains a set of transactions simulated to be from the smart contract
|
||||
|
var SetBlockchain0 = ` |
||||
|
// Set containing Blockchain transactions
|
||||
|
Type: Blockchain |
||||
|
|
||||
|
// deposits TokenID: 1
|
||||
|
CreateAccountDeposit(1) A: 50 |
||||
|
CreateAccountDeposit(1) B: 5 |
||||
|
CreateAccountDeposit(1) C: 20 |
||||
|
CreateAccountDeposit(1) D: 25 |
||||
|
CreateAccountDeposit(1) E: 25 |
||||
|
CreateAccountDeposit(1) F: 25 |
||||
|
CreateAccountDeposit(1) G: 25 |
||||
|
CreateAccountDeposit(1) H: 25 |
||||
|
CreateAccountDeposit(1) I: 25 |
||||
|
CreateAccountDeposit(1) J: 25 |
||||
|
CreateAccountDeposit(1) K: 25 |
||||
|
CreateAccountDeposit(1) L: 25 |
||||
|
CreateAccountDeposit(1) M: 25 |
||||
|
CreateAccountDeposit(1) N: 25 |
||||
|
CreateAccountDeposit(1) O: 25 |
||||
|
CreateAccountDeposit(1) P: 25 |
||||
|
CreateAccountDeposit(1) Q: 25 |
||||
|
CreateAccountDeposit(1) R: 25 |
||||
|
CreateAccountDeposit(1) S: 25 |
||||
|
CreateAccountDeposit(1) T: 25 |
||||
|
CreateAccountDeposit(1) U: 25 |
||||
|
CreateAccountDeposit(1) V: 25 |
||||
|
CreateAccountDeposit(1) W: 25 |
||||
|
CreateAccountDeposit(1) X: 25 |
||||
|
CreateAccountDeposit(1) Y: 25 |
||||
|
CreateAccountDeposit(1) Z: 25 |
||||
|
// deposits TokenID: 2
|
||||
|
CreateAccountDeposit(2) B: 5 |
||||
|
CreateAccountDeposit(2) A: 20 |
||||
|
// deposits TokenID: 3
|
||||
|
CreateAccountDeposit(3) B: 100 |
||||
|
// transactions TokenID: 1
|
||||
|
Transfer(1) A-B: 5 (1) |
||||
|
Transfer(1) A-L: 10 (1) |
||||
|
Transfer(1) A-M: 5 (1) |
||||
|
Transfer(1) A-N: 5 (1) |
||||
|
Transfer(1) A-O: 5 (1) |
||||
|
Transfer(1) B-C: 3 (1) |
||||
|
Transfer(1) C-A: 3 (255) |
||||
|
Transfer(1) D-A: 5 (1) |
||||
|
Transfer(1) D-Z: 5 (1) |
||||
|
Transfer(1) D-Y: 5 (1) |
||||
|
Transfer(1) D-X: 5 (1) |
||||
|
Transfer(1) E-Z: 5 (2) |
||||
|
Transfer(1) E-Y: 5 (1) |
||||
|
Transfer(1) E-X: 5 (1) |
||||
|
Transfer(1) F-Z: 5 (1) |
||||
|
Transfer(1) G-K: 3 (1) |
||||
|
Transfer(1) G-K: 3 (1) |
||||
|
Transfer(1) G-K: 3 (1) |
||||
|
Transfer(1) H-K: 3 (2) |
||||
|
Transfer(1) H-K: 3 (1) |
||||
|
Transfer(1) H-K: 3 (1) |
||||
|
|
||||
|
> batch |
||||
|
// A (3) still does not exist, coordinator should create new L1Tx to create the account
|
||||
|
Transfer(3) B-A: 5 (1) |
||||
|
Transfer(2) A-B: 5 (1) |
||||
|
Transfer(1) I-K: 3 (1) |
||||
|
Transfer(1) I-K: 3 (1) |
||||
|
Transfer(1) I-K: 3 (1) |
||||
|
Transfer(1) J-K: 3 (1) |
||||
|
Transfer(1) J-K: 3 (1) |
||||
|
Transfer(1) J-K: 3 (1) |
||||
|
Transfer(1) K-J: 3 (1) |
||||
|
Transfer(1) L-A: 5 (1) |
||||
|
Transfer(1) L-Z: 5 (1) |
||||
|
Transfer(1) L-Y: 5 (1) |
||||
|
Transfer(1) L-X: 5 (1) |
||||
|
Transfer(1) M-A: 5 (1) |
||||
|
Transfer(1) M-Z: 5 (1) |
||||
|
Transfer(1) M-Y: 5 (1) |
||||
|
Transfer(1) N-A: 5 (1) |
||||
|
Transfer(1) N-Z: 5 (2) |
||||
|
Transfer(1) N-Y: 5 (1) |
||||
|
Transfer(1) O-T: 3 (1) |
||||
|
Transfer(1) O-U: 3 (1) |
||||
|
Transfer(1) O-V: 3 (1) |
||||
|
Transfer(1) P-T: 3 (1) |
||||
|
Transfer(1) P-U: 3 (1) |
||||
|
Transfer(1) P-V: 3 (5) |
||||
|
Transfer(1) Q-O: 3 (1) |
||||
|
Transfer(1) Q-P: 3 (1) |
||||
|
Transfer(1) R-O: 3 (1) |
||||
|
Transfer(1) R-P: 3 (1) |
||||
|
Transfer(1) R-Q: 3 (1) |
||||
|
Transfer(1) S-O: 3 (1) |
||||
|
Transfer(1) S-P: 3 (1) |
||||
|
Transfer(1) S-Q: 3 (1) |
||||
|
Transfer(1) T-O: 3 (1) |
||||
|
Transfer(1) T-P: 3 (1) |
||||
|
Transfer(1) T-Q: 3 (1) |
||||
|
Transfer(1) U-Z: 5 (3) |
||||
|
Transfer(1) U-Y: 5 (1) |
||||
|
Transfer(1) U-T: 3 (1) |
||||
|
Transfer(1) V-Z: 5 (0) |
||||
|
Transfer(1) V-Y: 6 (1) |
||||
|
Transfer(1) V-T: 3 (1) |
||||
|
Transfer(1) W-K: 3 (1) |
||||
|
Transfer(1) W-J: 3 (1) |
||||
|
Transfer(1) W-A: 5 (1) |
||||
|
Transfer(1) W-Z: 5 (1) |
||||
|
Transfer(1) X-B: 5 (1) |
||||
|
Transfer(1) X-C: 5 (50) |
||||
|
Transfer(1) X-D: 5 (1) |
||||
|
Transfer(1) X-E: 5 (1) |
||||
|
Transfer(1) Y-B: 5 (1) |
||||
|
Transfer(1) Y-C: 5 (1) |
||||
|
Transfer(1) Y-D: 5 (1) |
||||
|
Transfer(1) Y-E: 5 (1) |
||||
|
Transfer(1) Z-A: 5 (1) |
||||
|
// exits
|
||||
|
ForceExit(1) A: 5 |
||||
|
Exit(1) K: 5 |
||||
|
Exit(1) X: 5 |
||||
|
Exit(1) Y: 5 |
||||
|
Exit(1) Z: 5 |
||||
|
|
||||
|
> batch |
||||
|
Deposit(1) A: 50 |
||||
|
Deposit(1) B: 5 |
||||
|
Deposit(1) C: 20 |
||||
|
Deposit(1) D: 25 |
||||
|
Deposit(1) E: 25 |
||||
|
Deposit(1) F: 25 |
||||
|
Deposit(1) G: 25 |
||||
|
Deposit(1) H: 25 |
||||
|
Deposit(1) I: 25 |
||||
|
Transfer(1) A-B: 5 (1) |
||||
|
Transfer(1) A-L: 10 (1) |
||||
|
Transfer(1) A-M: 5 (1) |
||||
|
Transfer(1) B-N: 5 (1) |
||||
|
Transfer(1) C-O: 5 (1) |
||||
|
Transfer(1) H-O: 5 (1) |
||||
|
Transfer(1) I-H: 5 (1) |
||||
|
Exit(1) A: 5 |
||||
|
` |
||||
|
|
||||
|
// SetPool0 contains a set of transactions from the PoolL2
|
||||
|
var SetPool0 = ` |
||||
|
Type: PoolL2 |
||||
|
PoolTransfer(1) A-B: 6 (1) |
||||
|
PoolTransfer(1) B-C: 3 (1) |
||||
|
PoolTransfer(1) C-A: 3 (1) |
||||
|
PoolTransfer(1) A-B: 1 (1) |
||||
|
PoolTransfer(2) A-B: 15 (1) |
||||
|
PoolTransfer(2) B-D: 3 (1) |
||||
|
PoolExit(1) A: 3 |
||||
|
PoolTransfer(1) A-B: 6 (1) |
||||
|
PoolTransfer(1) B-C: 3 (1) |
||||
|
PoolTransfer(1) A-C: 3 (1) |
||||
|
` |
@ -0,0 +1,21 @@ |
|||||
|
package transakcio |
||||
|
|
||||
|
import ( |
||||
|
"strings" |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/stretchr/testify/assert" |
||||
|
) |
||||
|
|
||||
|
func TestCompileSets(t *testing.T) { |
||||
|
parser := newParser(strings.NewReader(SetBlockchain0)) |
||||
|
_, err := parser.parse() |
||||
|
assert.Nil(t, err) |
||||
|
parser = newParser(strings.NewReader(SetPool0)) |
||||
|
_, err = parser.parse() |
||||
|
assert.Nil(t, err) |
||||
|
|
||||
|
tc := NewTestContext(t) |
||||
|
_ = tc.GenerateBlocks(SetBlockchain0) |
||||
|
_ = tc.GenerateBlocks(SetPool0) |
||||
|
} |
@ -0,0 +1,340 @@ |
|||||
|
package transakcio |
||||
|
|
||||
|
import ( |
||||
|
"crypto/ecdsa" |
||||
|
"math/big" |
||||
|
"strconv" |
||||
|
"strings" |
||||
|
"testing" |
||||
|
"time" |
||||
|
|
||||
|
ethCommon "github.com/ethereum/go-ethereum/common" |
||||
|
ethCrypto "github.com/ethereum/go-ethereum/crypto" |
||||
|
"github.com/hermeznetwork/hermez-node/common" |
||||
|
"github.com/hermeznetwork/hermez-node/log" |
||||
|
"github.com/iden3/go-iden3-crypto/babyjub" |
||||
|
"github.com/stretchr/testify/require" |
||||
|
) |
||||
|
|
||||
|
// TestContext contains the data of the test
|
||||
|
type TestContext struct { |
||||
|
t *testing.T |
||||
|
Instructions []instruction |
||||
|
accountsNames []string |
||||
|
Users map[string]*User |
||||
|
TokenIDs []common.TokenID |
||||
|
l1CreatedAccounts map[string]*Account |
||||
|
} |
||||
|
|
||||
|
// NewTestContext returns a new TestContext
|
||||
|
func NewTestContext(t *testing.T) *TestContext { |
||||
|
return &TestContext{ |
||||
|
t: t, |
||||
|
Users: make(map[string]*User), |
||||
|
l1CreatedAccounts: make(map[string]*Account), |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Account contains the data related to the account for a specific TokenID of a User
|
||||
|
type Account struct { |
||||
|
Idx common.Idx |
||||
|
Nonce common.Nonce |
||||
|
} |
||||
|
|
||||
|
// User contains the data related to a testing user
|
||||
|
type User struct { |
||||
|
BJJ *babyjub.PrivateKey |
||||
|
Addr ethCommon.Address |
||||
|
Accounts map[common.TokenID]*Account |
||||
|
} |
||||
|
|
||||
|
// BlockData contains the information of a Block
|
||||
|
type BlockData struct { |
||||
|
// block *common.Block // ethereum block
|
||||
|
// L1UserTxs that were submitted in the block
|
||||
|
L1UserTxs []common.L1Tx |
||||
|
Batches []BatchData |
||||
|
RegisteredTokens []common.Token |
||||
|
} |
||||
|
|
||||
|
// BatchData contains the information of a Batch
|
||||
|
type BatchData struct { |
||||
|
L1Batch bool // TODO: Remove once Batch.ForgeL1TxsNum is a pointer
|
||||
|
// L1UserTxs that were forged in the batch
|
||||
|
L1UserTxs []common.L1Tx |
||||
|
L1CoordinatorTxs []common.L1Tx |
||||
|
L2Txs []common.L2Tx |
||||
|
CreatedAccounts []common.Account |
||||
|
ExitTree []common.ExitInfo |
||||
|
Batch *common.Batch |
||||
|
} |
||||
|
|
||||
|
// GenerateBlocks returns an array of BlockData for a given set. It uses the
|
||||
|
// accounts (keys & nonces) of the TestContext.
|
||||
|
func (tc *TestContext) GenerateBlocks(set string) []BlockData { |
||||
|
parser := newParser(strings.NewReader(set)) |
||||
|
parsedSet, err := parser.parse() |
||||
|
require.Nil(tc.t, err) |
||||
|
|
||||
|
tc.Instructions = parsedSet.instructions |
||||
|
tc.accountsNames = parsedSet.accounts |
||||
|
tc.TokenIDs = parsedSet.tokenIDs |
||||
|
|
||||
|
tc.generateKeys(tc.accountsNames) |
||||
|
|
||||
|
var blocks []BlockData |
||||
|
currBatchNum := 0 |
||||
|
var currBlock BlockData |
||||
|
var currBatch BatchData |
||||
|
idx := 256 |
||||
|
for _, inst := range parsedSet.instructions { |
||||
|
switch inst.typ { |
||||
|
case common.TxTypeCreateAccountDeposit, common.TxTypeCreateAccountDepositTransfer: |
||||
|
tx := common.L1Tx{ |
||||
|
// TxID
|
||||
|
FromEthAddr: tc.Users[inst.from].Addr, |
||||
|
FromBJJ: tc.Users[inst.from].BJJ.Public(), |
||||
|
TokenID: inst.tokenID, |
||||
|
LoadAmount: big.NewInt(int64(inst.loadAmount)), |
||||
|
Type: inst.typ, |
||||
|
} |
||||
|
if tc.Users[inst.from].Accounts[inst.tokenID] == nil { // if account is not set yet, set it and increment idx
|
||||
|
tc.Users[inst.from].Accounts[inst.tokenID] = &Account{ |
||||
|
Idx: common.Idx(idx), |
||||
|
Nonce: common.Nonce(0), |
||||
|
} |
||||
|
|
||||
|
tc.l1CreatedAccounts[idxTokenIDToString(inst.from, inst.tokenID)] = tc.Users[inst.from].Accounts[inst.tokenID] |
||||
|
idx++ |
||||
|
} |
||||
|
if inst.typ == common.TxTypeCreateAccountDepositTransfer { |
||||
|
tx.Amount = big.NewInt(int64(inst.amount)) |
||||
|
} |
||||
|
currBatch.L1UserTxs = append(currBatch.L1UserTxs, tx) |
||||
|
case common.TxTypeDeposit, common.TxTypeDepositTransfer: |
||||
|
if tc.Users[inst.from].Accounts[inst.tokenID] == nil { |
||||
|
log.Fatalf("Deposit at User %s for TokenID %d while account not created yet", inst.from, inst.tokenID) |
||||
|
} |
||||
|
tx := common.L1Tx{ |
||||
|
// TxID
|
||||
|
FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx, |
||||
|
FromEthAddr: tc.Users[inst.from].Addr, |
||||
|
FromBJJ: tc.Users[inst.from].BJJ.Public(), |
||||
|
TokenID: inst.tokenID, |
||||
|
LoadAmount: big.NewInt(int64(inst.loadAmount)), |
||||
|
Type: inst.typ, |
||||
|
} |
||||
|
if tc.Users[inst.from].Accounts[inst.tokenID].Idx == common.Idx(0) { |
||||
|
// if account.Idx is not set yet, set it and increment idx
|
||||
|
tc.Users[inst.from].Accounts[inst.tokenID].Idx = common.Idx(idx) |
||||
|
|
||||
|
tc.l1CreatedAccounts[idxTokenIDToString(inst.from, inst.tokenID)] = tc.Users[inst.from].Accounts[inst.tokenID] |
||||
|
idx++ |
||||
|
} |
||||
|
if inst.typ == common.TxTypeDepositTransfer { |
||||
|
tx.Amount = big.NewInt(int64(inst.amount)) |
||||
|
// if ToIdx is not set yet, set it and increment idx
|
||||
|
if tc.Users[inst.to].Accounts[inst.tokenID].Idx == common.Idx(0) { |
||||
|
tc.Users[inst.to].Accounts[inst.tokenID].Idx = common.Idx(idx) |
||||
|
|
||||
|
tc.l1CreatedAccounts[idxTokenIDToString(inst.to, inst.tokenID)] = tc.Users[inst.to].Accounts[inst.tokenID] |
||||
|
tx.ToIdx = common.Idx(idx) |
||||
|
idx++ |
||||
|
} else { |
||||
|
// if Idx account of To already exist, use it for ToIdx
|
||||
|
tx.ToIdx = tc.Users[inst.to].Accounts[inst.tokenID].Idx |
||||
|
} |
||||
|
} |
||||
|
currBatch.L1UserTxs = append(currBatch.L1UserTxs, tx) |
||||
|
case common.TxTypeTransfer: |
||||
|
if tc.Users[inst.from].Accounts[inst.tokenID] == nil { |
||||
|
log.Fatalf("Transfer from User %s for TokenID %d while account not created yet", inst.from, inst.tokenID) |
||||
|
} |
||||
|
tc.Users[inst.from].Accounts[inst.tokenID].Nonce++ |
||||
|
// if account of receiver does not exist, create a new CoordinatorL1Tx creating the account
|
||||
|
if _, ok := tc.l1CreatedAccounts[idxTokenIDToString(inst.to, inst.tokenID)]; !ok { |
||||
|
tx := common.L1Tx{ |
||||
|
FromEthAddr: tc.Users[inst.to].Addr, |
||||
|
FromBJJ: tc.Users[inst.to].BJJ.Public(), |
||||
|
TokenID: inst.tokenID, |
||||
|
LoadAmount: big.NewInt(int64(inst.amount)), |
||||
|
Type: common.TxTypeCreateAccountDeposit, |
||||
|
} |
||||
|
tc.Users[inst.to].Accounts[inst.tokenID] = &Account{ |
||||
|
Idx: common.Idx(idx), |
||||
|
Nonce: common.Nonce(0), |
||||
|
} |
||||
|
tc.l1CreatedAccounts[idxTokenIDToString(inst.to, inst.tokenID)] = tc.Users[inst.to].Accounts[inst.tokenID] |
||||
|
currBatch.L1CoordinatorTxs = append(currBatch.L1CoordinatorTxs, tx) |
||||
|
idx++ |
||||
|
} |
||||
|
tx := common.L2Tx{ |
||||
|
FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx, |
||||
|
ToIdx: tc.Users[inst.to].Accounts[inst.tokenID].Idx, |
||||
|
Amount: big.NewInt(int64(inst.amount)), |
||||
|
Fee: common.FeeSelector(inst.fee), |
||||
|
Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce, |
||||
|
Type: common.TxTypeTransfer, |
||||
|
} |
||||
|
nTx, err := common.NewPoolL2Tx(tx.PoolL2Tx()) |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
nL2Tx, err := nTx.L2Tx() |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
tx = *nL2Tx |
||||
|
tx.BatchNum = common.BatchNum(currBatchNum) // when converted to PoolL2Tx BatchNum parameter is lost
|
||||
|
|
||||
|
currBatch.L2Txs = append(currBatch.L2Txs, tx) |
||||
|
case common.TxTypeExit: |
||||
|
tc.Users[inst.from].Accounts[inst.tokenID].Nonce++ |
||||
|
tx := common.L2Tx{ |
||||
|
FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx, |
||||
|
ToIdx: common.Idx(1), // as is an Exit
|
||||
|
Amount: big.NewInt(int64(inst.amount)), |
||||
|
Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce, |
||||
|
Type: common.TxTypeExit, |
||||
|
} |
||||
|
nTx, err := common.NewPoolL2Tx(tx.PoolL2Tx()) |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
nL2Tx, err := nTx.L2Tx() |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
tx = *nL2Tx |
||||
|
currBatch.L2Txs = append(currBatch.L2Txs, tx) |
||||
|
case common.TxTypeForceExit: |
||||
|
tx := common.L1Tx{ |
||||
|
FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx, |
||||
|
ToIdx: common.Idx(1), // as is an Exit
|
||||
|
TokenID: inst.tokenID, |
||||
|
Amount: big.NewInt(int64(inst.amount)), |
||||
|
Type: common.TxTypeExit, |
||||
|
} |
||||
|
currBatch.L1UserTxs = append(currBatch.L1UserTxs, tx) |
||||
|
case typeNewBatch: |
||||
|
currBlock.Batches = append(currBlock.Batches, currBatch) |
||||
|
currBatchNum++ |
||||
|
currBatch = BatchData{} |
||||
|
case typeNewBlock: |
||||
|
currBlock.Batches = append(currBlock.Batches, currBatch) |
||||
|
currBatchNum++ |
||||
|
currBatch = BatchData{} |
||||
|
blocks = append(blocks, currBlock) |
||||
|
currBlock = BlockData{} |
||||
|
default: |
||||
|
log.Fatalf("Unexpected type: %s", inst.typ) |
||||
|
} |
||||
|
} |
||||
|
currBlock.Batches = append(currBlock.Batches, currBatch) |
||||
|
blocks = append(blocks, currBlock) |
||||
|
|
||||
|
return blocks |
||||
|
} |
||||
|
|
||||
|
// GeneratePoolL2Txs returns an array of common.PoolL2Tx from a given set. It
|
||||
|
// uses the accounts (keys & nonces) of the TestContext.
|
||||
|
func (tc *TestContext) GeneratePoolL2Txs(set string) []common.PoolL2Tx { |
||||
|
parser := newParser(strings.NewReader(set)) |
||||
|
parsedSet, err := parser.parse() |
||||
|
require.Nil(tc.t, err) |
||||
|
|
||||
|
tc.Instructions = parsedSet.instructions |
||||
|
tc.accountsNames = parsedSet.accounts |
||||
|
tc.TokenIDs = parsedSet.tokenIDs |
||||
|
|
||||
|
tc.generateKeys(tc.accountsNames) |
||||
|
|
||||
|
txs := []common.PoolL2Tx{} |
||||
|
for _, inst := range tc.Instructions { |
||||
|
switch inst.typ { |
||||
|
case common.TxTypeTransfer: |
||||
|
if tc.Users[inst.from].Accounts[inst.tokenID] == nil { |
||||
|
log.Fatalf("Transfer from User %s for TokenID %d while account not created yet", inst.from, inst.tokenID) |
||||
|
} |
||||
|
if tc.Users[inst.to].Accounts[inst.tokenID] == nil { |
||||
|
log.Fatalf("Transfer to User %s for TokenID %d while account not created yet", inst.to, inst.tokenID) |
||||
|
} |
||||
|
tc.Users[inst.from].Accounts[inst.tokenID].Nonce++ |
||||
|
// if account of receiver does not exist, don't use
|
||||
|
// ToIdx, and use only ToEthAddr & ToBJJ
|
||||
|
tx := common.PoolL2Tx{ |
||||
|
FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx, |
||||
|
ToIdx: tc.Users[inst.to].Accounts[inst.tokenID].Idx, |
||||
|
ToEthAddr: tc.Users[inst.to].Addr, |
||||
|
ToBJJ: tc.Users[inst.to].BJJ.Public(), |
||||
|
TokenID: inst.tokenID, |
||||
|
Amount: big.NewInt(int64(inst.amount)), |
||||
|
Fee: common.FeeSelector(inst.fee), |
||||
|
Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce, |
||||
|
State: common.PoolL2TxStatePending, |
||||
|
Timestamp: time.Now(), |
||||
|
RqToEthAddr: common.EmptyAddr, |
||||
|
RqToBJJ: nil, |
||||
|
Type: common.TxTypeTransfer, |
||||
|
} |
||||
|
nTx, err := common.NewPoolL2Tx(&tx) |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
tx = *nTx |
||||
|
// perform signature and set it to tx.Signature
|
||||
|
toSign, err := tx.HashToSign() |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
sig := tc.Users[inst.to].BJJ.SignPoseidon(toSign) |
||||
|
tx.Signature = sig |
||||
|
|
||||
|
txs = append(txs, tx) |
||||
|
case common.TxTypeExit: |
||||
|
tc.Users[inst.from].Accounts[inst.tokenID].Nonce++ |
||||
|
tx := common.PoolL2Tx{ |
||||
|
FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx, |
||||
|
ToIdx: common.Idx(1), // as is an Exit
|
||||
|
TokenID: inst.tokenID, |
||||
|
Amount: big.NewInt(int64(inst.amount)), |
||||
|
Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce, |
||||
|
Type: common.TxTypeExit, |
||||
|
} |
||||
|
txs = append(txs, tx) |
||||
|
default: |
||||
|
log.Fatalf("instruction type unrecognized: %s", inst.typ) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return txs |
||||
|
} |
||||
|
|
||||
|
// generateKeys generates BabyJubJub & Address keys for the given list of
|
||||
|
// account names in a deterministic way. This means, that for the same given
|
||||
|
// 'accNames' in a certain order, the keys will be always the same.
|
||||
|
func (tc *TestContext) generateKeys(accNames []string) { |
||||
|
for i := 1; i < len(accNames)+1; i++ { |
||||
|
if _, ok := tc.Users[accNames[i-1]]; ok { |
||||
|
// account already created
|
||||
|
continue |
||||
|
} |
||||
|
// babyjubjub key
|
||||
|
var sk babyjub.PrivateKey |
||||
|
copy(sk[:], []byte(strconv.Itoa(i))) // only for testing
|
||||
|
|
||||
|
// eth address
|
||||
|
var key ecdsa.PrivateKey |
||||
|
key.D = big.NewInt(int64(i)) // only for testing
|
||||
|
key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes()) |
||||
|
key.Curve = ethCrypto.S256() |
||||
|
addr := ethCrypto.PubkeyToAddress(key.PublicKey) |
||||
|
|
||||
|
u := User{ |
||||
|
BJJ: &sk, |
||||
|
Addr: addr, |
||||
|
Accounts: make(map[common.TokenID]*Account), |
||||
|
} |
||||
|
tc.Users[accNames[i-1]] = &u |
||||
|
} |
||||
|
} |
@ -0,0 +1,183 @@ |
|||||
|
package transakcio |
||||
|
|
||||
|
import ( |
||||
|
"math/big" |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/hermeznetwork/hermez-node/common" |
||||
|
"github.com/stretchr/testify/assert" |
||||
|
) |
||||
|
|
||||
|
func TestGenerateBlocks(t *testing.T) { |
||||
|
set := ` |
||||
|
Type: Blockchain |
||||
|
|
||||
|
CreateAccountDeposit(1) A: 10 |
||||
|
CreateAccountDeposit(2) A: 20 |
||||
|
CreateAccountDeposit(1) B: 5 |
||||
|
CreateAccountDeposit(1) C: 5 |
||||
|
CreateAccountDepositTransfer(1) D-A: 15, 10 (3) |
||||
|
|
||||
|
Transfer(1) A-B: 6 (1) |
||||
|
Transfer(1) B-D: 3 (1) |
||||
|
Transfer(1) A-D: 1 (1) |
||||
|
|
||||
|
// set new batch
|
||||
|
> batch |
||||
|
|
||||
|
DepositTransfer(1) A-B: 15, 10 (1) |
||||
|
Transfer(1) C-A : 3 (1) |
||||
|
Transfer(2) A-B: 15 (1) |
||||
|
|
||||
|
CreateAccountDeposit(1) User0: 20 |
||||
|
CreateAccountDeposit(3) User1: 20 |
||||
|
Transfer(1) User0-User1: 15 (1) |
||||
|
Transfer(3) User1-User0: 15 (1) |
||||
|
Transfer(1) A-C: 1 (1) |
||||
|
|
||||
|
> batch |
||||
|
|
||||
|
Transfer(1) User1-User0: 1 (1) |
||||
|
|
||||
|
> block |
||||
|
|
||||
|
// Exits
|
||||
|
Transfer(1) A-B: 1 (1) |
||||
|
Exit(1) A: 5 |
||||
|
` |
||||
|
tc := NewTestContext(t) |
||||
|
blocks := tc.GenerateBlocks(set) |
||||
|
assert.Equal(t, 2, len(blocks)) |
||||
|
assert.Equal(t, 3, len(blocks[0].Batches)) |
||||
|
assert.Equal(t, 1, len(blocks[1].Batches)) |
||||
|
assert.Equal(t, 5, len(blocks[0].Batches[0].L1UserTxs)) |
||||
|
assert.Equal(t, 0, len(blocks[1].Batches[0].L1UserTxs)) |
||||
|
|
||||
|
// Check expected values generated by each line
|
||||
|
// #0: Deposit(1) A: 10
|
||||
|
tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[0], common.TxTypeCreateAccountDeposit, 1, "A", "", big.NewInt(10), nil) |
||||
|
// #1: Deposit(2) A: 20
|
||||
|
tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[1], common.TxTypeCreateAccountDeposit, 2, "A", "", big.NewInt(20), nil) |
||||
|
// #2: Deposit(1) A: 20
|
||||
|
tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[2], common.TxTypeCreateAccountDeposit, 1, "B", "", big.NewInt(5), nil) |
||||
|
// #3: CreateAccountDeposit(1) C: 5
|
||||
|
tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[3], common.TxTypeCreateAccountDeposit, 1, "C", "", big.NewInt(5), nil) |
||||
|
// #4: CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
|
||||
|
tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[4], common.TxTypeCreateAccountDepositTransfer, 1, "D", "A", big.NewInt(15), big.NewInt(10)) |
||||
|
// #5: Transfer(1) A-B: 6 (1)
|
||||
|
tc.checkL2TxParams(t, blocks[0].Batches[0].L2Txs[0], common.TxTypeTransfer, 1, "A", "B", big.NewInt(6), common.BatchNum(0), common.Nonce(1)) |
||||
|
// #6: Transfer(1) B-D: 3 (1)
|
||||
|
tc.checkL2TxParams(t, blocks[0].Batches[0].L2Txs[1], common.TxTypeTransfer, 1, "B", "D", big.NewInt(3), common.BatchNum(0), common.Nonce(1)) |
||||
|
// #7: Transfer(1) A-D: 1 (1)
|
||||
|
tc.checkL2TxParams(t, blocks[0].Batches[0].L2Txs[2], common.TxTypeTransfer, 1, "A", "D", big.NewInt(1), common.BatchNum(0), common.Nonce(2)) |
||||
|
// change of Batch
|
||||
|
// #8: DepositTransfer(1) A-B: 15, 10 (1)
|
||||
|
tc.checkL1TxParams(t, blocks[0].Batches[1].L1UserTxs[0], common.TxTypeDepositTransfer, 1, "A", "B", big.NewInt(15), big.NewInt(10)) |
||||
|
// #9: Deposit(1) User0: 20
|
||||
|
tc.checkL1TxParams(t, blocks[0].Batches[1].L1UserTxs[0], common.TxTypeDepositTransfer, 1, "A", "B", big.NewInt(15), big.NewInt(10)) |
||||
|
// #10: Transfer(1) C-A : 3 (1)
|
||||
|
tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[0], common.TxTypeTransfer, 1, "C", "A", big.NewInt(3), common.BatchNum(1), common.Nonce(1)) |
||||
|
// #11: Transfer(2) A-B: 15 (1)
|
||||
|
tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[1], common.TxTypeTransfer, 2, "A", "B", big.NewInt(15), common.BatchNum(1), common.Nonce(1)) |
||||
|
// #12: Deposit(1) User0: 20
|
||||
|
tc.checkL1TxParams(t, blocks[0].Batches[1].L1UserTxs[1], common.TxTypeCreateAccountDeposit, 1, "User0", "", big.NewInt(20), nil) |
||||
|
// #13: Deposit(3) User1: 20
|
||||
|
tc.checkL1TxParams(t, blocks[0].Batches[1].L1UserTxs[2], common.TxTypeCreateAccountDeposit, 3, "User1", "", big.NewInt(20), nil) |
||||
|
// #14: Transfer(1) User0-User1: 15 (1)
|
||||
|
tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[2], common.TxTypeTransfer, 1, "User0", "User1", big.NewInt(15), common.BatchNum(1), common.Nonce(1)) |
||||
|
// #15: Transfer(3) User1-User0: 15 (1)
|
||||
|
tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[3], common.TxTypeTransfer, 3, "User1", "User0", big.NewInt(15), common.BatchNum(1), common.Nonce(1)) |
||||
|
// #16: Transfer(1) A-C: 1 (1)
|
||||
|
tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[4], common.TxTypeTransfer, 1, "A", "C", big.NewInt(1), common.BatchNum(1), common.Nonce(3)) |
||||
|
// change of Batch
|
||||
|
// #17: Transfer(1) User1-User0: 1 (1)
|
||||
|
tc.checkL2TxParams(t, blocks[0].Batches[2].L2Txs[0], common.TxTypeTransfer, 1, "User1", "User0", big.NewInt(1), common.BatchNum(2), common.Nonce(1)) |
||||
|
// change of Block (implies also a change of batch)
|
||||
|
// #18: Transfer(1) A-B: 1 (1)
|
||||
|
tc.checkL2TxParams(t, blocks[1].Batches[0].L2Txs[0], common.TxTypeTransfer, 1, "A", "B", big.NewInt(1), common.BatchNum(3), common.Nonce(4)) |
||||
|
} |
||||
|
|
||||
|
func (tc *TestContext) checkL1TxParams(t *testing.T, tx common.L1Tx, typ common.TxType, tokenID common.TokenID, from, to string, loadAmount, amount *big.Int) { |
||||
|
assert.Equal(t, typ, tx.Type) |
||||
|
if tx.FromIdx != common.Idx(0) { |
||||
|
assert.Equal(t, tc.Users[from].Accounts[tokenID].Idx, tx.FromIdx) |
||||
|
} |
||||
|
assert.Equal(t, tc.Users[from].Addr.Hex(), tx.FromEthAddr.Hex()) |
||||
|
assert.Equal(t, tc.Users[from].BJJ.Public(), tx.FromBJJ) |
||||
|
if tx.ToIdx != common.Idx(0) { |
||||
|
assert.Equal(t, tc.Users[to].Accounts[tokenID].Idx, tx.ToIdx) |
||||
|
} |
||||
|
if loadAmount != nil { |
||||
|
assert.Equal(t, loadAmount, tx.LoadAmount) |
||||
|
} |
||||
|
if amount != nil { |
||||
|
assert.Equal(t, amount, tx.Amount) |
||||
|
} |
||||
|
} |
||||
|
func (tc *TestContext) checkL2TxParams(t *testing.T, tx common.L2Tx, typ common.TxType, tokenID common.TokenID, from, to string, amount *big.Int, batchNum common.BatchNum, nonce common.Nonce) { |
||||
|
assert.Equal(t, typ, tx.Type) |
||||
|
assert.Equal(t, tc.Users[from].Accounts[tokenID].Idx, tx.FromIdx) |
||||
|
if tx.Type != common.TxTypeExit { |
||||
|
assert.Equal(t, tc.Users[to].Accounts[tokenID].Idx, tx.ToIdx) |
||||
|
} |
||||
|
if amount != nil { |
||||
|
assert.Equal(t, amount, tx.Amount) |
||||
|
} |
||||
|
assert.Equal(t, batchNum, tx.BatchNum) |
||||
|
assert.Equal(t, nonce, tx.Nonce) |
||||
|
} |
||||
|
|
||||
|
func TestGeneratePoolL2Txs(t *testing.T) { |
||||
|
set := ` |
||||
|
Type: Blockchain |
||||
|
|
||||
|
CreateAccountDeposit(1) A: 10 |
||||
|
CreateAccountDeposit(2) A: 20 |
||||
|
CreateAccountDeposit(1) B: 5 |
||||
|
CreateAccountDeposit(1) C: 5 |
||||
|
CreateAccountDeposit(1) User0: 5 |
||||
|
CreateAccountDeposit(1) User1: 0 |
||||
|
CreateAccountDeposit(3) User0: 0 |
||||
|
CreateAccountDeposit(3) User1: 5 |
||||
|
CreateAccountDeposit(2) B: 5 |
||||
|
CreateAccountDeposit(2) D: 0 |
||||
|
` |
||||
|
tc := NewTestContext(t) |
||||
|
_ = tc.GenerateBlocks(set) |
||||
|
set = ` |
||||
|
Type: PoolL2 |
||||
|
PoolTransfer(1) A-B: 6 (1) |
||||
|
PoolTransfer(1) B-C: 3 (1) |
||||
|
PoolTransfer(1) C-A: 3 (1) |
||||
|
PoolTransfer(1) A-B: 1 (1) |
||||
|
PoolTransfer(2) A-B: 15 (1) |
||||
|
PoolTransfer(1) User0-User1: 15 (1) |
||||
|
PoolTransfer(3) User1-User0: 15 (1) |
||||
|
PoolTransfer(2) B-D: 3 (1) |
||||
|
PoolExit(1) A: 3 |
||||
|
` |
||||
|
poolL2Txs := tc.GeneratePoolL2Txs(set) |
||||
|
assert.Equal(t, 9, len(poolL2Txs)) |
||||
|
assert.Equal(t, common.TxTypeTransfer, poolL2Txs[0].Type) |
||||
|
assert.Equal(t, common.TxTypeExit, poolL2Txs[8].Type) |
||||
|
assert.Equal(t, tc.Users["B"].Addr.Hex(), poolL2Txs[0].ToEthAddr.Hex()) |
||||
|
assert.Equal(t, tc.Users["B"].BJJ.Public().String(), poolL2Txs[0].ToBJJ.String()) |
||||
|
assert.Equal(t, tc.Users["User1"].Addr.Hex(), poolL2Txs[5].ToEthAddr.Hex()) |
||||
|
assert.Equal(t, tc.Users["User1"].BJJ.Public().String(), poolL2Txs[5].ToBJJ.String()) |
||||
|
|
||||
|
assert.Equal(t, common.Nonce(1), poolL2Txs[0].Nonce) |
||||
|
assert.Equal(t, common.Nonce(2), poolL2Txs[3].Nonce) |
||||
|
assert.Equal(t, common.Nonce(3), poolL2Txs[8].Nonce) |
||||
|
|
||||
|
// load another set in the same TestContext
|
||||
|
set = ` |
||||
|
Type: PoolL2 |
||||
|
PoolTransfer(1) A-B: 6 (1) |
||||
|
PoolTransfer(1) B-C: 3 (1) |
||||
|
PoolTransfer(1) A-C: 3 (1) |
||||
|
` |
||||
|
poolL2Txs = tc.GeneratePoolL2Txs(set) |
||||
|
assert.Equal(t, common.Nonce(4), poolL2Txs[0].Nonce) |
||||
|
assert.Equal(t, common.Nonce(2), poolL2Txs[1].Nonce) |
||||
|
assert.Equal(t, common.Nonce(5), poolL2Txs[2].Nonce) |
||||
|
} |
@ -0,0 +1,9 @@ |
|||||
|
package transakcio |
||||
|
|
||||
|
// TODO
|
||||
|
// func extendTokenIDs(o, n []common.TokenID) []common.TokenID {
|
||||
|
// return o
|
||||
|
// }
|
||||
|
// func extendAccounts(o, n map[string]*Account) map[string]*Account {
|
||||
|
// return o
|
||||
|
// }
|
@ -1,183 +0,0 @@ |
|||||
package test |
|
||||
|
|
||||
import ( |
|
||||
"crypto/ecdsa" |
|
||||
"math/big" |
|
||||
"strconv" |
|
||||
"strings" |
|
||||
"testing" |
|
||||
|
|
||||
ethCommon "github.com/ethereum/go-ethereum/common" |
|
||||
ethCrypto "github.com/ethereum/go-ethereum/crypto" |
|
||||
"github.com/hermeznetwork/hermez-node/common" |
|
||||
"github.com/iden3/go-iden3-crypto/babyjub" |
|
||||
"github.com/stretchr/testify/require" |
|
||||
) |
|
||||
|
|
||||
// Account contains the data related to a testing account
|
|
||||
type Account struct { |
|
||||
BJJ *babyjub.PrivateKey |
|
||||
Addr ethCommon.Address |
|
||||
Idx common.Idx |
|
||||
Nonce common.Nonce |
|
||||
} |
|
||||
|
|
||||
// GenerateKeys generates BabyJubJub & Address keys for the given list of
|
|
||||
// account names in a deterministic way. This means, that for the same given
|
|
||||
// 'accNames' the keys will be always the same.
|
|
||||
func GenerateKeys(t *testing.T, accNames []string) map[string]*Account { |
|
||||
acc := make(map[string]*Account) |
|
||||
for i := 1; i < len(accNames)+1; i++ { |
|
||||
// babyjubjub key
|
|
||||
var sk babyjub.PrivateKey |
|
||||
copy(sk[:], []byte(strconv.Itoa(i))) // only for testing
|
|
||||
|
|
||||
// eth address
|
|
||||
var key ecdsa.PrivateKey |
|
||||
key.D = big.NewInt(int64(i)) // only for testing
|
|
||||
key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes()) |
|
||||
key.Curve = ethCrypto.S256() |
|
||||
addr := ethCrypto.PubkeyToAddress(key.PublicKey) |
|
||||
|
|
||||
a := Account{ |
|
||||
BJJ: &sk, |
|
||||
Addr: addr, |
|
||||
Nonce: 0, |
|
||||
} |
|
||||
acc[accNames[i-1]] = &a |
|
||||
} |
|
||||
return acc |
|
||||
} |
|
||||
|
|
||||
// GenerateTestTxs generates L1Tx & PoolL2Tx in a deterministic way for the
|
|
||||
// given Instructions.
|
|
||||
func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]common.L1Tx, [][]common.L1Tx, [][]common.PoolL2Tx, []common.Token) { |
|
||||
accounts := GenerateKeys(t, instructions.Accounts) |
|
||||
l1CreatedAccounts := make(map[string]*Account) |
|
||||
|
|
||||
var batchL1Txs []common.L1Tx |
|
||||
var batchCoordinatorL1Txs []common.L1Tx |
|
||||
var batchPoolL2Txs []common.PoolL2Tx |
|
||||
var l1Txs [][]common.L1Tx |
|
||||
var coordinatorL1Txs [][]common.L1Tx |
|
||||
var poolL2Txs [][]common.PoolL2Tx |
|
||||
idx := 256 |
|
||||
for _, inst := range instructions.Instructions { |
|
||||
switch inst.Type { |
|
||||
case common.TxTypeCreateAccountDeposit: |
|
||||
tx := common.L1Tx{ |
|
||||
// TxID
|
|
||||
FromEthAddr: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Addr, |
|
||||
FromBJJ: accounts[idxTokenIDToString(inst.From, inst.TokenID)].BJJ.Public(), |
|
||||
TokenID: inst.TokenID, |
|
||||
Amount: big.NewInt(0), |
|
||||
LoadAmount: big.NewInt(int64(inst.Amount)), |
|
||||
Type: common.TxTypeCreateAccountDeposit, |
|
||||
} |
|
||||
batchL1Txs = append(batchL1Txs, tx) |
|
||||
if accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx == common.Idx(0) { // if account.Idx is not set yet, set it and increment idx
|
|
||||
accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx = common.Idx(idx) |
|
||||
|
|
||||
l1CreatedAccounts[idxTokenIDToString(inst.From, inst.TokenID)] = accounts[idxTokenIDToString(inst.From, inst.TokenID)] |
|
||||
idx++ |
|
||||
} |
|
||||
case common.TxTypeTransfer: |
|
||||
// if account of receiver does not exist, create a new CoordinatorL1Tx creating the account
|
|
||||
if _, ok := l1CreatedAccounts[idxTokenIDToString(inst.To, inst.TokenID)]; !ok { |
|
||||
tx := common.L1Tx{ |
|
||||
FromEthAddr: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr, |
|
||||
FromBJJ: accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.Public(), |
|
||||
TokenID: inst.TokenID, |
|
||||
LoadAmount: big.NewInt(int64(inst.Amount)), |
|
||||
Type: common.TxTypeCreateAccountDeposit, |
|
||||
} |
|
||||
accounts[idxTokenIDToString(inst.To, inst.TokenID)].Idx = common.Idx(idx) |
|
||||
l1CreatedAccounts[idxTokenIDToString(inst.To, inst.TokenID)] = accounts[idxTokenIDToString(inst.To, inst.TokenID)] |
|
||||
batchCoordinatorL1Txs = append(batchCoordinatorL1Txs, tx) |
|
||||
idx++ |
|
||||
} |
|
||||
tx := common.PoolL2Tx{ |
|
||||
FromIdx: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx, |
|
||||
ToIdx: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Idx, |
|
||||
ToEthAddr: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr, |
|
||||
ToBJJ: accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.Public(), |
|
||||
TokenID: inst.TokenID, |
|
||||
Amount: big.NewInt(int64(inst.Amount)), |
|
||||
Fee: common.FeeSelector(inst.Fee), |
|
||||
Nonce: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Nonce, |
|
||||
State: common.PoolL2TxStatePending, |
|
||||
RqToEthAddr: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr, |
|
||||
RqToBJJ: accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.Public(), |
|
||||
Type: common.TxTypeTransfer, |
|
||||
} |
|
||||
nTx, err := common.NewPoolL2Tx(&tx) |
|
||||
if err != nil { |
|
||||
panic(err) |
|
||||
} |
|
||||
tx = *nTx |
|
||||
// perform signature and set it to tx.Signature
|
|
||||
toSign, err := tx.HashToSign() |
|
||||
if err != nil { |
|
||||
panic(err) |
|
||||
} |
|
||||
sig := accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.SignPoseidon(toSign) |
|
||||
tx.Signature = sig |
|
||||
|
|
||||
accounts[idxTokenIDToString(inst.From, inst.TokenID)].Nonce++ |
|
||||
batchPoolL2Txs = append(batchPoolL2Txs, tx) |
|
||||
|
|
||||
case common.TxTypeExit, common.TxTypeForceExit: |
|
||||
tx := common.L1Tx{ |
|
||||
FromIdx: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx, |
|
||||
ToIdx: common.Idx(1), // as is an Exit
|
|
||||
TokenID: inst.TokenID, |
|
||||
Amount: big.NewInt(int64(inst.Amount)), |
|
||||
Type: common.TxTypeExit, |
|
||||
} |
|
||||
batchL1Txs = append(batchL1Txs, tx) |
|
||||
case TypeNewBatch: |
|
||||
l1Txs = append(l1Txs, batchL1Txs) |
|
||||
coordinatorL1Txs = append(coordinatorL1Txs, batchCoordinatorL1Txs) |
|
||||
poolL2Txs = append(poolL2Txs, batchPoolL2Txs) |
|
||||
batchL1Txs = []common.L1Tx{} |
|
||||
batchCoordinatorL1Txs = []common.L1Tx{} |
|
||||
batchPoolL2Txs = []common.PoolL2Tx{} |
|
||||
default: |
|
||||
continue |
|
||||
} |
|
||||
} |
|
||||
l1Txs = append(l1Txs, batchL1Txs) |
|
||||
coordinatorL1Txs = append(coordinatorL1Txs, batchCoordinatorL1Txs) |
|
||||
poolL2Txs = append(poolL2Txs, batchPoolL2Txs) |
|
||||
tokens := []common.Token{} |
|
||||
for i := 0; i < len(poolL2Txs); i++ { |
|
||||
for j := 0; j < len(poolL2Txs[i]); j++ { |
|
||||
id := poolL2Txs[i][j].TokenID |
|
||||
found := false |
|
||||
for k := 0; k < len(tokens); k++ { |
|
||||
if tokens[k].TokenID == id { |
|
||||
found = true |
|
||||
break |
|
||||
} |
|
||||
} |
|
||||
if !found { |
|
||||
tokens = append(tokens, common.Token{ |
|
||||
TokenID: id, |
|
||||
EthBlockNum: 1, |
|
||||
EthAddr: ethCommon.BigToAddress(big.NewInt(int64(i*10000 + j))), |
|
||||
}) |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
return l1Txs, coordinatorL1Txs, poolL2Txs, tokens |
|
||||
} |
|
||||
|
|
||||
// GenerateTestTxsFromSet reurns the L1 & L2 transactions for a given Set of
|
|
||||
// Instructions code
|
|
||||
func GenerateTestTxsFromSet(t *testing.T, set string) ([][]common.L1Tx, [][]common.L1Tx, [][]common.PoolL2Tx, []common.Token) { |
|
||||
parser := NewParser(strings.NewReader(set)) |
|
||||
instructions, err := parser.Parse() |
|
||||
require.Nil(t, err) |
|
||||
|
|
||||
return GenerateTestTxs(t, instructions) |
|
||||
} |
|
@ -1,59 +0,0 @@ |
|||||
package test |
|
||||
|
|
||||
import ( |
|
||||
"strings" |
|
||||
"testing" |
|
||||
|
|
||||
"github.com/hermeznetwork/hermez-node/common" |
|
||||
"github.com/stretchr/testify/assert" |
|
||||
) |
|
||||
|
|
||||
func TestGenerateTestL2Txs(t *testing.T) { |
|
||||
s := ` |
|
||||
A (1): 10 |
|
||||
A (2): 20 |
|
||||
B (1): 5 |
|
||||
A-B (1): 6 1 |
|
||||
B-C (1): 3 1 |
|
||||
> advance batch |
|
||||
C-A (1): 3 1 |
|
||||
A-B (1): 1 1 |
|
||||
A-B (2): 15 1 |
|
||||
User0 (1): 20 |
|
||||
User1 (3) : 20 |
|
||||
User0-User1 (1): 15 1 |
|
||||
User1-User0 (3): 15 1 |
|
||||
B-D (2): 3 1 |
|
||||
` |
|
||||
parser := NewParser(strings.NewReader(s)) |
|
||||
instructions, err := parser.Parse() |
|
||||
assert.Nil(t, err) |
|
||||
|
|
||||
l1txs, coordinatorL1txs, l2txs, _ := GenerateTestTxs(t, instructions) |
|
||||
assert.Equal(t, 2, len(l1txs)) |
|
||||
assert.Equal(t, 3, len(l1txs[0])) |
|
||||
assert.Equal(t, 1, len(coordinatorL1txs[0])) |
|
||||
assert.Equal(t, 2, len(l2txs[0])) |
|
||||
assert.Equal(t, 2, len(l1txs[1])) |
|
||||
assert.Equal(t, 4, len(coordinatorL1txs[1])) |
|
||||
assert.Equal(t, 6, len(l2txs[1])) |
|
||||
|
|
||||
accounts := GenerateKeys(t, instructions.Accounts) |
|
||||
|
|
||||
// l1txs
|
|
||||
assert.Equal(t, common.TxTypeCreateAccountDeposit, l1txs[0][0].Type) |
|
||||
assert.Equal(t, accounts["A1"].BJJ.Public().String(), l1txs[0][0].FromBJJ.String()) |
|
||||
assert.Equal(t, accounts["A2"].BJJ.Public().String(), l1txs[0][1].FromBJJ.String()) |
|
||||
assert.Equal(t, accounts["B1"].BJJ.Public().String(), l1txs[0][2].FromBJJ.String()) |
|
||||
assert.Equal(t, accounts["User13"].BJJ.Public().String(), l1txs[1][1].FromBJJ.String()) |
|
||||
|
|
||||
// l2txs
|
|
||||
assert.Equal(t, common.TxTypeTransfer, l2txs[0][0].Type) |
|
||||
assert.Equal(t, common.Idx(256), l2txs[0][0].FromIdx) |
|
||||
assert.Equal(t, common.Idx(258), l2txs[0][0].ToIdx) |
|
||||
assert.Equal(t, accounts["B1"].BJJ.Public().String(), l2txs[0][0].ToBJJ.String()) |
|
||||
assert.Equal(t, accounts["B1"].Addr.Hex(), l2txs[0][0].ToEthAddr.Hex()) |
|
||||
assert.Equal(t, common.Nonce(0), l2txs[0][0].Nonce) |
|
||||
assert.Equal(t, common.Nonce(1), l2txs[1][1].Nonce) |
|
||||
assert.Equal(t, common.FeeSelector(1), l2txs[0][0].Fee) |
|
||||
} |
|