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