forked from hyperledger-labs/mirbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workitems.go
184 lines (157 loc) · 5.17 KB
/
workitems.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
183
184
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
Refactored: 1
*/
package mirbft
import (
"fmt"
"github.com/hyperledger-labs/mirbft/pkg/events"
"github.com/hyperledger-labs/mirbft/pkg/pb/eventpb"
)
// WorkItems is a buffer for storing outstanding events that need to be processed by the node.
// It contains a separate list for each type of event.
type workItems struct {
wal *events.EventList
net *events.EventList
hash *events.EventList
client *events.EventList
app *events.EventList
reqStore *events.EventList
protocol *events.EventList
crypto *events.EventList
}
// NewWorkItems allocates and returns a pointer to a new WorkItems object.
func newWorkItems() *workItems {
return &workItems{
wal: &events.EventList{},
net: &events.EventList{},
hash: &events.EventList{},
client: &events.EventList{},
app: &events.EventList{},
reqStore: &events.EventList{},
protocol: &events.EventList{},
crypto: &events.EventList{},
}
}
// AddEvents adds events produced by modules to the workItems buffer.
// According to their types, the events are distributed to the appropriate internal sub-buffers.
// When AddEvents returns a non-nil error, any subset of the events may have been added.
func (wi *workItems) AddEvents(events *events.EventList) error {
iter := events.Iterator()
for event := iter.Next(); event != nil; event = iter.Next() {
switch t := event.Type.(type) {
case *eventpb.Event_Init:
wi.protocol.PushBack(event)
// TODO: Should the Init event also go elsewhere? Clients? All the modules?
case *eventpb.Event_Tick:
wi.protocol.PushBack(event)
// TODO: Should the Tick event also go elsewhere? Clients?
case *eventpb.Event_SendMessage:
wi.net.PushBack(event)
case *eventpb.Event_MessageReceived, *eventpb.Event_Iss, *eventpb.Event_RequestReady,
*eventpb.Event_AppSnapshot:
wi.protocol.PushBack(event)
case *eventpb.Event_Request, *eventpb.Event_RequestSigVerified:
wi.client.PushBack(event)
case *eventpb.Event_StoreVerifiedRequest:
wi.reqStore.PushBack(event)
case *eventpb.Event_VerifyRequestSig:
wi.crypto.PushBack(event)
case *eventpb.Event_HashRequest:
wi.hash.PushBack(event)
case *eventpb.Event_HashResult:
// For hash results, their origin determines the destination.
switch t.HashResult.Origin.Type.(type) {
case *eventpb.HashOrigin_Request:
// If the origin is a request received directly from a client,
// it is the client tracker that created the request and the result goes back to it.
wi.client.PushBack(event)
case *eventpb.HashOrigin_Iss:
// The ISS origin goes to the Protocol module (ISS is a protocol implementation).
wi.protocol.PushBack(event)
}
case *eventpb.Event_WalAppend:
wi.wal.PushBack(event)
case *eventpb.Event_Deliver, *eventpb.Event_AppSnapshotRequest:
wi.app.PushBack(event)
case *eventpb.Event_WalEntry:
switch walEntry := t.WalEntry.Event.Type.(type) {
case *eventpb.Event_Iss:
// TODO: WAL loading is now disabled for ISS by commenting out the next line.
// Implement recovery and re-enable (un-comment).
// wi.protocol.PushBack(t.WalEntry.Event)
case *eventpb.Event_PersistDummyBatch:
wi.protocol.PushBack(t.WalEntry.Event)
default:
return fmt.Errorf("unsupported WAL entry event type %T", walEntry)
}
// TODO: Remove these eventually.
case *eventpb.Event_PersistDummyBatch:
wi.wal.PushBack(event)
case *eventpb.Event_AnnounceDummyBatch:
wi.app.PushBack(event)
case *eventpb.Event_StoreDummyRequest:
wi.reqStore.PushBack(event)
default:
return fmt.Errorf("cannot add event of unknown type %T", t)
}
}
return nil
}
// Getters.
func (wi *workItems) WAL() *events.EventList {
return wi.wal
}
func (wi *workItems) Net() *events.EventList {
return wi.net
}
func (wi *workItems) Hash() *events.EventList {
return wi.hash
}
func (wi *workItems) Client() *events.EventList {
return wi.client
}
func (wi *workItems) App() *events.EventList {
return wi.app
}
func (wi *workItems) ReqStore() *events.EventList {
return wi.reqStore
}
func (wi *workItems) Protocol() *events.EventList {
return wi.protocol
}
func (wi *workItems) Crypto() *events.EventList {
return wi.crypto
}
// Methods for clearing the buffers.
// Each of them returns the list of events that have been removed from workItems.
func (wi *workItems) ClearWAL() *events.EventList {
return clearEventList(&wi.wal)
}
func (wi *workItems) ClearNet() *events.EventList {
return clearEventList(&wi.net)
}
func (wi *workItems) ClearHash() *events.EventList {
return clearEventList(&wi.hash)
}
func (wi *workItems) ClearClient() *events.EventList {
return clearEventList(&wi.client)
}
func (wi *workItems) ClearApp() *events.EventList {
return clearEventList(&wi.app)
}
func (wi *workItems) ClearReqStore() *events.EventList {
return clearEventList(&wi.reqStore)
}
func (wi *workItems) ClearProtocol() *events.EventList {
return clearEventList(&wi.protocol)
}
func (wi *workItems) ClearCrypto() *events.EventList {
return clearEventList(&wi.crypto)
}
func clearEventList(listPtr **events.EventList) *events.EventList {
oldList := *listPtr
*listPtr = &events.EventList{}
return oldList
}