// 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) }
// Seals the current open block, store it in db and create the next open block func newAdminBlock(chain *common.AdminChain) *common.AdminBlock { // acquire the last block block := chain.NextBlock if chain.NextBlockHeight != dchain.NextDBHeight { panic("Admin Block height does not match Directory Block height:" + string(dchain.NextDBHeight)) } block.Header.MessageCount = uint32(len(block.ABEntries)) block.Header.BodySize = uint32(block.MarshalledSize() - block.Header.MarshalledSize()) _, err := block.PartialHash() if err != nil { panic(err) } _, err = block.LedgerKeyMR() if err != nil { panic(err) } // Create the block and add a new block for new coming entries chain.BlockMutex.Lock() chain.NextBlockHeight++ chain.NextBlock, err = common.CreateAdminBlock(chain, block, 10) if err != nil { panic(err) } chain.BlockMutex.Unlock() //Store the block in db db.ProcessABlockBatch(block) procLog.Infof("Admin Block: block " + strconv.FormatUint(uint64(block.Header.DBHeight), 10) + " created for chain: " + chain.ChainID.String()) return block }