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