Jayant Krishnamurthy 2 роки тому
батько
коміт
b80abe795d
3 змінених файлів з 22 додано та 5 видалено
  1. 2 0
      pyth-rng/src/api.rs
  2. 13 0
      pyth-rng/src/api/index.rs
  3. 7 5
      pyth-rng/src/command/run.rs

+ 2 - 0
pyth-rng/src/api.rs

@@ -10,6 +10,7 @@ use axum::{
 use ethers::core::types::Address;
 
 pub use {
+    index::*,
     revelation::*,
 };
 
@@ -17,6 +18,7 @@ use crate::ethereum::PythContract;
 use crate::state::HashChainState;
 
 mod revelation;
+mod index;
 
 /// The state of the randomness service for a single blockchain.
 #[derive(Clone)]

+ 13 - 0
pyth-rng/src/api/index.rs

@@ -0,0 +1,13 @@
+use axum::{
+    response::IntoResponse,
+    Json,
+};
+
+/// This is the index page for the REST service. It lists all the available endpoints.
+///
+/// TODO: Dynamically generate this list if possible.
+pub async fn index() -> impl IntoResponse {
+    Json([
+        "/v1/revelation",
+    ])
+}

+ 7 - 5
pyth-rng/src/command/run.rs

@@ -1,3 +1,4 @@
+use anyhow::anyhow;
 use std::error::Error;
 
 use clap::Parser;
@@ -18,7 +19,7 @@ use {
 };
 use ethers::core::types::Address;
 
-use crate::api::{ApiState, revelation};
+use crate::api;
 use crate::state::{HashChainState, PebbleHashChain};
 use crate::ethereum::PythContract;
 
@@ -34,7 +35,7 @@ pub async fn run(opts: &RunOptions) -> Result<(), Box<dyn Error>> {
     )
     ),
     tags(
-    (name = "hermes", description = "Pyth Real-Time Pricing API")
+    (name = "pyth-rng", description = "Pyth Random Number Service")
     )
     )]
     struct ApiDoc;
@@ -55,19 +56,20 @@ pub async fn run(opts: &RunOptions) -> Result<(), Box<dyn Error>> {
     };
 
     if chain_state.reveal(provider_info.original_commitment_sequence_number)? != provider_info.original_commitment {
-        println!("warning: root of chain does not match commitment!");
+        return Err(anyhow!("The root of the generated hash chain does not match the commitment. Is the secret configured correctly?").into());
     } else {
         println!("Root of chain matches commitment");
     }
 
-    let mut state = ApiState { state: Arc::new(chain_state), contract, provider_address: provider_addr };
+    let mut state = api::ApiState { state: Arc::new(chain_state), contract, provider_address: provider_addr };
 
     // Initialize Axum Router. Note the type here is a `Router<State>` due to the use of the
     // `with_state` method which replaces `Body` with `State` in the type signature.
     let app = Router::new();
     let app = app
         .merge(SwaggerUi::new("/docs").url("/docs/openapi.json", ApiDoc::openapi()))
-        .route("/v1/revelation", get(revelation))
+        .route("/", get(api::index))
+        .route("/v1/revelation", get(api::revelation))
         .with_state(state.clone())
         // Permissive CORS layer to allow all origins
         .layer(CorsLayer::permissive());