forked from s7techlab/cckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
235 lines (186 loc) · 4.81 KB
/
context.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package router
import (
"time"
"github.com/hyperledger/fabric/core/chaincode/lib/cid"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/s7techlab/cckit/state"
)
// Default parameter name
const DefaultParam = `default`
type (
// Context of chaincode invoke
Context interface {
Stub() shim.ChaincodeStubInterface
Client() (cid.ClientIdentity, error)
Response() Response
Logger() *shim.ChaincodeLogger
Path() string
State() state.State
UseState(state.State) state.State
Time() (time.Time, error)
ReplaceArgs(args [][]byte) Context // replace args, for usage in preMiddleware
GetArgs() [][]byte
// Deprecated: Use Params instead.
Args() InterfaceMap
// Deprecated: Use Arg instead.
Arg(string) interface{}
// Deprecated: Use ParamString instead.
ArgString(string) string
// Deprecated: Use ParamBytes instead.
ArgBytes(string) []byte
// Deprecated: Use ParamInt instead.
ArgInt(string) int
// Deprecated: Use SetParam instead.
SetArg(string, interface{})
// Params returns parameter values.
Params() InterfaceMap
// Param returns parameter value.
Param(name ...string) interface{}
// ParamString returns parameter value as string.
ParamString(name string) string
// ParamBytes returns parameter value as bytes.
ParamBytes(name string) []byte
// ParamInt returns parameter value as bytes.
ParamInt(name string) int
// SetParam sets parameter value.
SetParam(name string, value interface{})
// Get retrieves data from the context.
Get(key string) interface{}
// Set saves data in the context.
Set(key string, value interface{})
// Deprecated: Use Event().Set() instead
SetEvent(string, interface{}) error
Event() state.Event
UseEvent(state.Event) state.Event
}
context struct {
stub shim.ChaincodeStubInterface
logger *shim.ChaincodeLogger
state state.State
event state.Event
args [][]byte
params InterfaceMap
store InterfaceMap
}
)
func (c *context) Stub() shim.ChaincodeStubInterface {
return c.stub
}
func (c *context) Client() (cid.ClientIdentity, error) {
return cid.New(c.Stub())
}
func (c *context) Response() Response {
return ContextResponse{c}
}
func (c *context) Logger() *shim.ChaincodeLogger {
return c.logger
}
func (c *context) Path() string {
if len(c.GetArgs()) == 0 {
return ``
}
return string(c.GetArgs()[0])
}
func (c *context) State() state.State {
if c.state == nil {
c.state = state.NewState(c.stub, c.logger)
}
return c.state
}
func (c *context) UseState(s state.State) state.State {
c.state = s
return c.state
}
func (c *context) Event() state.Event {
if c.event == nil {
c.event = state.NewEvent(c.stub)
}
return c.event
}
func (c *context) UseEvent(e state.Event) state.Event {
c.event = e
return c.event
}
// Time
func (c *context) Time() (time.Time, error) {
txTimestamp, err := c.stub.GetTxTimestamp()
if err != nil {
return time.Unix(0, 0), err
}
return time.Unix(txTimestamp.GetSeconds(), int64(txTimestamp.GetNanos())), nil
}
// ReplaceArgs replace args, for usage in preMiddleware
func (c *context) ReplaceArgs(args [][]byte) Context {
c.args = args
return c
}
func (c *context) GetArgs() [][]byte {
if c.args != nil {
return c.args
}
return c.stub.GetArgs()
}
// Deprecated: Use Params instead.
func (c *context) Args() InterfaceMap {
return c.Params()
}
func (c *context) Params() InterfaceMap {
return c.params
}
// Deprecated: Use SetParam instead.
func (c *context) SetArg(name string, value interface{}) {
c.SetParam(name, value)
}
func (c *context) SetParam(name string, value interface{}) {
if c.params == nil {
c.params = make(InterfaceMap)
}
c.params[name] = value
}
// Deprecated: Use Param instead.
func (c *context) Arg(name string) interface{} {
return c.Param(name)
}
func (c *context) Param(name ...string) interface{} {
var pname = DefaultParam
if len(name) > 0 {
pname = name[0]
}
return c.params[pname]
}
// Deprecated: Use ParamString instead.
func (c *context) ArgString(name string) string {
return c.ParamString(name)
}
func (c *context) ParamString(name string) string {
out, _ := c.Param(name).(string)
return out
}
// Deprecated: Use ParamBytes instead.
func (c *context) ArgBytes(name string) []byte {
return c.ParamBytes(name)
}
func (c *context) ParamBytes(name string) []byte {
out, _ := c.Param(name).([]byte)
return out
}
// Deprecated: Use ParamInt instead.
func (c *context) ArgInt(name string) int {
return c.ParamInt(name)
}
func (c *context) ParamInt(name string) int {
out, _ := c.Param(name).(int)
return out
}
func (c *context) Set(key string, val interface{}) {
if c.store == nil {
c.store = make(InterfaceMap)
}
c.store[key] = val
}
func (c *context) Get(key string) interface{} {
return c.store[key]
}
func (c *context) SetEvent(name string, payload interface{}) error {
return c.Event().Set(name, payload)
}