// Seals the current open block, store it in db and create the next open block func newEntryCreditBlock(chain *common.ECChain) *common.ECBlock { // acquire the last block block := chain.NextBlock if chain.NextBlockHeight != dchain.NextDBHeight { panic("Entry Credit Block height does not match Directory Block height:" + string(dchain.NextDBHeight)) } block.BuildHeader() // Create the block and add a new block for new coming entries chain.BlockMutex.Lock() chain.NextBlockHeight++ var err error chain.NextBlock, err = common.NextECBlock(block) if err != nil { procLog.Debug("EntryCreditBlock Error: " + err.Error()) return nil } chain.NextBlock.AddEntry(serverIndex) chain.BlockMutex.Unlock() //Store the block in db db.ProcessECBlockBatch(block) procLog.Infof("EntryCreditBlock: block" + strconv.FormatUint(uint64(block.Header.DBHeight), 10) + " created for chain: " + chain.ChainID.String()) return block }
// 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.NextDBHeight == 0 { ecchain.NextBlockHeight = 0 ecchain.NextBlock = common.NewECBlock() ecchain.NextBlock.AddEntry(serverIndex) for i := 0; i < 10; i++ { marker := common.NewMinuteNumber() marker.Number = uint8(i + 1) ecchain.NextBlock.AddEntry(marker) } } else { // Entry Credit Chain should have the same height as the dir chain ecchain.NextBlockHeight = dchain.NextDBHeight var err error ecchain.NextBlock, err = common.NextECBlock(&ecBlocks[ecchain.NextBlockHeight-1]) if err != nil { panic(err) } } // create a backup copy before processing entries copyCreditMap(eCreditMap, eCreditMapBackup) exportECChain(ecchain) // ONly for debugging if procLog.Level() > factomlog.Info { printCreditMap() } }