-
Notifications
You must be signed in to change notification settings - Fork 1
/
utxo_collection.go
executable file
·93 lines (74 loc) · 2.67 KB
/
utxo_collection.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
package utxo
import (
"fmt"
"sort"
"strings"
"github.com/shatll-s/nexelliad/domain/consensus/model/externalapi"
)
type utxoCollection map[externalapi.DomainOutpoint]externalapi.UTXOEntry
// NewUTXOCollection creates a UTXO-Collection from the given map from outpoint to UTXOEntry
func NewUTXOCollection(utxoMap map[externalapi.DomainOutpoint]externalapi.UTXOEntry) externalapi.UTXOCollection {
return utxoCollection(utxoMap)
}
// Get returns the model.UTXOEntry represented by provided outpoint,
// and a boolean value indicating if said model.UTXOEntry is in the set or not
func (uc utxoCollection) Get(outpoint *externalapi.DomainOutpoint) (externalapi.UTXOEntry, bool) {
entry, ok := uc[*outpoint]
return entry, ok
}
// Contains returns a boolean value indicating whether a UTXO entry is in the set
func (uc utxoCollection) Contains(outpoint *externalapi.DomainOutpoint) bool {
_, ok := uc[*outpoint]
return ok
}
func (uc utxoCollection) Len() int {
return len(uc)
}
func (uc utxoCollection) Clone() utxoCollection {
if uc == nil {
return nil
}
clone := make(utxoCollection, len(uc))
for outpoint, entry := range uc {
clone[outpoint] = entry
}
return clone
}
func (uc utxoCollection) String() string {
utxoStrings := make([]string, len(uc))
i := 0
for outpoint, utxoEntry := range uc {
utxoStrings[i] = fmt.Sprintf("(%s, %d) => %d, daaScore: %d",
outpoint.TransactionID, outpoint.Index, utxoEntry.Amount(), utxoEntry.BlockDAAScore())
i++
}
// Sort strings for determinism.
sort.Strings(utxoStrings)
return fmt.Sprintf("[ %s ]", strings.Join(utxoStrings, ", "))
}
// add adds a new UTXO entry to this collection
func (uc utxoCollection) add(outpoint *externalapi.DomainOutpoint, entry externalapi.UTXOEntry) {
uc[*outpoint] = entry
}
// addMultiple adds multiple UTXO entries to this collection
func (uc utxoCollection) addMultiple(collectionToAdd utxoCollection) {
for outpoint, entry := range collectionToAdd {
uc[outpoint] = entry
}
}
// remove removes a UTXO entry from this collection if it exists
func (uc utxoCollection) remove(outpoint *externalapi.DomainOutpoint) {
delete(uc, *outpoint)
}
// removeMultiple removes multiple UTXO entries from this collection if it exists
func (uc utxoCollection) removeMultiple(collectionToRemove utxoCollection) {
for outpoint := range collectionToRemove {
delete(uc, outpoint)
}
}
// containsWithDAAScore returns a boolean value indicating whether a model.UTXOEntry
// is in the set and its DAA score is equal to the given DAA score.
func (uc utxoCollection) containsWithDAAScore(outpoint *externalapi.DomainOutpoint, daaScore uint64) bool {
entry, ok := uc.Get(outpoint)
return ok && entry.BlockDAAScore() == daaScore
}