@@ -28,6 +28,7 @@ import (
28
28
"github.com/hyperledger/fabric/core/ledger/util"
29
29
"github.com/hyperledger/fabric/protos/common"
30
30
"github.com/hyperledger/fabric/protos/ledger/rwset/kvrwset"
31
+ "github.com/hyperledger/fabric/protos/peer"
31
32
"github.com/spf13/viper"
32
33
)
33
34
@@ -58,17 +59,17 @@ func TestValidator(t *testing.T) {
58
59
rwsetBuilder1 := rwsetutil .NewRWSetBuilder ()
59
60
rwsetBuilder1 .AddToReadSet ("ns1" , "key1" , version .NewHeight (1 , 0 ))
60
61
rwsetBuilder1 .AddToReadSet ("ns2" , "key2" , nil )
61
- checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder1 .GetTxReadWriteSet ()}, []int {})
62
+ checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder1 .GetTxReadWriteSet ()}, nil , []int {})
62
63
63
64
//rwset2 should not be valid
64
65
rwsetBuilder2 := rwsetutil .NewRWSetBuilder ()
65
66
rwsetBuilder2 .AddToReadSet ("ns1" , "key1" , version .NewHeight (1 , 1 ))
66
- checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder2 .GetTxReadWriteSet ()}, []int {0 })
67
+ checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder2 .GetTxReadWriteSet ()}, nil , []int {0 })
67
68
68
69
//rwset3 should not be valid
69
70
rwsetBuilder3 := rwsetutil .NewRWSetBuilder ()
70
71
rwsetBuilder3 .AddToReadSet ("ns1" , "key1" , nil )
71
- checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder3 .GetTxReadWriteSet ()}, []int {0 })
72
+ checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder3 .GetTxReadWriteSet ()}, nil , []int {0 })
72
73
73
74
// rwset4 and rwset5 within same block - rwset4 should be valid and makes rwset5 as invalid
74
75
rwsetBuilder4 := rwsetutil .NewRWSetBuilder ()
@@ -78,7 +79,27 @@ func TestValidator(t *testing.T) {
78
79
rwsetBuilder5 := rwsetutil .NewRWSetBuilder ()
79
80
rwsetBuilder5 .AddToReadSet ("ns1" , "key1" , version .NewHeight (1 , 0 ))
80
81
checkValidation (t , validator ,
81
- []* rwsetutil.TxRwSet {rwsetBuilder4 .GetTxReadWriteSet (), rwsetBuilder5 .GetTxReadWriteSet ()}, []int {1 })
82
+ []* rwsetutil.TxRwSet {rwsetBuilder4 .GetTxReadWriteSet (), rwsetBuilder5 .GetTxReadWriteSet ()}, nil , []int {1 })
83
+ }
84
+
85
+ func TestValidatorSkipInvalidTxs (t * testing.T ) {
86
+ testDBEnv := stateleveldb .NewTestVDBEnv (t )
87
+ defer testDBEnv .Cleanup ()
88
+ db , err := testDBEnv .DBProvider .GetDBHandle ("TestDB" )
89
+ testutil .AssertNoError (t , err , "" )
90
+ validator := NewValidator (db )
91
+
92
+ rwsetBuilder1 := rwsetutil .NewRWSetBuilder ()
93
+ rwsetBuilder1 .AddToWriteSet ("ns1" , "key1" , []byte ("value1" ))
94
+ rwsetBuilder2 := rwsetutil .NewRWSetBuilder ()
95
+ rwsetBuilder2 .AddToWriteSet ("ns1" , "key2" , []byte ("value2" ))
96
+ rwsetBuilder3 := rwsetutil .NewRWSetBuilder ()
97
+ rwsetBuilder3 .AddToWriteSet ("ns1" , "key3" , []byte ("value3" ))
98
+ flags := util .NewTxValidationFlags (4 )
99
+ flags .SetFlag (1 , peer .TxValidationCode_BAD_CREATOR_SIGNATURE )
100
+ checkValidation (t , validator ,
101
+ []* rwsetutil.TxRwSet {rwsetBuilder1 .GetTxReadWriteSet (), rwsetBuilder2 .GetTxReadWriteSet (), rwsetBuilder3 .GetTxReadWriteSet ()},
102
+ flags , []int {1 })
82
103
}
83
104
84
105
func TestPhantomValidation (t * testing.T ) {
@@ -106,7 +127,7 @@ func TestPhantomValidation(t *testing.T) {
106
127
rwsetutil .NewKVRead ("key2" , version .NewHeight (1 , 1 )),
107
128
rwsetutil .NewKVRead ("key3" , version .NewHeight (1 , 2 ))})
108
129
rwsetBuilder1 .AddToRangeQuerySet ("ns1" , rqi1 )
109
- checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder1 .GetTxReadWriteSet ()}, []int {})
130
+ checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder1 .GetTxReadWriteSet ()}, nil , []int {})
110
131
111
132
//rwset2 should not be valid - Version of key4 changed
112
133
rwsetBuilder2 := rwsetutil .NewRWSetBuilder ()
@@ -116,7 +137,7 @@ func TestPhantomValidation(t *testing.T) {
116
137
rwsetutil .NewKVRead ("key3" , version .NewHeight (1 , 2 )),
117
138
rwsetutil .NewKVRead ("key4" , version .NewHeight (1 , 2 ))})
118
139
rwsetBuilder2 .AddToRangeQuerySet ("ns1" , rqi2 )
119
- checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder2 .GetTxReadWriteSet ()}, []int {0 })
140
+ checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder2 .GetTxReadWriteSet ()}, nil , []int {0 })
120
141
121
142
//rwset3 should not be valid - simulate key3 got commited to db
122
143
rwsetBuilder3 := rwsetutil .NewRWSetBuilder ()
@@ -125,7 +146,7 @@ func TestPhantomValidation(t *testing.T) {
125
146
rwsetutil .NewKVRead ("key2" , version .NewHeight (1 , 1 )),
126
147
rwsetutil .NewKVRead ("key4" , version .NewHeight (1 , 3 ))})
127
148
rwsetBuilder3 .AddToRangeQuerySet ("ns1" , rqi3 )
128
- checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder3 .GetTxReadWriteSet ()}, []int {0 })
149
+ checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder3 .GetTxReadWriteSet ()}, nil , []int {0 })
129
150
130
151
// //Remove a key in rwset4 and rwset5 should become invalid
131
152
rwsetBuilder4 := rwsetutil .NewRWSetBuilder ()
@@ -138,7 +159,7 @@ func TestPhantomValidation(t *testing.T) {
138
159
rwsetutil .NewKVRead ("key4" , version .NewHeight (1 , 3 ))})
139
160
rwsetBuilder5 .AddToRangeQuerySet ("ns1" , rqi5 )
140
161
checkValidation (t , validator , []* rwsetutil.TxRwSet {
141
- rwsetBuilder4 .GetTxReadWriteSet (), rwsetBuilder5 .GetTxReadWriteSet ()}, []int {1 })
162
+ rwsetBuilder4 .GetTxReadWriteSet (), rwsetBuilder5 .GetTxReadWriteSet ()}, nil , []int {1 })
142
163
143
164
//Add a key in rwset6 and rwset7 should become invalid
144
165
rwsetBuilder6 := rwsetutil .NewRWSetBuilder ()
@@ -152,7 +173,7 @@ func TestPhantomValidation(t *testing.T) {
152
173
rwsetutil .NewKVRead ("key4" , version .NewHeight (1 , 3 ))})
153
174
rwsetBuilder7 .AddToRangeQuerySet ("ns1" , rqi7 )
154
175
checkValidation (t , validator , []* rwsetutil.TxRwSet {
155
- rwsetBuilder6 .GetTxReadWriteSet (), rwsetBuilder7 .GetTxReadWriteSet ()}, []int {1 })
176
+ rwsetBuilder6 .GetTxReadWriteSet (), rwsetBuilder7 .GetTxReadWriteSet ()}, nil , []int {1 })
156
177
}
157
178
158
179
func TestPhantomHashBasedValidation (t * testing.T ) {
@@ -190,7 +211,7 @@ func TestPhantomHashBasedValidation(t *testing.T) {
190
211
}
191
212
rqi1 .SetMerkelSummary (buildTestHashResults (t , 2 , kvReadsDuringSimulation1 ))
192
213
rwsetBuilder1 .AddToRangeQuerySet ("ns1" , rqi1 )
193
- checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder1 .GetTxReadWriteSet ()}, []int {})
214
+ checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder1 .GetTxReadWriteSet ()}, nil , []int {})
194
215
195
216
rwsetBuilder2 := rwsetutil .NewRWSetBuilder ()
196
217
rqi2 := & kvrwset.RangeQueryInfo {StartKey : "key1" , EndKey : "key9" , ItrExhausted : false }
@@ -207,18 +228,19 @@ func TestPhantomHashBasedValidation(t *testing.T) {
207
228
}
208
229
rqi2 .SetMerkelSummary (buildTestHashResults (t , 2 , kvReadsDuringSimulation2 ))
209
230
rwsetBuilder2 .AddToRangeQuerySet ("ns1" , rqi2 )
210
- checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder2 .GetTxReadWriteSet ()}, []int {0 })
231
+ checkValidation (t , validator , []* rwsetutil.TxRwSet {rwsetBuilder2 .GetTxReadWriteSet ()}, nil , []int {0 })
211
232
}
212
233
213
- func checkValidation (t * testing.T , validator * Validator , rwsets []* rwsetutil.TxRwSet , invalidTxIndexes []int ) {
234
+ func checkValidation (t * testing.T , validator * Validator , rwsets []* rwsetutil.TxRwSet ,
235
+ alreadyMarkedFlags util.TxValidationFlags , expectedInvalidTxIndexes []int ) {
214
236
simulationResults := [][]byte {}
215
237
for _ , txRWS := range rwsets {
216
238
sr , err := txRWS .ToProtoBytes ()
217
239
testutil .AssertNoError (t , err , "" )
218
240
simulationResults = append (simulationResults , sr )
219
241
}
220
242
block := testutil .ConstructBlock (t , 1 , []byte ("dummyPreviousHash" ), simulationResults , false )
221
- block .Metadata .Metadata [common .BlockMetadataIndex_TRANSACTIONS_FILTER ] = util . NewTxValidationFlags ( len ( block . Data . Data ))
243
+ block .Metadata .Metadata [common .BlockMetadataIndex_TRANSACTIONS_FILTER ] = alreadyMarkedFlags
222
244
_ , err := validator .ValidateAndPrepareBatch (block , true )
223
245
txsFltr := util .TxValidationFlags (block .Metadata .Metadata [common .BlockMetadataIndex_TRANSACTIONS_FILTER ])
224
246
invalidTxs := make ([]int , 0 )
@@ -228,8 +250,8 @@ func checkValidation(t *testing.T, validator *Validator, rwsets []*rwsetutil.TxR
228
250
}
229
251
}
230
252
testutil .AssertNoError (t , err , "" )
231
- testutil .AssertEquals (t , len (invalidTxs ), len (invalidTxIndexes ))
232
- testutil .AssertContainsAll (t , invalidTxs , invalidTxIndexes )
253
+ testutil .AssertEquals (t , len (invalidTxs ), len (expectedInvalidTxIndexes ))
254
+ testutil .AssertContainsAll (t , invalidTxs , expectedInvalidTxIndexes )
233
255
}
234
256
235
257
func buildTestHashResults (t * testing.T , maxDegree int , kvReads []* kvrwset.KVRead ) * kvrwset.QueryReadsMerkleSummary {
0 commit comments