Skip to content

Commit

Permalink
feat: add onchain delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Nov 5, 2024
1 parent da1d87c commit d39396f
Show file tree
Hide file tree
Showing 18 changed files with 634 additions and 137 deletions.
353 changes: 350 additions & 3 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions crates/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ redis.workspace = true
# Ethereum Types
reth-primitives.workspace = true
ethereum-consensus.workspace = true
alloy-sol-types = "0.8.8"
alloy-contract ="0.5.2"
alloy-provider = { version = "0.5.2", features = ["reqwest"], default-features = false }
alloy-transport = "0.5.2"

# Testing and Mocking
serial_test.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/constraints/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use uuid::Uuid;

use crate::constraints::error::ConstraintsApiError;

pub(crate) const MAX_GATEWAY_ELECTION_SIZE: usize = 1024 * 1024; // TODO: this should be a fixed size that we calc
// pub(crate) const MAX_GATEWAY_ELECTION_SIZE: usize = 1024 * 1024; // TODO: this should be a fixed size that we calc
pub(crate) const MAX_SET_CONSTRAINTS_SIZE: usize = 1024 * 1024; // TODO: this should be a fixed size that we calc

/// Information about the current head slot and next elected gateway.
Expand Down
78 changes: 78 additions & 0 deletions crates/api/src/delegation/contract_delegation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::sync::Arc;

use alloy_provider::{
fillers::{BlobGasFiller, ChainIdFiller, FillProvider, GasFiller, JoinFill, NonceFiller},
network::Ethereum,
Identity, RootProvider,
};
use alloy_sol_types::sol;
use alloy_transport::BoxTransport;
use ethereum_consensus::primitives::{BlsPublicKey, BlsSignature};
use helix_common::api::constraints_api::{PreconferElection, SignedPreconferElection};
use reth_primitives::{Address, Bytes};
use TaiyiCore::TaiyiCoreInstance;

use super::{error::Error, traits::DelegationTrait};

const DEFAULT_GAS_LIMIT: u64 = 100_000;

type RecommendProvider = FillProvider<
JoinFill<Identity, JoinFill<GasFiller, JoinFill<BlobGasFiller, JoinFill<NonceFiller, ChainIdFiller>>>>,
RootProvider<BoxTransport>,
BoxTransport,
Ethereum,
>;
sol! {
#[derive(Debug)]
struct PreconferElectionRes {
bytes validatorPubkey;
bytes preconferPubkey;
uint256 chainId;
address preconferAddress;
}

#[sol(rpc)]
contract TaiyiCore {
#[derive(Debug)]
function getPreconferElection(bytes calldata validatorPubKey) external view returns (PreconferElectionRes memory);
}
}

#[derive(Clone)]
pub struct ContractDelegation {
delegation_contract_address: Address,
provider: RecommendProvider,
}

impl ContractDelegation {
pub fn new(delegation_contract_address: Address, provider: RecommendProvider) -> Self {
Self { delegation_contract_address, provider }
}
}

#[async_trait::async_trait]
impl DelegationTrait for ContractDelegation {
async fn get_preconfer_election(&self, validator_pubkey: &BlsPublicKey, slot: u64) -> Result<Option<SignedPreconferElection>, Error> {
let pubkey = Bytes::from(validator_pubkey.as_ref().to_vec());
let taiyi_core_contract = Arc::new(TaiyiCoreInstance::new(self.delegation_contract_address, self.provider.clone()));
let preconfer_election =
taiyi_core_contract.getPreconferElection(pubkey).call().await.map_err(|e| Error::GetPreconferElection(e.to_string()))?;
if preconfer_election._0.preconferAddress.is_zero() {
return Ok(None);
}
let signed_preconfer_election = SignedPreconferElection {
message: PreconferElection {
slot_number: slot,
preconfer_pubkey: BlsPublicKey::try_from(preconfer_election._0.preconferPubkey.as_ref())
.map_err(|e| Error::BlsPublicKey(e.to_string()))?,
proposer_pubkey: BlsPublicKey::try_from(preconfer_election._0.validatorPubkey.as_ref())
.map_err(|e| Error::BlsPublicKey(e.to_string()))?,
chain_id: preconfer_election._0.chainId.to(),
// gas limit is not used
gas_limit: DEFAULT_GAS_LIMIT,
},
signature: BlsSignature::default(),
};
Ok(Some(signed_preconfer_election))
}
}
7 changes: 7 additions & 0 deletions crates/api/src/delegation/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("error getting preconfer election: {0}")]
GetPreconferElection(String),
#[error("error parsing bls public key: {0}")]
BlsPublicKey(String),
}
15 changes: 15 additions & 0 deletions crates/api/src/delegation/mock_delegation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use async_trait::async_trait;
use ethereum_consensus::primitives::BlsPublicKey;
use helix_common::api::constraints_api::SignedPreconferElection;

use super::{error::Error, traits::DelegationTrait};

#[derive(Clone, Default)]
pub struct MockDelegation {}

#[async_trait]
impl DelegationTrait for MockDelegation {
async fn get_preconfer_election(&self, _validator_pubkey: &BlsPublicKey, _slot: u64) -> Result<Option<SignedPreconferElection>, Error> {
Ok(None)
}
}
7 changes: 7 additions & 0 deletions crates/api/src/delegation/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod contract_delegation;
pub mod error;
pub mod mock_delegation;
pub mod traits;

pub use contract_delegation::ContractDelegation;
pub use mock_delegation::MockDelegation;
11 changes: 11 additions & 0 deletions crates/api/src/delegation/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use async_trait::async_trait;
use ethereum_consensus::primitives::BlsPublicKey;
use helix_common::api::constraints_api::SignedPreconferElection;

use super::error::Error;

#[async_trait]
#[auto_impl::auto_impl(Arc)]
pub trait DelegationTrait: Send + Sync + Clone {
async fn get_preconfer_election(&self, validator_pubkey: &BlsPublicKey, slot: u64) -> Result<Option<SignedPreconferElection>, Error>;
}
1 change: 1 addition & 0 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pub mod builder;
pub mod constraints;
pub mod delegation;
pub mod gossiper;
pub mod integration_tests;
pub mod middleware;
Expand Down
Loading

0 comments on commit d39396f

Please sign in to comment.