Flesh out eventuality task

This commit is contained in:
Luke Parker
2024-08-24 23:43:31 -04:00
parent 945f31dfc7
commit 379780a3c9
11 changed files with 174 additions and 29 deletions

View File

@@ -1,8 +1,9 @@
use core::fmt::Debug;
use std::collections::HashMap;
use group::{Group, GroupEncoding};
use crate::{Id, Address, ReceivedOutput};
use crate::{Id, Address, ReceivedOutput, Eventuality, EventualityTracker};
/// A block header from an external network.
pub trait BlockHeader: Send + Sync + Sized + Clone + Debug {
@@ -15,6 +16,12 @@ pub trait BlockHeader: Send + Sync + Sized + Clone + Debug {
fn parent(&self) -> [u8; 32];
}
/// A transaction from an external network.
pub trait Transaction: Send + Sync + Sized {
/// The type used to identify transactions on this external network.
type Id: Id;
}
/// A block from an external network.
///
/// A block is defined as a consensus event associated with a set of transactions. It is not
@@ -30,12 +37,31 @@ pub trait Block: Send + Sync + Sized + Clone + Debug {
type Key: Group + GroupEncoding;
/// The type used to represent addresses on this external network.
type Address: Address;
/// The type used to represent transactions on this external network.
type Transaction: Transaction;
/// The type used to represent received outputs on this external network.
type Output: ReceivedOutput<Self::Key, Self::Address>;
type Output: ReceivedOutput<
Self::Key,
Self::Address,
TransactionId = <Self::Transaction as Transaction>::Id,
>;
/// The type used to represent an Eventuality for a transaction on this external network.
type Eventuality: Eventuality<
OutputId = <Self::Output as ReceivedOutput<Self::Key, Self::Address>>::Id,
>;
/// The ID of this block.
fn id(&self) -> [u8; 32];
/// Scan all outputs within this block to find the outputs spendable by this key.
fn scan_for_outputs(&self, key: Self::Key) -> Vec<Self::Output>;
/// Check if this block resolved any Eventualities.
///
/// Returns tbe resolved Eventualities, indexed by the ID of the transactions which resolved
/// them.
fn check_for_eventuality_resolutions(
&self,
eventualities: &mut EventualityTracker<Self::Eventuality>,
) -> HashMap<<Self::Transaction as Transaction>::Id, Self::Eventuality>;
}

View File

@@ -1,8 +1,12 @@
use std::collections::HashMap;
use std::io;
use std::{io, collections::HashMap};
use crate::Id;
/// A description of a transaction which will eventually happen.
pub trait Eventuality: Sized + Send + Sync {
/// The type used to identify a received output.
type OutputId: Id;
/// A unique byte sequence which can be used to identify potentially resolving transactions.
///
/// Both a transaction and an Eventuality are expected to be able to yield lookup sequences.
@@ -15,6 +19,9 @@ pub trait Eventuality: Sized + Send + Sync {
/// identified, the full check is performed.
fn lookup(&self) -> Vec<u8>;
/// The output this plan forwarded.
fn forwarded_output(&self) -> Option<Self::OutputId>;
/// Read an Eventuality.
fn read<R: io::Read>(reader: &mut R) -> io::Result<Self>;
/// Serialize an Eventuality to a `Vec<u8>`.

View File

@@ -2,7 +2,7 @@
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
use core::fmt::Debug;
use core::{hash::Hash, fmt::Debug};
use group::GroupEncoding;
@@ -29,6 +29,8 @@ pub trait Id:
+ Clone
+ Default
+ PartialEq
+ Eq
+ Hash
+ AsRef<[u8]>
+ AsMut<[u8]>
+ Debug

View File

@@ -89,12 +89,16 @@ pub trait ReceivedOutput<K: GroupEncoding, A: Address>:
{
/// The type used to identify this output.
type Id: 'static + Id;
/// The type used to identify the transaction which created this output.
type TransactionId: 'static + Id;
/// The type of this output.
fn kind(&self) -> OutputType;
/// The ID of this output.
fn id(&self) -> Self::Id;
/// The ID of the transaction which created this output.
fn transaction_id(&self) -> Self::TransactionId;
/// The key this output was received by.
fn key(&self) -> K;