-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsbac_sm.go
182 lines (161 loc) · 4.26 KB
/
sbac_sm.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package sbac
import (
"fmt"
"sync"
"chainspace.io/chainspace-go/internal/log"
"chainspace.io/chainspace-go/internal/log/fld"
)
type StateSBAC uint8
const (
StateSBACWaiting StateSBAC = iota
StateSBACAccepted
StateSBACRejected
)
func (e StateSBAC) String() string {
switch e {
case StateSBACWaiting:
return "StateSBACWaiting"
case StateSBACAccepted:
return "StateSBACAccepted"
case StateSBACRejected:
return "StateSBACRejected"
default:
return "error"
}
}
type SBACEventAction func(st *States, decisions map[uint64]SignedDecision, e *SBACEvent) (StateSBAC, error)
type SBACStateMachine struct {
action SBACEventAction
mu sync.Mutex
msgs map[uint64]*SBACMessage
decisions map[uint64]SignedDecision
phase SBACOp
state StateSBAC
}
func (c *SBACStateMachine) State() StateSBAC {
return c.state
}
func (c *SBACStateMachine) processEvent(st *States, e *SBACEvent) error {
if e.Kind() != EventKindSBACMessage {
return fmt.Errorf("SBACStateMachine, invalid EventKind(%v)",
e.Kind().String())
}
if c.state != StateSBACWaiting {
return fmt.Errorf("SBACStateMachine already finished, state(%v)",
c.state.String())
}
var err error
c.msgs[e.msg.PeerID] = e.msg
c.SetDecision(e.PeerID(), SignedDecision{e.msg.Decision, e.msg.Signature})
c.state, err = c.action(st, c.GetDecisions(), e)
return err
}
func (c *SBACStateMachine) Phase() SBACOp {
return c.phase
}
func (c *SBACStateMachine) Data() interface{} {
return c.msgs
}
func (c *SBACStateMachine) GetDecisions() map[uint64]SignedDecision {
c.mu.Lock()
out := map[uint64]SignedDecision{}
for k, v := range c.decisions {
out[k] = v
}
c.mu.Unlock()
return out
}
func (c *SBACStateMachine) SetDecision(n uint64, d SignedDecision) {
c.mu.Lock()
defer c.mu.Unlock()
c.decisions[n] = d
}
func NewSBACStateMachine(phase SBACOp, action SBACEventAction) *SBACStateMachine {
return &SBACStateMachine{
action: action,
phase: phase,
state: StateSBACWaiting,
msgs: map[uint64]*SBACMessage{},
decisions: map[uint64]SignedDecision{},
}
}
func (s *ServiceSBAC) onSBACEvent(
st *States, decisions map[uint64]SignedDecision, e *SBACEvent) (StateSBAC, error) {
shards := s.shardsInvolvedWithoutSelf(st.detail.Tx)
var somePending bool
// for each shards, get the nodes id, and checks if they answered
// vtwotplusone := quorum2t1(s.shardSize)
vtwotplusone := s.shardSize
vtplusone := quorumt1(s.shardSize)
for _, v := range shards {
nodes := s.top.NodesInShard(v)
var accepted uint64
var rejected uint64
for _, nodeID := range nodes {
if d, ok := decisions[nodeID]; ok {
if d.Decision == SBACDecision_ACCEPT {
accepted += 1
continue
}
rejected += 1
}
}
if rejected >= vtplusone {
if log.AtDebug() {
log.Debug("transaction rejected",
log.String("sbac.phase", e.msg.Op.String()),
fld.TxID(st.detail.HashID),
fld.PeerShard(v),
log.Uint64("t+1", vtplusone),
log.Uint64("rejected", rejected),
)
}
return StateSBACRejected, nil
}
if accepted >= vtwotplusone {
if log.AtDebug() {
log.Debug("transaction accepted",
log.String("sbac.phase", e.msg.Op.String()),
fld.TxID(st.detail.HashID),
fld.PeerShard(v),
log.Uint64s("shards_involved", shards),
log.Uint64("2t+1", vtwotplusone),
log.Uint64("accepted", accepted),
)
}
continue
}
somePending = true
}
if somePending {
if log.AtDebug() {
log.Debug("transaction pending, not enough answers from shards",
fld.TxID(st.detail.HashID),
log.String("sbac.phase", e.msg.Op.String()))
}
return StateSBACWaiting, nil
}
if log.AtDebug() {
log.Debug("transaction accepted by all shards",
fld.TxID(st.detail.HashID),
log.String("sbac.phase", e.msg.Op.String()))
}
// verify signatures now
for k, v := range decisions {
// TODO(): what to do with nodes with invalid signature
ok, err := s.verifyTransactionSignature(st.detail.Tx, v.Signature, k)
if err != nil {
log.Error("unable to verify signature",
log.String("sbac.phase", e.msg.Op.String()),
fld.TxID(st.detail.HashID),
fld.Err(err))
}
if !ok {
log.Error("invalid signature for a decision",
log.String("sbac.phase", e.msg.Op.String()),
fld.TxID(st.detail.HashID),
fld.PeerID(k))
}
}
return StateSBACAccepted, nil
}