-
Notifications
You must be signed in to change notification settings - Fork 3
/
jeffCoin.go
299 lines (254 loc) · 8.78 KB
/
jeffCoin.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// jeffcoin.go
package main
import (
"flag"
"fmt"
"os"
"time"
log "github.com/sirupsen/logrus"
blockchain "github.com/JeffDeCola/jeffCoin/blockchain"
routingnode "github.com/JeffDeCola/jeffCoin/routingnode"
testblockchain "github.com/JeffDeCola/jeffCoin/testblockchain"
wallet "github.com/JeffDeCola/jeffCoin/wallet"
webserver "github.com/JeffDeCola/jeffCoin/webserver"
)
const (
toolVersion = "1.3.5"
)
var genesisPtr, testblockchainPtr, gcePtr, walletPtr *bool
var nodeNamePtr, nodeIPPtr, nodeHTTPPortPtr, nodeTCPPortPtr *string
var networkIPPtr, networkTCPPortPtr *string
var nodePasswordPtr *string
func checkErr(err error) {
if err != nil {
fmt.Printf("Error is %+v\n", err)
log.Fatal("ERROR:", err)
}
}
func genesisNode(jeffCoinAddress string) {
genesisBlockDataString := `
{
"blockID": 0,
"timestamp": "",
"transactions": [
{
"txID": 0,
"inputs": [
{
"refTxID": -1,
"inPubKey": "",
"signature": "Hello World, Welcome to the first transaction and block of jeffCoin"
}
],
"outputs": [
{
"outPubKey": "` + jeffCoinAddress + `",
"value": 100000000
}
]
}
],
"hash": "",
"prevhash": "",
"difficulty": 8,
"nonce": ""
}`
// GENESIS blockchain
s := "GENESIS blockchain"
log.Info("MAIN: " + s)
blockchain.GenesisBlockchain(genesisBlockDataString)
// GENESIS nodeList
s = "GENESIS nodeList"
log.Info("MAIN: " + s)
routingnode.GenesisNodeList()
}
func newNode() {
// REQUEST nodeList FROM THE NETWORK
s := "REQUEST nodeList FROM THE NETWORK"
log.Info("MAIN: " + s)
err := routingnode.RequestNodeList(*networkIPPtr, *networkTCPPortPtr)
checkErr(err)
// BROADCAST thisNode TO THE NETWORK
s = "BROADCAST thisNode TO THE NETWORK"
log.Info("MAIN: " + s)
err = routingnode.BroadcastThisNode()
checkErr(err)
// APPEND thisNode to nodeList
s = "APPEND thisNode to nodeList"
log.Info("MAIN: " + s)
routingnode.AppendThisNode()
// REQUEST blockchain FROM THE NETWORK
s = "REQUEST blockchain FROM THE NETWORK"
log.Info("MAIN: " + s)
err = blockchain.RequestBlockchain(*networkIPPtr, *networkTCPPortPtr)
checkErr(err)
}
// masterFlowControl - Controls the mining of blocks and addint to the chain
func masterFlowControl(genesis bool) {
// START IT UP
if genesis {
}
}
func init() {
// SET FORMAT
log.SetFormatter(&log.TextFormatter{})
// log.SetFormatter(&log.JSONFormatter{})
// SET OUTPUT (DEFAULT stderr)
log.SetOutput(os.Stdout)
// GCE
gcePtr = flag.Bool("gce", false, "Is this Node on GCE")
// CREATE FIRST NODE (GENISIS)
genesisPtr = flag.Bool("genesis", false, "Create your first Node")
// LOG LEVEL
logLevelPtr := flag.String("loglevel", "info", "LogLevel (info, debug or trace)")
// NETWORK NODE IP
networkIPPtr = flag.String("networkip", "127.0.0.1", "Network IP")
// NETWORK NODE TCP PORT
networkTCPPortPtr = flag.String("networktcpport", "3000", "Network TCP Port")
// YOUR NODE WEB PORT
nodeHTTPPortPtr = flag.String("nodehttpport", "2001", "Node Web Port")
// YOUR NODE IP
nodeIPPtr = flag.String("nodeip", "127.0.0.1", "Node IP")
// NODE NAME
nodeNamePtr = flag.String("nodename", "Jeff", "Node Name")
// PASSWORD
nodePasswordPtr = flag.String("nodepassword", "yourpassword", "Set/Reset your Password")
// YOUR NODE TCP PORT
nodeTCPPortPtr = flag.String("nodetcpport", "3001", "Node TCP Port")
// TEST FLAG
testblockchainPtr = flag.Bool("testblockchain", false, "Loads the blockchain with test data")
// VERSION FLAG
versionPtr := flag.Bool("v", false, "prints current version")
// VERSION FLAG
walletPtr = flag.Bool("wallet", false, "Only the wallet and webserver (GUI/API)")
// Parse the flags
flag.Parse()
// SET LOG LEVEL
if *logLevelPtr == "trace" {
log.SetLevel(log.TraceLevel)
} else if *logLevelPtr == "debug" {
log.SetLevel(log.DebugLevel)
} else if *logLevelPtr == "info" {
log.SetLevel(log.InfoLevel)
} else {
log.Error("Error setting log levels")
os.Exit(0)
}
// CHECK VERSION
if *versionPtr {
fmt.Println(toolVersion)
os.Exit(0)
}
// MUST HAVE A PASSWORD
if *nodePasswordPtr == "yourpassword" {
log.Error("You must supply a Node Password")
os.Exit(0)
}
}
func main() {
fmt.Printf("\nSTART...\n")
fmt.Printf("Press return to exit\n\n")
// DO YOU HAVE A PASSWORD HASH FILE
if _, err := os.Stat("credentials/" + *nodeNamePtr + "-password-hash.json"); err == nil {
// Read password hash from a file and put in struct
// Will also check password hash
s := "READ existing password hash from a file and put in struct"
log.Info("MAIN: " + s)
webserver.ReadPasswordFile(*nodeNamePtr, *nodePasswordPtr)
} else {
// Write the password hash to a file and put in struct
s := "Writes the password hash to a file"
log.Info("MAIN: " + s)
webserver.WritePasswordFile(*nodeNamePtr, *nodePasswordPtr)
}
// DO YOU HAVE A WALLET FILE
if _, err := os.Stat("credentials/" + *nodeNamePtr + "-wallet-encrypted.json"); err == nil {
// READ existing wallet from a file (Keys and jeffCoin Address) and put in struct
// Will also decrypt encrypted Private Key in file
s := "READ existing wallet from a file (Keys and jeffCoin Address) and put in struct"
log.Info("MAIN: " + s)
wallet.ReadWalletFile(*nodeNamePtr, *nodePasswordPtr)
} else {
// GENESIS wallet - Creates the wallet and write to file (Keys and jeffCoin Address)
// Will also encrypt Private Key for file
s := "GENESIS wallet - Creates the wallet and write to file (Keys and jeffCoin Address)"
log.Info("MAIN: " + s)
wallet.GenesisWallet(*nodeNamePtr, *nodePasswordPtr)
}
// "DESTROY" PASSWORD
*nodePasswordPtr = "destroy"
s := "Node Password destroyed"
log.Info("MAIN: " + s)
// START WEBSERVER (HTTP SERVER)
s = "START WEBSERVER (HTTP SERVER)"
log.Info("MAIN: " + s)
if *gcePtr {
go webserver.StartHTTPServer("0.0.0.0", *nodeHTTPPortPtr)
} else {
go webserver.StartHTTPServer(*nodeIPPtr, *nodeHTTPPortPtr)
}
// GIVE IT A SECOND
time.Sleep(1 * time.Second)
// START ROUTING NODE (TCP SERVER)
// Do not start if wallet only
if !*walletPtr {
s = "START ROUTING NODE (TCP SERVER)"
log.Info("MAIN: " + s)
if *gcePtr {
go routingnode.StartRoutingNode("0.0.0.0", *nodeTCPPortPtr)
} else {
go routingnode.StartRoutingNode(*nodeIPPtr, *nodeTCPPortPtr)
}
}
// GIVE IT A SECOND
time.Sleep(1 * time.Second)
// LOAD thisNode
s = "LOAD thisNode"
log.Info("MAIN: " + s)
myNode := routingnode.NodeStruct{
NodeName: *nodeNamePtr,
NodeIP: *nodeIPPtr,
NodeHTTPPort: *nodeHTTPPortPtr,
NodeTCPPort: *nodeTCPPortPtr,
NetworkIP: *networkIPPtr,
NetworkTCPPort: *networkTCPPortPtr,
TestBlockChain: *testblockchainPtr,
WalletOnly: *walletPtr,
ToolVersion: toolVersion,
}
myNode.LoadThisNode()
// CREATE GENESIS NODE OR A NEW NODE
// Do not start if wallet only
if !*walletPtr {
if *genesisPtr {
theWallet := wallet.GetWallet()
genesisNode(theWallet.PublicKeyHex)
} else {
newNode()
}
}
// TESTING - LOAD BLOCKCHAIN WITH TEST DATA
if *testblockchainPtr {
testblockchain.LoadTestDatatoBlockchain()
// SHOW BALANCES
balance := blockchain.GetAddressBalance(testblockchain.MockFoundersPubKey)
log.Info("\n\nThe balance for " + testblockchain.MockFoundersPubKey[0:40] + "... (Address) is " + balance + "\n\n")
balance = blockchain.GetAddressBalance(testblockchain.MockJeffPubKey)
log.Info("\n\nThe balance for " + testblockchain.MockJeffPubKey[0:40] + "... (Address) is " + balance + "\n\n")
balance = blockchain.GetAddressBalance(testblockchain.MockMattPubKey)
log.Info("\n\nThe balance for " + testblockchain.MockMattPubKey[0:40] + "... (Address) is " + balance + "\n\n")
balance = blockchain.GetAddressBalance(testblockchain.MockJillPubKey)
log.Info("\n\nThe balance for " + testblockchain.MockJillPubKey[0:40] + "... (Address) is " + balance + "\n\n")
balance = blockchain.GetAddressBalance(testblockchain.MockCoinVaultPubKey)
log.Info("\n\nThe balance for " + testblockchain.MockCoinVaultPubKey[0:40] + "... (Address) is " + balance + "\n\n")
}
// KICK OFF MASTER CONTROL
s = "KICK OFF MASTER CONTROL"
log.Info("MAIN: " + s)
go masterFlowControl(*genesisPtr)
// PRESS RETURN TO EXIT
s = "PRESS RETURN TO EXIT"
log.Info("MAIN: " + s)
fmt.Scanln()
fmt.Printf("\n...DONE\n")
}