use std::{collections::BTreeMap, vec};
use custom_debug_derive::Debug;
use linera_base::{
data_types::{ArithmeticError, Blob, Event, OracleResponse, Timestamp},
ensure,
identifiers::{BlobId, ChainId, ChannelFullName, StreamId},
};
use crate::{ExecutionError, OutgoingMessage};
#[derive(Debug, Default)]
pub struct TransactionTracker {
#[debug(skip_if = Option::is_none)]
replaying_oracle_responses: Option<vec::IntoIter<OracleResponse>>,
#[debug(skip_if = Vec::is_empty)]
oracle_responses: Vec<OracleResponse>,
#[debug(skip_if = Vec::is_empty)]
outgoing_messages: Vec<OutgoingMessage>,
local_time: Timestamp,
transaction_index: u32,
next_message_index: u32,
next_application_index: u32,
events: Vec<Event>,
blobs: BTreeMap<BlobId, Blob>,
subscribe: Vec<(ChannelFullName, ChainId)>,
unsubscribe: Vec<(ChannelFullName, ChainId)>,
operation_result: Option<Vec<u8>>,
}
#[derive(Debug, Default)]
pub struct TransactionOutcome {
#[debug(skip_if = Vec::is_empty)]
pub oracle_responses: Vec<OracleResponse>,
#[debug(skip_if = Vec::is_empty)]
pub outgoing_messages: Vec<OutgoingMessage>,
pub next_message_index: u32,
pub next_application_index: u32,
pub events: Vec<Event>,
pub blobs: Vec<Blob>,
pub subscribe: Vec<(ChannelFullName, ChainId)>,
pub unsubscribe: Vec<(ChannelFullName, ChainId)>,
pub operation_result: Vec<u8>,
}
impl TransactionTracker {
pub fn new(
local_time: Timestamp,
transaction_index: u32,
next_message_index: u32,
next_application_index: u32,
oracle_responses: Option<Vec<OracleResponse>>,
) -> Self {
TransactionTracker {
local_time,
transaction_index,
next_message_index,
next_application_index,
replaying_oracle_responses: oracle_responses.map(Vec::into_iter),
..Self::default()
}
}
pub fn with_blobs(mut self, blobs: BTreeMap<BlobId, Blob>) -> Self {
self.blobs = blobs;
self
}
pub fn local_time(&self) -> Timestamp {
self.local_time
}
pub fn set_local_time(&mut self, local_time: Timestamp) {
self.local_time = local_time;
}
pub fn transaction_index(&self) -> u32 {
self.transaction_index
}
pub fn next_message_index(&self) -> u32 {
self.next_message_index
}
pub fn next_application_index(&mut self) -> u32 {
let index = self.next_application_index;
self.next_application_index += 1;
index
}
pub fn add_outgoing_message(
&mut self,
message: OutgoingMessage,
) -> Result<(), ArithmeticError> {
self.next_message_index = self
.next_message_index
.checked_add(1)
.ok_or(ArithmeticError::Overflow)?;
self.outgoing_messages.push(message);
Ok(())
}
pub fn add_outgoing_messages(
&mut self,
messages: impl IntoIterator<Item = OutgoingMessage>,
) -> Result<(), ArithmeticError> {
for message in messages {
self.add_outgoing_message(message)?;
}
Ok(())
}
pub fn add_event(&mut self, stream_id: StreamId, index: u32, value: Vec<u8>) {
self.events.push(Event {
stream_id,
index,
value,
});
}
pub fn add_created_blob(&mut self, blob: Blob) {
self.blobs.insert(blob.id(), blob);
}
pub fn created_blobs(&self) -> &BTreeMap<BlobId, Blob> {
&self.blobs
}
pub fn subscribe(&mut self, name: ChannelFullName, subscriber: ChainId) {
self.subscribe.push((name, subscriber));
}
pub fn unsubscribe(&mut self, name: ChannelFullName, subscriber: ChainId) {
self.unsubscribe.push((name, subscriber));
}
pub fn add_oracle_response(&mut self, oracle_response: OracleResponse) {
self.oracle_responses.push(oracle_response);
}
pub fn add_operation_result(&mut self, result: Option<Vec<u8>>) {
self.operation_result = result
}
pub fn replay_oracle_response(
&mut self,
oracle_response: OracleResponse,
) -> Result<bool, ExecutionError> {
let replaying = if let Some(recorded_response) = self.next_replayed_oracle_response()? {
ensure!(
recorded_response == oracle_response,
ExecutionError::OracleResponseMismatch
);
true
} else {
false
};
self.add_oracle_response(oracle_response);
Ok(replaying)
}
pub fn next_replayed_oracle_response(
&mut self,
) -> Result<Option<OracleResponse>, ExecutionError> {
let Some(responses) = &mut self.replaying_oracle_responses else {
return Ok(None); };
let response = responses
.next()
.ok_or_else(|| ExecutionError::MissingOracleResponse)?;
Ok(Some(response))
}
pub fn into_outcome(self) -> Result<TransactionOutcome, ExecutionError> {
let TransactionTracker {
replaying_oracle_responses,
oracle_responses,
outgoing_messages,
local_time: _,
transaction_index: _,
next_message_index,
next_application_index,
events,
blobs,
subscribe,
unsubscribe,
operation_result,
} = self;
if let Some(mut responses) = replaying_oracle_responses {
ensure!(
responses.next().is_none(),
ExecutionError::UnexpectedOracleResponse
);
}
Ok(TransactionOutcome {
outgoing_messages,
oracle_responses,
next_message_index,
next_application_index,
events,
blobs: blobs.into_values().collect(),
subscribe,
unsubscribe,
operation_result: operation_result.unwrap_or_default(),
})
}
}
#[cfg(with_testing)]
impl TransactionTracker {
pub fn new_replaying(oracle_responses: Vec<OracleResponse>) -> Self {
TransactionTracker::new(Timestamp::from(0), 0, 0, 0, Some(oracle_responses))
}
pub fn new_replaying_blobs<T>(blob_ids: T) -> Self
where
T: IntoIterator,
T::Item: std::borrow::Borrow<BlobId>,
{
use std::borrow::Borrow;
let oracle_responses = blob_ids
.into_iter()
.map(|blob_id| OracleResponse::Blob(*blob_id.borrow()))
.collect();
TransactionTracker::new_replaying(oracle_responses)
}
}