-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (146 loc) · 5.4 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
164
165
166
167
168
169
170
171
172
package main
import (
"context"
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/asgaines/blockchain/chain"
"github.com/asgaines/blockchain/mining"
"github.com/asgaines/blockchain/nodes"
pb "github.com/asgaines/blockchain/protogo/blockchain"
"google.golang.org/grpc"
)
const ascii = `
$$$$$$\ $$\ $$\ $$\ $$\ $$\ $$\ $$\
$$ __$$\ \__|$$ | $$ | $$$\ $$$ |\__| \__|
$$ / \__|$$\ $$\ $$\ $$ | $$$$$$$ | $$$$$$\ $$$$$$\ $$$$\ $$$$ |$$\ $$$$$$$\ $$\ $$$$$$$\ $$$$$$\
$$ |$$$$\ $$ | $$ |$$ |$$ |$$ __$$ |$$ __$$\ $$ __$$\ $$\$$\$$ $$ |$$ |$$ __$$\ $$ |$$ __$$\ $$ __$$\
$$ |\_$$ |$$ | $$ |$$ |$$ |$$ / $$ |$$$$$$$$ |$$ | \__| $$ \$$$ $$ |$$ |$$ | $$ |$$ |$$ | $$ |$$ / $$ |
$$ | $$ |$$ | $$ |$$ |$$ |$$ | $$ |$$ ____|$$ | $$ |\$ /$$ |$$ |$$ | $$ |$$ |$$ | $$ |$$ | $$ |
\$$$$$$ |\$$$$$$ |$$ |$$ |\$$$$$$$ |\$$$$$$$\ $$ | $$ | \_/ $$ |$$ |$$ | $$ |$$ |$$ | $$ |\$$$$$$$ |
\______/ \______/ \__|\__| \_______| \_______|\__| \__| \__|\__|\__| \__|\__|\__| \__| \____$$ |
$$\ $$ |
\$$$$$$ |
\______/
__ __ __ __
___ / / ___ _ __ __ _ ___ _ __/ / ___ _/ /_ __ _____ __ __ ___ ____ / /_
(_-</ _ \/ _ \ |/|/ / / ' \/ -_) | |/|/ / _ \/ _ \/ __/ / // / _ \/ // / / _ \/ _ \/ __/
/___/_//_/\___/__,__/ /_/_/_/\__/ |__,__/_//_/\_,_/\__/ \_, /\___/\_,_/ \_, /\___/\__/
/___/ /___/
`
func main() {
var poolID int
var bindAddr string
var returnAddr string
var seedAddrsRaw string
var minPeers int
var maxPeers int
var targetDurPerBlock time.Duration
var recalcPeriod int
var speedArg string
var numMiners int
var filesPrefix string
flag.IntVar(&poolID, "poolid", 0, "The ID for a node within a single miner's pool (nodes with same pubkey).")
flag.StringVar(&bindAddr, "bindAddr", ":20403", "Local address to bind/listen on")
flag.StringVar(&returnAddr, "returnAddr", "", "External address (host:port) for peers to return connections")
flag.StringVar(&seedAddrsRaw, "seeds", "", "Seeding of potential peers for peer discovery. An optional comma-separated list of host/ips with port.")
flag.IntVar(&minPeers, "minpeers", 25, "The minimum number of peers to aim for; any fewer will trigger a peer discovery event")
flag.IntVar(&maxPeers, "maxpeers", 50, "The maximum number of peers to seed out to")
flag.DurationVar(&targetDurPerBlock, "targetdur", 10*time.Second, "The desired amount of time between block mining events; controls the difficulty of the mining")
flag.IntVar(&recalcPeriod, "recalc", 10, "How many blocks to solve before recalculating difficulty target")
flag.StringVar(&speedArg, "speed", "medium", "Speed of hashing, CPU usage. One of low/medium/high/ultra")
flag.IntVar(&numMiners, "miners", 1, "The number of concurrent miners to run, one per thread")
flag.StringVar(&filesPrefix, "filesprefix", "run", "Common prefix for all output files")
flag.Parse()
key := os.Getenv("BLOCKCHAIN_KEY")
if key == "" {
flag.Usage()
log.Fatal("Please set BLOCKCHAIN_KEY env variable")
}
if returnAddr == "" {
flag.Usage()
log.Fatal("please include returnAddr (external host:port) for peers to connect back to your node")
} else if _, _, err := net.SplitHostPort(returnAddr); err != nil {
flag.Usage()
log.Fatal("invalid returnAddr")
}
speed, err := mining.ToSpeed(speedArg)
if err != nil {
flag.Usage()
log.Fatal(err)
}
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
fmt.Println(ascii)
hb := sha256.Sum256([]byte(key))
pubkey := hex.EncodeToString(hb[:])
log.Printf("Your public key is: %s", pubkey)
if _, _, err := net.SplitHostPort(bindAddr); err != nil {
log.Fatalf("invalid bindAddr: %s", bindAddr)
}
hasher := chain.NewHasher()
filesPrefix = fmt.Sprintf("%s_%dp_%dm", targetDurPerBlock, recalcPeriod, numMiners)
miners := make([]mining.Miner, 0, numMiners)
for n := 0; n < numMiners; n++ {
miners = append(miners, mining.NewMiner(
n,
pubkey,
targetDurPerBlock,
speed,
hasher,
))
}
node := nodes.NewNode(
miners,
pubkey,
poolID,
minPeers,
maxPeers,
targetDurPerBlock,
recalcPeriod,
returnAddr,
strings.Split(seedAddrsRaw, ","),
speed,
filesPrefix,
hasher,
)
wg.Add(1)
go func() {
defer wg.Done()
node.Run(ctx)
}()
<-node.Ready()
gsrv := grpc.NewServer()
pb.RegisterNodeServer(gsrv, node)
lis, err := net.Listen("tcp", bindAddr)
if err != nil {
log.Fatal(err)
}
wg.Add(1)
go func() {
log.Printf("gRPC server listening on %s", bindAddr)
if err := gsrv.Serve(lis); err != nil {
log.Printf("gRPC server: %v", err)
}
wg.Done()
}()
sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
go cancel()
go func() {
gsrv.GracefulStop()
}()
}()
wg.Wait()
}