txprocessor

package
v2.0.0-rc3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2021 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package txprocessor is the module that takes the transactions from the input and processes them, updating the Balances and Nonces of the Accounts in the StateDB.

It's a package used by 3 other different packages, and its behaviour will differ depending on the Type of the StateDB of the TxProcessor:

- TypeSynchronizer:

  • The StateDB contains the full State MerkleTree, where the leafs are the accounts
  • Updates the StateDB and as output returns: ExitInfos, CreatedAccounts, CoordinatorIdxsMap, CollectedFees, UpdatedAccounts
  • Internally computes the ExitTree

- TypeTxSelector:

  • The StateDB contains only the Accounts, which are the equivalent to only the leafs of the State MerkleTree
  • Updates the Accounts from the StateDB

- TypeBatchBuilder:

  • The StateDB contains the full State MerkleTree, where the leafs are the accounts
  • Updates the StateDB. As output returns: ZKInputs, CoordinatorIdxsMap
  • Internally computes the ZKInputs

Packages dependency overview:

Outputs: + ExitInfos              +                  +                       +
	 | CreatedAccounts        |                  |                       |
	 | CoordinatorIdxsMap     |                  |    ZKInputs           |
	 | CollectedFees          |                  |    CoordinatorIdxsMap |
	 | UpdatedAccounts        |                  |                       |
	 +------------------------+----------------+ +-----------------------+

	    +------------+           +----------+             +------------+
	    |Synchronizer|           |TxSelector|             |BatchBuilder|
	    +-----+------+           +-----+----+             +-----+------+
		  |                        |                        |
		  v                        v                        v
	     TxProcessor              TxProcessor              TxProcessor
		  +                        +                        +
		  |                        |                        |
	     +----+----+                   v                   +----+----+
	     |         |                StateDB                |         |
	     v         v                   +                   v         v
	  StateDB  ExitTree                |                StateDB  ExitTree
	     +                        +----+----+              +
	     |                        |         |              |
	+----+----+                   v         v         +----+----+
	|         |                 KVDB  AccountsDB      |         |
	v         v                                       v         v
      KVDB   MerkleTree                                 KVDB   MerkleTree

The structure of the TxProcessor can be understand as:

  • StateDB: where the Rollup state is stored. It contains the Accounts & MerkleTree.
  • Config: parameters of the configuration of the circuit
  • ZKInputs: computed inputs for the circuit, depends on the Config parameters
  • ExitTree: only in the TypeSynchronizer & TypeBatchBuilder, contains the MerkleTree with the processed Exits of the Batch

The main exposed method of the TxProcessor is `ProcessTxs`, which as general lines does:

  • if type==(Synchronizer || BatchBuilder), creates an ephemeral ExitTree
  • processes:
  • L1UserTxs --> for each tx calls ProcessL1Tx()
  • L1CoordinatorTxs --> for each tx calls ProcessL1Tx()
  • L2Txs --> for each tx calls ProcessL2Tx()
  • internally, it computes the Fees
  • each transaction processment includes:
  • updating the Account Balances (for sender & receiver, and in case that there is fee, updates the fee receiver account)
  • which includes updating the State MerkleTree (except for the type==TxSelector, which only updates the Accounts (leafs))
  • in case of Synchronizer & BatchBuilder, updates the ExitTree for the txs of type Exit (L1 & L2)
  • in case of BatchBuilder, computes the ZKInputs while processing the txs
  • if type==Synchronizer, once all the txs are processed, for each Exit it generates the ExitInfo data

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidRqOffset RqOffset must be a value between 0 and 7 (both included)
	ErrInvalidRqOffset = "RqOffset must be a value between 0 and 7 (both included)"
)

Functions

func BJJCompressedTo256BigInts

func BJJCompressedTo256BigInts(pkComp babyjub.PublicKeyComp) [256]*big.Int

BJJCompressedTo256BigInts returns a [256]*big.Int array with the bit representation of the babyjub.PublicKeyComp

Types

type Config

type Config struct {
	NLevels uint32
	// MaxFeeTx is the maximum number of coordinator accounts that can receive fees
	MaxFeeTx uint32
	MaxTx    uint32
	MaxL1Tx  uint32
	// ChainID of the blockchain
	ChainID uint16
}

Config contains the TxProcessor configuration parameters

type ProcessTxOutput

type ProcessTxOutput struct {
	ZKInputs           *common.ZKInputs
	ExitInfos          []common.ExitInfo
	CreatedAccounts    []common.Account
	CoordinatorIdxsMap map[common.TokenID]common.Idx
	CollectedFees      map[common.TokenID]*big.Int
	// UpdatedAccounts returns the current state of each account
	// created/updated by any of the processed transactions.
	UpdatedAccounts map[common.Idx]*common.Account
}

ProcessTxOutput contains the output of the ProcessTxs method

type TxProcessor

type TxProcessor struct {

	// AccumulatedFees contains the accumulated fees for each token (Coord
	// Idx) in the processed batch
	AccumulatedFees map[common.Idx]*big.Int
	// contains filtered or unexported fields
}

TxProcessor represents the TxProcessor object

func NewTxProcessor

func NewTxProcessor(state *statedb.StateDB, config Config) *TxProcessor

NewTxProcessor returns a new TxProcessor with the given *StateDB & Config

func (*TxProcessor) AccumulatedCoordFees

func (txProcessor *TxProcessor) AccumulatedCoordFees() map[common.Idx]*big.Int

AccumulatedCoordFees returns the accumulated fees for each token (coordinator idx) in the processed batch

func (*TxProcessor) CheckEnoughBalance

func (txProcessor *TxProcessor) CheckEnoughBalance(tx common.PoolL2Tx) (bool, *big.Int, *big.Int)

CheckEnoughBalance returns true if the sender of the transaction has enough balance in the account to send the Amount+Fee, and also returns the account Balance and the Fee+Amount (which is used to give information about why the transaction is not selected in case that this method returns false.

func (*TxProcessor) ProcessL1Tx

func (txProcessor *TxProcessor) ProcessL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx) (*common.Idx,
	*common.Account, bool, *common.Account, error)

ProcessL1Tx process the given L1Tx applying the needed updates to the StateDB depending on the transaction Type. It returns the 3 parameters related to the Exit (in case of): Idx, ExitAccount, boolean determining if the Exit created a new Leaf in the ExitTree. And another *common.Account parameter which contains the created account in case that has been a new created account and that the StateDB is of type TypeSynchronizer.

func (*TxProcessor) ProcessL2Tx

func (txProcessor *TxProcessor) ProcessL2Tx(coordIdxsMap map[common.TokenID]common.Idx,
	collectedFees map[common.TokenID]*big.Int, exitTree *merkletree.MerkleTree,
	tx *common.PoolL2Tx) (*common.Idx, *common.Account, bool, error)

ProcessL2Tx process the given L2Tx applying the needed updates to the StateDB depending on the transaction Type. It returns the 3 parameters related to the Exit (in case of): Idx, ExitAccount, boolean determining if the Exit created a new Leaf in the ExitTree.

func (*TxProcessor) ProcessTxs

func (txProcessor *TxProcessor) ProcessTxs(coordIdxs []common.Idx, l1usertxs, l1coordinatortxs []common.L1Tx,
	l2txs []common.PoolL2Tx) (ptOut *ProcessTxOutput, err error)

ProcessTxs process the given L1Txs & L2Txs applying the needed updates to the StateDB depending on the transaction Type. If StateDB type==TypeBatchBuilder, returns the common.ZKInputs to generate the SnarkProof later used by the BatchBuilder. If StateDB type==TypeSynchronizer, assumes that the call is done from the Synchronizer, returns common.ExitTreeLeaf that is later used by the Synchronizer to update the HistoryDB, and adds Nonce & TokenID to the L2Txs. And if TypeSynchronizer returns an array of common.Account with all the created accounts.

func (*TxProcessor) StateDB

func (txProcessor *TxProcessor) StateDB() *statedb.StateDB

StateDB returns a pointer to the StateDB of the TxProcessor

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL