Documentation ¶
Index ¶
- Constants
- Variables
- func CreateAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common.Idx, ...) (*merkletree.CircomProcessorProof, error)
- func GetAccountInTreeDB(sto db.Storage, idx common.Idx) (*common.Account, error)
- func UpdateAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common.Idx, ...) (*merkletree.CircomProcessorProof, error)
- type Config
- type Last
- type LocalStateDB
- type StateDB
- func (s *StateDB) Close()
- func (s *StateDB) CreateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error)
- func (s *StateDB) CurrentBatch() common.BatchNum
- func (s *StateDB) CurrentIdx() common.Idx
- func (s *StateDB) DeleteOldCheckpoints() error
- func (s *StateDB) GetAccount(idx common.Idx) (*common.Account, error)
- func (s *StateDB) GetCurrentIdx() (common.Idx, error)
- func (s *StateDB) GetIdxByEthAddr(addr ethCommon.Address, tokenID common.TokenID) (common.Idx, error)
- func (s *StateDB) GetIdxByEthAddrBJJ(addr ethCommon.Address, pk babyjub.PublicKeyComp, tokenID common.TokenID) (common.Idx, error)
- func (s *StateDB) GetTokenIDsFromIdxs(idxs []common.Idx) (map[common.TokenID]common.Idx, error)
- func (s *StateDB) LastGetAccount(idx common.Idx) (*common.Account, error)
- func (s *StateDB) LastGetCurrentBatch() (common.BatchNum, error)
- func (s *StateDB) LastMTGetRoot() (*big.Int, error)
- func (s *StateDB) LastRead(fn func(sdbLast *Last) error) error
- func (s *StateDB) MTGetProof(idx common.Idx) (*merkletree.CircomVerifierProof, error)
- func (s *StateDB) MakeCheckpoint() error
- func (s *StateDB) Reset(batchNum common.BatchNum) error
- func (s *StateDB) SetCurrentIdx(idx common.Idx) error
- func (s *StateDB) TestGetAccounts() ([]common.Account, error)
- func (s *StateDB) Type() TypeStateDB
- func (s *StateDB) UpdateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error)
- type TypeStateDB
Constants ¶
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 ¶
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 ¶
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) GetAccount ¶
GetAccount returns the account for the given Idx
func (*Last) GetAccounts ¶
GetAccounts returns all the accounts in the db. Use for debugging pruposes only.
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 ¶
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) 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 ¶
CurrentBatch returns the current in-memory CurrentBatch of the StateDB.db
func (*StateDB) CurrentIdx ¶
CurrentIdx returns the current in-memory CurrentIdx of the StateDB.db
func (*StateDB) DeleteOldCheckpoints ¶ added in v1.1.0
DeleteOldCheckpoints deletes old checkpoints when there are more than `cfg.keep` checkpoints
func (*StateDB) GetAccount ¶
GetAccount returns the account for the given Idx
func (*StateDB) GetCurrentIdx ¶
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 ¶
GetTokenIDsFromIdxs returns a map containing the common.TokenID with its respective common.Idx for a given slice of common.Idx
func (*StateDB) LastGetAccount ¶
LastGetAccount is a thread-safe method to query an account in the last checkpoint of the StateDB.
func (*StateDB) LastGetCurrentBatch ¶
LastGetCurrentBatch is a thread-safe method to get the current BatchNum in the last checkpoint of the StateDB.
func (*StateDB) LastMTGetRoot ¶
LastMTGetRoot returns the root of the underlying Merkle Tree in the last checkpoint of the StateDB.
func (*StateDB) LastRead ¶
LastRead is a thread-safe method to query the last checkpoint of the StateDB via the Last type methods
func (*StateDB) MTGetProof ¶
MTGetProof returns the CircomVerifierProof for a given Idx
func (*StateDB) MakeCheckpoint ¶
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 ¶
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 ¶
SetCurrentIdx stores Idx in the StateDB
func (*StateDB) TestGetAccounts ¶
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.