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
// Copyright (c) 2021-2024 Espresso Systems (espressosys.com)
// This file is part of the HotShot repository.

// You should have received a copy of the MIT License
// along with the HotShot repository. If not, see <https://mit-license.org/>.

use std::path::PathBuf;

use clap::Args;
use committable::Committable;
use derive_more::From;
use futures::FutureExt;
use hotshot_types::{traits::node_implementation::NodeType, utils::BuilderCommitment};
use serde::{Deserialize, Serialize};
use tagged_base64::TaggedBase64;
use thiserror::Error;
use tide_disco::{api::ApiError, method::ReadState, Api, RequestError, RequestParams, StatusCode};
use vbs::version::StaticVersionType;

use super::{
    data_source::{AcceptsTxnSubmits, BuilderDataSource},
    Version,
};
use crate::api::load_api;

#[derive(Args, Default)]
pub struct Options {
    #[arg(long = "builder-api-path", env = "HOTSHOT_BUILDER_API_PATH")]
    pub api_path: Option<PathBuf>,

    /// Additional API specification files to merge with `builder-api-path`.
    ///
    /// These optional files may contain route definitions for application-specific routes that have
    /// been added as extensions to the basic builder API.
    #[arg(
        long = "builder-extension",
        env = "HOTSHOT_BUILDER_EXTENSIONS",
        value_delimiter = ','
    )]
    pub extensions: Vec<toml::Value>,
}

#[derive(Clone, Debug, Error, Deserialize, Serialize)]
pub enum BuildError {
    #[error("The requested resource does not exist or is not known to this builder service")]
    NotFound,
    #[error("The requested resource exists but is not currently available")]
    Missing,
    #[error("Error trying to fetch the requested resource: {0}")]
    Error(String),
}

/// Enum to keep track on status of a transaction
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum TransactionStatus {
    Pending,
    Sequenced { leaf: u64 },
    Rejected { reason: String }, // Rejection reason is in the String format
    Unknown,
}

#[derive(Clone, Debug, Error, Deserialize, Serialize)]
pub enum Error {
    #[error("Error processing request: {0}")]
    Request(#[from] RequestError),
    #[error("Error building block from {resource}: {source}")]
    BlockAvailable {
        source: BuildError,
        resource: String,
    },
    #[error("Error claiming block {resource}: {source}")]
    BlockClaim {
        source: BuildError,
        resource: String,
    },
    #[error("Error unpacking transactions: {0}")]
    TxnUnpack(RequestError),
    #[error("Error submitting transaction: {0}")]
    TxnSubmit(BuildError),
    #[error("Error getting builder address: {0}")]
    BuilderAddress(#[from] BuildError),
    #[error("Error getting transaction status: {0}")]
    TxnStat(BuildError),
    #[error("Custom error {status}: {message}")]
    Custom { message: String, status: StatusCode },
}

impl tide_disco::error::Error for Error {
    fn catch_all(status: StatusCode, msg: String) -> Self {
        Error::Custom {
            message: msg,
            status,
        }
    }

    fn status(&self) -> StatusCode {
        match self {
            Error::Request { .. } => StatusCode::BAD_REQUEST,
            Error::BlockAvailable { source, .. } | Error::BlockClaim { source, .. } => match source
            {
                BuildError::NotFound => StatusCode::NOT_FOUND,
                BuildError::Missing => StatusCode::NOT_FOUND,
                BuildError::Error { .. } => StatusCode::INTERNAL_SERVER_ERROR,
            },
            Error::TxnUnpack { .. } => StatusCode::BAD_REQUEST,
            Error::TxnSubmit { .. } => StatusCode::INTERNAL_SERVER_ERROR,
            Error::Custom { .. } => StatusCode::INTERNAL_SERVER_ERROR,
            Error::BuilderAddress { .. } => StatusCode::INTERNAL_SERVER_ERROR,
            Error::TxnStat { .. } => StatusCode::INTERNAL_SERVER_ERROR,
        }
    }
}

pub(crate) fn try_extract_param<T: for<'a> TryFrom<&'a TaggedBase64>>(
    params: &RequestParams,
    param_name: &str,
) -> Result<T, Error> {
    params
        .param(param_name)?
        .as_tagged_base64()?
        .try_into()
        .map_err(|_| Error::Custom {
            message: format!("Invalid {param_name}"),
            status: StatusCode::UNPROCESSABLE_ENTITY,
        })
}

pub fn define_api<State, Types: NodeType>(
    options: &Options,
) -> Result<Api<State, Error, Version>, ApiError>
where
    State: 'static + Send + Sync + ReadState,
    <State as ReadState>::State: Send + Sync + BuilderDataSource<Types>,
{
    let mut api = load_api::<State, Error, Version>(
        options.api_path.as_ref(),
        include_str!("../../api/v0_1/builder.toml"),
        options.extensions.clone(),
    )?;
    api.with_version("0.1.0".parse().unwrap())
        .get("available_blocks", |req, state| {
            async move {
                let hash = req.blob_param("parent_hash")?;
                let view_number = req.integer_param("view_number")?;
                let signature = try_extract_param(&req, "signature")?;
                let sender = try_extract_param(&req, "sender")?;
                state
                    .available_blocks(&hash, view_number, sender, &signature)
                    .await
                    .map_err(|source| Error::BlockAvailable {
                        source,
                        resource: hash.to_string(),
                    })
            }
            .boxed()
        })?
        .get("claim_block", |req, state| {
            async move {
                let block_hash: BuilderCommitment = req.blob_param("block_hash")?;
                let view_number = req.integer_param("view_number")?;
                let signature = try_extract_param(&req, "signature")?;
                let sender = try_extract_param(&req, "sender")?;
                state
                    .claim_block(&block_hash, view_number, sender, &signature)
                    .await
                    .map_err(|source| Error::BlockClaim {
                        source,
                        resource: block_hash.to_string(),
                    })
            }
            .boxed()
        })?
        .get("claim_block_with_num_nodes", |req, state| {
            async move {
                let block_hash: BuilderCommitment = req.blob_param("block_hash")?;
                let view_number = req.integer_param("view_number")?;
                let signature = try_extract_param(&req, "signature")?;
                let sender = try_extract_param(&req, "sender")?;
                let num_nodes = req.integer_param("num_nodes")?;
                state
                    .claim_block_with_num_nodes(
                        &block_hash,
                        view_number,
                        sender,
                        &signature,
                        num_nodes,
                    )
                    .await
                    .map_err(|source| Error::BlockClaim {
                        source,
                        resource: block_hash.to_string(),
                    })
            }
            .boxed()
        })?
        .get("claim_header_input", |req, state| {
            async move {
                let block_hash: BuilderCommitment = req.blob_param("block_hash")?;
                let view_number = req.integer_param("view_number")?;
                let signature = try_extract_param(&req, "signature")?;
                let sender = try_extract_param(&req, "sender")?;
                state
                    .claim_block_header_input(&block_hash, view_number, sender, &signature)
                    .await
                    .map_err(|source| Error::BlockClaim {
                        source,
                        resource: block_hash.to_string(),
                    })
            }
            .boxed()
        })?
        .get("builder_address", |_req, state| {
            async move { state.builder_address().await.map_err(|e| e.into()) }.boxed()
        })?;
    Ok(api)
}

pub fn submit_api<State, Types: NodeType, Ver: StaticVersionType + 'static>(
    options: &Options,
) -> Result<Api<State, Error, Ver>, ApiError>
where
    State: 'static + Send + Sync + ReadState,
    <State as ReadState>::State: Send + Sync + AcceptsTxnSubmits<Types>,
{
    let mut api = load_api::<State, Error, Ver>(
        options.api_path.as_ref(),
        include_str!("../../api/v0_1/submit.toml"),
        options.extensions.clone(),
    )?;
    api.with_version("0.0.1".parse().unwrap())
        .at("submit_txn", |req: RequestParams, state| {
            async move {
                let tx = req
                    .body_auto::<<Types as NodeType>::Transaction, Ver>(Ver::instance())
                    .map_err(Error::TxnUnpack)?;
                let hash = tx.commit();
                state
                    .read(|state| state.submit_txns(vec![tx]))
                    .await
                    .map_err(Error::TxnSubmit)?;
                Ok(hash)
            }
            .boxed()
        })?
        .at("submit_batch", |req: RequestParams, state| {
            async move {
                let txns = req
                    .body_auto::<Vec<<Types as NodeType>::Transaction>, Ver>(Ver::instance())
                    .map_err(Error::TxnUnpack)?;
                let hashes = txns.iter().map(|tx| tx.commit()).collect::<Vec<_>>();
                state
                    .read(|state| state.submit_txns(txns))
                    .await
                    .map_err(Error::TxnSubmit)?;
                Ok(hashes)
            }
            .boxed()
        })?
        .get("get_status", |req: RequestParams, state| {
            async move {
                let tx = req
                    .body_auto::<<Types as NodeType>::Transaction, Ver>(Ver::instance())
                    .map_err(Error::TxnUnpack)?;
                let hash = tx.commit();
                state.txn_status(hash).await.map_err(Error::TxnStat)?;
                Ok(hash)
            }
            .boxed()
        })?;
    Ok(api)
}