From ee113181753efe7da6d6b87de83bd3ebd994039b Mon Sep 17 00:00:00 2001 From: Mathieu Baudet <1105398+ma2bd@users.noreply.github.com> Date: Thu, 18 Jul 2024 12:55:29 +0200 Subject: [PATCH] use CryptoHash instead of ChainAndHeight in received_log --- linera-chain/src/chain.rs | 13 +++----- linera-chain/src/data_types.rs | 7 ---- linera-core/src/client.rs | 33 +++++-------------- linera-core/src/data_types.rs | 4 +-- linera-core/src/local_node.rs | 7 ++++ linera-core/src/unit_tests/worker_tests.rs | 17 ++++------ .../gql/service_requests.graphql | 5 +-- .../gql/service_schema.graphql | 16 ++------- 8 files changed, 32 insertions(+), 70 deletions(-) diff --git a/linera-chain/src/chain.rs b/linera-chain/src/chain.rs index 2828cc1c398..79a11eb624e 100644 --- a/linera-chain/src/chain.rs +++ b/linera-chain/src/chain.rs @@ -35,8 +35,8 @@ use {linera_base::identifiers::BytecodeId, linera_execution::BytecodeLocation}; use crate::{ data_types::{ - Block, BlockExecutionOutcome, ChainAndHeight, ChannelFullName, Event, EventRecord, - IncomingMessage, MessageAction, MessageBundle, Origin, OutgoingMessage, Target, + Block, BlockExecutionOutcome, ChannelFullName, Event, EventRecord, IncomingMessage, + MessageAction, MessageBundle, Origin, OutgoingMessage, Target, }, inbox::{Cursor, InboxError, InboxStateView}, manager::ChainManager, @@ -252,8 +252,8 @@ where /// Hashes of all certified blocks for this sender. /// This ends with `block_hash` and has length `usize::from(next_block_height)`. pub confirmed_log: LogView, - /// Sender chain and height of all certified blocks known as a receiver (local ordering). - pub received_log: LogView, + /// The hash of all the certified blocks known as a receiver (local ordering). + pub received_log: LogView, /// Mailboxes used to receive messages indexed by their origin. pub inboxes: ReentrantCollectionView>, @@ -630,10 +630,7 @@ where } } // Remember the certificate for future validator/client synchronizations. - self.received_log.push(ChainAndHeight { - chain_id: origin.sender, - height: bundle.height, - }); + self.received_log.push(bundle.hash); Ok(()) } diff --git a/linera-chain/src/data_types.rs b/linera-chain/src/data_types.rs index 3cee4260fa6..7b568a2bc12 100644 --- a/linera-chain/src/data_types.rs +++ b/linera-chain/src/data_types.rs @@ -94,13 +94,6 @@ impl Block { } } -/// A chain ID with a block height. -#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, SimpleObject)] -pub struct ChainAndHeight { - pub chain_id: ChainId, - pub height: BlockHeight, -} - /// A message received from a block of another chain. #[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize, SimpleObject)] pub struct IncomingMessage { diff --git a/linera-core/src/client.rs b/linera-core/src/client.rs index a0a1777c57e..41f47bc6142 100644 --- a/linera-core/src/client.rs +++ b/linera-core/src/client.rs @@ -54,9 +54,7 @@ use tokio_stream::wrappers::UnboundedReceiverStream; use tracing::{debug, error, info, Instrument as _}; use crate::{ - data_types::{ - BlockHeightRange, ChainInfo, ChainInfoQuery, ChainInfoResponse, ClientOutcome, RoundTimeout, - }, + data_types::{ChainInfo, ChainInfoQuery, ChainInfoResponse, ClientOutcome, RoundTimeout}, local_node::{LocalNodeClient, LocalNodeError}, node::{ CrossChainMessageDelivery, LocalValidatorNode, LocalValidatorNodeProvider, NodeError, @@ -1071,31 +1069,16 @@ where response.check(name)?; let mut certificates = Vec::new(); let mut new_tracker = tracker; - for entry in response.info.requested_received_log { - let query = ChainInfoQuery::new(entry.chain_id) - .with_sent_certificate_hashes_in_range(BlockHeightRange::single(entry.height)); - let local_response = node_client - .handle_chain_info_query(query.clone()) - .await - .map_err(|error| NodeError::LocalNodeQuery { + for hash in response.info.requested_received_log { + if node_client.has_certificate(hash).await.map_err(|error| { + NodeError::LocalNodeQuery { error: error.to_string(), - })?; - if !local_response - .info - .requested_sent_certificate_hashes - .is_empty() - { - new_tracker += 1; + } + })? { + // Certificates present in storage have been executed already. continue; } - - let mut response = node.handle_chain_info_query(query).await?; - let Some(certificate_hash) = response.info.requested_sent_certificate_hashes.pop() - else { - break; - }; - - let certificate = node.download_certificate(certificate_hash).await?; + let certificate = node.download_certificate(hash).await?; let CertificateValue::ConfirmedBlock { executed_block, .. } = certificate.value() else { return Err(NodeError::InvalidChainInfoResponse); diff --git a/linera-core/src/data_types.rs b/linera-core/src/data_types.rs index 013e2f8c86c..9e509989783 100644 --- a/linera-core/src/data_types.rs +++ b/linera-core/src/data_types.rs @@ -10,7 +10,7 @@ use linera_base::{ identifiers::{ChainDescription, ChainId, Owner}, }; use linera_chain::{ - data_types::{ChainAndHeight, IncomingMessage, Medium, MessageBundle}, + data_types::{IncomingMessage, Medium, MessageBundle}, manager::ChainManagerInfo, ChainStateView, }; @@ -162,7 +162,7 @@ pub struct ChainInfo { /// The current number of received certificates (useful for `request_received_log_excluding_first_nth`) pub count_received_log: usize, /// The response to `request_received_certificates_excluding_first_nth` - pub requested_received_log: Vec, + pub requested_received_log: Vec, } /// The response to an `ChainInfoQuery` diff --git a/linera-core/src/local_node.rs b/linera-core/src/local_node.rs index ecc1aa2a6cd..de57ae6bc63 100644 --- a/linera-core/src/local_node.rs +++ b/linera-core/src/local_node.rs @@ -9,6 +9,7 @@ use futures::{ lock::{Mutex, MutexGuard}, }; use linera_base::{ + crypto::CryptoHash, data_types::{ArithmeticError, Blob, BlockHeight, HashedBlob}, identifiers::{BlobId, ChainId, MessageId}, }; @@ -187,6 +188,12 @@ where let node = self.lock_node().await; node.state.storage_client().clone() } + + #[tracing::instrument(level = "trace", skip_all)] + pub(crate) async fn has_certificate(&self, hash: CryptoHash) -> Result { + let client = self.storage_client().await; + Ok(client.contains_certificate(hash).await?) + } } impl LocalNodeClient diff --git a/linera-core/src/unit_tests/worker_tests.rs b/linera-core/src/unit_tests/worker_tests.rs index f91aea61fab..65bf3097a19 100644 --- a/linera-core/src/unit_tests/worker_tests.rs +++ b/linera-core/src/unit_tests/worker_tests.rs @@ -25,8 +25,8 @@ use linera_base::{ }; use linera_chain::{ data_types::{ - Block, BlockExecutionOutcome, BlockProposal, Certificate, ChainAndHeight, ChannelFullName, - Event, HashedCertificateValue, IncomingMessage, LiteVote, Medium, MessageAction, Origin, + Block, BlockExecutionOutcome, BlockProposal, Certificate, ChannelFullName, Event, + HashedCertificateValue, IncomingMessage, LiteVote, Medium, MessageAction, Origin, OutgoingMessage, SignatureAggregator, }, test::{make_child_block, make_first_block, BlockTestExt, VoteTestExt}, @@ -1850,7 +1850,7 @@ where }) ); - let certificate = make_simple_transfer_certificate( + let transfer_certificate = make_simple_transfer_certificate( ChainDescription::Root(1), &sender_key_pair, ChainId::root(2), @@ -1864,13 +1864,13 @@ where .await; let info = worker - .fully_handle_certificate(certificate.clone(), vec![], vec![]) + .fully_handle_certificate(transfer_certificate.clone(), vec![], vec![]) .await? .info; assert_eq!(ChainId::root(1), info.chain_id); assert_eq!(Amount::ZERO, info.chain_balance); assert_eq!(BlockHeight::from(1), info.next_block_height); - assert_eq!(Some(certificate.hash()), info.block_hash); + assert_eq!(Some(transfer_certificate.hash()), info.block_hash); assert!(info.manager.pending.is_none()); assert_eq!( worker @@ -1891,7 +1891,7 @@ where vec![IncomingMessage { origin: Origin::chain(ChainId::root(1)), event: Event { - certificate_hash: certificate.hash(), + certificate_hash: transfer_certificate.hash(), height: BlockHeight::ZERO, index: 0, authenticated_signer: None, @@ -1950,10 +1950,7 @@ where assert_eq!(response.info.requested_received_log.len(), 1); assert_eq!( response.info.requested_received_log[0], - ChainAndHeight { - chain_id: ChainId::root(1), - height: BlockHeight::ZERO - } + transfer_certificate.hash() ); Ok(()) } diff --git a/linera-service-graphql-client/gql/service_requests.graphql b/linera-service-graphql-client/gql/service_requests.graphql index 30bc712ba64..025ef8f2a79 100644 --- a/linera-service-graphql-client/gql/service_requests.graphql +++ b/linera-service-graphql-client/gql/service_requests.graphql @@ -93,10 +93,7 @@ query Chain( entries } receivedLog { - entries { - chainId - height - } + entries } inboxes { keys diff --git a/linera-service-graphql-client/gql/service_schema.graphql b/linera-service-graphql-client/gql/service_schema.graphql index 900be7bae3d..a31a589d4e2 100644 --- a/linera-service-graphql-client/gql/service_schema.graphql +++ b/linera-service-graphql-client/gql/service_schema.graphql @@ -143,14 +143,6 @@ type CertificateValue { status: String! } -""" -A chain ID with a block height. -""" -type ChainAndHeight { - chainId: ChainId! - height: BlockHeight! -} - """ How to create a chain """ @@ -195,9 +187,9 @@ type ChainStateExtendedView { """ confirmedLog: LogView_CryptoHash_b0541e41! """ - Sender chain and height of all certified blocks known as a receiver (local ordering). + The hash of all the certified blocks known as a receiver (local ordering). """ - receivedLog: LogView_ChainAndHeight_de85b393! + receivedLog: LogView_CryptoHash_b0541e41! """ Mailboxes used to receive messages indexed by their origin. """ @@ -496,10 +488,6 @@ A scalar that can represent any JSON Object value. """ scalar JSONObject -type LogView_ChainAndHeight_de85b393 { - entries(start: Int, end: Int): [ChainAndHeight!]! -} - type LogView_CryptoHash_b0541e41 { entries(start: Int, end: Int): [CryptoHash!]! }