func exportDChain(chain *common.DChain) { if len(chain.Blocks) == 0 || procLog.Level() < factomlog.Info { //log.Println("no blocks to save for chain: " + string (*chain.ChainID)) return } // get all ecBlocks from db dBlocks, _ := db.FetchAllDBlocks() sort.Sort(util.ByDBlockIDAccending(dBlocks)) for _, block := range dBlocks { data, err := block.MarshalBinary() if err != nil { panic(err) } strChainID := chain.ChainID.String() if fileNotExists(dataStorePath + strChainID) { err := os.MkdirAll(dataStorePath+strChainID, 0777) if err == nil { procLog.Info("Created directory " + dataStorePath + strChainID) } else { procLog.Error(err) } } err = ioutil.WriteFile(fmt.Sprintf(dataStorePath+strChainID+"/store.%09d.block", block.Header.DBHeight), data, 0777) if err != nil { panic(err) } } }
// 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.DBHeight != 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.DBHeight { panic(errors.New("BlockID does not equal index for chain:" + dchain.ChainID.String() + " block:" + fmt.Sprintf("%v", dchain.Blocks[i].Header.DBHeight))) } } //Create an empty block and append to the chain if len(dchain.Blocks) == 0 { dchain.NextDBHeight = 0 dchain.NextBlock, _ = common.CreateDBlock(dchain, nil, 10) } else { dchain.NextDBHeight = 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.NextDBHeight-1, dchain.NextBlock.Header.PrevLedgerKeyMR) } exportDChain(dchain) //Double check the sealed flag if dchain.NextBlock.IsSealed == true { panic("dchain.Blocks[dchain.NextBlockID].IsSealed for chain:" + dchain.ChainID.String()) } }