l2db

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 l2db is responsible for storing and retrieving the data received by the coordinator through the api. Note that this data will be different for each coordinator in the network, as this represents the L2 information.

The data managed by this package is fundamentally PoolL2Tx and AccountCreationAuth. All this data come from the API sent by clients and is used by the txselector to decide which transactions are selected to forge a batch.

Some of the database tooling used in this package such as meddler and migration tools is explained in the db package.

This package is spitted in different files following these ideas: - l2db.go: constructor and functions used by packages other than the api. - apiqueries.go: functions used by the API, the queries implemented in this functions use a semaphore to restrict the maximum concurrent connections to the database. - views.go: structs used to retrieve/store data from/to the database. When possible, the common structs are used, however most of the time there is no 1:1 relation between the struct fields and the tables of the schema, especially when joining tables. In some cases, some of the structs defined in this file also include custom Marshallers to easily match the expected api formats.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccountCreationAuthAPI

type AccountCreationAuthAPI struct {
	EthAddr   apitypes.HezEthAddr   `json:"hezEthereumAddress" meddler:"eth_addr" `
	BJJ       apitypes.HezBJJ       `json:"bjj"                meddler:"bjj" `
	Signature apitypes.EthSignature `json:"signature"          meddler:"signature" `
	Timestamp time.Time             `json:"timestamp"          meddler:"timestamp,utctime"`
}

AccountCreationAuthAPI represents an account creation auth in the expected format by the API

type GetPoolTxsAPIRequest

type GetPoolTxsAPIRequest struct {
	EthAddr     *ethCommon.Address
	FromEthAddr *ethCommon.Address
	ToEthAddr   *ethCommon.Address
	Bjj         *babyjub.PublicKeyComp
	FromBjj     *babyjub.PublicKeyComp
	ToBjj       *babyjub.PublicKeyComp
	TxType      *common.TxType
	TokenID     *common.TokenID
	Idx         *common.Idx
	FromIdx     *common.Idx
	ToIdx       *common.Idx
	State       *common.PoolL2TxState

	FromItem *uint
	Limit    *uint
	Order    string
}

GetPoolTxsAPIRequest is an API request struct for getting txs from the pool

type L2DB

type L2DB struct {
	// contains filtered or unexported fields
}

L2DB stores L2 txs and authorization registers received by the coordinator and keeps them until they are no longer relevant due to them being forged or invalid after a safety period

func NewL2DB

func NewL2DB(
	dbRead, dbWrite *sqlx.DB,
	safetyPeriod common.BatchNum,
	maxTxs uint32,
	minFeeUSD float64,
	maxFeeUSD float64,
	TTL time.Duration,
	apiConnCon *db.APIConnectionController,
) *L2DB

NewL2DB creates a L2DB. To create it, it's needed db connection, safety period expressed in batches, maxTxs that the DB should have and TTL (time to live) for pending txs.

func (*L2DB) AddAccountCreationAuth

func (l2db *L2DB) AddAccountCreationAuth(auth *common.AccountCreationAuth) error

AddAccountCreationAuth inserts an account creation authorization into the DB

func (*L2DB) AddAccountCreationAuthAPI

func (l2db *L2DB) AddAccountCreationAuthAPI(auth *common.AccountCreationAuth) error

AddAccountCreationAuthAPI inserts an account creation authorization into the DB

func (*L2DB) AddAtomicTxsAPI

func (l2db *L2DB) AddAtomicTxsAPI(txs []common.PoolL2Tx) error

AddAtomicTxsAPI inserts transactions into the pool if minFeeUSD <= total fee in USD <= maxFeeUSD. It's assumed that the given txs conform a single atomic group and AtomicGroupID will be set for all the txs awith value last AtomigGroupID in the DB +1

func (*L2DB) AddManyAccountCreationAuth

func (l2db *L2DB) AddManyAccountCreationAuth(auths []common.AccountCreationAuth) error

AddManyAccountCreationAuth inserts a batch of accounts creation authorization if not exist into the DB

func (*L2DB) AddTxAPI

func (l2db *L2DB) AddTxAPI(tx *common.PoolL2Tx) error

AddTxAPI inserts a tx to the pool

func (*L2DB) AddTxTest

func (l2db *L2DB) AddTxTest(tx *common.PoolL2Tx) error

AddTxTest inserts a tx into the L2DB, without security checks. This is useful for test purposes,

func (*L2DB) DB

func (l2db *L2DB) DB() *sqlx.DB

DB returns a pointer to the L2DB.db. This method should be used only for internal testing purposes.

func (*L2DB) DoneForging

func (l2db *L2DB) DoneForging(txIDs []common.TxID, batchNum common.BatchNum) error

DoneForging updates the state of the transactions that have been forged so the state of the txs referenced by txIDs will be changed from Forging -> Forged

func (*L2DB) GetAccountCreationAuth

func (l2db *L2DB) GetAccountCreationAuth(addr ethCommon.Address) (*common.AccountCreationAuth, error)

GetAccountCreationAuth returns an account creation authorization from the DB

func (*L2DB) GetAccountCreationAuthAPI

func (l2db *L2DB) GetAccountCreationAuthAPI(addr ethCommon.Address) (*AccountCreationAuthAPI, error)

GetAccountCreationAuthAPI returns an account creation authorization from the DB

func (*L2DB) GetPendingTxs

func (l2db *L2DB) GetPendingTxs() ([]common.PoolL2Tx, error)

GetPendingTxs return all the pending txs of the L2DB, that have a non NULL AbsoluteFee

func (*L2DB) GetPendingUniqueFromIdxs

func (l2db *L2DB) GetPendingUniqueFromIdxs() ([]common.Idx, error)

GetPendingUniqueFromIdxs returns from all the pending transactions, the set of unique FromIdx

func (*L2DB) GetPoolTxsAPI

func (l2db *L2DB) GetPoolTxsAPI(request GetPoolTxsAPIRequest) ([]PoolTxAPI, uint64, error)

GetPoolTxsAPI return Txs from the pool

func (*L2DB) GetPoolTxsByAtomicGroupIDAPI

func (l2db *L2DB) GetPoolTxsByAtomicGroupIDAPI(atomicGroupID common.AtomicGroupID) ([]PoolTxAPI, error)

GetPoolTxsByAtomicGroupIDAPI return Txs from the pool that belong to the given atomicGroupID

func (*L2DB) GetTx

func (l2db *L2DB) GetTx(txID common.TxID) (*common.PoolL2Tx, error)

GetTx return the specified Tx in common.PoolL2Tx format

func (*L2DB) GetTxAPI

func (l2db *L2DB) GetTxAPI(txID common.TxID) (*PoolTxAPI, error)

GetTxAPI return the specified Tx in PoolTxAPI format

func (*L2DB) InvalidateOldNonces

func (l2db *L2DB) InvalidateOldNonces(updatedAccounts []common.IdxNonce, batchNum common.BatchNum) (err error)

InvalidateOldNonces invalidate txs with nonces that are smaller or equal than their respective accounts nonces. The state of the affected txs will be changed from Pending to Invalid

func (*L2DB) InvalidateTxs

func (l2db *L2DB) InvalidateTxs(txIDs []common.TxID, batchNum common.BatchNum) error

InvalidateTxs updates the state of the transactions that are invalid. The state of the txs referenced by txIDs will be changed from * -> Invalid

func (*L2DB) MinFeeUSD

func (l2db *L2DB) MinFeeUSD() float64

MinFeeUSD returns the minimum fee in USD that is required to accept txs into the pool

func (*L2DB) Purge

func (l2db *L2DB) Purge(currentBatchNum common.BatchNum) (err error)

Purge deletes transactions that have been forged or marked as invalid for longer than the safety period it also deletes pending txs that have been in the L2DB for longer than the ttl if maxTxs has been exceeded

func (*L2DB) PurgeByExternalDelete

func (l2db *L2DB) PurgeByExternalDelete() error

PurgeByExternalDelete deletes all pending transactions marked with true in the `external_delete` column. An external process can set this column to true to instruct the coordinator to delete the tx when possible.

func (*L2DB) Reorg

func (l2db *L2DB) Reorg(lastValidBatch common.BatchNum) error

Reorg updates the state of txs that were updated in a batch that has been discarted due to a blockchain reorg. The state of the affected txs can change form Forged -> Pending or from Invalid -> Pending

func (*L2DB) StartForging

func (l2db *L2DB) StartForging(txIDs []common.TxID, batchNum common.BatchNum) error

StartForging updates the state of the transactions that will begin the forging process. The state of the txs referenced by txIDs will be changed from Pending -> Forging

func (*L2DB) UpdateTxsInfo

func (l2db *L2DB) UpdateTxsInfo(txs []common.PoolL2Tx, batchNum common.BatchNum) error

UpdateTxsInfo updates the parameter Info of the pool transactions

type PoolTxAPI

type PoolTxAPI struct {
	ItemID               uint64                `meddler:"item_id"`
	TxID                 common.TxID           `meddler:"tx_id"`
	FromIdx              apitypes.HezIdx       `meddler:"from_idx"`
	EffectiveFromEthAddr *apitypes.HezEthAddr  `meddler:"effective_from_eth_addr"`
	EffectiveFromBJJ     *apitypes.HezBJJ      `meddler:"effective_from_bjj"`
	ToIdx                *apitypes.HezIdx      `meddler:"to_idx"`
	EffectiveToEthAddr   *apitypes.HezEthAddr  `meddler:"effective_to_eth_addr"`
	EffectiveToBJJ       *apitypes.HezBJJ      `meddler:"effective_to_bjj"`
	Amount               apitypes.BigIntStr    `meddler:"amount"`
	Fee                  common.FeeSelector    `meddler:"fee"`
	Nonce                common.Nonce          `meddler:"nonce"`
	State                common.PoolL2TxState  `meddler:"state"`
	MaxNumBatch          uint32                `meddler:"max_num_batch,zeroisnull"`
	Info                 *string               `meddler:"info"`
	ErrorCode            *int                  `meddler:"error_code"`
	ErrorType            *string               `meddler:"error_type"`
	Signature            babyjub.SignatureComp `meddler:"signature"`
	RqFromIdx            *apitypes.HezIdx      `meddler:"rq_from_idx"`
	RqToIdx              *apitypes.HezIdx      `meddler:"rq_to_idx"`
	RqToEthAddr          *apitypes.HezEthAddr  `meddler:"rq_to_eth_addr"`
	RqToBJJ              *apitypes.HezBJJ      `meddler:"rq_to_bjj"`
	RqTokenID            *common.TokenID       `meddler:"rq_token_id"`
	RqAmount             *apitypes.BigIntStr   `meddler:"rq_amount"`
	RqFee                *common.FeeSelector   `meddler:"rq_fee"`
	RqNonce              *common.Nonce         `meddler:"rq_nonce"`
	Type                 common.TxType         `meddler:"tx_type"`
	// Extra read fileds
	BatchNum         *common.BatchNum  `meddler:"batch_num"`
	Timestamp        time.Time         `meddler:"timestamp,utctime"`
	TotalItems       uint64            `meddler:"total_items"`
	TokenID          common.TokenID    `meddler:"token_id"`
	TokenItemID      uint64            `meddler:"token_item_id"`
	TokenEthBlockNum int64             `meddler:"eth_block_num"`
	TokenEthAddr     ethCommon.Address `meddler:"eth_addr"`
	TokenName        string            `meddler:"name"`
	TokenSymbol      string            `meddler:"symbol"`
	TokenDecimals    uint64            `meddler:"decimals"`
	TokenUSD         *float64          `meddler:"usd"`
	TokenUSDUpdate   *time.Time        `meddler:"usd_update"`
}

PoolTxAPI represents a L2 Tx pool with extra metadata used by the API

func (PoolTxAPI) MarshalJSON

func (tx PoolTxAPI) MarshalJSON() ([]byte, error)

MarshalJSON is used to neast some of the fields of PoolTxAPI without the need of auxiliar structs

Jump to

Keyboard shortcuts

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