瀏覽代碼

[Fortuna] register fortuna uri on chain (#1257)

* uri registration

* pre-commit

* use uri to create register uri

* pre commit

* move get register uri to api
Dev Kalra 1 年之前
父節點
當前提交
58411495a0

+ 9 - 8
fortuna/Cargo.lock

@@ -1477,9 +1477,9 @@ checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
 
 [[package]]
 name = "form_urlencoded"
-version = "1.2.0"
+version = "1.2.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652"
+checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456"
 dependencies = [
  "percent-encoding",
 ]
@@ -1515,6 +1515,7 @@ dependencies = [
  "tower-http",
  "tracing",
  "tracing-subscriber",
+ "url",
  "utoipa",
  "utoipa-swagger-ui",
 ]
@@ -1914,9 +1915,9 @@ checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
 
 [[package]]
 name = "idna"
-version = "0.4.0"
+version = "0.5.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c"
+checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6"
 dependencies = [
  "unicode-bidi",
  "unicode-normalization",
@@ -2582,9 +2583,9 @@ dependencies = [
 
 [[package]]
 name = "percent-encoding"
-version = "2.3.0"
+version = "2.3.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94"
+checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
 
 [[package]]
 name = "petgraph"
@@ -4199,9 +4200,9 @@ checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
 
 [[package]]
 name = "url"
-version = "2.4.1"
+version = "2.5.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "143b538f18257fac9cad154828a57c6bf5157e1aa604d4816b5995bf6de87ae5"
+checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633"
 dependencies = [
  "form_urlencoded",
  "idna",

+ 1 - 0
fortuna/Cargo.toml

@@ -32,6 +32,7 @@ utoipa             = { version = "3.4.0", features = ["axum_extras"] }
 utoipa-swagger-ui  = { version = "3.1.4", features = ["axum"] }
 once_cell = "1.18.0"
 lazy_static = "1.4.0"
+url = "2.5.0"
 
 [dev-dependencies]
 axum-test = "13.1.1"

+ 12 - 0
fortuna/src/api.rs

@@ -6,6 +6,7 @@ use {
         },
         state::HashChainState,
     },
+    anyhow::Result,
     axum::{
         body::Body,
         http::StatusCode,
@@ -30,6 +31,7 @@ use {
         sync::Arc,
     },
     tokio::sync::RwLock,
+    url::Url,
 };
 pub use {
     chain_ids::*,
@@ -179,6 +181,16 @@ pub fn routes(state: ApiState) -> Router<(), Body> {
         .with_state(state)
 }
 
+/// We are registering the provider on chain with the following url:
+/// `{base_uri}/v1/chains/{chain_id}`
+/// The path and API are highly coupled. Please be sure to keep them consistent.
+pub fn get_register_uri(base_uri: &str, chain_id: &str) -> Result<String> {
+    let base_uri = Url::parse(base_uri)?;
+    let path = format!("/v1/chains/{}", chain_id);
+    let uri = base_uri.join(&path)?;
+    Ok(uri.to_string())
+}
+
 #[cfg(test)]
 mod test {
     use {

+ 0 - 1
fortuna/src/chain/ethereum.rs

@@ -91,7 +91,6 @@ impl SignablePythContract {
         };
 
         let wallet__ = private_key
-            .clone()
             .parse::<LocalWallet>()?
             .with_chain_id(chain_id.as_u64());
 

+ 10 - 7
fortuna/src/command/setup_provider.rs

@@ -1,5 +1,6 @@
 use {
     crate::{
+        api::get_register_uri,
         chain::ethereum::SignablePythContract,
         command::{
             register_provider,
@@ -44,6 +45,8 @@ pub async fn setup_provider(opts: &SetupProviderOptions) -> Result<()> {
 
         let mut register = false;
 
+        let uri = get_register_uri(&opts.base_uri, &chain_id)?;
+
         // This condition satisfies for both when there is no registration and when there are no
         // more random numbers left to request
         if provider_info.end_sequence_number <= provider_info.sequence_number {
@@ -85,12 +88,12 @@ pub async fn setup_provider(opts: &SetupProviderOptions) -> Result<()> {
 
         if register {
             register_provider(&RegisterProviderOptions {
-                config:      opts.config.clone(),
-                chain_id:    chain_id.clone(),
+                config: opts.config.clone(),
+                chain_id: chain_id.clone(),
                 private_key: private_key.clone(),
-                randomness:  opts.randomness.clone(),
-                fee:         opts.fee,
-                uri:         opts.uri.clone(),
+                randomness: opts.randomness.clone(),
+                fee: opts.fee,
+                uri,
             })
             .await?;
         } else {
@@ -100,9 +103,9 @@ pub async fn setup_provider(opts: &SetupProviderOptions) -> Result<()> {
                 }
             }
 
-            if bincode::deserialize::<String>(&provider_info.uri)? != opts.uri {
+            if bincode::deserialize::<String>(&provider_info.uri)? != uri {
                 if let Some(receipt) = contract
-                    .set_provider_uri(bincode::serialize(&opts.uri)?.into())
+                    .set_provider_uri(bincode::serialize(&uri)?.into())
                     .send()
                     .await?
                     .log_msg("Pending transfer hash")

+ 3 - 4
fortuna/src/config/setup_provider.rs

@@ -29,11 +29,10 @@ pub struct SetupProviderOptions {
     #[arg(default_value = "100")]
     pub fee: u128,
 
-    /// The URI where clients can retrieve random values from this provider,
-    /// i.e., wherever fortuna for this provider will be hosted.
+    /// The base URI for fortuna.
+    /// e.g., https://fortuna-staging.pyth.network
     #[arg(long = "uri")]
-    #[arg(default_value = "")]
-    pub uri: String,
+    pub base_uri: String,
 }
 
 impl SetupProviderOptions {