Feature/testing framework languagefeature/sql-semaphore1
@ -0,0 +1,195 @@ |
|||
package statedb |
|||
|
|||
import ( |
|||
"encoding/binary" |
|||
"math/big" |
|||
|
|||
"github.com/hermeznetwork/hermez-node/common" |
|||
"github.com/iden3/go-merkletree/db" |
|||
) |
|||
|
|||
// KEYIDX is used as key in the db to store the current Idx
|
|||
var KEYIDX = []byte("idx") |
|||
|
|||
// ProcessPoolL2Tx process the given PoolL2Tx applying the needed updates to
|
|||
// the StateDB depending on the transaction Type.
|
|||
func (s *StateDB) ProcessPoolL2Tx(tx *common.PoolL2Tx) error { |
|||
switch tx.Type { |
|||
case common.TxTypeTransfer: |
|||
// go to the MT account of sender and receiver, and update
|
|||
// balance & nonce
|
|||
err := s.applyTransfer(tx.Tx()) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
case common.TxTypeExit: |
|||
// execute exit flow
|
|||
default: |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
// ProcessL1Tx process the given L1Tx applying the needed updates to the
|
|||
// StateDB depending on the transaction Type.
|
|||
func (s *StateDB) ProcessL1Tx(tx *common.L1Tx) error { |
|||
switch tx.Type { |
|||
case common.TxTypeForceTransfer, common.TxTypeTransfer: |
|||
// go to the MT account of sender and receiver, and update balance
|
|||
// & nonce
|
|||
err := s.applyTransfer(tx.Tx()) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
case common.TxTypeCreateAccountDeposit: |
|||
// add new account to the MT, update balance of the MT account
|
|||
err := s.applyCreateAccount(tx) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
case common.TxTypeDeposit: |
|||
// update balance of the MT account
|
|||
err := s.applyDeposit(tx, false) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
case common.TxTypeDepositTransfer: |
|||
// update balance in MT account, update balance & nonce of sender
|
|||
// & receiver
|
|||
err := s.applyDeposit(tx, true) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
case common.TxTypeCreateAccountDepositTransfer: |
|||
// add new account to the merkletree, update balance in MT account,
|
|||
// update balance & nonce of sender & receiver
|
|||
err := s.applyCreateAccount(tx) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
err = s.applyTransfer(tx.Tx()) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
case common.TxTypeExit: |
|||
// execute exit flow
|
|||
default: |
|||
} |
|||
|
|||
return nil |
|||
} |
|||
|
|||
// applyCreateAccount creates a new account in the account of the depositer, it
|
|||
// stores the deposit value
|
|||
func (s *StateDB) applyCreateAccount(tx *common.L1Tx) error { |
|||
account := &common.Account{ |
|||
TokenID: tx.TokenID, |
|||
Nonce: 0, |
|||
Balance: tx.LoadAmount, |
|||
PublicKey: tx.FromBJJ, |
|||
EthAddr: tx.FromEthAddr, |
|||
} |
|||
|
|||
err := s.CreateAccount(common.Idx(s.idx+1), account) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
|
|||
s.idx = s.idx + 1 |
|||
return s.setIdx(s.idx) |
|||
} |
|||
|
|||
// applyDeposit updates the balance in the account of the depositer, if
|
|||
// andTransfer parameter is set to true, the method will also apply the
|
|||
// Transfer of the L1Tx/DepositTransfer
|
|||
func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error { |
|||
// deposit the tx.LoadAmount into the sender account
|
|||
accSender, err := s.GetAccount(tx.FromIdx) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
accSender.Balance = new(big.Int).Add(accSender.Balance, tx.LoadAmount) |
|||
|
|||
// in case that the tx is a L1Tx>DepositTransfer
|
|||
if transfer { |
|||
accReceiver, err := s.GetAccount(tx.ToIdx) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
// substract amount to the sender
|
|||
accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount) |
|||
// add amount to the receiver
|
|||
accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount) |
|||
// update receiver account in localStateDB
|
|||
err = s.UpdateAccount(tx.ToIdx, accReceiver) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
} |
|||
// update sender account in localStateDB
|
|||
err = s.UpdateAccount(tx.FromIdx, accSender) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
// applyTransfer updates the balance & nonce in the account of the sender, and
|
|||
// the balance in the account of the receiver
|
|||
func (s *StateDB) applyTransfer(tx *common.Tx) error { |
|||
// get sender and receiver accounts from localStateDB
|
|||
accSender, err := s.GetAccount(tx.FromIdx) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
accReceiver, err := s.GetAccount(tx.ToIdx) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
|
|||
// substract amount to the sender
|
|||
accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount) |
|||
// add amount to the receiver
|
|||
accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount) |
|||
|
|||
// update receiver account in localStateDB
|
|||
err = s.UpdateAccount(tx.ToIdx, accReceiver) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
// update sender account in localStateDB
|
|||
err = s.UpdateAccount(tx.FromIdx, accSender) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
|
|||
return nil |
|||
} |
|||
|
|||
// getIdx returns the stored Idx from the localStateDB, which is the last Idx
|
|||
// used for an Account in the localStateDB.
|
|||
func (s *StateDB) getIdx() (uint64, error) { |
|||
idxBytes, err := s.DB().Get(KEYIDX) |
|||
if err == db.ErrNotFound { |
|||
return 0, nil |
|||
} |
|||
if err != nil { |
|||
return 0, err |
|||
} |
|||
idx := binary.LittleEndian.Uint64(idxBytes[:8]) |
|||
return idx, nil |
|||
} |
|||
|
|||
// setIdx stores Idx in the localStateDB
|
|||
func (s *StateDB) setIdx(idx uint64) error { |
|||
tx, err := s.DB().NewTx() |
|||
if err != nil { |
|||
return err |
|||
} |
|||
var idxBytes [8]byte |
|||
binary.LittleEndian.PutUint64(idxBytes[:], idx) |
|||
tx.Put(KEYIDX, idxBytes[:]) |
|||
if err := tx.Commit(); err != nil { |
|||
return err |
|||
} |
|||
return nil |
|||
} |
@ -0,0 +1,356 @@ |
|||
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 ecomment = fmt.Errorf("comment in parseline") |
|||
|
|||
const ( |
|||
ILLEGAL Token = iota |
|||
WS |
|||
EOF |
|||
|
|||
IDENT // val
|
|||
) |
|||
|
|||
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
|
|||
} |
|||
|
|||
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() |
|||
} |
|||
|
|||
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
|
|||
c := &Instruction{} |
|||
tok, lit := p.scanIgnoreWhitespace() |
|||
if tok == EOF { |
|||
return nil, errof |
|||
} |
|||
c.Literal += lit |
|||
if lit == "/" { |
|||
_, _ = p.s.r.ReadString('\n') |
|||
return nil, ecomment |
|||
} |
|||
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 > 255 { |
|||
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 |
|||
} |
|||
|
|||
// 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 == ecomment { |
|||
i++ |
|||
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[instruction.From] = true |
|||
if instruction.Type == 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,80 @@ |
|||
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 |
|||
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, 12, len(instructions.Instructions)) |
|||
assert.Equal(t, 5, 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, "User0 (1): 20", instructions.Instructions[7].Raw()) |
|||
assert.Equal(t, "Type: Create&Deposit, From: User0, Amount: 20, TokenID: 1,\n", instructions.Instructions[7].String()) |
|||
assert.Equal(t, "User0-User1 (1): 15 1", instructions.Instructions[9].Raw()) |
|||
assert.Equal(t, "Type: Transfer, From: User0, To: User1, Amount: 15, Fee: 1, TokenID: 1,\n", instructions.Instructions[9].String()) |
|||
assert.Equal(t, "A (1)E: 5", instructions.Instructions[11].Raw()) |
|||
assert.Equal(t, "Type: ForceExit, From: A, Amount: 5, TokenID: 1,\n", instructions.Instructions[11].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()) |
|||
} |
@ -0,0 +1,111 @@ |
|||
package test |
|||
|
|||
import ( |
|||
"crypto/ecdsa" |
|||
"math/big" |
|||
"strconv" |
|||
"testing" |
|||
"time" |
|||
|
|||
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" |
|||
) |
|||
|
|||
type Account struct { |
|||
BJJ *babyjub.PrivateKey |
|||
Addr ethCommon.Address |
|||
Idx common.Idx |
|||
Nonce uint64 |
|||
} |
|||
|
|||
// 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.PoolL2Tx) { |
|||
accounts := GenerateKeys(t, instructions.Accounts) |
|||
|
|||
// debug
|
|||
// fmt.Println("accounts:")
|
|||
// for n, a := range accounts {
|
|||
// fmt.Printf(" %s: bjj:%s - addr:%s\n", n, a.BJJ.Public().String()[:10], a.Addr.Hex()[:10])
|
|||
// }
|
|||
|
|||
var l1txs []*common.L1Tx |
|||
var l2txs []*common.PoolL2Tx |
|||
idx := 1 |
|||
for _, inst := range instructions.Instructions { |
|||
switch inst.Type { |
|||
case common.TxTypeCreateAccountDeposit: |
|||
tx := common.L1Tx{ |
|||
// TxID
|
|||
FromEthAddr: accounts[inst.From].Addr, |
|||
FromBJJ: accounts[inst.From].BJJ.Public(), |
|||
TokenID: inst.TokenID, |
|||
LoadAmount: big.NewInt(int64(inst.Amount)), |
|||
Type: common.TxTypeCreateAccountDeposit, |
|||
} |
|||
l1txs = append(l1txs, &tx) |
|||
if accounts[inst.From].Idx == common.Idx(0) { // if account.Idx is not set yet, set it and increment idx
|
|||
accounts[inst.From].Idx = common.Idx(idx) |
|||
idx++ |
|||
} |
|||
case common.TxTypeTransfer: |
|||
tx := common.PoolL2Tx{ |
|||
// TxID: nil,
|
|||
FromIdx: accounts[inst.From].Idx, |
|||
ToIdx: accounts[inst.To].Idx, |
|||
ToEthAddr: accounts[inst.To].Addr, |
|||
ToBJJ: accounts[inst.To].BJJ.Public(), |
|||
TokenID: inst.TokenID, |
|||
Amount: big.NewInt(int64(inst.Amount)), |
|||
Fee: common.FeeSelector(inst.Fee), |
|||
Nonce: accounts[inst.From].Nonce, |
|||
State: common.PoolL2TxStatePending, |
|||
Timestamp: time.Now(), |
|||
BatchNum: 0, |
|||
Type: common.TxTypeTransfer, |
|||
} |
|||
// if inst.Fee == 0 {
|
|||
// tx.Fee = common.FeeSelector(i % 255)
|
|||
// }
|
|||
// TODO once signature function is ready, perform
|
|||
// signature and set it to tx.Signature
|
|||
|
|||
accounts[inst.From].Nonce++ |
|||
l2txs = append(l2txs, &tx) |
|||
default: |
|||
continue |
|||
} |
|||
|
|||
} |
|||
|
|||
return l1txs, l2txs |
|||
} |
@ -0,0 +1,50 @@ |
|||
package test |
|||
|
|||
import ( |
|||
"strings" |
|||
"testing" |
|||
|
|||
"github.com/hermeznetwork/hermez-node/common" |
|||
"github.com/stretchr/testify/assert" |
|||
"github.com/stretchr/testify/require" |
|||
) |
|||
|
|||
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 |
|||
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 |
|||
` |
|||
parser := NewParser(strings.NewReader(s)) |
|||
instructions, err := parser.Parse() |
|||
assert.Nil(t, err) |
|||
|
|||
l1txs, l2txs := GenerateTestTxs(t, instructions) |
|||
require.Equal(t, 5, len(l1txs)) |
|||
require.Equal(t, 6, len(l2txs)) |
|||
|
|||
// l1txs
|
|||
assert.Equal(t, common.TxTypeCreateAccountDeposit, l1txs[0].Type) |
|||
assert.Equal(t, "5bac784d938067d980a9d39bdd79bf84a0cbb296977c47cc30de2d5ce9229d2f", l1txs[0].FromBJJ.String()) |
|||
assert.Equal(t, "5bac784d938067d980a9d39bdd79bf84a0cbb296977c47cc30de2d5ce9229d2f", l1txs[1].FromBJJ.String()) |
|||
assert.Equal(t, "323ff10c28df37ecb787fe216e111db64aa7cfa2c517509fe0057ff08a10b30c", l1txs[2].FromBJJ.String()) |
|||
assert.Equal(t, "a25c7150609ecfcf90fc3f419474e8bc28ea5978df1b0a68339bff884c117e19", l1txs[4].FromBJJ.String()) |
|||
|
|||
// l2txs
|
|||
assert.Equal(t, common.TxTypeTransfer, l2txs[0].Type) |
|||
assert.Equal(t, common.Idx(1), l2txs[0].FromIdx) |
|||
assert.Equal(t, common.Idx(2), l2txs[0].ToIdx) |
|||
assert.Equal(t, "323ff10c28df37ecb787fe216e111db64aa7cfa2c517509fe0057ff08a10b30c", l2txs[0].ToBJJ.String()) |
|||
assert.Equal(t, "0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF", l2txs[0].ToEthAddr.Hex()) |
|||
assert.Equal(t, uint64(0), l2txs[0].Nonce) |
|||
assert.Equal(t, uint64(1), l2txs[3].Nonce) |
|||
assert.Equal(t, common.FeeSelector(1), l2txs[0].Fee) |
|||
} |