-
Notifications
You must be signed in to change notification settings - Fork 211
/
hare.go
176 lines (146 loc) · 4.48 KB
/
hare.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
package main
import (
"fmt"
cmdp "github.com/spacemeshos/go-spacemesh/cmd"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/common/util"
"github.com/spacemeshos/go-spacemesh/hare"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/monitoring"
"github.com/spacemeshos/go-spacemesh/oracle"
"github.com/spacemeshos/go-spacemesh/p2p"
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/timesync"
"github.com/spf13/cobra"
"net/http"
"os"
"runtime"
"runtime/pprof"
"time"
)
import _ "net/http/pprof"
// Hare cmd
var Cmd = &cobra.Command{
Use: "hare",
Short: "start hare",
Run: func(cmd *cobra.Command, args []string) {
log.JSONLog(true)
hareApp := NewHareApp()
defer hareApp.Cleanup()
// monitor app
hareApp.updater = monitoring.NewMemoryUpdater()
hareApp.monitor = monitoring.NewMonitor(1*time.Second, 5*time.Second, hareApp.updater, make(chan struct{}))
hareApp.monitor.Start()
// start app
hareApp.Initialize(cmd)
hareApp.Start(cmd, args)
<-hareApp.ha.CloseChannel()
},
}
func init() {
cmdp.AddCommands(Cmd)
}
type mockBlockProvider struct {
}
func (mbp *mockBlockProvider) LayerBlockIds(layerId types.LayerID) ([]types.BlockID, error) {
return buildSet(), nil
}
type HareApp struct {
*cmdp.BaseApp
p2p p2p.Service
oracle *oracle.OracleClient
sgn hare.Signer
ha *hare.Hare
clock *timesync.TimeClock
updater *monitoring.MemoryUpdater
monitor *monitoring.Monitor
}
func IsSynced() bool {
return true
}
func NewHareApp() *HareApp {
return &HareApp{BaseApp: cmdp.NewBaseApp(), sgn: signing.NewEdSigner()}
}
func (app *HareApp) Cleanup() {
// TODO: move to array of cleanup functions and execute all here
app.oracle.Unregister(true, app.sgn.PublicKey().String())
}
func buildSet() []types.BlockID {
s := make([]types.BlockID, 200, 200)
for i := uint64(0); i < 200; i++ {
s = append(s, types.NewExistingBlock(1, util.Uint64ToBytes(i)).Id())
}
return s
}
type mockIdProvider struct {
}
func (mip *mockIdProvider) GetIdentity(edId string) (types.NodeId, error) {
return types.NodeId{Key: edId, VRFPublicKey: []byte{}}, nil
}
type mockStateQuerier struct {
}
func (msq mockStateQuerier) IsIdentityActiveOnConsensusView(edId string, layer types.LayerID) (bool, error) {
return true, nil
}
func validateBlocks(blocks []types.BlockID) bool {
return true
}
func (app *HareApp) Start(cmd *cobra.Command, args []string) {
log.Info("Starting hare main")
if app.Config.MemProfile != "" {
log.Info("Starting mem profiling")
f, err := os.Create(app.Config.MemProfile)
if err != nil {
log.Error("could not create memory profile: ", err)
}
defer f.Close()
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Error("could not write memory profile: ", err)
}
}
if app.Config.PprofHttpServer {
log.Info("Starting pprof server")
go func() {
err := http.ListenAndServe(":6060", nil)
if err != nil {
log.Error("cannot start http server", err)
}
}()
}
log.Info("Initializing P2P services")
swarm, err := p2p.New(cmdp.Ctx, app.Config.P2P, log.NewDefault("p2p_haretest"), app.Config.DataDir)
app.p2p = swarm
if err != nil {
log.Panic("Error starting p2p services err=%v", err)
}
pub := app.sgn.PublicKey()
lg := log.NewDefault(pub.String())
oracle.SetServerAddress(app.Config.OracleServer)
app.oracle = oracle.NewOracleClientWithWorldID(uint64(app.Config.OracleServerWorldId))
app.oracle.Register(true, pub.String()) // todo: configure no faulty nodes
hareOracle := oracle.NewHareOracleFromClient(app.oracle)
gTime, err := time.Parse(time.RFC3339, app.Config.GenesisTime)
if err != nil {
log.Panic("error parsing config err=%v", err)
}
ld := time.Duration(app.Config.LayerDurationSec) * time.Second
app.clock = timesync.NewClock(timesync.RealClock{}, ld, gTime, lg)
app.ha = hare.New(app.Config.HARE, app.p2p, app.sgn, types.NodeId{Key: app.sgn.PublicKey().String(), VRFPublicKey: []byte{}}, validateBlocks, IsSynced, &mockBlockProvider{}, hareOracle, uint16(app.Config.LayersPerEpoch), &mockIdProvider{}, &mockStateQuerier{}, app.clock.Subscribe(), lg)
log.Info("Starting hare service")
err = app.ha.Start()
if err != nil {
log.Panic("error starting maatuf err=%v", err)
}
err = app.p2p.Start()
if err != nil {
log.Panic("error starting p2p err=%v", err)
}
app.clock.StartNotifying()
}
func main() {
if err := Cmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}