Beispiel #1
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
}
Beispiel #2
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
}