-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmocked_peer_decorator.go
151 lines (125 loc) · 3.6 KB
/
mocked_peer_decorator.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
package testing
import (
"context"
"errors"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger-labs/cckit/sdk"
)
var (
ErrPeerInvoke = errors.New(`invoke failed`)
ErrPeerQuery = errors.New(`query failed`)
ChaincodeSimulationErrorReponse = &peer.Response{
Status: shim.ERROR, Message: `chaincode simulation produced error response`}
)
type (
MockedPeerDecorator struct {
SDK sdk.SDK
InvokeMutator InvokeMutator
QueryMutator QueryMutator
}
InvokeMutator func(
sdk sdk.SDK,
ctx context.Context,
channel string,
chaincode string,
args [][]byte,
identity msp.SigningIdentity,
transArgs map[string][]byte,
txWaiterType string) (res *peer.Response, chaincodeTx string, err error)
QueryMutator func(
sdk sdk.SDK,
ctx context.Context,
channel string,
chaincode string,
args [][]byte,
identity msp.SigningIdentity,
transArgs map[string][]byte) (*peer.Response, error)
)
func NewPeerDecorator(sdk sdk.SDK) *MockedPeerDecorator {
return &MockedPeerDecorator{
SDK: sdk,
}
}
func (mpd *MockedPeerDecorator) CurrentIdentity() msp.SigningIdentity {
return mpd.SDK.CurrentIdentity()
}
func (mpd *MockedPeerDecorator) Invoke(
ctx context.Context,
channel string,
chaincode string,
args [][]byte,
identity msp.SigningIdentity,
transArgs map[string][]byte,
txWaiterType string) (res *peer.Response, chaincodeTx string, err error) {
if mpd.InvokeMutator != nil {
return mpd.InvokeMutator(mpd.SDK, ctx, channel, chaincode, args, identity, transArgs, txWaiterType)
}
return mpd.SDK.Invoke(ctx, channel, chaincode, args, identity, transArgs, txWaiterType)
}
func (mpd *MockedPeerDecorator) Query(
ctx context.Context,
channel string,
chaincode string,
args [][]byte,
identity msp.SigningIdentity,
transArgs map[string][]byte) (*peer.Response, error) {
if mpd.QueryMutator != nil {
return mpd.QueryMutator(mpd.SDK, ctx, channel, chaincode, args, identity, transArgs)
}
return mpd.SDK.Query(ctx, channel, chaincode, args, identity, transArgs)
}
func (mpd *MockedPeerDecorator) Events(
ctx context.Context,
channel string,
chaincode string,
identity msp.SigningIdentity,
blockRange ...int64,
) (events chan interface {
Event() *peer.ChaincodeEvent
Block() uint64
TxTimestamp() *timestamp.Timestamp
}, closer func() error, err error) {
return mpd.SDK.Events(ctx, channel, chaincode, identity, blockRange...)
}
func (mpd *MockedPeerDecorator) FailChaincode(chaincodes ...string) {
mpd.FailInvoke(chaincodes...)
mpd.FailQuery(chaincodes...)
}
func (mpd *MockedPeerDecorator) FailInvoke(chaincodes ...string) {
mpd.InvokeMutator = func(
sdk sdk.SDK,
ctx context.Context,
channel string,
chaincode string,
args [][]byte,
identity msp.SigningIdentity,
transArgs map[string][]byte,
txWaiterType string) (res *peer.Response, chaincodeTx string, err error) {
for _, c := range chaincodes {
if chaincode == c {
return nil, ``, ErrPeerInvoke
}
}
return sdk.Invoke(ctx, channel, chaincode, args, identity, transArgs, txWaiterType)
}
}
func (mpd *MockedPeerDecorator) FailQuery(chaincodes ...string) {
mpd.QueryMutator = func(
sdk sdk.SDK,
ctx context.Context,
channel string,
chaincode string,
args [][]byte,
identity msp.SigningIdentity,
transArgs map[string][]byte) (res *peer.Response, err error) {
for _, c := range chaincodes {
if chaincode == c {
return nil, ErrPeerQuery
}
}
return sdk.Query(ctx, channel, chaincode, args, identity, transArgs)
}
}