forked from gattaca-com/helix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
634 additions
and
137 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.