This repository was archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(API): init darwinia API * feat(darwinia): stable last_confirmed API * feat(api): equip submit_proposal api with borrow error * fix(pool): add mutex lock to transaction pool * fix(pool): drop lock in relay service to avoid dead lock * feat(darwinia): add relay_proposals api * feat(darwinia): check the current proposals while submitting proposal * feat(darwinia): add should_redeem api * chore(git): update the Cargo.lock
- Loading branch information
Showing
15 changed files
with
552 additions
and
218 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,148 @@ | ||
//! Darwinia API | ||
use crate::{ | ||
pool::EthereumTransaction, | ||
result::{Error, Result}, | ||
Config, | ||
}; | ||
use primitives::{ | ||
chain::eth::{HeaderStuff, PendingHeader}, | ||
frame::ethereum::{ | ||
backing::VerifiedProofStoreExt, | ||
game::{PendingHeadersStoreExt, RelayProposalT, RelayProposalsStoreExt}, | ||
relay::{ConfirmedBlockNumbersStoreExt, SubmitProposalCallExt}, | ||
}, | ||
runtime::DarwiniaRuntime, | ||
}; | ||
use sp_keyring::sr25519::sr25519::Pair; | ||
use substrate_subxt::{sp_core::Pair as PairTrait, Client, ClientBuilder, PairSigner}; | ||
use web3::types::H256; | ||
|
||
/// Dawrinia API | ||
pub struct Darwinia { | ||
client: Client<DarwiniaRuntime>, | ||
/// Keyring signer | ||
pub signer: PairSigner<DarwiniaRuntime, Pair>, | ||
} | ||
|
||
impl Darwinia { | ||
/// New darwinia API | ||
pub async fn new(config: &Config) -> Result<Darwinia> { | ||
let pair = Pair::from_string(&config.seed, None).unwrap(); | ||
let signer = PairSigner::<DarwiniaRuntime, Pair>::new(pair); | ||
let client = ClientBuilder::<DarwiniaRuntime>::new() | ||
.set_url(&config.node) | ||
.build() | ||
.await?; | ||
|
||
Ok(Darwinia { client, signer }) | ||
} | ||
|
||
/// Get relay proposals | ||
pub async fn relay_proposals(&self) -> Result<Vec<RelayProposalT>> { | ||
Ok(self.client.relay_proposals(None).await?) | ||
} | ||
|
||
/// Get current proposals | ||
pub async fn current_proposals(&self) -> Result<Vec<u64>> { | ||
let proposals = self.relay_proposals().await?; | ||
let mut blocks = vec![]; | ||
for p in proposals { | ||
blocks.append( | ||
&mut p | ||
.bonded_proposal | ||
.iter() | ||
.map(|bp| bp.1.header.number) | ||
.collect(), | ||
) | ||
} | ||
|
||
Ok(blocks) | ||
} | ||
|
||
/// Get confirmed block numbers | ||
pub async fn confirmed_block_numbers(&self) -> Result<Vec<u64>> { | ||
Ok(self.client.confirmed_block_numbers(None).await?) | ||
} | ||
|
||
/// Get the last confirmed block | ||
pub async fn last_confirmed(&self) -> Result<u64> { | ||
Ok( | ||
if let Some(confirmed) = self.confirmed_block_numbers().await?.iter().max() { | ||
*confirmed | ||
} else { | ||
0 | ||
}, | ||
) | ||
} | ||
|
||
/// Get pending headers | ||
pub async fn pending_headers(&self) -> Result<Vec<PendingHeader>> { | ||
Ok(self.client.pending_headers(None).await?) | ||
} | ||
|
||
/// Submit Proposal | ||
pub async fn submit_proposal(&self, proposal: Vec<HeaderStuff>) -> Result<H256> { | ||
Ok(self.client.submit_proposal(&self.signer, proposal).await?) | ||
} | ||
|
||
/// Check if should redeem | ||
pub async fn should_redeem(&self, tx: EthereumTransaction) -> Result<()> { | ||
if let Some(res) = self | ||
.client | ||
.verified_proof(tx.hash(), tx.index, None) | ||
.await? | ||
{ | ||
if res { | ||
Err(Error::Bridger(format!( | ||
"The tx {:?} has been redeemed", | ||
tx.hash, | ||
))) | ||
} else { | ||
Ok(()) | ||
} | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Check if should relay | ||
pub async fn should_relay(&self, target: u64) -> Result<u64> { | ||
let last_confirmed = self.last_confirmed().await?; | ||
if target <= last_confirmed { | ||
return Err(Error::Bridger(format!( | ||
"The target block {} is not greater than the last confirmed {}", | ||
target, last_confirmed, | ||
))); | ||
} | ||
|
||
// Check if confirmed | ||
let confirmed_blocks = self.confirmed_block_numbers().await?; | ||
if confirmed_blocks.contains(&target) { | ||
return Err(Error::Bridger(format!( | ||
"The target block {} has already been submitted", | ||
target, | ||
))); | ||
} | ||
|
||
// Check if the target block is pending | ||
let pending_headers = self.pending_headers().await?; | ||
for p in pending_headers { | ||
if p.1 == target { | ||
return Err(Error::Bridger(format!( | ||
"The target block {} is pending", | ||
target, | ||
))); | ||
} | ||
} | ||
|
||
// Check if the target block is in relayer game | ||
let proposals = self.current_proposals().await?; | ||
if proposals.contains(&target) { | ||
return Err(Error::Bridger(format!( | ||
"The target block {} has been in relayer game", | ||
target, | ||
))); | ||
} | ||
Ok(last_confirmed) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
//! Briger APIs | ||
mod darwinia; | ||
mod shadow; | ||
|
||
pub use shadow::Shadow; | ||
pub use self::{darwinia::Darwinia, shadow::Shadow}; |
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
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 |
---|---|---|
|
@@ -6,7 +6,6 @@ extern crate log; | |
|
||
mod config; | ||
mod listener; | ||
mod runtime; | ||
|
||
pub mod api; | ||
pub mod cmd; | ||
|
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 |
---|---|---|
@@ -1,17 +1,53 @@ | ||
//! Transaction pool | ||
use std::cmp::{Ord, Ordering, PartialOrd}; | ||
use web3::types::H256; | ||
|
||
/// Reedeemable Ethereum transactions | ||
pub enum EthereumTransaction { | ||
/// Ethereum transaction event with hash | ||
#[derive(PartialEq, Eq, Debug)] | ||
pub enum EthereumTransactionHash { | ||
/// Deposit event | ||
Deposit(H256), | ||
/// Token event | ||
Token(H256), | ||
} | ||
|
||
/// Reedeemable Ethereum transaction | ||
#[derive(PartialEq, Eq)] | ||
pub struct EthereumTransaction { | ||
/// Transaction event with hash | ||
pub hash: EthereumTransactionHash, | ||
/// Transaction block | ||
pub block: u64, | ||
/// Transaction index | ||
pub index: u64, | ||
} | ||
|
||
impl EthereumTransaction { | ||
/// Get the hash | ||
pub fn hash(&self) -> [u8; 32] { | ||
match self.hash { | ||
EthereumTransactionHash::Token(h) => h, | ||
EthereumTransactionHash::Deposit(h) => h, | ||
} | ||
.to_fixed_bytes() | ||
} | ||
} | ||
|
||
impl PartialOrd for EthereumTransaction { | ||
fn partial_cmp(&self, o: &Self) -> Option<Ordering> { | ||
self.block.partial_cmp(&o.block) | ||
} | ||
} | ||
|
||
impl Ord for EthereumTransaction { | ||
fn cmp(&self, o: &Self) -> Ordering { | ||
self.block.cmp(&o.block) | ||
} | ||
} | ||
|
||
/// Transaction pool | ||
#[derive(Default)] | ||
pub struct Pool { | ||
/// Ethereum transactions | ||
pub eth: Vec<EthereumTransaction>, | ||
pub ethereum: Vec<EthereumTransaction>, | ||
} |
Oops, something went wrong.