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