forked from kaspanet/kaspad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utxo_iterator.go
79 lines (69 loc) · 1.91 KB
/
utxo_iterator.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
package utxo
import (
"github.com/pkg/errors"
"github.com/zoomy-network/zoomyd/domain/consensus/model/externalapi"
)
type utxoOutpointEntryPair struct {
outpoint externalapi.DomainOutpoint
entry externalapi.UTXOEntry
}
type utxoCollectionIterator struct {
index int
pairs []utxoOutpointEntryPair
isClosed bool
}
func (uc utxoCollection) Iterator() externalapi.ReadOnlyUTXOSetIterator {
pairs := make([]utxoOutpointEntryPair, len(uc))
i := 0
for outpoint, entry := range uc {
pairs[i] = utxoOutpointEntryPair{
outpoint: outpoint,
entry: entry,
}
i++
}
return &utxoCollectionIterator{index: -1, pairs: pairs}
}
func (uci *utxoCollectionIterator) First() bool {
if uci.isClosed {
panic("Tried using a closed utxoCollectionIterator")
}
uci.index = 0
return len(uci.pairs) > 0
}
func (uci *utxoCollectionIterator) Next() bool {
if uci.isClosed {
panic("Tried using a closed utxoCollectionIterator")
}
uci.index++
return uci.index < len(uci.pairs)
}
func (uci *utxoCollectionIterator) Get() (outpoint *externalapi.DomainOutpoint, utxoEntry externalapi.UTXOEntry, err error) {
if uci.isClosed {
return nil, nil, errors.New("Tried using a closed utxoCollectionIterator")
}
pair := uci.pairs[uci.index]
return &pair.outpoint, pair.entry, nil
}
func (uci *utxoCollectionIterator) WithDiff(diff externalapi.UTXODiff) (externalapi.ReadOnlyUTXOSetIterator, error) {
if uci.isClosed {
return nil, errors.New("Tried using a closed utxoCollectionIterator")
}
d, ok := diff.(*immutableUTXODiff)
if !ok {
return nil, errors.New("diff is not of type *immutableUTXODiff")
}
return &readOnlyUTXOIteratorWithDiff{
baseIterator: uci,
diff: d,
toAddIterator: diff.ToAdd().Iterator(),
}, nil
}
func (uci *utxoCollectionIterator) Close() error {
if uci.isClosed {
return errors.New("Tried using a closed utxoCollectionIterator")
}
uci.isClosed = true
uci.pairs = nil
return nil
}