6 log "github.com/sirupsen/logrus"
8 "github.com/vapor/account"
9 "github.com/vapor/consensus"
10 "github.com/vapor/consensus/segwit"
11 "github.com/vapor/crypto/sha3pool"
12 dbm "github.com/vapor/database/leveldb"
13 "github.com/vapor/errors"
14 "github.com/vapor/protocol/bc"
15 "github.com/vapor/protocol/bc/types"
18 // GetAccountUtxos return all account unspent outputs
19 func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSmartContract bool, vote bool) []*account.UTXO {
20 prefix := account.UTXOPreFix
22 prefix = account.SUTXOPrefix
25 accountUtxos := []*account.UTXO{}
27 accountUtxos = w.AccountMgr.ListUnconfirmedUtxo(accountID, isSmartContract)
30 accountUtxoIter := w.DB.IteratorPrefix([]byte(prefix + id))
31 defer accountUtxoIter.Release()
33 for accountUtxoIter.Next() {
34 accountUtxo := &account.UTXO{}
35 if err := json.Unmarshal(accountUtxoIter.Value(), accountUtxo); err != nil {
36 log.WithFields(log.Fields{"module": logModule, "err": err}).Warn("GetAccountUtxos fail on unmarshal utxo")
40 if vote && accountUtxo.Vote == nil {
44 if accountID == accountUtxo.AccountID || accountID == "" {
45 accountUtxos = append(accountUtxos, accountUtxo)
51 func (w *Wallet) attachUtxos(batch dbm.Batch, b *types.Block, txStatus *bc.TransactionStatus) {
52 for txIndex, tx := range b.Transactions {
53 statusFail, err := txStatus.GetStatus(txIndex)
55 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("attachUtxos fail on get tx status")
59 //hand update the transaction input utxos
60 inputUtxos := txInToUtxos(tx, statusFail)
61 for _, inputUtxo := range inputUtxos {
62 if segwit.IsP2WScript(inputUtxo.ControlProgram) {
63 batch.Delete(account.StandardUTXOKey(inputUtxo.OutputID))
65 batch.Delete(account.ContractUTXOKey(inputUtxo.OutputID))
69 //hand update the transaction output utxos
70 validHeight := uint64(0)
72 validHeight = b.Height + consensus.CoinbasePendingBlockNumber
74 outputUtxos := txOutToUtxos(tx, statusFail, validHeight)
75 utxos := w.filterAccountUtxo(outputUtxos)
76 if err := batchSaveUtxos(utxos, batch); err != nil {
77 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("attachUtxos fail on batchSaveUtxos")
82 func (w *Wallet) detachUtxos(batch dbm.Batch, b *types.Block, txStatus *bc.TransactionStatus) {
83 for txIndex := len(b.Transactions) - 1; txIndex >= 0; txIndex-- {
84 tx := b.Transactions[txIndex]
85 for j := range tx.Outputs {
87 switch resOut := tx.Entries[*tx.ResultIds[j]].(type) {
88 case *bc.IntraChainOutput:
89 code = resOut.ControlProgram.Code
91 code = resOut.ControlProgram.Code
96 if segwit.IsP2WScript(code) {
97 batch.Delete(account.StandardUTXOKey(*tx.ResultIds[j]))
99 batch.Delete(account.ContractUTXOKey(*tx.ResultIds[j]))
103 statusFail, err := txStatus.GetStatus(txIndex)
105 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("detachUtxos fail on get tx status")
109 inputUtxos := txInToUtxos(tx, statusFail)
110 utxos := w.filterAccountUtxo(inputUtxos)
111 if err := batchSaveUtxos(utxos, batch); err != nil {
112 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("detachUtxos fail on batchSaveUtxos")
118 func (w *Wallet) filterAccountUtxo(utxos []*account.UTXO) []*account.UTXO {
119 outsByScript := make(map[string][]*account.UTXO, len(utxos))
120 for _, utxo := range utxos {
121 scriptStr := string(utxo.ControlProgram)
122 outsByScript[scriptStr] = append(outsByScript[scriptStr], utxo)
125 result := make([]*account.UTXO, 0, len(utxos))
126 for s := range outsByScript {
127 if !segwit.IsP2WScript([]byte(s)) {
128 for _, utxo := range outsByScript[s] {
129 result = append(result, utxo)
135 sha3pool.Sum256(hash[:], []byte(s))
136 data := w.DB.Get(account.ContractKey(hash))
141 cp := &account.CtrlProgram{}
142 if err := json.Unmarshal(data, cp); err != nil {
143 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("filterAccountUtxo fail on unmarshal control program")
147 for _, utxo := range outsByScript[s] {
148 utxo.AccountID = cp.AccountID
149 utxo.Address = cp.Address
150 utxo.ControlProgramIndex = cp.KeyIndex
151 utxo.Change = cp.Change
152 result = append(result, utxo)
158 func batchSaveUtxos(utxos []*account.UTXO, batch dbm.Batch) error {
159 for _, utxo := range utxos {
160 data, err := json.Marshal(utxo)
162 return errors.Wrap(err, "failed marshal accountutxo")
165 if segwit.IsP2WScript(utxo.ControlProgram) {
166 batch.Set(account.StandardUTXOKey(utxo.OutputID), data)
168 batch.Set(account.ContractUTXOKey(utxo.OutputID), data)
174 func txInToUtxos(tx *types.Tx, statusFail bool) []*account.UTXO {
175 utxos := []*account.UTXO{}
176 for _, inpID := range tx.Tx.InputIDs {
177 sp, err := tx.Spend(inpID)
182 entryOutput, err := tx.Entry(*sp.SpentOutputId)
184 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("txInToUtxos fail on get entryOutput")
188 utxo := &account.UTXO{}
189 switch resOut := entryOutput.(type) {
190 case *bc.IntraChainOutput:
191 if statusFail && *resOut.Source.Value.AssetId != *consensus.BTMAssetID {
194 utxo = &account.UTXO{
195 OutputID: *sp.SpentOutputId,
196 AssetID: *resOut.Source.Value.AssetId,
197 Amount: resOut.Source.Value.Amount,
198 ControlProgram: resOut.ControlProgram.Code,
199 SourceID: *resOut.Source.Ref,
200 SourcePos: resOut.Source.Position,
204 if statusFail && *resOut.Source.Value.AssetId != *consensus.BTMAssetID {
207 utxo = &account.UTXO{
208 OutputID: *sp.SpentOutputId,
209 AssetID: *resOut.Source.Value.AssetId,
210 Amount: resOut.Source.Value.Amount,
211 ControlProgram: resOut.ControlProgram.Code,
212 SourceID: *resOut.Source.Ref,
213 SourcePos: resOut.Source.Position,
218 log.WithFields(log.Fields{"module": logModule}).Error("txInToUtxos fail on get resOut")
222 utxos = append(utxos, utxo)
227 func txOutToUtxos(tx *types.Tx, statusFail bool, vaildHeight uint64) []*account.UTXO {
228 utxos := []*account.UTXO{}
229 for i, out := range tx.Outputs {
230 entryOutput, err := tx.Entry(*tx.ResultIds[i])
232 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("txOutToUtxos fail on get entryOutput")
236 utxo := &account.UTXO{}
237 switch bcOut := entryOutput.(type) {
238 case *bc.IntraChainOutput:
239 if statusFail && *out.AssetAmount().AssetId != *consensus.BTMAssetID {
242 utxo = &account.UTXO{
243 OutputID: *tx.OutputID(i),
244 AssetID: *out.AssetAmount().AssetId,
245 Amount: out.AssetAmount().Amount,
246 ControlProgram: out.ControlProgram(),
247 SourceID: *bcOut.Source.Ref,
248 SourcePos: bcOut.Source.Position,
249 ValidHeight: vaildHeight,
253 if statusFail && *out.AssetAmount().AssetId != *consensus.BTMAssetID {
256 utxo = &account.UTXO{
257 OutputID: *tx.OutputID(i),
258 AssetID: *out.AssetAmount().AssetId,
259 Amount: out.AssetAmount().Amount,
260 ControlProgram: out.ControlProgram(),
261 SourceID: *bcOut.Source.Ref,
262 SourcePos: bcOut.Source.Position,
263 ValidHeight: vaildHeight,
268 log.WithFields(log.Fields{"module": logModule}).Warn("txOutToUtxos fail on get bcOut")
272 utxos = append(utxos, utxo)