Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fortuna] Add confirmation delay before responding #1161

Merged
merged 11 commits into from
Dec 4, 2023
Merged

Conversation

jayantk
Copy link
Contributor

@jayantk jayantk commented Dec 2, 2023

This PR adds a configurable confirmation delay to fortuna for each blockchain. The purpose of this delay is to ensure that the request has confirmed and will not be rolled back in a chain reorg before revealing the random number.

In order to implement this change, I had to update the entropy contract to store the block number regardless of whether or not the blockhash was used. I did this using 1 bit of the number to save storage slots (sigh).

Copy link

vercel bot commented Dec 2, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
example-oracle-amm ⬜️ Ignored (Inspect) Visit Preview Dec 4, 2023 5:40am
xc-admin-frontend ⬜️ Ignored (Inspect) Visit Preview Dec 4, 2023 5:40am

@@ -1,19 +1,29 @@
[
{
"type": "constructor",
"inputs": [
"type": "function",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we hadn't regenerated this in a while so there are unrelated ABI changes mixed in here.

@@ -35,7 +34,7 @@ pub struct RegisterProviderOptions {
/// The fee to charge (in wei) for each requested random number
#[arg(long = "pyth-contract-fee")]
#[arg(default_value = "100")]
pub fee: U256,
pub fee: u128,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fallout from updating the ABI. this changed on-chain in a previous pr and didn't get updated here


match maybe_request {
Some(_) => {
Some(r) if current_block_number - state.confirmation_blocks >= r.block_number => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it shouldn't happen but let's make it saturating sub to be extra careful

Copy link
Collaborator

@ali-bahjati ali-bahjati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks correct.
However, this optimization to embed something in an existing type freaks me out and increases the likelihood of making mistakes in the future. I am not sure it is entirely worth it but you will know better. The least thing is changing the name of the variable everywhere so it doesn't get mistaken by just a blockNumber.

@@ -194,9 +197,15 @@ impl EntropyReader for PythContract {
Ok(Some(reader::Request {
provider: r.provider,
sequence_number: r.sequence_number,
block_number: r.block_number.abs().try_into()?,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To support my comment. This abs here is so subtle (and without docs) that can get easily missed if someone edits this code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would suggest struct BlockHeight() with a validating constructor to make this never slip through, in general I'd advocate introducing more types and avoiding type in general.


let (maybe_request, current_block_number) =
try_join!(maybe_request_fut, current_block_number_fut)
.map_err(|_| RestError::TemporarilyUnavailable)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor point but it might be nice to log which of these failed by logging the error just in case.

fortuna/src/api/revelation.rs Show resolved Hide resolved
@@ -115,6 +118,9 @@ pub struct EthereumConfig {
/// Address of a Pyth Randomness contract to interact with.
pub contract_addr: Address,

/// How many blocks to wait before revealing the random number.
pub confirmation_blocks: BlockNumber,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very very minor nitpick and possibly bikeshedding, but I think the name is a bit confusing, reveal_delay_blocks might be a bit better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah agree with this. renamed it.

@@ -194,9 +197,15 @@ impl EntropyReader for PythContract {
Ok(Some(reader::Request {
provider: r.provider,
sequence_number: r.sequence_number,
block_number: r.block_number.abs().try_into()?,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would suggest struct BlockHeight() with a validating constructor to make this never slip through, in general I'd advocate introducing more types and avoiding type in general.

@ali-bahjati ali-bahjati merged commit c592fd3 into main Dec 4, 2023
7 checks passed
@ali-bahjati ali-bahjati deleted the fortuna_abi branch December 4, 2023 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants