Пример #1
0
func exportECBlock(block *common.ECBlock) {
	if block == nil || procLog.Level() < factomlog.Info {
		return
	}

	data, err := block.MarshalBinary()
	if err != nil {
		panic(err)
	}

	strChainID := block.Header.ECChainID.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)
	}

}
Пример #2
0
func (db *LevelDb) ProcessECBlockMultiBatch(block *common.ECBlock) error {
	if block == nil {
		return nil
	}

	if db.lbatch == nil {
		return fmt.Errorf("db.lbatch == nil")
	}

	binaryBlock, err := block.MarshalBinary()
	if err != nil {
		return err
	}

	// Insert the binary factom block
	var key = []byte{byte(TBL_CB)}
	hash, err := block.HeaderHash()
	if err != nil {
		return err
	}
	key = append(key, hash.Bytes()...)
	db.lbatch.Put(key, binaryBlock)

	// Insert block height cross reference
	var dbNumkey = []byte{byte(TBL_CB_NUM)}
	dbNumkey = append(dbNumkey, common.EC_CHAINID...)
	var buf bytes.Buffer
	binary.Write(&buf, binary.BigEndian, block.Header.EBHeight)
	dbNumkey = append(dbNumkey, buf.Bytes()...)
	db.lbatch.Put(dbNumkey, hash.Bytes())
	//fmt.Println("ProcessECBlockBatch: key=", hex.EncodeToString(dbNumkey), ", hash=", hash)

	// Update the chain head reference
	key = []byte{byte(TBL_CHAIN_HEAD)}
	key = append(key, common.EC_CHAINID...)
	//hash, err = block.HeaderHash()
	//if err != nil {
	//return err
	//}
	db.lbatch.Put(key, hash.Bytes())

	return nil
}
Пример #3
0
// ProcessECBlockBatche inserts the ECBlock and update all it's cbentries in DB
func (db *LevelDb) ProcessECBlockBatch(block *common.ECBlock) error {

	if block != nil {
		if db.lbatch == nil {
			db.lbatch = new(leveldb.Batch)
		}

		defer db.lbatch.Reset()

		binaryBlock, err := block.MarshalBinary()
		if err != nil {
			return err
		}

		// Insert the binary factom block
		var key []byte = []byte{byte(TBL_CB)}
		hash, err := block.HeaderHash()
		if err != nil {
			return err
		}
		key = append(key, hash.Bytes()...)
		db.lbatch.Put(key, binaryBlock)

		// Update the chain head reference
		key = []byte{byte(TBL_CHAIN_HEAD)}
		key = append(key, common.EC_CHAINID...)
		hash, err = block.HeaderHash()
		if err != nil {
			return err
		}
		db.lbatch.Put(key, hash.Bytes())

		err = db.lDb.Write(db.lbatch, db.wo)
		if err != nil {
			log.Println("batch failed %v\n", err)
			return err
		}

	}
	return nil
}