Exemple #1
0
func TestStateImpl_DB_Changes(t *testing.T) {
	// number of buckets at each level 26,9,3,1
	testHasher, stateImplTestWrapper, stateDelta := createFreshDBAndInitTestStateImplWithCustomHasher(t, 26, 3)
	// populate hash fucntion such that
	// ["chaincodeID1", "key1"] is bucketized to bucket 1
	testHasher.populate("chaincodeID1", "key1", 0)
	testHasher.populate("chaincodeID1", "key2", 0)
	testHasher.populate("chaincodeID2", "key1", 1)
	testHasher.populate("chaincodeID2", "key3", 3)
	testHasher.populate("chaincodeID10", "key10", 24)

	// prepare stateDelta
	stateDelta.Set("chaincodeID1", "key1", []byte("value1"), nil)
	stateDelta.Set("chaincodeID1", "key2", []byte("value2"), nil)
	stateDelta.Set("chaincodeID2", "key1", []byte("value3"), nil)
	stateDelta.Set("chaincodeID2", "key3", []byte("value4"), nil)

	stateImplTestWrapper.prepareWorkingSetAndComputeCryptoHash(stateDelta)
	stateImplTestWrapper.persistChangesAndResetInMemoryChanges()

	// Read state from DB
	testutil.AssertEquals(t, stateImplTestWrapper.get("chaincodeID1", "key1"), []byte("value1"))
	testutil.AssertEquals(t, stateImplTestWrapper.get("chaincodeID2", "key1"), []byte("value3"))

	// fetch datanode from DB
	dataNodeFromDB, _ := fetchDataNodeFromDB(newDataKey("chaincodeID2", "key1"))
	testutil.AssertEquals(t, dataNodeFromDB, newDataNode(newDataKey("chaincodeID2", "key1"), []byte("value3")))

	//fetch non-existing data node from DB
	dataNodeFromDB, _ = fetchDataNodeFromDB(newDataKey("chaincodeID10", "key10"))
	t.Logf("isNIL...[%t]", dataNodeFromDB == nil)
	testutil.AssertNil(t, dataNodeFromDB)

	// fetch all data nodes from db that belong to bucket 1 at lowest level
	dataNodesFromDB, _ := fetchDataNodesFromDBFor(newBucketKeyAtLowestLevel(1))
	testutil.AssertContainsAll(t, dataNodesFromDB,
		dataNodes{newDataNode(newDataKey("chaincodeID1", "key1"), []byte("value1")),
			newDataNode(newDataKey("chaincodeID1", "key2"), []byte("value2"))})

	// fetch all data nodes from db that belong to bucket 2 at lowest level
	dataNodesFromDB, _ = fetchDataNodesFromDBFor(newBucketKeyAtLowestLevel(2))
	testutil.AssertContainsAll(t, dataNodesFromDB,
		dataNodes{newDataNode(newDataKey("chaincodeID2", "key1"), []byte("value3"))})

	// fetch first bucket at second level
	bucketNodeFromDB, _ := fetchBucketNodeFromDB(newBucketKey(2, 1))
	testutil.AssertEquals(t, bucketNodeFromDB.bucketKey, newBucketKey(2, 1))
	//check childrenCryptoHash entries in the bucket node from DB
	testutil.AssertEquals(t, bucketNodeFromDB.childrenCryptoHash[0],
		expectedBucketHashForTest([]string{"chaincodeID1", "key1", "value1", "key2", "value2"}))

	testutil.AssertEquals(t, bucketNodeFromDB.childrenCryptoHash[1],
		expectedBucketHashForTest([]string{"chaincodeID2", "key1", "value3"}))

	testutil.AssertNil(t, bucketNodeFromDB.childrenCryptoHash[2])

	// third bucket at second level should be nil
	bucketNodeFromDB, _ = fetchBucketNodeFromDB(newBucketKey(2, 3))
	testutil.AssertNil(t, bucketNodeFromDB)
}
func TestBucketTreeDeltaBasic(t *testing.T) {
	conf = initConfig(26, 3, fnvHash)
	bucketTreeDelta := newBucketTreeDelta()
	b1 := bucketTreeDelta.getOrCreateBucketNode(newBucketKey(2, 1))
	testutil.AssertSame(t, bucketTreeDelta.getOrCreateBucketNode(newBucketKey(2, 1)), b1)
	b2 := bucketTreeDelta.getOrCreateBucketNode(newBucketKey(2, 2))
	b3 := bucketTreeDelta.getOrCreateBucketNode(newBucketKey(2, 3))
	testutil.AssertContainsAll(t, bucketTreeDelta.getBucketNodesAt(2), []*bucketNode{b1, b2, b3})

	b4 := bucketTreeDelta.getOrCreateBucketNode(newBucketKey(1, 1))
	b5 := bucketTreeDelta.getOrCreateBucketNode(newBucketKey(1, 2))
	testutil.AssertContainsAll(t, bucketTreeDelta.getBucketNodesAt(1), []*bucketNode{b4, b5})

	b6 := bucketTreeDelta.getOrCreateBucketNode(newBucketKey(0, 1))
	testutil.AssertContainsAll(t, bucketTreeDelta.getBucketNodesAt(0), []*bucketNode{b6})
	testutil.AssertContainsAll(t, bucketTreeDelta.getBucketNodesAt(1), []*bucketNode{b4, b5})
	testutil.AssertContainsAll(t, bucketTreeDelta.getBucketNodesAt(2), []*bucketNode{b1, b2, b3})

	testutil.AssertEquals(t, len(bucketTreeDelta.getBucketNodesAt(0)), 1)
	testutil.AssertEquals(t, len(bucketTreeDelta.getBucketNodesAt(1)), 2)
	testutil.AssertEquals(t, len(bucketTreeDelta.getBucketNodesAt(2)), 3)

	testutil.AssertSame(t, bucketTreeDelta.getRootNode(), b6)
}