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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#![deny(missing_docs)]
//! # Server
//!
//! A simple REST server for the Essential platform.

use anyhow::anyhow;
use axum::{
    extract::{Path, Query, State},
    response::{
        sse::{Event, KeepAlive},
        IntoResponse, Sse,
    },
    routing::{get, post},
    Json, Router,
};
use essential_server::{CheckSolutionOutput, Essential, SolutionOutcome, StateRead, Storage};
use essential_server_types::{CheckSolution, QueryStateReads, QueryStateReadsOutput};
use essential_types::{
    contract::{Contract, SignedContract},
    convert::word_from_bytes,
    predicate::Predicate,
    solution::Solution,
    Block, ContentAddress, PredicateAddress, Word,
};
use futures::{Stream, StreamExt};
use hyper::body::Incoming;
use hyper_util::rt::{TokioExecutor, TokioIo};
use serde::Deserialize;
use std::{net::SocketAddr, time::Duration};
use tokio::{
    net::{TcpListener, ToSocketAddrs},
    sync::oneshot,
    task::JoinSet,
};
use tower::Service;
use tower_http::cors::CorsLayer;

const MAX_CONNECTIONS: usize = 2000;

#[derive(Debug, Clone)]
/// Server configuration.
pub struct Config {
    /// Whether the rest server should build blocks
    /// or just serve requests.
    /// Default is `true`.
    pub build_blocks: bool,
    /// Essential server configuration.
    pub server_config: essential_server::Config,
}

#[derive(Deserialize)]
/// Type to deserialize a time range query parameters.
struct TimeRange {
    /// Start of the time range in seconds.
    start: u64,
    /// End of the time range in seconds.
    end: u64,
}

#[derive(Deserialize)]
/// Type to deserialize a time query parameters.
struct Time {
    /// Time in seconds.
    time: u64,
}

#[derive(Deserialize)]
/// Type to deserialize a page query parameter.
struct Page {
    /// The page number to start from.
    page: u64,
}

#[derive(Deserialize)]
/// Type to deserialize a block number query parameter.
struct BlockNumber {
    /// The block number to start from.
    block: u64,
}

/// Run the server.
///
/// - Takes the essential library to run it.
/// - Address to bind to.
/// - A channel that returns the actual chosen local address.
/// - An optional channel that can be used to shutdown the server.
pub async fn run<S, A>(
    essential: Essential<S>,
    addr: A,
    local_addr: oneshot::Sender<SocketAddr>,
    shutdown_rx: Option<oneshot::Receiver<()>>,
    config: Config,
) -> anyhow::Result<()>
where
    A: ToSocketAddrs,
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    // Spawn essential and get the handle.
    let handle = if config.build_blocks {
        Some(essential.clone().spawn(config.server_config)?)
    } else {
        None
    };

    let cors = CorsLayer::new()
        .allow_origin(tower_http::cors::Any)
        .allow_methods([http::Method::GET, http::Method::POST, http::Method::OPTIONS])
        .allow_headers([http::header::CONTENT_TYPE]);

    // Create all the endpoints.
    let app = Router::new()
        .route("/", get(health_check))
        .route("/deploy-contract", post(deploy_contract))
        .route("/get-contract/:address", get(get_contract))
        .route("/get-predicate/:contract/:address", get(get_predicate))
        .route("/list-contracts", get(list_contracts))
        .route("/subscribe-contracts", get(subscribe_contracts))
        .route("/submit-solution", post(submit_solution))
        .route("/list-solutions-pool", get(list_solutions_pool))
        .route("/query-state/:address/:key", get(query_state))
        .route("/list-blocks", get(list_blocks))
        .route("/subscribe-blocks", get(subscribe_blocks))
        .route("/solution-outcome/:hash", get(solution_outcome))
        .route("/check-solution", post(check_solution))
        .route(
            "/check-solution-with-contracts",
            post(check_solution_with_contracts),
        )
        .route("/query-state-reads", post(query_state_reads))
        .layer(cors)
        .with_state(essential.clone());

    // Bind to the address.
    let listener = TcpListener::bind(addr).await?;

    // Send the local address to the caller.
    // This is useful when the address or port is chosen by the OS.
    let addr = listener.local_addr()?;
    local_addr
        .send(addr)
        .map_err(|_| anyhow::anyhow!("Failed to send local address"))?;

    // Serve the app.
    serve(app, listener, shutdown_rx).await;

    // After the server is done, shutdown essential.
    if let Some(handle) = handle {
        handle.shutdown().await?;
    }

    Ok(())
}

async fn serve(app: Router, listener: TcpListener, shutdown_rx: Option<oneshot::Receiver<()>>) {
    let shut = shutdown(shutdown_rx);
    tokio::pin!(shut);

    let mut conn_contract = JoinSet::new();
    // Continuously accept new connections up to max connections.
    loop {
        // Accept a new connection or wait for a shutdown signal.
        let (socket, remote_addr) = tokio::select! {
            _ = &mut shut => {
                break;
            }
            v = listener.accept() => {
                match v {
                    Ok(v) => v,
                    Err(err) => {
                        #[cfg(feature = "tracing")]
                        tracing::trace!("Failed to accept connection {}", err);
                        continue;
                    }
                }
            }
        };

        #[cfg(feature = "tracing")]
        tracing::trace!("Accepted new connection from: {}", remote_addr);

        // We don't need to call `poll_ready` because `Router` is always ready.
        let tower_service = app.clone();

        // Spawn a task to handle the connection. That way we can handle multiple connections
        // concurrently.

        conn_contract.spawn(async move {
            // Hyper has its own `AsyncRead` and `AsyncWrite` traits and doesn't use tokio.
            // `TokioIo` converts between them.
            let socket = TokioIo::new(socket);

            // Hyper also has its own `Service` trait and doesn't use tower. We can use
            // `hyper::service::service_fn` to create a hyper `Service` that calls our app through
            // `tower::Service::call`.
            let hyper_service =
                hyper::service::service_fn(move |request: axum::extract::Request<Incoming>| {
                    // We have to clone `tower_service` because hyper's `Service` uses `&self` whereas
                    // tower's `Service` requires `&mut self`.
                    //
                    // We don't need to call `poll_ready` since `Router` is always ready.
                    tower_service.clone().call(request)
                });

            // `TokioExecutor` tells hyper to use `tokio::spawn` to spawn tasks.
            let conn =
                hyper_util::server::conn::auto::Builder::new(TokioExecutor::new()).http2_only();
            let conn = conn.serve_connection(socket, hyper_service);
            let _ = conn.await;
        });

        // Wait for existing connection to close or wait for a shutdown signal.
        if conn_contract.len() > MAX_CONNECTIONS {
            #[cfg(feature = "tracing")]
            tracing::info!("Max number of connections reached: {}", MAX_CONNECTIONS);
            tokio::select! {
                _ = &mut shut => {
                    break;
                }
                _ = conn_contract.join_next() => {},

            }
        }
    }
}

/// The return a health check response.
async fn health_check() {}

/// The deploy contract post endpoint.
///
/// Takes a signed vector of contract as a json payload.
async fn deploy_contract<S>(
    State(essential): State<Essential<S>>,
    Json(payload): Json<SignedContract>,
) -> Result<Json<ContentAddress>, Error>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let address = essential.deploy_contract(payload).await?;
    Ok(Json(address))
}

/// The submit solution post endpoint.
///
/// Takes a signed solution as a json payload.
async fn submit_solution<S>(
    State(essential): State<Essential<S>>,
    Json(payload): Json<Solution>,
) -> Result<Json<ContentAddress>, Error>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let hash = essential.submit_solution(payload).await?;
    Ok(Json(hash))
}

/// The get contract get endpoint.
///
/// Takes a content address (encoded as hex) as a path parameter.
async fn get_contract<S>(
    State(essential): State<Essential<S>>,
    Path(address): Path<String>,
) -> Result<Json<Option<SignedContract>>, Error>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let address: ContentAddress = address
        .parse()
        .map_err(|e| anyhow!("failed to parse contract content address: {e}"))?;
    let contract = essential.get_contract(&address).await?;
    Ok(Json(contract))
}

/// The get predicate get endpoint.
///
/// Takes a contract content address and a predicate content address as path parameters.
/// Both are encoded as hex.
async fn get_predicate<S>(
    State(essential): State<Essential<S>>,
    Path((contract, address)): Path<(String, String)>,
) -> Result<Json<Option<Predicate>>, Error>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let contract: ContentAddress = contract
        .parse()
        .map_err(|e| anyhow!("failed to parse contract content address: {e}"))?;
    let predicate: ContentAddress = address
        .parse()
        .map_err(|e| anyhow!("failed to parse predicate content address: {e}"))?;
    let predicate = essential
        .get_predicate(&PredicateAddress {
            contract,
            predicate,
        })
        .await?;
    Ok(Json(predicate))
}

/// The list contracts get endpoint.
///
/// Takes optional time range and page as query parameters.
async fn list_contracts<S>(
    State(essential): State<Essential<S>>,
    time_range: Option<Query<TimeRange>>,
    page: Option<Query<Page>>,
) -> Result<Json<Vec<Contract>>, Error>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let time_range =
        time_range.map(|range| Duration::from_secs(range.start)..Duration::from_secs(range.end));

    let contracts = essential
        .list_contracts(time_range, page.map(|p| p.page as usize))
        .await?;
    Ok(Json(contracts))
}

/// The subscribe contracts get endpoint.
///
/// Takes optional time and page as query parameters.
async fn subscribe_contracts<S>(
    State(essential): State<Essential<S>>,
    time: Option<Query<Time>>,
    page: Option<Query<Page>>,
) -> Sse<impl Stream<Item = Result<Event, StdError>>>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let time = time.map(|t| Duration::from_secs(t.time));

    let contracts = essential.subscribe_contracts(time, page.map(|p| p.page as usize));
    Sse::new(
        contracts
            .map::<Result<_, Error>, _>(|contract| Ok(Event::default().json_data(contract?)?))
            .map(|r| r.map_err(StdError)),
    )
    .keep_alive(KeepAlive::default())
}

/// The list blocks get endpoint.
///
/// Takes optional time range and page as query parameters.
async fn list_blocks<S>(
    State(essential): State<Essential<S>>,
    time_range: Option<Query<TimeRange>>,
    block: Option<Query<BlockNumber>>,
    page: Option<Query<Page>>,
) -> Result<Json<Vec<Block>>, Error>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let time_range =
        time_range.map(|range| Duration::from_secs(range.start)..Duration::from_secs(range.end));

    let blocks = essential
        .list_blocks(
            time_range,
            block.map(|b| b.block),
            page.map(|p| p.page as usize),
        )
        .await?;
    Ok(Json(blocks))
}

/// The subscribe blocks get endpoint.
///
/// Takes optional time and page as query parameters.
async fn subscribe_blocks<S>(
    State(essential): State<Essential<S>>,
    time: Option<Query<Time>>,
    block: Option<Query<BlockNumber>>,
    page: Option<Query<Page>>,
) -> Sse<impl Stream<Item = Result<Event, StdError>>>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let time = time.map(|time| Duration::from_secs(time.time));

    let blocks =
        essential.subscribe_blocks(time, block.map(|b| b.block), page.map(|p| p.page as usize));
    Sse::new(
        blocks
            .map::<Result<_, Error>, _>(|block| Ok(Event::default().json_data(block?)?))
            .map(|r| r.map_err(StdError)),
    )
    .keep_alive(KeepAlive::default())
}

/// The list solutions pool get endpoint.
async fn list_solutions_pool<S>(
    State(essential): State<Essential<S>>,
    page: Option<Query<Page>>,
) -> Result<Json<Vec<Solution>>, Error>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let solutions = essential
        .list_solutions_pool(page.map(|p| p.page as usize))
        .await?;
    Ok(Json(solutions))
}

/// The query state get endpoint.
///
/// Takes a content address and a byte array key as path parameters.
/// Both are encoded as hex.
async fn query_state<S>(
    State(essential): State<Essential<S>>,
    Path((address, key)): Path<(String, String)>,
) -> Result<Json<Vec<Word>>, Error>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let address: ContentAddress = address
        .parse()
        .map_err(|e| anyhow!("failed to parse contract content address: {e}"))?;
    let key: Vec<u8> = hex::decode(key).map_err(|e| anyhow!("failed to decode key: {e}"))?;

    // Convert the key to words.
    let key = key
        .chunks_exact(8)
        .map(|chunk| word_from_bytes(chunk.try_into().expect("Safe due to chunk size")))
        .collect::<Vec<_>>();

    let state = essential.query_state(&address, &key).await?;
    Ok(Json(state))
}

/// The solution outcome get endpoint.
///
/// Takes a solution content address as a path parameter encoded hex.
async fn solution_outcome<S>(
    State(essential): State<Essential<S>>,
    Path(address): Path<String>,
) -> Result<Json<Vec<SolutionOutcome>>, Error>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let address: ContentAddress = address
        .parse()
        .map_err(|e| anyhow!("failed to parse solution content address: {e}"))?;
    let outcome = essential.solution_outcome(&address.0).await?;
    Ok(Json(outcome))
}

/// The check solution post endpoint.
///
/// Takes a signed solution as a json payload.
async fn check_solution<S>(
    State(essential): State<Essential<S>>,
    Json(payload): Json<Solution>,
) -> Result<Json<CheckSolutionOutput>, Error>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let outcome = essential.check_solution(payload).await?;
    Ok(Json(outcome))
}

/// The check solution with data post endpoint.
///
/// Takes a signed solution and a list of contract as a json payload.
async fn check_solution_with_contracts<S>(
    State(essential): State<Essential<S>>,
    Json(payload): Json<CheckSolution>,
) -> Result<Json<CheckSolutionOutput>, Error>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let outcome = essential
        .check_solution_with_contracts(payload.solution, payload.contracts)
        .await?;
    Ok(Json(outcome))
}

/// The query state reads post endpoint.
///
/// Takes a json state read query and returns the outcome
async fn query_state_reads<S>(
    State(essential): State<Essential<S>>,
    Json(payload): Json<QueryStateReads>,
) -> Result<Json<QueryStateReadsOutput>, Error>
where
    S: Storage + StateRead + Clone + Send + Sync + 'static,
    <S as StateRead>::Future: Send,
    <S as StateRead>::Error: Send,
{
    let out = essential.query_state_reads(payload).await?;
    Ok(Json(out))
}

/// Shutdown the server manually or on ctrl-c.
async fn shutdown(rx: Option<oneshot::Receiver<()>>) {
    // The manual signal is used to shutdown the server.
    let manual = async {
        match rx {
            Some(rx) => {
                rx.await.ok();
            }
            None => futures::future::pending().await,
        }
    };

    // The ctrl-c signal is used to shutdown the server.
    let ctrl_c = async {
        tokio::signal::ctrl_c()
            .await
            .expect("Failed to listen for ctrl-c");
    };

    // Wait for either signal.
    tokio::select! {
        _ = manual => {},
        _ = ctrl_c => {},
    }
}

#[derive(Debug)]
struct Error(anyhow::Error);

#[derive(Debug)]
struct StdError(Error);

impl IntoResponse for Error {
    fn into_response(self) -> axum::response::Response {
        // Return an internal server error with the error message.
        (
            axum::http::StatusCode::INTERNAL_SERVER_ERROR,
            format!("{}", self.0),
        )
            .into_response()
    }
}

impl<E> From<E> for Error
where
    E: Into<anyhow::Error>,
{
    fn from(err: E) -> Self {
        Self(err.into())
    }
}

impl From<Error> for StdError {
    fn from(err: Error) -> Self {
        Self(err)
    }
}

impl std::error::Error for StdError {}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl std::fmt::Display for StdError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            build_blocks: true,
            server_config: Default::default(),
        }
    }
}