/
utxo_view.go
266 lines (224 loc) · 6.51 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package state
import (
"github.com/bytom/vapor/consensus"
"github.com/bytom/vapor/database/storage"
"github.com/bytom/vapor/errors"
"github.com/bytom/vapor/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 {
if err := view.applyCrossChainUtxo(block, tx); err != nil {
return err
}
if err := view.applySpendUtxo(block, tx, statusFail); err != nil {
return err
}
return view.applyOutputUtxo(block, tx, statusFail)
}
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 {
if err := view.detachCrossChainUtxo(tx); err != nil {
return err
}
if err := view.detachSpendUtxo(tx, statusFail); err != nil {
return err
}
return view.detachOutputUtxo(tx, statusFail)
}
func (view *UtxoViewpoint) DetachBlock(block *bc.Block, txStatus *bc.TransactionStatus) error {
for i := len(block.Transactions) - 1; i >= 0; i-- {
statusFail, err := txStatus.GetStatus(i)
if err != nil {
return err
}
if err := view.DetachTransaction(block.Transactions[i], statusFail); err != nil {
return err
}
}
return nil
}
func (view *UtxoViewpoint) HasUtxo(hash *bc.Hash) bool {
_, ok := view.Entries[*hash]
return ok
}
func (view *UtxoViewpoint) applyCrossChainUtxo(block *bc.Block, tx *bc.Tx) error {
for _, prevout := range tx.MainchainOutputIDs {
entry, ok := view.Entries[prevout]
if !ok {
return errors.New("fail to find mainchain output entry")
}
if entry.Spent {
return errors.New("mainchain output has been spent")
}
entry.BlockHeight = block.Height
entry.SpendOutput()
}
return nil
}
func (view *UtxoViewpoint) applyOutputUtxo(block *bc.Block, tx *bc.Tx, statusFail bool) error {
for _, id := range tx.TxHeader.ResultIds {
entryOutput, err := tx.Entry(*id)
if err != nil {
return err
}
var assetID bc.AssetID
utxoType := storage.NormalUTXOType
switch output := entryOutput.(type) {
case *bc.IntraChainOutput:
if output.Source.Value.Amount == uint64(0) {
continue
}
assetID = *output.Source.Value.AssetId
case *bc.VoteOutput:
assetID = *output.Source.Value.AssetId
utxoType = storage.VoteUTXOType
default:
// due to it's a retirement, utxo doesn't care this output type so skip it
continue
}
if statusFail && assetID != *consensus.BTMAssetID {
continue
}
if block != nil && len(block.Transactions) > 0 && block.Transactions[0].ID == tx.ID {
utxoType = storage.CoinbaseUTXOType
}
view.Entries[*id] = storage.NewUtxoEntry(utxoType, block.Height, false)
}
return nil
}
func (view *UtxoViewpoint) applySpendUtxo(block *bc.Block, tx *bc.Tx, statusFail bool) error {
for _, prevout := range tx.SpentOutputIDs {
entryOutput, err := tx.Entry(prevout)
if err != nil {
return err
}
var assetID bc.AssetID
switch output := entryOutput.(type) {
case *bc.IntraChainOutput:
assetID = *output.Source.Value.AssetId
case *bc.VoteOutput:
assetID = *output.Source.Value.AssetId
default:
return errors.Wrapf(bc.ErrEntryType, "entry %x has unexpected type %T", prevout.Bytes(), entryOutput)
}
if statusFail && 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")
}
switch entry.Type {
case storage.CoinbaseUTXOType:
if (entry.BlockHeight + consensus.ActiveNetParams.CoinbasePendingBlockNumber) > block.Height {
return errors.New("coinbase utxo is not ready for use")
}
case storage.VoteUTXOType:
if (entry.BlockHeight + consensus.ActiveNetParams.VotePendingBlockNumber) > block.Height {
return errors.New("Coin is within the voting lock time")
}
}
entry.SpendOutput()
}
return nil
}
func (view *UtxoViewpoint) detachCrossChainUtxo(tx *bc.Tx) error {
for _, prevout := range tx.MainchainOutputIDs {
entry, ok := view.Entries[prevout]
if !ok {
return errors.New("fail to find mainchain output entry")
}
if !entry.Spent {
return errors.New("mainchain output is unspent")
}
entry.UnspendOutput()
}
return nil
}
func (view *UtxoViewpoint) detachOutputUtxo(tx *bc.Tx, statusFail bool) error {
for _, id := range tx.TxHeader.ResultIds {
entryOutput, err := tx.Entry(*id)
if err != nil {
return err
}
var assetID bc.AssetID
utxoType := storage.NormalUTXOType
switch output := entryOutput.(type) {
case *bc.IntraChainOutput:
if output.Source.Value.Amount == uint64(0) {
continue
}
assetID = *output.Source.Value.AssetId
case *bc.VoteOutput:
assetID = *output.Source.Value.AssetId
utxoType = storage.VoteUTXOType
default:
// due to it's a retirement, utxo doesn't care this output type so skip it
continue
}
if statusFail && assetID != *consensus.BTMAssetID {
continue
}
view.Entries[*id] = storage.NewUtxoEntry(utxoType, 0, true)
}
return nil
}
func (view *UtxoViewpoint) detachSpendUtxo(tx *bc.Tx, statusFail bool) error {
for _, prevout := range tx.SpentOutputIDs {
entryOutput, err := tx.Entry(prevout)
if err != nil {
return err
}
var assetID bc.AssetID
utxoType := storage.NormalUTXOType
switch output := entryOutput.(type) {
case *bc.IntraChainOutput:
assetID = *output.Source.Value.AssetId
case *bc.VoteOutput:
assetID = *output.Source.Value.AssetId
utxoType = storage.VoteUTXOType
default:
return errors.Wrapf(bc.ErrEntryType, "entry %x has unexpected type %T", prevout.Bytes(), entryOutput)
}
if statusFail && 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(utxoType, 0, false)
continue
}
entry.UnspendOutput()
}
return nil
}