-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (64 loc) · 1.41 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
package main
import (
"crypto/rand"
"flag"
fmt "fmt"
"log"
"net"
"os"
"os/signal"
grpc "google.golang.org/grpc"
)
func main() {
var nodeHost string
var nodePort string
var serverPort string
flag.StringVar(&serverPort, "serverport", "8886", "Port of the server")
flag.StringVar(&nodeHost, "nodehost", "localhost", "Hostname of the node to connect")
flag.StringVar(&nodePort, "nodeport", "", "Port of the node to connect")
flag.Parse()
data := make([]byte, 64)
rand.Read(data)
originBlock := &Block{
Hash: []byte("origin"),
Nonce: []byte{},
ParentHash: []byte("origin"),
Data: data[:],
}
fmt.Printf("Origin block hash=%x\n", originBlock.Hash)
api := &API{
Blocks: map[string]*Block{
"": originBlock,
},
LatestBlock: originBlock,
}
go api.Mine()
go func() {
lis, err := net.Listen("tcp", "0.0.0.0:"+serverPort)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
RegisterBlockchainServer(s, api)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}()
go func() {
if nodePort != "" {
conn, err := grpc.Dial(nodeHost+":"+nodePort, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
}
}()
c := make(chan os.Signal, 1000)
signal.Notify(c, os.Interrupt)
func() {
for _ = range c {
fmt.Println("Exiting...")
return
}
}()
}