-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifier.go
196 lines (171 loc) · 6.86 KB
/
verifier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package tests
import (
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger"
lgrutil "github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/ledger/rwset/kvrwset"
protopeer "github.com/hyperledger/fabric/protos/peer"
"github.com/stretchr/testify/assert"
)
// verifier provides functions that help tests with less verbose code for querying the ledger
// and verifying the actual results with the expected results
// For the straight forward functions, tests can call them directly on the ledger
type verifier struct {
lgr ledger.PeerLedger
assert *assert.Assertions
t *testing.T
}
func newVerifier(lgr ledger.PeerLedger, t *testing.T) *verifier {
return &verifier{lgr, assert.New(t), t}
}
func (v *verifier) verifyLedgerHeight(expectedHt uint64) {
info, err := v.lgr.GetBlockchainInfo()
v.assert.NoError(err)
v.assert.Equal(expectedHt, info.Height)
}
func (v *verifier) verifyPubState(ns, key string, expectedVal string) {
qe, err := v.lgr.NewQueryExecutor()
v.assert.NoError(err)
defer qe.Done()
committedVal, err := qe.GetState(ns, key)
v.assert.NoError(err)
v.t.Logf("val=%s", committedVal)
var expectedValBytes []byte
if expectedVal != "" {
expectedValBytes = []byte(expectedVal)
}
v.assert.Equal(expectedValBytes, committedVal)
}
func (v *verifier) verifyPvtState(ns, coll, key string, expectedVal string) {
qe, err := v.lgr.NewQueryExecutor()
v.assert.NoError(err)
defer qe.Done()
committedVal, err := qe.GetPrivateData(ns, coll, key)
v.assert.NoError(err)
v.t.Logf("val=%s", committedVal)
var expectedValBytes []byte
if expectedVal != "" {
expectedValBytes = []byte(expectedVal)
}
v.assert.Equal(expectedValBytes, committedVal)
}
func (v *verifier) verifyMostRecentCollectionConfigBelow(blockNum uint64, chaincodeName string, expectOut *expectedCollConfInfo) {
configHistory, err := v.lgr.GetConfigHistoryRetriever()
v.assert.NoError(err)
actualCollectionConfigInfo, err := configHistory.MostRecentCollectionConfigBelow(blockNum, chaincodeName)
v.assert.NoError(err)
if expectOut == nil {
v.assert.Nil(actualCollectionConfigInfo)
return
}
v.t.Logf("Retrieved CollectionConfigInfo=%s", spew.Sdump(actualCollectionConfigInfo))
actualCommittingBlockNum := actualCollectionConfigInfo.CommittingBlockNum
actualCollConf := convertFromCollConfigProto(actualCollectionConfigInfo.CollectionConfig)
v.assert.Equal(expectOut.committingBlockNum, actualCommittingBlockNum)
v.assert.Equal(expectOut.collConfs, actualCollConf)
}
func (v *verifier) verifyBlockAndPvtData(blockNum uint64, filter ledger.PvtNsCollFilter, verifyLogic func(r *retrievedBlockAndPvtdata)) {
out, err := v.lgr.GetPvtDataAndBlockByNum(blockNum, filter)
v.assert.NoError(err)
v.t.Logf("Retrieved Block = %s, pvtdata = %s", spew.Sdump(out.Block), spew.Sdump(out.PvtData))
verifyLogic(&retrievedBlockAndPvtdata{out, v.assert})
}
func (v *verifier) verifyBlockAndPvtDataSameAs(blockNum uint64, expectedOut *ledger.BlockAndPvtData) {
v.verifyBlockAndPvtData(blockNum, nil, func(r *retrievedBlockAndPvtdata) {
r.sameAs(expectedOut)
})
}
func (v *verifier) verifyGetTransactionByID(txid string, expectedOut *protopeer.ProcessedTransaction) {
tran, err := v.lgr.GetTransactionByID(txid)
v.assert.NoError(err)
envelopEqual := proto.Equal(expectedOut.TransactionEnvelope, tran.TransactionEnvelope)
v.assert.True(envelopEqual)
v.assert.Equal(expectedOut.ValidationCode, tran.ValidationCode)
}
func (v *verifier) verifyTxValidationCode(txid string, expectedCode protopeer.TxValidationCode) {
tran, err := v.lgr.GetTransactionByID(txid)
v.assert.NoError(err)
v.assert.Equal(int32(expectedCode), tran.ValidationCode)
}
//////////// structs used by verifier //////////////////////////////////////////////////////////////
type expectedCollConfInfo struct {
committingBlockNum uint64
collConfs []*collConf
}
type retrievedBlockAndPvtdata struct {
*ledger.BlockAndPvtData
assert *assert.Assertions
}
func (r *retrievedBlockAndPvtdata) sameAs(expectedBlockAndPvtdata *ledger.BlockAndPvtData) {
r.samePvtdata(expectedBlockAndPvtdata.PvtData)
r.sameBlockHeaderAndData(expectedBlockAndPvtdata.Block)
r.sameMetadata(expectedBlockAndPvtdata.Block)
}
func (r *retrievedBlockAndPvtdata) hasNumTx(numTx int) {
r.assert.Len(r.Block.Data.Data, numTx)
}
func (r *retrievedBlockAndPvtdata) hasNoPvtdata() {
r.assert.Len(r.PvtData, 0)
}
func (r *retrievedBlockAndPvtdata) pvtdataShouldContain(txSeq int, ns, coll, key, value string) {
txPvtData := r.BlockAndPvtData.PvtData[uint64(txSeq)]
for _, nsdata := range txPvtData.WriteSet.NsPvtRwset {
if nsdata.Namespace == ns {
for _, colldata := range nsdata.CollectionPvtRwset {
if colldata.CollectionName == coll {
rwset := &kvrwset.KVRWSet{}
r.assert.NoError(proto.Unmarshal(colldata.Rwset, rwset))
for _, w := range rwset.Writes {
if w.Key == key {
r.assert.Equal([]byte(value), w.Value)
return
}
}
}
}
}
}
r.assert.FailNow("Requested kv not found")
}
func (r *retrievedBlockAndPvtdata) pvtdataShouldNotContain(ns, coll string) {
allTxPvtData := r.BlockAndPvtData.PvtData
for _, txPvtData := range allTxPvtData {
r.assert.False(txPvtData.Has(ns, coll))
}
}
func (r *retrievedBlockAndPvtdata) sameBlockHeaderAndData(expectedBlock *common.Block) {
r.assert.True(proto.Equal(expectedBlock.Data, r.BlockAndPvtData.Block.Data))
r.assert.True(proto.Equal(expectedBlock.Header, r.BlockAndPvtData.Block.Header))
}
func (r *retrievedBlockAndPvtdata) sameMetadata(expectedBlock *common.Block) {
// marshalling/unmarshalling treats a nil byte and empty byte interchangeably (based on which scheme is chosen proto vs gob)
// so explicitly comparing each metadata
retrievedMetadata := r.Block.Metadata.Metadata
expectedMetadata := expectedBlock.Metadata.Metadata
r.assert.Equal(len(expectedMetadata), len(retrievedMetadata))
for i := 0; i < len(retrievedMetadata); i++ {
if len(expectedMetadata[i])+len(retrievedMetadata[i]) != 0 {
r.assert.Equal(expectedMetadata[i], retrievedMetadata[i])
}
}
}
func (r *retrievedBlockAndPvtdata) containsValidationCode(txSeq int, validationCode protopeer.TxValidationCode) {
var txFilter lgrutil.TxValidationFlags
txFilter = r.BlockAndPvtData.Block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER]
r.assert.Equal(validationCode, txFilter.Flag(txSeq))
}
func (r *retrievedBlockAndPvtdata) samePvtdata(expectedPvtdata map[uint64]*ledger.TxPvtData) {
r.assert.Equal(len(expectedPvtdata), len(r.BlockAndPvtData.PvtData))
for txNum, pvtData := range expectedPvtdata {
actualPvtData := r.BlockAndPvtData.PvtData[txNum]
r.assert.Equal(pvtData.SeqInBlock, actualPvtData.SeqInBlock)
r.assert.True(proto.Equal(pvtData.WriteSet, actualPvtData.WriteSet))
}
}