-
Notifications
You must be signed in to change notification settings - Fork 0
/
utxo_view.go
135 lines (119 loc) · 3.36 KB
/
utxo_view.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package state
import (
"errors"
"github.com/bytom-gm/consensus"
"github.com/bytom-gm/database/storage"
"github.com/bytom-gm/protocol/bc"
)
// UtxoViewpoint represents a view into the set of unspent transaction outputs
type UtxoViewpoint struct {
Entries map[bc.Hash]*storage.UtxoEntry
}
// NewUtxoViewpoint returns a new empty unspent transaction output view.
func NewUtxoViewpoint() *UtxoViewpoint {
return &UtxoViewpoint{
Entries: make(map[bc.Hash]*storage.UtxoEntry),
}
}
func (view *UtxoViewpoint) ApplyTransaction(block *bc.Block, tx *bc.Tx, statusFail bool) error {
for _, prevout := range tx.SpentOutputIDs {
spentOutput, err := tx.Output(prevout)
if err != nil {
return err
}
if statusFail && *spentOutput.Source.Value.AssetId != *consensus.BTMAssetID {
continue
}
entry, ok := view.Entries[prevout]
if !ok {
return errors.New("fail to find utxo entry")
}
if entry.Spent {
return errors.New("utxo has been spent")
}
if entry.IsCoinBase && entry.BlockHeight+consensus.CoinbasePendingBlockNumber > block.Height {
return errors.New("coinbase utxo is not ready for use")
}
entry.SpendOutput()
}
for _, id := range tx.TxHeader.ResultIds {
output, err := tx.Output(*id)
if err != nil {
// error due to it's a retirement, utxo doesn't care this output type so skip it
continue
}
if statusFail && *output.Source.Value.AssetId != *consensus.BTMAssetID {
continue
}
isCoinbase := false
if block != nil && len(block.Transactions) > 0 && block.Transactions[0].ID == tx.ID {
isCoinbase = true
}
view.Entries[*id] = storage.NewUtxoEntry(isCoinbase, block.Height, false)
}
return nil
}
func (view *UtxoViewpoint) ApplyBlock(block *bc.Block, txStatus *bc.TransactionStatus) error {
for i, tx := range block.Transactions {
statusFail, err := txStatus.GetStatus(i)
if err != nil {
return err
}
if err := view.ApplyTransaction(block, tx, statusFail); err != nil {
return err
}
}
return nil
}
func (view *UtxoViewpoint) CanSpend(hash *bc.Hash) bool {
entry := view.Entries[*hash]
return entry != nil && !entry.Spent
}
func (view *UtxoViewpoint) DetachTransaction(tx *bc.Tx, statusFail bool) error {
for _, prevout := range tx.SpentOutputIDs {
spentOutput, err := tx.Output(prevout)
if err != nil {
return err
}
if statusFail && *spentOutput.Source.Value.AssetId != *consensus.BTMAssetID {
continue
}
entry, ok := view.Entries[prevout]
if ok && !entry.Spent {
return errors.New("try to revert an unspent utxo")
}
if !ok {
view.Entries[prevout] = storage.NewUtxoEntry(false, 0, false)
continue
}
entry.UnspendOutput()
}
for _, id := range tx.TxHeader.ResultIds {
output, err := tx.Output(*id)
if err != nil {
// error due to it's a retirement, utxo doesn't care this output type so skip it
continue
}
if statusFail && *output.Source.Value.AssetId != *consensus.BTMAssetID {
continue
}
view.Entries[*id] = storage.NewUtxoEntry(false, 0, true)
}
return nil
}
func (view *UtxoViewpoint) DetachBlock(block *bc.Block, txStatus *bc.TransactionStatus) error {
for i, tx := range block.Transactions {
statusFail, err := txStatus.GetStatus(i)
if err != nil {
return err
}
if err := view.DetachTransaction(tx, statusFail); err != nil {
return err
}
}
return nil
}
func (view *UtxoViewpoint) HasUtxo(hash *bc.Hash) bool {
_, ok := view.Entries[*hash]
return ok
}