func exportAChain(chain *common.AdminChain) { if procLog.Level() < factomlog.Info { return } // get all aBlocks from db aBlocks, _ := db.FetchAllABlocks() sort.Sort(util.ByABlockIDAccending(aBlocks)) for _, block := range aBlocks { 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 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))) } if !validateDBSignature(&aBlocks[i], dchain) { panic(errors.New("No valid signature found in Admin Block = " + fmt.Sprintf("%s\n", spew.Sdump(aBlocks[i])))) } } //Create an empty block and append to the chain if len(aBlocks) == 0 || dchain.NextDBHeight == 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.NextDBHeight achain.NextBlock, _ = common.CreateAdminBlock(achain, &aBlocks[achain.NextBlockHeight-1], 10) } exportAChain(achain) }