mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Merge pull request #64 from hermeznetwork/feature/testing-framework
Feature/testing framework language
This commit is contained in:
@@ -1,18 +1,11 @@
|
|||||||
package batchbuilder
|
package batchbuilder
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"math/big"
|
|
||||||
|
|
||||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/hermeznetwork/hermez-node/common"
|
"github.com/hermeznetwork/hermez-node/common"
|
||||||
"github.com/hermeznetwork/hermez-node/db/statedb"
|
"github.com/hermeznetwork/hermez-node/db/statedb"
|
||||||
"github.com/iden3/go-merkletree/db"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// KEYIDX is used as key in the db to store the current Idx
|
|
||||||
var KEYIDX = []byte("idx")
|
|
||||||
|
|
||||||
// ConfigCircuit contains the circuit configuration
|
// ConfigCircuit contains the circuit configuration
|
||||||
type ConfigCircuit struct {
|
type ConfigCircuit struct {
|
||||||
TxsMax uint64
|
TxsMax uint64
|
||||||
@@ -23,8 +16,6 @@ type ConfigCircuit struct {
|
|||||||
// BatchBuilder implements the batch builder type, which contains the
|
// BatchBuilder implements the batch builder type, which contains the
|
||||||
// functionalities
|
// functionalities
|
||||||
type BatchBuilder struct {
|
type BatchBuilder struct {
|
||||||
// idx holds the current Idx that the BatchBuilder is using
|
|
||||||
idx uint64
|
|
||||||
localStateDB *statedb.LocalStateDB
|
localStateDB *statedb.LocalStateDB
|
||||||
configCircuits []ConfigCircuit
|
configCircuits []ConfigCircuit
|
||||||
}
|
}
|
||||||
@@ -56,209 +47,29 @@ func NewBatchBuilder(dbpath string, synchronizerStateDB *statedb.StateDB, config
|
|||||||
// copy of the rollup state from the Synchronizer at that `batchNum`, otherwise
|
// copy of the rollup state from the Synchronizer at that `batchNum`, otherwise
|
||||||
// it can just roll back the internal copy.
|
// it can just roll back the internal copy.
|
||||||
func (bb *BatchBuilder) Reset(batchNum uint64, fromSynchronizer bool) error {
|
func (bb *BatchBuilder) Reset(batchNum uint64, fromSynchronizer bool) error {
|
||||||
if batchNum == 0 {
|
return bb.localStateDB.Reset(batchNum, fromSynchronizer)
|
||||||
bb.idx = 0
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
err := bb.localStateDB.Reset(batchNum, fromSynchronizer)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// idx is obtained from the statedb reset
|
|
||||||
bb.idx, err = bb.getIdx()
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BuildBatch takes the transactions and returns the common.ZKInputs of the next batch
|
// BuildBatch takes the transactions and returns the common.ZKInputs of the next batch
|
||||||
func (bb *BatchBuilder) BuildBatch(configBatch ConfigBatch, l1usertxs, l1coordinatortxs []*common.L1Tx, l2txs []*common.PoolL2Tx, tokenIDs []common.TokenID) (*common.ZKInputs, error) {
|
func (bb *BatchBuilder) BuildBatch(configBatch *ConfigBatch, l1usertxs, l1coordinatortxs []*common.L1Tx, l2txs []*common.PoolL2Tx, tokenIDs []common.TokenID) (*common.ZKInputs, error) {
|
||||||
for _, tx := range l1usertxs {
|
for _, tx := range l1usertxs {
|
||||||
err := bb.processL1Tx(tx)
|
err := bb.localStateDB.ProcessL1Tx(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, tx := range l1coordinatortxs {
|
for _, tx := range l1coordinatortxs {
|
||||||
err := bb.processL1Tx(tx)
|
err := bb.localStateDB.ProcessL1Tx(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, tx := range l2txs {
|
for _, tx := range l2txs {
|
||||||
switch tx.Type {
|
err := bb.localStateDB.ProcessPoolL2Tx(tx)
|
||||||
case common.TxTypeTransfer:
|
|
||||||
// go to the MT account of sender and receiver, and update
|
|
||||||
// balance & nonce
|
|
||||||
err := bb.applyTransfer(tx.Tx())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case common.TxTypeExit:
|
|
||||||
// execute exit flow
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bb *BatchBuilder) 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 := bb.applyTransfer(tx.Tx())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case common.TxTypeCreateAccountDeposit:
|
|
||||||
// add new account to the MT, update balance of the MT account
|
|
||||||
err := bb.applyCreateAccount(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case common.TxTypeDeposit: // TODO check if this type will ever exist, or will be TxTypeDepositAndTransfer with transfer 0 value
|
|
||||||
// update balance of the MT account
|
|
||||||
err := bb.applyDeposit(tx, false)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case common.TxTypeDepositAndTransfer:
|
|
||||||
// update balance in MT account, update balance & nonce of sender
|
|
||||||
// & receiver
|
|
||||||
err := bb.applyDeposit(tx, true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case common.TxTypeCreateAccountDepositAndTransfer:
|
|
||||||
// add new account to the merkletree, update balance in MT account,
|
|
||||||
// update balance & nonce of sender & receiver
|
|
||||||
err := bb.applyCreateAccount(tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = bb.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 (bb *BatchBuilder) applyCreateAccount(tx *common.L1Tx) error {
|
|
||||||
account := &common.Account{
|
|
||||||
TokenID: tx.TokenID,
|
|
||||||
Nonce: 0,
|
|
||||||
Balance: tx.LoadAmount,
|
|
||||||
PublicKey: tx.FromBJJ,
|
|
||||||
EthAddr: tx.FromEthAddr,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := bb.localStateDB.CreateAccount(common.Idx(bb.idx+1), account)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
bb.idx = bb.idx + 1
|
|
||||||
return bb.setIdx(bb.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/DepositAndTransfer
|
|
||||||
func (bb *BatchBuilder) applyDeposit(tx *common.L1Tx, transfer bool) error {
|
|
||||||
// deposit the tx.LoadAmount into the sender account
|
|
||||||
accSender, err := bb.localStateDB.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>DepositAndTransfer
|
|
||||||
if transfer {
|
|
||||||
accReceiver, err := bb.localStateDB.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 = bb.localStateDB.UpdateAccount(tx.ToIdx, accReceiver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// update sender account in localStateDB
|
|
||||||
err = bb.localStateDB.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 (bb *BatchBuilder) applyTransfer(tx *common.Tx) error {
|
|
||||||
// get sender and receiver accounts from localStateDB
|
|
||||||
accSender, err := bb.localStateDB.GetAccount(tx.FromIdx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
accReceiver, err := bb.localStateDB.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 = bb.localStateDB.UpdateAccount(tx.ToIdx, accReceiver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// update sender account in localStateDB
|
|
||||||
err = bb.localStateDB.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 (bb *BatchBuilder) getIdx() (uint64, error) {
|
|
||||||
idxBytes, err := bb.localStateDB.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 (bb *BatchBuilder) setIdx(idx uint64) error {
|
|
||||||
tx, err := bb.localStateDB.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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ func (l *Account) BigInts() ([NLEAFELEMS]*big.Int, error) {
|
|||||||
// HashValue returns the value of the Account, which is the Poseidon hash of its *big.Int representation
|
// HashValue returns the value of the Account, which is the Poseidon hash of its *big.Int representation
|
||||||
func (l *Account) HashValue() (*big.Int, error) {
|
func (l *Account) HashValue() (*big.Int, error) {
|
||||||
b0 := big.NewInt(0)
|
b0 := big.NewInt(0)
|
||||||
toHash := [poseidon.T]*big.Int{b0, b0, b0, b0, b0, b0}
|
toHash := []*big.Int{b0, b0, b0, b0, b0, b0}
|
||||||
lBI, err := l.BigInts()
|
lBI, err := l.BigInts()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ func TestAccountHashValue(t *testing.T) {
|
|||||||
|
|
||||||
v, err := account.HashValue()
|
v, err := account.HashValue()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "6335844662301214382338419199835935731871537354006112711277201708185593574314", v.String())
|
assert.Equal(t, "16085711911723375585301279875451049849443101031421093098714359651259271023730", v.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAccountErrNotInFF(t *testing.T) {
|
func TestAccountErrNotInFF(t *testing.T) {
|
||||||
|
|||||||
10
common/tx.go
10
common/tx.go
@@ -45,10 +45,10 @@ const (
|
|||||||
TxTypeDeposit TxType = "Deposit"
|
TxTypeDeposit TxType = "Deposit"
|
||||||
// TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
|
// TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
|
||||||
TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
|
TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
|
||||||
// TxTypeCreateAccountDepositAndTransfer represents L1->L2 transfer + L2->L2 transfer
|
// TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
|
||||||
TxTypeCreateAccountDepositAndTransfer TxType = "CreateAccountDepositAndTransfer"
|
TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
|
||||||
// TxTypeDepositAndTransfer TBD
|
// TxTypeDepositTransfer TBD
|
||||||
TxTypeDepositAndTransfer TxType = "TxTypeDepositAndTransfer"
|
TxTypeDepositTransfer TxType = "TxTypeDepositTransfer"
|
||||||
// TxTypeForceTransfer TBD
|
// TxTypeForceTransfer TBD
|
||||||
TxTypeForceTransfer TxType = "TxTypeForceTransfer"
|
TxTypeForceTransfer TxType = "TxTypeForceTransfer"
|
||||||
// TxTypeForceExit TBD
|
// TxTypeForceExit TBD
|
||||||
@@ -63,7 +63,7 @@ const (
|
|||||||
type Tx struct {
|
type Tx struct {
|
||||||
TxID TxID
|
TxID TxID
|
||||||
FromIdx Idx // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
|
FromIdx Idx // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
|
||||||
ToIdx Idx // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
|
ToIdx Idx // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositTransfer
|
||||||
TokenID TokenID
|
TokenID TokenID
|
||||||
Amount *big.Int
|
Amount *big.Int
|
||||||
Nonce uint64 // effective 48 bits used
|
Nonce uint64 // effective 48 bits used
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ func (c *Coordinator) forgeSequence() error {
|
|||||||
batchInfo.SetTxsInfo(l1UserTxsExtra, l1OperatorTxs, l2Txs) // TODO feesInfo
|
batchInfo.SetTxsInfo(l1UserTxsExtra, l1OperatorTxs, l2Txs) // TODO feesInfo
|
||||||
|
|
||||||
// 4. Call BatchBuilder with TxSelector output
|
// 4. Call BatchBuilder with TxSelector output
|
||||||
configBatch := batchbuilder.ConfigBatch{
|
configBatch := &batchbuilder.ConfigBatch{
|
||||||
ForgerAddress: c.config.ForgerAddress,
|
ForgerAddress: c.config.ForgerAddress,
|
||||||
}
|
}
|
||||||
zkInputs, err := c.batchBuilder.BuildBatch(configBatch, l1UserTxsExtra, l1OperatorTxs, l2Txs, nil) // TODO []common.TokenID --> feesInfo
|
zkInputs, err := c.batchBuilder.BuildBatch(configBatch, l1UserTxsExtra, l1OperatorTxs, l2Txs, nil) // TODO []common.TokenID --> feesInfo
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ type StateDB struct {
|
|||||||
currentBatch uint64
|
currentBatch uint64
|
||||||
db *pebble.PebbleStorage
|
db *pebble.PebbleStorage
|
||||||
mt *merkletree.MerkleTree
|
mt *merkletree.MerkleTree
|
||||||
|
// idx holds the current Idx that the BatchBuilder is using
|
||||||
|
idx uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStateDB creates a new StateDB, allowing to use an in-memory or in-disk
|
// NewStateDB creates a new StateDB, allowing to use an in-memory or in-disk
|
||||||
@@ -147,6 +149,11 @@ func (s *StateDB) DeleteCheckpoint(batchNum uint64) error {
|
|||||||
// those checkpoints will remain in the storage, and eventually will be
|
// those checkpoints will remain in the storage, and eventually will be
|
||||||
// deleted when MakeCheckpoint overwrites them.
|
// deleted when MakeCheckpoint overwrites them.
|
||||||
func (s *StateDB) Reset(batchNum uint64) error {
|
func (s *StateDB) Reset(batchNum uint64) error {
|
||||||
|
if batchNum == 0 {
|
||||||
|
s.idx = 0
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
checkpointPath := s.path + PATHBATCHNUM + strconv.Itoa(int(batchNum))
|
checkpointPath := s.path + PATHBATCHNUM + strconv.Itoa(int(batchNum))
|
||||||
currentPath := s.path + PATHCURRENT
|
currentPath := s.path + PATHCURRENT
|
||||||
|
|
||||||
@@ -174,7 +181,9 @@ func (s *StateDB) Reset(batchNum uint64) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
// idx is obtained from the statedb reset
|
||||||
|
s.idx, err = s.getIdx()
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAccount returns the account for the given Idx
|
// GetAccount returns the account for the given Idx
|
||||||
@@ -315,6 +324,10 @@ func NewLocalStateDB(path string, synchronizerDB *StateDB, withMT bool, nLevels
|
|||||||
// Reset performs a reset in the LocaStateDB. If fromSynchronizer is true, it
|
// Reset performs a reset in the LocaStateDB. If fromSynchronizer is true, it
|
||||||
// gets the state from LocalStateDB.synchronizerStateDB for the given batchNum. If fromSynchronizer is false, get the state from LocalStateDB checkpoints.
|
// gets the state from LocalStateDB.synchronizerStateDB for the given batchNum. If fromSynchronizer is false, get the state from LocalStateDB checkpoints.
|
||||||
func (l *LocalStateDB) Reset(batchNum uint64, fromSynchronizer bool) error {
|
func (l *LocalStateDB) Reset(batchNum uint64, fromSynchronizer bool) error {
|
||||||
|
if batchNum == 0 {
|
||||||
|
l.idx = 0
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
synchronizerCheckpointPath := l.synchronizerStateDB.path + PATHBATCHNUM + strconv.Itoa(int(batchNum))
|
synchronizerCheckpointPath := l.synchronizerStateDB.path + PATHBATCHNUM + strconv.Itoa(int(batchNum))
|
||||||
checkpointPath := l.path + PATHBATCHNUM + strconv.Itoa(int(batchNum))
|
checkpointPath := l.path + PATHBATCHNUM + strconv.Itoa(int(batchNum))
|
||||||
|
|||||||
195
db/statedb/txprocessors.go
Normal file
195
db/statedb/txprocessors.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
4
go.mod
4
go.mod
@@ -8,8 +8,8 @@ require (
|
|||||||
github.com/go-sql-driver/mysql v1.5.0 // indirect
|
github.com/go-sql-driver/mysql v1.5.0 // indirect
|
||||||
github.com/gobuffalo/packr/v2 v2.8.0
|
github.com/gobuffalo/packr/v2 v2.8.0
|
||||||
github.com/iden3/go-iden3-core v0.0.8
|
github.com/iden3/go-iden3-core v0.0.8
|
||||||
github.com/iden3/go-iden3-crypto v0.0.6-0.20200814110325-70841d78e7d8
|
github.com/iden3/go-iden3-crypto v0.0.6-0.20200819064831-09d161e9f670
|
||||||
github.com/iden3/go-merkletree v0.0.0-20200815144208-1f1bd54b93ae
|
github.com/iden3/go-merkletree v0.0.0-20200819092443-dc656fdd32fc
|
||||||
github.com/jinzhu/gorm v1.9.15
|
github.com/jinzhu/gorm v1.9.15
|
||||||
github.com/jmoiron/sqlx v1.2.0
|
github.com/jmoiron/sqlx v1.2.0
|
||||||
github.com/lib/pq v1.8.0
|
github.com/lib/pq v1.8.0
|
||||||
|
|||||||
6
go.sum
6
go.sum
@@ -274,6 +274,8 @@ github.com/iden3/go-iden3-crypto v0.0.6-0.20200813145745-66519124ca17 h1:aqy++85
|
|||||||
github.com/iden3/go-iden3-crypto v0.0.6-0.20200813145745-66519124ca17/go.mod h1:oBgthFLboAWi9feaBUFy7OxEcyn9vA1khHSL/WwWFyg=
|
github.com/iden3/go-iden3-crypto v0.0.6-0.20200813145745-66519124ca17/go.mod h1:oBgthFLboAWi9feaBUFy7OxEcyn9vA1khHSL/WwWFyg=
|
||||||
github.com/iden3/go-iden3-crypto v0.0.6-0.20200814110325-70841d78e7d8 h1:1QeVvr/DjiBpk8tw3vBfYD+zs0JeYl3fPIUA/foDq3w=
|
github.com/iden3/go-iden3-crypto v0.0.6-0.20200814110325-70841d78e7d8 h1:1QeVvr/DjiBpk8tw3vBfYD+zs0JeYl3fPIUA/foDq3w=
|
||||||
github.com/iden3/go-iden3-crypto v0.0.6-0.20200814110325-70841d78e7d8/go.mod h1:oBgthFLboAWi9feaBUFy7OxEcyn9vA1khHSL/WwWFyg=
|
github.com/iden3/go-iden3-crypto v0.0.6-0.20200814110325-70841d78e7d8/go.mod h1:oBgthFLboAWi9feaBUFy7OxEcyn9vA1khHSL/WwWFyg=
|
||||||
|
github.com/iden3/go-iden3-crypto v0.0.6-0.20200819064831-09d161e9f670 h1:gNBFu/WnRfNn+xywE04fgCWSHlb6wr0nIIll9i4R2fc=
|
||||||
|
github.com/iden3/go-iden3-crypto v0.0.6-0.20200819064831-09d161e9f670/go.mod h1:oBgthFLboAWi9feaBUFy7OxEcyn9vA1khHSL/WwWFyg=
|
||||||
github.com/iden3/go-merkletree v0.0.0-20200723202738-75e24244a1e3 h1:QR6LqG1HqqPE4myiLR73QFIieAfwODG3bqo1juuaqVI=
|
github.com/iden3/go-merkletree v0.0.0-20200723202738-75e24244a1e3 h1:QR6LqG1HqqPE4myiLR73QFIieAfwODG3bqo1juuaqVI=
|
||||||
github.com/iden3/go-merkletree v0.0.0-20200723202738-75e24244a1e3/go.mod h1:Fc49UeywIsj8nUfb5lxBzmWrMeMmqzTJ5F0OcjdiEME=
|
github.com/iden3/go-merkletree v0.0.0-20200723202738-75e24244a1e3/go.mod h1:Fc49UeywIsj8nUfb5lxBzmWrMeMmqzTJ5F0OcjdiEME=
|
||||||
github.com/iden3/go-merkletree v0.0.0-20200806171216-dd600560e44c h1:EzVMSVkwKdfcOR1a+rZe9dfbtAj6KdJnBxOEZ5B+gCQ=
|
github.com/iden3/go-merkletree v0.0.0-20200806171216-dd600560e44c h1:EzVMSVkwKdfcOR1a+rZe9dfbtAj6KdJnBxOEZ5B+gCQ=
|
||||||
@@ -284,6 +286,10 @@ github.com/iden3/go-merkletree v0.0.0-20200815105542-2277604e65dd h1:AkPlODLWkgQ
|
|||||||
github.com/iden3/go-merkletree v0.0.0-20200815105542-2277604e65dd/go.mod h1:/MsQOzDnxK8X/u7XP9ZBoZwZ4gIm9FlwfqckH5dRuTM=
|
github.com/iden3/go-merkletree v0.0.0-20200815105542-2277604e65dd/go.mod h1:/MsQOzDnxK8X/u7XP9ZBoZwZ4gIm9FlwfqckH5dRuTM=
|
||||||
github.com/iden3/go-merkletree v0.0.0-20200815144208-1f1bd54b93ae h1:CC8oKPM+38/Dkq20QymuIjyRo0UFzJZeH5DPbGWURrI=
|
github.com/iden3/go-merkletree v0.0.0-20200815144208-1f1bd54b93ae h1:CC8oKPM+38/Dkq20QymuIjyRo0UFzJZeH5DPbGWURrI=
|
||||||
github.com/iden3/go-merkletree v0.0.0-20200815144208-1f1bd54b93ae/go.mod h1:/MsQOzDnxK8X/u7XP9ZBoZwZ4gIm9FlwfqckH5dRuTM=
|
github.com/iden3/go-merkletree v0.0.0-20200815144208-1f1bd54b93ae/go.mod h1:/MsQOzDnxK8X/u7XP9ZBoZwZ4gIm9FlwfqckH5dRuTM=
|
||||||
|
github.com/iden3/go-merkletree v0.0.0-20200819075941-77df3096f7ea h1:0MtMnN42ULNxOnJYa0FH1qjeSEBPkYVH6ZCs22rz2FU=
|
||||||
|
github.com/iden3/go-merkletree v0.0.0-20200819075941-77df3096f7ea/go.mod h1:MRe6i0mi2oDVUzgBIHsNRE6XAg8EBuqIQZMsd+do+dU=
|
||||||
|
github.com/iden3/go-merkletree v0.0.0-20200819092443-dc656fdd32fc h1:VnRP7JCp5TJHnTC+r8NrXGkRBvp62tRGqeA3FcdEq+Q=
|
||||||
|
github.com/iden3/go-merkletree v0.0.0-20200819092443-dc656fdd32fc/go.mod h1:MRe6i0mi2oDVUzgBIHsNRE6XAg8EBuqIQZMsd+do+dU=
|
||||||
github.com/iden3/go-wasm3 v0.0.1/go.mod h1:j+TcAB94Dfrjlu5kJt83h2OqAU+oyNUTwNZnQyII1sI=
|
github.com/iden3/go-wasm3 v0.0.1/go.mod h1:j+TcAB94Dfrjlu5kJt83h2OqAU+oyNUTwNZnQyII1sI=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||||
github.com/influxdata/influxdb v1.2.3-0.20180221223340-01288bdb0883/go.mod h1:qZna6X/4elxqT3yI9iZYdZrWWdeFOOprn86kgg4+IzY=
|
github.com/influxdata/influxdb v1.2.3-0.20180221223340-01288bdb0883/go.mod h1:qZna6X/4elxqT3yI9iZYdZrWWdeFOOprn86kgg4+IzY=
|
||||||
|
|||||||
356
test/lang.go
Normal file
356
test/lang.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
80
test/lang_test.go
Normal file
80
test/lang_test.go
Normal file
@@ -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())
|
||||||
|
}
|
||||||
111
test/txs.go
Normal file
111
test/txs.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
50
test/txs_test.go
Normal file
50
test/txs_test.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
@@ -12,95 +12,6 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
|
||||||
func initTestDB(l2 *l2db.L2DB, sdb *statedb.StateDB) *mock.MockDB {
|
|
||||||
txs := []common.Tx{
|
|
||||||
{
|
|
||||||
FromIdx: common.Idx(0),
|
|
||||||
ToIdx: common.Idx(1),
|
|
||||||
TokenID: 1,
|
|
||||||
Nonce: 1,
|
|
||||||
AbsoluteFee: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
FromIdx: common.Idx(0),
|
|
||||||
ToIdx: common.Idx(1),
|
|
||||||
TokenID: 1,
|
|
||||||
Nonce: 2,
|
|
||||||
AbsoluteFee: 3,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
FromIdx: common.Idx(0),
|
|
||||||
ToIdx: common.Idx(1),
|
|
||||||
TokenID: 1,
|
|
||||||
Nonce: 4,
|
|
||||||
AbsoluteFee: 6,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
FromIdx: common.Idx(0),
|
|
||||||
ToIdx: common.Idx(1),
|
|
||||||
TokenID: 1,
|
|
||||||
Nonce: 4,
|
|
||||||
AbsoluteFee: 4,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ToIdx: common.Idx(1),
|
|
||||||
FromIdx: common.Idx(0),
|
|
||||||
TokenID: 1,
|
|
||||||
Nonce: 1,
|
|
||||||
AbsoluteFee: 4,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ToIdx: common.Idx(1),
|
|
||||||
FromIdx: common.Idx(0),
|
|
||||||
TokenID: 1,
|
|
||||||
Nonce: 2,
|
|
||||||
AbsoluteFee: 3,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ToIdx: common.Idx(1),
|
|
||||||
FromIdx: common.Idx(0),
|
|
||||||
TokenID: 1,
|
|
||||||
Nonce: 3,
|
|
||||||
AbsoluteFee: 5,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// this tx will not be selected, as the ToEthAddr does not have an account
|
|
||||||
FromIdx: common.Idx(1),
|
|
||||||
ToIdx: common.Idx(2),
|
|
||||||
TokenID: 1,
|
|
||||||
Nonce: 4,
|
|
||||||
AbsoluteFee: 5,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// n := 0
|
|
||||||
nBatch := 0
|
|
||||||
for i := 0; i < len(txs); i++ {
|
|
||||||
// for i := 0; i < nBatch; i++ {
|
|
||||||
// for j := 0; j < len(txs)/nBatch; j++ {
|
|
||||||
// store tx
|
|
||||||
l2db.AddTx(uint64(nBatch), txs[i])
|
|
||||||
|
|
||||||
// store account if not yet
|
|
||||||
accountID := getAccountID(txs[i].FromEthAddr, txs[i].TokenID)
|
|
||||||
if _, ok := m.AccountDB[accountID]; !ok {
|
|
||||||
account := common.Account{
|
|
||||||
// EthAddr: txs[i].FromEthAddr,
|
|
||||||
TokenID: txs[i].TokenID,
|
|
||||||
Nonce: 0,
|
|
||||||
Balance: big.NewInt(0),
|
|
||||||
}
|
|
||||||
m.AccountDB[accountID] = account
|
|
||||||
}
|
|
||||||
// n++
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
func TestGetL2TxSelection(t *testing.T) {
|
func TestGetL2TxSelection(t *testing.T) {
|
||||||
dir, err := ioutil.TempDir("", "tmpdb")
|
dir, err := ioutil.TempDir("", "tmpdb")
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user