4 "github.com/vapor/consensus"
5 "github.com/vapor/database/storage"
6 "github.com/vapor/errors"
7 "github.com/vapor/protocol/bc"
10 // UtxoViewpoint represents a view into the set of unspent transaction outputs
11 type UtxoViewpoint struct {
12 Entries map[bc.Hash]*storage.UtxoEntry
15 // NewUtxoViewpoint returns a new empty unspent transaction output view.
16 func NewUtxoViewpoint() *UtxoViewpoint {
17 return &UtxoViewpoint{
18 Entries: make(map[bc.Hash]*storage.UtxoEntry),
22 func (view *UtxoViewpoint) ApplyTransaction(block *bc.Block, tx *bc.Tx, statusFail bool) error {
23 for _, prevout := range tx.MainchainOutputIDs {
24 entry, ok := view.Entries[prevout]
26 return errors.New("fail to find mainchain output entry")
29 if entry.Type != storage.CrosschainUTXOType {
30 return errors.New("look up mainchainOutputID but find utxo not from mainchain")
34 return errors.New("mainchain output has been spent")
37 entry.BlockHeight = block.Height
41 for _, prevout := range tx.SpentOutputIDs {
42 assetID := bc.AssetID{}
43 entryOutput, err := tx.Entry(prevout)
48 switch output := entryOutput.(type) {
49 case *bc.IntraChainOutput:
50 assetID = *output.Source.Value.AssetId
52 assetID = *output.Source.Value.AssetId
54 return errors.Wrapf(bc.ErrEntryType, "entry %x has unexpected type %T", prevout.Bytes(), entryOutput)
57 if statusFail && assetID != *consensus.BTMAssetID {
61 entry, ok := view.Entries[prevout]
63 return errors.New("fail to find utxo entry")
65 if entry.Type == storage.CrosschainUTXOType {
66 return errors.New("look up spentOutputID but find utxo from mainchain")
69 return errors.New("utxo has been spent")
71 if (entry.Type == storage.CoinbaseUTXOType) && ((entry.BlockHeight + consensus.CoinbasePendingBlockNumber) > block.Height) {
72 return errors.New("coinbase utxo is not ready for use")
77 for _, id := range tx.TxHeader.ResultIds {
78 assetID := bc.AssetID{}
79 entryOutput, err := tx.Entry(*id)
84 switch output := entryOutput.(type) {
85 case *bc.IntraChainOutput:
86 assetID = *output.Source.Value.AssetId
88 assetID = *output.Source.Value.AssetId
90 // due to it's a retirement, utxo doesn't care this output type so skip it
94 if statusFail && assetID != *consensus.BTMAssetID {
98 utxoType := storage.NormalUTXOType
99 if block != nil && len(block.Transactions) > 0 && block.Transactions[0].ID == tx.ID {
100 utxoType = storage.CoinbaseUTXOType
102 view.Entries[*id] = storage.NewUtxoEntry(utxoType, block.Height, false)
107 func (view *UtxoViewpoint) ApplyBlock(block *bc.Block, txStatus *bc.TransactionStatus) error {
108 for i, tx := range block.Transactions {
109 statusFail, err := txStatus.GetStatus(i)
113 if err := view.ApplyTransaction(block, tx, statusFail); err != nil {
120 func (view *UtxoViewpoint) CanSpend(hash *bc.Hash) bool {
121 entry := view.Entries[*hash]
122 return entry != nil && !entry.Spent
125 func (view *UtxoViewpoint) DetachTransaction(tx *bc.Tx, statusFail bool) error {
126 for _, prevout := range tx.MainchainOutputIDs {
127 // don't simply delete(view.Entries, prevout), because we need to delete from db in saveUtxoView()
128 entry, ok := view.Entries[prevout]
129 if ok && (entry.Type != storage.CrosschainUTXOType) {
130 return errors.New("look up mainchainOutputID but find utxo not from mainchain")
133 if ok && !entry.Spent {
134 return errors.New("try to revert an unspent utxo")
138 view.Entries[prevout] = storage.NewUtxoEntry(storage.CrosschainUTXOType, 0, false)
141 entry.UnspendOutput()
144 for _, prevout := range tx.SpentOutputIDs {
145 assetID := bc.AssetID{}
146 entryOutput, err := tx.Entry(prevout)
151 switch output := entryOutput.(type) {
152 case *bc.IntraChainOutput:
153 assetID = *output.Source.Value.AssetId
155 assetID = *output.Source.Value.AssetId
157 return errors.Wrapf(bc.ErrEntryType, "entry %x has unexpected type %T", prevout.Bytes(), entryOutput)
160 if statusFail && assetID != *consensus.BTMAssetID {
164 entry, ok := view.Entries[prevout]
165 if ok && (entry.Type == storage.CrosschainUTXOType) {
166 return errors.New("look up SpentOutputIDs but find mainchain utxo")
169 if ok && !entry.Spent {
170 return errors.New("try to revert an unspent utxo")
174 view.Entries[prevout] = storage.NewUtxoEntry(storage.NormalUTXOType, 0, false)
177 entry.UnspendOutput()
180 for _, id := range tx.TxHeader.ResultIds {
181 assetID := bc.AssetID{}
182 entryOutput, err := tx.Entry(*id)
187 switch output := entryOutput.(type) {
188 case *bc.IntraChainOutput:
189 assetID = *output.Source.Value.AssetId
191 assetID = *output.Source.Value.AssetId
193 // due to it's a retirement, utxo doesn't care this output type so skip it
197 if statusFail && assetID != *consensus.BTMAssetID {
201 view.Entries[*id] = storage.NewUtxoEntry(storage.NormalUTXOType, 0, true)
206 func (view *UtxoViewpoint) DetachBlock(block *bc.Block, txStatus *bc.TransactionStatus) error {
207 for i := len(block.Transactions) - 1; i >= 0; i-- {
208 statusFail, err := txStatus.GetStatus(i)
212 if err := view.DetachTransaction(block.Transactions[i], statusFail); err != nil {
219 func (view *UtxoViewpoint) HasUtxo(hash *bc.Hash) bool {
220 _, ok := view.Entries[*hash]