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 idxBytes, err := idx.Bytes()
50 if err != nil {
51 return err
52 }
53 err = tx.Put(k, idxBytes[:])
54 if err != nil {
55 return err
56 }
57 // store Addr-idx
58 err = tx.Put(addr.Bytes(), idxBytes[:])
59 if err != nil {
60 return err
61 }
62 err = tx.Commit()
63 if err != nil {
64 return err
65 }
66 return nil
67}
68
69// GetIdxByEthAddr returns the smallest Idx in the StateDB for the given
70// Ethereum Address. Will return common.Idx(0) and error in case that Idx is
71// not found in the StateDB.
72func (s *StateDB) GetIdxByEthAddr(addr ethCommon.Address) (common.Idx, error) {
73 b, err := s.db.Get(addr.Bytes())
74 if err != nil {
75 return common.Idx(0), err
76 }
77 idx, err := common.IdxFromBytes(b)
78 if err != nil {
79 return common.Idx(0), err
80 }
81 return idx, nil
82}
83
84// GetIdxByEthAddrBJJ returns the smallest Idx in the StateDB for the given
85// Ethereum Address AND the given BabyJubJub PublicKey. If `addr` is the zero
86// address, it's ignored in the query. If `pk` is nil, it's ignored in the
87// query. Will return common.Idx(0) and error in case that Idx is not found in
88// the StateDB.
89func (s *StateDB) GetIdxByEthAddrBJJ(addr ethCommon.Address, pk *babyjub.PublicKey) (common.Idx, error) {
90 if pk == nil {
91 return s.GetIdxByEthAddr(addr)
92 }
93
94 k := concatEthAddrBJJ(addr, pk)
95 b, err := s.db.Get(k)
96 if err != nil {
97 return common.Idx(0), err
98 }
99 idx, err := common.IdxFromBytes(b)
100 if err != nil {
101 return common.Idx(0), err
102 }
103 return idx, nil
104}
105
106func siblingsToZKInputFormat(s []*merkletree.Hash) []*big.Int {
107 b := make([]*big.Int, len(s))
108 for i := 0; i < len(s); i++ {
109 b[i] = s[i].BigInt()
110 }
111 return b
112}
113
114// BJJCompressedTo256BigInts returns a [256]*big.Int array with the bit
115// representation of the babyjub.PublicKeyComp
116func BJJCompressedTo256BigInts(pkComp babyjub.PublicKeyComp) [256]*big.Int {
117 var r [256]*big.Int
118 b := pkComp[:]
119
120 for i := 0; i < 256; i++ {
121 if b[i/8]&(1<<(i%8)) == 0 {
122 r[i] = big.NewInt(0)
123 } else {
124 r[i] = big.NewInt(1)
125 }
126 }
127
128 return r
129}