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