mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 11:26:44 +01:00
Add transakcio set type define, add set load [...]
Add transakcio set type definition, add set loading, move transakcio to package, adapt branch to last master updates (fix compile due new common types & git conflicts). Update tests to pass the test, pending to adapt to new Transakcio interface.
This commit is contained in:
523
test/transakcio/lang.go
Normal file
523
test/transakcio/lang.go
Normal file
@@ -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
|
||||
}
|
||||
193
test/transakcio/lang_test.go
Normal file
193
test/transakcio/lang_test.go
Normal file
@@ -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())
|
||||
}
|
||||
162
test/transakcio/sets.go
Normal file
162
test/transakcio/sets.go
Normal file
@@ -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)
|
||||
`
|
||||
21
test/transakcio/sets_test.go
Normal file
21
test/transakcio/sets_test.go
Normal file
@@ -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)
|
||||
}
|
||||
340
test/transakcio/txs.go
Normal file
340
test/transakcio/txs.go
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
183
test/transakcio/txs_test.go
Normal file
183
test/transakcio/txs_test.go
Normal file
@@ -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)
|
||||
}
|
||||
9
test/transakcio/utils.go
Normal file
9
test/transakcio/utils.go
Normal file
@@ -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
|
||||
// }
|
||||
Reference in New Issue
Block a user