arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/db/statedb/utils.go
db/statedb/utils.goRaw
  1package statedb
  2
  3import (
  4	"bytes"
  5	"fmt"
  6
  7	ethCommon "github.com/ethereum/go-ethereum/common"
  8	"github.com/hermeznetwork/hermez-node/common"
  9	"github.com/hermeznetwork/hermez-node/log"
 10	"github.com/hermeznetwork/tracerr"
 11	"github.com/iden3/go-iden3-crypto/babyjub"
 12	"github.com/iden3/go-merkletree/db"
 13)
 14
 15func concatEthAddrTokenID(addr ethCommon.Address, tokenID common.TokenID) []byte {
 16	var b []byte
 17	b = append(b, addr.Bytes()...)
 18	b = append(b[:], tokenID.Bytes()[:]...)
 19	return b
 20}
 21func concatEthAddrBJJTokenID(addr ethCommon.Address, pk babyjub.PublicKeyComp, tokenID common.TokenID) []byte {
 22	pkComp := pk
 23	var b []byte
 24	b = append(b, addr.Bytes()...)
 25	b = append(b[:], pkComp[:]...)
 26	b = append(b[:], tokenID.Bytes()[:]...)
 27	return b
 28}
 29
 30// setIdxByEthAddrBJJ stores the given Idx in the StateDB as follows:
 31// - key: Eth Address, value: idx
 32// - key: EthAddr & BabyJubJub PublicKey Compressed, value: idx
 33// If Idx already exist for the given EthAddr & BJJ, the remaining Idx will be
 34// always the smallest one.
 35func (s *StateDB) setIdxByEthAddrBJJ(idx common.Idx, addr ethCommon.Address, pk babyjub.PublicKeyComp, tokenID common.TokenID) error {
 36	oldIdx, err := s.GetIdxByEthAddrBJJ(addr, pk, tokenID)
 37	if err == nil {
 38		// EthAddr & BJJ already have an Idx
 39		// check which Idx is smaller
 40		// if new idx is smaller, store the new one
 41		// if new idx is bigger, don't store and return, as the used one will be the old
 42		if idx >= oldIdx {
 43			log.Debug("StateDB.setIdxByEthAddrBJJ: Idx not stored because there already exist a smaller Idx for the given EthAddr & BJJ")
 44			return nil
 45		}
 46	}
 47
 48	// store idx for EthAddr & BJJ assuming that EthAddr & BJJ still don't
 49	// have an Idx stored in the DB, and if so, the already stored Idx is
 50	// bigger than the given one, so should be updated to the new one
 51	// (smaller)
 52	tx, err := s.db.DB().NewTx()
 53	if err != nil {
 54		return tracerr.Wrap(err)
 55	}
 56	idxBytes, err := idx.Bytes()
 57	if err != nil {
 58		return tracerr.Wrap(err)
 59	}
 60	// store Addr&BJJ-idx
 61	k := concatEthAddrBJJTokenID(addr, pk, tokenID)
 62	err = tx.Put(append(PrefixKeyAddrBJJ, k...), idxBytes[:])
 63	if err != nil {
 64		return tracerr.Wrap(err)
 65	}
 66
 67	// store Addr-idx
 68	k = concatEthAddrTokenID(addr, tokenID)
 69	err = tx.Put(append(PrefixKeyAddr, k...), idxBytes[:])
 70	if err != nil {
 71		return tracerr.Wrap(err)
 72	}
 73	err = tx.Commit()
 74	if err != nil {
 75		return tracerr.Wrap(err)
 76	}
 77	return nil
 78}
 79
 80// GetIdxByEthAddr returns the smallest Idx in the StateDB for the given
 81// Ethereum Address. Will return common.Idx(0) and error in case that Idx is
 82// not found in the StateDB.
 83func (s *StateDB) GetIdxByEthAddr(addr ethCommon.Address, tokenID common.TokenID) (common.Idx, error) {
 84	k := concatEthAddrTokenID(addr, tokenID)
 85	b, err := s.db.DB().Get(append(PrefixKeyAddr, k...))
 86	if err != nil {
 87		return common.Idx(0), tracerr.Wrap(fmt.Errorf("GetIdxByEthAddr: %s: ToEthAddr: %s, TokenID: %d",
 88			ErrIdxNotFound, addr.Hex(), tokenID))
 89	}
 90	idx, err := common.IdxFromBytes(b)
 91	if err != nil {
 92		return common.Idx(0), tracerr.Wrap(fmt.Errorf("GetIdxByEthAddr: %s: ToEthAddr: %s, TokenID: %d",
 93			err, addr.Hex(), tokenID))
 94	}
 95	return idx, nil
 96}
 97
 98// GetIdxByEthAddrBJJ returns the smallest Idx in the StateDB for the given
 99// Ethereum Address AND the given BabyJubJub PublicKey. If `addr` is the zero
100// address, it's ignored in the query.  If `pk` is nil, it's ignored in the
101// query.  Will return common.Idx(0) and error in case that Idx is not found in
102// the StateDB.
103func (s *StateDB) GetIdxByEthAddrBJJ(addr ethCommon.Address, pk babyjub.PublicKeyComp,
104	tokenID common.TokenID) (common.Idx, error) {
105	if !bytes.Equal(addr.Bytes(), common.EmptyAddr.Bytes()) && pk == common.EmptyBJJComp {
106		// ToEthAddr
107		// case ToEthAddr!=0 && ToBJJ=0
108		return s.GetIdxByEthAddr(addr, tokenID)
109	} else if !bytes.Equal(addr.Bytes(), common.EmptyAddr.Bytes()) &&
110		pk != common.EmptyBJJComp {
111		// case ToEthAddr!=0 && ToBJJ!=0
112		k := concatEthAddrBJJTokenID(addr, pk, tokenID)
113		b, err := s.db.DB().Get(append(PrefixKeyAddrBJJ, k...))
114		if tracerr.Unwrap(err) == db.ErrNotFound {
115			// return the error (ErrNotFound), so can be traced at upper layers
116			return common.Idx(0), tracerr.Wrap(ErrIdxNotFound)
117		} else if err != nil {
118			return common.Idx(0),
119				tracerr.Wrap(fmt.Errorf("GetIdxByEthAddrBJJ: %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d", ErrIdxNotFound, addr.Hex(), pk, tokenID))
120		}
121		idx, err := common.IdxFromBytes(b)
122		if err != nil {
123			return common.Idx(0),
124				tracerr.Wrap(fmt.Errorf("GetIdxByEthAddrBJJ: %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d", err, addr.Hex(), pk, tokenID))
125		}
126		return idx, nil
127	}
128	// rest of cases (included case ToEthAddr==0) are not possible
129	return common.Idx(0),
130		tracerr.Wrap(fmt.Errorf("GetIdxByEthAddrBJJ: Not found, %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d", ErrGetIdxNoCase, addr.Hex(), pk, tokenID))
131}
132
133// GetTokenIDsFromIdxs returns a map containing the common.TokenID with its
134// respective common.Idx for a given slice of common.Idx
135func (s *StateDB) GetTokenIDsFromIdxs(idxs []common.Idx) (map[common.TokenID]common.Idx, error) {
136	m := make(map[common.TokenID]common.Idx)
137	for i := 0; i < len(idxs); i++ {
138		a, err := s.GetAccount(idxs[i])
139		if err != nil {
140			return nil, tracerr.Wrap(fmt.Errorf("GetTokenIDsFromIdxs error on GetAccount with Idx==%d: %s", idxs[i], err.Error()))
141		}
142		m[a.TokenID] = idxs[i]
143	}
144	return m, nil
145}