"sync/atomic"
"time"
- log "github.com/sirupsen/logrus"
-
"github.com/vapor/errors"
"github.com/vapor/protocol/bc"
)
}
}
- rawUTXOs := uk.store.GetUTXOs()
- for _, rawUTXO := range rawUTXOs {
- utxo := new(UTXO)
- if err := json.Unmarshal(rawUTXO, utxo); err != nil {
- log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo")
- continue
- }
- appendUtxo(utxo)
+ UTXOs := uk.store.GetUTXOs()
+ for _, UTXO := range UTXOs {
+ appendUtxo(UTXO)
}
if !useUnconfirmed {
"encoding/json"
"strings"
+ log "github.com/sirupsen/logrus"
acc "github.com/vapor/account"
"github.com/vapor/common"
"github.com/vapor/crypto/ed25519/chainkd"
}
// GetUTXOs get utxos by accountID
-func (store *AccountStore) GetUTXOs() [][]byte {
+func (store *AccountStore) GetUTXOs() []*acc.UTXO {
utxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
defer utxoIter.Release()
- utxos := make([][]byte, 0)
+ utxos := []*acc.UTXO{}
for utxoIter.Next() {
- utxos = append(utxos, utxoIter.Value())
+ utxo := new(acc.UTXO)
+ if err := json.Unmarshal(utxoIter.Value(), utxo); err != nil {
+ log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo")
+ continue
+ }
+ utxos = append(utxos, utxo)
}
return utxos
}
"sync/atomic"
"time"
- log "github.com/sirupsen/logrus"
-
acc "github.com/vapor/account"
"github.com/vapor/protocol/bc"
)
uk.mtx.Lock()
defer uk.mtx.Unlock()
- utxos, immatureAmount := uk.findUtxos(accountID, assetID, useUnconfirmed, vote)
+ utxos, immatureAmount := uk.FindUtxos(accountID, assetID, useUnconfirmed, vote)
optUtxos, optAmount, reservedAmount := uk.optUTXOs(utxos, amount)
if optAmount+reservedAmount+immatureAmount < amount {
return nil, acc.ErrInsufficient
return nil, acc.ErrMatchUTXO
}
-func (uk *UTXOKeeper) findUtxos(accountID string, assetID *bc.AssetID, useUnconfirmed bool, vote []byte) ([]*acc.UTXO, uint64) {
+func (uk *UTXOKeeper) FindUtxos(accountID string, assetID *bc.AssetID, useUnconfirmed bool, vote []byte) ([]*acc.UTXO, uint64) {
immatureAmount := uint64(0)
currentHeight := uk.CurrentHeight()
utxos := []*acc.UTXO{}
}
}
- rawUTXOs := uk.Store.GetUTXOs()
- for _, rawUTXO := range rawUTXOs {
- utxo := new(acc.UTXO)
- if err := json.Unmarshal(rawUTXO, utxo); err != nil {
- log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo")
- continue
- }
- appendUtxo(utxo)
+ UTXOs := uk.Store.GetUTXOs()
+ for _, UTXO := range UTXOs {
+ appendUtxo(UTXO)
}
if !useUnconfirmed {
}
return optUtxos, optAmount, reservedAmount
}
-
-func (uk *UTXOKeeper) FindUtxos(accountID string, assetID *bc.AssetID, useUnconfirmed bool, vote []byte) ([]*acc.UTXO, uint64) {
- immatureAmount := uint64(0)
- currentHeight := uk.CurrentHeight()
- utxos := []*acc.UTXO{}
- appendUtxo := func(u *acc.UTXO) {
- if u.AccountID != accountID || u.AssetID != *assetID || !bytes.Equal(u.Vote, vote) {
- return
- }
- if u.ValidHeight > currentHeight {
- immatureAmount += u.Amount
- } else {
- utxos = append(utxos, u)
- }
- }
-
- rawUTXOs := uk.Store.GetUTXOs()
- for _, rawUTXO := range rawUTXOs {
- utxo := new(acc.UTXO)
- if err := json.Unmarshal(rawUTXO, utxo); err != nil {
- log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo")
- continue
- }
- appendUtxo(utxo)
- }
-
- if !useUnconfirmed {
- return utxos, immatureAmount
- }
-
- for _, u := range uk.Unconfirmed {
- appendUtxo(u)
- }
- return utxos, immatureAmount
-}