Ejemplo n.º 1
0
func (blockchain *blockchain) persistRawBlock(block *protos.Block, blockNumber uint64) error {
	blockBytes, blockBytesErr := block.Bytes()
	if blockBytesErr != nil {
		return blockBytesErr
	}
	writeBatch := gorocksdb.NewWriteBatch()
	defer writeBatch.Destroy()
	writeBatch.PutCF(db.GetDBHandle().BlockchainCF, encodeBlockNumberDBKey(blockNumber), blockBytes)

	blockHash, err := block.GetHash()
	if err != nil {
		return err
	}

	// Need to check as we suport out of order blocks in cases such as block/state synchronization. This is
	// really blockchain height, not size.
	if blockchain.getSize() < blockNumber+1 {
		sizeBytes := encodeUint64(blockNumber + 1)
		writeBatch.PutCF(db.GetDBHandle().BlockchainCF, blockCountKey, sizeBytes)
		blockchain.size = blockNumber + 1
		blockchain.previousBlockHash = blockHash
	}

	if blockchain.indexer.isSynchronous() {
		blockchain.indexer.createIndexesSync(block, blockNumber, blockHash, writeBatch)
	}

	opt := gorocksdb.NewDefaultWriteOptions()
	defer opt.Destroy()
	err = db.GetDBHandle().DB.Write(opt, writeBatch)
	if err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 2
0
func TestLedgerPutRawBlock(t *testing.T) {
	ledgerTestWrapper := createFreshDBAndTestLedgerWrapper(t)
	ledger := ledgerTestWrapper.ledger
	block := new(protos.Block)
	block.PreviousBlockHash = []byte("foo")
	block.StateHash = []byte("bar")
	ledger.PutRawBlock(block, 4)
	testutil.AssertEquals(t, ledgerTestWrapper.GetBlockByNumber(4), block)

	ledger.BeginTxBatch(1)
	ledger.TxBegin("txUuid")
	ledger.SetState("chaincode1", "key1", []byte("value1"))
	ledger.TxFinished("txUuid", true)
	transaction, _ := buildTestTx(t)
	ledger.CommitTxBatch(1, []*protos.Transaction{transaction}, nil, []byte("proof"))

	previousHash, _ := block.GetHash()
	newBlock := ledgerTestWrapper.GetBlockByNumber(5)

	if !bytes.Equal(newBlock.PreviousBlockHash, previousHash) {
		t.Fatalf("Expected new block to properly set its previous hash")
	}

	// Assert that a non-existent block is nil
	testutil.AssertNil(t, ledgerTestWrapper.GetBlockByNumber(2))
}
Ejemplo n.º 3
0
func (blockchain *blockchain) addPersistenceChangesForNewBlock(ctx context.Context,
	block *protos.Block, stateHash []byte, writeBatch *gorocksdb.WriteBatch) (uint64, error) {
	block = blockchain.buildBlock(block, stateHash)
	if block.NonHashData == nil {
		block.NonHashData = &protos.NonHashData{LocalLedgerCommitTimestamp: util.CreateUtcTimestamp()}
	} else {
		block.NonHashData.LocalLedgerCommitTimestamp = util.CreateUtcTimestamp()
	}
	blockNumber := blockchain.size
	blockHash, err := block.GetHash()
	if err != nil {
		return 0, err
	}
	blockBytes, blockBytesErr := block.Bytes()
	if blockBytesErr != nil {
		return 0, blockBytesErr
	}
	writeBatch.PutCF(db.GetDBHandle().BlockchainCF, encodeBlockNumberDBKey(blockNumber), blockBytes)
	writeBatch.PutCF(db.GetDBHandle().BlockchainCF, blockCountKey, encodeUint64(blockNumber+1))
	if blockchain.indexer.isSynchronous() {
		blockchain.indexer.createIndexesSync(block, blockNumber, blockHash, writeBatch)
	}
	blockchain.lastProcessedBlock = &lastProcessedBlock{block, blockNumber, blockHash}
	return blockNumber, nil
}
Ejemplo n.º 4
0
func (blockchain *blockchain) getBlockchainInfoForBlock(height uint64, block *protos.Block) *protos.BlockchainInfo {
	hash, _ := block.GetHash()
	info := &protos.BlockchainInfo{
		Height:            height,
		CurrentBlockHash:  hash,
		PreviousBlockHash: block.PreviousBlockHash}

	return info
}
Ejemplo n.º 5
0
		})
		It("creates, populates and finishes a transaction", func() {
			Expect(ledgerPtr.BeginTxBatch(1)).To(BeNil())
			ledgerPtr.TxBegin("txUuid")
			Expect(ledgerPtr.SetState("chaincode1", "key1", []byte("value1"))).To(BeNil())
			ledgerPtr.TxFinished("txUuid", true)
		})
		It("should commit the batch", func() {
			uuid := util.GenerateUUID()
			tx, err := protos.NewTransaction(protos.ChaincodeID{Path: "testUrl"}, uuid, "anyfunction", []string{"param1, param2"})
			Expect(err).To(BeNil())
			err = ledgerPtr.CommitTxBatch(1, []*protos.Transaction{tx}, nil, []byte("proof"))
			Expect(err).To(BeNil())
		})
		It("should have retrieved a block without error", func() {
			previousHash, _ := block.GetHash()
			newBlock, err := ledgerPtr.GetBlockByNumber(5)
			Expect(err).To(BeNil())
			Expect(newBlock.PreviousBlockHash).To(Equal(previousHash))
		})
	})

	Describe("Ledger SetRawState", func() {
		//var hash1, hash2, hash3 []byte
		//var snapshot *state.StateSnapshot
		var hash1, hash2, hash3 []byte
		var err error
		ledgerPtr := InitSpec()

		It("creates, populates and finishes a batch", func() {
			Expect(ledgerPtr.BeginTxBatch(1)).To(BeNil())
Ejemplo n.º 6
0
// HashBlock returns the hash of the included block, useful for mocking
func (p *PeerImpl) HashBlock(block *pb.Block) ([]byte, error) {
	return block.GetHash()
}
Ejemplo n.º 7
0
func (mock *MockLedger) HashBlock(block *protos.Block) ([]byte, error) {
	return block.GetHash()
}
Ejemplo n.º 8
0
// HashBlock returns the hash of the included block, useful for mocking
func (h *Helper) HashBlock(block *pb.Block) ([]byte, error) {
	return block.GetHash()
}