forked from hyperledger/fabric-x-committer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsigverifier.go
More file actions
141 lines (124 loc) · 4.06 KB
/
Copy pathsigverifier.go
File metadata and controls
141 lines (124 loc) · 4.06 KB
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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package adapters
import (
"context"
"github.com/cockroachdb/errors"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
"github.com/hyperledger/fabric-x-committer/api/protoblocktx"
"github.com/hyperledger/fabric-x-committer/api/protosigverifierservice"
"github.com/hyperledger/fabric-x-committer/api/types"
"github.com/hyperledger/fabric-x-committer/loadgen/metrics"
"github.com/hyperledger/fabric-x-committer/loadgen/workload"
"github.com/hyperledger/fabric-x-committer/utils/connection"
)
type (
// SvAdapter applies load on the SV.
SvAdapter struct {
commonAdapter
config *connection.MultiClientConfig
}
)
// NewSVAdapter instantiate SvAdapter.
func NewSVAdapter(config *connection.MultiClientConfig, res *ClientResources) *SvAdapter {
return &SvAdapter{
commonAdapter: commonAdapter{res: res},
config: config,
}
}
// RunWorkload applies load on the SV.
func (c *SvAdapter) RunWorkload(ctx context.Context, txStream *workload.StreamWithSetup) error {
updateMsg, err := createUpdate(c.res.Profile.Transaction.Policy)
if err != nil {
return err
}
connections, err := connection.NewConnectionPerEndpoint(c.config)
if err != nil {
return err
}
defer connection.CloseConnectionsLog(connections...)
dCtx, dCancel := context.WithCancel(ctx)
defer dCancel()
g, gCtx := errgroup.WithContext(dCtx)
streams := make([]protosigverifierservice.Verifier_StartStreamClient, len(connections))
for i, conn := range connections {
client := protosigverifierservice.NewVerifierClient(conn)
logger.Infof("Opening stream to %s", c.config.Endpoints[i])
streams[i], err = client.StartStream(gCtx)
if err != nil {
return errors.Wrapf(err, "failed opening a stream to %s", c.config.Endpoints[i])
}
logger.Infof("Set verification verification policy")
err = streams[i].Send(&protosigverifierservice.Batch{Update: updateMsg})
if err != nil {
return errors.Wrap(err, "failed submitting verification policy")
}
}
for _, stream := range streams {
stream := stream
g.Go(func() error {
return sendBlocks(gCtx, &c.commonAdapter, txStream, workload.MapToVerifierBatch, stream.Send)
})
g.Go(func() error {
defer dCancel() // We stop sending if we can't track the received items.
return c.receiveStatus(gCtx, stream)
})
}
return errors.Wrap(g.Wait(), "workload done")
}
func createUpdate(policy *workload.PolicyProfile) (*protosigverifierservice.Update, error) {
txSigner := workload.NewTxSignerVerifier(policy)
envelopeBytes, err := workload.CreateConfigEnvelope(policy)
if err != nil {
return nil, err
}
updateMsg := &protosigverifierservice.Update{
Config: &protoblocktx.ConfigTransaction{
Envelope: envelopeBytes,
},
NamespacePolicies: &protoblocktx.NamespacePolicies{
Policies: make([]*protoblocktx.PolicyItem, 0, len(txSigner.HashSigners)),
},
}
for ns, p := range txSigner.HashSigners {
if ns == types.MetaNamespaceID {
continue
}
policy, err := proto.Marshal(p.GetVerificationPolicy())
if err != nil {
return nil, errors.Wrap(err, "failed to serialize policy")
}
updateMsg.NamespacePolicies.Policies = append(
updateMsg.NamespacePolicies.Policies,
&protoblocktx.PolicyItem{
Namespace: ns,
Policy: policy,
},
)
}
return updateMsg, nil
}
func (c *SvAdapter) receiveStatus(
ctx context.Context, stream protosigverifierservice.Verifier_StartStreamClient,
) error {
for ctx.Err() == nil {
responseBatch, err := stream.Recv()
if err != nil {
return errors.Wrap(connection.FilterStreamRPCError(err), "failed receiving verification status")
}
logger.Debugf("Received SV batch with %d responses", len(responseBatch.Responses))
statusBatch := make([]metrics.TxStatus, len(responseBatch.Responses))
for i, response := range responseBatch.Responses {
logger.Debugf("Received Responses: %s", response.Status)
statusBatch[i] = metrics.TxStatus{TxID: response.Ref.TxId, Status: response.Status}
}
c.res.Metrics.OnReceiveBatch(statusBatch)
if c.res.isReceiveLimit() {
return nil
}
}
return nil
}