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
// 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 snafu::{ResultExt, Snafu};
use tagged_base64::TaggedBase64;
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, Snafu, Deserialize, Serialize)]
#[snafu(visibility(pub))]
pub enum BuildError {
    /// The requested resource does not exist or is not known to this builder service.
    NotFound,
    /// The requested resource exists but is not currently available.
    Missing,
    /// There was an error while trying to fetch the requested resource.
    #[snafu(display("Failed to fetch requested resource: {message}"))]
    Error { message: String },
}

#[derive(Clone, Debug, From, Snafu, Deserialize, Serialize)]
#[snafu(visibility(pub))]
pub enum Error {
    Request {
        source: RequestError,
    },
    #[snafu(display("error building block from {resource}: {source}"))]
    #[from(ignore)]
    BlockAvailable {
        source: BuildError,
        resource: String,
    },
    #[snafu(display("error claiming block {resource}: {source}"))]
    #[from(ignore)]
    BlockClaim {
        source: BuildError,
        resource: String,
    },
    #[snafu(display("error unpacking transaction: {source}"))]
    #[from(ignore)]
    TxnUnpack {
        source: RequestError,
    },
    #[snafu(display("error submitting transaction: {source}"))]
    #[from(ignore)]
    TxnSubmit {
        source: BuildError,
    },
    #[snafu(display("error getting builder address: {source}"))]
    #[from(ignore)]
    BuilderAddress {
        source: BuildError,
    },
    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,
        }
    }
}

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
                    .context(BlockAvailableSnafu {
                        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
                    .context(BlockClaimSnafu {
                        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
                    .context(BlockClaimSnafu {
                        resource: block_hash.to_string(),
                    })
            }
            .boxed()
        })?
        .get("builder_address", |_req, state| {
            async move { state.builder_address().await.context(BuilderAddressSnafu) }.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 + 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())
                    .context(TxnUnpackSnafu)?;
                let hash = tx.commit();
                state.submit_txns(vec![tx]).await.context(TxnSubmitSnafu)?;
                Ok(hash)
            }
            .boxed()
        })?
        .at("submit_batch", |req: RequestParams, state| {
            async move {
                let txns = req
                    .body_auto::<Vec<<Types as NodeType>::Transaction>, Ver>(Ver::instance())
                    .context(TxnUnpackSnafu)?;
                let hashes = txns.iter().map(|tx| tx.commit()).collect::<Vec<_>>();
                state.submit_txns(txns).await.context(TxnSubmitSnafu)?;
                Ok(hashes)
            }
            .boxed()
        })?;
    Ok(api)
}