-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
367 lines (322 loc) · 10.9 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* Copyright 2017 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ------------------------------------------------------------------------------
*/
package processor
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/rberg2/sawtooth-go-sdk/messaging"
"github.com/rberg2/sawtooth-go-sdk/protobuf/events_pb2"
"github.com/rberg2/sawtooth-go-sdk/protobuf/state_context_pb2"
"github.com/rberg2/sawtooth-go-sdk/protobuf/validator_pb2"
)
// Context provides an abstract interface for getting and setting validator
// state. All validator interactions by a handler should be through a Context
// instance. Currently, the Context class is NOT thread-safe and Context classes
// may not share the same messaging.Connection object.
type Context struct {
connection messaging.Connection
contextId string
}
type Attribute struct {
Key string
Value string
}
// Construct a new context object given an initialized Stream and Context ID.
func NewContext(connection messaging.Connection, contextId string) *Context {
return &Context{
connection: connection,
contextId: contextId,
}
}
// GetState queries the validator state for data at each of the addresses in the
// given slice. A string->[]byte map is returned. If an address is not set,
// it will not exist in the map.
//
// results, err := context.Get(addresses)
// if err != nil {
// fmt.Println("Error getting data!")
// }
// data, ok := results[address]
// if !ok {
// fmt.Prinln("No data stored at address!")
// }
//
func (self *Context) GetState(addresses []string) (map[string][]byte, error) {
// Construct the message
request := &state_context_pb2.TpStateGetRequest{
ContextId: self.contextId,
Addresses: addresses,
}
bytes, err := proto.Marshal(request)
if err != nil {
return nil, fmt.Errorf("Failed to marshal TpStateGetRequest: %v", err)
}
// Send the message and get the response
corrId, err := self.connection.SendNewMsg(
validator_pb2.Message_TP_STATE_GET_REQUEST, bytes,
)
if err != nil {
return nil, fmt.Errorf("Failed to send TpStateGetRequest: %v", err)
}
_, msg, err := self.connection.RecvMsgWithId(corrId)
if msg.GetCorrelationId() != corrId {
return nil, fmt.Errorf(
"Expected message with correlation id %v but got %v",
corrId, msg.GetCorrelationId(),
)
}
if msg.GetMessageType() != validator_pb2.Message_TP_STATE_GET_RESPONSE {
return nil, fmt.Errorf(
"Expected TpStateGetResponse but got %v", msg.GetMessageType(),
)
}
// Parse the result
response := &state_context_pb2.TpStateGetResponse{}
err = proto.Unmarshal(msg.GetContent(), response)
if err != nil {
return nil, fmt.Errorf("Failed to unmarshal TpStateGetResponse: %v", err)
}
// Use a switch in case new Status values are added
switch response.Status {
case state_context_pb2.TpStateGetResponse_AUTHORIZATION_ERROR:
return nil, &AuthorizationException{Msg: fmt.Sprint("Tried to get unauthorized address: ", addresses)}
}
// Construct and return a map
results := make(map[string][]byte)
for _, entry := range response.GetEntries() {
if len(entry.GetData()) != 0 {
results[entry.GetAddress()] = entry.GetData()
}
}
return results, nil
}
// SetState requests that each address in the validator state be set to the given
// value. A slice of addresses set is returned or an error if there was a
// problem setting the addresses. For example:
//
// responses, err := context.Set(dataMap)
// if err != nil {
// fmt.Println("Error setting addresses!")
// }
// set, ok := results[address]
// if !ok {
// fmt.Prinln("Address was not set!")
// }
//
func (self *Context) SetState(pairs map[string][]byte) ([]string, error) {
// Construct the message
entries := make([]*state_context_pb2.TpStateEntry, 0, len(pairs))
for address, data := range pairs {
entries = append(entries, &state_context_pb2.TpStateEntry{
Address: address,
Data: data,
})
}
request := &state_context_pb2.TpStateSetRequest{
ContextId: self.contextId,
Entries: entries,
}
bytes, err := proto.Marshal(request)
if err != nil {
return nil, fmt.Errorf("Failed to marshal: %v", err)
}
// Send the message and get the response
corrId, err := self.connection.SendNewMsg(
validator_pb2.Message_TP_STATE_SET_REQUEST, bytes,
)
if err != nil {
return nil, fmt.Errorf("Failed to send set: %v", err)
}
_, msg, err := self.connection.RecvMsgWithId(corrId)
if msg.GetCorrelationId() != corrId {
return nil, fmt.Errorf(
"Expected message with correlation id %v but got %v",
corrId, msg.GetCorrelationId(),
)
}
if msg.GetMessageType() != validator_pb2.Message_TP_STATE_SET_RESPONSE {
return nil, fmt.Errorf(
"Expected TP_STATE_SET_RESPONSE but got %v", msg.GetMessageType(),
)
}
// Parse the result
response := &state_context_pb2.TpStateSetResponse{}
err = proto.Unmarshal(msg.Content, response)
if err != nil {
return nil, fmt.Errorf("Failed to unmarshal TpStateSetResponse: %v", err)
}
// Use a switch in case new Status values are added
switch response.GetStatus() {
case state_context_pb2.TpStateSetResponse_AUTHORIZATION_ERROR:
addresses := make([]string, 0, len(pairs))
for a, _ := range pairs {
addresses = append(addresses, a)
}
return nil, &AuthorizationException{Msg: fmt.Sprint("Tried to set unauthorized address: ", addresses)}
}
return response.GetAddresses(), nil
}
// DeleteState requests that each address in the validator state be
// deleted from state. A slice of addresses deleted is returned or an
// error if there was a problem deleting the addresses. For example:
//
// responses, err := context.DeleteState(dataMap)
// if err != nil {
// fmt.Println("Error deleting addresses!")
// }
// delete, ok := results[address]
// if !ok {
// fmt.Prinln("Address was not deleted!")
// }
//
func (self *Context) DeleteState(addresses []string) ([]string, error) {
// Construct the message
request := &state_context_pb2.TpStateDeleteRequest{
ContextId: self.contextId,
Addresses: addresses,
}
bytes, err := proto.Marshal(request)
if err != nil {
return nil, fmt.Errorf("Failed to marshal TpStateDeleteRequest: %v", err)
}
// Send the message and get the response
corrId, err := self.connection.SendNewMsg(
validator_pb2.Message_TP_STATE_DELETE_REQUEST, bytes,
)
if err != nil {
return nil, fmt.Errorf("Failed to send TpStateDeleteRequest: %v", err)
}
_, msg, err := self.connection.RecvMsgWithId(corrId)
if msg.GetCorrelationId() != corrId {
return nil, fmt.Errorf(
"Expected message with correlation id %v but got %v",
corrId, msg.GetCorrelationId(),
)
}
if msg.GetMessageType() != validator_pb2.Message_TP_STATE_DELETE_RESPONSE {
return nil, fmt.Errorf(
"Expected TpStateDeleteResponse but got %v", msg.GetMessageType(),
)
}
// Parse the result
response := &state_context_pb2.TpStateDeleteResponse{}
err = proto.Unmarshal(msg.GetContent(), response)
if err != nil {
return nil, fmt.Errorf("Failed to unmarshal TpStatedDeleteResponse: %v", err)
}
// Use a switch in case new Status values are added
switch response.Status {
case state_context_pb2.TpStateDeleteResponse_AUTHORIZATION_ERROR:
return nil, &AuthorizationException{Msg: fmt.Sprint("Tried to get unauthorized address: ", addresses)}
}
return response.GetAddresses(), nil
}
// Add a blob to the execution result for this transaction.
func (self *Context) AddReceiptData(data []byte) error {
// Append the data to the transaction receipt and set the type
request := &state_context_pb2.TpReceiptAddDataRequest{
ContextId: self.contextId,
Data: data,
}
bytes, err := proto.Marshal(request)
if err != nil {
return fmt.Errorf("Failed to marshal: %v", err)
}
// Send the message and get the response
corrId, err := self.connection.SendNewMsg(
validator_pb2.Message_TP_RECEIPT_ADD_DATA_REQUEST, bytes,
)
if err != nil {
return fmt.Errorf("Failed to add receipt data: %v", err)
}
_, msg, err := self.connection.RecvMsgWithId(corrId)
if msg.GetCorrelationId() != corrId {
return fmt.Errorf(
"Expected message with correlation id %v but got %v",
corrId, msg.GetCorrelationId(),
)
}
if msg.GetMessageType() != validator_pb2.Message_TP_RECEIPT_ADD_DATA_RESPONSE {
return fmt.Errorf(
"Expected TP_RECEIPT_ADD_DATA_RESPONSE but got %v", msg.GetMessageType(),
)
}
// Parse the result
response := &state_context_pb2.TpReceiptAddDataResponse{}
err = proto.Unmarshal(msg.Content, response)
if err != nil {
return fmt.Errorf("Failed to unmarshal TpReceiptAddDataResponse: %v", err)
}
// Use a switch in case new Status values are added
switch response.GetStatus() {
case state_context_pb2.TpReceiptAddDataResponse_ERROR:
return fmt.Errorf("Failed to add receipt data")
}
return nil
}
// Add a new event to the execution result for this transaction.
func (self *Context) AddEvent(event_type string, attributes []Attribute, event_data []byte) error {
event_attributes := make([]*events_pb2.Event_Attribute, 0, len(attributes))
for _, attribute := range attributes {
event_attributes = append(event_attributes, &events_pb2.Event_Attribute{attribute.Key, attribute.Value})
}
event := &events_pb2.Event{
EventType: event_type,
Attributes: event_attributes,
Data: event_data,
}
// Construct message
request := &state_context_pb2.TpEventAddRequest{
ContextId: self.contextId,
Event: event,
}
bytes, err := proto.Marshal(request)
if err != nil {
return fmt.Errorf("Failed to marshal: %v", err)
}
// Send the message and get the response
corrId, err := self.connection.SendNewMsg(
validator_pb2.Message_TP_EVENT_ADD_REQUEST, bytes,
)
if err != nil {
return fmt.Errorf("Failed to add event: %v", err)
}
_, msg, err := self.connection.RecvMsgWithId(corrId)
if msg.GetCorrelationId() != corrId {
return fmt.Errorf(
"Expected message with correlation id %v but got %v",
corrId, msg.GetCorrelationId(),
)
}
if msg.GetMessageType() != validator_pb2.Message_TP_EVENT_ADD_RESPONSE {
return fmt.Errorf(
"Expected TP_EVENT_ADD_RESPONSE but got %v", msg.GetMessageType(),
)
}
// Parse the result
response := &state_context_pb2.TpEventAddResponse{}
err = proto.Unmarshal(msg.Content, response)
if err != nil {
return fmt.Errorf("Failed to unmarshal TpEventAddResponse: %v", err)
}
// Use a switch in case new Status values are added
switch response.GetStatus() {
case state_context_pb2.TpEventAddResponse_ERROR:
return fmt.Errorf("Failed to add event")
}
return nil
}