-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_registry.go
187 lines (132 loc) · 3.48 KB
/
handler_registry.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chaincode
import (
"sync"
"github.com/mcc-github/blockchain/core/ledger"
"github.com/pkg/errors"
)
type HandlerRegistry struct {
allowUnsolicitedRegistration bool
mutex sync.Mutex
handlers map[string]*Handler
launching map[string]*LaunchState
}
type LaunchState struct {
mutex sync.Mutex
notified bool
done chan struct{}
err error
}
func NewLaunchState() *LaunchState {
return &LaunchState{
done: make(chan struct{}),
}
}
func (l *LaunchState) Done() <-chan struct{} {
return l.done
}
func (l *LaunchState) Err() error {
l.mutex.Lock()
err := l.err
l.mutex.Unlock()
return err
}
func (l *LaunchState) Notify(err error) {
l.mutex.Lock()
if !l.notified {
l.notified = true
l.err = err
close(l.done)
}
l.mutex.Unlock()
}
func NewHandlerRegistry(allowUnsolicitedRegistration bool) *HandlerRegistry {
return &HandlerRegistry{
handlers: map[string]*Handler{},
launching: map[string]*LaunchState{},
allowUnsolicitedRegistration: allowUnsolicitedRegistration,
}
}
func (r *HandlerRegistry) Launching(ccid string) (*LaunchState, bool) {
r.mutex.Lock()
defer r.mutex.Unlock()
if launchState, ok := r.launching[ccid]; ok {
return launchState, true
}
if _, ok := r.handlers[ccid]; ok {
launchState := NewLaunchState()
launchState.Notify(nil)
return launchState, true
}
launchState := NewLaunchState()
r.launching[ccid] = launchState
return launchState, false
}
func (r *HandlerRegistry) Ready(ccid string) {
r.mutex.Lock()
defer r.mutex.Unlock()
launchStatus := r.launching[ccid]
if launchStatus != nil {
launchStatus.Notify(nil)
}
}
func (r *HandlerRegistry) Failed(ccid string, err error) {
r.mutex.Lock()
defer r.mutex.Unlock()
launchStatus := r.launching[ccid]
if launchStatus != nil {
launchStatus.Notify(err)
}
}
func (r *HandlerRegistry) Handler(ccid string) *Handler {
r.mutex.Lock()
h := r.handlers[ccid]
r.mutex.Unlock()
return h
}
func (r *HandlerRegistry) Register(h *Handler) error {
r.mutex.Lock()
defer r.mutex.Unlock()
if r.handlers[h.chaincodeID] != nil {
chaincodeLogger.Debugf("duplicate registered handler(key:%s) return error", h.chaincodeID)
return errors.Errorf("duplicate chaincodeID: %s", h.chaincodeID)
}
if r.launching[h.chaincodeID] == nil && !r.allowUnsolicitedRegistration {
return errors.Errorf("peer will not accept external chaincode connection %s (except in dev mode)", h.chaincodeID)
}
r.handlers[h.chaincodeID] = h
chaincodeLogger.Debugf("registered handler complete for chaincode %s", h.chaincodeID)
return nil
}
func (r *HandlerRegistry) Deregister(ccid string) error {
chaincodeLogger.Debugf("deregister handler: %s", ccid)
r.mutex.Lock()
handler := r.handlers[ccid]
delete(r.handlers, ccid)
delete(r.launching, ccid)
r.mutex.Unlock()
if handler == nil {
return errors.Errorf("could not find handler: %s", ccid)
}
handler.Close()
chaincodeLogger.Debugf("deregistered handler with key: %s", ccid)
return nil
}
type TxQueryExecutorGetter struct {
HandlerRegistry *HandlerRegistry
CCID string
}
func (g *TxQueryExecutorGetter) TxQueryExecutor(chainID, txID string) ledger.SimpleQueryExecutor {
handler := g.HandlerRegistry.Handler(g.CCID)
if handler == nil {
return nil
}
txContext := handler.TXContexts.Get(chainID, txID)
if txContext == nil {
return nil
}
return txContext.TXSimulator
}