-
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.
Fix the issue of Nil/Zero-length-byte-array value (#2310)
Signed-off-by: manish <[email protected]>
- Loading branch information
1 parent
e5ecdef
commit 1de0825
Showing
14 changed files
with
466 additions
and
7 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
148 changes: 148 additions & 0 deletions
148
core/ledger/kvledger/tests/nilvalue_no_delete_marker_test.go
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,148 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package tests | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"github.com/hyperledger/fabric-protos-go/ledger/rwset" | ||
"github.com/hyperledger/fabric-protos-go/ledger/rwset/kvrwset" | ||
"github.com/hyperledger/fabric-protos-go/peer" | ||
"github.com/hyperledger/fabric/common/util" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestNilValNoDeleteMarker tests for a special writeset which carries a nil value and yet the delete marker is set to false. | ||
// This kind of write-set gets produced in previous versions. See FAB-18386 for more details. | ||
func TestNilValNoDeleteMarker(t *testing.T) { | ||
env := newEnv(t) | ||
defer env.cleanup() | ||
env.initLedgerMgmt() | ||
|
||
testLedger := env.newTestHelperCreateLgr("test-ledger", t) | ||
testLedger.simulateDeployTx("cc1", []*collConf{ | ||
{ | ||
name: "coll1", | ||
}, | ||
}) | ||
testLedger.cutBlockAndCommitLegacy() | ||
|
||
testLedger.simulateDataTx("txid1", func(s *simulator) { | ||
s.setState("cc1", "pubKey1", "pubValue1") | ||
s.setPvtdata("cc1", "coll1", "pvtKey1", "pvtValue1") | ||
s.setPvtdata("cc1", "coll1", "pvtKey2", "pvtValue2") | ||
}) | ||
testLedger.cutBlockAndCommitLegacy() | ||
|
||
testLedger.verifyPubState("cc1", "pubKey1", "pubValue1") | ||
testLedger.verifyPvtdataHashState("cc1", "coll1", "pvtKey1", util.ComputeSHA256([]byte("pvtValue1"))) | ||
testLedger.verifyPvtdataHashState("cc1", "coll1", "pvtKey2", util.ComputeSHA256([]byte("pvtValue2"))) | ||
testLedger.verifyPvtState("cc1", "coll1", "pvtKey1", "pvtValue1") | ||
testLedger.verifyPvtState("cc1", "coll1", "pvtKey2", "pvtValue2") | ||
|
||
// Handcraft writeset that includes a delete for each of the above keys. We handcraft here because the one generated by the simulator | ||
// will always have IsDelete flag true and we want to test when this flag is set to false and the actual value is nil. See FAB-18386 for | ||
// more details. | ||
pubWrites := &kvrwset.KVRWSet{ | ||
Writes: []*kvrwset.KVWrite{ | ||
{ | ||
Key: "pubKey1", | ||
IsDelete: false, | ||
Value: nil, | ||
}, | ||
}, | ||
} | ||
|
||
hashedWrites := &kvrwset.HashedRWSet{ | ||
HashedWrites: []*kvrwset.KVWriteHash{ | ||
{ | ||
KeyHash: util.ComputeSHA256([]byte("pvtKey1")), | ||
IsDelete: false, | ||
ValueHash: nil, | ||
}, | ||
{ | ||
KeyHash: util.ComputeSHA256([]byte("pvtKey2")), | ||
IsDelete: false, | ||
ValueHash: util.ComputeSHA256([]byte{}), | ||
}, | ||
}, | ||
} | ||
|
||
pvtWrites := &kvrwset.KVRWSet{ | ||
Writes: []*kvrwset.KVWrite{ | ||
{ | ||
Key: "pvtKey1", | ||
IsDelete: false, | ||
}, | ||
{ | ||
Key: "pvtKey2", | ||
IsDelete: false, | ||
}, | ||
}, | ||
} | ||
|
||
pubWritesBytes, err := proto.Marshal(pubWrites) | ||
require.NoError(t, err) | ||
|
||
hashedWritesBytes, err := proto.Marshal(hashedWrites) | ||
require.NoError(t, err) | ||
|
||
pvtWritesBytes, err := proto.Marshal(pvtWrites) | ||
require.NoError(t, err) | ||
|
||
pubRwset := &rwset.TxReadWriteSet{ | ||
DataModel: rwset.TxReadWriteSet_KV, | ||
NsRwset: []*rwset.NsReadWriteSet{ | ||
{ | ||
Namespace: "cc1", | ||
Rwset: pubWritesBytes, | ||
CollectionHashedRwset: []*rwset.CollectionHashedReadWriteSet{ | ||
{ | ||
CollectionName: "coll1", | ||
HashedRwset: hashedWritesBytes, | ||
PvtRwsetHash: util.ComputeSHA256(pvtWritesBytes), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
pubRwsetBytes, err := proto.Marshal(pubRwset) | ||
require.NoError(t, err) | ||
envelope, err := constructTransaction("txid2", pubRwsetBytes) | ||
require.NoError(t, err) | ||
|
||
txAndPvtdata := &txAndPvtdata{ | ||
Txid: "txid2", | ||
Envelope: envelope, | ||
Pvtws: &rwset.TxPvtReadWriteSet{ | ||
DataModel: rwset.TxReadWriteSet_KV, | ||
NsPvtRwset: []*rwset.NsPvtReadWriteSet{ | ||
{ | ||
Namespace: "cc1", | ||
CollectionPvtRwset: []*rwset.CollectionPvtReadWriteSet{ | ||
{ | ||
CollectionName: "coll1", | ||
Rwset: pvtWritesBytes, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
testLedger.submitHandCraftedTx(txAndPvtdata) | ||
testLedger.cutBlockAndCommitLegacy() | ||
|
||
testLedger.verifyTxValidationCode("txid2", peer.TxValidationCode_VALID) | ||
testLedger.verifyPubState("cc1", "pubKey1", "") | ||
testLedger.verifyPvtdataHashState("cc1", "coll1", "pvtKey1", nil) | ||
testLedger.verifyPvtdataHashState("cc1", "coll1", "pvtKey2", nil) | ||
testLedger.verifyPvtState("cc1", "coll1", "pvtKey1", "") | ||
testLedger.verifyPvtState("cc1", "coll1", "pvtKey2", "") | ||
testLedger.verifyHistory("cc1", "pubKey1", []string{"", "pubValue1"}) | ||
} |
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
Oops, something went wrong.