[go: up one dir, main page]

Skip to content

Commit

Permalink
RWSet Preparation
Browse files Browse the repository at this point in the history
Resolves hyperledger#3025

Signed-off-by: Julian Castrence <juliancastrence@ibm.com>
  • Loading branch information
jrc-ibm authored and denyeart committed Jun 30, 2022
1 parent d2f0adb commit e235d41
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 1 deletion.
77 changes: 77 additions & 0 deletions core/chaincode/mock/tx_simulator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions core/endorser/fake/tx_simulator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions core/ledger/kvledger/txmgmt/rwsetutil/rwset_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ func (b *RWSetBuilder) AddToPvtAndHashedWriteSet(ns string, coll string, key str
b.getOrCreateCollHashedRwBuilder(ns, coll).writeMap[key] = kvWriteHash
}

// AddToHashedWriteSetPurge adds a purge key to the hashed write-set
func (b *RWSetBuilder) AddToHashedWriteSetPurge(ns string, coll string, key string) {
kvWriteHashPurge := newKVWriteHashPurge(key)
b.getOrCreateCollHashedRwBuilder(ns, coll).writeMap[key] = kvWriteHashPurge
}

// AddToHashedMetadataWriteSet adds a metadata to a key in the hashed write-set
func (b *RWSetBuilder) AddToHashedMetadataWriteSet(ns, coll, key string, metadata map[string][]byte) {
// pvt write set just need the key; not the entire metadata. The metadata is stored only
Expand Down
30 changes: 29 additions & 1 deletion core/ledger/kvledger/txmgmt/rwsetutil/rwset_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ func TestTxSimulationResultWithPvtData(t *testing.T) {
rwSetBuilder.AddToHashedReadSet("ns2", "coll1", "key2", version.NewHeight(1, 1))
rwSetBuilder.AddToPvtAndHashedWriteSet("ns2", "coll2", "key1", []byte("pvt-ns2-coll2-key1-value"))

// pvt rwset ns3
rwSetBuilder.AddToHashedWriteSetPurge("ns3", "coll1", "key1")

actualSimRes, err := rwSetBuilder.GetTxSimulationResults()
require.NoError(t, err)

Expand Down Expand Up @@ -151,6 +154,8 @@ func TestTxSimulationResultWithPvtData(t *testing.T) {
Writes: []*kvrwset.KVWrite{newKVWrite("key1", []byte("ns2-key1-value"))},
}

pubNs3 := &kvrwset.KVRWSet{}

hashedNs1Coll1 := &kvrwset.HashedRWSet{
HashedReads: []*kvrwset.KVReadHash{
constructTestPvtKVReadHash(t, "key1", version.NewHeight(1, 1)),
Expand Down Expand Up @@ -179,6 +184,12 @@ func TestTxSimulationResultWithPvtData(t *testing.T) {
},
}

hashedNs3Coll1 := &kvrwset.HashedRWSet{
HashedWrites: []*kvrwset.KVWriteHash{
constructTestKVWriteHashPurge(t, "key1"),
},
}

combinedNs1 := &rwset.NsReadWriteSet{
Namespace: "ns1",
Rwset: serializeTestProtoMsg(t, pubNs1),
Expand Down Expand Up @@ -212,9 +223,22 @@ func TestTxSimulationResultWithPvtData(t *testing.T) {
},
}
require.Equal(t, combinedNs2, actualSimRes.PubSimulationResults.NsRwset[1])

combinedNs3 := &rwset.NsReadWriteSet{
Namespace: "ns3",
Rwset: serializeTestProtoMsg(t, pubNs3),
CollectionHashedRwset: []*rwset.CollectionHashedReadWriteSet{
{
CollectionName: "coll1",
HashedRwset: serializeTestProtoMsg(t, hashedNs3Coll1),
},
},
}
require.Equal(t, combinedNs3, actualSimRes.PubSimulationResults.NsRwset[2])

expectedPubRWSet := &rwset.TxReadWriteSet{
DataModel: rwset.TxReadWriteSet_KV,
NsRwset: []*rwset.NsReadWriteSet{combinedNs1, combinedNs2},
NsRwset: []*rwset.NsReadWriteSet{combinedNs1, combinedNs2, combinedNs3},
}
require.Equal(t, expectedPubRWSet, actualSimRes.PubSimulationResults)
}
Expand Down Expand Up @@ -356,6 +380,10 @@ func constructTestPvtKVWriteHash(t *testing.T, key string, value []byte) *kvrwse
return kvWriteHash
}

func constructTestKVWriteHashPurge(t *testing.T, key string) *kvrwset.KVWriteHash {
return newKVWriteHashPurge(key)
}

func serializeTestProtoMsg(t *testing.T, protoMsg proto.Message) []byte {
msgBytes, err := proto.Marshal(protoMsg)
require.NoError(t, err)
Expand Down
5 changes: 5 additions & 0 deletions core/ledger/kvledger/txmgmt/rwsetutil/rwset_proto_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ func newPvtKVWriteAndHash(key string, value []byte) (*kvrwset.KVWrite, *kvrwset.
return kvWrite, &kvrwset.KVWriteHash{KeyHash: keyHash, IsDelete: kvWrite.IsDelete, ValueHash: valueHash}
}

func newKVWriteHashPurge(key string) *kvrwset.KVWriteHash {
keyHash := util.ComputeStringHash(key)
return &kvrwset.KVWriteHash{KeyHash: keyHash, IsDelete: true, IsPurge: true}
}

// IsKVWriteDelete returns true if the kvWrite indicates a delete operation. See FAB-18386 for details.
func IsKVWriteDelete(kvWrite *kvrwset.KVWrite) bool {
return kvWrite.IsDelete || len(kvWrite.Value) == 0
Expand Down
10 changes: 10 additions & 0 deletions core/ledger/kvledger/txmgmt/rwsetutil/rwset_proto_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ func TestVersionConversion(t *testing.T) {
require.Equal(t, protoVer, newProtoVersion(internalVer))
}

func TestNewKVWriteHashPurge(t *testing.T) {
sampleKey := "purge-key1"
sampleKVWriteHash := &kvrwset.KVWriteHash{
KeyHash: util.ComputeStringHash(sampleKey),
IsDelete: true,
IsPurge: true,
}
require.Equal(t, sampleKVWriteHash, newKVWriteHashPurge(sampleKey))
}

func TestIsDelete(t *testing.T) {
t.Run("kvWrite", func(t *testing.T) {
kvWritesToBeInterpretedAsDelete := []*kvrwset.KVWrite{
Expand Down
13 changes: 13 additions & 0 deletions core/ledger/kvledger/txmgmt/txmgr/tx_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ func (s *txSimulator) DeletePrivateData(ns, coll, key string) error {
return s.SetPrivateData(ns, coll, key, nil)
}

// PurgePrivateData implements method in interface `ledger.TxSimulator`
func (s *txSimulator) PurgePrivateData(ns, coll, key string) error {
if err := s.queryExecutor.validateCollName(ns, coll); err != nil {
return err
}
if err := s.checkWritePrecondition(key, nil); err != nil {
return err
}
s.writePerformed = true
s.rwsetBuilder.AddToHashedWriteSetPurge(ns, coll, key)
return nil
}

// SetPrivateDataMultipleKeys implements method in interface `ledger.TxSimulator`
func (s *txSimulator) SetPrivateDataMultipleKeys(ns, coll string, kvs map[string][]byte) error {
for k, v := range kvs {
Expand Down
Loading

0 comments on commit e235d41

Please sign in to comment.