func exportFctChain(chain *common.FctChain) { if procLog.Level() < factomlog.Info { return } // get all aBlocks from db FBlocks, _ := db.FetchAllFBlocks() sort.Sort(util.ByFBlockIDAccending(FBlocks)) for _, block := range FBlocks { 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.GetDBHeight()), data, 0777) if err != nil { panic(err) } } }
// 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 { FactoshisPerCredit = fBlocks[i].GetExchRate() common.FactoidState.SetFactoshisPerEC(FactoshisPerCredit) // 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.NextDBHeight == 0 { common.FactoidState.SetFactoshisPerEC(FactoshisPerCredit) fchain.NextBlockHeight = 0 // func GetGenesisFBlock(ftime uint64, ExRate uint64, addressCnt int, Factoids uint64 ) IFBlock { //fchain.NextBlock = block.GetGenesisFBlock(0, FactoshisPerCredit, 10, 200000000000) fchain.NextBlock = block.GetGenesisFBlock() fmt.Println(fchain.NextBlock) gb := fchain.NextBlock // If a client, this block is going to get downloaded and added. Don't do it twice. if nodeMode == common.SERVER_NODE { err := common.FactoidState.AddTransactionBlock(gb) if err != nil { panic(err) } } } else { fchain.NextBlockHeight = dchain.NextDBHeight common.FactoidState.ProcessEndOfBlock2(dchain.NextDBHeight) fchain.NextBlock = common.FactoidState.GetCurrentBlock() } exportFctChain(fchain) }