Skip to content

Commit

Permalink
refactor: validator witness + using reqwest blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
ctian1 committed Sep 19, 2023
1 parent ccec577 commit d63726d
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 72 deletions.
2 changes: 1 addition & 1 deletion plonky2x/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ serde = { version = "1.0.187", features = ["derive"] }
serde_json = "1.0.103"
tokio = { version = "1", features = ["full"] }
anyhow = "1.0.75"
reqwest = { version = "0.11.4", features = ["json"] }
reqwest = { version = "0.11.4", features = ["blocking", "json"] }
array-macro = "2.1.5"
tracing = "0.1.37"
num-bigint = { version = "0.4", features = ["rand"] }
Expand Down
7 changes: 1 addition & 6 deletions plonky2x/src/frontend/eth/beacon/generators/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,22 @@ impl<L: PlonkParameters<D>, const D: usize> SimpleGenerator<L::Field, D>
.block_on(async {
match &self.input {
BeaconBalanceInput::IndexConst(idx) => {
self.client
.get_validator_balance_v2(hex!(block_root), *idx)
.await
self.client.get_validator_balance_v2(hex!(block_root), *idx)
}
BeaconBalanceInput::IndexVariable(idx) => {
let idx = idx.get(witness);
self.client
.get_validator_balance_v2(hex!(block_root), idx.as_u64())
.await
}
BeaconBalanceInput::PubkeyConst(pubkey) => {
let pubkey = hex!(pubkey.0);
self.client
.get_validator_balance_by_pubkey_v2(hex!(block_root), pubkey)
.await
}
BeaconBalanceInput::PubkeyVariable(pubkey) => {
let pubkey = hex!(pubkey.get(witness));
self.client
.get_validator_balance_by_pubkey_v2(hex!(block_root), pubkey)
.await
}
}
})
Expand Down
1 change: 0 additions & 1 deletion plonky2x/src/frontend/eth/beacon/generators/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl<L: PlonkParameters<D>, const D: usize> SimpleGenerator<L::Field, D>
let result = rt.block_on(async {
self.client
.get_balances_root(hex!(block_root.as_bytes()).to_string())
.await
.expect("failed to get validators root")
});

Expand Down
1 change: 0 additions & 1 deletion plonky2x/src/frontend/eth/beacon/generators/historical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D>
let result = rt.block_on(async {
self.client
.get_historical_block(hex!(block_root.as_bytes()).to_string(), offset.as_u64())
.await
.expect("failed to get validators root")
});

Expand Down
1 change: 1 addition & 0 deletions plonky2x/src/frontend/eth/beacon/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod validator;
mod validators;
mod withdrawal;
mod withdrawals;
mod validator_witness;

pub use balance::BeaconBalanceGenerator;
pub use balances::BeaconBalancesGenerator;
Expand Down
3 changes: 0 additions & 3 deletions plonky2x/src/frontend/eth/beacon/generators/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,17 @@ impl<L: PlonkParameters<D>, const D: usize> SimpleGenerator<L::Field, D>
BeaconValidatorGeneratorInput::IndexConst(idx) => self
.client
.get_validator(hex!(block_root), *idx)
.await
.expect("failed to get validator"),
BeaconValidatorGeneratorInput::IndexVariable(idx) => {
let idx = idx.get(witness).as_u64();
self.client
.get_validator(hex!(block_root), idx)
.await
.expect("failed to get validator")
}
BeaconValidatorGeneratorInput::PubkeyVariable(pubkey) => {
let pubkey = hex!(pubkey.get(witness));
self.client
.get_validator_by_pubkey(hex!(block_root), pubkey)
.await
.expect("failed to get validator")
}
}
Expand Down
28 changes: 28 additions & 0 deletions plonky2x/src/frontend/eth/beacon/generators/validator_witness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::env;

use serde::{Deserialize, Serialize};

use crate::frontend::eth::beacon::vars::BeaconValidatorVariable;
use crate::frontend::generator::simple::hint::Hint;
use crate::frontend::uint::uint64::U64Variable;
use crate::frontend::vars::ValueStream;
use crate::prelude::{Bytes32Variable, PlonkParameters};
use crate::utils::eth::beacon::BeaconClient;
use crate::utils::hex;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BeaconValidatorWitnessGenerator {}

impl<L: PlonkParameters<D>, const D: usize> Hint<L, D> for BeaconValidatorWitnessGenerator {
fn hint(&self, input_stream: &mut ValueStream<L, D>, output_stream: &mut ValueStream<L, D>) {
let client = BeaconClient::new(env::var("CONSENSUS_RPC_1").unwrap());
let header_root = input_stream.read_value::<Bytes32Variable>();
let validator_index = input_stream.read_value::<U64Variable>();

let response = client
.get_validator_witness(hex!(header_root), validator_index.as_u64())
.unwrap();

output_stream.write_value::<BeaconValidatorVariable>(response.validator);
}
}
1 change: 0 additions & 1 deletion plonky2x/src/frontend/eth/beacon/generators/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl<L: PlonkParameters<D>, const D: usize> SimpleGenerator<L::Field, D>
let result = rt.block_on(async {
self.client
.get_validators_root(hex!(block_root.as_bytes()).to_string())
.await
.expect("failed to get validators root")
});

Expand Down
1 change: 0 additions & 1 deletion plonky2x/src/frontend/eth/beacon/generators/withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ impl<L: PlonkParameters<D>, const D: usize> SimpleGenerator<L::Field, D>
let result = rt.block_on(async {
self.client
.get_withdrawal(hex!(block_root.as_bytes()).to_string(), idx.as_u64())
.await
.expect("failed to get validators root")
});

Expand Down
1 change: 0 additions & 1 deletion plonky2x/src/frontend/eth/beacon/generators/withdrawals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl<L: PlonkParameters<D>, const D: usize> SimpleGenerator<L::Field, D>
let result = rt.block_on(async {
self.client
.get_withdrawals_root(hex!(block_root.as_bytes()).to_string())
.await
.expect("failed to get validators root")
});

Expand Down
Loading

0 comments on commit d63726d

Please sign in to comment.