-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
90 lines (70 loc) · 1.97 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
package main
import (
"log"
"math/big"
"net"
"os"
"path"
"time"
"github.com/klaytn/klaytn/common"
"github.com/klayoracle/klayoracle-monorepo/node/protonode"
"google.golang.org/grpc"
"github.com/klayoracle/klayoracle-monorepo/node/config"
"github.com/klayoracle/klayoracle-monorepo/node/core"
"github.com/klayoracle/klayoracle-monorepo/node/boot"
)
func main() {
wd, err := os.Getwd()
if err != nil {
log.Fatal("cannot determine working directory: ", err)
}
if os.Getenv("WORK_DIR") != "" {
wd = os.Getenv("WORK_DIR")
}
boot.Boot(wd, path.Join(wd, "config.yaml"), path.Join(wd, ".env"))
//@Todo refactor
core.NewWssClient()
defer func() {
config.Loaded.Logger.Warnw("closing blockchain client connection")
core.KlaytnClient.Close()
}()
config.Loaded.Logger.Info("Working directory: ", wd, "\n")
//Start Node service
n := &core.Node{}
n.Jobs = make(map[string]*protonode.Adapter) //Fix nil map issue
s, err := core.NewNodeServiceServer(n)
if err != nil {
config.Loaded.Logger.Fatal(err)
}
//Set round queue default
n.RoundQueue = new([]core.RoundQueue)
nodeAddress := common.HexToAddress(os.Getenv("PUBLIC_ADDRESS"))
nonce, err := core.KlaytnClient.NonceAt(core.KlaytnClientCtx, nodeAddress, nil)
if err != nil {
config.Loaded.Logger.Fatal("cannot determine nonce for node address: %v", err)
}
n.LastNonce = big.NewInt(int64(nonce))
//Start watching queue every 5 secs
go func() {
for now := range time.Tick(time.Second * 5) {
config.Loaded.Logger.Infow("checking round queue for available answer", "size", len(*n.RoundQueue), "time", now.Local())
n.WatchRoundQueue()
}
}()
lis, err := net.Listen("tcp", os.Getenv("HOST_IP"))
if err != nil {
config.Loaded.Logger.Fatal("failed to listen: %v", err)
}
shutServer := make(chan *grpc.Server, 1)
go func() {
if err = s.Serve(lis); err != nil {
shutServer <- s
}
}()
n.Peer()
for range shutServer {
s.Stop()
config.Loaded.Logger.Fatal("failed to serve: %v", err)
return
}
}