/
state.go
79 lines (67 loc) · 1.5 KB
/
state.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 base
import (
"bytes"
"github.com/ProtoconNet/mitum2/util"
"github.com/ProtoconNet/mitum2/util/hint"
)
var StateFixedtreeHint = hint.MustNewHint("state-fixedtree-v0.0.1")
type State interface {
util.Hasher // NOTE <key> + <value HashByte> + <height>
util.IsValider
Key() string
Value() StateValue
Height() Height // NOTE manifest height
Previous() util.Hash // NOTE previous state hash
Operations() []util.Hash // NOTE operation fact hash
}
type StateValue interface {
util.HashByter
util.IsValider
}
type StateMergeValue interface {
StateValue
Key() string
Value() StateValue
Merger(Height, State) StateValueMerger
}
type StateValueMerger interface {
State
Merge(value StateValue, operations []util.Hash) error
Close() error
}
type GetStateFunc func(key string) (State, bool, error)
func IsEqualState(a, b State) bool {
switch {
case a == nil || b == nil:
return false
case !a.Hash().Equal(b.Hash()):
return false
case a.Key() != b.Key():
return false
case a.Height() != b.Height():
return false
case !IsEqualStateValue(a.Value(), b.Value()):
return false
case len(a.Operations()) != len(b.Operations()):
return false
default:
ao := a.Operations()
bo := b.Operations()
for i := range ao {
if !ao[i].Equal(bo[i]) {
return false
}
}
return true
}
}
func IsEqualStateValue(a, b StateValue) bool {
switch {
case a == nil || b == nil:
return false
case !bytes.Equal(a.HashBytes(), b.HashBytes()):
return false
default:
return true
}
}