예제 #1
0
func TestRangeScanIterator(t *testing.T) {
	ledgerTestWrapper := createFreshDBAndTestLedgerWrapper(t)
	ledger := ledgerTestWrapper.ledger

	///////// Test with an empty Ledger //////////
	//////////////////////////////////////////////
	itr, _ := ledger.GetStateRangeScanIterator("chaincodeID2", "key2", "key5", false)
	statemgmt.AssertIteratorContains(t, itr, map[string][]byte{})
	itr.Close()

	itr, _ = ledger.GetStateRangeScanIterator("chaincodeID2", "key2", "key5", true)
	statemgmt.AssertIteratorContains(t, itr, map[string][]byte{})
	itr.Close()

	// Commit initial data to ledger
	ledger.BeginTxBatch(0)
	ledger.TxBegin("txUuid1")
	ledger.SetState("chaincodeID1", "key1", []byte("value1"))

	ledger.SetState("chaincodeID2", "key1", []byte("value1"))
	ledger.SetState("chaincodeID2", "key2", []byte("value2"))
	ledger.SetState("chaincodeID2", "key3", []byte("value3"))

	ledger.SetState("chaincodeID3", "key1", []byte("value1"))

	ledger.SetState("chaincodeID4", "key1", []byte("value1"))
	ledger.SetState("chaincodeID4", "key2", []byte("value2"))
	ledger.SetState("chaincodeID4", "key3", []byte("value3"))
	ledger.SetState("chaincodeID4", "key4", []byte("value4"))
	ledger.SetState("chaincodeID4", "key5", []byte("value5"))
	ledger.SetState("chaincodeID4", "key6", []byte("value6"))
	ledger.SetState("chaincodeID4", "key7", []byte("value7"))

	ledger.SetState("chaincodeID5", "key1", []byte("value5"))
	ledger.SetState("chaincodeID6", "key1", []byte("value6"))

	ledger.TxFinished("txUuid1", true)
	transaction, _ := buildTestTx(t)
	ledger.CommitTxBatch(0, []*protos.Transaction{transaction}, nil, []byte("proof"))

	// Add new keys and modify existing keys in on-going tx-batch
	ledger.BeginTxBatch(1)
	ledger.TxBegin("txUuid1")
	ledger.SetState("chaincodeID4", "key2", []byte("value2_new"))
	ledger.DeleteState("chaincodeID4", "key3")
	ledger.SetState("chaincodeID4", "key8", []byte("value8_new"))

	///////////////////// Test with committed=true ///////////
	//////////////////////////////////////////////////////////
	// test range scan for chaincodeID4
	itr, _ = ledger.GetStateRangeScanIterator("chaincodeID4", "key2", "key5", true)
	statemgmt.AssertIteratorContains(t, itr,
		map[string][]byte{
			"key2": []byte("value2"),
			"key3": []byte("value3"),
			"key4": []byte("value4"),
			"key5": []byte("value5"),
		})
	itr.Close()

	// test with empty start-key
	itr, _ = ledger.GetStateRangeScanIterator("chaincodeID4", "", "key5", true)
	statemgmt.AssertIteratorContains(t, itr,
		map[string][]byte{
			"key1": []byte("value1"),
			"key2": []byte("value2"),
			"key3": []byte("value3"),
			"key4": []byte("value4"),
			"key5": []byte("value5"),
		})
	itr.Close()

	// test with empty end-key
	itr, _ = ledger.GetStateRangeScanIterator("chaincodeID4", "", "", true)
	statemgmt.AssertIteratorContains(t, itr,
		map[string][]byte{
			"key1": []byte("value1"),
			"key2": []byte("value2"),
			"key3": []byte("value3"),
			"key4": []byte("value4"),
			"key5": []byte("value5"),
			"key6": []byte("value6"),
			"key7": []byte("value7"),
		})
	itr.Close()

	///////////////////// Test with committed=false ///////////
	//////////////////////////////////////////////////////////
	// test range scan for chaincodeID4
	itr, _ = ledger.GetStateRangeScanIterator("chaincodeID4", "key2", "key5", false)
	statemgmt.AssertIteratorContains(t, itr,
		map[string][]byte{
			"key2": []byte("value2_new"),
			"key4": []byte("value4"),
			"key5": []byte("value5"),
		})
	itr.Close()

	// test with empty start-key
	itr, _ = ledger.GetStateRangeScanIterator("chaincodeID4", "", "key5", false)
	statemgmt.AssertIteratorContains(t, itr,
		map[string][]byte{
			"key1": []byte("value1"),
			"key2": []byte("value2_new"),
			"key4": []byte("value4"),
			"key5": []byte("value5"),
		})
	itr.Close()

	// test with empty end-key
	itr, _ = ledger.GetStateRangeScanIterator("chaincodeID4", "", "", false)
	statemgmt.AssertIteratorContains(t, itr,
		map[string][]byte{
			"key1": []byte("value1"),
			"key2": []byte("value2_new"),
			"key4": []byte("value4"),
			"key5": []byte("value5"),
			"key6": []byte("value6"),
			"key7": []byte("value7"),
			"key8": []byte("value8_new"),
		})
	itr.Close()
}
func TestCompositeRangeScanIterator(t *testing.T) {
	stateTestWrapper, state := createFreshDBAndConstructState(t)

	// commit initial test state to db
	state.TxBegin("txUuid")
	state.Set("chaincode1", "key1", []byte("value1"))
	state.Set("chaincode1", "key2", []byte("value2"))
	state.Set("chaincode1", "key3", []byte("value3"))
	state.Set("chaincode1", "key4", []byte("value4"))
	state.Set("chaincode1", "key5", []byte("value5"))
	state.Set("chaincode1", "key6", []byte("value6"))
	state.Set("chaincode1", "key7", []byte("value7"))

	state.Set("chaincode2", "key1", []byte("value1"))
	state.Set("chaincode2", "key2", []byte("value2"))
	state.Set("chaincode2", "key3", []byte("value3"))
	state.TxFinish("txUuid", true)
	stateTestWrapper.persistAndClearInMemoryChanges(0)

	// change and delete a few existing keys and add a new key
	state.TxBegin("txUUID")
	state.Set("chaincode1", "key3", []byte("value3_new"))
	state.Set("chaincode1", "key4", []byte("value4_new"))
	state.Set("chaincode1", "key5", []byte("value5_new"))
	state.Delete("chaincode1", "key6")
	state.Set("chaincode1", "key8", []byte("value8_new"))
	state.TxFinish("txUUID", true)

	// change and delete a few existing keys and add a new key, in the on-going tx
	state.TxBegin("txUUID")
	state.Set("chaincode1", "key3", []byte("value3_new_new"))
	state.Delete("chaincode1", "key4")
	state.Set("chaincode1", "key9", []byte("value9_new_new"))

	// Test with commited=false //////////////////////////
	/////////////////////////////////////////////////////
	// test keys between key2 and key8
	itr, _ := state.GetRangeScanIterator("chaincode1", "key2", "key8", false)
	statemgmt.AssertIteratorContains(t, itr,
		map[string][]byte{
			// from current tx
			"key3": []byte("value3_new_new"),

			// from current batch
			"key5": []byte("value5_new"),
			"key8": []byte("value8_new"),

			// from commited results
			"key2": []byte("value2"),
			"key7": []byte("value7"),
		})
	itr.Close()

	// test with an empty startKey
	itr, _ = state.GetRangeScanIterator("chaincode1", "", "key8", false)
	statemgmt.AssertIteratorContains(t, itr,
		map[string][]byte{
			// from current tx
			"key3": []byte("value3_new_new"),

			// from current batch
			"key5": []byte("value5_new"),
			"key8": []byte("value8_new"),

			// from commited results
			"key1": []byte("value1"),
			"key2": []byte("value2"),
			"key7": []byte("value7"),
		})
	itr.Close()

	// test with an empty endKey
	itr, _ = state.GetRangeScanIterator("chaincode1", "", "", false)
	statemgmt.AssertIteratorContains(t, itr,
		map[string][]byte{
			// from current tx
			"key3": []byte("value3_new_new"),
			"key9": []byte("value9_new_new"),

			// from current batch
			"key5": []byte("value5_new"),
			"key8": []byte("value8_new"),

			// from commited results
			"key1": []byte("value1"),
			"key2": []byte("value2"),
			"key7": []byte("value7"),
		})
	itr.Close()

	// Test with commited=true //////////////////////////
	/////////////////////////////////////////////////////
	// test keys between key2 and key8
	itr, _ = state.GetRangeScanIterator("chaincode1", "key2", "key8", true)
	statemgmt.AssertIteratorContains(t, itr,
		map[string][]byte{
			// from commited results
			"key2": []byte("value2"),
			"key3": []byte("value3"),
			"key4": []byte("value4"),
			"key5": []byte("value5"),
			"key6": []byte("value6"),
			"key7": []byte("value7"),
		})
	itr.Close()

	// test with an empty startKey
	itr, _ = state.GetRangeScanIterator("chaincode1", "", "key8", true)
	statemgmt.AssertIteratorContains(t, itr,
		map[string][]byte{
			// from commited results
			"key1": []byte("value1"),
			"key2": []byte("value2"),
			"key3": []byte("value3"),
			"key4": []byte("value4"),
			"key5": []byte("value5"),
			"key6": []byte("value6"),
			"key7": []byte("value7"),
		})
	itr.Close()

	// test with an empty endKey
	itr, _ = state.GetRangeScanIterator("chaincode1", "", "", true)
	statemgmt.AssertIteratorContains(t, itr,
		map[string][]byte{
			// from commited results
			"key1": []byte("value1"),
			"key2": []byte("value2"),
			"key3": []byte("value3"),
			"key4": []byte("value4"),
			"key5": []byte("value5"),
			"key6": []byte("value6"),
			"key7": []byte("value7"),
		})
	itr.Close()
}