7 log "github.com/sirupsen/logrus"
8 "github.com/tendermint/tmlibs/db"
10 "github.com/vapor/account"
11 "github.com/vapor/consensus"
12 "github.com/vapor/consensus/segwit"
13 "github.com/vapor/crypto/sha3pool"
14 "github.com/vapor/errors"
15 "github.com/vapor/protocol/bc"
16 "github.com/vapor/protocol/bc/types"
17 "github.com/vapor/protocol/vm/vmutil"
20 // GetAccountUtxos return all account unspent outputs
21 func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSmartContract bool) []*account.UTXO {
22 prefix := account.UTXOPreFix
24 prefix = account.SUTXOPrefix
27 accountUtxos := []*account.UTXO{}
29 accountUtxos = w.AccountMgr.ListUnconfirmedUtxo(accountID, isSmartContract)
32 accountUtxoIter := w.DB.IteratorPrefix([]byte(prefix + id))
33 defer accountUtxoIter.Release()
35 for accountUtxoIter.Next() {
36 accountUtxo := &account.UTXO{}
37 if err := json.Unmarshal(accountUtxoIter.Value(), accountUtxo); err != nil {
38 log.WithField("err", err).Warn("GetAccountUtxos fail on unmarshal utxo")
42 if accountID == accountUtxo.AccountID || accountID == "" {
43 accountUtxos = append(accountUtxos, accountUtxo)
49 func (w *Wallet) attachUtxos(batch db.Batch, b *types.Block, txStatus *bc.TransactionStatus) {
52 a.UnmarshalText([]byte("bef9c83e5cadc6dbb80b81387f3e3c3fadd76b917e5337f5442b9ef071c06526"))
53 batch.Delete(account.StandardUTXOKey(a))
54 a.UnmarshalText([]byte("1a5e2141a12823dabf343b5ace0a181a3d018e24f3dc6e7c3704b66fc040ca7b"))
55 batch.Delete(account.StandardUTXOKey(a))
56 a.UnmarshalText([]byte("4647b1e0893f56438f9bbde6134840f1595da799cfc6ece77c4d9aabdf9cfe50"))
57 batch.Delete(account.StandardUTXOKey(a))
58 a.UnmarshalText([]byte("928094d14b00aaf674ee291bbfb0c843a4dab53984f6235b998338fe0fa2d688"))
59 batch.Delete(account.StandardUTXOKey(a))
60 a.UnmarshalText([]byte("e20aee90018f8b6483d5590786fcf495bccfa7f1a3a5a5a9106c4143f71d49a4"))
61 batch.Delete(account.StandardUTXOKey(a))
62 a.UnmarshalText([]byte("2cb18fe2dd3eb8dcf2df43aa6650851dd0b6de291bfffd151a36703c92f8e864"))
63 batch.Delete(account.StandardUTXOKey(a))
64 a.UnmarshalText([]byte("48d71e6da11de69983b0cc79787f0f9422a144c94e687dfec11b4a57fdca2832"))
65 batch.Delete(account.StandardUTXOKey(a))
67 for txIndex, tx := range b.Transactions {
68 statusFail, err := txStatus.GetStatus(txIndex)
70 log.WithField("err", err).Error("attachUtxos fail on get tx status")
74 //hand update the transaction input utxos
75 inputUtxos := txInToUtxos(tx, statusFail)
76 for _, inputUtxo := range inputUtxos {
77 if segwit.IsP2WScript(inputUtxo.ControlProgram) {
78 batch.Delete(account.StandardUTXOKey(inputUtxo.OutputID))
80 batch.Delete(account.ContractUTXOKey(inputUtxo.OutputID))
84 //hand update the transaction output utxos
85 validHeight := uint64(0)
87 validHeight = b.Height + consensus.CoinbasePendingBlockNumber
89 outputUtxos := txOutToUtxos(tx, statusFail, validHeight)
90 utxos := w.filterAccountUtxo(outputUtxos)
91 if err := batchSaveUtxos(utxos, batch); err != nil {
92 log.WithField("err", err).Error("attachUtxos fail on batchSaveUtxos")
97 func (w *Wallet) detachUtxos(batch db.Batch, b *types.Block, txStatus *bc.TransactionStatus) {
98 for txIndex := len(b.Transactions) - 1; txIndex >= 0; txIndex-- {
99 tx := b.Transactions[txIndex]
100 for j := range tx.Outputs {
101 resOut, err := tx.Output(*tx.ResultIds[j])
106 if segwit.IsP2WScript(resOut.ControlProgram.Code) {
107 batch.Delete(account.StandardUTXOKey(*tx.ResultIds[j]))
109 batch.Delete(account.ContractUTXOKey(*tx.ResultIds[j]))
113 statusFail, err := txStatus.GetStatus(txIndex)
115 log.WithField("err", err).Error("detachUtxos fail on get tx status")
119 inputUtxos := txInToUtxos(tx, statusFail)
120 utxos := w.filterAccountUtxo(inputUtxos)
121 if err := batchSaveUtxos(utxos, batch); err != nil {
122 log.WithField("err", err).Error("detachUtxos fail on batchSaveUtxos")
128 func (w *Wallet) filterAccountUtxo(utxos []*account.UTXO) []*account.UTXO {
129 outsByScript := make(map[string][]*account.UTXO, len(utxos))
130 redeemContract := w.dposAddress.ScriptAddress()
131 program, _ := vmutil.P2WPKHProgram(redeemContract)
132 isDposAddress := false
133 for _, utxo := range utxos {
134 scriptStr := string(utxo.ControlProgram)
135 outsByScript[scriptStr] = append(outsByScript[scriptStr], utxo)
138 result := make([]*account.UTXO, 0, len(utxos))
139 for s := range outsByScript {
140 if !segwit.IsP2WScript([]byte(s)) {
141 for _, utxo := range outsByScript[s] {
142 result = append(result, utxo)
148 sha3pool.Sum256(hash[:], []byte(s))
149 data := w.DB.Get(account.ContractKey(hash))
151 if s == string(program) {
160 cp := &account.CtrlProgram{}
161 if err := json.Unmarshal(data, cp); err != nil {
162 log.WithField("err", err).Error("filterAccountUtxo fail on unmarshal control program")
165 for _, utxo := range outsByScript[s] {
166 utxo.AccountID = cp.AccountID
167 utxo.Address = cp.Address
168 utxo.ControlProgramIndex = cp.KeyIndex
169 utxo.Change = cp.Change
170 result = append(result, utxo)
173 for _, utxo := range outsByScript[s] {
174 utxo.Address = w.dposAddress.EncodeAddress()
175 result = append(result, utxo)
177 isDposAddress = false
183 func batchSaveUtxos(utxos []*account.UTXO, batch db.Batch) error {
184 for _, utxo := range utxos {
185 data, err := json.Marshal(utxo)
187 return errors.Wrap(err, "failed marshal accountutxo")
190 if segwit.IsP2WScript(utxo.ControlProgram) {
191 batch.Set(account.StandardUTXOKey(utxo.OutputID), data)
193 batch.Set(account.ContractUTXOKey(utxo.OutputID), data)
199 func txInToUtxos(tx *types.Tx, statusFail bool) []*account.UTXO {
200 utxos := []*account.UTXO{}
201 for _, inpID := range tx.Tx.InputIDs {
202 sp, err := tx.Spend(inpID)
206 resOut, err := tx.Output(*sp.SpentOutputId)
208 log.WithField("err", err).Error("txInToUtxos fail on get resOut")
212 if statusFail && *resOut.Source.Value.AssetId != *consensus.BTMAssetID {
213 fmt.Println("statusFail:", statusFail)
217 utxos = append(utxos, &account.UTXO{
218 OutputID: *sp.SpentOutputId,
219 AssetID: *resOut.Source.Value.AssetId,
220 Amount: resOut.Source.Value.Amount,
221 ControlProgram: resOut.ControlProgram.Code,
222 SourceID: *resOut.Source.Ref,
223 SourcePos: resOut.Source.Position,
229 func txOutToUtxos(tx *types.Tx, statusFail bool, vaildHeight uint64) []*account.UTXO {
230 utxos := []*account.UTXO{}
231 for i, out := range tx.Outputs {
232 bcOut, err := tx.Output(*tx.ResultIds[i])
237 if statusFail && *out.AssetAmount.AssetId != *consensus.BTMAssetID {
241 utxos = append(utxos, &account.UTXO{
242 OutputID: *tx.OutputID(i),
243 AssetID: *out.AssetAmount.AssetId,
245 ControlProgram: out.ControlProgram,
246 SourceID: *bcOut.Source.Ref,
247 SourcePos: bcOut.Source.Position,
248 ValidHeight: vaildHeight,