Skip to content

Commit

Permalink
[Fortuna] register fortuna uri on chain (#1257)
Browse files Browse the repository at this point in the history
* uri registration

* pre-commit

* use uri to create register uri

* pre commit

* move get register uri to api
  • Loading branch information
Dev Kalra authored Jan 30, 2024
1 parent 6870209 commit 5841149
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
17 changes: 9 additions & 8 deletions fortuna/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fortuna/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 12 additions & 0 deletions fortuna/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use {
},
state::HashChainState,
},
anyhow::Result,
axum::{
body::Body,
http::StatusCode,
Expand All @@ -30,6 +31,7 @@ use {
sync::Arc,
},
tokio::sync::RwLock,
url::Url,
};
pub use {
chain_ids::*,
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion fortuna/src/chain/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ impl SignablePythContract {
};

let wallet__ = private_key
.clone()
.parse::<LocalWallet>()?
.with_chain_id(chain_id.as_u64());

Expand Down
17 changes: 10 additions & 7 deletions fortuna/src/command/setup_provider.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
crate::{
api::get_register_uri,
chain::ethereum::SignablePythContract,
command::{
register_provider,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
7 changes: 3 additions & 4 deletions fortuna/src/config/setup_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5841149

Please sign in to comment.