statedb

package
v1.5.0-rc1 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// TypeSynchronizer defines a StateDB used by the Synchronizer, that
	// generates the ExitTree when processing the txs
	TypeSynchronizer = "synchronizer"
	// TypeTxSelector defines a StateDB used by the TxSelector, without
	// computing ExitTree neither the ZKInputs
	TypeTxSelector = "txselector"
	// TypeBatchBuilder defines a StateDB used by the BatchBuilder, that
	// generates the ExitTree and the ZKInput when processing the txs
	TypeBatchBuilder = "batchbuilder"
	// MaxNLevels is the maximum value of NLevels for the merkle tree,
	// which comes from the fact that AccountIdx has 48 bits.
	MaxNLevels = 48
)

Variables

View Source
var (
	// ErrStateDBWithoutMT is used when a method that requires a MerkleTree
	// is called in a StateDB that does not have a MerkleTree defined
	ErrStateDBWithoutMT = errors.New(
		"Can not call method to use MerkleTree in a StateDB without MerkleTree")

	// ErrAccountAlreadyExists is used when CreateAccount is called and the
	// Account already exists
	ErrAccountAlreadyExists = errors.New("Can not CreateAccount because Account already exists")

	// ErrIdxNotFound is used when trying to get the Idx from EthAddr or
	// EthAddr&ToBJJ
	ErrIdxNotFound = errors.New("Idx can not be found")
	// ErrGetIdxNoCase is used when trying to get the Idx from EthAddr &
	// BJJ with not compatible combination
	ErrGetIdxNoCase = errors.New(
		"Can not get Idx due unexpected combination of ethereum Address & BabyJubJub PublicKey")

	// PrefixKeyIdx is the key prefix for idx in the db
	PrefixKeyIdx = []byte("i:")
	// PrefixKeyAccHash is the key prefix for account hash in the db
	PrefixKeyAccHash = []byte("h:")
	// PrefixKeyMT is the key prefix for merkle tree in the db
	PrefixKeyMT = []byte("m:")
	// PrefixKeyAddr is the key prefix for address in the db
	PrefixKeyAddr = []byte("a:")
	// PrefixKeyAddrBJJ is the key prefix for address-babyjubjub in the db
	PrefixKeyAddrBJJ = []byte("ab:")
)

Functions

func CreateAccountInTreeDB

func CreateAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common.Idx,
	account *common.Account) (*merkletree.CircomProcessorProof, error)

CreateAccountInTreeDB is abstracted from StateDB to be used from StateDB and from ExitTree. Creates a new Account in the StateDB for the given Idx. If StateDB.MT==nil, MerkleTree is not affected, otherwise updates the MerkleTree, returning a CircomProcessorProof.

func GetAccountInTreeDB

func GetAccountInTreeDB(sto db.Storage, idx common.Idx) (*common.Account, error)

GetAccountInTreeDB is abstracted from StateDB to be used from StateDB and from ExitTree. GetAccount returns the account for the given Idx

func UpdateAccountInTreeDB

func UpdateAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common.Idx,
	account *common.Account) (*merkletree.CircomProcessorProof, error)

UpdateAccountInTreeDB is abstracted from StateDB to be used from StateDB and from ExitTree. Updates the Account in the StateDB for the given Idx. If StateDB.mt==nil, MerkleTree is not affected, otherwise updates the MerkleTree, returning a CircomProcessorProof.

Types

type Config

type Config struct {
	// Path where the checkpoints will be stored
	Path string
	// Keep is the number of old checkpoints to keep.  If 0, all
	// checkpoints are kept.
	Keep int
	// NoLast skips having an opened DB with a checkpoint to the last
	// batchNum for thread-safe reads.
	NoLast bool
	// Type of StateDB (
	Type TypeStateDB
	// NLevels is the number of merkle tree levels in case the Type uses a
	// merkle tree.  If the Type doesn't use a merkle tree, NLevels should
	// be 0.
	NLevels int
	// contains filtered or unexported fields
}

Config of the StateDB

type Last

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

Last offers a subset of view methods of the StateDB that can be called via the LastRead method of StateDB in a thread-safe manner to obtain a consistent view to the last batch of the StateDB.

func (*Last) DB

func (s *Last) DB() db.Storage

DB returns the underlying storage of Last

func (*Last) GetAccount

func (s *Last) GetAccount(idx common.Idx) (*common.Account, error)

GetAccount returns the account for the given Idx

func (*Last) GetAccounts

func (s *Last) GetAccounts() ([]common.Account, error)

GetAccounts returns all the accounts in the db. Use for debugging pruposes only.

func (*Last) GetCurrentBatch

func (s *Last) GetCurrentBatch() (common.BatchNum, error)

GetCurrentBatch returns the current BatchNum stored in Last.db

type LocalStateDB

type LocalStateDB struct {
	*StateDB
	// contains filtered or unexported fields
}

LocalStateDB represents the local StateDB which allows to make copies from the synchronizer StateDB, and is used by the tx-selector and the batch-builder. LocalStateDB is an in-memory storage.

func NewLocalStateDB

func NewLocalStateDB(cfg Config, synchronizerDB *StateDB) (*LocalStateDB, error)

NewLocalStateDB returns a new LocalStateDB connected to the given synchronizerDB. Checkpoints older than the value defined by `keep` will be deleted.

func (*LocalStateDB) CheckpointExists

func (l *LocalStateDB) CheckpointExists(batchNum common.BatchNum) (bool, error)

CheckpointExists returns true if the checkpoint exists

func (*LocalStateDB) Reset

func (l *LocalStateDB) Reset(batchNum common.BatchNum, fromSynchronizer bool) error

Reset performs a reset in the LocalStateDB. 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.

type StateDB

type StateDB struct {
	MT *merkletree.MerkleTree
	// contains filtered or unexported fields
}

StateDB represents the StateDB object

func NewStateDB

func NewStateDB(cfg Config) (*StateDB, error)

NewStateDB creates a new StateDB, allowing to use an in-memory or in-disk storage. Checkpoints older than the value defined by `keep` will be deleted. func NewStateDB(pathDB string, keep int, typ TypeStateDB, nLevels int) (*StateDB, error) {

func (*StateDB) Close

func (s *StateDB) Close()

Close the StateDB

func (*StateDB) CreateAccount

func (s *StateDB) CreateAccount(idx common.Idx, account *common.Account) (
	*merkletree.CircomProcessorProof, error)

CreateAccount creates a new Account in the StateDB for the given Idx. If StateDB.MT==nil, MerkleTree is not affected, otherwise updates the MerkleTree, returning a CircomProcessorProof.

func (*StateDB) CurrentBatch

func (s *StateDB) CurrentBatch() common.BatchNum

CurrentBatch returns the current in-memory CurrentBatch of the StateDB.db

func (*StateDB) CurrentIdx

func (s *StateDB) CurrentIdx() common.Idx

CurrentIdx returns the current in-memory CurrentIdx of the StateDB.db

func (*StateDB) DeleteOldCheckpoints added in v1.1.0

func (s *StateDB) DeleteOldCheckpoints() error

DeleteOldCheckpoints deletes old checkpoints when there are more than `cfg.keep` checkpoints

func (*StateDB) GetAccount

func (s *StateDB) GetAccount(idx common.Idx) (*common.Account, error)

GetAccount returns the account for the given Idx

func (*StateDB) GetCurrentIdx

func (s *StateDB) GetCurrentIdx() (common.Idx, error)

GetCurrentIdx returns the stored Idx from the localStateDB, which is the last Idx used for an Account in the localStateDB.

func (*StateDB) GetIdxByEthAddr

func (s *StateDB) GetIdxByEthAddr(addr ethCommon.Address, tokenID common.TokenID) (common.Idx,
	error)

GetIdxByEthAddr returns the smallest Idx in the StateDB for the given Ethereum Address. Will return common.Idx(0) and error in case that Idx is not found in the StateDB.

func (*StateDB) GetIdxByEthAddrBJJ

func (s *StateDB) GetIdxByEthAddrBJJ(addr ethCommon.Address, pk babyjub.PublicKeyComp,
	tokenID common.TokenID) (common.Idx, error)

GetIdxByEthAddrBJJ returns the smallest Idx in the StateDB for the given Ethereum Address AND the given BabyJubJub PublicKey. If `addr` is the zero address, it's ignored in the query. If `pk` is nil, it's ignored in the query. Will return common.Idx(0) and error in case that Idx is not found in the StateDB.

func (*StateDB) GetTokenIDsFromIdxs

func (s *StateDB) GetTokenIDsFromIdxs(idxs []common.Idx) (map[common.TokenID]common.Idx, error)

GetTokenIDsFromIdxs returns a map containing the common.TokenID with its respective common.Idx for a given slice of common.Idx

func (*StateDB) LastGetAccount

func (s *StateDB) LastGetAccount(idx common.Idx) (*common.Account, error)

LastGetAccount is a thread-safe method to query an account in the last checkpoint of the StateDB.

func (*StateDB) LastGetCurrentBatch

func (s *StateDB) LastGetCurrentBatch() (common.BatchNum, error)

LastGetCurrentBatch is a thread-safe method to get the current BatchNum in the last checkpoint of the StateDB.

func (*StateDB) LastMTGetRoot

func (s *StateDB) LastMTGetRoot() (*big.Int, error)

LastMTGetRoot returns the root of the underlying Merkle Tree in the last checkpoint of the StateDB.

func (*StateDB) LastRead

func (s *StateDB) LastRead(fn func(sdbLast *Last) error) error

LastRead is a thread-safe method to query the last checkpoint of the StateDB via the Last type methods

func (*StateDB) MTGetProof

func (s *StateDB) MTGetProof(idx common.Idx) (*merkletree.CircomVerifierProof, error)

MTGetProof returns the CircomVerifierProof for a given Idx

func (*StateDB) MakeCheckpoint

func (s *StateDB) MakeCheckpoint() error

MakeCheckpoint does a checkpoint at the given batchNum in the defined path. Internally this advances & stores the current BatchNum, and then stores a Checkpoint of the current state of the StateDB.

func (*StateDB) Reset

func (s *StateDB) Reset(batchNum common.BatchNum) error

Reset resets the StateDB to the checkpoint at the given batchNum. Reset does not delete the checkpoints between old current and the new current, those checkpoints will remain in the storage, and eventually will be deleted when MakeCheckpoint overwrites them.

func (*StateDB) SetCurrentIdx

func (s *StateDB) SetCurrentIdx(idx common.Idx) error

SetCurrentIdx stores Idx in the StateDB

func (*StateDB) TestGetAccounts

func (s *StateDB) TestGetAccounts() ([]common.Account, error)

TestGetAccounts returns all the accounts in the db. Use only in tests. Outside tests getting all the accounts is discouraged because it's an expensive operation, but if you must do it, use `LastRead()` method to get a thread-safe and consistent view of the stateDB.

func (*StateDB) Type

func (s *StateDB) Type() TypeStateDB

Type returns the StateDB configured Type

func (*StateDB) UpdateAccount

func (s *StateDB) UpdateAccount(idx common.Idx, account *common.Account) (
	*merkletree.CircomProcessorProof, error)

UpdateAccount updates the Account in the StateDB for the given Idx. If StateDB.mt==nil, MerkleTree is not affected, otherwise updates the MerkleTree, returning a CircomProcessorProof.

type TypeStateDB

type TypeStateDB string

TypeStateDB determines the type of StateDB

Jump to

Keyboard shortcuts

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