forked from hyperledger-labs/mirbft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkitems.go
More file actions
117 lines (94 loc) · 2.81 KB
/
workitems.go
File metadata and controls
117 lines (94 loc) · 2.81 KB
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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
Refactored: 1
*/
package mirbft
import (
"fmt"
"github.com/hyperledger-labs/mirbft/pkg/pb/state"
"github.com/hyperledger-labs/mirbft/pkg/statemachine"
)
// 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 *statemachine.EventList
net *statemachine.EventList
hash *statemachine.EventList
client *statemachine.EventList
app *statemachine.EventList
reqStore *statemachine.EventList
stateMachine *statemachine.EventList
}
// NewWorkItems allocates and returns a pointer to a new WorkItems object.
func NewWorkItems() *WorkItems {
return &WorkItems{
wal: &statemachine.EventList{},
net: &statemachine.EventList{},
hash: &statemachine.EventList{},
client: &statemachine.EventList{},
app: &statemachine.EventList{},
reqStore: &statemachine.EventList{},
stateMachine: &statemachine.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.
func (wi *WorkItems) AddEvents(events *statemachine.EventList) {
iter := events.Iterator()
for event := iter.Next(); event != nil; event = iter.Next() {
switch t := event.Type.(type) {
case *state.Event_HashResult:
wi.StateMachine().PushBack(event)
case *state.Event_TickElapsed:
wi.StateMachine().PushBack(event)
// TODO: Should the TickElapsed event also go elsewhere?
default:
panic(fmt.Sprintf("unknown event type %T", t))
}
}
}
// Getters.
func (wi *WorkItems) WAL() *statemachine.EventList {
return wi.wal
}
func (wi *WorkItems) Net() *statemachine.EventList {
return wi.net
}
func (wi *WorkItems) Hash() *statemachine.EventList {
return wi.hash
}
func (wi *WorkItems) Client() *statemachine.EventList {
return wi.client
}
func (wi *WorkItems) App() *statemachine.EventList {
return wi.app
}
func (wi *WorkItems) ReqStore() *statemachine.EventList {
return wi.reqStore
}
func (wi *WorkItems) StateMachine() *statemachine.EventList {
return wi.stateMachine
}
// Methods for clearing the buffers.
func (wi *WorkItems) ClearWAL() {
wi.wal = &statemachine.EventList{}
}
func (wi *WorkItems) ClearNet() {
wi.net = &statemachine.EventList{}
}
func (wi *WorkItems) ClearHash() {
wi.hash = &statemachine.EventList{}
}
func (wi *WorkItems) ClearClient() {
wi.client = &statemachine.EventList{}
}
func (wi *WorkItems) ClearApp() {
wi.app = &statemachine.EventList{}
}
func (wi *WorkItems) ClearReqStore() {
wi.reqStore = &statemachine.EventList{}
}
func (wi *WorkItems) ClearStateMachine() {
wi.stateMachine = &statemachine.EventList{}
}