-
Notifications
You must be signed in to change notification settings - Fork 1
/
immutable_utxo_diff.go
executable file
·103 lines (84 loc) · 2.54 KB
/
immutable_utxo_diff.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
package utxo
import (
"github.com/shatll-s/nexelliad/domain/consensus/model/externalapi"
"github.com/pkg/errors"
)
type immutableUTXODiff struct {
mutableUTXODiff *mutableUTXODiff
isInvalidated bool
}
func (iud *immutableUTXODiff) ToAdd() externalapi.UTXOCollection {
if iud.isInvalidated {
panic(errors.New("Attempt to read from an invalidated UTXODiff"))
}
return iud.mutableUTXODiff.ToAdd()
}
func (iud *immutableUTXODiff) ToRemove() externalapi.UTXOCollection {
if iud.isInvalidated {
panic(errors.New("Attempt to read from an invalidated UTXODiff"))
}
return iud.mutableUTXODiff.ToRemove()
}
func (iud *immutableUTXODiff) WithDiff(other externalapi.UTXODiff) (externalapi.UTXODiff, error) {
if iud.isInvalidated {
panic(errors.New("Attempt to read from an invalidated UTXODiff"))
}
return iud.mutableUTXODiff.WithDiff(other)
}
func (iud *immutableUTXODiff) DiffFrom(other externalapi.UTXODiff) (externalapi.UTXODiff, error) {
if iud.isInvalidated {
panic(errors.New("Attempt to read from an invalidated UTXODiff"))
}
return iud.mutableUTXODiff.DiffFrom(other)
}
// NewUTXODiff creates an empty UTXODiff
func NewUTXODiff() externalapi.UTXODiff {
return newUTXODiff()
}
func newUTXODiff() *immutableUTXODiff {
return &immutableUTXODiff{
mutableUTXODiff: newMutableUTXODiff(),
isInvalidated: false,
}
}
// NewUTXODiffFromCollections returns a new UTXODiff with the given toAdd and toRemove collections
func NewUTXODiffFromCollections(toAdd, toRemove externalapi.UTXOCollection) (externalapi.UTXODiff, error) {
add, ok := toAdd.(utxoCollection)
if !ok {
return nil, errors.New("toAdd is not of type utxoCollection")
}
remove, ok := toRemove.(utxoCollection)
if !ok {
return nil, errors.New("toRemove is not of type utxoCollection")
}
return &immutableUTXODiff{
mutableUTXODiff: &mutableUTXODiff{
toAdd: add,
toRemove: remove,
},
}, nil
}
func (iud *immutableUTXODiff) CloneMutable() externalapi.MutableUTXODiff {
if iud.isInvalidated {
panic(errors.New("Attempt to read from an invalidated UTXODiff"))
}
return iud.cloneMutable()
}
func (iud *immutableUTXODiff) Reversed() externalapi.UTXODiff {
if iud.isInvalidated {
panic(errors.New("Attempt to read from an invalidated UTXODiff"))
}
return &immutableUTXODiff{
mutableUTXODiff: iud.mutableUTXODiff.Reversed(),
isInvalidated: false,
}
}
func (iud *immutableUTXODiff) cloneMutable() *mutableUTXODiff {
if iud == nil {
return nil
}
return iud.mutableUTXODiff.clone()
}
func (iud immutableUTXODiff) String() string {
return iud.mutableUTXODiff.String()
}