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 outputUtxos := txOutToUtxos(tx, statusFail, b.Height)
71 utxos := w.filterAccountUtxo(outputUtxos)
72 if err := batchSaveUtxos(utxos, batch); err != nil {
73 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("attachUtxos fail on batchSaveUtxos")
78 func (w *Wallet) detachUtxos(batch dbm.Batch, b *types.Block, txStatus *bc.TransactionStatus) {
79 for txIndex := len(b.Transactions) - 1; txIndex >= 0; txIndex-- {
80 tx := b.Transactions[txIndex]
81 for j := range tx.Outputs {
83 switch resOut := tx.Entries[*tx.ResultIds[j]].(type) {
84 case *bc.IntraChainOutput:
85 code = resOut.ControlProgram.Code
87 code = resOut.ControlProgram.Code
92 if segwit.IsP2WScript(code) {
93 batch.Delete(account.StandardUTXOKey(*tx.ResultIds[j]))
95 batch.Delete(account.ContractUTXOKey(*tx.ResultIds[j]))
99 statusFail, err := txStatus.GetStatus(txIndex)
101 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("detachUtxos fail on get tx status")
105 inputUtxos := txInToUtxos(tx, statusFail)
106 utxos := w.filterAccountUtxo(inputUtxos)
107 if err := batchSaveUtxos(utxos, batch); err != nil {
108 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("detachUtxos fail on batchSaveUtxos")
114 func (w *Wallet) filterAccountUtxo(utxos []*account.UTXO) []*account.UTXO {
115 outsByScript := make(map[string][]*account.UTXO, len(utxos))
116 for _, utxo := range utxos {
117 scriptStr := string(utxo.ControlProgram)
118 outsByScript[scriptStr] = append(outsByScript[scriptStr], utxo)
121 result := make([]*account.UTXO, 0, len(utxos))
122 for s := range outsByScript {
123 if !segwit.IsP2WScript([]byte(s)) {
124 for _, utxo := range outsByScript[s] {
125 result = append(result, utxo)
131 sha3pool.Sum256(hash[:], []byte(s))
132 data := w.DB.Get(account.ContractKey(hash))
137 cp := &account.CtrlProgram{}
138 if err := json.Unmarshal(data, cp); err != nil {
139 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("filterAccountUtxo fail on unmarshal control program")
143 for _, utxo := range outsByScript[s] {
144 utxo.AccountID = cp.AccountID
145 utxo.Address = cp.Address
146 utxo.ControlProgramIndex = cp.KeyIndex
147 utxo.Change = cp.Change
148 result = append(result, utxo)
154 func batchSaveUtxos(utxos []*account.UTXO, batch dbm.Batch) error {
155 for _, utxo := range utxos {
156 data, err := json.Marshal(utxo)
158 return errors.Wrap(err, "failed marshal accountutxo")
161 if segwit.IsP2WScript(utxo.ControlProgram) {
162 batch.Set(account.StandardUTXOKey(utxo.OutputID), data)
164 batch.Set(account.ContractUTXOKey(utxo.OutputID), data)
170 func txInToUtxos(tx *types.Tx, statusFail bool) []*account.UTXO {
171 utxos := []*account.UTXO{}
172 for _, inpID := range tx.Tx.InputIDs {
174 e, err := tx.Entry(inpID)
178 utxo := &account.UTXO{}
179 switch inp := e.(type) {
181 resOut, err := tx.IntraChainOutput(*inp.SpentOutputId)
183 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("txInToUtxos fail on get resOut for spedn")
186 if statusFail && *resOut.Source.Value.AssetId != *consensus.BTMAssetID {
189 utxo = &account.UTXO{
190 OutputID: *inp.SpentOutputId,
191 AssetID: *resOut.Source.Value.AssetId,
192 Amount: resOut.Source.Value.Amount,
193 ControlProgram: resOut.ControlProgram.Code,
194 SourceID: *resOut.Source.Ref,
195 SourcePos: resOut.Source.Position,
198 resOut, err := tx.VoteOutput(*inp.SpentOutputId)
200 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("txInToUtxos fail on get resOut for vetoInput")
203 if statusFail && *resOut.Source.Value.AssetId != *consensus.BTMAssetID {
206 utxo = &account.UTXO{
207 OutputID: *inp.SpentOutputId,
208 AssetID: *resOut.Source.Value.AssetId,
209 Amount: resOut.Source.Value.Amount,
210 ControlProgram: resOut.ControlProgram.Code,
211 SourceID: *resOut.Source.Ref,
212 SourcePos: resOut.Source.Position,
216 log.WithFields(log.Fields{"module": logModule, "err": errors.Wrapf(bc.ErrEntryType, "entry %x has unexpected type %T", inpID.Bytes(), e)}).Error("txInToUtxos fail on get resOut")
219 utxos = append(utxos, utxo)
224 func txOutToUtxos(tx *types.Tx, statusFail bool, blockHeight uint64) []*account.UTXO {
225 validHeight := uint64(0)
226 if tx.Inputs[0].InputType() == types.CoinbaseInputType {
227 validHeight = blockHeight + consensus.CoinbasePendingBlockNumber
230 utxos := []*account.UTXO{}
231 for i, out := range tx.Outputs {
232 entryOutput, err := tx.Entry(*tx.ResultIds[i])
234 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("txOutToUtxos fail on get entryOutput")
238 utxo := &account.UTXO{}
239 switch bcOut := entryOutput.(type) {
240 case *bc.IntraChainOutput:
241 if statusFail && *out.AssetAmount().AssetId != *consensus.BTMAssetID {
244 utxo = &account.UTXO{
245 OutputID: *tx.OutputID(i),
246 AssetID: *out.AssetAmount().AssetId,
247 Amount: out.AssetAmount().Amount,
248 ControlProgram: out.ControlProgram(),
249 SourceID: *bcOut.Source.Ref,
250 SourcePos: bcOut.Source.Position,
251 ValidHeight: validHeight,
255 if statusFail && *out.AssetAmount().AssetId != *consensus.BTMAssetID {
259 voteValidHeight := blockHeight + consensus.VotePendingBlockNumber
260 if validHeight < voteValidHeight {
261 validHeight = voteValidHeight
264 utxo = &account.UTXO{
265 OutputID: *tx.OutputID(i),
266 AssetID: *out.AssetAmount().AssetId,
267 Amount: out.AssetAmount().Amount,
268 ControlProgram: out.ControlProgram(),
269 SourceID: *bcOut.Source.Ref,
270 SourcePos: bcOut.Source.Position,
271 ValidHeight: validHeight,
276 log.WithFields(log.Fields{"module": logModule}).Warn("txOutToUtxos fail on get bcOut")
280 utxos = append(utxos, utxo)