Golang implemented sidechain for Bytom
Revision | 78fd2d88ecff4263c7ba83d1725e78f2c929e1ad (tree) |
---|---|
Time | 2019-06-28 02:55:43 |
Author | Chengcheng Zhang <943420582@qq.c...> |
Commiter | Chengcheng Zhang |
update GetUTXOs
@@ -33,7 +33,7 @@ type AccountStorer interface { | ||
33 | 33 | SetControlProgram(common.Hash, *CtrlProgram) error |
34 | 34 | SetContractIndex(string, uint64) |
35 | 35 | SetBip44ContractIndex(string, bool, uint64) |
36 | - GetUTXOs() [][]byte | |
36 | + GetUTXOs() []*UTXO | |
37 | 37 | GetStandardUTXO(bc.Hash) []byte |
38 | 38 | GetContractUTXO(bc.Hash) []byte |
39 | 39 | SetStandardUTXO(bc.Hash, []byte) |
@@ -9,8 +9,6 @@ import ( | ||
9 | 9 | "sync/atomic" |
10 | 10 | "time" |
11 | 11 | |
12 | - log "github.com/sirupsen/logrus" | |
13 | - | |
14 | 12 | "github.com/vapor/errors" |
15 | 13 | "github.com/vapor/protocol/bc" |
16 | 14 | ) |
@@ -222,14 +220,9 @@ func (uk *utxoKeeper) findUtxos(accountID string, assetID *bc.AssetID, useUnconf | ||
222 | 220 | } |
223 | 221 | } |
224 | 222 | |
225 | - rawUTXOs := uk.store.GetUTXOs() | |
226 | - for _, rawUTXO := range rawUTXOs { | |
227 | - utxo := new(UTXO) | |
228 | - if err := json.Unmarshal(rawUTXO, utxo); err != nil { | |
229 | - log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo") | |
230 | - continue | |
231 | - } | |
232 | - appendUtxo(utxo) | |
223 | + UTXOs := uk.store.GetUTXOs() | |
224 | + for _, UTXO := range UTXOs { | |
225 | + appendUtxo(UTXO) | |
233 | 226 | } |
234 | 227 | |
235 | 228 | if !useUnconfirmed { |
@@ -4,6 +4,7 @@ import ( | ||
4 | 4 | "encoding/json" |
5 | 5 | "strings" |
6 | 6 | |
7 | + log "github.com/sirupsen/logrus" | |
7 | 8 | acc "github.com/vapor/account" |
8 | 9 | "github.com/vapor/common" |
9 | 10 | "github.com/vapor/crypto/ed25519/chainkd" |
@@ -328,13 +329,18 @@ func (store *AccountStore) SetBip44ContractIndex(accountID string, change bool, | ||
328 | 329 | } |
329 | 330 | |
330 | 331 | // GetUTXOs get utxos by accountID |
331 | -func (store *AccountStore) GetUTXOs() [][]byte { | |
332 | +func (store *AccountStore) GetUTXOs() []*acc.UTXO { | |
332 | 333 | utxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix)) |
333 | 334 | defer utxoIter.Release() |
334 | 335 | |
335 | - utxos := make([][]byte, 0) | |
336 | + utxos := []*acc.UTXO{} | |
336 | 337 | for utxoIter.Next() { |
337 | - utxos = append(utxos, utxoIter.Value()) | |
338 | + utxo := new(acc.UTXO) | |
339 | + if err := json.Unmarshal(utxoIter.Value(), utxo); err != nil { | |
340 | + log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo") | |
341 | + continue | |
342 | + } | |
343 | + utxos = append(utxos, utxo) | |
338 | 344 | } |
339 | 345 | return utxos |
340 | 346 | } |
@@ -9,8 +9,6 @@ import ( | ||
9 | 9 | "sync/atomic" |
10 | 10 | "time" |
11 | 11 | |
12 | - log "github.com/sirupsen/logrus" | |
13 | - | |
14 | 12 | acc "github.com/vapor/account" |
15 | 13 | "github.com/vapor/protocol/bc" |
16 | 14 | ) |
@@ -88,7 +86,7 @@ func (uk *UTXOKeeper) Reserve(accountID string, assetID *bc.AssetID, amount uint | ||
88 | 86 | uk.mtx.Lock() |
89 | 87 | defer uk.mtx.Unlock() |
90 | 88 | |
91 | - utxos, immatureAmount := uk.findUtxos(accountID, assetID, useUnconfirmed, vote) | |
89 | + utxos, immatureAmount := uk.FindUtxos(accountID, assetID, useUnconfirmed, vote) | |
92 | 90 | optUtxos, optAmount, reservedAmount := uk.optUTXOs(utxos, amount) |
93 | 91 | if optAmount+reservedAmount+immatureAmount < amount { |
94 | 92 | return nil, acc.ErrInsufficient |
@@ -161,7 +159,7 @@ func (uk *UTXOKeeper) FindUtxo(outHash bc.Hash, useUnconfirmed bool) (*acc.UTXO, | ||
161 | 159 | return nil, acc.ErrMatchUTXO |
162 | 160 | } |
163 | 161 | |
164 | -func (uk *UTXOKeeper) findUtxos(accountID string, assetID *bc.AssetID, useUnconfirmed bool, vote []byte) ([]*acc.UTXO, uint64) { | |
162 | +func (uk *UTXOKeeper) FindUtxos(accountID string, assetID *bc.AssetID, useUnconfirmed bool, vote []byte) ([]*acc.UTXO, uint64) { | |
165 | 163 | immatureAmount := uint64(0) |
166 | 164 | currentHeight := uk.CurrentHeight() |
167 | 165 | utxos := []*acc.UTXO{} |
@@ -176,14 +174,9 @@ func (uk *UTXOKeeper) findUtxos(accountID string, assetID *bc.AssetID, useUnconf | ||
176 | 174 | } |
177 | 175 | } |
178 | 176 | |
179 | - rawUTXOs := uk.Store.GetUTXOs() | |
180 | - for _, rawUTXO := range rawUTXOs { | |
181 | - utxo := new(acc.UTXO) | |
182 | - if err := json.Unmarshal(rawUTXO, utxo); err != nil { | |
183 | - log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo") | |
184 | - continue | |
185 | - } | |
186 | - appendUtxo(utxo) | |
177 | + UTXOs := uk.Store.GetUTXOs() | |
178 | + for _, UTXO := range UTXOs { | |
179 | + appendUtxo(UTXO) | |
187 | 180 | } |
188 | 181 | |
189 | 182 | if !useUnconfirmed { |
@@ -248,38 +241,3 @@ func (uk *UTXOKeeper) optUTXOs(utxos []*acc.UTXO, amount uint64) ([]*acc.UTXO, u | ||
248 | 241 | } |
249 | 242 | return optUtxos, optAmount, reservedAmount |
250 | 243 | } |
251 | - | |
252 | -func (uk *UTXOKeeper) FindUtxos(accountID string, assetID *bc.AssetID, useUnconfirmed bool, vote []byte) ([]*acc.UTXO, uint64) { | |
253 | - immatureAmount := uint64(0) | |
254 | - currentHeight := uk.CurrentHeight() | |
255 | - utxos := []*acc.UTXO{} | |
256 | - appendUtxo := func(u *acc.UTXO) { | |
257 | - if u.AccountID != accountID || u.AssetID != *assetID || !bytes.Equal(u.Vote, vote) { | |
258 | - return | |
259 | - } | |
260 | - if u.ValidHeight > currentHeight { | |
261 | - immatureAmount += u.Amount | |
262 | - } else { | |
263 | - utxos = append(utxos, u) | |
264 | - } | |
265 | - } | |
266 | - | |
267 | - rawUTXOs := uk.Store.GetUTXOs() | |
268 | - for _, rawUTXO := range rawUTXOs { | |
269 | - utxo := new(acc.UTXO) | |
270 | - if err := json.Unmarshal(rawUTXO, utxo); err != nil { | |
271 | - log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo") | |
272 | - continue | |
273 | - } | |
274 | - appendUtxo(utxo) | |
275 | - } | |
276 | - | |
277 | - if !useUnconfirmed { | |
278 | - return utxos, immatureAmount | |
279 | - } | |
280 | - | |
281 | - for _, u := range uk.Unconfirmed { | |
282 | - appendUtxo(u) | |
283 | - } | |
284 | - return utxos, immatureAmount | |
285 | -} |