-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway.back
237 lines (200 loc) · 6.16 KB
/
gateway.back
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
/*
Copyright Zhigui.com. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package fabric_connector
import (
"context"
"math/rand"
"reflect"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
clientEvent "github.com/hyperledger/fabric-sdk-go/pkg/client/event"
"github.com/hyperledger/fabric-sdk-go/pkg/client/ledger"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
contextApi "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/hyperledger/fabric-sdk-go/pkg/gateway"
"github.com/pkg/errors"
)
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
type ChaincodeEvent func(*fab.CCEvent)
type GatewayService struct {
org string
user string
gw *gateway.Gateway
sdk *fabsdk.FabricSDK
networks map[string]*gateway.Network
providers map[string]contextApi.ChannelProvider
}
func NewGatewayService(configBytes []byte, user string) (*GatewayService, error) {
configOpt := config.FromRaw(configBytes, "yaml")
gw, err := gateway.Connect(
gateway.WithConfig(configOpt),
gateway.WithUser(user),
)
if err != nil {
return nil, err
}
orgVal := reflect.ValueOf(gw).Elem().FieldByName("org")
org := orgVal.String()
sdk, err := fabsdk.New(configOpt)
if err != nil {
return nil, errors.Errorf("Failed to create new SDK: %s", err.Error())
}
return &GatewayService{
org: org,
user: user,
gw: gw,
sdk: sdk,
networks: make(map[string]*gateway.Network),
providers: make(map[string]contextApi.ChannelProvider),
}, nil
}
func (gs *GatewayService) InvokeChainCode(channelID, ccID, function string, args [][]byte) ([]byte, string, error) {
// Get the network channel
//network, err := gs.GetNetwork(channelID)
//if err != nil {
// return nil, "", err
//}
//newNetWork := (*FabNetwork)(unsafe.Pointer(network))
//client := reflect.ValueOf(network).Elem().FieldByName("client")
//method := client.MethodByName("Execute")
//if !method.IsValid() {
// return nil, "", errors.New("MethodByName: Execute invalid")
//}
//
//execute, ok := method.Interface().(func(request channel.Request, options ...channel.RequestOption) (channel.Response, error))
//if !ok {
// return nil, "", errors.New("invalid method type: Execute")
//}
chClient, err := channel.New(gs.GetChannelProvider(channelID))
if err != nil {
return nil, "", err
}
response, err := chClient.Execute(
channel.Request{
ChaincodeID: ccID,
Fcn: function,
Args: args,
},
channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
return nil, "", err
}
return response.Payload, string(response.TransactionID), nil
}
func (gs *GatewayService) QueryChainCode(channelID, ccID, function string, args [][]byte) ([]byte, error) {
chClient, err := channel.New(gs.GetChannelProvider(channelID))
if err != nil {
return nil, err
}
response, err := chClient.Query(channel.Request{ChaincodeID: ccID, Fcn: function, Args: args},
channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
return nil, err
}
return response.Payload, nil
}
func (gs *GatewayService) SubmitTransaction(channelID, ccID, function string, args []string) ([]byte, error) {
// Get the network channel
network, err := gs.GetNetwork(channelID)
if err != nil {
return nil, err
}
// Get the smart contract
contract := network.GetContract(ccID)
// Submit a transaction in that contract to the ledger
result, err := contract.SubmitTransaction(function, args...)
if err != nil {
return nil, err
}
return result, nil
}
func (gs *GatewayService) EvaluateTransaction(channelID, ccID, function string, args []string) ([]byte, error) {
// Get the network channel
network, err := gs.GetNetwork(channelID)
if err != nil {
return nil, err
}
// Get the smart contract
contract := network.GetContract(ccID)
// Submit a transaction in that contract to the ledger
result, err := contract.EvaluateTransaction(function, args...)
if err != nil {
return nil, err
}
return result, nil
}
func (gs *GatewayService) QueryTransaction(channelID string, transactionID fab.TransactionID) (*pb.ProcessedTransaction, error) {
ledgerClient, err := ledger.New(gs.GetChannelProvider(channelID))
if err != nil {
return nil, err
}
return ledgerClient.QueryTransaction(transactionID)
}
func (gs *GatewayService) RegisterChaincodeEvent(ctx context.Context, channelID, ccID, eventID string, event ChaincodeEvent) error {
// Get the network channel
network, err := gs.GetNetwork(channelID)
if err != nil {
return err
}
// Get the smart contract
contract := network.GetContract(ccID)
reg, notifier, err := contract.RegisterEvent(eventID)
if err != nil {
return err
}
for {
select {
case ccEvent := <-notifier:
event(ccEvent)
case <-ctx.Done():
contract.Unregister(reg)
return nil
}
}
}
func (gs *GatewayService) RegisterBlockEvent(ctx context.Context, channelID string, event BlockEventWithTransaction) error {
cliEvent, err := clientEvent.New(gs.GetChannelProvider(channelID), clientEvent.WithBlockEvents())
if err != nil {
return err
}
if err := registerBlockEvent(ctx, cliEvent, event, false); err != nil {
return err
}
return nil
}
func (gs *GatewayService) Close() {
gs.gw.Close()
}
func (gs *GatewayService) GetNetwork(channelID string) (*gateway.Network, error) {
if network, ok := gs.networks[channelID]; ok {
return network, nil
}
network, err := gs.gw.GetNetwork(channelID)
if err != nil {
return nil, err
}
gs.networks[channelID] = network
return network, nil
}
func (gs *GatewayService) GetChannelProvider(channelID string) contextApi.ChannelProvider {
if cp, ok := gs.providers[channelID]; ok {
return cp
}
channelProvider := gs.sdk.ChannelContext(channelID, fabsdk.WithUser(gs.user), fabsdk.WithOrg(gs.org))
gs.providers[channelID] = channelProvider
return channelProvider
}
func RandStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
return string(b)
}