mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Add TxCompressedData & V2 with some extra parsers for common
This commit is contained in:
42
test/lang.go
42
test/lang.go
@@ -16,7 +16,7 @@ var errof = fmt.Errorf("eof in parseline")
|
||||
var ecomment = fmt.Errorf("comment in parseline")
|
||||
|
||||
const (
|
||||
ILLEGAL Token = iota
|
||||
ILLEGAL token = iota
|
||||
WS
|
||||
EOF
|
||||
|
||||
@@ -80,9 +80,9 @@ func (i Instruction) Raw() string {
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
type Token int
|
||||
type token int
|
||||
|
||||
type Scanner struct {
|
||||
type scanner struct {
|
||||
r *bufio.Reader
|
||||
}
|
||||
|
||||
@@ -102,12 +102,12 @@ 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)}
|
||||
// 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 {
|
||||
func (s *scanner) read() rune {
|
||||
ch, _, err := s.r.ReadRune()
|
||||
if err != nil {
|
||||
return eof
|
||||
@@ -115,12 +115,12 @@ func (s *Scanner) read() rune {
|
||||
return ch
|
||||
}
|
||||
|
||||
func (s *Scanner) unread() {
|
||||
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) {
|
||||
// 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) {
|
||||
@@ -144,7 +144,7 @@ func (s *Scanner) scan() (tok Token, lit string) {
|
||||
return ILLEGAL, string(ch)
|
||||
}
|
||||
|
||||
func (s *Scanner) scanWhitespace() (token Token, lit string) {
|
||||
func (s *scanner) scanWhitespace() (token token, lit string) {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteRune(s.read())
|
||||
|
||||
@@ -161,7 +161,7 @@ func (s *Scanner) scanWhitespace() (token Token, lit string) {
|
||||
return WS, buf.String()
|
||||
}
|
||||
|
||||
func (s *Scanner) scanIndent() (tok Token, lit string) {
|
||||
func (s *scanner) scanIndent() (tok token, lit string) {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteRune(s.read())
|
||||
|
||||
@@ -177,16 +177,16 @@ func (s *Scanner) scanIndent() (tok Token, lit string) {
|
||||
}
|
||||
|
||||
if len(buf.String()) == 1 {
|
||||
return Token(rune(buf.String()[0])), buf.String()
|
||||
return token(rune(buf.String()[0])), buf.String()
|
||||
}
|
||||
return IDENT, buf.String()
|
||||
}
|
||||
|
||||
// Parser defines the parser
|
||||
type Parser struct {
|
||||
s *Scanner
|
||||
s *scanner
|
||||
buf struct {
|
||||
tok Token
|
||||
tok token
|
||||
lit string
|
||||
n int
|
||||
}
|
||||
@@ -197,7 +197,7 @@ func NewParser(r io.Reader) *Parser {
|
||||
return &Parser{s: NewScanner(r)}
|
||||
}
|
||||
|
||||
func (p *Parser) scan() (tok Token, lit string) {
|
||||
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
|
||||
@@ -210,7 +210,7 @@ func (p *Parser) scan() (tok Token, lit string) {
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Parser) scanIgnoreWhitespace() (tok Token, lit string) {
|
||||
func (p *Parser) scanIgnoreWhitespace() (tok token, lit string) {
|
||||
tok, lit = p.scan()
|
||||
if tok == WS {
|
||||
tok, lit = p.scan()
|
||||
@@ -319,6 +319,10 @@ func (p *Parser) parseLine() (*Instruction, error) {
|
||||
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
|
||||
@@ -338,9 +342,9 @@ func (p *Parser) Parse() (Instructions, error) {
|
||||
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
|
||||
accounts[idxTokenIDToString(instruction.From, instruction.TokenID)] = true
|
||||
if instruction.Type == common.TxTypeTransfer { // type: Transfer
|
||||
accounts[instruction.To] = true
|
||||
accounts[idxTokenIDToString(instruction.To, instruction.TokenID)] = true
|
||||
}
|
||||
tokenids[instruction.TokenID] = true
|
||||
i++
|
||||
|
||||
@@ -35,7 +35,8 @@ func TestParse(t *testing.T) {
|
||||
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, 5, len(instructions.Accounts))
|
||||
fmt.Println(instructions.Accounts)
|
||||
assert.Equal(t, 3, len(instructions.TokenIDs))
|
||||
|
||||
if debug {
|
||||
|
||||
25
test/txs.go
25
test/txs.go
@@ -17,7 +17,7 @@ type Account struct {
|
||||
BJJ *babyjub.PrivateKey
|
||||
Addr ethCommon.Address
|
||||
Idx common.Idx
|
||||
Nonce uint64
|
||||
Nonce common.Nonce
|
||||
}
|
||||
|
||||
// GenerateKeys generates BabyJubJub & Address keys for the given list of
|
||||
@@ -66,40 +66,37 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([]*common.L1Tx, [
|
||||
case common.TxTypeCreateAccountDeposit:
|
||||
tx := common.L1Tx{
|
||||
// TxID
|
||||
FromEthAddr: accounts[inst.From].Addr,
|
||||
FromBJJ: accounts[inst.From].BJJ.Public(),
|
||||
FromEthAddr: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Addr,
|
||||
FromBJJ: accounts[idxTokenIDToString(inst.From, inst.TokenID)].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)
|
||||
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)
|
||||
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(),
|
||||
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[inst.From].Nonce,
|
||||
Nonce: accounts[idxTokenIDToString(inst.From, inst.TokenID)].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++
|
||||
accounts[idxTokenIDToString(inst.From, inst.TokenID)].Nonce++
|
||||
l2txs = append(l2txs, &tx)
|
||||
default:
|
||||
continue
|
||||
|
||||
@@ -17,6 +17,7 @@ func TestGenerateTestL2Txs(t *testing.T) {
|
||||
A-B (1): 6 1
|
||||
B-C (1): 3 1
|
||||
C-A (1): 3 1
|
||||
A-B (1): 1 1
|
||||
A-B (2): 15 1
|
||||
User0 (1): 20
|
||||
User1 (3) : 20
|
||||
@@ -29,22 +30,22 @@ func TestGenerateTestL2Txs(t *testing.T) {
|
||||
|
||||
l1txs, l2txs := GenerateTestTxs(t, instructions)
|
||||
require.Equal(t, 5, len(l1txs))
|
||||
require.Equal(t, 6, len(l2txs))
|
||||
require.Equal(t, 7, 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())
|
||||
assert.Equal(t, "323ff10c28df37ecb787fe216e111db64aa7cfa2c517509fe0057ff08a10b30c", l1txs[1].FromBJJ.String())
|
||||
assert.Equal(t, "f3587ad5cc7414a47545770b6c75bc71930f63c491eb2294dde8b8a6670b8e96", l1txs[2].FromBJJ.String())
|
||||
assert.Equal(t, "b6856a87832b182e5a9a1e738dbcd1f3c728bbc67ea1010aaff563eb5316131b", 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.Idx(3), l2txs[0].ToIdx)
|
||||
assert.Equal(t, "f3587ad5cc7414a47545770b6c75bc71930f63c491eb2294dde8b8a6670b8e96", l2txs[0].ToBJJ.String())
|
||||
assert.Equal(t, "0x6813Eb9362372EEF6200f3b1dbC3f819671cBA69", l2txs[0].ToEthAddr.Hex())
|
||||
assert.Equal(t, common.Nonce(0), l2txs[0].Nonce)
|
||||
assert.Equal(t, common.Nonce(1), l2txs[3].Nonce)
|
||||
assert.Equal(t, common.FeeSelector(1), l2txs[0].Fee)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user