Skip to content

Commit

Permalink
Merge branch 'main' into random23_other_abi_change
Browse files Browse the repository at this point in the history
  • Loading branch information
jayantk committed Nov 27, 2023
2 parents 1f930c0 + 7641f7e commit a7cc2c9
Show file tree
Hide file tree
Showing 36 changed files with 569 additions and 396 deletions.
7 changes: 2 additions & 5 deletions contract_manager/scripts/update_pricefeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const parser = yargs(hideBin(process.argv))
},
endpoint: {
type: "string",
desc: "Hermes endpoint to use, defaults to https://hermes.pyth.network for mainnet and https://hermes-beta.pyth.network for testnet",
desc: "Hermes endpoint to use, defaults to https://hermes.pyth.network",
},
});

Expand All @@ -39,11 +39,8 @@ async function main() {
)}`
);
}
const defaultEndpoint = contract.getChain().isMainnet()
? "https://hermes.pyth.network"
: "https://hermes-beta.pyth.network";
const priceService = new PriceServiceConnection(
argv.endpoint || defaultEndpoint
argv.endpoint || "https://hermes.pyth.network"
);
const vaas = await priceService.getLatestVaas(argv["feed-id"] as string[]);
const privateKey = toPrivateKey(argv["private-key"]);
Expand Down
3 changes: 2 additions & 1 deletion fortuna/Cargo.lock

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

3 changes: 2 additions & 1 deletion fortuna/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[package]
name = "fortuna"
version = "1.0.0"
version = "2.0.0"
edition = "2021"

[dependencies]
anyhow = "1.0.75"
axum = { version = "0.6.20", features = ["json", "ws", "macros"] }
axum-macros = { version = "0.3.8" }
base64 = { version = "0.21.0" }
bincode = "1.3.3"
byteorder = "1.5.0"
clap = { version = "4.4.6", features = ["derive", "cargo", "env"] }
ethabi = "18.0.0"
Expand Down
78 changes: 11 additions & 67 deletions fortuna/src/abi.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,4 @@
[
{
"inputs": [
{
"internalType": "uint256",
"name": "pythFeeInWei",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "AssertionFailure",
"type": "error"
},
{
"inputs": [],
"name": "IncorrectProviderRevelation",
"type": "error"
},
{
"inputs": [],
"name": "IncorrectUserRevelation",
"type": "error"
},
{
"inputs": [],
"name": "InsufficientFee",
"type": "error"
},
{
"inputs": [],
"name": "NoSuchProvider",
"type": "error"
},
{
"inputs": [],
"name": "OutOfRandomness",
"type": "error"
},
{
"anonymous": false,
"inputs": [
Expand All @@ -66,9 +25,9 @@
"type": "uint64"
},
{
"internalType": "bytes32",
"internalType": "bytes",
"name": "commitmentMetadata",
"type": "bytes32"
"type": "bytes"
},
{
"internalType": "uint64",
Expand All @@ -92,7 +51,7 @@
}
],
"indexed": false,
"internalType": "struct PythRandomStructs.ProviderInfo",
"internalType": "struct EntropyStructs.ProviderInfo",
"name": "provider",
"type": "tuple"
}
Expand Down Expand Up @@ -130,19 +89,14 @@
"name": "providerCommitmentSequenceNumber",
"type": "uint64"
},
{
"internalType": "bytes32",
"name": "providerCommitmentMetadata",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "blockNumber",
"type": "uint256"
}
],
"indexed": false,
"internalType": "struct PythRandomStructs.Request",
"internalType": "struct EntropyStructs.Request",
"name": "request",
"type": "tuple"
}
Expand Down Expand Up @@ -180,19 +134,14 @@
"name": "providerCommitmentSequenceNumber",
"type": "uint64"
},
{
"internalType": "bytes32",
"name": "providerCommitmentMetadata",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "blockNumber",
"type": "uint256"
}
],
"indexed": false,
"internalType": "struct PythRandomStructs.Request",
"internalType": "struct EntropyStructs.Request",
"name": "request",
"type": "tuple"
},
Expand Down Expand Up @@ -337,9 +286,9 @@
"type": "uint64"
},
{
"internalType": "bytes32",
"internalType": "bytes",
"name": "commitmentMetadata",
"type": "bytes32"
"type": "bytes"
},
{
"internalType": "uint64",
Expand All @@ -362,7 +311,7 @@
"type": "uint64"
}
],
"internalType": "struct PythRandomStructs.ProviderInfo",
"internalType": "struct EntropyStructs.ProviderInfo",
"name": "info",
"type": "tuple"
}
Expand Down Expand Up @@ -412,18 +361,13 @@
"name": "providerCommitmentSequenceNumber",
"type": "uint64"
},
{
"internalType": "bytes32",
"name": "providerCommitmentMetadata",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "blockNumber",
"type": "uint256"
}
],
"internalType": "struct PythRandomStructs.Request",
"internalType": "struct EntropyStructs.Request",
"name": "req",
"type": "tuple"
}
Expand All @@ -444,9 +388,9 @@
"type": "bytes32"
},
{
"internalType": "bytes32",
"internalType": "bytes",
"name": "commitmentMetadata",
"type": "bytes32"
"type": "bytes"
},
{
"internalType": "uint64",
Expand Down
26 changes: 20 additions & 6 deletions fortuna/src/command/register_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ use {
std::sync::Arc,
};

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct CommitmentMetadata {
pub seed: [u8; 32],
pub chain_length: u64,
}

/// Register as a randomness provider. This method will generate and commit to a new random
/// hash chain from the configured secret & a newly generated random value.
pub async fn register_provider(opts: &RegisterProviderOptions) -> Result<()> {
Expand All @@ -25,21 +31,29 @@ pub async fn register_provider(opts: &RegisterProviderOptions) -> Result<()> {

// Create a new random hash chain.
let random = rand::random::<[u8; 32]>();
let mut chain = PebbleHashChain::from_config(&opts.randomness, &opts.chain_id, random)?;
let commitment_length = opts.randomness.chain_length;
let mut chain = PebbleHashChain::from_config(
&opts.randomness.secret,
&opts.chain_id,
&random,
commitment_length,
)?;

// Arguments to the contract to register our new provider.
let fee_in_wei = opts.fee;
let commitment = chain.reveal()?;
// Store the random seed in the metadata field so that we can regenerate the hash chain
// at-will. (This is secure because you can't generate the chain unless you also have the secret)
let commitment_metadata = random;
let commitment_length = opts.randomness.chain_length;
// Store the random seed and chain length in the metadata field so that we can regenerate the hash
// chain at-will. (This is secure because you can't generate the chain unless you also have the secret)
let commitment_metadata = CommitmentMetadata {
seed: random,
chain_length: commitment_length,
};

if let Some(r) = contract
.register(
fee_in_wei,
commitment,
commitment_metadata,
bincode::serialize(&commitment_metadata)?.into(),
commitment_length,
)
.send()
Expand Down
12 changes: 10 additions & 2 deletions fortuna/src/command/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use {
crate::{
api,
chain::ethereum::PythContract,
command::register_provider::CommitmentMetadata,
config::{
Config,
RunOptions,
Expand Down Expand Up @@ -59,8 +60,15 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
// TODO: we may want to load the hash chain in a lazy/fault-tolerant way. If there are many blockchains,
// then it's more likely that some RPC fails. We should tolerate these faults and generate the hash chain
// later when a user request comes in for that chain.
let random: [u8; 32] = provider_info.commitment_metadata;
let hash_chain = PebbleHashChain::from_config(&opts.randomness, &chain_id, random)?;
let metadata =
bincode::deserialize::<CommitmentMetadata>(&provider_info.commitment_metadata)?;

let hash_chain = PebbleHashChain::from_config(
&opts.randomness.secret,
&chain_id,
&metadata.seed,
metadata.chain_length,
)?;
let chain_state = HashChainState {
offsets: vec![provider_info
.original_commitment_sequence_number
Expand Down
16 changes: 7 additions & 9 deletions fortuna/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use {
crate::{
api::ChainId,
config::RandomnessOptions,
},
crate::api::ChainId,
anyhow::{
ensure,
Result,
Expand Down Expand Up @@ -36,17 +33,18 @@ impl PebbleHashChain {
}

pub fn from_config(
opts: &RandomnessOptions,
secret: &str,
chain_id: &ChainId,
random: [u8; 32],
random: &[u8; 32],
chain_length: u64,
) -> Result<Self> {
let mut input: Vec<u8> = vec![];
input.extend_from_slice(&hex::decode(opts.secret.clone())?);
input.extend_from_slice(&hex::decode(secret)?);
input.extend_from_slice(&chain_id.as_bytes());
input.extend_from_slice(&random);
input.extend_from_slice(random);

let secret: [u8; 32] = Keccak256::digest(input).into();
Ok(Self::new(secret, opts.chain_length.try_into()?))
Ok(Self::new(secret, chain_length.try_into()?))
}

/// Reveal the next hash in the chain using the previous proof.
Expand Down
Loading

0 comments on commit a7cc2c9

Please sign in to comment.