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
use anyhow as failure;
use exonum::runtime::{AnyTx, CoreError, ExecutionContext, ExecutionError, InstanceId};
use exonum_derive::*;
use exonum_proto::ProtobufConvert;
use exonum_rust_runtime::{FallthroughAuth, GenericCall, GenericCallMut, MethodDescriptor, TxStub};
use semver::VersionReq;
use serde_derive::*;
use crate::{proto, ArtifactReq, MiddlewareService};
#[derive(Debug, Clone, Copy, ExecutionFail)]
pub enum Error {
ArtifactMismatch = 0,
VersionMismatch = 1,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(BinaryValue, ProtobufConvert)]
#[protobuf_convert(source = "proto::CheckedCall")]
pub struct CheckedCall {
pub artifact_name: String,
#[protobuf_convert(with = "self::pb_version_req")]
pub artifact_version: VersionReq,
pub inner: AnyTx,
}
mod pb_version_req {
use super::*;
#[allow(clippy::needless_pass_by_value)]
pub fn from_pb(pb: String) -> anyhow::Result<VersionReq> {
pb.parse().map_err(From::from)
}
pub fn to_pb(value: &VersionReq) -> String {
value.to_string()
}
}
impl GenericCall<InstanceId> for ArtifactReq {
type Output = CheckedCall;
fn generic_call(
&self,
instance_id: InstanceId,
method: MethodDescriptor<'_>,
args: Vec<u8>,
) -> Self::Output {
CheckedCall {
artifact_name: self.0.name.clone(),
artifact_version: self.0.version.clone(),
inner: TxStub.generic_call(instance_id, method, args),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[derive(ProtobufConvert, BinaryValue)]
#[protobuf_convert(source = "proto::Batch")]
pub struct Batch {
pub inner: Vec<AnyTx>,
}
impl Batch {
pub fn new() -> Self {
Self::default()
}
pub fn with_call(mut self, call: AnyTx) -> Self {
self.inner.push(call);
self
}
}
impl GenericCallMut<InstanceId> for Batch {
type Output = ();
fn generic_call_mut(
&mut self,
instance_id: InstanceId,
method: MethodDescriptor<'_>,
args: Vec<u8>,
) -> Self::Output {
self.inner
.push(TxStub.generic_call(instance_id, method, args));
}
}
#[exonum_interface]
pub trait MiddlewareInterface<Ctx> {
type Output;
#[interface_method(id = 0)]
fn checked_call(&self, context: Ctx, arg: CheckedCall) -> Self::Output;
#[interface_method(id = 1)]
fn batch(&self, context: Ctx, arg: Batch) -> Self::Output;
}
impl MiddlewareInterface<ExecutionContext<'_>> for MiddlewareService {
type Output = Result<(), ExecutionError>;
fn checked_call(&self, context: ExecutionContext<'_>, arg: CheckedCall) -> Self::Output {
let instance_id = arg.inner.call_info.instance_id;
let dispatcher_schema = context.data().for_dispatcher();
let state = dispatcher_schema
.get_instance(instance_id)
.ok_or(CoreError::IncorrectInstanceId)?;
let artifact = &state.spec.artifact;
if arg.artifact_name != artifact.name {
return Err(Error::ArtifactMismatch.into());
}
if !arg.artifact_version.matches(&artifact.version) {
return Err(Error::VersionMismatch.into());
}
let method = MethodDescriptor::inherent(arg.inner.call_info.method_id);
FallthroughAuth(context).generic_call_mut(instance_id, method, arg.inner.arguments)
}
fn batch(&self, context: ExecutionContext<'_>, arg: Batch) -> Self::Output {
let mut fallthrough_auth = FallthroughAuth(context);
for call in arg.inner {
let method = MethodDescriptor::inherent(call.call_info.method_id);
fallthrough_auth.generic_call_mut(
call.call_info.instance_id,
method,
call.arguments,
)?;
}
Ok(())
}
}
#[test]
fn checked_call_in_json() {
use exonum::runtime::CallInfo;
use serde_json::json;
let mut checked_call = CheckedCall {
artifact_name: "test-artifact".to_string(),
artifact_version: "^1.0.0".parse().unwrap(),
inner: AnyTx::new(CallInfo::new(100, 0), vec![]),
};
assert_eq!(
serde_json::to_value(&checked_call).unwrap(),
json!({
"artifact_name": "test-artifact",
"artifact_version": "^1.0.0",
"inner": checked_call.inner,
})
);
checked_call.artifact_version = ">=0.9, <2".parse().unwrap();
assert_eq!(
serde_json::to_value(&checked_call).unwrap(),
json!({
"artifact_name": "test-artifact",
"artifact_version": ">= 0.9, < 2",
"inner": checked_call.inner,
})
);
}