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