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")
67 return errors.New("utxo has been spent")
71 case storage.CrosschainUTXOType:
72 return errors.New("look up spentOutputID but find utxo from mainchain")
74 case storage.CoinbaseUTXOType:
75 if (entry.BlockHeight + consensus.CoinbasePendingBlockNumber) > block.Height {
76 return errors.New("coinbase utxo is not ready for use")
79 case storage.VoteUTXOType:
80 if (entry.BlockHeight + consensus.VotePendingBlockNumber) > block.Height {
81 return errors.New("Coin is within the voting lock time")
88 for _, id := range tx.TxHeader.ResultIds {
89 assetID := bc.AssetID{}
90 entryOutput, err := tx.Entry(*id)
95 utxoType := storage.NormalUTXOType
97 switch output := entryOutput.(type) {
98 case *bc.IntraChainOutput:
99 assetID = *output.Source.Value.AssetId
101 assetID = *output.Source.Value.AssetId
102 utxoType = storage.VoteUTXOType
104 // due to it's a retirement, utxo doesn't care this output type so skip it
108 if statusFail && assetID != *consensus.BTMAssetID {
112 if block != nil && len(block.Transactions) > 0 && block.Transactions[0].ID == tx.ID {
113 utxoType = storage.CoinbaseUTXOType
115 view.Entries[*id] = storage.NewUtxoEntry(utxoType, block.Height, false)
120 func (view *UtxoViewpoint) ApplyBlock(block *bc.Block, txStatus *bc.TransactionStatus) error {
121 for i, tx := range block.Transactions {
122 statusFail, err := txStatus.GetStatus(i)
126 if err := view.ApplyTransaction(block, tx, statusFail); err != nil {
133 func (view *UtxoViewpoint) CanSpend(hash *bc.Hash) bool {
134 entry := view.Entries[*hash]
135 return entry != nil && !entry.Spent
138 func (view *UtxoViewpoint) DetachTransaction(tx *bc.Tx, statusFail bool) error {
139 for _, prevout := range tx.MainchainOutputIDs {
140 // don't simply delete(view.Entries, prevout), because we need to delete from db in saveUtxoView()
141 entry, ok := view.Entries[prevout]
142 if ok && (entry.Type != storage.CrosschainUTXOType) {
143 return errors.New("look up mainchainOutputID but find utxo not from mainchain")
146 if ok && !entry.Spent {
147 return errors.New("try to revert an unspent utxo")
151 view.Entries[prevout] = storage.NewUtxoEntry(storage.CrosschainUTXOType, 0, false)
154 entry.UnspendOutput()
157 for _, prevout := range tx.SpentOutputIDs {
158 assetID := bc.AssetID{}
159 entryOutput, err := tx.Entry(prevout)
164 utxoType := storage.NormalUTXOType
165 switch output := entryOutput.(type) {
166 case *bc.IntraChainOutput:
167 assetID = *output.Source.Value.AssetId
169 assetID = *output.Source.Value.AssetId
170 utxoType = storage.VoteUTXOType
172 return errors.Wrapf(bc.ErrEntryType, "entry %x has unexpected type %T", prevout.Bytes(), entryOutput)
175 if statusFail && assetID != *consensus.BTMAssetID {
179 entry, ok := view.Entries[prevout]
180 if ok && (entry.Type == storage.CrosschainUTXOType) {
181 return errors.New("look up SpentOutputIDs but find mainchain utxo")
184 if ok && !entry.Spent {
185 return errors.New("try to revert an unspent utxo")
189 view.Entries[prevout] = storage.NewUtxoEntry(utxoType, 0, false)
192 entry.UnspendOutput()
195 for _, id := range tx.TxHeader.ResultIds {
196 assetID := bc.AssetID{}
197 entryOutput, err := tx.Entry(*id)
202 utxoType := storage.NormalUTXOType
203 switch output := entryOutput.(type) {
204 case *bc.IntraChainOutput:
205 assetID = *output.Source.Value.AssetId
207 assetID = *output.Source.Value.AssetId
208 utxoType = storage.VoteUTXOType
210 // due to it's a retirement, utxo doesn't care this output type so skip it
214 if statusFail && assetID != *consensus.BTMAssetID {
218 view.Entries[*id] = storage.NewUtxoEntry(utxoType, 0, true)
223 func (view *UtxoViewpoint) DetachBlock(block *bc.Block, txStatus *bc.TransactionStatus) error {
224 for i := len(block.Transactions) - 1; i >= 0; i-- {
225 statusFail, err := txStatus.GetStatus(i)
229 if err := view.DetachTransaction(block.Transactions[i], statusFail); err != nil {
236 func (view *UtxoViewpoint) HasUtxo(hash *bc.Hash) bool {
237 _, ok := view.Entries[*hash]