Avoid using pointers in common

This commit is contained in:
Arnau B
2020-10-07 18:43:48 +02:00
parent 5ec18f0378
commit 5097939b12
30 changed files with 673 additions and 564 deletions

View File

@@ -247,19 +247,13 @@ func (s *StateDB) Reset(batchNum common.BatchNum) error {
}
// GetAccount returns the account for the given Idx
func (s *StateDB) GetAccount(idx *common.Idx) (*common.Account, error) {
if idx == nil {
return nil, errors.New("idx cannot be nil")
}
func (s *StateDB) GetAccount(idx common.Idx) (*common.Account, error) {
return getAccountInTreeDB(s.db, idx)
}
// getAccountInTreeDB is abstracted from StateDB to be used from StateDB and
// from ExitTree. GetAccount returns the account for the given Idx
func getAccountInTreeDB(sto db.Storage, idx *common.Idx) (*common.Account, error) {
if idx == nil {
return nil, errors.New("idx cannot be nil")
}
func getAccountInTreeDB(sto db.Storage, idx common.Idx) (*common.Account, error) {
idxBytes, err := idx.Bytes()
if err != nil {
return nil, err
@@ -281,12 +275,12 @@ func getAccountInTreeDB(sto db.Storage, idx *common.Idx) (*common.Account, error
// StateDB.mt==nil, MerkleTree is not affected, otherwise updates the
// MerkleTree, returning a CircomProcessorProof.
func (s *StateDB) CreateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
cpp, err := createAccountInTreeDB(s.db, s.mt, &idx, account)
cpp, err := createAccountInTreeDB(s.db, s.mt, idx, account)
if err != nil {
return cpp, err
}
// store idx by EthAddr & BJJ
err = s.setIdxByEthAddrBJJ(idx, &account.EthAddr, account.PublicKey)
err = s.setIdxByEthAddrBJJ(idx, account.EthAddr, account.PublicKey)
return cpp, err
}
@@ -294,10 +288,7 @@ func (s *StateDB) CreateAccount(idx common.Idx, account *common.Account) (*merkl
// 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 createAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx *common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
if idx == nil {
return nil, errors.New("idx cannot be nil")
}
func createAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
// store at the DB the key: v, and value: leaf.Bytes()
v, err := account.HashValue()
if err != nil {
@@ -346,10 +337,7 @@ func createAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx *commo
// 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.
func (s *StateDB) UpdateAccount(idx *common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
if idx == nil {
return nil, errors.New("idx cannot be nil")
}
func (s *StateDB) UpdateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
return updateAccountInTreeDB(s.db, s.mt, idx, account)
}
@@ -357,10 +345,7 @@ func (s *StateDB) UpdateAccount(idx *common.Idx, account *common.Account) (*merk
// 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.
func updateAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx *common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
if idx == nil {
return nil, errors.New("idx cannot be nil")
}
func updateAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
// store at the DB the key: v, and value: account.Bytes()
v, err := account.HashValue()
if err != nil {