/
cliactions.go
368 lines (343 loc) · 9.51 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
package main
import (
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"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.Fatal("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.Fatal("Not enough arguments: expecing chainparams.json")
}
actionNewChain(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.Fatal(err)
}
dbEnsureBlockchainTables(db)
keypair, publicKeyHash, err := cryptoGetAPrivateKey()
if err != nil {
log.Fatal(err)
}
lastBlockHeight := dbGetBlockchainHeight()
dbb, err := dbGetBlockByHeight(lastBlockHeight)
if err != nil {
log.Fatal(err)
}
if err = dbSetMetaInt(db, "Version", CurrentBlockVersion); err != nil {
log.Panic(err)
}
err = dbSetMetaString(db, "PreviousBlockHash", dbb.Hash)
if err != nil {
log.Fatal(err)
}
signature, err := cryptoSignHex(keypair, dbb.Hash)
if err != nil {
log.Fatal(err)
}
err = dbSetMetaString(db, "PreviousBlockHashSignature", signature)
if err != nil {
log.Fatal(err)
}
pkdb, err := dbGetPublicKey(publicKeyHash)
if err != nil {
log.Panic(err)
}
previousBlockHashSignature, err := hex.DecodeString(signature)
if err != nil {
log.Fatal(err)
}
if creatorString, ok := pkdb.metadata["BlockCreator"]; ok {
err = dbSetMetaString(db, "Creator", creatorString)
if err != nil {
log.Fatal(err)
}
}
err = dbSetMetaString(db, "CreatorPublicKey", pkdb.publicKeyHash)
if err != nil {
log.Fatal(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 := 1; h <= dbGetBlockchainHeight(); 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{})
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)")
}
// Shows the public keys which correspond to private keys in the system database.
func actionMyKeys() {
for _, k := range dbGetMyPublicKeys() {
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.Fatal(err)
}
ncp := NewChainParams{}
err = json.Unmarshal(jsonData, &ncp)
if err != nil {
log.Fatal(err)
}
if ncp.GenesisBlockTimestamp == "" {
ncp.GenesisBlockTimestamp = time.Now().Format(time.RFC3339)
}
if ncp.CreatorPublicKey != "" || ncp.GenesisBlockHash != "" || ncp.GenesisBlockHashSignature != "" {
log.Fatal("chainparams.json must not contain cryptographic properties")
}
log.Println("Creating a new blockchain from", jsonFilename)
empty, err := isDirEmpty(cfg.DataDir)
if err != nil {
log.Fatal(err)
}
if !empty {
log.Fatal("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.Fatal(err)
}
freshDb = false
}
// Modify the new genesis db to include the metadata
blockFilename := fmt.Sprintf(blockFilenameFormat, blockchainSubdirectory, 0)
log.Println("Creating the genesis block at", blockFilename)
db, err := dbOpen(blockFilename, false)
if err != nil {
log.Fatal("dbOpen", err)
}
if freshDb {
_, err = db.Exec("PRAGMA page_size=512")
if err != nil {
log.Fatal(err)
}
}
_, err = db.Exec("PRAGMA journal_mode=DELETE")
if err != nil {
log.Fatal(err)
}
dbEnsureBlockchainTables(db)
err = dbSetMetaInt(db, "Version", CurrentBlockVersion)
if err != nil {
log.Fatal(err)
}
err = dbSetMetaString(db, "PreviousBlockHash", GenesisBlockPreviousBlockHash)
if err != nil {
log.Fatal(err)
}
err = dbSetMetaString(db, "Creator", ncp.Creator)
if err != nil {
log.Fatal(err)
}
err = dbSetMetaString(db, "Timestamp", ncp.GenesisBlockTimestamp)
if err != nil {
log.Fatal(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 := dbGetMyPublicKeys()
if len(pubKeys) != 1 {
log.Fatal("There should have been only one genesis keypair, found", len(pubKeys))
}
err = dbSetMetaString(db, "CreatorPublicKey", pubKeys[0])
pKey, onePubKey, err := cryptoGetAPrivateKey()
if err != nil {
log.Fatal(err)
}
if onePubKey != pubKeys[0] {
log.Fatal("The impossible has happened: two attempts to get the single public key have different results:", pubKeys[0], onePubKey)
}
log.Println("Genesis public key:", onePubKey)
prevSig, err := cryptoSignHex(pKey, GenesisBlockPreviousBlockHash)
if err != nil {
log.Fatal("cryptoSignHex", err)
}
err = dbSetMetaString(db, "PreviousBlockHashSignature", prevSig)
if err != nil {
log.Fatal(err)
}
err = dbSetMetaString(db, "CreatorPubKey", onePubKey)
if err != nil {
log.Fatal(err)
}
err = db.Close()
if err != nil {
log.Fatal(err)
}
// Hash it, sign it, generate chainparams
hash, err := hashFileToHexString(blockFilename)
if err != nil {
log.Fatal(err)
}
ncp.GenesisBlockHash = hash
ncp.CreatorPublicKey = onePubKey
ncp.GenesisBlockHashSignature, err = cryptoSignHex(pKey, hash)
if err != nil {
log.Fatal(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.Fatal(err)
}
err = ioutil.WriteFile(fmt.Sprintf("%s/%s", cfg.DataDir, chainParamsBaseName), cpJSON, 0644)
if err != nil {
log.Fatal(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: onePubKey,
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.")
}