-
Notifications
You must be signed in to change notification settings - Fork 43
/
cliactions.go
542 lines (502 loc) · 14.4 KB
/
cliactions.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package main
import (
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"reflect"
"strings"
"time"
)
// The binary can be called with some actions, like signblock, importblock, signkey.
// This function processes those and returns true if it has found something to execute.
// The processActions() function is called after the blockchain database is initialised
// and active.
func processActions() bool {
if flag.NArg() == 0 {
return false
}
cmd := flag.Arg(0)
switch cmd {
case "help":
actionHelp()
return true
case "mykeys":
actionMyKeys()
return true
case "query":
actionQuery(flag.Arg(1))
return true
case "signimportblock":
if flag.NArg() < 2 {
log.Fatalln("Not enough arguments: expecting <sqlite db filename>")
}
actionSignImportBlock(flag.Arg(1))
return true
}
return false
}
// processPreBlockchainActions is called to process actions which need to executed
// before the blockchain database is running.
func processPreBlockchainActions() bool {
if flag.NArg() == 0 {
return false
}
cmd := flag.Arg(0)
switch cmd {
case "newchain":
if flag.NArg() < 2 {
log.Fatalln("Not enough arguments: expecing chainparams.json")
}
actionNewChain(flag.Arg(1))
return true
case "pull":
if flag.NArg() < 2 {
log.Fatalln("Not enough arguments: expecting chain URL")
}
actionPull(flag.Arg(1))
return true
}
return false
}
// Opens the given block file (SQLite database), creates metadata tables in it, signes the
// block with one of the private keys, and accepts the resulting block into the blockchain.
func actionSignImportBlock(fn string) {
db, err := dbOpen(fn, false)
if err != nil {
log.Fatalln(err)
}
dbEnsureBlockchainTables(db)
keypair, publicKeyHash, err := cryptoGetAPrivateKey()
if err != nil {
log.Fatalln(err)
}
lastBlockHeight := dbGetBlockchainHeight()
dbb, err := dbGetBlockByHeight(lastBlockHeight)
if err != nil {
log.Fatalln(err)
}
if err = dbSetMetaInt(db, "Version", CurrentBlockVersion); err != nil {
log.Panic(err)
}
err = dbSetMetaString(db, "PreviousBlockHash", dbb.Hash)
if err != nil {
log.Fatalln(err)
}
signature, err := cryptoSignHex(keypair, dbb.Hash)
if err != nil {
log.Fatalln(err)
}
err = dbSetMetaString(db, "PreviousBlockHashSignature", signature)
if err != nil {
log.Fatalln(err)
}
err = dbSetMetaString(db, "Timestamp", time.Now().Format(time.RFC3339))
if err != nil {
log.Fatalln(err)
}
pkdb, err := dbGetPublicKey(publicKeyHash)
if err != nil {
log.Panic(err)
}
previousBlockHashSignature, err := hex.DecodeString(signature)
if err != nil {
log.Fatalln(err)
}
if creatorString, ok := pkdb.metadata["BlockCreator"]; ok {
err = dbSetMetaString(db, "Creator", creatorString)
if err != nil {
log.Fatalln(err)
}
}
err = dbSetMetaString(db, "CreatorPublicKey", pkdb.publicKeyHash)
if err != nil {
log.Fatalln(err)
}
if err = db.Close(); err != nil {
log.Panic(err)
}
blockHashHex, err := hashFileToHexString(fn)
if err != nil {
log.Panic(err)
}
signature, err = cryptoSignHex(keypair, blockHashHex)
if err != nil {
log.Panic(err)
}
blockHashSignature, _ := hex.DecodeString(signature)
newBlockHeight := lastBlockHeight + 1
newBlock := DbBlockchainBlock{Hash: blockHashHex, HashSignature: blockHashSignature, PreviousBlockHash: dbb.Hash, PreviousBlockHashSignature: previousBlockHashSignature,
Version: CurrentBlockVersion, SignaturePublicKeyHash: pkdb.publicKeyHash, Height: newBlockHeight, TimeAccepted: time.Now()}
err = blockchainCopyFile(fn, newBlockHeight)
if err != nil {
log.Panic(err)
}
err = dbInsertBlock(&newBlock)
if err != nil {
log.Panic(err)
}
}
// Runs a SQL query over all the blocks.
func actionQuery(q string) {
log.Println("Running query:", q)
errCount := 0
for h := dbGetBlockchainHeight(); h > 0; h-- {
fn := blockchainGetFilename(h)
db, err := dbOpen(fn, true)
if err != nil {
log.Panic(err)
}
rows, err := db.Query(q)
if err != nil {
errCount++
continue
}
cols, err := rows.Columns()
if err != nil {
log.Panic(err)
}
for rows.Next() {
columns := make([]interface{}, len(cols))
columnPointers := make([]interface{}, len(cols))
for i := range columns {
columnPointers[i] = &columns[i]
}
if err := rows.Scan(columnPointers...); err != nil {
log.Panic(err)
}
row := make(map[string]interface{})
for i, colName := range cols {
val := columnPointers[i].(*interface{})
if reflect.TypeOf(*val).String() == "[]uint8" {
row[colName] = string((*val).([]byte))
} else {
row[colName] = *val
}
}
buf, err := json.Marshal(row)
if err != nil {
log.Panic(err)
}
fmt.Println(string(buf))
}
}
if errCount != 0 {
log.Println("There have been", errCount, "errors.")
}
}
// Shows the help message.
func actionHelp() {
fmt.Printf("usage: %s [flags] [command]\n", os.Args[0])
flag.PrintDefaults()
fmt.Println("Commands:")
fmt.Println("\thelp\t\tShows this help message")
fmt.Println("\tmykeys\t\tShows a list of my public keys")
fmt.Println("\tquery\t\tExecutes a SQL query on the blockchain (expects 1 argument: SQL query)")
fmt.Println("\tsignimportblock\tSigns a block (creates metadata tables in it first) and imports it into the blockchain (expects 1 argument: a sqlite db filename)")
fmt.Println("\tnewchain\tStarts a new chain with the given parameters (expects 1 argument: chainparams.json)")
fmt.Println("\tpull\t\tPulls a blockchain from a HTTP URL (expects 1 argument: URL, e.g. http://example.com:2018/)")
}
// Shows the public keys which correspond to private keys in the system database.
func actionMyKeys() {
for _, k := range dbGetMyPublicKeyHashes() {
fmt.Println(k)
}
}
// NewChainParams is extended from ChainParams for new chain creation
type NewChainParams struct {
ChainParams
GenesisDb string `json:"genesis_db"`
}
func actionNewChain(jsonFilename string) {
jsonData, err := ioutil.ReadFile(jsonFilename)
if err != nil {
log.Fatalln(err)
}
ncp := NewChainParams{}
err = json.Unmarshal(jsonData, &ncp)
if err != nil {
log.Fatalln(err)
}
if ncp.GenesisBlockTimestamp == "" {
ncp.GenesisBlockTimestamp = time.Now().Format(time.RFC3339)
}
if ncp.CreatorPublicKey != "" || ncp.GenesisBlockHash != "" || ncp.GenesisBlockHashSignature != "" {
log.Fatalln("chainparams.json must not contain cryptographic properties")
}
log.Println("Creating a new blockchain from", jsonFilename)
empty, err := isDirEmpty(cfg.DataDir)
if err != nil {
log.Fatalln(err)
}
if !empty {
log.Fatalln("Data directory must not be empty:", cfg.DataDir)
}
ensureBlockchainSubdirectoryExists()
freshDb := true
if ncp.GenesisDb != "" && fileExists(ncp.GenesisDb) {
err = blockchainCopyFile(ncp.GenesisDb, 0)
if err != nil {
log.Fatalln(err)
}
freshDb = false
}
// Modify the new genesis db to include the metadata
blockFilename := blockchainGetFilename(0)
log.Println("Creating the genesis block at", blockFilename)
db, err := dbOpen(blockFilename, false)
if err != nil {
log.Fatalln("dbOpen", err)
}
if freshDb {
_, err = db.Exec("PRAGMA page_size=512")
if err != nil {
log.Fatalln(err)
}
}
_, err = db.Exec("PRAGMA journal_mode=DELETE")
if err != nil {
log.Fatalln(err)
}
dbEnsureBlockchainTables(db)
err = dbSetMetaInt(db, "Version", CurrentBlockVersion)
if err != nil {
log.Fatalln(err)
}
err = dbSetMetaString(db, "PreviousBlockHash", GenesisBlockPreviousBlockHash)
if err != nil {
log.Fatalln(err)
}
err = dbSetMetaString(db, "Creator", ncp.Creator)
if err != nil {
log.Fatalln(err)
}
err = dbSetMetaString(db, "Timestamp", ncp.GenesisBlockTimestamp)
if err != nil {
log.Fatalln(err)
}
err = dbSetMetaString(db, "Description", ncp.Description)
if err != nil {
log.Fatalln(err)
}
if len(ncp.BootstrapPeers) > 0 {
// bootstrapPeers is required to be filled in before dbInit()
bootstrapPeers = peerStringMap{}
for _, peer := range ncp.BootstrapPeers {
bootstrapPeers[peer] = time.Now()
}
}
dbInit() // Create system databases
cryptoInit() // Create the genesis keypair
pubKeys := dbGetMyPublicKeyHashes()
if len(pubKeys) != 1 {
log.Fatalln("There should have been only one genesis keypair, found", len(pubKeys))
}
err = dbSetMetaString(db, "CreatorPublicKey", pubKeys[0])
pKey, pubKeyHash, err := cryptoGetAPrivateKey()
if err != nil {
log.Fatalln(err)
}
if pubKeyHash != pubKeys[0] {
log.Fatalln("The impossible has happened: two attempts to get the single public key have different results:", pubKeys[0], pubKeyHash)
}
log.Println("Genesis public key:", pubKeyHash)
prevSig, err := cryptoSignHex(pKey, GenesisBlockPreviousBlockHash)
if err != nil {
log.Fatalln("cryptoSignHex", err)
}
err = dbSetMetaString(db, "PreviousBlockHashSignature", prevSig)
if err != nil {
log.Fatalln(err)
}
err = dbSetMetaString(db, "CreatorPubKey", pubKeyHash)
if err != nil {
log.Fatalln(err)
}
// Write the public key into the genesis block
pubKey, err := dbGetPublicKey(pubKeyHash)
if err != nil {
log.Fatalln("Error getting public key from db", err)
}
selfSig, err := cryptoSignPublicKeyHash(pKey, pubKeyHash)
if err != nil {
log.Fatalln("Error signing publicKey", err)
}
_, err = db.Exec("INSERT INTO _keys (op, pubkey_hash, pubkey, sigkey_hash, signature) VALUES (?, ?, ?, ?, ?)",
"A", pubKeyHash, hex.EncodeToString(pubKey.publicKeyBytes), pubKeyHash, hex.EncodeToString(selfSig))
if err != nil {
log.Fatalln("Error recording the genesis block public key")
}
err = db.Close()
if err != nil {
log.Fatalln(err)
}
// Hash it, sign it, generate chainparams
hash, err := hashFileToHexString(blockFilename)
if err != nil {
log.Fatalln(err)
}
ncp.GenesisBlockHash = hash
ncp.CreatorPublicKey = pubKeyHash
ncp.GenesisBlockHashSignature, err = cryptoSignHex(pKey, hash)
if err != nil {
log.Fatalln(err)
}
log.Println("Genesis block hash:", ncp.GenesisBlockHash)
// Save the chainparams to the data dir
cpJSON, err := json.Marshal(ncp.ChainParams)
if err != nil {
log.Fatalln(err)
}
err = ioutil.WriteFile(fmt.Sprintf("%s/%s", cfg.DataDir, chainParamsBaseName), cpJSON, 0644)
if err != nil {
log.Fatalln(err)
}
// Record the genesis block into the system database
newBlock := DbBlockchainBlock{
Hash: ncp.GenesisBlockHash,
HashSignature: mustDecodeHex(ncp.GenesisBlockHashSignature),
PreviousBlockHash: GenesisBlockPreviousBlockHash,
PreviousBlockHashSignature: mustDecodeHex(prevSig),
Version: CurrentBlockVersion,
SignaturePublicKeyHash: pubKeyHash,
Height: 0,
TimeAccepted: time.Now(),
}
err = dbInsertBlock(&newBlock)
if err != nil {
log.Panic(err)
}
// Reopen the database to verify
log.Println("Reloading to verify...")
blockchainInit(false)
// If we make it to here, everything's ok.
log.Println("All done.")
}
func actionPull(baseURL string) {
if !strings.HasSuffix(baseURL, "/") {
baseURL = baseURL + "/"
}
// Step 1: fetch chainparams
cpURL := fmt.Sprintf("%schainparams.json", baseURL)
resp, err := http.Get(cpURL)
if err != nil {
log.Fatalln("Error getting chainparams", cpURL, err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln("Error reading chainparams", cpURL, err)
}
err = json.Unmarshal(body, &chainParams)
if err != nil {
log.Println(string(body))
log.Fatalln("Error decoding chainparams", cpURL, err)
}
if chainParams.GenesisBlockHash == "" || chainParams.GenesisBlockHashSignature == "" {
log.Fatalln("Incomplete chainparams data", cpURL)
}
// Step 2: Fetch the genesis block
gbURL := fmt.Sprintf("%s/block/0", baseURL)
resp, err = http.Get(gbURL)
if err != nil {
log.Fatalln("Error getting genesis block", gbURL, err)
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln("Error reading genesis block", gbURL, err)
}
// Step 3: initialise data directories
if fileExists(cfg.DataDir) {
if empty, err := isDirEmpty(cfg.DataDir); err != nil || !empty {
log.Fatalln("Blockchain directory must be empty", cfg.DataDir)
}
}
if _, err = os.Stat(cfg.DataDir); err != nil {
log.Println("Data directory", cfg.DataDir, "doesn't exist, creating.")
err = os.Mkdir(cfg.DataDir, 0700)
if err != nil {
log.Panicln(err)
}
}
ensureBlockchainSubdirectoryExists()
blockFilename := blockchainGetFilename(0)
err = ioutil.WriteFile(blockFilename, body, 0664)
if err != nil {
log.Fatalln("Cannot write genesis block", blockFilename, err)
}
hash, err := hashFileToHexString(blockFilename)
if err != nil {
log.Fatalln(err)
}
if hash != chainParams.GenesisBlockHash {
log.Fatalln("Mismatching genesis block hash")
}
// Step 4: Initialise databases
dbInit()
dbClearSavedPeers()
cryptoInit()
blk, err := OpenBlockFile(blockFilename)
if err != nil {
log.Fatalln("Error opening genesis block", blockFilename, err, "--", cfg.DataDir, "is in inconsistent state")
}
kops, err := blk.dbGetKeyOps()
if err != nil {
log.Fatalln("Error reading genesis block keys", err)
}
if len(kops) == 0 {
log.Fatalln("No key ops in genesis block?!")
}
verified := false
for kHash, ops := range kops {
for _, op := range ops {
if op.op == "A" {
pubKey, err := cryptoDecodePublicKeyBytes(op.publicKeyBytes)
if err != nil {
log.Fatalln("Error decoding genesis block public key", kHash, err)
}
if chainParams.CreatorPublicKey != getPubKeyHash(op.publicKeyBytes) {
continue
}
if err = cryptoVerifyHex(pubKey, chainParams.GenesisBlockHash, chainParams.GenesisBlockHashSignature); err == nil {
verified = true
dbWritePublicKey(op.publicKeyBytes, chainParams.CreatorPublicKey, 0)
} else {
log.Fatalln("Error verifying genesis block signature", err)
}
}
}
}
if !verified {
log.Fatalln("Cannot verify genesis block signature")
}
blk.Close()
hashSignature, err := hex.DecodeString(chainParams.GenesisBlockHashSignature)
if err != nil {
log.Fatalln("Error hex-decoding hash signature", err)
}
blk.HashSignature = hashSignature
err = dbInsertBlock(blk.DbBlockchainBlock)
if err != nil {
log.Panic(err)
}
// Save the chainparams to the data dir
cpJSON, err := json.Marshal(chainParams)
if err != nil {
log.Fatalln(err)
}
err = ioutil.WriteFile(fmt.Sprintf("%s/%s", cfg.DataDir, chainParamsBaseName), cpJSON, 0644)
if err != nil {
log.Fatalln(err)
}
// Reopen the database to verify
log.Println("Reloading to verify...")
blockchainInit(false)
// If we make it to here, everything's ok.
log.Println("All done.")
}