• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revision78fd2d88ecff4263c7ba83d1725e78f2c929e1ad (tree)
Time2019-06-28 02:55:43
AuthorChengcheng Zhang <943420582@qq.c...>
CommiterChengcheng Zhang

Log Message

update GetUTXOs

Change Summary

Incremental Difference

--- a/account/store.go
+++ b/account/store.go
@@ -33,7 +33,7 @@ type AccountStorer interface {
3333 SetControlProgram(common.Hash, *CtrlProgram) error
3434 SetContractIndex(string, uint64)
3535 SetBip44ContractIndex(string, bool, uint64)
36- GetUTXOs() [][]byte
36+ GetUTXOs() []*UTXO
3737 GetStandardUTXO(bc.Hash) []byte
3838 GetContractUTXO(bc.Hash) []byte
3939 SetStandardUTXO(bc.Hash, []byte)
--- a/account/utxo_keeper.go
+++ b/account/utxo_keeper.go
@@ -9,8 +9,6 @@ import (
99 "sync/atomic"
1010 "time"
1111
12- log "github.com/sirupsen/logrus"
13-
1412 "github.com/vapor/errors"
1513 "github.com/vapor/protocol/bc"
1614 )
@@ -222,14 +220,9 @@ func (uk *utxoKeeper) findUtxos(accountID string, assetID *bc.AssetID, useUnconf
222220 }
223221 }
224222
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)
233226 }
234227
235228 if !useUnconfirmed {
--- a/database/account_store.go
+++ b/database/account_store.go
@@ -4,6 +4,7 @@ import (
44 "encoding/json"
55 "strings"
66
7+ log "github.com/sirupsen/logrus"
78 acc "github.com/vapor/account"
89 "github.com/vapor/common"
910 "github.com/vapor/crypto/ed25519/chainkd"
@@ -328,13 +329,18 @@ func (store *AccountStore) SetBip44ContractIndex(accountID string, change bool,
328329 }
329330
330331 // GetUTXOs get utxos by accountID
331-func (store *AccountStore) GetUTXOs() [][]byte {
332+func (store *AccountStore) GetUTXOs() []*acc.UTXO {
332333 utxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
333334 defer utxoIter.Release()
334335
335- utxos := make([][]byte, 0)
336+ utxos := []*acc.UTXO{}
336337 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)
338344 }
339345 return utxos
340346 }
--- a/test/mock/UTXO.go
+++ b/test/mock/UTXO.go
@@ -9,8 +9,6 @@ import (
99 "sync/atomic"
1010 "time"
1111
12- log "github.com/sirupsen/logrus"
13-
1412 acc "github.com/vapor/account"
1513 "github.com/vapor/protocol/bc"
1614 )
@@ -88,7 +86,7 @@ func (uk *UTXOKeeper) Reserve(accountID string, assetID *bc.AssetID, amount uint
8886 uk.mtx.Lock()
8987 defer uk.mtx.Unlock()
9088
91- utxos, immatureAmount := uk.findUtxos(accountID, assetID, useUnconfirmed, vote)
89+ utxos, immatureAmount := uk.FindUtxos(accountID, assetID, useUnconfirmed, vote)
9290 optUtxos, optAmount, reservedAmount := uk.optUTXOs(utxos, amount)
9391 if optAmount+reservedAmount+immatureAmount < amount {
9492 return nil, acc.ErrInsufficient
@@ -161,7 +159,7 @@ func (uk *UTXOKeeper) FindUtxo(outHash bc.Hash, useUnconfirmed bool) (*acc.UTXO,
161159 return nil, acc.ErrMatchUTXO
162160 }
163161
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) {
165163 immatureAmount := uint64(0)
166164 currentHeight := uk.CurrentHeight()
167165 utxos := []*acc.UTXO{}
@@ -176,14 +174,9 @@ func (uk *UTXOKeeper) findUtxos(accountID string, assetID *bc.AssetID, useUnconf
176174 }
177175 }
178176
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)
187180 }
188181
189182 if !useUnconfirmed {
@@ -248,38 +241,3 @@ func (uk *UTXOKeeper) optUTXOs(utxos []*acc.UTXO, amount uint64) ([]*acc.UTXO, u
248241 }
249242 return optUtxos, optAmount, reservedAmount
250243 }
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-}
Show on old repository browser