This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmocked_peer.go
214 lines (179 loc) · 4.56 KB
/
mocked_peer.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
package testing
import (
"context"
"errors"
"fmt"
"sync"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/msp"
)
type (
MockedPeer struct {
// channel name -> chaincode name
ChannelCC ChannelsMockStubs
m sync.Mutex
}
ChannelMockStubs map[string]*MockStub
ChannelsMockStubs map[string]ChannelMockStubs
EventSubscription struct {
events chan *peer.ChaincodeEvent
errors chan error
closer sync.Once
}
)
// NewPeer implements Peer interface
func NewPeer() *MockedPeer {
return &MockedPeer{
ChannelCC: make(ChannelsMockStubs),
}
}
func (mp *MockedPeer) WithChannel(channel string, mockStubs ...*MockStub) *MockedPeer {
if _, ok := mp.ChannelCC[channel]; !ok {
mp.ChannelCC[channel] = make(ChannelMockStubs)
}
for _, ms := range mockStubs {
mp.ChannelCC[channel][ms.Name] = ms
for chName, chMock := range mp.ChannelCC {
for ccName, cc := range chMock {
// add visibility of added cc to all others cc
cc.MockPeerChaincode(ms.Name+`/`+channel, ms)
// add visibility of other cc to added cc
ms.MockPeerChaincode(ccName+`/`+chName, cc)
}
}
}
return mp
}
func (mp *MockedPeer) CurrentIdentity() msp.SigningIdentity {
return nil
}
func (mp *MockedPeer) Invoke(
_ context.Context,
channel string,
chaincode string,
args [][]byte,
identity msp.SigningIdentity,
transArgs map[string][]byte,
_ string) (res *peer.Response, chaincodeTx string, err error) {
mp.m.Lock()
defer mp.m.Unlock()
mockStub, err := mp.Chaincode(channel, chaincode)
if err != nil {
return nil, ``, err
}
response := mockStub.From(identity).WithTransient(transArgs).InvokeBytes(args...)
if response.Status == shim.ERROR {
err = errors.New(response.Message)
}
return &response, mockStub.TxID, err
}
func (mp *MockedPeer) Query(
_ context.Context,
channel string,
chaincode string,
args [][]byte,
identity msp.SigningIdentity,
transArgs map[string][]byte) (*peer.Response, error) {
mp.m.Lock()
defer mp.m.Unlock()
mockStub, err := mp.Chaincode(channel, chaincode)
if err != nil {
return nil, err
}
response := mockStub.From(identity).WithTransient(transArgs).QueryBytes(args...)
if response.Status == shim.ERROR {
err = errors.New(response.Message)
}
return &response, err
}
func (mp *MockedPeer) Chaincode(channel string, chaincode string) (*MockStub, error) {
ms, exists := mp.ChannelCC[channel][chaincode]
if !exists {
return nil, fmt.Errorf(`%s: channell=%s, chaincode=%s`, ErrChaincodeNotExists, channel, chaincode)
}
return ms, nil
}
func (mp *MockedPeer) Events(
ctx context.Context,
channel string,
chaincode string,
_ msp.SigningIdentity,
blockRange ...int64,
) (events chan interface {
Event() *peer.ChaincodeEvent
Block() uint64
TxTimestamp() *timestamp.Timestamp
}, closer func() error, err error) {
mockStub, err := mp.Chaincode(channel, chaincode)
if err != nil {
return nil, nil, err
}
var (
eventsRaw chan *peer.ChaincodeEvent
)
// from the oldest block to current channel height
if len(blockRange) > 0 && blockRange[0] == 0 {
// create copy of mockStub events chan
eventsRaw, closer = mockStub.EventSubscription(0)
// In real HLF peer stream not closed
//// close events channel if its empty, because we receive events until current channel height
//go func() {
// ticker := time.NewTicker(5 * time.Millisecond)
// for {
// <-ticker.C
// if len(events) == 0 {
// closer()
// ticker.Stop()
// return
// }
// }
//}()
} else {
eventsRaw, closer = mockStub.EventSubscription()
}
go func() {
<-ctx.Done()
_ = closer()
}()
eventsExtended := make(chan interface {
Event() *peer.ChaincodeEvent
Block() uint64
TxTimestamp() *timestamp.Timestamp
})
go func() {
for e := range eventsRaw {
eventsExtended <- &ChaincodeEvent{
event: e,
// todo: store information about block and timestamp in MockStub
block: 55,
txTimestamp: nil,
}
}
close(eventsExtended)
}()
return eventsExtended, closer, nil
}
func (mp *MockedPeer) Blocks(
_ context.Context,
_ string,
_ msp.SigningIdentity,
_ ...int64,
) (blockChan <-chan *common.Block, closer func() error, err error) {
panic("not implemented")
}
func (es *EventSubscription) Events() chan *peer.ChaincodeEvent {
return es.events
}
func (es *EventSubscription) Errors() chan error {
return es.errors
}
func (es *EventSubscription) Close() error {
es.closer.Do(func() {
close(es.events)
close(es.errors)
})
return nil
}