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
use std::{
fmt::Display,
fs::File,
io::{Read, Seek},
time::Duration,
};
use either::Either;
use futures::future::BoxFuture;
use locutus_core::{ClientEventsProxy, ClientId, OpenRequest};
use locutus_runtime::prelude::*;
use locutus_stdlib::api::{ClientError, ClientRequest, ContractRequest, ErrorKind, HostResponse};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::{util, CommandSender, DynError};
use super::{state::AppState, DeserializationFmt, LocalNodeCliConfig};
const HELP: &str = "Locutus Contract Development Environment
SUBCOMMANDS:
help Print this message
get Gets the current value of the contract. It will be piped into the set output pipe (file, terminal, etc.)
update Attempts to update the contract and prints out the result of the operation
put Puts the state for the contract for the first time
exit Exit from the TUI";
type HostIncomingMsg = Result<OpenRequest<'static>, ClientError>;
pub(super) async fn user_fn_handler(
config: LocalNodeCliConfig,
command_sender: CommandSender,
app_state: AppState,
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let mut input = StdInput::new(config, app_state)?;
tracing::debug!("running... send a command or write \"help\" for help");
loop {
let command = input.recv().await;
let command = command?;
let dc = command.request.is_disconnect();
command_sender.send(command.request).await?;
if dc {
break;
}
}
Ok(())
}
struct StdInput {
config: LocalNodeCliConfig,
contract: ContractContainer,
input: File,
buf: Vec<u8>,
app_state: AppState,
}
impl StdInput {
fn new(config: LocalNodeCliConfig, app_state: AppState) -> Result<Self, DynError> {
let params = config
.params
.as_ref()
.map(|p| {
let mut f = File::open(p)?;
let mut buf = vec![];
f.read_to_end(&mut buf)?;
Ok::<_, DynError>(buf)
})
.transpose()?
.unwrap_or_default();
let contract = ContractContainer::Wasm(WasmAPIVersion::V1(WrappedContract::try_from((
&*config.contract,
params.into(),
))?));
Ok(StdInput {
input: File::open(&config.input_file)?,
config,
contract,
buf: vec![],
app_state,
})
}
fn read_input<T>(&mut self) -> Result<T, DynError>
where
T: DeserializeOwned,
{
let mut buf = vec![];
self.input.read_to_end(&mut buf).unwrap();
util::deserialize(self.config.ser_format, &buf)
}
fn get_command_input<T>(&mut self, cmd: Command) -> Result<T, ClientError>
where
T: From<Vec<u8>>,
{
self.input.rewind().map_err(|e| ErrorKind::Unhandled {
cause: format!("{e}"),
})?;
match self.config.ser_format {
Some(DeserializationFmt::Json) => {
let state: serde_json::Value =
self.read_input().map_err(|e| ErrorKind::Unhandled {
cause: format!("deserialization error: {e}"),
})?;
let json_str =
serde_json::to_string_pretty(&state).map_err(|e| ErrorKind::Unhandled {
cause: format!("{e}"),
})?;
tracing::debug!("{cmd:?} value:\n{json_str}");
Ok(json_str.into_bytes().into())
}
#[cfg(feature = "messagepack")]
Some(DeserializationFmt::MessagePack) => {
let mut buf = vec![];
self.input.read_to_end(&mut buf).unwrap();
let state = rmpv::decode::read_value_ref(&mut buf.as_ref()).map_err(|e| {
ErrorKind::Unhandled {
cause: format!("deserialization error: {e}"),
}
})?;
tracing::debug!("{cmd:?} value:\n{state}");
Ok(buf.into())
}
_ => {
let state: Vec<u8> = self.read_input().map_err(|e| ErrorKind::Unhandled {
cause: format!("deserialization error: {e}"),
})?;
Ok(state.into())
}
}
}
}
#[derive(Serialize, Deserialize)]
pub(super) enum CommandInput<'a> {
Put {
#[serde(borrow)]
state: State<'a>,
},
Update {
#[serde(borrow)]
delta: StateDelta<'a>,
},
}
impl<'a> CommandInput<'a> {
fn unwrap_put(self) -> State<'a> {
match self {
Self::Put { state } => state,
_ => panic!("expected put"),
}
}
fn unwrap_delta(self) -> StateDelta<'a> {
match self {
Self::Update { delta } => delta,
_ => panic!("expected put"),
}
}
}
impl Display for CommandInput<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Put { .. } => {
write!(f, "Put")
}
Self::Update { .. } => {
write!(f, "Update")
}
}
}
}
#[derive(Debug)]
enum Command {
Put,
Get,
GetParams,
Update,
Help,
Exit,
}
impl TryFrom<&[u8]> for Command {
type Error = String;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let cmd = std::str::from_utf8(value).map_err(|e| format!("{e}"))?;
match cmd {
"put" => Ok(Command::Put),
"get" => Ok(Command::Get),
"get params" => Ok(Command::GetParams),
"update" => Ok(Command::Update),
"help" => Ok(Command::Help),
"exit" => Ok(Command::Exit),
v => Err(format!("unknown command: {v}")),
}
}
}
struct CommandInfo {
cmd: Command,
contract: ContractContainer,
input: Option<CommandInput<'static>>,
}
impl From<CommandInfo> for OpenRequest<'static> {
fn from(cmd: CommandInfo) -> Self {
let key = cmd.contract.key();
let req = match cmd.cmd {
Command::Get => ContractRequest::Get {
key,
fetch_contract: false,
}
.into(),
Command::Put => {
let state = cmd.input.unwrap().unwrap_put();
ContractRequest::Put {
contract: cmd.contract,
state: WrappedState::new(state.into_bytes()),
related_contracts: Default::default(),
}
.into()
}
Command::Update => {
let data = cmd.input.unwrap().unwrap_delta().into();
ContractRequest::Update { key, data }.into()
}
Command::Exit => ClientRequest::Disconnect {
cause: Some("shutdown".to_owned()),
},
_ => unreachable!(),
};
OpenRequest::new(ClientId::new(0), req)
}
}
impl ClientEventsProxy for StdInput {
fn recv(&mut self) -> BoxFuture<'_, HostIncomingMsg> {
Box::pin(async {
loop {
self.buf.clear();
let f = async {
let stdin = std::io::stdin();
'stdin: for b in stdin.bytes() {
let b = b.map_err(|_| {
ClientError::from(ErrorKind::TransportProtocolDisconnect)
})?;
if b == b'\n' {
break 'stdin;
}
self.buf.push(b);
}
if String::from_utf8_lossy(&self.buf[..]).trim().is_empty() {
return Ok(Either::Right(()));
}
match Command::try_from(&self.buf[..]) {
Ok(Command::Help) => {
tracing::debug!("{HELP}");
}
Ok(cmd @ Command::Put) => {
let state: State = match self.get_command_input(Command::Put) {
Ok(v) => v,
Err(e) => {
tracing::debug!("Put event error: {e}");
return Ok(Either::Right(()));
}
};
return Ok(Either::Left(
CommandInfo {
cmd,
contract: self.contract.clone(),
input: Some(CommandInput::Put { state }),
}
.into(),
));
}
Ok(cmd @ Command::Update) => {
let delta: StateDelta = match self.get_command_input(Command::Update) {
Ok(v) => v,
Err(e) => {
tracing::debug!("Update event error: {e}");
return Ok(Either::Right(()));
}
};
return Ok(Either::Left(
CommandInfo {
cmd,
contract: self.contract.clone(),
input: Some(CommandInput::Update { delta }),
}
.into(),
));
}
Ok(Command::GetParams) => {
let _node = &*self.app_state.local_node.read().await;
}
Ok(cmd) => {
return Ok(Either::Left(
CommandInfo {
cmd,
contract: self.contract.clone(),
input: None,
}
.into(),
));
}
Err(err) => {
tracing::debug!("{err}");
}
}
tokio::time::sleep(Duration::from_millis(10)).await;
Ok(Either::<OpenRequest, _>::Right(()))
};
match f.await {
Ok(Either::Right(_)) => continue,
Ok(Either::Left(r)) => return Ok(r),
Err(err) => return Err(err),
}
}
})
}
fn send(
&mut self,
_id: ClientId,
_response: Result<HostResponse, ClientError>,
) -> BoxFuture<'_, Result<(), ClientError>> {
unimplemented!()
}
}
impl Clone for StdInput {
fn clone(&self) -> Self {
Self {
config: self.config.clone(),
contract: self.contract.clone(),
buf: Vec::new(),
input: File::open(&self.config.input_file).unwrap(),
app_state: self.app_state.clone(),
}
}
}