-
Notifications
You must be signed in to change notification settings - Fork 253
Domain chain network stack updates #2999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fec2aed
Modify DomainsAPI
shamil-gadelshin 8d3bb30
Add networking for obtaining domain block receipt
shamil-gadelshin c686d71
Restore block and state requests for domains
shamil-gadelshin 2796912
Disable domain sync
shamil-gadelshin 3894eee
Fix cargo fmt errors.
shamil-gadelshin 0741642
Refactor request handler for the last confirmed domain block.
shamil-gadelshin e456ef1
Merge branch 'main' into domain-networking-updates
shamil-gadelshin 5bb3f51
Refactor execution receipt acquisition.
shamil-gadelshin 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
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,229 @@ | ||
| // Remove after adding domain snap-sync | ||
| #![allow(dead_code)] | ||
|
|
||
| use crate::domains::request_handler::{ | ||
| generate_protocol_name, LastConfirmedBlockRequest, LastConfirmedBlockResponse, | ||
| }; | ||
| use async_trait::async_trait; | ||
| use domain_runtime_primitives::Balance; | ||
| use futures::channel::oneshot; | ||
| use parity_scale_codec::{Decode, Encode}; | ||
| use sc_network::{IfDisconnected, NetworkRequest, PeerId, RequestFailure}; | ||
| use sc_network_sync::SyncingService; | ||
| use sp_blockchain::HeaderBackend; | ||
| use sp_domains::{DomainId, ExecutionReceiptFor}; | ||
| use sp_runtime::traits::{Block as BlockT, Header}; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
| use tokio::time::sleep; | ||
| use tracing::{debug, error, trace}; | ||
|
|
||
| pub(crate) mod request_handler; | ||
|
|
||
| const REQUEST_PAUSE: Duration = Duration::from_secs(5); | ||
|
|
||
| /// Last confirmed domain block info error | ||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum LastConfirmedDomainBlockResponseError { | ||
| #[error("Last confirmed domain block info request failed: {0}")] | ||
| RequestFailed(#[from] RequestFailure), | ||
|
|
||
| #[error("Last confirmed domain block info request canceled")] | ||
| RequestCanceled, | ||
|
|
||
| #[error("Last confirmed domain block info request failed: invalid protocol")] | ||
| InvalidProtocol, | ||
|
|
||
| #[error("Failed to decode response: {0}")] | ||
| DecodeFailed(String), | ||
| } | ||
|
|
||
| #[async_trait] | ||
| pub trait LastDomainBlockReceiptProvider<Block: BlockT, CBlock: BlockT>: Send { | ||
| async fn get_execution_receipt( | ||
| &self, | ||
| block_hash: Option<CBlock::Hash>, | ||
| ) -> Option<ExecutionReceiptFor<Block::Header, CBlock, Balance>>; | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl<Block: BlockT, CBlock: BlockT> LastDomainBlockReceiptProvider<Block, CBlock> for () { | ||
| async fn get_execution_receipt( | ||
| &self, | ||
| _: Option<CBlock::Hash>, | ||
| ) -> Option<ExecutionReceiptFor<Block::Header, CBlock, Balance>> { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl<Block, CBlock, Client, NR> LastDomainBlockReceiptProvider<Block, CBlock> | ||
| for LastDomainBlockInfoReceiver<Block, Client, NR> | ||
| where | ||
| Block: BlockT, | ||
| CBlock: BlockT, | ||
| NR: NetworkRequest + Sync + Send, | ||
| Client: HeaderBackend<Block>, | ||
| { | ||
| async fn get_execution_receipt( | ||
| &self, | ||
| block_hash: Option<CBlock::Hash>, | ||
| ) -> Option<ExecutionReceiptFor<Block::Header, CBlock, Balance>> { | ||
| self.get_last_confirmed_domain_block_receipt::<CBlock>(block_hash) | ||
| .await | ||
| } | ||
| } | ||
|
|
||
| pub struct LastDomainBlockInfoReceiver<Block, Client, NR> | ||
| where | ||
| Block: BlockT, | ||
| NR: NetworkRequest, | ||
| Client: HeaderBackend<Block>, | ||
| { | ||
| domain_id: DomainId, | ||
| fork_id: Option<String>, | ||
| client: Arc<Client>, | ||
| network_service: NR, | ||
| sync_service: Arc<SyncingService<Block>>, | ||
| } | ||
|
|
||
| impl<Block, Client, NR> LastDomainBlockInfoReceiver<Block, Client, NR> | ||
| where | ||
| Block: BlockT, | ||
| NR: NetworkRequest, | ||
| Client: HeaderBackend<Block>, | ||
| { | ||
| pub fn new( | ||
| domain_id: DomainId, | ||
| fork_id: Option<String>, | ||
| client: Arc<Client>, | ||
| network_service: NR, | ||
| sync_service: Arc<SyncingService<Block>>, | ||
| ) -> Self { | ||
| Self { | ||
| domain_id, | ||
| fork_id, | ||
| client, | ||
| network_service, | ||
| sync_service, | ||
| } | ||
| } | ||
| pub async fn get_last_confirmed_domain_block_receipt<CBlock: BlockT>( | ||
| &self, | ||
| block_hash: Option<CBlock::Hash>, | ||
| ) -> Option<ExecutionReceiptFor<Block::Header, CBlock, Balance>> { | ||
| let info = self.client.info(); | ||
| let protocol_name = generate_protocol_name(info.genesis_hash, self.fork_id.as_deref()); | ||
|
|
||
| debug!(domain_id=%self.domain_id, %protocol_name, "Started obtaining domain info..."); | ||
|
|
||
| loop { | ||
| let peers_info = match self.sync_service.peers_info().await { | ||
| Ok(peers_info) => peers_info, | ||
| Err(error) => { | ||
| error!("Peers info request returned an error: {error}",); | ||
| sleep(REQUEST_PAUSE).await; | ||
|
|
||
| continue; | ||
| } | ||
| }; | ||
|
|
||
| // Enumerate peers until we find a suitable source for domain info | ||
| 'peers: for (peer_id, peer_info) in peers_info.iter() { | ||
| debug!( | ||
| "Domain data request. peer = {peer_id}, info = {:?}", | ||
| peer_info | ||
| ); | ||
|
|
||
| if !peer_info.is_synced { | ||
| trace!("Domain data request skipped (not synced). peer = {peer_id}"); | ||
|
|
||
| continue 'peers; | ||
| } | ||
|
|
||
| let request = LastConfirmedBlockRequest::<CBlock> { | ||
| domain_id: self.domain_id, | ||
| block_hash, | ||
| }; | ||
|
|
||
| let response = send_request::<NR, CBlock, Block::Header>( | ||
| protocol_name.clone(), | ||
| *peer_id, | ||
| request, | ||
| &self.network_service, | ||
| ) | ||
| .await; | ||
|
|
||
| match response { | ||
| Ok(response) => { | ||
| trace!("Response from a peer {peer_id},",); | ||
|
|
||
| return Some(response.last_confirmed_block_receipt); | ||
| } | ||
| Err(error) => { | ||
| debug!("Domain info request failed. peer = {peer_id}: {error}"); | ||
|
|
||
| continue 'peers; | ||
| } | ||
| } | ||
| } | ||
| debug!( | ||
| domain_id=%self.domain_id, | ||
| "No synced peers to handle the domain confirmed block infor request. Pausing..." | ||
| ); | ||
|
|
||
| sleep(REQUEST_PAUSE).await; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async fn send_request<NR: NetworkRequest, Block: BlockT, DomainHeader: Header>( | ||
| protocol_name: String, | ||
| peer_id: PeerId, | ||
| request: LastConfirmedBlockRequest<Block>, | ||
| network_service: &NR, | ||
| ) -> Result<LastConfirmedBlockResponse<Block, DomainHeader>, LastConfirmedDomainBlockResponseError> | ||
| { | ||
| let (tx, rx) = oneshot::channel(); | ||
|
|
||
| debug!("Sending request: {request:?} (peer={peer_id})"); | ||
|
|
||
| let encoded_request = request.encode(); | ||
|
|
||
| network_service.start_request( | ||
| peer_id, | ||
| protocol_name.clone().into(), | ||
| encoded_request, | ||
| None, | ||
| tx, | ||
| IfDisconnected::ImmediateError, | ||
| ); | ||
|
|
||
| let result = rx | ||
| .await | ||
| .map_err(|_| LastConfirmedDomainBlockResponseError::RequestCanceled)?; | ||
|
|
||
| match result { | ||
| Ok((data, response_protocol_name)) => { | ||
| if response_protocol_name != protocol_name.into() { | ||
| return Err(LastConfirmedDomainBlockResponseError::InvalidProtocol); | ||
| } | ||
|
|
||
| let response = decode_response(&data) | ||
| .map_err(LastConfirmedDomainBlockResponseError::DecodeFailed)?; | ||
|
|
||
| Ok(response) | ||
| } | ||
| Err(error) => Err(error.into()), | ||
| } | ||
| } | ||
|
|
||
| fn decode_response<Block: BlockT, DomainHeader: Header>( | ||
| mut response: &[u8], | ||
| ) -> Result<LastConfirmedBlockResponse<Block, DomainHeader>, String> { | ||
| let response = LastConfirmedBlockResponse::decode(&mut response).map_err(|error| { | ||
| format!("Failed to decode last confirmed domain block info response: {error}") | ||
| })?; | ||
|
|
||
| Ok(response) | ||
| } | ||
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.