-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit - Removes the dependency on bccsp package from much deeper code and contains the dependency only at the level of top level - kvledger - Though the refactoring is straight forward, still this commit adds a test to make sure that the code that generates and validates the merkle tree for the range queries is backward compatible. This test was long due that can help catch any incompatible changes in the code or the hash function used Signed-off-by: manish <manish.sethi@gmail.com>
- Loading branch information
1 parent
839a4fa
commit 8559997
Showing
17 changed files
with
225 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package kvledger | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric-protos-go/common" | ||
"github.com/hyperledger/fabric/common/ledger/testutil" | ||
"github.com/hyperledger/fabric/common/util" | ||
lgr "github.com/hyperledger/fabric/core/ledger" | ||
"github.com/hyperledger/fabric/core/ledger/mock" | ||
"github.com/hyperledger/fabric/protoutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestBackwardCompatibilityRWSetV21 is added to protect against any changes in the hash function | ||
// that is used in preparing the rwset that includes a merkle tree for the range query | ||
func TestBackwardCompatibilityRWSetV21(t *testing.T) { | ||
rwsetBytes, err := ioutil.ReadFile("testdata/rwsetbytes_v21") | ||
require.NoError(t, err) | ||
b := testGenerateSampleRWSet(t) | ||
require.Equal(t, rwsetBytes, b) | ||
} | ||
|
||
// TestGenerateSampleRWSet generates the rwset that includes a merkle tree for a range-query read-set as well | ||
// The data present in the file testdata/rwsetbytes_v21 is generated by running the below test code on version 2.1 | ||
// To regenrate the data, (if needed in order to add some more data to the generated rwset), checkout release-2.1, | ||
// and uncomment and run the following test | ||
// func TestGenerateSampleRWSet(t *testing.T) { | ||
// b := testGenerateSampleRWSet(t) | ||
// require.NoError(t, ioutil.WriteFile("testdata/rwsetbytes_v21", b, 0644)) | ||
// } | ||
|
||
func testGenerateSampleRWSet(t *testing.T) []byte { | ||
conf, cleanup := testConfig(t) | ||
defer cleanup() | ||
provider := testutilNewProvider(conf, t, &mock.DeployedChaincodeInfoProvider{}) | ||
defer provider.Close() | ||
|
||
bg, gb := testutil.NewBlockGenerator(t, "testLedger", false) | ||
gbHash := protoutil.BlockHeaderHash(gb.Header) | ||
ledger, err := provider.Create(gb) | ||
require.NoError(t, err) | ||
defer ledger.Close() | ||
|
||
bcInfo, err := ledger.GetBlockchainInfo() | ||
require.NoError(t, err) | ||
require.Equal(t, &common.BlockchainInfo{ | ||
Height: 1, CurrentBlockHash: gbHash, PreviousBlockHash: nil, | ||
}, bcInfo) | ||
|
||
txid := util.GenerateUUID() | ||
|
||
// perform a range query for significant larger scan so that the merkle tree building kicks in | ||
// each level contains max 50 nodes per the current configuration | ||
simulator, err := ledger.NewTxSimulator(txid) | ||
for i := 0; i < 10011; i++ { | ||
simulator.SetState("ns1", fmt.Sprintf("key-%000d", i), []byte(fmt.Sprintf("value-%000d", i))) | ||
} | ||
simulator.Done() | ||
simRes, err := simulator.GetTxSimulationResults() | ||
require.NoError(t, err) | ||
pubSimBytes, err := simRes.GetPubSimulationBytes() | ||
require.NoError(t, err) | ||
block1 := bg.NextBlock([][]byte{pubSimBytes}) | ||
ledger.CommitLegacy(&lgr.BlockAndPvtData{Block: block1}, &lgr.CommitOptions{}) | ||
|
||
simulator, err = ledger.NewTxSimulator(txid) | ||
require.NoError(t, err) | ||
simulator.GetState("ns1", fmt.Sprintf("key-%000d", 5)) | ||
simulator.SetState("ns1", fmt.Sprintf("key-%000d", 6), []byte(fmt.Sprintf("value-%000d-new", 6))) | ||
itr, err := simulator.GetStateRangeScanIterator("ns1", "", "") | ||
require.NoError(t, err) | ||
numKVs := 0 | ||
for { | ||
kv, err := itr.Next() | ||
require.NoError(t, err) | ||
if kv == nil { | ||
break | ||
} | ||
numKVs++ | ||
} | ||
require.Equal(t, 10011, numKVs) | ||
simulator.Done() | ||
simRes, err = simulator.GetTxSimulationResults() | ||
require.NoError(t, err) | ||
pubSimBytes, err = simRes.GetPubSimulationBytes() | ||
require.NoError(t, err) | ||
return pubSimBytes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
� | ||
ns1� | ||
|
||
key-5�*�2 曮��-�^�c�6&�ǫ����7��E�� F������ќ;1CA���T�:k�j�U� ��ڼL� ؈�fw{����~��`�,H�� ����W?S��N�m�<�>��8J��y�\4�z� | ||
key-6value-6-new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.