use linera_base::{
crypto::{AccountPublicKey, AccountSecretKey},
data_types::{Amount, BlockHeight, Round, Timestamp},
hashed::Hashed,
identifiers::{ChainId, Owner},
};
use linera_execution::{
committee::{Committee, Epoch, ValidatorState},
system::Recipient,
Message, MessageKind, Operation, ResourceControlPolicy, SystemOperation,
};
use crate::{
block::ConfirmedBlock,
data_types::{
BlockProposal, IncomingBundle, PostedMessage, ProposedBlock, SignatureAggregator, Vote,
},
types::{CertificateValue, GenericCertificate},
};
pub fn make_child_block(parent: &Hashed<ConfirmedBlock>) -> ProposedBlock {
let parent_value = parent.inner();
let parent_header = &parent_value.block().header;
ProposedBlock {
epoch: parent_header.epoch,
chain_id: parent_header.chain_id,
incoming_bundles: vec![],
operations: vec![],
previous_block_hash: Some(parent.hash()),
height: parent_header.height.try_add_one().unwrap(),
authenticated_signer: parent_header.authenticated_signer,
timestamp: parent_header.timestamp,
}
}
pub fn make_first_block(chain_id: ChainId) -> ProposedBlock {
ProposedBlock {
epoch: Epoch::ZERO,
chain_id,
incoming_bundles: vec![],
operations: vec![],
previous_block_hash: None,
height: BlockHeight::ZERO,
authenticated_signer: None,
timestamp: Timestamp::default(),
}
}
pub trait BlockTestExt: Sized {
fn with_authenticated_signer(self, authenticated_signer: Option<Owner>) -> Self;
fn with_operation(self, operation: impl Into<Operation>) -> Self;
fn with_transfer(self, owner: Option<Owner>, recipient: Recipient, amount: Amount) -> Self;
fn with_simple_transfer(self, chain_id: ChainId, amount: Amount) -> Self;
fn with_incoming_bundle(self, incoming_bundle: IncomingBundle) -> Self;
fn with_timestamp(self, timestamp: impl Into<Timestamp>) -> Self;
fn with_epoch(self, epoch: impl Into<Epoch>) -> Self;
fn into_first_proposal(self, key_pair: &AccountSecretKey) -> BlockProposal {
self.into_proposal_with_round(key_pair, Round::MultiLeader(0))
}
fn into_proposal_with_round(self, key_pair: &AccountSecretKey, round: Round) -> BlockProposal;
}
impl BlockTestExt for ProposedBlock {
fn with_authenticated_signer(mut self, authenticated_signer: Option<Owner>) -> Self {
self.authenticated_signer = authenticated_signer;
self
}
fn with_operation(mut self, operation: impl Into<Operation>) -> Self {
self.operations.push(operation.into());
self
}
fn with_transfer(self, owner: Option<Owner>, recipient: Recipient, amount: Amount) -> Self {
self.with_operation(SystemOperation::Transfer {
owner,
recipient,
amount,
})
}
fn with_simple_transfer(self, chain_id: ChainId, amount: Amount) -> Self {
self.with_transfer(None, Recipient::chain(chain_id), amount)
}
fn with_incoming_bundle(mut self, incoming_bundle: IncomingBundle) -> Self {
self.incoming_bundles.push(incoming_bundle);
self
}
fn with_timestamp(mut self, timestamp: impl Into<Timestamp>) -> Self {
self.timestamp = timestamp.into();
self
}
fn with_epoch(mut self, epoch: impl Into<Epoch>) -> Self {
self.epoch = epoch.into();
self
}
fn into_proposal_with_round(self, key_pair: &AccountSecretKey, round: Round) -> BlockProposal {
BlockProposal::new_initial(round, self, key_pair)
}
}
pub trait VoteTestExt<T>: Sized {
fn into_certificate(self) -> GenericCertificate<T>;
}
impl<T: CertificateValue> VoteTestExt<T> for Vote<T> {
fn into_certificate(self) -> GenericCertificate<T> {
let state = ValidatorState {
network_address: "".to_string(),
votes: 100,
account_public_key: AccountPublicKey::test_key(1),
};
let committee = Committee::new(
vec![(self.public_key, state)].into_iter().collect(),
ResourceControlPolicy::only_fuel(),
);
SignatureAggregator::new(self.value, self.round, &committee)
.append(self.public_key, self.signature)
.unwrap()
.unwrap()
}
}
pub trait MessageTestExt: Sized {
fn to_posted(self, index: u32, kind: MessageKind) -> PostedMessage;
}
impl<T: Into<Message>> MessageTestExt for T {
fn to_posted(self, index: u32, kind: MessageKind) -> PostedMessage {
PostedMessage {
authenticated_signer: None,
grant: Amount::ZERO,
refund_grant_to: None,
kind,
index,
message: self.into(),
}
}
}