forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.go
232 lines (198 loc) · 6.94 KB
/
service.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (C) 2019 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package agreement
//go:generate dbgen -i agree.sql -p agreement -n agree -o agreeInstall.go
import (
"context"
"time"
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/util/db"
"github.com/algorand/go-algorand/util/execpool"
"github.com/algorand/go-algorand/util/timers"
)
const (
defaultCadaverName = "agreement"
)
// Service represents an instance of an execution of Algorand's agreement protocol.
type Service struct {
parameters
// for exiting
quit chan struct{}
done chan struct{}
quitFn context.CancelFunc // TODO instead of storing this, pass a context into Start()
// external events
demux *demux
loopback pseudonode
log serviceLogger
tracer *tracer
voteVerifier *AsyncVoteVerifier
persistenceLoop *asyncPersistenceLoop
monitor *coserviceMonitor
persistRouter rootRouter
persistStatus player
persistActions []action
}
// Parameters holds the parameters necessary to run the agreement protocol.
type Parameters struct {
Ledger
Network
KeyManager
BlockValidator
BlockFactory
RandomSource
EventsProcessingMonitor
timers.Clock
db.Accessor
logging.Logger
config.Local
execpool.BacklogPool
}
// parameters is a convenience typedef for Parameters.
type parameters Parameters
// externalDemuxSignals used to syncronize the external signals that goes to the demux with the main loop.
type externalDemuxSignals struct {
Deadline time.Duration
FastRecoveryDeadline time.Duration
CurrentRound round
}
// MakeService creates a new Agreement Service instance given a set of Parameters.
//
// Call Start to start execution and Shutdown to finish execution.
func MakeService(p Parameters) *Service {
s := new(Service)
s.parameters = parameters(p)
s.log = serviceLogger{Logger: p.Logger}
s.quit = make(chan struct{})
s.done = make(chan struct{})
// GOAL2-541: tracer is not concurrency safe. It should only ever be
// accessed by main state machine loop.
s.tracer = makeTracer(s.log, defaultCadaverName, p.CadaverSizeTarget,
s.Local.EnableAgreementReporting, s.Local.EnableAgreementTimeMetrics)
s.voteVerifier = MakeAsyncVoteVerifier(s.BacklogPool)
s.demux = makeDemux(s.Network, s.Ledger, s.BlockValidator, s.voteVerifier, s.EventsProcessingMonitor, s.log)
s.loopback = makePseudonode(s.BlockFactory, s.BlockValidator, s.KeyManager, s.Ledger, s.voteVerifier, s.log)
s.persistenceLoop = makeAsyncPersistenceLoop(s.log, s.Accessor, s.Ledger)
return s
}
// SetTracerFilename updates the tracer filename used.
func (s *Service) SetTracerFilename(filename string) {
s.tracer.cadaver.baseFilename = filename
}
// Start executing the agreement protocol.
func (s *Service) Start() {
ctx, quitFn := context.WithCancel(context.Background())
s.quitFn = quitFn
s.persistenceLoop.Start()
input := make(chan externalEvent)
output := make(chan []action)
ready := make(chan externalDemuxSignals)
go s.demuxLoop(ctx, input, output, ready)
go s.mainLoop(input, output, ready)
}
// Shutdown the execution of the protocol.
//
// This method returns after all resources have been cleaned up.
func (s *Service) Shutdown() {
close(s.quit)
s.quitFn()
<-s.done
s.persistenceLoop.Quit()
}
// demuxLoop repeatedly executes pending actions and then requests the next event from the Service.demux.
func (s *Service) demuxLoop(ctx context.Context, input chan<- externalEvent, output <-chan []action, ready <-chan externalDemuxSignals) {
for a := range output {
s.do(ctx, a)
extSignals := <-ready
e, ok := s.demux.next(s, extSignals.Deadline, extSignals.FastRecoveryDeadline, extSignals.CurrentRound)
if !ok {
close(input)
break
}
input <- e
}
s.demux.quit()
s.loopback.Quit()
s.voteVerifier.Quit()
close(s.done)
}
// mainLoop drives the state machine.
//
// After possibly restoring from disk and then initializing, it does the following in a loop:
// 1. Execute all pending actions.
// 2. Obtain an input event from the demultiplexer.
// 3. Drive the state machine with this input to obtain a slice of pending actions.
// 4. If necessary, persist state to disk.
func (s *Service) mainLoop(input <-chan externalEvent, output chan<- []action, ready chan<- externalDemuxSignals) {
// setup
var clock timers.Clock
var router rootRouter
var status player
var a []action
var err error
raw, err := restore(s.log, s.Accessor)
if err == nil {
clock, router, status, a, err = decode(raw, s.Clock)
if err != nil {
reset(s.log, s.Accessor)
} else {
s.log.Infof("decode (agreement): restored crash state from database (pending %v @ %+v)", a, status)
}
}
// err will tell us if the restore/decode operations above completed successfully or not.
if err != nil || status.Round < s.Ledger.NextRound() {
// in this case, we don't have fresh and valid state
// pretend a new round has just started, and propose a block
status = player{Round: s.Ledger.NextRound(), Step: soft, Deadline: filterTimeout}
router = makeRootRouter(status)
a1 := pseudonodeAction{T: assemble, Round: s.Ledger.NextRound()}
a2 := rezeroAction{}
a = make([]action, 0)
a = append(a, a1, a2)
} else {
s.Clock = clock
}
for {
output <- a
ready <- externalDemuxSignals{Deadline: status.Deadline, FastRecoveryDeadline: status.FastRecoveryDeadline, CurrentRound: status.Round}
e, ok := <-input
if !ok {
break
}
status, a = router.submitTop(s.tracer, status, e)
if persistent(a) {
s.persistRouter = router
s.persistStatus = status
s.persistActions = a
}
}
close(output)
}
// persistState encodes the existing state of the agreement service and enqueue the
// encoded state to the persistence loop so it will get stored asynchronously.
// the done channel would get closed once operation complete successfully, or return an
// error if not.
// usage semantics : caller should ensure to call this function only when we have participation
// keys for the given voting round.
func (s *Service) persistState(done chan error) (events <-chan externalEvent) {
raw := encode(s.Clock, s.persistRouter, s.persistStatus, s.persistActions)
return s.persistenceLoop.Enqueue(s.Clock, s.persistStatus.Round, s.persistStatus.Period, s.persistStatus.Step, raw, done)
}
func (s *Service) do(ctx context.Context, as []action) {
for _, a := range as {
a.do(ctx, s)
}
}