mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Avoid using pointers in common
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -130,7 +130,7 @@ func TestStateDBWithoutMT(t *testing.T) {
|
||||
|
||||
// get non-existing account, expecting an error
|
||||
unexistingAccount := common.Idx(1)
|
||||
_, err = sdb.GetAccount(&unexistingAccount)
|
||||
_, err = sdb.GetAccount(unexistingAccount)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, db.ErrNotFound, err)
|
||||
|
||||
@@ -142,14 +142,14 @@ func TestStateDBWithoutMT(t *testing.T) {
|
||||
|
||||
for i := 0; i < len(accounts); i++ {
|
||||
existingAccount := common.Idx(i)
|
||||
accGetted, err := sdb.GetAccount(&existingAccount)
|
||||
accGetted, err := sdb.GetAccount(existingAccount)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, accounts[i], accGetted)
|
||||
}
|
||||
|
||||
// try already existing idx and get error
|
||||
existingAccount := common.Idx(1)
|
||||
_, err = sdb.GetAccount(&existingAccount) // check that exist
|
||||
_, err = sdb.GetAccount(existingAccount) // check that exist
|
||||
assert.Nil(t, err)
|
||||
_, err = sdb.CreateAccount(common.Idx(1), accounts[1]) // check that can not be created twice
|
||||
assert.NotNil(t, err)
|
||||
@@ -159,7 +159,7 @@ func TestStateDBWithoutMT(t *testing.T) {
|
||||
for i := 0; i < len(accounts); i++ {
|
||||
accounts[i].Nonce = accounts[i].Nonce + 1
|
||||
existingAccount = common.Idx(i)
|
||||
_, err = sdb.UpdateAccount(&existingAccount, accounts[i])
|
||||
_, err = sdb.UpdateAccount(existingAccount, accounts[i])
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
@@ -182,8 +182,7 @@ func TestStateDBWithMT(t *testing.T) {
|
||||
}
|
||||
|
||||
// get non-existing account, expecting an error
|
||||
accountIdx := common.Idx(1)
|
||||
_, err = sdb.GetAccount(&accountIdx)
|
||||
_, err = sdb.GetAccount(common.Idx(1))
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, db.ErrNotFound, err)
|
||||
|
||||
@@ -194,15 +193,13 @@ func TestStateDBWithMT(t *testing.T) {
|
||||
}
|
||||
|
||||
for i := 0; i < len(accounts); i++ {
|
||||
accountIdx = common.Idx(i)
|
||||
accGetted, err := sdb.GetAccount(&accountIdx)
|
||||
accGetted, err := sdb.GetAccount(common.Idx(i))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, accounts[i], accGetted)
|
||||
}
|
||||
|
||||
// try already existing idx and get error
|
||||
accountIdx = 1
|
||||
_, err = sdb.GetAccount(&accountIdx) // check that exist
|
||||
_, err = sdb.GetAccount(common.Idx(1)) // check that exist
|
||||
assert.Nil(t, err)
|
||||
_, err = sdb.CreateAccount(common.Idx(1), accounts[1]) // check that can not be created twice
|
||||
assert.NotNil(t, err)
|
||||
@@ -214,12 +211,10 @@ func TestStateDBWithMT(t *testing.T) {
|
||||
// update accounts
|
||||
for i := 0; i < len(accounts); i++ {
|
||||
accounts[i].Nonce = accounts[i].Nonce + 1
|
||||
accountIdx = common.Idx(i)
|
||||
_, err = sdb.UpdateAccount(&accountIdx, accounts[i])
|
||||
_, err = sdb.UpdateAccount(common.Idx(i), accounts[i])
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
accountIdx = 1
|
||||
a, err := sdb.GetAccount(&accountIdx) // check that account value has been updated
|
||||
a, err := sdb.GetAccount(common.Idx(1)) // check that account value has been updated
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, accounts[1].Nonce, a.Nonce)
|
||||
}
|
||||
|
||||
@@ -230,11 +230,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
|
||||
if s.zki != nil {
|
||||
// Txs
|
||||
// s.zki.TxCompressedData[s.i] = tx.TxCompressedData() // uncomment once L1Tx.TxCompressedData is ready
|
||||
if tx.FromIdx != nil {
|
||||
s.zki.FromIdx[s.i] = tx.FromIdx.BigInt()
|
||||
} else {
|
||||
s.zki.FromIdx[s.i] = big.NewInt(0)
|
||||
}
|
||||
s.zki.FromIdx[s.i] = tx.FromIdx.BigInt()
|
||||
s.zki.ToIdx[s.i] = tx.ToIdx.BigInt()
|
||||
s.zki.OnChain[s.i] = big.NewInt(1)
|
||||
|
||||
@@ -299,7 +295,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
|
||||
if err != nil {
|
||||
return nil, nil, false, err
|
||||
}
|
||||
return tx.FromIdx, exitAccount, newExit, nil
|
||||
return &tx.FromIdx, exitAccount, newExit, nil
|
||||
default:
|
||||
}
|
||||
|
||||
@@ -314,7 +310,7 @@ func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2
|
||||
var err error
|
||||
var auxToIdx common.Idx
|
||||
// if tx.ToIdx==0, get toIdx by ToEthAddr or ToBJJ
|
||||
if tx.ToIdx == nil || *tx.ToIdx == common.Idx(0) {
|
||||
if tx.ToIdx == common.Idx(0) {
|
||||
auxToIdx, err = s.GetIdxByEthAddrBJJ(tx.ToEthAddr, tx.ToBJJ)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
@@ -331,7 +327,7 @@ func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2
|
||||
s.zki.ToIdx[s.i] = tx.ToIdx.BigInt()
|
||||
|
||||
// fill AuxToIdx if needed
|
||||
if tx.ToIdx == nil {
|
||||
if tx.ToIdx == 0 {
|
||||
// use toIdx that can have been filled by tx.ToIdx or
|
||||
// if tx.Idx==0 (this case), toIdx is filled by the Idx
|
||||
// from db by ToEthAddr&ToBJJ
|
||||
@@ -339,12 +335,7 @@ func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2
|
||||
}
|
||||
|
||||
s.zki.ToBJJAy[s.i] = tx.ToBJJ.Y
|
||||
if tx.ToEthAddr != nil {
|
||||
s.zki.ToEthAddr[s.i] = common.EthAddrToBigInt(*tx.ToEthAddr)
|
||||
} else {
|
||||
// Not sure if this should throw an error
|
||||
s.zki.ToEthAddr[s.i] = big.NewInt(0)
|
||||
}
|
||||
s.zki.ToEthAddr[s.i] = common.EthAddrToBigInt(tx.ToEthAddr)
|
||||
|
||||
s.zki.OnChain[s.i] = big.NewInt(0)
|
||||
s.zki.NewAccount[s.i] = big.NewInt(0)
|
||||
@@ -448,7 +439,7 @@ func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
|
||||
// in case that the tx is a L1Tx>DepositTransfer
|
||||
var accReceiver *common.Account
|
||||
if transfer {
|
||||
accReceiver, err = s.GetAccount(&tx.ToIdx)
|
||||
accReceiver, err = s.GetAccount(tx.ToIdx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -478,7 +469,7 @@ func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
|
||||
// this is done after updating Sender Account (depositer)
|
||||
if transfer {
|
||||
// update receiver account in localStateDB
|
||||
p, err := s.UpdateAccount(&tx.ToIdx, accReceiver)
|
||||
p, err := s.UpdateAccount(tx.ToIdx, accReceiver)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -507,17 +498,14 @@ func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
|
||||
// the real ToIdx is found trhrough the ToEthAddr or ToBJJ.
|
||||
func (s *StateDB) applyTransfer(tx *common.Tx, auxToIdx common.Idx) error {
|
||||
if auxToIdx == 0 {
|
||||
if tx.ToIdx == nil {
|
||||
return errors.New("tx.ToIdx cannot be nil if auxToIdx is 0")
|
||||
}
|
||||
auxToIdx = *tx.ToIdx
|
||||
auxToIdx = tx.ToIdx
|
||||
}
|
||||
// get sender and receiver accounts from localStateDB
|
||||
accSender, err := s.GetAccount(tx.FromIdx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
accReceiver, err := s.GetAccount(&auxToIdx)
|
||||
accReceiver, err := s.GetAccount(auxToIdx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -548,7 +536,7 @@ func (s *StateDB) applyTransfer(tx *common.Tx, auxToIdx common.Idx) error {
|
||||
}
|
||||
|
||||
// update receiver account in localStateDB
|
||||
pReceiver, err := s.UpdateAccount(&auxToIdx, accReceiver)
|
||||
pReceiver, err := s.UpdateAccount(auxToIdx, accReceiver)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -578,7 +566,7 @@ func (s *StateDB) applyCreateAccountDepositTransfer(tx *common.L1Tx) error {
|
||||
EthAddr: tx.FromEthAddr,
|
||||
}
|
||||
accSender.Balance = new(big.Int).Add(accSender.Balance, tx.LoadAmount)
|
||||
accReceiver, err := s.GetAccount(&tx.ToIdx)
|
||||
accReceiver, err := s.GetAccount(tx.ToIdx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -610,7 +598,7 @@ func (s *StateDB) applyCreateAccountDepositTransfer(tx *common.L1Tx) error {
|
||||
}
|
||||
|
||||
// update receiver account in localStateDB
|
||||
p, err = s.UpdateAccount(&tx.ToIdx, accReceiver)
|
||||
p, err = s.UpdateAccount(tx.ToIdx, accReceiver)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -40,8 +40,7 @@ func TestProcessTxs(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
accountIdx := common.Idx(256)
|
||||
acc, err := sdb.GetAccount(&accountIdx)
|
||||
acc, err := sdb.GetAccount(common.Idx(256))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "23", acc.Balance.String())
|
||||
}
|
||||
@@ -80,8 +79,7 @@ func TestProcessTxsSynchronizer(t *testing.T) {
|
||||
// Nonce & TokenID =0, after ProcessTxs call has the expected value
|
||||
|
||||
assert.Equal(t, 0, len(exitInfos))
|
||||
accountIdx := common.Idx(256)
|
||||
acc, err := sdb.GetAccount(&accountIdx)
|
||||
acc, err := sdb.GetAccount(common.Idx(256))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "28", acc.Balance.String())
|
||||
|
||||
@@ -90,7 +88,7 @@ func TestProcessTxsSynchronizer(t *testing.T) {
|
||||
_, exitInfos, err = sdb.ProcessTxs(l1Txs[1], coordinatorL1Txs[1], poolL2Txs[1])
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 5, len(exitInfos))
|
||||
acc, err = sdb.GetAccount(&accountIdx)
|
||||
acc, err = sdb.GetAccount(common.Idx(256))
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, "48", acc.Balance.String())
|
||||
|
||||
@@ -99,7 +97,7 @@ func TestProcessTxsSynchronizer(t *testing.T) {
|
||||
_, exitInfos, err = sdb.ProcessTxs(l1Txs[2], coordinatorL1Txs[2], poolL2Txs[2])
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 1, len(exitInfos))
|
||||
acc, err = sdb.GetAccount(&accountIdx)
|
||||
acc, err = sdb.GetAccount(common.Idx(256))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "23", acc.Balance.String())
|
||||
}
|
||||
@@ -132,8 +130,7 @@ func TestProcessTxsBatchBuilder(t *testing.T) {
|
||||
_, exitInfos, err := sdb.ProcessTxs(l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0])
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 0, len(exitInfos))
|
||||
accountIdx := common.Idx(256)
|
||||
acc, err := sdb.GetAccount(&accountIdx)
|
||||
acc, err := sdb.GetAccount(common.Idx(256))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "28", acc.Balance.String())
|
||||
|
||||
@@ -142,7 +139,7 @@ func TestProcessTxsBatchBuilder(t *testing.T) {
|
||||
_, exitInfos, err = sdb.ProcessTxs(l1Txs[1], coordinatorL1Txs[1], poolL2Txs[1])
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 5, len(exitInfos))
|
||||
acc, err = sdb.GetAccount(&accountIdx)
|
||||
acc, err = sdb.GetAccount(common.Idx(256))
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, "48", acc.Balance.String())
|
||||
|
||||
@@ -151,7 +148,7 @@ func TestProcessTxsBatchBuilder(t *testing.T) {
|
||||
_, exitInfos, err = sdb.ProcessTxs(l1Txs[2], coordinatorL1Txs[2], poolL2Txs[2])
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 1, len(exitInfos))
|
||||
acc, err = sdb.GetAccount(&accountIdx)
|
||||
acc, err = sdb.GetAccount(common.Idx(256))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "23", acc.Balance.String())
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package statedb
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"math/big"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
@@ -12,7 +11,7 @@ import (
|
||||
"github.com/iden3/go-merkletree"
|
||||
)
|
||||
|
||||
func concatEthAddrBJJ(addr *ethCommon.Address, pk *babyjub.PublicKey) []byte {
|
||||
func concatEthAddrBJJ(addr ethCommon.Address, pk *babyjub.PublicKey) []byte {
|
||||
pkComp := pk.Compress()
|
||||
var b []byte
|
||||
b = append(b, addr.Bytes()...)
|
||||
@@ -25,7 +24,7 @@ func concatEthAddrBJJ(addr *ethCommon.Address, pk *babyjub.PublicKey) []byte {
|
||||
// - key: EthAddr & BabyJubJub PublicKey Compressed, value: idx
|
||||
// If Idx already exist for the given EthAddr & BJJ, the remaining Idx will be
|
||||
// always the smallest one.
|
||||
func (s *StateDB) setIdxByEthAddrBJJ(idx common.Idx, addr *ethCommon.Address, pk *babyjub.PublicKey) error {
|
||||
func (s *StateDB) setIdxByEthAddrBJJ(idx common.Idx, addr ethCommon.Address, pk *babyjub.PublicKey) error {
|
||||
oldIdx, err := s.GetIdxByEthAddrBJJ(addr, pk)
|
||||
if err == nil {
|
||||
// EthAddr & BJJ already have an Idx
|
||||
@@ -71,10 +70,7 @@ func (s *StateDB) setIdxByEthAddrBJJ(idx common.Idx, addr *ethCommon.Address, pk
|
||||
// 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 (s *StateDB) GetIdxByEthAddr(addr *ethCommon.Address) (common.Idx, error) {
|
||||
if addr == nil {
|
||||
return 0, errors.New("addr cannot be nil")
|
||||
}
|
||||
func (s *StateDB) GetIdxByEthAddr(addr ethCommon.Address) (common.Idx, error) {
|
||||
b, err := s.db.Get(addr.Bytes())
|
||||
if err != nil {
|
||||
return common.Idx(0), ErrToIdxNotFound
|
||||
@@ -91,10 +87,7 @@ func (s *StateDB) GetIdxByEthAddr(addr *ethCommon.Address) (common.Idx, error) {
|
||||
// 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 (s *StateDB) GetIdxByEthAddrBJJ(addr *ethCommon.Address, pk *babyjub.PublicKey) (common.Idx, error) {
|
||||
if addr == nil {
|
||||
return 0, errors.New("addr cannot be nil")
|
||||
}
|
||||
func (s *StateDB) GetIdxByEthAddrBJJ(addr ethCommon.Address, pk *babyjub.PublicKey) (common.Idx, error) {
|
||||
if !bytes.Equal(addr.Bytes(), common.EmptyAddr.Bytes()) && pk == nil {
|
||||
// case ToEthAddr!=0 && ToBJJ=0
|
||||
return s.GetIdxByEthAddr(addr)
|
||||
|
||||
@@ -32,47 +32,47 @@ func TestGetIdx(t *testing.T) {
|
||||
idx3 := common.Idx(1233)
|
||||
|
||||
// store the keys for idx by Addr & BJJ
|
||||
err = sdb.setIdxByEthAddrBJJ(idx, &addr, pk)
|
||||
err = sdb.setIdxByEthAddrBJJ(idx, addr, pk)
|
||||
require.Nil(t, err)
|
||||
|
||||
idxR, err := sdb.GetIdxByEthAddrBJJ(&addr, pk)
|
||||
idxR, err := sdb.GetIdxByEthAddrBJJ(addr, pk)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, idx, idxR)
|
||||
|
||||
// expect error when getting only by EthAddr, as value does not exist
|
||||
// in the db for only EthAddr
|
||||
_, err = sdb.GetIdxByEthAddr(&addr)
|
||||
_, err = sdb.GetIdxByEthAddr(addr)
|
||||
assert.Nil(t, err)
|
||||
_, err = sdb.GetIdxByEthAddr(&addr2)
|
||||
_, err = sdb.GetIdxByEthAddr(addr2)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// expect to fail
|
||||
idxR, err = sdb.GetIdxByEthAddrBJJ(&addr2, pk)
|
||||
idxR, err = sdb.GetIdxByEthAddrBJJ(addr2, pk)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, common.Idx(0), idxR)
|
||||
idxR, err = sdb.GetIdxByEthAddrBJJ(&addr, pk2)
|
||||
idxR, err = sdb.GetIdxByEthAddrBJJ(addr, pk2)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, common.Idx(0), idxR)
|
||||
|
||||
// try to store bigger idx, will not affect as already exist a smaller
|
||||
// Idx for that Addr & BJJ
|
||||
err = sdb.setIdxByEthAddrBJJ(idx2, &addr, pk)
|
||||
err = sdb.setIdxByEthAddrBJJ(idx2, addr, pk)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// store smaller idx
|
||||
err = sdb.setIdxByEthAddrBJJ(idx3, &addr, pk)
|
||||
err = sdb.setIdxByEthAddrBJJ(idx3, addr, pk)
|
||||
assert.Nil(t, err)
|
||||
|
||||
idxR, err = sdb.GetIdxByEthAddrBJJ(&addr, pk)
|
||||
idxR, err = sdb.GetIdxByEthAddrBJJ(addr, pk)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, idx3, idxR)
|
||||
|
||||
// by EthAddr should work
|
||||
idxR, err = sdb.GetIdxByEthAddr(&addr)
|
||||
idxR, err = sdb.GetIdxByEthAddr(addr)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, idx3, idxR)
|
||||
// expect error when trying to get Idx by addr2 & pk2
|
||||
idxR, err = sdb.GetIdxByEthAddrBJJ(&addr2, pk2)
|
||||
idxR, err = sdb.GetIdxByEthAddrBJJ(addr2, pk2)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, ErrToIdxNotFound, err)
|
||||
assert.Equal(t, common.Idx(0), idxR)
|
||||
|
||||
Reference in New Issue
Block a user