-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.go
More file actions
212 lines (164 loc) · 4.16 KB
/
kernel.go
File metadata and controls
212 lines (164 loc) · 4.16 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
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
// Copyright © 2018 J. Strobus White.
// This file is part of the blocktop blockchain development kit.
//
// Blocktop is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Blocktop 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with blocktop. If not, see <http://www.gnu.org/licenses/>.
package kernel
import (
"context"
"time"
spec "github.com/blocktop/go-spec"
"github.com/golang/glog"
)
type Kernel struct {
name string
net spec.NetworkNode
started bool
stop func()
}
var kernel *Kernel
var initHanlders []func() = make([]func(), 0)
func Init(c *KernelConfig) {
if !c.valid() {
panic("all KernelConfig fields are required")
}
k := &Kernel{}
k.name = c.Blockchain.Name()
kernel = k
initTime(c.BlockFrequency)
initMetrics()
initNet(c.NetworkNode)
initBlock(c)
initProc()
for _, handler := range initHanlders {
handler()
}
}
func panicIfUninitialized() {
if !Initialized() {
panic("kernel is not initialized")
}
}
func Initialized() bool {
return kernel != nil
}
func OnInit(f func()) {
initHanlders = append(initHanlders, f)
}
func Metrics() *KernelMetrics {
panicIfUninitialized()
return metrics
}
func Time() *KernelTime {
panicIfUninitialized()
return ktime
}
func Network() *KernelNet {
panicIfUninitialized()
return net
}
func Proc() *KernelProc {
panicIfUninitialized()
return proc
}
func Block() *KernelBlock {
panicIfUninitialized()
return blk
}
func Started() bool {
return kernel.started
}
func Start(parentCtx context.Context) {
panicIfUninitialized()
if kernel.started {
return
}
ctx, cancel := context.WithCancel(parentCtx)
kernel.stop = cancel
kernel.started = true
net.start()
ktime.up()
go kernel.runBlockCycle(ctx)
}
func Stop() {
panicIfUninitialized()
if !kernel.started {
return
}
net.stop()
if kernel.stop != nil {
kernel.stop()
kernel.stop = nil
}
kernel.started = false
}
const (
blockCycleStateMaint = iota
blockCycleStateProc
)
func (k *Kernel) runBlockCycle(ctx context.Context) {
state := blockCycleStateProc
for {
select {
case <-ctx.Done():
Stop()
return
default:
switch state {
case blockCycleStateProc:
ktime.startCycle()
k.proc()
state = blockCycleStateMaint
case blockCycleStateMaint:
k.maint()
state = blockCycleStateProc
}
}
}
}
func (k *Kernel) maint() {
maintStartTime := time.Now().UnixNano()
glog.V(3).Infoln("------------- maint cycle -------------")
glog.V(3).Infof("Uptime: %s", ktime.UpTime().String())
glog.V(3).Infof("Kernel time: %s", ktime.String())
blk.stop()
blk.maint()
net.setMetrics()
maintEndTime := time.Now().UnixNano()
metrics.setMaintTime(maintEndTime - maintStartTime)
}
func (k *Kernel) proc() {
procStartTime := time.Now().UnixNano()
glog.V(3).Infoln("------------- proc cycle --------------")
glog.V(3).Infof("Uptime: %s", ktime.UpTime().String())
glog.V(3).Infof("Kernel time: %s", ktime.String())
// Anything broadcast during this proc cycle will not be sent
// Until the cycle is over. This prevents a message created
// during the cycle from being received during the same cycle
// e.g. runaway process.
net.beginProc()
// Resume processing of blocks received and queued.
blk.start()
procTime := metrics.computeProcTime()
glog.V(3).Infof("%s: computed process time %dms", ktime.String(), procTime/time.Millisecond)
timer := time.NewTimer(procTime)
blk.generate()
// Wait for the block to generate and the timer to expire, which ever comes _last_
// (blk.generate is not a goroutine).
<-timer.C
net.endProc()
procEndTime := time.Now().UnixNano()
actualProcTime := procEndTime - procStartTime
glog.V(3).Infof("%s: actual process time %dms", ktime.String(), actualProcTime/int64(time.Millisecond))
metrics.setActualProcTime(actualProcTime)
}