-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (139 loc) · 4.13 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
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
package main
import (
"time"
"net/http"
"github.com/gorilla/mux"
"os"
"log"
"github.com/mohanarpit/yolochain/handlers"
"github.com/mohanarpit/yolochain/models"
"github.com/joho/godotenv"
"net"
"github.com/mohanarpit/yolochain/blockchain"
"flag"
"google.golang.org/grpc"
"github.com/mohanarpit/yolochain/blockchainGrpc"
"google.golang.org/grpc/reflection"
)
func runHTTPServer(httpHandler http.Handler) error {
httpAddr := os.Getenv("HTTP_ADDR")
log.Println("Listening on HTTP Port: ", httpAddr)
s := &http.Server{
Addr: httpAddr,
Handler: httpHandler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
if err := s.ListenAndServe(); err != nil {
return err
}
return nil
}
func makeStandaloneHTTPRouter() http.Handler {
muxRouter := mux.NewRouter()
muxRouter.HandleFunc("/", handlers.HandleGetBlockchain).Methods("GET")
muxRouter.HandleFunc("/", handlers.HandleWriteBlockchain).Methods("POST")
return muxRouter
}
func makePOSHTTPRouter() http.Handler {
muxRouter := mux.NewRouter()
muxRouter.HandleFunc("/", handlers.HandleGetBlockchain).Methods("GET")
return muxRouter
}
// standaloneMain is the simplest blockchain that runs in standalone mode
// It exposes a HTTP server which can be used to query and write data to the Blockchain
func standaloneMain() {
blockchain.BootstrapBlockchain()
log.Fatal(runHTTPServer(makeStandaloneHTTPRouter()))
}
// networkMain supports the "network" mode in the blockchain. It allows clients to connect to it and create new blocks
// Currently it doesn't completely satisfy the POW as defined by the Blockchain paper
func networkMain() {
models.BlockchainServer = make(chan []models.Block)
models.InputMsgChan = make(chan models.Message)
blockchain.BootstrapBlockchain()
server, err := net.Listen("tcp", os.Getenv("TCP_ADDR"))
if err != nil {
log.Fatal(err)
}
defer server.Close()
for {
conn, err := server.Accept()
if err != nil {
log.Fatal(err)
}
go handlers.HandleConn(conn)
}
}
// posMain runs the blockchain server in Proof of Stake mode. It allows clients to connect to it, stake a certain number of
// tokens and then assigns the new block to the winner client node based on the number of tokens that are staked.
func posMain() {
models.BlockchainServer = make(chan []models.Block)
models.InputMsgChan = make(chan models.Message)
// Genesis block to bootstrap the blockchain application
blockchain.BootstrapBlockchain()
// TCP Server to accept connections from clients
tcpAddr := os.Getenv("TCP_ADDR")
server, err := net.Listen("tcp", tcpAddr)
if err != nil {
log.Fatal(err)
}
defer server.Close()
log.Println("Listening on TCP Port: ", tcpAddr)
// Goroutine to handle the candidateBlocks from which the winner block will be chosen
go blockchain.HandleCandidateBlocks()
// Goroutine to pick the winners at regular intervals
go func() {
for {
blockchain.PickPOSWinner()
}
}()
// Goroutine to start the HTTP server for REST calls
go func() {
log.Fatal(runHTTPServer(makePOSHTTPRouter()))
}()
// Goroutine to handle the TCP connections for clients staking tokens
go func() {
for {
conn, err := server.Accept()
if err != nil {
log.Fatal(err)
}
go handlers.HandlePOSConn(conn)
}
}()
// Starting the Control server over GRPC
models.Cluster = append(models.Cluster, "localhost:5050")
grpcAddr := os.Getenv("GRPC_ADDR")
listener, err := net.Listen("tcp", grpcAddr)
if err != nil {
log.Fatalln(err)
}
s := grpc.NewServer()
blockchainGrpc.RegisterControlServiceServer(s, &handlers.Server{})
reflection.Register(s)
log.Println("Going to listen on the GRPC port: " + grpcAddr)
if err := s.Serve(listener); err != nil {
log.Fatalln(err)
}
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal(err)
}
var mode = flag.String("mode", "pos", "The mode in which you want to run the blockchain. Available modes are standalone/network/pos/grpc. Default is pos.")
flag.Parse()
log.Println("Going to run the Yolochain in mode: ", *mode)
switch *mode {
case "pos":
posMain()
case "standalone":
standaloneMain()
case "network":
networkMain()
default:
break
}
}