// FetchAllDBlocks gets all of the fbInfo func (db *LevelDb) FetchAllDBlocks() (dBlocks []common.DirectoryBlock, err error) { db.dbLock.Lock() defer db.dbLock.Unlock() var fromkey []byte = []byte{byte(TBL_DB)} // Table Name (1 bytes) // Timestamp (8 bytes) var tokey []byte = []byte{byte(TBL_DB + 1)} // Table Name (1 bytes) dBlockSlice := make([]common.DirectoryBlock, 0, 10) iter := db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro) for iter.Next() { var dBlock common.DirectoryBlock _, err := dBlock.UnmarshalBinaryData(iter.Value()) if err != nil { return nil, err } //TODO: to be optimized?? dBlock.DBHash = common.Sha(iter.Value()) dBlockSlice = append(dBlockSlice, dBlock) } iter.Release() err = iter.Error() return dBlockSlice, nil }
// ProcessDBlockBatche inserts the DBlock and update all it's dbentries in DB func (db *LevelDb) ProcessDBlockBatch(dblock *common.DirectoryBlock) error { if dblock != nil { if db.lbatch == nil { db.lbatch = new(leveldb.Batch) } defer db.lbatch.Reset() binaryDblock, err := dblock.MarshalBinary() if err != nil { return err } if dblock.DBHash == nil { dblock.DBHash = common.Sha(binaryDblock) } if dblock.KeyMR == nil { dblock.BuildKeyMerkleRoot() } // Insert the binary directory block var key []byte = []byte{byte(TBL_DB)} key = append(key, dblock.DBHash.Bytes()...) db.lbatch.Put(key, binaryDblock) // Insert block height cross reference var dbNumkey []byte = []byte{byte(TBL_DB_NUM)} var buf bytes.Buffer binary.Write(&buf, binary.BigEndian, dblock.Header.DBHeight) dbNumkey = append(dbNumkey, buf.Bytes()...) db.lbatch.Put(dbNumkey, dblock.DBHash.Bytes()) // Insert the directory block merkle root cross reference key = []byte{byte(TBL_DB_MR)} key = append(key, dblock.KeyMR.Bytes()...) binaryDBHash, _ := dblock.DBHash.MarshalBinary() db.lbatch.Put(key, binaryDBHash) // Update the chain head reference key = []byte{byte(TBL_CHAIN_HEAD)} key = append(key, common.D_CHAINID...) db.lbatch.Put(key, dblock.KeyMR.Bytes()) err = db.lDb.Write(db.lbatch, db.wo) if err != nil { return err } // Update DirBlock Height cache db.lastDirBlkHeight = int64(dblock.Header.DBHeight) db.lastDirBlkSha, _ = wire.NewShaHash(dblock.DBHash.Bytes()) db.lastDirBlkShaCached = true } return nil }
// handleABlockMsg is invoked when a peer receives a entry credit block message. func (p *peer) handleABlockMsg(msg *wire.MsgABlock, buf []byte) { binary, _ := msg.ABlk.MarshalBinary() commonHash := common.Sha(binary) hash, _ := wire.NewShaHash(commonHash.Bytes()) iv := wire.NewInvVect(wire.InvTypeFactomAdminBlock, hash) p.AddKnownInventory(iv) inMsgQueue <- msg }
// handleFactoidMsg func (p *peer) handleFactoidMsg(msg *wire.MsgFactoidTX, buf []byte) { binary, _ := msg.Transaction.MarshalBinary() commonHash := common.Sha(binary) hash, _ := wire.NewShaHash(commonHash.Bytes()) iv := wire.NewInvVect(wire.InvTypeTx, hash) p.AddKnownInventory(iv) inMsgQueue <- msg }
// pushGetEntryDataMsg takes the passed EBlock // and return all the corresponding EBEntries func (p *peer) pushGetEntryDataMsg(eblock *common.EBlock) { binary, _ := eblock.MarshalBinary() commonHash := common.Sha(binary) hash, _ := wire.NewShaHash(commonHash.Bytes()) iv := wire.NewInvVect(wire.InvTypeFactomEntry, hash) gdmsg := wire.NewMsgGetEntryData() gdmsg.AddInvVect(iv) if len(gdmsg.InvList) > 0 { p.QueueMessage(gdmsg, nil) } }
// handleEBlockMsg is invoked when a peer receives an entry block bitcoin message. func (p *peer) handleEBlockMsg(msg *wire.MsgEBlock, buf []byte) { binary, _ := msg.EBlk.MarshalBinary() commonHash := common.Sha(binary) hash, _ := wire.NewShaHash(commonHash.Bytes()) iv := wire.NewInvVect(wire.InvTypeFactomEntryBlock, hash) p.AddKnownInventory(iv) p.pushGetEntryDataMsg(msg.EBlk) inMsgQueue <- msg }
// handleDirBlockMsg is invoked when a peer receives a dir block message. func (p *peer) handleDirBlockMsg(msg *wire.MsgDirBlock, buf []byte) { binary, _ := msg.DBlk.MarshalBinary() commonHash := common.Sha(binary) hash, _ := wire.NewShaHash(commonHash.Bytes()) iv := wire.NewInvVect(wire.InvTypeFactomDirBlock, hash) p.AddKnownInventory(iv) p.pushGetNonDirDataMsg(msg.DBlk) inMsgQueue <- msg delete(p.requestedBlocks, *hash) delete(p.server.blockManager.requestedBlocks, *hash) }
// build blocks from all process lists func buildBlocks() error { // Allocate the first three dbentries for Admin block, ECBlock and Factoid block dchain.AddDBEntry(&common.DBEntry{}) // AdminBlock dchain.AddDBEntry(&common.DBEntry{}) // ECBlock dchain.AddDBEntry(&common.DBEntry{}) // factoid if plMgr != nil && plMgr.MyProcessList.IsValid() { buildFromProcessList(plMgr.MyProcessList) } // Entry Credit Chain ecBlock := newEntryCreditBlock(ecchain) dchain.AddECBlockToDBEntry(ecBlock) exportECBlock(ecBlock) // Admin chain aBlock := newAdminBlock(achain) dchain.AddABlockToDBEntry(aBlock) exportABlock(aBlock) // Factoid chain fBlock := newFactoidBlock(fchain) dchain.AddFBlockToDBEntry(fBlock) exportFctBlock(fBlock) // sort the echains by chain id var keys []string for k := range chainIDMap { keys = append(keys, k) } sort.Strings(keys) // Entry Chains for _, k := range keys { chain := chainIDMap[k] eblock := newEntryBlock(chain) if eblock != nil { dchain.AddEBlockToDBEntry(eblock) } exportEBlock(eblock) } // Directory Block chain procLog.Debug("in buildBlocks") dbBlock := newDirectoryBlock(dchain) // Generate the inventory vector and relay it. binary, _ := dbBlock.MarshalBinary() commonHash := common.Sha(binary) hash, _ := wire.NewShaHash(commonHash.Bytes()) outMsgQueue <- (&wire.MsgInt_DirBlock{hash}) // Update dir block height cache in db db.UpdateBlockHeightCache(dbBlock.Header.DBHeight, commonHash) db.UpdateNextBlockHeightCache(dchain.NextDBHeight) exportDBlock(dbBlock) // re-initialize the process lit manager initProcessListMgr() // Initialize timer for the new dblock if nodeMode == common.SERVER_NODE { timer := &BlockTimer{ nextDBlockHeight: dchain.NextDBHeight, inCtlMsgQueue: inCtlMsgQueue, } go timer.StartBlockTimer() } // place an anchor into btc placeAnchor(dbBlock) return nil }