borderless-runtime 0.5.0

Definition of the webassembly runtime for borderless smart-contracts and software-agents
Documentation
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
pub use super::*;
use crate::log_shim::*;
use crate::{db::controller::Controller, rt::agent::Runtime};
use borderless::events::{Message, Topic, TopicDto};
use borderless::{
    events::{CallAction, Events},
    http::queries::Pagination,
    AgentId, BorderlessId,
};
use borderless_kv_store::{backend::lmdb::Lmdb, Db};
use http::method::Method;
use serde_json::json;
use std::collections::VecDeque;
use std::convert::Infallible;
use std::future::Future;
use std::{
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
    time::Instant,
};
use tokio::sync::Mutex;

#[derive(Serialize)]
pub struct ActionResp {
    pub events: Events,
    pub action: CallAction,
}

pub trait EventHandler: Clone + Send + Sync {
    type Error: std::fmt::Display + Send + Sync;

    fn handle_events(&self, events: Events)
        -> impl Future<Output = Result<(), Self::Error>> + Send;
}

/// A dummy implementation of an event-handler, that does nothing with the events.
///
/// Useful for testing.
#[derive(Clone)]
pub struct NoEventHandler;

impl EventHandler for NoEventHandler {
    type Error = Infallible;

    async fn handle_events(&self, _events: Events) -> Result<(), Self::Error> {
        Ok(())
    }
}

/// A dummy implementation of an event-handler, that immediately applies all agent events
///
/// Discards all contract events in the process.
///
/// Useful for testing.
#[derive(Clone)]
pub struct RecursiveEventHandler<S: Db> {
    pub rt: Arc<Mutex<Runtime<S>>>,
}

impl<S: Db> EventHandler for RecursiveEventHandler<S> {
    type Error = crate::Error;

    async fn handle_events(&self, events: Events) -> Result<(), Self::Error> {
        let mut rt = self.rt.lock().await;
        let db = rt.get_db();
        let sub_handler = Controller::new(&db).messages();

        let mut agent_events: VecDeque<_> = events.local.into();
        // Handle events one by one
        while let Some(Message {
            publisher,
            topic,
            value,
        }) = agent_events.pop_front()
        {
            let subscribers = sub_handler.get_topic_subscribers(publisher, topic)?;

            for (subscriber, method) in subscribers {
                let action = CallAction::by_method(method, value.clone());
                // Queue all process events and apply them again
                if let Some(events) = rt.process_action(&subscriber, action).await? {
                    agent_events.extend(events.local);
                }
            }
        }
        Ok(())
    }
}

/// Simple service around the runtime
#[derive(Clone)]
pub struct SwAgentService<E, S = Lmdb>
where
    S: Db + 'static,
    E: EventHandler,
{
    rt: Arc<Mutex<Runtime<S>>>,
    db: S,
    // TODO: This is not optimal. The runtime is not tied to a tx-writer,
    // and for our multi-tenant contract-node we require this to be flexible.
    writer: BorderlessId,
    event_handler: E,
}

impl<S, E> SwAgentService<E, S>
where
    S: Db + 'static,
    E: EventHandler,
{
    pub fn new(db: S, rt: Runtime<S>, writer: BorderlessId, event_handler: E) -> Self {
        Self {
            rt: Arc::new(Mutex::new(rt)),
            db,
            writer,
            event_handler,
        }
    }

    pub fn with_shared(
        db: S,
        rt: Arc<Mutex<Runtime<S>>>,
        event_handler: E,
        writer: BorderlessId,
    ) -> Self {
        Self {
            rt,
            db,
            writer,
            event_handler,
        }
    }

    async fn process_rq(&self, req: Request) -> crate::Result<Response> {
        let start = Instant::now();
        let path = req.uri().path().to_string();
        let result = match *req.method() {
            Method::GET => self.process_get_rq(req).await,
            Method::POST => self.process_post_rq(req).await,
            _ => Ok(method_not_allowed()),
        };
        let elapsed = start.elapsed();
        // TODO: I don't know if this should be logged every time
        match &result {
            Ok(res) => info!(
                "Request success. path={path}. Time elapsed: {elapsed:?}, status={}",
                res.status()
            ),
            Err(e) => warn!("Request failed. path={path}. Time elapsed: {elapsed:?}, error={e}"),
        }
        result
    }

    async fn process_get_rq(&self, req: Request) -> crate::Result<Response> {
        let path = req.uri().path();
        let query = req.uri().query();

        if path == "/" {
            let agents = self.rt.lock().await.available_agents()?;
            return Ok(json_response(&agents));
        }

        let mut pieces = path.split('/').skip(1);

        // Extract agent-id from first piece
        let agent_id: AgentId = match pieces.next().and_then(|first| first.parse().ok()) {
            Some(aid) => aid,
            None => return Ok(reject_404()),
        };
        let controller = Controller::new(&self.db);

        // Ensure, that the agent exists
        if !controller.agent_exists(&agent_id)? {
            return Ok(reject_404());
        }

        // Get top-level route
        let route = match pieces.next() {
            Some(r) => r,
            None => {
                // Get full agent info
                let full_info = controller.agent_full(&agent_id)?;
                return Ok(json_response(&full_info));
            }
        };

        // Build truncated path
        let mut trunc = String::new();
        for piece in pieces {
            trunc.push('/');
            trunc.push_str(piece);
        }
        if trunc.is_empty() {
            trunc.push('/');
        }
        if let Some(query) = query {
            trunc.push('?');
            trunc.push_str(query);
        }
        match route {
            "state" => {
                // TODO: The agent should also parse query parameters !
                let mut rt = self.rt.lock().await;
                let (status, payload) = rt.http_get_state(&agent_id, trunc).await?;
                if status == 200 {
                    Ok(json_body(payload))
                } else {
                    Ok(reject_404())
                }
            }
            "logs" => {
                // Extract pagination
                let pagination = Pagination::from_query(query).unwrap_or_default();

                // Get logs
                let log = controller.logs(agent_id).get_logs_paginated(pagination)?;

                Ok(json_response(&log))
            }
            "sinks" => {
                let sinks = controller.agent_sinks(&agent_id)?;
                Ok(json_response(&sinks))
            }
            "subs" => {
                let subs = controller.agent_subs(&agent_id)?;
                Ok(json_response(&subs))
            }
            "desc" => {
                let desc = controller.agent_desc(&agent_id)?;
                Ok(json_response_nested(desc, &trunc))
            }
            "meta" => {
                let meta = controller.agent_meta(&agent_id)?;
                Ok(json_response_nested(meta, &trunc))
            }
            "symbols" => {
                let mut rt = self.rt.lock().await;
                let symbols = rt.get_symbols(&agent_id).await?;
                Ok(json_response_nested(symbols, &trunc))
            }
            "pkg" => match trunc.as_str() {
                "/" => {
                    let result = controller.agent_pkg_full(&agent_id)?.map(|r| r.into_dto());
                    Ok(json_response(&result))
                }
                "/def" => {
                    let result = controller.agent_pkg_def(&agent_id)?.map(|r| r.into_dto());
                    Ok(json_response(&result))
                }
                "/source" => {
                    let result = controller.agent_pkg_source(&agent_id)?;
                    Ok(json_response(&result))
                }
                _ => Ok(reject_404()),
            },
            // Same as empty path
            "" => {
                let full_info = controller.agent_full(&agent_id)?;
                Ok(json_response(&full_info))
            }
            _ => Ok(reject_404()),
        }
    }

    async fn process_post_rq(&self, req: Request) -> crate::Result<Response> {
        let path = req.uri().path();

        if path == "/" {
            return Ok(method_not_allowed());
        }

        let mut pieces = path.split('/').skip(1);

        // Extract agent-id from first piece
        let aid_str = match pieces.next() {
            Some(s) => s,
            None => return Ok(method_not_allowed()),
        };
        let agent_id: AgentId = match aid_str.parse() {
            Ok(aid) => aid,
            Err(e) => return Ok(bad_request(format!("failed to parse agent-id - {e}"))),
        };

        // Get top-level route
        let route = match pieces.next() {
            Some(r) => r,
            None => return Ok(reject_404()),
        };

        // Build truncated path
        let mut trunc = String::new();
        let mut cnt = 0;
        for piece in pieces {
            trunc.push('/');
            trunc.push_str(piece);
            cnt += 1;
        }
        // NOTE: The action route only has one additional path parameter
        if cnt > 1 {
            return Ok(reject_404());
        }
        if trunc.is_empty() {
            trunc.push('/');
        }
        if let Some(query) = req.uri().query() {
            trunc.push('?');
            trunc.push_str(query);
        }
        match route {
            "action" => {
                // Check request header
                let (parts, payload) = req.into_parts();
                if !check_json_content(&parts) {
                    return Ok(unsupported_media_type());
                }
                let (events, action) = {
                    let mut rt = self.rt.lock().await;
                    rt.set_executor(self.writer)?; // For agents the executor and the writer are actually the same
                    match rt
                        .http_post_action(&agent_id, trunc, payload.into(), &self.writer)
                        .await?
                    {
                        Ok(out) => out,
                        Err((status, err)) => {
                            return Ok(err_response(status.try_into().unwrap(), err))
                        }
                    }
                };
                // Forward the events
                match self.event_handler.handle_events(events.clone()).await {
                    Ok(_) => {
                        // Build action response
                        let resp = ActionResp { events, action };
                        Ok(json_response(&resp))
                    }
                    Err(e) => Ok(into_server_error(e)),
                }
            }
            "subscribe" => {
                // Check request header
                let (parts, payload) = req.into_parts();
                if !check_json_content(&parts) {
                    return Ok(unsupported_media_type());
                }
                // Extract Topic from request
                let payload: Vec<u8> = payload.into();
                let dto = match TopicDto::from_bytes(payload.as_slice()) {
                    Ok(dto) => dto,
                    Err(e) => return Ok(bad_request(format!("failed to parse topic - {e}"))),
                };
                let topic = Topic::from(dto);
                // Control that there are no newline characters in topic
                if !topic.validate() {
                    return Ok(bad_request("topic contains invalid characters".to_string()));
                }
                // Start subscription
                Controller::new(&self.db)
                    .messages()
                    .subscribe(agent_id, topic)
                    .expect("Handle error");
                Ok(json_response(&json!({"success": true})))
            }
            "unsubscribe" => {
                // Check request header
                let (parts, payload) = req.into_parts();
                if !check_json_content(&parts) {
                    return Ok(unsupported_media_type());
                }
                // Extract Topic from request
                let payload: Vec<u8> = payload.into();
                let dto = match TopicDto::from_bytes(payload.as_slice()) {
                    Ok(dto) => dto,
                    Err(e) => return Ok(bad_request(format!("failed to parse topic - {e}"))),
                };
                let topic = Topic::from(dto);
                // Stop subscription
                Controller::new(&self.db)
                    .messages()
                    .unsubscribe(agent_id, topic)
                    .expect("Handle error");
                Ok(json_response(&json!({"success": true})))
            }
            "" => Ok(method_not_allowed()),
            _ => Ok(reject_404()),
        }
    }
}

impl<E, S> Service<Request> for SwAgentService<E, S>
where
    S: Db + 'static,
    E: EventHandler + 'static,
{
    type Response = Response;
    type Error = Infallible;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: Request) -> Self::Future {
        let this = self.clone();
        let fut = async move {
            let result: Response = match this.process_rq(req).await {
                Ok(r) => r,
                Err(e) => into_server_error(e),
            };
            Ok(result)
        };
        Box::pin(fut)
    }
}