Skip to content

Commit

Permalink
[fortuna] improve logging and update uri encoding (#1317)
Browse files Browse the repository at this point in the history
* [fortuna] improve logging

* update version

* update uri encoding
  • Loading branch information
Dev Kalra authored Feb 22, 2024
1 parent f22c0c8 commit fb4c254
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion fortuna/Cargo.lock

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

2 changes: 1 addition & 1 deletion fortuna/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fortuna"
version = "3.2.3"
version = "3.2.4"
edition = "2021"

[dependencies]
Expand Down
5 changes: 4 additions & 1 deletion fortuna/src/command/register_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use {
},
anyhow::Result,
ethers::{
abi::Bytes,
signers::{
LocalWallet,
Signer,
Expand Down Expand Up @@ -60,7 +61,9 @@ pub async fn register_provider(opts: &RegisterProviderOptions) -> Result<()> {
commitment,
bincode::serialize(&commitment_metadata)?.into(),
commitment_length,
bincode::serialize(&opts.uri)?.into(),
// Use Bytes to serialize the uri. Most users will be using JS/TS to deserialize this uri.
// Bincode is a different encoding mechanisms, and I didn't find any JS/TS library to parse bincode.
Bytes::from(opts.uri.as_str()).into(),
);
let mut gas_estimate = call.estimate_gas().await?;
let gas_multiplier = U256::from(2); //TODO: smarter gas estimation
Expand Down
37 changes: 25 additions & 12 deletions fortuna/src/command/setup_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ use {
},
},
anyhow::Result,
ethers::signers::{
LocalWallet,
Signer,
ethers::{
abi::Bytes as AbiBytes,
signers::{
LocalWallet,
Signer,
},
types::Bytes,
},
std::sync::Arc,
};
Expand All @@ -40,22 +44,25 @@ pub async fn setup_provider(opts: &SetupProviderOptions) -> Result<()> {
// Initialize a Provider to interface with the EVM contract.
let contract =
Arc::new(SignablePythContract::from_config(&chain_config, &private_key).await?);

tracing::info!("{}: fetching provider info", chain_id);
let provider_info = contract.get_provider_info(provider_address).call().await?;
tracing::info!("Provider info: {:?}", provider_info);
tracing::info!("{0}: provider info: {1:?}", chain_id, provider_info);

let mut register = false;

let uri = get_register_uri(&opts.base_uri, &chain_id)?;
let uri_as_bytes: Bytes = AbiBytes::from(uri.as_str()).into();

// 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 {
tracing::info!(
"endSequenceNumber <= sequenceNumber. endSequenceNumber={0}, sequenceNumber={1}",
"{0}: endSequenceNumber <= sequenceNumber. endSequenceNumber={1}, sequenceNumber={2}",
chain_id,
provider_info.end_sequence_number,
provider_info.sequence_number
);
tracing::info!("Registering to {}", &chain_id);
register = true;
} else {
let metadata =
Expand All @@ -80,13 +87,16 @@ pub async fn setup_provider(opts: &SetupProviderOptions) -> Result<()> {
if chain_state.reveal(provider_info.original_commitment_sequence_number)?
!= provider_info.original_commitment
{
tracing::info!("The root of the generated hash chain for chain id {} does not match the commitment", &chain_id);
tracing::info!("Registering to {}", &chain_id);
tracing::info!(
"{}: the root of the generated hash chain does not match the commitment",
&chain_id
);
register = true;
}
}

if register {
tracing::info!("{}: registering", &chain_id);
register_provider(&RegisterProviderOptions {
config: opts.config.clone(),
chain_id: chain_id.clone(),
Expand All @@ -96,22 +106,25 @@ pub async fn setup_provider(opts: &SetupProviderOptions) -> Result<()> {
uri,
})
.await?;
tracing::info!("{}: registered", &chain_id);
} else {
if provider_info.fee_in_wei != opts.fee {
tracing::info!("{}: updating provider fee", chain_id);
if let Some(r) = contract.set_provider_fee(opts.fee).send().await?.await? {
tracing::info!("Updated provider fee: {:?}", r);
tracing::info!("{0}: updated provider fee: {1:?}", chain_id, r);
}
}

if bincode::deserialize::<String>(&provider_info.uri)? != uri {
if &provider_info.uri != &uri_as_bytes {
tracing::info!("{}: updating provider uri", chain_id);
if let Some(receipt) = contract
.set_provider_uri(bincode::serialize(&uri)?.into())
.set_provider_uri(uri_as_bytes)
.send()
.await?
.log_msg("Pending transfer hash")
.await?
{
tracing::info!("Updated provider uri: {:?}", receipt);
tracing::info!("{0}: updated provider uri: {1:?}", chain_id, receipt);
}
}
}
Expand Down

0 comments on commit fb4c254

Please sign in to comment.