Skip to content

Commit

Permalink
Use BlockExecutionOutcome in ExecutedBlock. (linera-io#1940)
Browse files Browse the repository at this point in the history
* Use BlockExecutionOutcome in ExecutedBlock.

* Add Block links to documentation.
  • Loading branch information
afck authored Apr 24, 2024
1 parent 93a0299 commit 7c57034
Show file tree
Hide file tree
Showing 17 changed files with 651 additions and 564 deletions.
36 changes: 14 additions & 22 deletions linera-chain/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,14 @@ impl OutgoingMessage {
}
}

/// A block, together with the messages and the state hash resulting from its execution.
/// A [`Block`], together with the outcome from its execution.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize, SimpleObject)]
pub struct ExecutedBlock {
pub block: Block,
pub messages: Vec<OutgoingMessage>,
/// For each transaction, the cumulative number of messages created by this and all previous
/// transactions, i.e. `message_counts[i]` is the index of the first message created by
/// transaction `i + 1` or later.
pub message_counts: Vec<u32>,
pub state_hash: CryptoHash,
pub outcome: BlockExecutionOutcome,
}

/// The messages and the state hash resulting from a block's execution.
/// The messages and the state hash resulting from a [`Block`]'s execution.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize, SimpleObject)]
pub struct BlockExecutionOutcome {
pub messages: Vec<OutgoingMessage>,
Expand Down Expand Up @@ -610,7 +605,7 @@ impl CertificateValue {
};
self.height() == message_id.height
&& self.chain_id() == message_id.chain_id
&& executed_block.messages.len() > index
&& executed_block.messages().len() > index
}

pub fn is_confirmed(&self) -> bool {
Expand All @@ -627,7 +622,7 @@ impl CertificateValue {

#[cfg(with_testing)]
pub fn messages(&self) -> Option<&Vec<OutgoingMessage>> {
Some(&self.executed_block()?.messages)
Some(self.executed_block()?.messages())
}

pub fn executed_block(&self) -> Option<&ExecutedBlock> {
Expand Down Expand Up @@ -675,6 +670,10 @@ impl Event {
}

impl ExecutedBlock {
pub fn messages(&self) -> &Vec<OutgoingMessage> {
&self.outcome.messages
}

/// Returns the `message_index`th outgoing message created by the `operation_index`th operation,
/// or `None` if there is no such operation or message.
pub fn message_id_for_operation(
Expand All @@ -686,10 +685,10 @@ impl ExecutedBlock {
let transaction_index = block.incoming_messages.len().checked_add(operation_index)?;
let first_message_index = match transaction_index.checked_sub(1) {
None => 0,
Some(index) => *self.message_counts.get(index)?,
Some(index) => *self.outcome.message_counts.get(index)?,
};
let index = first_message_index.checked_add(message_index)?;
let next_transaction_index = *self.message_counts.get(transaction_index)?;
let next_transaction_index = *self.outcome.message_counts.get(transaction_index)?;
if index < next_transaction_index {
Some(self.message_id(index))
} else {
Expand All @@ -706,7 +705,7 @@ impl ExecutedBlock {
if self.block.chain_id != *chain_id || self.block.height != *height {
return None;
}
self.messages.get(usize::try_from(*index).ok()?)
self.messages().get(usize::try_from(*index).ok()?)
}

/// Returns the message ID belonging to the `index`th outgoing message in this block.
Expand All @@ -721,16 +720,9 @@ impl ExecutedBlock {

impl BlockExecutionOutcome {
pub fn with(self, block: Block) -> ExecutedBlock {
let BlockExecutionOutcome {
messages,
message_counts,
state_hash,
} = self;
ExecutedBlock {
block,
messages,
message_counts,
state_hash,
outcome: self,
}
}
}
Expand Down Expand Up @@ -996,7 +988,7 @@ impl Certificate {
pub fn message_bundle_for(&self, medium: &Medium, recipient: ChainId) -> Option<MessageBundle> {
let executed_block = self.value().executed_block()?;
let messages = (0u32..)
.zip(&executed_block.messages)
.zip(executed_block.messages())
.filter(|(_, message)| message.has_destination(medium, recipient))
.map(|(idx, message)| (idx, message.clone()))
.collect();
Expand Down
12 changes: 6 additions & 6 deletions linera-chain/src/unit_tests/data_types_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ fn test_signed_values() {

let block =
make_first_block(ChainId::root(1)).with_simple_transfer(ChainId::root(2), Amount::ONE);
let executed_block = ExecutedBlock {
block,
let executed_block = BlockExecutionOutcome {
messages: Vec::new(),
message_counts: vec![1],
state_hash: CryptoHash::test_hash("state"),
};
}
.with(block);
let value = HashedValue::new_confirmed(executed_block);

let v = LiteVote::new(value.lite(), Round::Fast, &key1);
Expand All @@ -43,12 +43,12 @@ fn test_certificates() {

let block =
make_first_block(ChainId::root(1)).with_simple_transfer(ChainId::root(1), Amount::ONE);
let executed_block = ExecutedBlock {
block,
let executed_block = BlockExecutionOutcome {
messages: Vec::new(),
message_counts: vec![1],
state_hash: CryptoHash::test_hash("state"),
};
}
.with(block);
let value = HashedValue::new_confirmed(executed_block);

let v1 = LiteVote::new(value.lite(), Round::Fast, &key1);
Expand Down
162 changes: 88 additions & 74 deletions linera-core/src/unit_tests/wasm_worker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use linera_base::{
};
use linera_chain::{
data_types::{
ChannelFullName, Event, ExecutedBlock, HashedValue, IncomingMessage, MessageAction, Origin,
OutgoingMessage,
BlockExecutionOutcome, ChannelFullName, Event, HashedValue, IncomingMessage, MessageAction,
Origin, OutgoingMessage,
},
test::{make_child_block, make_first_block, BlockTestExt},
};
Expand Down Expand Up @@ -136,19 +136,21 @@ where
..SystemExecutionState::new(Epoch::ZERO, publisher_chain, admin_id)
};
let publisher_state_hash = publisher_system_state.clone().into_hash().await;
let publish_block_proposal = HashedValue::new_confirmed(ExecutedBlock {
block: publish_block,
messages: vec![OutgoingMessage {
destination: Destination::Recipient(publisher_chain.into()),
authenticated_signer: None,
grant: Amount::ZERO,
refund_grant_to: None,
kind: MessageKind::Protected,
message: Message::System(publish_message.clone()),
}],
message_counts: vec![1],
state_hash: publisher_state_hash,
});
let publish_block_proposal = HashedValue::new_confirmed(
BlockExecutionOutcome {
messages: vec![OutgoingMessage {
destination: Destination::Recipient(publisher_chain.into()),
authenticated_signer: None,
grant: Amount::ZERO,
refund_grant_to: None,
kind: MessageKind::Protected,
message: Message::System(publish_message.clone()),
}],
message_counts: vec![1],
state_hash: publisher_state_hash,
}
.with(publish_block),
);
let publish_certificate = make_certificate(&committee, &worker, publish_block_proposal);

let info = worker
Expand Down Expand Up @@ -210,12 +212,14 @@ where
kind: MessageKind::Simple,
message: Message::System(broadcast_message.clone()),
};
let failing_broadcast_block_proposal = HashedValue::new_confirmed(ExecutedBlock {
block: broadcast_block.clone(),
messages: vec![failing_broadcast_outgoing_message],
message_counts: vec![1],
state_hash: publisher_state_hash,
});
let failing_broadcast_block_proposal = HashedValue::new_confirmed(
BlockExecutionOutcome {
messages: vec![failing_broadcast_outgoing_message],
message_counts: vec![1],
state_hash: publisher_state_hash,
}
.with(broadcast_block.clone()),
);
let failing_broadcast_certificate =
make_certificate(&committee, &worker, failing_broadcast_block_proposal);

Expand All @@ -232,12 +236,14 @@ where
kind: MessageKind::Simple,
message: Message::System(broadcast_message.clone()),
};
let broadcast_block_proposal = HashedValue::new_confirmed(ExecutedBlock {
block: broadcast_block,
messages: vec![broadcast_outgoing_message],
message_counts: vec![1],
state_hash: publisher_state_hash,
});
let broadcast_block_proposal = HashedValue::new_confirmed(
BlockExecutionOutcome {
messages: vec![broadcast_outgoing_message],
message_counts: vec![1],
state_hash: publisher_state_hash,
}
.with(broadcast_block),
);
let broadcast_certificate = make_certificate(&committee, &worker, broadcast_block_proposal);

let info = worker
Expand Down Expand Up @@ -277,19 +283,21 @@ where
};
creator_system_state.subscriptions.insert(publisher_channel);
let creator_state = creator_system_state.clone().into_view().await;
let subscribe_block_proposal = HashedValue::new_confirmed(ExecutedBlock {
block: subscribe_block,
messages: vec![OutgoingMessage {
destination: Destination::Recipient(publisher_chain.into()),
authenticated_signer: None,
grant: Amount::ZERO,
refund_grant_to: None,
kind: MessageKind::Protected,
message: Message::System(subscribe_message.clone()),
}],
message_counts: vec![1],
state_hash: creator_state.crypto_hash().await?,
});
let subscribe_block_proposal = HashedValue::new_confirmed(
BlockExecutionOutcome {
messages: vec![OutgoingMessage {
destination: Destination::Recipient(publisher_chain.into()),
authenticated_signer: None,
grant: Amount::ZERO,
refund_grant_to: None,
kind: MessageKind::Protected,
message: Message::System(subscribe_message.clone()),
}],
message_counts: vec![1],
state_hash: creator_state.crypto_hash().await?,
}
.with(subscribe_block),
);
let subscribe_certificate = make_certificate(&committee, &worker, subscribe_block_proposal);

let info = worker
Expand Down Expand Up @@ -325,21 +333,23 @@ where
.with_incoming_message(accept_message);
publisher_system_state.timestamp = Timestamp::from(3);
let publisher_state_hash = publisher_system_state.into_hash().await;
let accept_block_proposal = HashedValue::new_confirmed(ExecutedBlock {
block: accept_block,
messages: vec![OutgoingMessage {
destination: Destination::Recipient(creator_chain.into()),
authenticated_signer: None,
grant: Amount::ZERO,
refund_grant_to: None,
kind: MessageKind::Protected,
message: Message::System(SystemMessage::Notify {
id: creator_chain.into(),
}),
}],
message_counts: vec![1],
state_hash: publisher_state_hash,
});
let accept_block_proposal = HashedValue::new_confirmed(
BlockExecutionOutcome {
messages: vec![OutgoingMessage {
destination: Destination::Recipient(creator_chain.into()),
authenticated_signer: None,
grant: Amount::ZERO,
refund_grant_to: None,
kind: MessageKind::Protected,
message: Message::System(SystemMessage::Notify {
id: creator_chain.into(),
}),
}],
message_counts: vec![1],
state_hash: publisher_state_hash,
}
.with(accept_block),
);
let accept_certificate = make_certificate(&committee, &worker, accept_block_proposal);

let info = worker
Expand Down Expand Up @@ -418,19 +428,21 @@ where
initial_value_bytes.clone(),
)
.await?;
let create_block_proposal = HashedValue::new_confirmed(ExecutedBlock {
block: create_block,
messages: vec![OutgoingMessage {
destination: Destination::Recipient(creator_chain.into()),
authenticated_signer: None,
grant: Amount::ZERO,
refund_grant_to: None,
kind: MessageKind::Protected,
message: Message::System(SystemMessage::ApplicationCreated),
}],
message_counts: vec![0, 1],
state_hash: creator_state.crypto_hash().await?,
});
let create_block_proposal = HashedValue::new_confirmed(
BlockExecutionOutcome {
messages: vec![OutgoingMessage {
destination: Destination::Recipient(creator_chain.into()),
authenticated_signer: None,
grant: Amount::ZERO,
refund_grant_to: None,
kind: MessageKind::Protected,
message: Message::System(SystemMessage::ApplicationCreated),
}],
message_counts: vec![0, 1],
state_hash: creator_state.crypto_hash().await?,
}
.with(create_block),
);
let create_certificate = make_certificate(&committee, &worker, create_block_proposal);

let info = worker
Expand Down Expand Up @@ -474,12 +486,14 @@ where
)
.await?;
creator_state.system.timestamp.set(Timestamp::from(5));
let run_block_proposal = HashedValue::new_confirmed(ExecutedBlock {
block: run_block,
messages: vec![],
message_counts: vec![0],
state_hash: creator_state.crypto_hash().await?,
});
let run_block_proposal = HashedValue::new_confirmed(
BlockExecutionOutcome {
messages: vec![],
message_counts: vec![0],
state_hash: creator_state.crypto_hash().await?,
}
.with(run_block),
);
let run_certificate = make_certificate(&committee, &worker, run_block_proposal);

let info = worker
Expand Down
Loading

0 comments on commit 7c57034

Please sign in to comment.