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
Feemarket strategy #308
Merged
Merged
Feemarket strategy #308
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
146a710
Update dependencies
fewensa 4289f54
fix compile
fewensa af0448f
Update dependencies
fewensa cd3a2e5
Add custom relay strategy
fewensa 123b7e3
test client substrate
fewensa 3ede4cb
test client substrate
fewensa 51da5f6
Update lock
fewensa 20ac672
Update lock
fewensa c5822a3
fee market strategy
fewensa fe4351a
clippy
fewensa a6bce9b
Update dependencies
fewensa d698f94
test and fix api bug
fewensa 70266e8
Remove unused dependency
fewensa e4ec0bd
fix test
fewensa 9aecf1d
Update dependencies
fewensa 962f7ba
Merge remote-tracking branch 'origin/master' into darwinia-feemarket-…
fewensa 6b2e427
Merge branch 'master' into darwinia-feemarket-strategy
hackfisher 37abc2f
Update dependencies
fewensa 4634e76
New api: `update_relay_fee` `update_locked_collateral`
fewensa f7c7af9
crazy strategy
fewensa b9bf5a0
subscan component
fewensa c267f44
price api
fewensa c4358ed
add reasonable strategy (unfinished), fix compile
fewensa 38511ba
update fee service
fewensa 81789db
use blocking api
fewensa 81ffefa
test update fee
fewensa a689a33
Update lock
fewensa 5e0b712
Merge remote-tracking branch 'origin/master' into darwinia-feemarket-…
fewensa 432de8e
Update dependencies
fewensa e95ed51
Update dependencies
fewensa d471d7d
Update storage key
fewensa b26fb6e
test update fee
fewensa 911c5da
clippy
fewensa 8102da7
docs
fewensa 9352ecf
docs
fewensa 36928ae
fix tests
fewensa 95c4cdd
Update dependencies
fewensa 44b04a7
Merge remote-tracking branch 'origin/master' into darwinia-feemarket-…
fewensa e50477a
fix conflicts
fewensa fcd8e19
Just test
fewensa ef45a60
Revert CI
fewensa e214e97
Change use futures_timer instead thread sleep
fewensa 0050860
Merge remote-tracking branch 'origin/master' into darwinia-feemarket-…
fewensa db9df97
fix conflicts
fewensa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,160 @@ | ||
| use bp_messages::{LaneId, MessageNonce}; | ||
| use codec::Encode; | ||
| use common_primitives::AccountId; | ||
| use common_primitives::Balance; | ||
| use common_primitives::BlockNumber; | ||
| use dp_fee::{Order, Relayer}; | ||
| use relay_substrate_client::{ChainBase, Client, TransactionSignScheme, UnsignedTransaction}; | ||
| use sp_core::storage::StorageKey; | ||
| use sp_core::{Bytes, Pair}; | ||
|
|
||
| use crate::{patch, PangolinChain}; | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct PangolinApi { | ||
| client: Client<PangolinChain>, | ||
| } | ||
|
|
||
| impl PangolinApi { | ||
| pub fn new(client: Client<PangolinChain>) -> Self { | ||
| Self { client } | ||
| } | ||
| } | ||
|
|
||
| impl PangolinApi { | ||
| pub async fn client(&self) -> &Client<PangolinChain> { | ||
| &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)) | ||
| } | ||
|
|
||
| /// 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)) | ||
| } | ||
|
|
||
| /// Query relayer info by account id | ||
| pub async fn relayer( | ||
| &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?) | ||
| } | ||
|
|
||
| pub async fn is_relayer(&self, account: AccountId) -> anyhow::Result<bool> { | ||
| self.relayer(account).await.map(|item| item.is_some()) | ||
| } | ||
|
|
||
| /// Query order | ||
| pub async fn order( | ||
| &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?) | ||
| } | ||
|
|
||
| /// 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?) | ||
| } | ||
|
|
||
| /// Update relay fee | ||
| pub async fn update_relay_fee( | ||
| &self, | ||
| signer: <PangolinChain as TransactionSignScheme>::AccountKeyPair, | ||
| amount: <PangolinChain as ChainBase>::Balance, | ||
| ) -> anyhow::Result<()> { | ||
| let signer_id = (*signer.public().as_array_ref()).into(); | ||
| let genesis_hash = *self.client.genesis_hash(); | ||
| self.client | ||
| .submit_signed_extrinsic(signer_id, move |_, transaction_nonce| { | ||
| Bytes( | ||
| PangolinChain::sign_transaction( | ||
| genesis_hash, | ||
| &signer, | ||
| relay_substrate_client::TransactionEra::immortal(), | ||
| UnsignedTransaction::new( | ||
| pangolin_runtime::FeeMarketCall::update_relay_fee(amount).into(), | ||
| transaction_nonce, | ||
| ), | ||
| ) | ||
| .encode(), | ||
| ) | ||
| }) | ||
| .await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Update locked collateral | ||
| pub async fn update_locked_collateral( | ||
| &self, | ||
| signer: <PangolinChain as TransactionSignScheme>::AccountKeyPair, | ||
| amount: <PangolinChain as ChainBase>::Balance, | ||
| ) -> anyhow::Result<()> { | ||
| let signer_id = (*signer.public().as_array_ref()).into(); | ||
| let genesis_hash = *self.client.genesis_hash(); | ||
| self.client | ||
| .submit_signed_extrinsic(signer_id, move |_, transaction_nonce| { | ||
| Bytes( | ||
| PangolinChain::sign_transaction( | ||
| genesis_hash, | ||
| &signer, | ||
| relay_substrate_client::TransactionEra::immortal(), | ||
| UnsignedTransaction::new( | ||
| pangolin_runtime::FeeMarketCall::update_locked_collateral(amount) | ||
| .into(), | ||
| transaction_nonce, | ||
| ), | ||
| ) | ||
| .encode(), | ||
| ) | ||
| }) | ||
| .await?; | ||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or 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,109 @@ | ||
| use std::ops::Range; | ||
|
|
||
| use common_primitives::AccountId; | ||
| use messages_relay::message_lane::MessageLane; | ||
| use messages_relay::message_lane_loop::{ | ||
| SourceClient as MessageLaneSourceClient, TargetClient as MessageLaneTargetClient, | ||
| }; | ||
| use messages_relay::relay_strategy::{RelayReference, RelayStrategy}; | ||
| use relay_substrate_client::Client; | ||
|
|
||
| use crate::api::PangolinApi; | ||
| use crate::PangolinChain; | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct PangolinRelayStrategy { | ||
| api: PangolinApi, | ||
| account: AccountId, | ||
| } | ||
|
|
||
| impl PangolinRelayStrategy { | ||
| pub fn new(client: Client<PangolinChain>, account: AccountId) -> Self { | ||
| Self { | ||
| api: PangolinApi::new(client), | ||
| account, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl RelayStrategy for PangolinRelayStrategy { | ||
| async fn decide< | ||
| P: MessageLane, | ||
| SourceClient: MessageLaneSourceClient<P>, | ||
| TargetClient: MessageLaneTargetClient<P>, | ||
| >( | ||
| &self, | ||
| reference: &mut RelayReference<P, SourceClient, TargetClient>, | ||
| ) -> bool { | ||
| let nonce = &reference.nonce; | ||
| let order = self | ||
| .api | ||
| .order(bridge_primitives::PANGORO_PANGOLIN_LANE, *nonce) | ||
| .await | ||
| .map_err(|e| { | ||
| log::error!("Failed to query order: {:?}", e); | ||
| }) | ||
| .unwrap_or(None); | ||
|
|
||
| // If the order is not exists. | ||
| // 1. You are too behind. | ||
| // 2. The network question | ||
| // So, you can skip this currently | ||
| // Related: https://github.com/darwinia-network/darwinia-common/blob/90add536ed320ec7e17898e695c65ee9d7ce79b0/frame/fee-market/src/lib.rs?#L177 | ||
| if order.is_none() { | ||
| return false; | ||
| } | ||
| // ----- | ||
|
|
||
| let order = order.unwrap(); | ||
| let relayers = order.relayers; | ||
|
|
||
| // If not have any assigned relayers, everyone participates in the relay. | ||
| if relayers.is_empty() { | ||
| return true; | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| let prior_relayer = relayers.iter().find(|item| item.id == self.account); | ||
| let is_assigned_relayer = prior_relayer.is_some(); | ||
|
|
||
| // If you are assigned relayer, you must relay this nonce. | ||
| // If you don't do that, the fee market pallet will slash your deposit. | ||
| // Even though it is a timeout, although it will slash your deposit after the timeout is delivered, | ||
| // you can still get relay rewards. | ||
| if is_assigned_relayer { | ||
| return true; | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // If you aren't assigned relayer, only participate in the part about time out, earn more rewards | ||
| let latest_block_number = self | ||
| .api | ||
| .best_finalized_header_number() | ||
| .await | ||
| .map_err(|e| { | ||
| log::error!( | ||
| "Failed to query latest block, unable to decide whether to participate: {:?}", | ||
| e | ||
| ); | ||
| }) | ||
| .unwrap_or(0); | ||
| let ranges = relayers | ||
| .iter() | ||
| .map(|item| item.valid_range.clone()) | ||
| .collect::<Vec<Range<common_primitives::BlockNumber>>>(); | ||
|
|
||
| let mut maximum_timeout = 0; | ||
| for range in ranges { | ||
| maximum_timeout = std::cmp::max(maximum_timeout, range.end); | ||
| } | ||
| // If this order has timed out, decide to relay | ||
| if latest_block_number > maximum_timeout { | ||
| return true; | ||
| } | ||
| false | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.