Exemplo n.º 1
0
func TestPreviewTXBatchBlock(t *testing.T) {
	ledgerTestWrapper := createFreshDBAndTestLedgerWrapper(t)
	ledger := ledgerTestWrapper.ledger

	// Block 0
	ledger.BeginTxBatch(0)
	ledger.TxBegin("txUuid1")
	ledger.SetState("chaincode1", "key1", []byte("value1A"))
	ledger.SetState("chaincode2", "key2", []byte("value2A"))
	ledger.SetState("chaincode3", "key3", []byte("value3A"))
	ledger.TxFinished("txUuid1", true)
	transaction, _ := buildTestTx(t)

	previewBlock, err := ledger.GetTXBatchPreviewBlock(0, []*protos.Transaction{transaction}, []byte("proof"))
	testutil.AssertNoError(t, err, "Error fetching preview block.")

	ledger.CommitTxBatch(0, []*protos.Transaction{transaction}, nil, []byte("proof"))
	commitedBlock := ledgerTestWrapper.GetBlockByNumber(0)

	previewBlockHash, err := previewBlock.GetHash()
	testutil.AssertNoError(t, err, "Error fetching preview block hash.")

	commitedBlockHash, err := commitedBlock.GetHash()
	testutil.AssertNoError(t, err, "Error fetching committed block hash.")

	testutil.AssertEquals(t, previewBlockHash, commitedBlockHash)
}
Exemplo n.º 2
0
func InitTestLedger(t *testing.T) *Ledger {
	testDBWrapper.CreateFreshDB(t)
	_, err := GetLedger()
	testutil.AssertNoError(t, err, "Error while constructing ledger")
	newLedger, err := newLedger()
	testutil.AssertNoError(t, err, "Error while constructing ledger")
	ledger = newLedger
	return newLedger
}
Exemplo n.º 3
0
func (stateTrieTestWrapper *stateTrieTestWrapper) PrepareWorkingSetAndComputeCryptoHash(stateDelta *statemgmt.StateDelta) []byte {
	stateTrieTestWrapper.stateTrie.PrepareWorkingSet(stateDelta)
	cryptoHash, err := stateTrieTestWrapper.stateTrie.ComputeCryptoHash()
	testutil.AssertNoError(stateTrieTestWrapper.t, err, "Error while computing crypto hash")
	stateTrieTestWrapper.t.Logf("Cryptohash = [%x]", cryptoHash)
	return cryptoHash
}
Exemplo n.º 4
0
func newStateImplTestWrapperWithCustomConfig(t testing.TB, numBuckets int, maxGroupingAtEachLevel int) *stateImplTestWrapper {
	configMap := map[string]interface{}{ConfigNumBuckets: numBuckets, ConfigMaxGroupingAtEachLevel: maxGroupingAtEachLevel}
	stateImpl := NewStateImpl()
	err := stateImpl.Initialize(configMap)
	testutil.AssertNoError(t, err, "Error while constrcuting stateImpl")
	return &stateImplTestWrapper{configMap, stateImpl, t}
}
Exemplo n.º 5
0
func newStateImplTestWrapper(t *testing.T) *stateImplTestWrapper {
	var configMap map[string]interface{}
	stateImpl := NewStateImpl()
	err := stateImpl.Initialize(configMap)
	testutil.AssertNoError(t, err, "Error while constrcuting stateImpl")
	return &stateImplTestWrapper{configMap, stateImpl, t}
}
Exemplo n.º 6
0
func TestStateSnapshotIterator(t *testing.T) {
	testDBWrapper.CreateFreshDB(t)
	stateTrieTestWrapper := newStateTrieTestWrapper(t)
	stateTrie := stateTrieTestWrapper.stateTrie
	stateDelta := statemgmt.NewStateDelta()

	// insert keys
	stateDelta.Set("chaincodeID1", "key1", []byte("value1"), nil)
	stateDelta.Set("chaincodeID2", "key2", []byte("value2"), nil)
	stateDelta.Set("chaincodeID3", "key3", []byte("value3"), nil)
	stateDelta.Set("chaincodeID4", "key4", []byte("value4"), nil)
	stateDelta.Set("chaincodeID5", "key5", []byte("value5"), nil)
	stateDelta.Set("chaincodeID6", "key6", []byte("value6"), nil)
	stateTrie.PrepareWorkingSet(stateDelta)
	stateTrieTestWrapper.PersistChangesAndResetInMemoryChanges()
	//check that the key is persisted
	testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID1", "key1"), []byte("value1"))
	testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID2", "key2"), []byte("value2"))
	testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID3", "key3"), []byte("value3"))
	testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID4", "key4"), []byte("value4"))
	testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID5", "key5"), []byte("value5"))
	testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID6", "key6"), []byte("value6"))

	// take db snapeshot
	dbSnapshot := db.GetDBHandle().GetSnapshot()

	stateDelta1 := statemgmt.NewStateDelta()
	// delete a few keys
	stateDelta1.Delete("chaincodeID1", "key1", nil)
	stateDelta1.Delete("chaincodeID3", "key3", nil)
	stateDelta1.Delete("chaincodeID4", "key4", nil)
	stateDelta1.Delete("chaincodeID6", "key6", nil)

	// update remaining keys
	stateDelta1.Set("chaincodeID2", "key2", []byte("value2_new"), nil)
	stateDelta1.Set("chaincodeID5", "key5", []byte("value5_new"), nil)

	stateTrie.PrepareWorkingSet(stateDelta1)
	stateTrieTestWrapper.PersistChangesAndResetInMemoryChanges()
	//check that the keys are updated
	testutil.AssertNil(t, stateTrieTestWrapper.Get("chaincodeID1", "key1"))
	testutil.AssertNil(t, stateTrieTestWrapper.Get("chaincodeID3", "key3"))
	testutil.AssertNil(t, stateTrieTestWrapper.Get("chaincodeID4", "key4"))
	testutil.AssertNil(t, stateTrieTestWrapper.Get("chaincodeID6", "key6"))
	testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID2", "key2"), []byte("value2_new"))
	testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID5", "key5"), []byte("value5_new"))

	itr, err := newStateSnapshotIterator(dbSnapshot)
	testutil.AssertNoError(t, err, "Error while getting state snapeshot iterator")

	stateDeltaFromSnapshot := statemgmt.NewStateDelta()
	for itr.Next() {
		keyBytes, valueBytes := itr.GetRawKeyValue()
		t.Logf("key=[%s], value=[%s]", string(keyBytes), string(valueBytes))
		chaincodeID, key := statemgmt.DecodeCompositeKey(keyBytes)
		stateDeltaFromSnapshot.Set(chaincodeID, key, valueBytes, nil)
	}
	testutil.AssertEquals(t, stateDelta, stateDeltaFromSnapshot)
}
Exemplo n.º 7
0
func (testWrapper *blockchainTestWrapper) addNewBlock(block *protos.Block, stateHash []byte) uint64 {
	writeBatch := gorocksdb.NewWriteBatch()
	newBlockNumber, err := testWrapper.blockchain.addPersistenceChangesForNewBlock(context.TODO(), block, stateHash, writeBatch)
	testutil.AssertNoError(testWrapper.t, err, "Error while adding a new block")
	testDBWrapper.WriteToDB(testWrapper.t, writeBatch)
	testWrapper.blockchain.blockPersistenceStatus(true)
	return newBlockNumber
}
Exemplo n.º 8
0
func TestStateSnapshotIterator(t *testing.T) {
	testDBWrapper.CreateFreshDB(t)
	stateImplTestWrapper := newStateImplTestWrapper(t)
	stateDelta := statemgmt.NewStateDelta()

	// insert keys
	stateDelta.Set("chaincodeID1", "key1", []byte("value1"), nil)
	stateDelta.Set("chaincodeID2", "key2", []byte("value2"), nil)
	stateDelta.Set("chaincodeID3", "key3", []byte("value3"), nil)
	stateDelta.Set("chaincodeID4", "key4", []byte("value4"), nil)
	stateDelta.Set("chaincodeID5", "key5", []byte("value5"), nil)
	stateDelta.Set("chaincodeID6", "key6", []byte("value6"), nil)
	stateImplTestWrapper.prepareWorkingSet(stateDelta)
	stateImplTestWrapper.persistChangesAndResetInMemoryChanges()
	//check that the key is persisted
	testutil.AssertEquals(t, stateImplTestWrapper.get("chaincodeID5", "key5"), []byte("value5"))

	// take db snapeshot
	dbSnapshot := db.GetDBHandle().GetSnapshot()

	// delete keys
	stateDelta.Delete("chaincodeID1", "key1", nil)
	stateDelta.Delete("chaincodeID2", "key2", nil)
	stateDelta.Delete("chaincodeID3", "key3", nil)
	stateDelta.Delete("chaincodeID4", "key4", nil)
	stateDelta.Delete("chaincodeID5", "key5", nil)
	stateDelta.Delete("chaincodeID6", "key6", nil)
	stateImplTestWrapper.prepareWorkingSet(stateDelta)
	stateImplTestWrapper.persistChangesAndResetInMemoryChanges()
	//check that the key is deleted
	testutil.AssertNil(t, stateImplTestWrapper.get("chaincodeID5", "key5"))

	itr, err := newStateSnapshotIterator(dbSnapshot)
	testutil.AssertNoError(t, err, "Error while getting state snapeshot iterator")
	numKeys := 0
	for itr.Next() {
		key, value := itr.GetRawKeyValue()
		t.Logf("key=[%s], value=[%s]", string(key), string(value))
		numKeys++
	}
	testutil.AssertEquals(t, numKeys, 6)
}
Exemplo n.º 9
0
func TestGetTransactionByUUID(t *testing.T) {
	ledgerTestWrapper := createFreshDBAndTestLedgerWrapper(t)
	ledger := ledgerTestWrapper.ledger

	// Block 0
	ledger.BeginTxBatch(0)
	ledger.TxBegin("txUuid1")
	ledger.SetState("chaincode1", "key1", []byte("value1A"))
	ledger.SetState("chaincode2", "key2", []byte("value2A"))
	ledger.SetState("chaincode3", "key3", []byte("value3A"))
	ledger.TxFinished("txUuid1", true)
	transaction, uuid := buildTestTx(t)
	ledger.CommitTxBatch(0, []*protos.Transaction{transaction}, nil, []byte("proof"))

	ledgerTransaction, err := ledger.GetTransactionByUUID(uuid)
	testutil.AssertNoError(t, err, "Error fetching transaction by UUID.")
	testutil.AssertEquals(t, transaction, ledgerTransaction)

	ledgerTransaction, err = ledger.GetTransactionByUUID("InvalidUUID")
	testutil.AssertEquals(t, err, ErrResourceNotFound)
	testutil.AssertNil(t, ledgerTransaction)
}
Exemplo n.º 10
0
func TestBlockChain_SingleBlock(t *testing.T) {
	testDBWrapper.CreateFreshDB(t)
	blockchainTestWrapper := newTestBlockchainWrapper(t)
	blockchain := blockchainTestWrapper.blockchain

	// Create the Chaincode specification
	chaincodeSpec := &protos.ChaincodeSpec{Type: protos.ChaincodeSpec_GOLANG,
		ChaincodeID: &protos.ChaincodeID{Path: "Contracts"},
		CtorMsg:     &protos.ChaincodeInput{Function: "Initialize", Args: []string{"param1"}}}
	chaincodeDeploymentSepc := &protos.ChaincodeDeploymentSpec{ChaincodeSpec: chaincodeSpec}
	uuid := testutil.GenerateUUID(t)
	newChaincodeTx, err := protos.NewChaincodeDeployTransaction(chaincodeDeploymentSepc, uuid)
	testutil.AssertNoError(t, err, "Failed to create new chaincode Deployment Transaction")
	t.Logf("New chaincode tx: %v", newChaincodeTx)

	block1 := protos.NewBlock([]*protos.Transaction{newChaincodeTx})
	blockNumber := blockchainTestWrapper.addNewBlock(block1, []byte("stateHash1"))
	t.Logf("New chain: %v", blockchain)
	testutil.AssertEquals(t, blockNumber, uint64(0))
	testutil.AssertEquals(t, blockchain.getSize(), uint64(1))
	testutil.AssertEquals(t, blockchainTestWrapper.fetchBlockchainSizeFromDB(), uint64(1))
}
Exemplo n.º 11
0
func (ledgerTestWrapper *ledgerTestWrapper) CommitStateDelta(id interface{}) {
	err := ledgerTestWrapper.ledger.CommitStateDelta(id)
	testutil.AssertNoError(ledgerTestWrapper.t, err, "error committing state delta")
}
Exemplo n.º 12
0
func (ledgerTestWrapper *ledgerTestWrapper) ApplyStateDelta(id interface{}, delta *statemgmt.StateDelta) {
	err := ledgerTestWrapper.ledger.ApplyStateDelta(id, delta)
	testutil.AssertNoError(ledgerTestWrapper.t, err, "error applying state delta")
}
Exemplo n.º 13
0
func (ledgerTestWrapper *ledgerTestWrapper) GetTempStateHash() []byte {
	hash, err := ledgerTestWrapper.ledger.GetTempStateHash()
	testutil.AssertNoError(ledgerTestWrapper.t, err, "error while getting state hash from ledger")
	return hash
}
Exemplo n.º 14
0
func (ledgerTestWrapper *ledgerTestWrapper) GetStateDelta(blockNumber uint64) *statemgmt.StateDelta {
	delta, err := ledgerTestWrapper.ledger.GetStateDelta(blockNumber)
	testutil.AssertNoError(ledgerTestWrapper.t, err, "error while getting state delta from ledger")
	return delta
}
Exemplo n.º 15
0
func (ledgerTestWrapper *ledgerTestWrapper) PutRawBlock(block *protos.Block, blockNumber uint64) {
	err := ledgerTestWrapper.ledger.PutRawBlock(block, blockNumber)
	testutil.AssertNoError(ledgerTestWrapper.t, err, "error while verifying chain")
}
Exemplo n.º 16
0
func (ledgerTestWrapper *ledgerTestWrapper) VerifyChain(highBlock, lowBlock uint64) uint64 {
	result, err := ledgerTestWrapper.ledger.VerifyChain(highBlock, lowBlock)
	testutil.AssertNoError(ledgerTestWrapper.t, err, "error while verifying chain")
	return result
}
Exemplo n.º 17
0
func (testWrapper *stateImplTestWrapper) getRangeScanIterator(chaincodeID string, startKey string, endKey string) statemgmt.RangeScanIterator {
	itr, err := testWrapper.stateImpl.GetRangeScanIterator(chaincodeID, startKey, endKey)
	testutil.AssertNoError(testWrapper.t, err, "Error while getting iterator")
	return itr
}
Exemplo n.º 18
0
func (testWrapper *blockchainTestWrapper) getTransactionByBlockHash(blockHash []byte, txIndex uint64) *protos.Transaction {
	tx, err := testWrapper.blockchain.getTransactionByBlockHash(blockHash, txIndex)
	testutil.AssertNoError(testWrapper.t, err, "Error while getting tx from blockchain")
	return tx
}
Exemplo n.º 19
0
func (testWrapper *blockchainTestWrapper) getLastBlock() *protos.Block {
	block, err := testWrapper.blockchain.getLastBlock()
	testutil.AssertNoError(testWrapper.t, err, "Error while getting block from blockchain")
	return block
}
Exemplo n.º 20
0
func (testWrapper *stateImplTestWrapper) constructNewStateImpl() {
	stateImpl := NewStateImpl()
	err := stateImpl.Initialize(testWrapper.configMap)
	testutil.AssertNoError(testWrapper.t, err, "Error while constructing new state tree")
	testWrapper.stateImpl = stateImpl
}
Exemplo n.º 21
0
func (testWrapper *stateImplTestWrapper) computeCryptoHash() []byte {
	cryptoHash, err := testWrapper.stateImpl.ComputeCryptoHash()
	testutil.AssertNoError(testWrapper.t, err, "Error while computing crypto hash")
	return cryptoHash
}
Exemplo n.º 22
0
func (ledgerTestWrapper *ledgerTestWrapper) RollbackStateDelta(id interface{}) {
	err := ledgerTestWrapper.ledger.RollbackStateDelta(id)
	testutil.AssertNoError(ledgerTestWrapper.t, err, "error rolling back state delta")
}
Exemplo n.º 23
0
func newTestBlockchainWrapper(t *testing.T) *blockchainTestWrapper {
	blockchain, err := newBlockchain()
	testutil.AssertNoError(t, err, "Error while getting handle to chain")
	return &blockchainTestWrapper{t, blockchain}
}
Exemplo n.º 24
0
func (testWrapper *stateImplTestWrapper) addChangesForPersistence(writeBatch *gorocksdb.WriteBatch) {
	err := testWrapper.stateImpl.AddChangesForPersistence(writeBatch)
	testutil.AssertNoError(testWrapper.t, err, "Error while adding changes to db write-batch")
}
Exemplo n.º 25
0
func (testWrapper *blockchainTestWrapper) fetchBlockchainSizeFromDB() uint64 {
	size, err := fetchBlockchainSizeFromDB()
	testutil.AssertNoError(testWrapper.t, err, "Error while fetching blockchain size from db")
	return size
}
Exemplo n.º 26
0
func createFreshDBAndTestLedgerWrapper(t *testing.T) *ledgerTestWrapper {
	testDBWrapper.CreateFreshDB(t)
	ledger, err := newLedger()
	testutil.AssertNoError(t, err, "Error while constructing ledger")
	return &ledgerTestWrapper{ledger, t}
}
Exemplo n.º 27
0
func (testWrapper *blockchainTestWrapper) getBlockByHash(blockHash []byte) *protos.Block {
	block, err := testWrapper.blockchain.getBlockByHash(blockHash)
	testutil.AssertNoError(testWrapper.t, err, "Error while getting block by blockhash from blockchain")
	return block
}
Exemplo n.º 28
0
func (ledgerTestWrapper *ledgerTestWrapper) GetState(chaincodeID string, key string, committed bool) []byte {
	value, err := ledgerTestWrapper.ledger.GetState(chaincodeID, key, committed)
	testutil.AssertNoError(ledgerTestWrapper.t, err, "error while getting state from ledger")
	return value
}
Exemplo n.º 29
0
func (testWrapper *blockchainTestWrapper) getTransactionByUUID(txUUID string) *protos.Transaction {
	tx, err := testWrapper.blockchain.getTransactionByUUID(txUUID)
	testutil.AssertNoError(testWrapper.t, err, "Error while getting tx from blockchain")
	return tx
}
Exemplo n.º 30
0
func (ledgerTestWrapper *ledgerTestWrapper) GetBlockByNumber(blockNumber uint64) *protos.Block {
	block, err := ledgerTestWrapper.ledger.GetBlockByNumber(blockNumber)
	testutil.AssertNoError(ledgerTestWrapper.t, err, "error while getting block from ledger")
	return block
}