forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin_endorser.go
246 lines (210 loc) · 7.95 KB
/
plugin_endorser.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Copyright IBM Corp. 2018 All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package endorser
import (
"fmt"
"sync"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/handlers/endorsement/api"
endorsement3 "github.com/hyperledger/fabric/core/handlers/endorsement/api/identities"
"github.com/hyperledger/fabric/core/transientstore"
pb "github.com/hyperledger/fabric/protos/peer"
putils "github.com/hyperledger/fabric/protos/utils"
"github.com/pkg/errors"
)
//go:generate mockery -dir . -name TransientStoreRetriever -case underscore -output mocks/
//go:generate mockery -dir ../transientstore/ -name Store -case underscore -output mocks/
// TransientStoreRetriever retrieves transient stores
type TransientStoreRetriever interface {
// StoreForChannel returns the transient store for the given channel
StoreForChannel(channel string) transientstore.Store
}
//go:generate mockery -dir . -name ChannelStateRetriever -case underscore -output mocks/
// ChannelStateRetriever retrieves Channel state
type ChannelStateRetriever interface {
// ChannelState returns a QueryCreator for the given Channel
NewQueryCreator(channel string) (QueryCreator, error)
}
//go:generate mockery -dir . -name PluginMapper -case underscore -output mocks/
// PluginMapper maps plugin names to their corresponding factories
type PluginMapper interface {
PluginFactoryByName(name PluginName) endorsement.PluginFactory
}
// MapBasedPluginMapper maps plugin names to their corresponding factories
type MapBasedPluginMapper map[string]endorsement.PluginFactory
// PluginFactoryByName returns a plugin factory for the given plugin name, or nil if not found
func (m MapBasedPluginMapper) PluginFactoryByName(name PluginName) endorsement.PluginFactory {
return m[string(name)]
}
// Context defines the data that is related to an in-flight endorsement
type Context struct {
PluginName string
Channel string
TxID string
Proposal *pb.Proposal
SignedProposal *pb.SignedProposal
Visibility []byte
Response *pb.Response
Event []byte
ChaincodeID *pb.ChaincodeID
SimRes []byte
}
// String returns a text representation of this context
func (c Context) String() string {
return fmt.Sprintf("{plugin: %s, channel: %s, tx: %s, chaincode: %s}", c.PluginName, c.Channel, c.TxID, c.ChaincodeID.Name)
}
// PluginSupport aggregates the support interfaces
// needed for the operation of the plugin endorser
type PluginSupport struct {
ChannelStateRetriever
endorsement3.SigningIdentityFetcher
PluginMapper
TransientStoreRetriever
}
// NewPluginEndorser endorses with using a plugin
func NewPluginEndorser(ps *PluginSupport) *PluginEndorser {
return &PluginEndorser{
SigningIdentityFetcher: ps.SigningIdentityFetcher,
PluginMapper: ps.PluginMapper,
pluginChannelMapping: make(map[PluginName]*pluginsByChannel),
ChannelStateRetriever: ps.ChannelStateRetriever,
TransientStoreRetriever: ps.TransientStoreRetriever,
}
}
// PluginName defines the name of the plugin as it appears in the configuration
type PluginName string
type pluginsByChannel struct {
sync.RWMutex
pluginFactory endorsement.PluginFactory
channels2Plugins map[string]endorsement.Plugin
pe *PluginEndorser
}
func (pbc *pluginsByChannel) createPluginIfAbsent(channel string) (endorsement.Plugin, error) {
pbc.RLock()
plugin, exists := pbc.channels2Plugins[channel]
pbc.RUnlock()
if exists {
return plugin, nil
}
pbc.Lock()
defer pbc.Unlock()
plugin, exists = pbc.channels2Plugins[channel]
if exists {
return plugin, nil
}
pluginInstance := pbc.pluginFactory.New()
plugin, err := pbc.initPlugin(pluginInstance, channel)
if err != nil {
return nil, err
}
pbc.channels2Plugins[channel] = plugin
return plugin, nil
}
func (pbc *pluginsByChannel) initPlugin(plugin endorsement.Plugin, channel string) (endorsement.Plugin, error) {
var dependencies []endorsement.Dependency
var err error
// If this is a channel endorsement, add the channel state as a dependency
if channel != "" {
query, err := pbc.pe.NewQueryCreator(channel)
if err != nil {
return nil, errors.Wrap(err, "failed obtaining channel state")
}
store := pbc.pe.TransientStoreRetriever.StoreForChannel(channel)
if store == nil {
return nil, errors.Errorf("transient store for channel %s was not initialized", channel)
}
dependencies = append(dependencies, &ChannelState{QueryCreator: query, Store: store})
}
// Add the SigningIdentityFetcher as a dependency
dependencies = append(dependencies, pbc.pe.SigningIdentityFetcher)
err = plugin.Init(dependencies...)
if err != nil {
return nil, err
}
return plugin, nil
}
// PluginEndorser endorsers proposal responses using plugins
type PluginEndorser struct {
sync.Mutex
PluginMapper
pluginChannelMapping map[PluginName]*pluginsByChannel
ChannelStateRetriever
endorsement3.SigningIdentityFetcher
TransientStoreRetriever
}
// EndorseWithPlugin endorses the response with a plugin
func (pe *PluginEndorser) EndorseWithPlugin(ctx Context) (*pb.ProposalResponse, error) {
endorserLogger.Debug("Entering endorsement for", ctx)
if ctx.Response == nil {
return nil, errors.New("response is nil")
}
if ctx.Response.Status >= shim.ERRORTHRESHOLD {
return &pb.ProposalResponse{Response: ctx.Response}, nil
}
plugin, err := pe.getOrCreatePlugin(PluginName(ctx.PluginName), ctx.Channel)
if err != nil {
endorserLogger.Warning("Endorsement with plugin for", ctx, " failed:", err)
return nil, errors.Errorf("plugin with name %s could not be used: %v", ctx.PluginName, err)
}
prpBytes, err := proposalResponsePayloadFromContext(ctx)
if err != nil {
endorserLogger.Warning("Endorsement with plugin for", ctx, " failed:", err)
return nil, errors.Wrap(err, "failed assembling proposal response payload")
}
endorsement, prpBytes, err := plugin.Endorse(prpBytes, ctx.SignedProposal)
if err != nil {
endorserLogger.Warning("Endorsement with plugin for", ctx, " failed:", err)
return nil, errors.WithStack(err)
}
resp := &pb.ProposalResponse{
Version: 1,
Endorsement: endorsement,
Payload: prpBytes,
Response: ctx.Response,
}
endorserLogger.Debug("Exiting", ctx)
return resp, nil
}
// getAndStorePlugin returns a plugin instance for the given plugin name and channel
func (pe *PluginEndorser) getOrCreatePlugin(plugin PluginName, channel string) (endorsement.Plugin, error) {
pluginFactory := pe.PluginFactoryByName(plugin)
if pluginFactory == nil {
return nil, errors.Errorf("plugin with name %s wasn't found", plugin)
}
pluginsByChannel := pe.getOrCreatePluginChannelMapping(PluginName(plugin), pluginFactory)
return pluginsByChannel.createPluginIfAbsent(channel)
}
func (pe *PluginEndorser) getOrCreatePluginChannelMapping(plugin PluginName, pf endorsement.PluginFactory) *pluginsByChannel {
pe.Lock()
defer pe.Unlock()
endorserChannelMapping, exists := pe.pluginChannelMapping[PluginName(plugin)]
if !exists {
endorserChannelMapping = &pluginsByChannel{
pluginFactory: pf,
channels2Plugins: make(map[string]endorsement.Plugin),
pe: pe,
}
pe.pluginChannelMapping[PluginName(plugin)] = endorserChannelMapping
}
return endorserChannelMapping
}
func proposalResponsePayloadFromContext(ctx Context) ([]byte, error) {
hdr, err := putils.GetHeader(ctx.Proposal.Header)
if err != nil {
endorserLogger.Warning("Failed parsing header", err)
return nil, errors.Wrap(err, "failed parsing header")
}
pHashBytes, err := putils.GetProposalHash1(hdr, ctx.Proposal.Payload, ctx.Visibility)
if err != nil {
endorserLogger.Warning("Failed computing proposal hash", err)
return nil, errors.Wrap(err, "could not compute proposal hash")
}
prpBytes, err := putils.GetBytesProposalResponsePayload(pHashBytes, ctx.Response, ctx.SimRes, ctx.Event, ctx.ChaincodeID)
if err != nil {
endorserLogger.Warning("Failed marshaling the proposal response payload to bytes", err)
return nil, errors.New("failure while marshaling the ProposalResponsePayload")
}
return prpBytes, nil
}