hermez-node/db/statedb/utils.go
db/statedb/utils.goRaw
1package statedb
2
3import (
4 "bytes"
5 "errors"
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(k, idxBytes[:])
56 if err != nil {
57 return err
58 }
59 // store Addr-idx
60 err = tx.Put(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 if addr == nil {
76 return 0, errors.New("addr cannot be nil")
77 }
78 b, err := s.db.Get(addr.Bytes())
79 if err != nil {
80 return common.Idx(0), ErrToIdxNotFound
81 }
82 idx, err := common.IdxFromBytes(b)
83 if err != nil {
84 return common.Idx(0), ErrToIdxNotFound
85 }
86 return idx, nil
87}
88
89// GetIdxByEthAddrBJJ returns the smallest Idx in the StateDB for the given
90// Ethereum Address AND the given BabyJubJub PublicKey. If `addr` is the zero
91// address, it's ignored in the query. If `pk` is nil, it's ignored in the
92// query. Will return common.Idx(0) and error in case that Idx is not found in
93// the StateDB.
94func (s *StateDB) GetIdxByEthAddrBJJ(addr *ethCommon.Address, pk *babyjub.PublicKey) (common.Idx, error) {
95 if addr == nil {
96 return 0, errors.New("addr cannot be nil")
97 }
98 if !bytes.Equal(addr.Bytes(), common.EmptyAddr.Bytes()) && pk == nil {
99 // case ToEthAddr!=0 && ToBJJ=0
100 return s.GetIdxByEthAddr(addr)
101 } else if !bytes.Equal(addr.Bytes(), common.EmptyAddr.Bytes()) && pk != nil {
102 // case ToEthAddr!=0 && ToBJJ!=0
103 k := concatEthAddrBJJ(addr, pk)
104 b, err := s.db.Get(k)
105 if err != nil {
106 return common.Idx(0), ErrToIdxNotFound
107 }
108 idx, err := common.IdxFromBytes(b)
109 if err != nil {
110 return common.Idx(0), ErrToIdxNotFound
111 }
112 return idx, nil
113 }
114 // rest of cases (included case ToEthAddr==0) are not possible
115 return common.Idx(0), ErrToIdxNotFound
116}
117
118func siblingsToZKInputFormat(s []*merkletree.Hash) []*big.Int {
119 b := make([]*big.Int, len(s))
120 for i := 0; i < len(s); i++ {
121 b[i] = s[i].BigInt()
122 }
123 return b
124}
125
126// BJJCompressedTo256BigInts returns a [256]*big.Int array with the bit
127// representation of the babyjub.PublicKeyComp
128func BJJCompressedTo256BigInts(pkComp babyjub.PublicKeyComp) [256]*big.Int {
129 var r [256]*big.Int
130 b := pkComp[:]
131
132 for i := 0; i < 256; i++ {
133 if b[i/8]&(1<<(i%8)) == 0 {
134 r[i] = big.NewInt(0)
135 } else {
136 r[i] = big.NewInt(1)
137 }
138 }
139
140 return r
141}