/
utils.go
77 lines (65 loc) · 1.61 KB
/
utils.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
package linhybsterpb
import (
"bytes"
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/ibalajiarun/go-consensus/peer/peerpb"
)
type Index uint64
type View uint64
func (idx *Index) GetAndIncrement() Index {
index := *idx
*idx = *idx + 1
return index
}
func (m *InstanceState) IsPrepared() bool {
return m.Status == InstanceState_Prepared
}
func (m *InstanceState) IsCommitted() bool {
return m.Status == InstanceState_Committed
}
func (m *NormalMessage) Equals(other *NormalMessage) bool {
if m.View != other.View {
return false
}
if m.Index != other.Index {
return false
}
return bytes.Equal(m.CommandHash, other.CommandHash)
}
func (m *ResultMessage) Equals(other *ResultMessage) bool {
if len(m.Result) != len(other.Result) {
return false
}
if !bytes.Equal(m.Result, other.Result) {
return false
}
return true
}
// WrapMessageInner wraps a union type of Message in a new isMessage_Type.
func WrapMessageInner(msg proto.Message) isLinHybsterMessage_Type {
switch t := msg.(type) {
case *NormalMessage:
return &LinHybsterMessage_Normal{Normal: t}
case *ResultMessage:
return &LinHybsterMessage_Result{Result: t}
default:
panic(fmt.Sprintf("proto: unexpected type %T in WrapMessageInner", t))
}
}
// WrapLinHybsterMessage wraps a union type of Message in a new PBFTMessage without a
// destination.
func WrapLinHybsterMessage(msg proto.Message) *LinHybsterMessage {
return &LinHybsterMessage{Type: WrapMessageInner(msg)}
}
func ReplicaIDEquals(a, b []peerpb.PeerID) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}