-
Notifications
You must be signed in to change notification settings - Fork 11.2k
/
main.rs
263 lines (225 loc) · 7.5 KB
/
main.rs
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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use anyhow::Result;
use axum::{
extract::Path,
response::IntoResponse,
routing::{get, post},
Extension, Json, Router,
};
use clap::Parser;
use http::{Method, StatusCode};
use std::{net::SocketAddr, sync::Arc};
use sui_cluster_test::{
cluster::{Cluster, LocalNewCluster},
config::{ClusterTestOpt, Env},
faucet::{FaucetClient, FaucetClientFactory},
};
use sui_faucet::{
BatchFaucetResponse, BatchStatusFaucetResponse, FaucetError, FaucetRequest, FaucetResponse,
FixedAmountRequest,
};
use tower::ServiceBuilder;
use tower_http::cors::{Any, CorsLayer};
use uuid::Uuid;
/// Start a Sui validator and fullnode for easy testing.
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
/// Config directory that will be used to store network config, node db, keystore
/// sui genesis -f --with-faucet generates a genesis config that can be used to start this process.
/// Example: sui-test-validator --config-dir ~/.sui/sui_config
/// We can use any config dir that is generated by the sui genesis.
#[clap(short, long)]
config_dir: Option<std::path::PathBuf>,
/// Port to start the Fullnode RPC server on
#[clap(long, default_value = "9000")]
fullnode_rpc_port: u16,
/// Port to start the Sui faucet on
#[clap(long, default_value = "9123")]
faucet_port: u16,
/// Host to start the GraphQl server on
#[clap(long, default_value = "127.0.0.1")]
graphql_host: String,
/// Port to start the GraphQl server on
/// Explicitly setting this enables the server
#[clap(long)]
graphql_port: Option<u16>,
/// Port to start the Indexer RPC server on
#[clap(long, default_value = "9124")]
indexer_rpc_port: u16,
/// Port for the Indexer Postgres DB
/// 5432 is the default port for postgres on Mac
#[clap(long, default_value = "5432")]
pg_port: u16,
/// Hostname for the Indexer Postgres DB
#[clap(long, default_value = "localhost")]
pg_host: String,
/// DB name for the Indexer Postgres DB
#[clap(long, default_value = "sui_indexer")]
pg_db_name: String,
/// DB username for the Indexer Postgres DB
#[clap(long, default_value = "postgres")]
pg_user: String,
/// DB password for the Indexer Postgres DB
#[clap(long, default_value = "postgrespw")]
pg_password: String,
/// The duration for epochs (defaults to one minute)
#[clap(long, default_value = "60000")]
epoch_duration_ms: u64,
/// if we should run indexer
#[clap(long)]
pub with_indexer: bool,
}
#[tokio::main]
async fn main() -> Result<()> {
let (_guard, _filter_handle) = telemetry_subscribers::TelemetryConfig::new()
.with_env()
.init();
let args = Args::parse();
let Args {
config_dir,
fullnode_rpc_port,
graphql_host,
graphql_port,
indexer_rpc_port,
pg_port,
pg_host,
pg_db_name,
pg_user,
pg_password,
epoch_duration_ms,
faucet_port,
with_indexer,
} = args;
// We don't pass epoch duration if we have a genesis config.
let epoch_duration_ms = if config_dir.is_some() {
None
} else {
Some(epoch_duration_ms)
};
if graphql_port.is_none() {
println!("Graphql port not provided. Graphql service will not run.")
}
if !with_indexer {
println!("`with_indexer` flag unset. Indexer service will not run.")
}
let cluster_config = ClusterTestOpt {
env: Env::NewLocal,
fullnode_address: Some(format!("0.0.0.0:{}", fullnode_rpc_port)),
indexer_address: with_indexer.then_some(format!("0.0.0.0:{}", indexer_rpc_port)),
pg_address: Some(format!(
"postgres://{pg_user}:{pg_password}@{pg_host}:{pg_port}/{pg_db_name}"
)),
faucet_address: Some(format!("127.0.0.1:{}", faucet_port)),
epoch_duration_ms,
config_dir,
graphql_address: graphql_port.map(|p| format!("{}:{}", graphql_host, p)),
};
println!("Starting Sui validator with config: {:#?}", cluster_config);
let cluster = LocalNewCluster::start(&cluster_config).await?;
println!("Fullnode RPC URL: {}", cluster.fullnode_url());
if with_indexer {
println!(
"Indexer RPC URL: {}",
cluster.indexer_url().clone().unwrap_or_default()
);
}
start_faucet(&cluster, faucet_port).await?;
Ok(())
}
struct AppState {
faucet: Arc<dyn FaucetClient + Sync + Send>,
}
async fn start_faucet(cluster: &LocalNewCluster, port: u16) -> Result<()> {
let faucet = FaucetClientFactory::new_from_cluster(cluster).await;
let app_state = Arc::new(AppState { faucet });
let cors = CorsLayer::new()
.allow_methods(vec![Method::GET, Method::POST])
.allow_headers(Any)
.allow_origin(Any);
let app = Router::new()
.route("/", get(health))
.route("/gas", post(faucet_request))
.route("/v1/gas", post(faucet_batch_request))
.route("/v1/status/:task_id", get(request_status))
.layer(
ServiceBuilder::new()
.layer(cors)
.layer(Extension(app_state))
.into_inner(),
);
let addr = SocketAddr::from(([0, 0, 0, 0], port));
println!("Faucet URL: http://{}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await?;
Ok(())
}
/// basic handler that responds with a static string
async fn health() -> &'static str {
"OK"
}
async fn faucet_request(
Extension(state): Extension<Arc<AppState>>,
Json(payload): Json<FaucetRequest>,
) -> impl IntoResponse {
let result = match payload {
FaucetRequest::FixedAmountRequest(FixedAmountRequest { recipient }) => {
state.faucet.request_sui_coins(recipient).await
}
_ => {
return (
StatusCode::BAD_REQUEST,
Json(FaucetResponse::from(FaucetError::Internal(
"Input Error.".to_string(),
))),
)
}
};
if !result.transferred_gas_objects.is_empty() {
(StatusCode::CREATED, Json(result))
} else {
(StatusCode::INTERNAL_SERVER_ERROR, Json(result))
}
}
async fn faucet_batch_request(
Extension(state): Extension<Arc<AppState>>,
Json(payload): Json<FaucetRequest>,
) -> impl IntoResponse {
let result = match payload {
FaucetRequest::FixedAmountRequest(FixedAmountRequest { recipient }) => {
state.faucet.batch_request_sui_coins(recipient).await
}
_ => {
return (
StatusCode::BAD_REQUEST,
Json(BatchFaucetResponse::from(FaucetError::Internal(
"Input Error.".to_string(),
))),
)
}
};
if result.task.is_some() {
(StatusCode::CREATED, Json(result))
} else {
(StatusCode::INTERNAL_SERVER_ERROR, Json(result))
}
}
async fn request_status(
Extension(state): Extension<Arc<AppState>>,
Path(id): Path<String>,
) -> impl IntoResponse {
match Uuid::parse_str(&id) {
Ok(task_id) => {
let status = state.faucet.get_batch_send_status(task_id).await;
(StatusCode::CREATED, Json(status))
}
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(BatchStatusFaucetResponse::from(FaucetError::Internal(
e.to_string(),
))),
),
}
}