Skip to content

Commit 9c58f13

Browse files
committed
[FAB-6074] rm endorserid from tstore APIs
This CR replaces endorserId with UUID. Change-Id: I54105eb4f63658f49696a5697e9d8b157f10ede4 Signed-off-by: senthil <cendhu@gmail.com>
1 parent 368b92a commit 9c58f13

File tree

8 files changed

+124
-134
lines changed

8 files changed

+124
-134
lines changed

core/transientstore/store.go

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/hyperledger/fabric/protos/ledger/rwset"
1414

1515
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
16+
"github.com/hyperledger/fabric/common/util"
1617
"github.com/hyperledger/fabric/core/ledger"
1718
"github.com/hyperledger/fabric/core/ledger/pvtdatastorage"
1819
"github.com/syndtr/goleveldb/leveldb/iterator"
@@ -38,13 +39,10 @@ type StoreProvider interface {
3839
// the permanent storage or the pruning of some data items is enforced by the policy
3940
type Store interface {
4041
// Persist stores the private read-write set of a transaction in the transient store
41-
Persist(txid string, endorserid string, endorsementBlkHt uint64, privateSimulationResults *rwset.TxPvtReadWriteSet) error
42+
Persist(txid string, endorsementBlkHt uint64, privateSimulationResults *rwset.TxPvtReadWriteSet) error
4243
// GetTxPvtRWSetByTxid returns an iterator due to the fact that the txid may have multiple private
4344
// RWSets persisted from different endorsers (via Gossip)
44-
GetTxPvtRWSetByTxid(txid string, filter ledger.PvtNsCollFilter) (*rwsetScanner, error)
45-
// GetSelfSimulatedTxPvtRWSetByTxid returns the private read-write set generated from the simulation
46-
// performed by the peer itself
47-
GetSelfSimulatedTxPvtRWSetByTxid(txid string) (*EndorserPvtSimulationResults, error)
45+
GetTxPvtRWSetByTxid(txid string, filter ledger.PvtNsCollFilter) (*RwsetScanner, error)
4846
// Purge removes private read-writes set generated by endorsers at block height lesser than
4947
// a given maxBlockNumToRetain. In other words, Purge only retains private read-write sets
5048
// that were generated at block height of maxBlockNumToRetain or higher.
@@ -56,7 +54,6 @@ type Store interface {
5654

5755
// EndorserPvtSimulationResults captures the deatils of the simulation results specific to an endorser
5856
type EndorserPvtSimulationResults struct {
59-
EndorserID string
6057
EndorsementBlockHeight uint64
6158
PvtSimulationResults *rwset.TxPvtReadWriteSet
6259
}
@@ -78,7 +75,7 @@ type store struct {
7875
ledgerID string
7976
}
8077

81-
type rwsetScanner struct {
78+
type RwsetScanner struct {
8279
txid string
8380
dbItr iterator.Iterator
8481
filter ledger.PvtNsCollFilter
@@ -102,56 +99,38 @@ func (provider *storeProvider) Close() {
10299
}
103100

104101
// Persist stores the private read-write set of a transaction in the transient store
105-
func (s *store) Persist(txid string, endorserid string,
106-
endorsementBlkHt uint64, privateSimulationResults *rwset.TxPvtReadWriteSet) error {
102+
func (s *store) Persist(txid string, endorsementBlkHt uint64,
103+
privateSimulationResults *rwset.TxPvtReadWriteSet) error {
107104
dbBatch := leveldbhelper.NewUpdateBatch()
108105

109-
// Create compositeKey with appropriate prefix, txid, endorserid and endorsementBlkHt
110-
compositeKey := createCompositeKeyForPvtRWSet(txid, endorserid, endorsementBlkHt)
106+
// Create compositeKey with appropriate prefix, txid, uuid and endorsementBlkHt
107+
// Due to the fact that the txid may have multiple private RWSets persisted from different
108+
// endorsers (via Gossip), we postfix an uuid with the txid to avoid collision.
109+
uuid := util.GenerateUUID()
110+
compositeKey := createCompositeKeyForPvtRWSet(txid, uuid, endorsementBlkHt)
111111
privateSimulationResultsBytes, err := proto.Marshal(privateSimulationResults)
112112
if err != nil {
113113
return err
114114
}
115115
dbBatch.Put(compositeKey, privateSimulationResultsBytes)
116116

117-
// Create compositeKey with appropriate prefix, endorsementBlkHt, txid, endorserid & Store
117+
// Create compositeKey with appropriate prefix, endorsementBlkHt, txid, uuid & Store
118118
// the compositeKey (purge index) a null byte as value.
119-
compositeKey = createCompositeKeyForPurgeIndex(endorsementBlkHt, txid, endorserid)
119+
compositeKey = createCompositeKeyForPurgeIndex(endorsementBlkHt, txid, uuid)
120120
dbBatch.Put(compositeKey, emptyValue)
121121

122122
return s.db.WriteBatch(dbBatch, true)
123123
}
124124

125125
// GetTxPvtRWSetByTxid returns an iterator due to the fact that the txid may have multiple private
126-
// RWSets persisted from different endorserids. Eventually, we may pass a set of collections name
127-
// (filters) along with txid.
128-
func (s *store) GetTxPvtRWSetByTxid(txid string, filter ledger.PvtNsCollFilter) (*rwsetScanner, error) {
126+
// RWSets persisted from different endorsers.
127+
func (s *store) GetTxPvtRWSetByTxid(txid string, filter ledger.PvtNsCollFilter) (*RwsetScanner, error) {
129128
// Construct startKey and endKey to do an range query
130129
startKey := createTxidRangeStartKey(txid)
131130
endKey := createTxidRangeEndKey(txid)
132131

133132
iter := s.db.GetIterator(startKey, endKey)
134-
return &rwsetScanner{txid, iter, filter}, nil
135-
}
136-
137-
// GetSelfSimulatedTxPvtRWSetByTxid returns the private write set generated from the simulation performed
138-
// by the peer itself. Eventually, we may pass a set of collections name (filters) along with txid.
139-
func (s *store) GetSelfSimulatedTxPvtRWSetByTxid(txid string) (*EndorserPvtSimulationResults, error) {
140-
var err error
141-
var itr *rwsetScanner
142-
if itr, err = s.GetTxPvtRWSetByTxid(txid, nil); err != nil {
143-
return nil, err
144-
}
145-
defer itr.Close()
146-
var res *EndorserPvtSimulationResults
147-
for {
148-
if res, err = itr.Next(); res == nil || err != nil {
149-
return nil, err
150-
}
151-
if selfSimulated(res) {
152-
return res, nil
153-
}
154-
}
133+
return &RwsetScanner{txid, iter, filter}, nil
155134
}
156135

157136
// Purge removes private read-writes set generated by endorsers at block height lesser than
@@ -165,12 +144,12 @@ func (s *store) Purge(maxBlockNumToRetain uint64) error {
165144

166145
dbBatch := leveldbhelper.NewUpdateBatch()
167146

168-
// Get all txid and endorserid from above result and remove it from transient store (both
147+
// Get all txid and uuid from above result and remove it from transient store (both
169148
// read/write set and the corresponding index.
170149
for iter.Next() {
171150
dbKey := iter.Key()
172-
txid, endorserid, endorsementBlkHt := splitCompositeKeyOfPurgeIndex(dbKey)
173-
compositeKey := createCompositeKeyForPvtRWSet(txid, endorserid, endorsementBlkHt)
151+
txid, uuid, endorsementBlkHt := splitCompositeKeyOfPurgeIndex(dbKey)
152+
compositeKey := createCompositeKeyForPvtRWSet(txid, uuid, endorsementBlkHt)
174153
dbBatch.Delete(compositeKey)
175154
dbBatch.Delete(dbKey)
176155
}
@@ -203,13 +182,13 @@ func (s *store) Shutdown() {
203182

204183
// Next moves the iterator to the next key/value pair.
205184
// It returns whether the iterator is exhausted.
206-
func (scanner *rwsetScanner) Next() (*EndorserPvtSimulationResults, error) {
185+
func (scanner *RwsetScanner) Next() (*EndorserPvtSimulationResults, error) {
207186
if !scanner.dbItr.Next() {
208187
return nil, nil
209188
}
210189
dbKey := scanner.dbItr.Key()
211190
dbVal := scanner.dbItr.Value()
212-
endorserid, endorsementBlkHt := splitCompositeKeyOfPvtRWSet(dbKey)
191+
_, endorsementBlkHt := splitCompositeKeyOfPvtRWSet(dbKey)
213192

214193
txPvtRWSet := &rwset.TxPvtReadWriteSet{}
215194
if err := proto.Unmarshal(dbVal, txPvtRWSet); err != nil {
@@ -218,17 +197,12 @@ func (scanner *rwsetScanner) Next() (*EndorserPvtSimulationResults, error) {
218197
filteredTxPvtRWSet := pvtdatastorage.TrimPvtWSet(txPvtRWSet, scanner.filter)
219198

220199
return &EndorserPvtSimulationResults{
221-
EndorserID: endorserid,
222200
EndorsementBlockHeight: endorsementBlkHt,
223201
PvtSimulationResults: filteredTxPvtRWSet,
224202
}, nil
225203
}
226204

227205
// Close releases resource held by the iterator
228-
func (scanner *rwsetScanner) Close() {
206+
func (scanner *RwsetScanner) Close() {
229207
scanner.dbItr.Release()
230208
}
231-
232-
func selfSimulated(pvtSimRes *EndorserPvtSimulationResults) bool {
233-
return pvtSimRes.EndorserID == ""
234-
}

core/transientstore/store_helper.go

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
2-
Copyright IBM Corp. 2017 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package transientstore
@@ -31,14 +21,14 @@ var (
3121
)
3222

3323
// createCompositeKeyForPvtRWSet creates a key for storing private read-write set
34-
// in the transient store. The structure of the key is <prwsetPrefix>~txid~endorserid~endorsementBlkHt.
35-
func createCompositeKeyForPvtRWSet(txid string, endorserid string, endorsementBlkHt uint64) []byte {
24+
// in the transient store. The structure of the key is <prwsetPrefix>~txid~uuid~endorsementBlkHt.
25+
func createCompositeKeyForPvtRWSet(txid string, uuid string, endorsementBlkHt uint64) []byte {
3626
var compositeKey []byte
3727
compositeKey = append(compositeKey, prwsetPrefix)
3828
compositeKey = append(compositeKey, compositeKeySep)
3929
compositeKey = append(compositeKey, []byte(txid)...)
4030
compositeKey = append(compositeKey, compositeKeySep)
41-
compositeKey = append(compositeKey, []byte(endorserid)...)
31+
compositeKey = append(compositeKey, []byte(uuid)...)
4232
compositeKey = append(compositeKey, compositeKeySep)
4333
compositeKey = append(compositeKey, util.EncodeOrderPreservingVarUint64(endorsementBlkHt)...)
4434

@@ -47,37 +37,37 @@ func createCompositeKeyForPvtRWSet(txid string, endorserid string, endorsementBl
4737

4838
// createCompositeKeyForPurgeIndex creates a key to index private read-write set based on
4939
// endorsement block height such that purge based on block height can be achieved. The structure
50-
// of the key is <purgeIndexPrefix>~endorsementBlkHt~txid~endorserid.
51-
func createCompositeKeyForPurgeIndex(endorsementBlkHt uint64, txid string, endorserid string) []byte {
40+
// of the key is <purgeIndexPrefix>~endorsementBlkHt~txid~uuid.
41+
func createCompositeKeyForPurgeIndex(endorsementBlkHt uint64, txid string, uuid string) []byte {
5242
var compositeKey []byte
5343
compositeKey = append(compositeKey, purgeIndexPrefix)
5444
compositeKey = append(compositeKey, compositeKeySep)
5545
compositeKey = append(compositeKey, util.EncodeOrderPreservingVarUint64(endorsementBlkHt)...)
5646
compositeKey = append(compositeKey, compositeKeySep)
5747
compositeKey = append(compositeKey, []byte(txid)...)
5848
compositeKey = append(compositeKey, compositeKeySep)
59-
compositeKey = append(compositeKey, []byte(endorserid)...)
49+
compositeKey = append(compositeKey, []byte(uuid)...)
6050

6151
return compositeKey
6252
}
6353

64-
// splitCompositeKeyOfPvtRWSet splits the compositeKey (<prwsetPrefix>~txid~endorserid~endorsementBlkHt) into endorserId and endorsementBlkHt.
65-
func splitCompositeKeyOfPvtRWSet(compositeKey []byte) (endorserid string, endorsementBlkHt uint64) {
54+
// splitCompositeKeyOfPvtRWSet splits the compositeKey (<prwsetPrefix>~txid~uuid~endorsementBlkHt) into endorserId and endorsementBlkHt.
55+
func splitCompositeKeyOfPvtRWSet(compositeKey []byte) (uuid string, endorsementBlkHt uint64) {
6656
compositeKey = compositeKey[2:]
6757
firstSepIndex := bytes.IndexByte(compositeKey, compositeKeySep)
6858
secondSepIndex := firstSepIndex + bytes.IndexByte(compositeKey[firstSepIndex+1:], compositeKeySep) + 1
69-
endorserid = string(compositeKey[firstSepIndex+1 : secondSepIndex])
59+
uuid = string(compositeKey[firstSepIndex+1 : secondSepIndex])
7060
endorsementBlkHt, _ = util.DecodeOrderPreservingVarUint64(compositeKey[secondSepIndex+1:])
71-
return endorserid, endorsementBlkHt
61+
return uuid, endorsementBlkHt
7262
}
7363

74-
// splitCompositeKeyOfPurgeIndex splits the compositeKey (<purgeIndexPrefix>~endorsementBlkHt~txid~endorserid) into txid, endorserid and endorsementBlkHt.
75-
func splitCompositeKeyOfPurgeIndex(compositeKey []byte) (txid string, endorserid string, endorsementBlkHt uint64) {
64+
// splitCompositeKeyOfPurgeIndex splits the compositeKey (<purgeIndexPrefix>~endorsementBlkHt~txid~uuid) into txid, uuid and endorsementBlkHt.
65+
func splitCompositeKeyOfPurgeIndex(compositeKey []byte) (txid string, uuid string, endorsementBlkHt uint64) {
7666
var n int
7767
endorsementBlkHt, n = util.DecodeOrderPreservingVarUint64(compositeKey[2:])
7868
splits := bytes.Split(compositeKey[n+3:], []byte{compositeKeySep})
7969
txid = string(splits[0])
80-
endorserid = string(splits[1])
70+
uuid = string(splits[1])
8171
return
8272
}
8373

0 commit comments

Comments
 (0)