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