Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
753 changes: 305 additions & 448 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions components/client-crab-s2s/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ bp-messages = { git = "https://github.com/darwinia-network/parity-bri
bp-runtime = { git = "https://github.com/darwinia-network/parity-bridges-common.git", tag = "darwinia-v0.11.6-4" }

## Bridge dependencies
crab-runtime = { git = "https://github.com/darwinia-network/darwinia.git", tag = "v0.11.6" }
common-primitives = { git = "https://github.com/darwinia-network/darwinia.git", tag = "v0.11.6" }
bridge-primitives = { git = "https://github.com/darwinia-network/darwinia.git", tag = "v0.11.6" }
dp-fee = { git = "https://github.com/darwinia-network/darwinia-common.git", tag = "darwinia-v0.11.6-3" }
crab-runtime = { git = "https://github.com/darwinia-network/darwinia.git", tag = "v0.11.6-bridger" }
darwinia-common-primitives = { git = "https://github.com/darwinia-network/darwinia.git", tag = "v0.11.6-bridger" }
darwinia-bridge-primitives = { git = "https://github.com/darwinia-network/darwinia.git", tag = "v0.11.6-bridger" }
dp-fee = { git = "https://github.com/darwinia-network/darwinia-common.git", tag = "darwinia-v0.11.6-4" }



Expand Down
165 changes: 99 additions & 66 deletions components/client-crab-s2s/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use bp_messages::{LaneId, MessageNonce};
use codec::Encode;
use common_primitives::AccountId;
use common_primitives::Balance;
use common_primitives::BlockNumber;
use darwinia_common_primitives::AccountId;
use darwinia_common_primitives::Balance;
use darwinia_common_primitives::BlockNumber;
use dp_fee::{Order, Relayer};
use relay_substrate_client::{ChainBase, Client, TransactionSignScheme, UnsignedTransaction};
use relay_utils::relay_loop::Client as RelayLoopClient;
use relay_utils::MaybeConnectionError;
use sp_core::storage::StorageKey;
use sp_core::{Bytes, Pair};

Expand All @@ -22,96 +24,110 @@ impl CrabApi {
}

impl CrabApi {
pub async fn client(&self) -> &Client<CrabChain> {
&self.client
}

/// Query assigned relayers
pub async fn assigned_relayers(&self) -> anyhow::Result<Vec<Relayer<AccountId, Balance>>> {
Ok(self
.client
.storage_value(
StorageKey(
patch::storage_prefix("FeeMarket".as_bytes(), "AssignedRelayers".as_bytes())
.to_vec(),
),
None,
)
.await?
.unwrap_or_else(Vec::new))
pub async fn assigned_relayers(&mut self) -> anyhow::Result<Vec<Relayer<AccountId, Balance>>> {
let storage_key = StorageKey(
patch::storage_prefix("FeeMarket".as_bytes(), "AssignedRelayers".as_bytes()).to_vec(),
);
match self.client.storage_value(storage_key, None).await {
Ok(v) => Ok(v.unwrap_or_default()),
Err(e) => {
if e.is_connection_error() {
self.client.reconnect().await?;
}
Err(e.into())
}
}
}

/// Query order
pub async fn order(
&self,
&mut self,
laned_id: LaneId,
message_nonce: MessageNonce,
) -> anyhow::Result<Option<Order<AccountId, BlockNumber, Balance>>> {
Ok(self
.client
.storage_value(
bp_runtime::storage_map_final_key_blake2_128concat(
"FeeMarket",
"Orders",
(laned_id, message_nonce).encode().as_slice(),
),
None,
)
.await?)
let storage_key = bp_runtime::storage_map_final_key_blake2_128concat(
"FeeMarket",
"Orders",
(laned_id, message_nonce).encode().as_slice(),
);
match self.client.storage_value(storage_key.clone(), None).await {
Ok(v) => Ok(v),
Err(e) => {
if e.is_connection_error() {
self.client.reconnect().await?;
}
Err(e.into())
}
}
}

/// Query all relayers
pub async fn relayers(&self) -> anyhow::Result<Vec<AccountId>> {
Ok(self
.client
.storage_value(
StorageKey(
patch::storage_prefix("FeeMarket".as_bytes(), "Relayers".as_bytes()).to_vec(),
),
None,
)
.await?
.unwrap_or_else(Vec::new))
pub async fn relayers(&mut self) -> anyhow::Result<Vec<AccountId>> {
let storage_key = StorageKey(
patch::storage_prefix("FeeMarket".as_bytes(), "Relayers".as_bytes()).to_vec(),
);
match self.client.storage_value(storage_key, None).await {
Ok(v) => Ok(v.unwrap_or_default()),
Err(e) => {
if e.is_connection_error() {
self.client.reconnect().await?;
}
Err(e.into())
}
}
}

/// Query relayer info by account id
pub async fn relayer(
&self,
&mut self,
account: AccountId,
) -> anyhow::Result<Option<Relayer<AccountId, Balance>>> {
Ok(self
.client
.storage_value(
bp_runtime::storage_map_final_key_blake2_128concat(
"FeeMarket",
"RelayersMap",
account.encode().as_slice(),
),
None,
)
.await?)
let storage_key = bp_runtime::storage_map_final_key_blake2_128concat(
"FeeMarket",
"RelayersMap",
account.encode().as_slice(),
);
match self.client.storage_value(storage_key.clone(), None).await {
Ok(v) => Ok(v),
Err(e) => {
if e.is_connection_error() {
self.client.reconnect().await?;
}
Err(e.into())
}
}
}

pub async fn is_relayer(&self, account: AccountId) -> anyhow::Result<bool> {
pub async fn is_relayer(&mut self, account: AccountId) -> anyhow::Result<bool> {
self.relayer(account).await.map(|item| item.is_some())
}

/// Return number of the best finalized block.
pub async fn best_finalized_header_number(
&self,
) -> anyhow::Result<common_primitives::BlockNumber> {
Ok(self.client.best_finalized_header_number().await?)
&mut self,
) -> anyhow::Result<darwinia_common_primitives::BlockNumber> {
match self.client.best_finalized_header_number().await {
Ok(v) => Ok(v),
Err(e) => {
if e.is_connection_error() {
self.client.reconnect().await?;
}
Err(e.into())
}
}
}

/// Update relay fee
pub async fn update_relay_fee(
&self,
&mut self,
signer: <CrabChain as TransactionSignScheme>::AccountKeyPair,
amount: <CrabChain as ChainBase>::Balance,
) -> anyhow::Result<()> {
let signer_id = (*signer.public().as_array_ref()).into();
let genesis_hash = *self.client.genesis_hash();
self.client
match self
.client
.submit_signed_extrinsic(signer_id, move |_, transaction_nonce| {
Bytes(
CrabChain::sign_transaction(
Expand All @@ -126,19 +142,28 @@ impl CrabApi {
.encode(),
)
})
.await?;
Ok(())
.await
{
Ok(_v) => Ok(()),
Err(e) => {
if e.is_connection_error() {
self.client.reconnect().await?;
}
Err(e.into())
}
}
}

/// Update locked collateral
pub async fn update_locked_collateral(
&self,
&mut self,
signer: <CrabChain as TransactionSignScheme>::AccountKeyPair,
amount: <CrabChain as ChainBase>::Balance,
) -> anyhow::Result<()> {
let signer_id = (*signer.public().as_array_ref()).into();
let genesis_hash = *self.client.genesis_hash();
self.client
match self
.client
.submit_signed_extrinsic(signer_id, move |_, transaction_nonce| {
Bytes(
CrabChain::sign_transaction(
Expand All @@ -153,7 +178,15 @@ impl CrabApi {
.encode(),
)
})
.await?;
Ok(())
.await
{
Ok(_v) => Ok(()),
Err(e) => {
if e.is_connection_error() {
self.client.reconnect().await?;
}
Err(e.into())
}
}
}
}
34 changes: 19 additions & 15 deletions components/client-crab-s2s/src/crab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
use std::time::Duration;

/// Pangoro header id.
pub type HeaderId = relay_utils::HeaderId<common_primitives::Hash, common_primitives::BlockNumber>;
pub type HeaderId = relay_utils::HeaderId<
darwinia_common_primitives::Hash,
darwinia_common_primitives::BlockNumber,
>;

/// Pangoro chain definition.
#[derive(Debug, Clone, Copy)]
Expand All @@ -22,23 +25,24 @@ impl BridgeChain for CrabChain {
}

impl ChainBase for CrabChain {
type BlockNumber = common_primitives::BlockNumber;
type Hash = common_primitives::Hash;
type Hasher = common_primitives::Hashing;
type Header = common_primitives::Header;

type AccountId = common_primitives::AccountId;
type Balance = common_primitives::Balance;
type Index = common_primitives::Nonce;
type Signature = common_primitives::Signature;
type BlockNumber = darwinia_common_primitives::BlockNumber;
type Hash = darwinia_common_primitives::Hash;
type Hasher = darwinia_common_primitives::Hashing;
type Header = darwinia_common_primitives::Header;

type AccountId = darwinia_common_primitives::AccountId;
type Balance = darwinia_common_primitives::Balance;
type Index = darwinia_common_primitives::Nonce;
type Signature = darwinia_common_primitives::Signature;
}

impl Chain for CrabChain {
const NAME: &'static str = "Crab";
const AVERAGE_BLOCK_INTERVAL: Duration =
Duration::from_millis(common_primitives::MILLISECS_PER_BLOCK);
const STORAGE_PROOF_OVERHEAD: u32 = bridge_primitives::EXTRA_STORAGE_PROOF_SIZE;
const MAXIMAL_ENCODED_ACCOUNT_ID_SIZE: u32 = bridge_primitives::MAXIMAL_ENCODED_ACCOUNT_ID_SIZE;
Duration::from_millis(darwinia_common_primitives::MILLISECS_PER_BLOCK);
const STORAGE_PROOF_OVERHEAD: u32 = darwinia_bridge_primitives::EXTRA_STORAGE_PROOF_SIZE;
const MAXIMAL_ENCODED_ACCOUNT_ID_SIZE: u32 =
darwinia_bridge_primitives::MAXIMAL_ENCODED_ACCOUNT_ID_SIZE;

type SignedBlock = crab_runtime::SignedBlock;
type Call = crab_runtime::Call;
Expand Down Expand Up @@ -110,7 +114,7 @@ impl TransactionSignScheme for CrabChain {
tx.signature
.as_ref()
.map(|(address, _, _)| {
let account_id: common_primitives::AccountId =
let account_id: darwinia_common_primitives::AccountId =
(*signer.public().as_array_ref()).into();
*address == crab_runtime::Address::from(account_id)
})
Expand All @@ -135,4 +139,4 @@ impl TransactionSignScheme for CrabChain {
pub type SigningParams = sp_core::sr25519::Pair;

/// Pangoro header type used in headers sync.
pub type SyncHeader = relay_substrate_client::SyncHeader<common_primitives::Header>;
pub type SyncHeader = relay_substrate_client::SyncHeader<darwinia_common_primitives::Header>;
10 changes: 5 additions & 5 deletions components/client-crab-s2s/src/feemarket.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Range;

use common_primitives::AccountId;
use darwinia_common_primitives::AccountId;
use messages_relay::message_lane::MessageLane;
use messages_relay::message_lane_loop::{
SourceClient as MessageLaneSourceClient, TargetClient as MessageLaneTargetClient,
Expand Down Expand Up @@ -32,7 +32,7 @@ impl CrabRelayStrategy {
SourceClient: MessageLaneSourceClient<P>,
TargetClient: MessageLaneTargetClient<P>,
>(
&self,
&mut self,
reference: &mut RelayReference<P, SourceClient, TargetClient>,
) -> bool {
log::trace!(
Expand All @@ -42,7 +42,7 @@ impl CrabRelayStrategy {
let nonce = &reference.nonce;
let order = self
.api
.order(bridge_primitives::DARWINIA_CRAB_LANE, *nonce)
.order(darwinia_bridge_primitives::DARWINIA_CRAB_LANE, *nonce)
.await
.map_err(|e| {
log::error!("[Pangoro] Failed to query order: {:?}", e);
Expand Down Expand Up @@ -101,7 +101,7 @@ impl CrabRelayStrategy {
let ranges = relayers
.iter()
.map(|item| item.valid_range.clone())
.collect::<Vec<Range<common_primitives::BlockNumber>>>();
.collect::<Vec<Range<darwinia_common_primitives::BlockNumber>>>();

let mut maximum_timeout = 0;
for range in ranges {
Expand All @@ -126,7 +126,7 @@ impl RelayStrategy for CrabRelayStrategy {
SourceClient: MessageLaneSourceClient<P>,
TargetClient: MessageLaneTargetClient<P>,
>(
&self,
&mut self,
reference: &mut RelayReference<P, SourceClient, TargetClient>,
) -> bool {
let decide = self.handle(reference).await;
Expand Down
10 changes: 5 additions & 5 deletions components/client-darwinia-s2s/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ bp-messages = { git = "https://github.com/darwinia-network/parity-bri
bp-runtime = { git = "https://github.com/darwinia-network/parity-bridges-common.git", tag = "darwinia-v0.11.6-4" }

## Bridge dependencies
darwinia-runtime = { git = "https://github.com/darwinia-network/darwinia.git", tag = "v0.11.6" }
common-primitives = { git = "https://github.com/darwinia-network/darwinia.git", tag = "v0.11.6" }
bridge-primitives = { git = "https://github.com/darwinia-network/darwinia.git", tag = "v0.11.6" }
darwinia-bridge-ethereum = { git = "https://github.com/darwinia-network/darwinia-common.git", tag = "darwinia-v0.11.6-3" }
dp-fee = { git = "https://github.com/darwinia-network/darwinia-common.git", tag = "darwinia-v0.11.6-3" }
darwinia-runtime = { git = "https://github.com/darwinia-network/darwinia.git", tag = "v0.11.6-bridger" }
darwinia-common-primitives = { git = "https://github.com/darwinia-network/darwinia.git", tag = "v0.11.6-bridger" }
darwinia-bridge-primitives = { git = "https://github.com/darwinia-network/darwinia.git", tag = "v0.11.6-bridger" }
darwinia-bridge-ethereum = { git = "https://github.com/darwinia-network/darwinia-common.git", tag = "darwinia-v0.11.6-4" }
dp-fee = { git = "https://github.com/darwinia-network/darwinia-common.git", tag = "darwinia-v0.11.6-4" }



Expand Down
Loading