-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
100 lines (85 loc) · 1.95 KB
/
main.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
package main
import (
"flag"
"io"
"log"
"os"
"os/signal"
"os/user"
"syscall"
"time"
)
const (
eventQuit = iota
)
type sysEventMessage struct {
event int
idata int
}
var sysEventChannel = make(chan sysEventMessage, 5)
var startTime = time.Now()
var logFileName = flag.String("log", "/tmp/wot1.log", "Log file ('-' for only stderr)")
var walletFileName = flag.String("wallet", DefaultWalletFilename, "Wallet filename")
var dataDir = flag.String("datadir", "~/.wot", "Data directory for the blockchain")
var miningActive = flag.Bool("mining", true, "Enables mining on this node")
var miningRewardAddress = flag.String("miningAddress", "", "Which address receives the mining reward")
func main() {
flag.Parse()
if *logFileName != "-" {
f, err := os.OpenFile(*logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
if err != nil {
log.Panic("Cannot open log file " + *logFileName)
}
defer f.Close()
log.SetOutput(io.MultiWriter(os.Stderr, f))
} else {
log.SetOutput(os.Stderr)
}
if (*dataDir)[0] == '~' {
usr, err := user.Current()
if err != nil {
log.Fatal("Cannot get current user's home directory")
}
*dataDir = usr.HomeDir + (*dataDir)[1:]
}
if len(flag.Args()) > 0 {
if processSimpleCmdLineActions() {
return
}
}
initGenesis()
initDataDir()
initDatabase()
initWallet(true)
if len(flag.Args()) > 0 {
if processCmdLineActions() {
return
}
}
log.Println("Starting up...")
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel, syscall.SIGINT)
go webServer()
if *miningActive {
go miningRig()
}
for {
select {
case msg := <-sysEventChannel:
switch msg.event {
case eventQuit:
log.Println("Exiting")
shutdownDatabase()
os.Exit(msg.idata)
}
case sig := <-sigChannel:
switch sig {
case syscall.SIGINT:
sysEventChannel <- sysEventMessage{event: eventQuit, idata: 0}
log.Println("^C detected")
}
case <-time.After(60 * time.Second):
log.Println("Tick.")
}
}
}