-
Notifications
You must be signed in to change notification settings - Fork 5
/
calls.rs
167 lines (149 loc) · 4.82 KB
/
calls.rs
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
use crate::{calls::Remoting, errors::Result, prelude::*};
use core::future::Future;
use futures::FutureExt;
use gstd::{msg, prog};
#[derive(Default)]
pub struct GStdArgs {
wait_up_to: Option<BlockCount>,
#[cfg(not(feature = "ethexe"))]
reply_deposit: Option<GasUnit>,
#[cfg(not(feature = "ethexe"))]
reply_hook: Option<Box<dyn FnOnce() + Send + 'static>>,
}
#[cfg(not(feature = "ethexe"))]
impl GStdArgs {
pub fn with_wait_up_to(mut self, block_count: Option<BlockCount>) -> Self {
self.wait_up_to = block_count;
self
}
pub fn with_reply_deposit(mut self, reply_deposit: Option<GasUnit>) -> Self {
self.reply_deposit = reply_deposit;
self
}
pub fn with_reply_hook<F: FnOnce() + Send + 'static>(mut self, f: F) -> Self {
self.reply_hook = Some(Box::new(f));
self
}
pub fn wait_up_to(&self) -> Option<BlockCount> {
self.wait_up_to
}
pub fn reply_deposit(&self) -> Option<GasUnit> {
self.reply_deposit
}
}
#[derive(Debug, Default, Clone)]
pub struct GStdRemoting;
impl GStdRemoting {
fn send_for_reply(
target: ActorId,
payload: impl AsRef<[u8]>,
#[cfg(not(feature = "ethexe"))] gas_limit: Option<GasUnit>,
value: ValueUnit,
#[allow(unused_variables)] args: GStdArgs,
) -> Result<msg::MessageFuture, crate::errors::Error> {
#[cfg(not(feature = "ethexe"))]
let mut message_future = if let Some(gas_limit) = gas_limit {
msg::send_bytes_with_gas_for_reply(
target,
payload,
gas_limit,
value,
args.reply_deposit.unwrap_or_default(),
)?
} else {
msg::send_bytes_for_reply(
target,
payload,
value,
args.reply_deposit.unwrap_or_default(),
)?
};
#[cfg(feature = "ethexe")]
let mut message_future = msg::send_bytes_for_reply(target, payload, value)?;
message_future = message_future.up_to(args.wait_up_to)?;
#[cfg(not(feature = "ethexe"))]
if let Some(reply_hook) = args.reply_hook {
return Ok(message_future.handle_reply(reply_hook)?);
}
Ok(message_future)
}
}
impl Remoting for GStdRemoting {
type Args = GStdArgs;
async fn activate(
self,
code_id: CodeId,
salt: impl AsRef<[u8]>,
payload: impl AsRef<[u8]>,
#[cfg(not(feature = "ethexe"))] gas_limit: Option<GasUnit>,
value: ValueUnit,
#[allow(unused_variables)] args: GStdArgs,
) -> Result<impl Future<Output = Result<(ActorId, Vec<u8>)>>> {
#[cfg(not(feature = "ethexe"))]
let mut reply_future = if let Some(gas_limit) = gas_limit {
prog::create_program_bytes_with_gas_for_reply(
code_id,
salt,
payload,
gas_limit,
value,
args.reply_deposit.unwrap_or_default(),
)?
} else {
prog::create_program_bytes_for_reply(
code_id,
salt,
payload,
value,
args.reply_deposit.unwrap_or_default(),
)?
};
#[cfg(feature = "ethexe")]
let mut reply_future = prog::create_program_bytes_for_reply(code_id, salt, payload, value)?;
reply_future = reply_future.up_to(args.wait_up_to)?;
#[cfg(not(feature = "ethexe"))]
if let Some(reply_hook) = args.reply_hook {
reply_future = reply_future.handle_reply(reply_hook)?;
}
let reply_future = reply_future.map(|result| result.map_err(Into::into));
Ok(reply_future)
}
async fn message(
self,
target: ActorId,
payload: impl AsRef<[u8]>,
#[cfg(not(feature = "ethexe"))] gas_limit: Option<GasUnit>,
value: ValueUnit,
args: GStdArgs,
) -> Result<impl Future<Output = Result<Vec<u8>>>> {
let reply_future = GStdRemoting::send_for_reply(
target,
payload,
#[cfg(not(feature = "ethexe"))]
gas_limit,
value,
args,
)?;
let reply_future = reply_future.map(|result| result.map_err(Into::into));
Ok(reply_future)
}
async fn query(
self,
target: ActorId,
payload: impl AsRef<[u8]>,
#[cfg(not(feature = "ethexe"))] gas_limit: Option<GasUnit>,
value: ValueUnit,
args: GStdArgs,
) -> Result<Vec<u8>> {
let reply_future = GStdRemoting::send_for_reply(
target,
payload,
#[cfg(not(feature = "ethexe"))]
gas_limit,
value,
args,
)?;
let reply = reply_future.await?;
Ok(reply)
}
}