forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.go
More file actions
432 lines (348 loc) · 12.5 KB
/
init.go
File metadata and controls
432 lines (348 loc) · 12.5 KB
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
// Copyright 2015 FactomProject Authors. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package process
import (
"errors"
"fmt"
"github.com/FactomProject/FactomCode/common"
"github.com/FactomProject/FactomCode/consensus"
"github.com/FactomProject/FactomCode/factomlog"
"github.com/FactomProject/FactomCode/util"
"github.com/FactomProject/btcd/wire"
fct "github.com/FactomProject/factoid"
"github.com/FactomProject/factoid/block"
"sort"
"strconv"
)
// Initialize Directory Block Chain from database
func initDChain() {
dchain = new(common.DChain)
//Initialize the Directory Block Chain ID
dchain.ChainID = new(common.Hash)
barray := common.D_CHAINID
dchain.ChainID.SetBytes(barray)
// get all dBlocks from db
dBlocks, _ := db.FetchAllDBlocks()
sort.Sort(util.ByDBlockIDAccending(dBlocks))
dchain.Blocks = make([]*common.DirectoryBlock, len(dBlocks), len(dBlocks)+1)
for i := 0; i < len(dBlocks); i = i + 1 {
if dBlocks[i].Header.BlockHeight != uint32(i) {
panic("Error in initializing dChain:" + dchain.ChainID.String())
}
dBlocks[i].Chain = dchain
dBlocks[i].IsSealed = true
dBlocks[i].IsSavedInDB = true
dchain.Blocks[i] = &dBlocks[i]
}
// double check the block ids
for i := 0; i < len(dchain.Blocks); i = i + 1 {
if uint32(i) != dchain.Blocks[i].Header.BlockHeight {
panic(errors.New("BlockID does not equal index for chain:" + dchain.ChainID.String() + " block:" + fmt.Sprintf("%v", dchain.Blocks[i].Header.BlockHeight)))
}
}
//Create an empty block and append to the chain
if len(dchain.Blocks) == 0 {
dchain.NextBlockHeight = 0
dchain.NextBlock, _ = common.CreateDBlock(dchain, nil, 10)
// Update dir block height cache in db
h, _ := common.HexToHash(common.GENESIS_DIR_BLOCK_HASH)
db.UpdateBlockHeightCache(0, h)
} else {
dchain.NextBlockHeight = uint32(len(dchain.Blocks))
dchain.NextBlock, _ = common.CreateDBlock(dchain, dchain.Blocks[len(dchain.Blocks)-1], 10)
// Update dir block height cache in db
db.UpdateBlockHeightCache(dchain.NextBlockHeight-1, dchain.NextBlock.Header.PrevBlockHash)
}
exportDChain(dchain)
//Double check the sealed flag
if dchain.NextBlock.IsSealed == true {
panic("dchain.Blocks[dchain.NextBlockID].IsSealed for chain:" + dchain.ChainID.String())
}
}
// Initialize Entry Credit Block Chain from database
func initECChain() {
eCreditMap = make(map[string]int32)
//Initialize the Entry Credit Chain ID
ecchain = common.NewECChain()
// get all ecBlocks from db
ecBlocks, _ := db.FetchAllECBlocks()
sort.Sort(util.ByECBlockIDAccending(ecBlocks))
for i, v := range ecBlocks {
if v.Header.DBHeight != uint32(i) {
panic("Error in initializing dChain:" + ecchain.ChainID.String() + " DBHeight:" + strconv.Itoa(int(v.Header.DBHeight)) + " i:" + strconv.Itoa(i))
}
// Calculate the EC balance for each account
initializeECreditMap(&v)
}
//Create an empty block and append to the chain
if len(ecBlocks) == 0 || dchain.NextBlockHeight == 0 {
ecchain.NextBlockHeight = 0
ecchain.NextBlock = common.NewECBlock()
} else {
// Entry Credit Chain should have the same height as the dir chain
ecchain.NextBlockHeight = dchain.NextBlockHeight
ecchain.NextBlock = common.NextECBlock(&ecBlocks[ecchain.NextBlockHeight-1])
}
// create a backup copy before processing entries
copyCreditMap(eCreditMap, eCreditMapBackup)
exportECChain(ecchain)
// ONly for debugging
if procLog.Level() > factomlog.Info {
printCreditMap()
}
}
// Initialize Admin Block Chain from database
func initAChain() {
//Initialize the Admin Chain ID
achain = new(common.AdminChain)
achain.ChainID = new(common.Hash)
achain.ChainID.SetBytes(common.ADMIN_CHAINID)
// get all aBlocks from db
aBlocks, _ := db.FetchAllABlocks()
sort.Sort(util.ByABlockIDAccending(aBlocks))
// double check the block ids
for i := 0; i < len(aBlocks); i = i + 1 {
if uint32(i) != aBlocks[i].Header.DBHeight {
panic(errors.New("BlockID does not equal index for chain:" + achain.ChainID.String() + " block:" + fmt.Sprintf("%v", aBlocks[i].Header.DBHeight)))
}
}
//Create an empty block and append to the chain
if len(aBlocks) == 0 || dchain.NextBlockHeight == 0 {
achain.NextBlockHeight = 0
achain.NextBlock, _ = common.CreateAdminBlock(achain, nil, 10)
} else {
// Entry Credit Chain should have the same height as the dir chain
achain.NextBlockHeight = dchain.NextBlockHeight
achain.NextBlock, _ = common.CreateAdminBlock(achain, &aBlocks[achain.NextBlockHeight-1], 10)
}
exportAChain(achain)
}
// Initialize Factoid Block Chain from database
func initFctChain() {
//Initialize the Admin Chain ID
fchain = new(common.FctChain)
fchain.ChainID = new(common.Hash)
fchain.ChainID.SetBytes(fct.FACTOID_CHAINID)
// get all aBlocks from db
fBlocks, _ := db.FetchAllFBlocks()
sort.Sort(util.ByFBlockIDAccending(fBlocks))
// double check the block ids
for i := 0; i < len(fBlocks); i = i + 1 {
if uint32(i) != fBlocks[i].GetDBHeight() {
panic(errors.New("BlockID does not equal index for chain:" + fchain.ChainID.String() + " block:" + fmt.Sprintf("%v", fBlocks[i].GetDBHeight())))
}else {
// initialize the FactoidState in sequence
err := common.FactoidState.AddTransactionBlock(fBlocks[i])
if err != nil {
panic("Failed to rebuild factoid state: " +err.Error());
}
}
}
//Create an empty block and append to the chain
if len(fBlocks) == 0 || dchain.NextBlockHeight == 0 {
fchain.NextBlockHeight = 0
// THIS IS IN TWO PLACES HERE! THEY NEED TO MATCH!
fchain.NextBlock = block.GetGenesisBlock(
0,1000000, 10, 200000000000)
} else {
// Entry Credit Chain should have the same height as the dir chain
fchain.NextBlockHeight = dchain.NextBlockHeight
fchain.NextBlock = block.NewFBlock(FactoshisPerCredit, dchain.NextBlockHeight)
}
exportFctChain(fchain)
}
// Initialize Entry Block Chains from database
func initEChains() {
chainIDMap = make(map[string]*common.EChain)
chains, err := db.FetchAllChains()
if err != nil {
panic(err)
}
for _, chain := range chains {
var newChain = chain
chainIDMap[newChain.ChainID.String()] = &newChain
exportEChain(&chain)
}
}
// Re-calculate Entry Credit Balance Map with a new Entry Credit Block
func initializeECreditMap(block *common.ECBlock) {
for _, entry := range block.Body.Entries {
// Only process: ECIDChainCommit, ECIDEntryCommit, ECIDBalanceIncrease
switch entry.ECID() {
case common.ECIDChainCommit:
e := entry.(*common.CommitChain)
eCreditMap[string(e.ECPubKey[:])] += int32(e.Credits)
common.FactoidState.UpdateECBalance(fct.NewAddress(e.ECPubKey[:]), int64(e.Credits))
case common.ECIDEntryCommit:
e := entry.(*common.CommitEntry)
eCreditMap[string(e.ECPubKey[:])] += int32(e.Credits)
common.FactoidState.UpdateECBalance(fct.NewAddress(e.ECPubKey[:]), int64(e.Credits))
case common.ECIDBalanceIncrease:
fmt.Println("\nIncreases!!!!\n")
e := entry.(*common.IncreaseBalance)
eCreditMap[string(e.ECPubKey[:])] += int32(e.Credits)
// Don't add the Increases to Factoid state, the Factoid processing will do that.
default:
fmt.Println("UNKNOWN\n")
}
}
}
// Initialize server private key and server public key for milestone 1
func initServerKeys() {
if nodeMode == common.SERVER_NODE {
var err error
serverPrivKey, err = common.NewPrivateKeyFromHex(serverPrivKeyHex)
if err != nil {
panic("Cannot parse Server Private Key from configuration file: " + err.Error())
}
} else {
serverPubKey = common.PubKeyFromString(common.SERVER_PUB_KEY)
}
}
// Initialize the process list manager with the proper dir block height
func initProcessListMgr() {
plMgr = consensus.NewProcessListMgr(dchain.NextBlockHeight, 1, 10)
}
// Initialize the entry chains in memory from db
func initEChainFromDB(chain *common.EChain) {
eBlocks, _ := db.FetchAllEBlocksByChain(chain.ChainID)
sort.Sort(util.ByEBlockIDAccending(*eBlocks))
for i := 0; i < len(*eBlocks); i = i + 1 {
if uint32(i) != (*eBlocks)[i].Header.EBHeight {
panic(errors.New("BlockID does not equal index for chain:" + chain.ChainID.String() + " block:" + fmt.Sprintf("%v", (*eBlocks)[i].Header.EBHeight)))
}
}
if len(*eBlocks) == 0 {
chain.NextBlockHeight = 0
chain.NextBlock, _ = common.CreateBlock(chain, nil, 10)
} else {
chain.NextBlockHeight = uint32(len(*eBlocks))
chain.NextBlock, _ = common.CreateBlock(chain, &(*eBlocks)[len(*eBlocks)-1], 10)
}
// Initialize chain with the first entry (Name and rules) for non-server mode
if nodeMode != common.SERVER_NODE && chain.FirstEntry == nil && len(*eBlocks) > 0 {
chain.FirstEntry, _ = db.FetchEntryByHash((*eBlocks)[0].EBEntries[0].EntryHash)
if chain.FirstEntry != nil {
db.InsertChain(chain)
}
}
if chain.NextBlock.IsSealed == true {
panic("chain.NextBlock.IsSealed for chain:" + chain.ChainID.String())
}
}
// Validate dir chain from genesis block
func validateDChain(c *common.DChain) error {
if nodeMode != common.SERVER_NODE && len(c.Blocks) == 0 {
return nil
}
if uint32(len(c.Blocks)) != c.NextBlockHeight {
return errors.New("Dir chain has an un-expected Next Block ID: " + strconv.Itoa(int(c.NextBlockHeight)))
}
//prevMR and prevBlkHash are used to validate against the block next in the chain
prevMR, prevBlkHash, err := validateDBlock(c, c.Blocks[0])
if err != nil {
return err
}
//validate the genesis block
//prevBlkHash is the block hash for c.Blocks[0]
if prevBlkHash == nil || prevBlkHash.String() != common.GENESIS_DIR_BLOCK_HASH {
panic("Genesis dir block is not as expected: " + prevBlkHash.String())
}
for i := 1; i < len(c.Blocks); i++ {
if !prevBlkHash.IsSameAs(c.Blocks[i].Header.PrevBlockHash) {
return errors.New("Previous block hash not matching for Dir block: " + strconv.Itoa(i))
}
if !prevMR.IsSameAs(c.Blocks[i].Header.PrevKeyMR) {
return errors.New("Previous merkle root not matching for Dir block: " + strconv.Itoa(i))
}
mr, dblkHash, err := validateDBlock(c, c.Blocks[i])
if err != nil {
c.Blocks[i].IsValidated = false
return err
}
prevMR = mr
prevBlkHash = dblkHash
c.Blocks[i].IsValidated = true
}
return nil
}
// Validate a dir block
func validateDBlock(c *common.DChain, b *common.DirectoryBlock) (merkleRoot *common.Hash, dbHash *common.Hash, err error) {
bodyMR, err := b.BuildBodyMR()
if err != nil {
return nil, nil, err
}
if !b.Header.BodyMR.IsSameAs(bodyMR) {
return nil, nil, errors.New("Invalid body MR for dir block: " + string(b.Header.BlockHeight))
}
for _, dbEntry := range b.DBEntries {
switch dbEntry.ChainID.String() {
case ecchain.ChainID.String():
err := validateCBlockByMR(dbEntry.MerkleRoot)
if err != nil {
return nil, nil, err
}
case achain.ChainID.String():
err := validateABlockByMR(dbEntry.MerkleRoot)
if err != nil {
return nil, nil, err
}
case wire.FChainID.String():
err := validateFBlockByMR(dbEntry.MerkleRoot)
if err != nil {
return nil, nil, err
}
default:
err := validateEBlockByMR(dbEntry.ChainID, dbEntry.MerkleRoot)
if err != nil {
return nil, nil, err
}
}
}
b.DBHash, _ = common.CreateHash(b)
b.BuildKeyMerkleRoot()
return b.KeyMR, b.DBHash, nil
}
// Validate Entry Credit Block by merkle root
func validateCBlockByMR(mr *common.Hash) error {
cb, _ := db.FetchECBlockByHash(mr)
if cb == nil {
return errors.New("Entry Credit block not found in db for merkle root: " + mr.String())
}
return nil
}
// Validate Admin Block by merkle root
func validateABlockByMR(mr *common.Hash) error {
b, _ := db.FetchABlockByHash(mr)
if b == nil {
return errors.New("Admin block not found in db for merkle root: " + mr.String())
}
return nil
}
// Validate FBlock by merkle root
func validateFBlockByMR(mr *common.Hash) error {
b, _ := db.FetchFBlockByHash(mr)
if b == nil {
return errors.New("Simple Coin block not found in db for merkle root: " + mr.String())
}
return nil
}
// Validate Entry Block by merkle root
func validateEBlockByMR(cid *common.Hash, mr *common.Hash) error {
eb, _ := db.FetchEBlockByMR(mr)
if eb == nil {
return errors.New("Entry block not found in db for merkle root: " + mr.String())
}
eb.BuildMerkleRoot()
if !mr.IsSameAs(eb.MerkleRoot) {
return errors.New("Entry block's merkle root does not match with: " + mr.String())
}
for _, ebEntry := range eb.EBEntries {
entry, _ := db.FetchEntryByHash(ebEntry.EntryHash)
if entry == nil {
return errors.New("Entry not found in db for entry hash: " + ebEntry.EntryHash.String())
}
}
return nil
}