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	"math/big"
  7
  8	ethCommon "github.com/ethereum/go-ethereum/common"
  9	"github.com/hermeznetwork/hermez-node/common"
 10	"github.com/hermeznetwork/hermez-node/log"
 11	"github.com/iden3/go-iden3-crypto/babyjub"
 12	"github.com/iden3/go-merkletree"
 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.PublicKey, tokenID common.TokenID) []byte {
 22	pkComp := pk.Compress()
 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.PublicKey, 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.NewTx()
 53	if err != nil {
 54		return err
 55	}
 56	k := concatEthAddrBJJTokenID(addr, pk, tokenID)
 57	// store Addr&BJJ-idx
 58	idxBytes, err := idx.Bytes()
 59	if err != nil {
 60		return err
 61	}
 62	err = tx.Put(append(PrefixKeyAddrBJJ, k...), idxBytes[:])
 63	if err != nil {
 64		return err
 65	}
 66	// store Addr-idx
 67	k = concatEthAddrTokenID(addr, tokenID)
 68	err = tx.Put(append(PrefixKeyAddr, k...), idxBytes[:])
 69	if err != nil {
 70		return err
 71	}
 72	err = tx.Commit()
 73	if err != nil {
 74		return err
 75	}
 76	return nil
 77}
 78
 79// GetIdxByEthAddr returns the smallest Idx in the StateDB for the given
 80// Ethereum Address. Will return common.Idx(0) and error in case that Idx is
 81// not found in the StateDB.
 82func (s *StateDB) GetIdxByEthAddr(addr ethCommon.Address, tokenID common.TokenID) (common.Idx, error) {
 83	k := concatEthAddrTokenID(addr, tokenID)
 84	b, err := s.db.Get(append(PrefixKeyAddr, k...))
 85	if err != nil {
 86		return common.Idx(0), ErrToIdxNotFound
 87	}
 88	idx, err := common.IdxFromBytes(b)
 89	if err != nil {
 90		return common.Idx(0), ErrToIdxNotFound
 91	}
 92	return idx, nil
 93}
 94
 95// GetIdxByEthAddrBJJ returns the smallest Idx in the StateDB for the given
 96// Ethereum Address AND the given BabyJubJub PublicKey. If `addr` is the zero
 97// address, it's ignored in the query.  If `pk` is nil, it's ignored in the
 98// query.  Will return common.Idx(0) and error in case that Idx is not found in
 99// the StateDB.
100func (s *StateDB) GetIdxByEthAddrBJJ(addr ethCommon.Address, pk *babyjub.PublicKey, tokenID common.TokenID) (common.Idx, error) {
101	if !bytes.Equal(addr.Bytes(), common.EmptyAddr.Bytes()) && pk == nil {
102		// case ToEthAddr!=0 && ToBJJ=0
103		return s.GetIdxByEthAddr(addr, tokenID)
104	} else if !bytes.Equal(addr.Bytes(), common.EmptyAddr.Bytes()) && pk != nil {
105		// case ToEthAddr!=0 && ToBJJ!=0
106		k := concatEthAddrBJJTokenID(addr, pk, tokenID)
107		b, err := s.db.Get(append(PrefixKeyAddrBJJ, k...))
108		if err != nil {
109			return common.Idx(0), ErrToIdxNotFound
110		}
111		idx, err := common.IdxFromBytes(b)
112		if err != nil {
113			return common.Idx(0), ErrToIdxNotFound
114		}
115		return idx, nil
116	}
117	// rest of cases (included case ToEthAddr==0) are not possible
118	return common.Idx(0), ErrToIdxNotFound
119}
120
121func (s *StateDB) getTokenIDsFromIdxs(idxs []common.Idx) (map[common.TokenID]common.Idx, error) {
122	m := make(map[common.TokenID]common.Idx)
123	for i := 0; i < len(idxs); i++ {
124		a, err := s.GetAccount(idxs[i])
125		if err != nil {
126			return nil, fmt.Errorf("getTokenIDsFromIdxs error on GetAccount with Idx==%d: %s", idxs[i], err.Error())
127		}
128		m[a.TokenID] = idxs[i]
129	}
130	return m, nil
131}
132
133func siblingsToZKInputFormat(s []*merkletree.Hash) []*big.Int {
134	b := make([]*big.Int, len(s))
135	for i := 0; i < len(s); i++ {
136		b[i] = s[i].BigInt()
137	}
138	return b
139}
140
141// BJJCompressedTo256BigInts returns a [256]*big.Int array with the bit
142// representation of the babyjub.PublicKeyComp
143func BJJCompressedTo256BigInts(pkComp babyjub.PublicKeyComp) [256]*big.Int {
144	var r [256]*big.Int
145	b := pkComp[:]
146
147	for i := 0; i < 256; i++ {
148		if b[i/8]&(1<<(i%8)) == 0 {
149			r[i] = big.NewInt(0)
150		} else {
151			r[i] = big.NewInt(1)
152		}
153	}
154
155	return r
156}