forked from FactomProject/factomd
/
factomd.go
110 lines (90 loc) · 2.73 KB
/
factomd.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
// Copyright 2015 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/FactomProject/factomd/btcd"
"github.com/FactomProject/factomd/btcd/limits"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/log"
"github.com/FactomProject/factomd/state"
"github.com/FactomProject/factomd/wsapi"
"os"
"runtime"
"strings"
"time"
)
// winServiceMain is only invoked on Windows. It detects when btcd is running
// as a service and reacts accordingly.
//var winServiceMain func() (bool, error)
// Build sets the factomd build id using git's SHA
// by compiling factomd with: -ldflags "-X main.Build=<build sha1>"
// e.g. get the short version of the sha1 of your latest commit by running
// $ git rev-parse --short HEAD
// $ go install -ldflags "-X main.Build=6c10244"
var Build string
func main() {
log.Print("//////////////////////// Copyright 2015 Factom Foundation")
log.Print("//////////////////////// Use of this source code is governed by the MIT")
log.Print("//////////////////////// license that can be found in the LICENSE file.")
log.Printf("Go compiler version: %s\n", runtime.Version())
log.Printf("Using build: %s\n", Build)
if !isCompilerVersionOK() {
for i := 0; i < 30; i++ {
fmt.Println("!!! !!! !!! ERROR: unsupported compiler version !!! !!! !!!")
}
time.Sleep(3 * time.Second)
os.Exit(1)
}
cfgFilename := ""
state := new(state.State)
state.Init(cfgFilename)
runtime.GOMAXPROCS(runtime.NumCPU())
if err := limits.SetLimits(); err != nil {
os.Exit(1)
}
btcd.AddInterruptHandler(func() {
log.Printf("Gracefully shutting down the database...")
state.GetDB().(interfaces.IDatabase).Close()
})
log.Print("Starting server")
server, _ := btcd.NewServer(state)
btcd.AddInterruptHandler(func() {
log.Printf("Gracefully shutting down the server...")
server.Stop()
server.WaitForShutdown()
})
server.Start()
state.SetServer(server)
//factomForkInit(server)
go NetworkProcessor(state)
go Timer(state)
go Validator(state)
go Leader(state)
go Follower(state)
go wsapi.Start(state)
shutdownChannel := make(chan struct{})
go func() {
server.WaitForShutdown()
log.Printf("Server shutdown complete")
shutdownChannel <- struct{}{}
}()
// Wait for shutdown signal from either a graceful server stop or from
// the interrupt handler.
<-shutdownChannel
log.Printf("Shutdown complete")
}
func isCompilerVersionOK() bool {
goodenough := false
if strings.Contains(runtime.Version(), "1.4") {
goodenough = true
}
if strings.Contains(runtime.Version(), "1.5") {
goodenough = true
}
if strings.Contains(runtime.Version(), "1.6") {
goodenough = true
}
return goodenough
}