예제 #1
0
func TestTransactionResult(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)

	transactionResult := &protos.TransactionResult{Uuid: uuid, ErrorCode: 500, Error: "bad"}

	ledger.CommitTxBatch(0, []*protos.Transaction{transaction}, []*protos.TransactionResult{transactionResult}, []byte("proof"))

	block := ledgerTestWrapper.GetBlockByNumber(0)

	nonHashData := block.GetNonHashData()
	if nonHashData == nil {
		t.Fatal("Expected block to have non hash data, but non hash data was nil.")
	}

	if nonHashData.TransactionResults == nil || len(nonHashData.TransactionResults) == 0 {
		t.Fatal("Expected block to have non hash data transaction results.")
	}

	testutil.AssertEquals(t, nonHashData.TransactionResults[0].Uuid, uuid)
	testutil.AssertEquals(t, nonHashData.TransactionResults[0].Error, "bad")
	testutil.AssertEquals(t, nonHashData.TransactionResults[0].ErrorCode, uint32(500))

}
예제 #2
0
func TestBlockChain_SimpleChain(t *testing.T) {
	testDBWrapper.CreateFreshDB(t)
	blockchainTestWrapper := newTestBlockchainWrapper(t)
	blockchain := blockchainTestWrapper.blockchain
	allBlocks, allStateHashes := blockchainTestWrapper.populateBlockChainWithSampleData()
	testutil.AssertEquals(t, blockchain.getSize(), uint64(len(allBlocks)))
	testutil.AssertEquals(t, blockchainTestWrapper.fetchBlockchainSizeFromDB(), uint64(len(allBlocks)))

	for i := range allStateHashes {
		t.Logf("Checking state hash for block number = [%d]", i)
		testutil.AssertEquals(t, blockchainTestWrapper.getBlock(uint64(i)).GetStateHash(), allStateHashes[i])
	}

	for i := range allBlocks {
		t.Logf("Checking block hash for block number = [%d]", i)
		blockhash, _ := blockchainTestWrapper.getBlock(uint64(i)).GetHash()
		expectedBlockHash, _ := allBlocks[i].GetHash()
		testutil.AssertEquals(t, blockhash, expectedBlockHash)
	}

	testutil.AssertNil(t, blockchainTestWrapper.getBlock(uint64(0)).PreviousBlockHash)

	i := 1
	for i < len(allBlocks) {
		t.Logf("Checking previous block hash for block number = [%d]", i)
		expectedPreviousBlockHash, _ := allBlocks[i-1].GetHash()
		testutil.AssertEquals(t, blockchainTestWrapper.getBlock(uint64(i)).PreviousBlockHash, expectedPreviousBlockHash)
		i++
	}
}
예제 #3
0
func testTrieNodeMarshalUnmarshal(trieNode *trieNode, t *testing.T) {
	trieNodeTestWrapper := &trieNodeTestWrapper{trieNode, t}
	serializedContent := trieNodeTestWrapper.marshal()
	trieNodeFromUnmarshal := trieNodeTestWrapper.unmarshal(trieNode.trieKey, serializedContent)
	testutil.AssertEquals(t, trieNodeFromUnmarshal.trieKey, trieNode.trieKey)
	testutil.AssertEquals(t, trieNodeFromUnmarshal.value, trieNode.value)
	testutil.AssertEquals(t, trieNodeFromUnmarshal.childrenCryptoHashes, trieNode.childrenCryptoHashes)
	testutil.AssertEquals(t, trieNodeFromUnmarshal.getNumChildren(), trieNode.getNumChildren())
}
예제 #4
0
func TestConfigInit(t *testing.T) {
	configs := viper.GetStringMap("ledger.state.dataStructure.configs")
	t.Logf("Configs loaded from yaml = %#v", configs)
	testDBWrapper.CreateFreshDB(t)
	stateImpl := NewStateImpl()
	stateImpl.Initialize(configs)
	testutil.AssertEquals(t, conf.getNumBucketsAtLowestLevel(), configs[ConfigNumBuckets])
	testutil.AssertEquals(t, conf.getMaxGroupingAtEachLevel(), configs[ConfigMaxGroupingAtEachLevel])
}
예제 #5
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)
}
예제 #6
0
func TestBucketKeyGetChildIndex(t *testing.T) {
	conf = newConfig(26, 3, fnvHash)
	bucketKey := newBucketKey(3, 22)
	testutil.AssertEquals(t, bucketKey.getParentKey().getChildIndex(bucketKey), 0)

	bucketKey = newBucketKey(3, 23)
	testutil.AssertEquals(t, bucketKey.getParentKey().getChildIndex(bucketKey), 1)

	bucketKey = newBucketKey(3, 24)
	testutil.AssertEquals(t, bucketKey.getParentKey().getChildIndex(bucketKey), 2)
}
예제 #7
0
func TestBucketKeyEqual(t *testing.T) {
	conf = newConfig(26, 3, fnvHash)
	bucketKey1 := newBucketKey(1, 2)
	bucketKey2 := newBucketKey(1, 2)
	testutil.AssertEquals(t, bucketKey1.equals(bucketKey2), true)
	bucketKey2 = newBucketKey(2, 2)
	testutil.AssertEquals(t, bucketKey1.equals(bucketKey2), false)
	bucketKey2 = newBucketKey(1, 3)
	testutil.AssertEquals(t, bucketKey1.equals(bucketKey2), false)
	bucketKey2 = newBucketKey(2, 3)
	testutil.AssertEquals(t, bucketKey1.equals(bucketKey2), false)
}
예제 #8
0
func TestLedgerCommit(t *testing.T) {
	ledgerTestWrapper := createFreshDBAndTestLedgerWrapper(t)
	ledger := ledgerTestWrapper.ledger
	ledger.BeginTxBatch(1)
	ledger.TxBegin("txUuid")
	ledger.SetState("chaincode1", "key1", []byte("value1"))
	ledger.SetState("chaincode2", "key2", []byte("value2"))
	ledger.SetState("chaincode3", "key3", []byte("value3"))
	ledger.TxFinished("txUuid", true)
	transaction, _ := buildTestTx(t)
	ledger.CommitTxBatch(1, []*protos.Transaction{transaction}, nil, []byte("proof"))
	testutil.AssertEquals(t, ledgerTestWrapper.GetState("chaincode1", "key1", false), []byte("value1"))
	testutil.AssertEquals(t, ledgerTestWrapper.GetState("chaincode1", "key1", true), []byte("value1"))
}
예제 #9
0
func TestBucketNodeMarshalUnmarshal(t *testing.T) {
	conf = newConfig(26, 3, fnvHash)
	bucketNode := newBucketNode(newBucketKey(2, 7))
	childKey1 := newBucketKey(3, 19)
	bucketNode.setChildCryptoHash(childKey1, []byte("cryptoHashChild1"))

	childKey3 := newBucketKey(3, 21)
	bucketNode.setChildCryptoHash(childKey3, []byte("cryptoHashChild3"))

	serializedBytes := bucketNode.marshal()
	deserializedBucketNode := unmarshalBucketNode(newBucketKey(2, 7), serializedBytes)
	testutil.AssertEquals(t, bucketNode.bucketKey, deserializedBucketNode.bucketKey)
	testutil.AssertEquals(t, bucketNode.childrenCryptoHash, deserializedBucketNode.childrenCryptoHash)
}
예제 #10
0
func TestStateDeltaCryptoHash(t *testing.T) {
	stateDelta := NewStateDelta()

	testutil.AssertNil(t, stateDelta.ComputeCryptoHash())

	stateDelta.Set("chaincodeID1", "key2", []byte("value2"), nil)
	stateDelta.Set("chaincodeID1", "key1", []byte("value1"), nil)
	stateDelta.Set("chaincodeID2", "key2", []byte("value2"), nil)
	stateDelta.Set("chaincodeID2", "key1", []byte("value1"), nil)
	testutil.AssertEquals(t, stateDelta.ComputeCryptoHash(), testutil.ComputeCryptoHash([]byte("chaincodeID1key1value1key2value2chaincodeID2key1value1key2value2")))

	stateDelta.Delete("chaincodeID2", "key1", nil)
	testutil.AssertEquals(t, stateDelta.ComputeCryptoHash(), testutil.ComputeCryptoHash([]byte("chaincodeID1key1value1key2value2chaincodeID2key1key2value2")))
}
예제 #11
0
func TestBucketNodeMerge(t *testing.T) {
	conf = newConfig(26, 3, fnvHash)
	bucketNode := newBucketNode(newBucketKey(2, 7))
	bucketNode.childrenCryptoHash[0] = []byte("cryptoHashChild1")
	bucketNode.childrenCryptoHash[2] = []byte("cryptoHashChild3")

	dbBucketNode := newBucketNode(newBucketKey(2, 7))
	dbBucketNode.childrenCryptoHash[0] = []byte("DBcryptoHashChild1")
	dbBucketNode.childrenCryptoHash[1] = []byte("DBcryptoHashChild2")

	bucketNode.mergeBucketNode(dbBucketNode)
	testutil.AssertEquals(t, bucketNode.childrenCryptoHash[0], []byte("cryptoHashChild1"))
	testutil.AssertEquals(t, bucketNode.childrenCryptoHash[1], []byte("DBcryptoHashChild2"))
	testutil.AssertEquals(t, bucketNode.childrenCryptoHash[2], []byte("cryptoHashChild3"))
}
예제 #12
0
func TestTrieNode_MergeAttributes(t *testing.T) {
	trieNode := newTrieNode(newTrieKey("chaincodeID", "key"), []byte("newValue!"), true)
	trieNode.setChildCryptoHash(0, []byte("crypto-hash-for-test-0"))
	trieNode.setChildCryptoHash(5, []byte("crypto-hash-for-test-5"))

	existingTrieNode := newTrieNode(newTrieKey("chaincodeID", "key"), []byte("existingValue"), false)
	existingTrieNode.setChildCryptoHash(5, []byte("crypto-hash-for-test-5-existing"))
	existingTrieNode.setChildCryptoHash(10, []byte("crypto-hash-for-test-10-existing"))

	trieNode.mergeMissingAttributesFrom(existingTrieNode)
	testutil.AssertEquals(t, trieNode.value, []byte("newValue!"))
	testutil.AssertEquals(t, trieNode.childrenCryptoHashes[0], []byte("crypto-hash-for-test-0"))
	testutil.AssertEquals(t, trieNode.childrenCryptoHashes[5], []byte("crypto-hash-for-test-5"))
	testutil.AssertEquals(t, trieNode.childrenCryptoHashes[10], []byte("crypto-hash-for-test-10-existing"))
}
예제 #13
0
// AssertIteratorContains - tests wether the iterator (itr) contains expected results (provided in map)
func AssertIteratorContains(t *testing.T, itr RangeScanIterator, expected map[string][]byte) {
	count := 0
	actual := make(map[string][]byte)
	for itr.Next() {
		count++
		k, v := itr.GetKeyValue()
		actual[k] = v
	}

	t.Logf("Results from iterator: %s", actual)
	testutil.AssertEquals(t, count, len(expected))
	for k, v := range expected {
		testutil.AssertEquals(t, actual[k], v)
	}
}
예제 #14
0
파일: pkg_test.go 프로젝트: butine/research
func TestExpectedBucketHashContentForTest(t *testing.T) {
	expectedHashContent1 := expectedBucketHashContentForTest(
		[]string{"chaincodeID1", "key1", "value1"},
		[]string{"chaincodeID_2", "key_1", "value_1", "key_2", "value_2"},
		[]string{"chaincodeID3", "key1", "value1", "key2", "value2", "key3", "value3"},
	)

	expectedHashContent2 := testutil.AppendAll(
		encodeNumberForTest(len("chaincodeID1")), []byte("chaincodeID1"),
		encodeNumberForTest(1),
		encodeNumberForTest(len("key1")), []byte("key1"), encodeNumberForTest(len("value1")), []byte("value1"),

		encodeNumberForTest(len("chaincodeID_2")), []byte("chaincodeID_2"),
		encodeNumberForTest(2),
		encodeNumberForTest(len("key_1")), []byte("key_1"), encodeNumberForTest(len("value_1")), []byte("value_1"),
		encodeNumberForTest(len("key_2")), []byte("key_2"), encodeNumberForTest(len("value_2")), []byte("value_2"),

		encodeNumberForTest(len("chaincodeID3")), []byte("chaincodeID3"),
		encodeNumberForTest(3),
		encodeNumberForTest(len("key1")), []byte("key1"), encodeNumberForTest(len("value1")), []byte("value1"),
		encodeNumberForTest(len("key2")), []byte("key2"), encodeNumberForTest(len("value2")), []byte("value2"),
		encodeNumberForTest(len("key3")), []byte("key3"), encodeNumberForTest(len("value3")), []byte("value3"),
	)
	testutil.AssertEquals(t, expectedHashContent1, expectedHashContent2)
}
func TestIndexesAsync_ClientWaitScenario(t *testing.T) {
	testDBWrapper.CreateFreshDB(t)
	testBlockchainWrapper := newTestBlockchainWrapper(t)
	chain := testBlockchainWrapper.blockchain
	if chain.indexer.isSynchronous() {
		t.Skip("Skipping because blockchain is configured to index block data synchronously")
	}
	blocks, _, err := testBlockchainWrapper.populateBlockChainWithSampleData()
	if err != nil {
		t.Logf("Error populating block chain with sample data: %s", err)
		t.Fail()
	}
	t.Log("Increasing size of blockchain by one artificially so as to make client wait")
	chain.size = chain.size + 1
	t.Log("Resetting size of blockchain to original and adding one block in a separate go routine so as to wake up the client")
	go func() {
		time.Sleep(2 * time.Second)
		chain.size = chain.size - 1
		blk, err := buildTestBlock(t)
		if err != nil {
			t.Logf("Error building test block: %s", err)
			t.Fail()
		}
		testBlockchainWrapper.addNewBlock(blk, []byte("stateHash"))
	}()
	t.Log("Executing client query. The client would wait and will be woken up")
	blockHash, _ := blocks[0].GetHash()
	block := testBlockchainWrapper.getBlockByHash(blockHash)
	testutil.AssertEquals(t, block, blocks[0])
}
예제 #16
0
func TestStateTrie_ComputeHash_AllInMemory_NoContents(t *testing.T) {
	testDBWrapper.CreateFreshDB(t)
	stateTrie := NewStateTrie()
	stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t}
	hash := stateTrieTestWrapper.PrepareWorkingSetAndComputeCryptoHash(statemgmt.NewStateDelta())
	testutil.AssertEquals(t, hash, nil)
}
예제 #17
0
func TestStateImpl_ComputeHash_AllInMemory_1(t *testing.T) {
	// number of buckets at each level 26,9,3,1
	testHasher, stateImplTestWrapper, stateDelta := createFreshDBAndInitTestStateImplWithCustomHasher(t, 26, 3)
	testHasher.populate("chaincodeID1", "key1", 0)
	testHasher.populate("chaincodeID2", "key2", 0)
	testHasher.populate("chaincodeID3", "key3", 0)
	testHasher.populate("chaincodeID4", "key4", 3)

	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)

	rootHash := stateImplTestWrapper.prepareWorkingSetAndComputeCryptoHash(stateDelta)

	expectedHashBucket3_1 := expectedBucketHashForTest(
		[]string{"chaincodeID1", "key1", "value1"},
		[]string{"chaincodeID2", "key2", "value2"},
		[]string{"chaincodeID3", "key3", "value3"},
	)
	expectedHashBucket3_4 := expectedBucketHashForTest(
		[]string{"chaincodeID4", "key4", "value4"},
	)
	expectedHash := testutil.ComputeCryptoHash(expectedHashBucket3_1, expectedHashBucket3_4)
	testutil.AssertEquals(t, rootHash, expectedHash)
}
예제 #18
0
func TestDataKey(t *testing.T) {
	conf = initConfig(26, 3, fnvHash)
	dataKey := newDataKey("chaincodeID", "key")
	encodedBytes := dataKey.getEncodedBytes()
	dataKeyFromEncodedBytes := newDataKeyFromEncodedBytes(encodedBytes)
	testutil.AssertEquals(t, dataKey, dataKeyFromEncodedBytes)
}
예제 #19
0
func TestTrieNode_ComputeCryptoHash_NoValue_SingleChild(t *testing.T) {
	trieNode := newTrieNode(newTrieKey("chaincodeID", "key"), nil, false)
	singleChildCryptoHash := []byte("childCryptoHash-0")
	trieNode.setChildCryptoHash(0, singleChildCryptoHash)
	hash := trieNode.computeCryptoHash()
	testutil.AssertEquals(t, hash, singleChildCryptoHash)
}
예제 #20
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)
}
예제 #21
0
func TestLedgerRollbackWithHash(t *testing.T) {
	ledgerTestWrapper := createFreshDBAndTestLedgerWrapper(t)
	ledger := ledgerTestWrapper.ledger

	ledger.BeginTxBatch(0)
	ledger.TxBegin("txUuid")
	ledger.SetState("chaincode0", "key1", []byte("value1"))
	ledger.SetState("chaincode0", "key2", []byte("value2"))
	ledger.SetState("chaincode0", "key3", []byte("value3"))
	ledger.TxFinished("txUuid", true)
	ledger.RollbackTxBatch(0)

	hash0 := ledgerTestWrapper.GetTempStateHash()

	ledger.BeginTxBatch(1)
	ledger.TxBegin("txUuid")
	ledger.SetState("chaincode1", "key1", []byte("value1"))
	ledger.SetState("chaincode2", "key2", []byte("value2"))
	ledger.SetState("chaincode3", "key3", []byte("value3"))
	ledger.TxFinished("txUuid", true)

	hash1 := ledgerTestWrapper.GetTempStateHash()
	testutil.AssertNotEquals(t, hash1, hash0)

	ledger.RollbackTxBatch(1)
	hash1 = ledgerTestWrapper.GetTempStateHash()
	testutil.AssertEquals(t, hash1, hash0)
	testutil.AssertNil(t, ledgerTestWrapper.GetState("chaincode1", "key1", false))
}
func TestIndexes_GetBlockByBlockNumber(t *testing.T) {
	testDBWrapper.CreateFreshDB(t)
	testBlockchainWrapper := newTestBlockchainWrapper(t)
	blocks, _ := testBlockchainWrapper.populateBlockChainWithSampleData()
	for i := range blocks {
		testutil.AssertEquals(t, testBlockchainWrapper.getBlock(uint64(i)), blocks[i])
	}
}
예제 #23
0
func TestBucketNodeComputeHash(t *testing.T) {
	conf = newConfig(26, 3, fnvHash)
	bucketNode := newBucketNode(newBucketKey(2, 7))
	testutil.AssertEquals(t, bucketNode.computeCryptoHash(), nil)

	childKey1 := newBucketKey(3, 19)
	bucketNode.setChildCryptoHash(childKey1, []byte("cryptoHashChild1"))
	testutil.AssertEquals(t, bucketNode.computeCryptoHash(), []byte("cryptoHashChild1"))

	childKey3 := newBucketKey(3, 21)
	bucketNode.setChildCryptoHash(childKey3, []byte("cryptoHashChild3"))
	testutil.AssertEquals(t, bucketNode.computeCryptoHash(), testutil.ComputeCryptoHash([]byte("cryptoHashChild1cryptoHashChild3")))

	childKey2 := newBucketKey(3, 20)
	bucketNode.setChildCryptoHash(childKey2, []byte("cryptoHashChild2"))
	testutil.AssertEquals(t, bucketNode.computeCryptoHash(), testutil.ComputeCryptoHash([]byte("cryptoHashChild1cryptoHashChild2cryptoHashChild3")))
}
예제 #24
0
func TestIndexes_GetTransactionByUUID(t *testing.T) {
	testDBWrapper.CreateFreshDB(t)
	testBlockchainWrapper := newTestBlockchainWrapper(t)
	tx1, uuid1 := buildTestTx(t)
	tx2, uuid2 := buildTestTx(t)
	block1 := protos.NewBlock([]*protos.Transaction{tx1, tx2}, nil)
	testBlockchainWrapper.addNewBlock(block1, []byte("stateHash1"))

	tx3, uuid3 := buildTestTx(t)
	tx4, uuid4 := buildTestTx(t)
	block2 := protos.NewBlock([]*protos.Transaction{tx3, tx4}, nil)
	testBlockchainWrapper.addNewBlock(block2, []byte("stateHash2"))

	testutil.AssertEquals(t, testBlockchainWrapper.getTransactionByUUID(uuid1), tx1)
	testutil.AssertEquals(t, testBlockchainWrapper.getTransactionByUUID(uuid2), tx2)
	testutil.AssertEquals(t, testBlockchainWrapper.getTransactionByUUID(uuid3), tx3)
	testutil.AssertEquals(t, testBlockchainWrapper.getTransactionByUUID(uuid4), tx4)
}
func TestIndexes_GetBlockByBlockHash(t *testing.T) {
	testDBWrapper.CreateFreshDB(t)
	testBlockchainWrapper := newTestBlockchainWrapper(t)
	blocks, _ := testBlockchainWrapper.populateBlockChainWithSampleData()
	for i := range blocks {
		blockHash, _ := blocks[i].GetHash()
		testutil.AssertEquals(t, testBlockchainWrapper.getBlockByHash(blockHash), blocks[i])
	}
}
예제 #26
0
func TestTrieNode_ComputeCryptoHash_WithValue_NoChild(t *testing.T) {
	trieKey := newTrieKey("chaincodeID", "key")
	value := []byte("testValue")

	trieNode := newTrieNode(trieKey, value, false)
	hash := trieNode.computeCryptoHash()
	expectedHash := expectedCryptoHashForTest(trieKey, value)
	testutil.AssertEquals(t, hash, expectedHash)
}
예제 #27
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)
}
func TestIndexes_GetTransactionByBlockNumberAndTxIndex(t *testing.T) {
	testDBWrapper.CreateFreshDB(t)
	testBlockchainWrapper := newTestBlockchainWrapper(t)
	blocks, _ := testBlockchainWrapper.populateBlockChainWithSampleData()
	for i, block := range blocks {
		for j, tx := range block.GetTransactions() {
			testutil.AssertEquals(t, testBlockchainWrapper.getTransaction(uint64(i), uint64(j)), tx)
		}
	}
}
예제 #29
0
func TestBlockChainEmptyChain(t *testing.T) {
	testDBWrapper.CreateFreshDB(t)
	blockchainTestWrapper := newTestBlockchainWrapper(t)
	testutil.AssertEquals(t, blockchainTestWrapper.blockchain.getSize(), uint64(0))
	block := blockchainTestWrapper.getLastBlock()
	if block != nil {
		t.Fatalf("Get last block on an empty chain should return nil.")
	}
	t.Logf("last block = [%s]", block)
}
예제 #30
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)
}