Make TransactionKind have a reference to Signed

Broken commit due to partial staging of one file.
This commit is contained in:
Luke Parker
2023-04-12 09:38:20 -04:00
parent 354ac856a5
commit d5a12a9b97
4 changed files with 41 additions and 29 deletions

View File

@@ -210,18 +210,18 @@ impl ReadWrite for Transaction {
} }
impl TransactionTrait for Transaction { impl TransactionTrait for Transaction {
fn kind(&self) -> TransactionKind { fn kind(&self) -> TransactionKind<'_> {
match self { match self {
Transaction::DkgCommitments(_, _, signed) => TransactionKind::Signed(signed.clone()), Transaction::DkgCommitments(_, _, signed) => TransactionKind::Signed(signed),
Transaction::DkgShares(_, _, signed) => TransactionKind::Signed(signed.clone()), Transaction::DkgShares(_, _, signed) => TransactionKind::Signed(signed),
Transaction::SignPreprocess(data) => TransactionKind::Signed(data.signed.clone()), Transaction::SignPreprocess(data) => TransactionKind::Signed(&data.signed),
Transaction::SignShare(data) => TransactionKind::Signed(data.signed.clone()), Transaction::SignShare(data) => TransactionKind::Signed(&data.signed),
Transaction::FinalizedBlock(_) => TransactionKind::Provided, Transaction::FinalizedBlock(_) => TransactionKind::Provided,
Transaction::BatchPreprocess(data) => TransactionKind::Signed(data.signed.clone()), Transaction::BatchPreprocess(data) => TransactionKind::Signed(&data.signed),
Transaction::BatchShare(data) => TransactionKind::Signed(data.signed.clone()), Transaction::BatchShare(data) => TransactionKind::Signed(&data.signed),
} }
} }
@@ -229,8 +229,7 @@ impl TransactionTrait for Transaction {
let mut tx = self.serialize(); let mut tx = self.serialize();
if let TransactionKind::Signed(signed) = self.kind() { if let TransactionKind::Signed(signed) = self.kind() {
// Make sure the part we're cutting off is the signature // Make sure the part we're cutting off is the signature
assert_eq!(&tx[(tx.len() - 64) ..], &signed.signature.serialize()); assert_eq!(tx.drain((tx.len() - 64) ..).collect::<Vec<_>>(), signed.signature.serialize());
tx.truncate(tx.len() - 64);
} }
Blake2s256::digest(tx).into() Blake2s256::digest(tx).into()
} }

View File

@@ -19,15 +19,35 @@ use crate::{
// A transaction solely defined by its nonce and a distinguisher (to allow creating distinct TXs // A transaction solely defined by its nonce and a distinguisher (to allow creating distinct TXs
// sharing a nonce). // sharing a nonce).
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
struct NonceTransaction(u32, u8); struct NonceTransaction(u32, u8, Signed);
impl NonceTransaction {
fn new(nonce: u32, distinguisher: u8) -> Self {
NonceTransaction(
nonce,
distinguisher,
Signed {
signer: <Ristretto as Ciphersuite>::G::identity(),
nonce,
signature: SchnorrSignature::<Ristretto> {
R: <Ristretto as Ciphersuite>::G::identity(),
s: <Ristretto as Ciphersuite>::F::ZERO,
},
},
)
}
}
impl ReadWrite for NonceTransaction { impl ReadWrite for NonceTransaction {
fn read<R: io::Read>(reader: &mut R) -> io::Result<Self> { fn read<R: io::Read>(reader: &mut R) -> io::Result<Self> {
let mut nonce = [0; 4]; let mut nonce = [0; 4];
reader.read_exact(&mut nonce)?; reader.read_exact(&mut nonce)?;
let nonce = u32::from_le_bytes(nonce);
let mut distinguisher = [0]; let mut distinguisher = [0];
reader.read_exact(&mut distinguisher)?; reader.read_exact(&mut distinguisher)?;
Ok(Self(u32::from_le_bytes(nonce), distinguisher[0]))
Ok(NonceTransaction::new(nonce, distinguisher[0]))
} }
fn write<W: io::Write>(&self, writer: &mut W) -> io::Result<()> { fn write<W: io::Write>(&self, writer: &mut W) -> io::Result<()> {
@@ -37,15 +57,8 @@ impl ReadWrite for NonceTransaction {
} }
impl Transaction for NonceTransaction { impl Transaction for NonceTransaction {
fn kind(&self) -> TransactionKind { fn kind(&self) -> TransactionKind<'_> {
TransactionKind::Signed(Signed { TransactionKind::Signed(&self.2)
signer: <Ristretto as Ciphersuite>::G::identity(),
nonce: self.0,
signature: SchnorrSignature::<Ristretto> {
R: <Ristretto as Ciphersuite>::G::identity(),
s: <Ristretto as Ciphersuite>::F::ZERO,
},
})
} }
fn hash(&self) -> [u8; 32] { fn hash(&self) -> [u8; 32] {

View File

@@ -51,7 +51,7 @@ impl ReadWrite for ProvidedTransaction {
} }
impl Transaction for ProvidedTransaction { impl Transaction for ProvidedTransaction {
fn kind(&self) -> TransactionKind { fn kind(&self) -> TransactionKind<'_> {
TransactionKind::Provided TransactionKind::Provided
} }
@@ -91,8 +91,8 @@ impl ReadWrite for SignedTransaction {
} }
impl Transaction for SignedTransaction { impl Transaction for SignedTransaction {
fn kind(&self) -> TransactionKind { fn kind(&self) -> TransactionKind<'_> {
TransactionKind::Signed(self.1.clone()) TransactionKind::Signed(&self.1)
} }
fn hash(&self) -> [u8; 32] { fn hash(&self) -> [u8; 32] {

View File

@@ -53,7 +53,7 @@ impl ReadWrite for Signed {
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
pub enum TransactionKind { pub enum TransactionKind<'a> {
/// This tranaction should be provided by every validator, solely ordered by the block producer. /// This tranaction should be provided by every validator, solely ordered by the block producer.
/// ///
/// This transaction is only valid if a supermajority of validators provided it. /// This transaction is only valid if a supermajority of validators provided it.
@@ -63,11 +63,11 @@ pub enum TransactionKind {
Unsigned, Unsigned,
/// A signed transaction. /// A signed transaction.
Signed(Signed), Signed(&'a Signed),
} }
pub trait Transaction: Send + Sync + Clone + Eq + Debug + ReadWrite { pub trait Transaction: Send + Sync + Clone + Eq + Debug + ReadWrite {
fn kind(&self) -> TransactionKind; fn kind(&self) -> TransactionKind<'_>;
/// Return the hash of this transaction. /// Return the hash of this transaction.
/// ///
/// The hash must NOT commit to the signature. /// The hash must NOT commit to the signature.
@@ -97,13 +97,13 @@ pub(crate) fn verify_transaction<T: Transaction>(
TransactionKind::Unsigned => {} TransactionKind::Unsigned => {}
TransactionKind::Signed(Signed { signer, nonce, signature }) => { TransactionKind::Signed(Signed { signer, nonce, signature }) => {
// TODO: Use presence as a whitelist, erroring on lack of // TODO: Use presence as a whitelist, erroring on lack of
if next_nonces.get(&signer).cloned().unwrap_or(0) != nonce { if next_nonces.get(signer).cloned().unwrap_or(0) != *nonce {
Err(TransactionError::Temporal)?; Err(TransactionError::Temporal)?;
} }
next_nonces.insert(signer, nonce + 1); next_nonces.insert(*signer, nonce + 1);
// TODO: Use Schnorr half-aggregation and a batch verification here // TODO: Use Schnorr half-aggregation and a batch verification here
if !signature.verify(signer, tx.sig_hash(genesis)) { if !signature.verify(*signer, tx.sig_hash(genesis)) {
Err(TransactionError::Fatal)?; Err(TransactionError::Fatal)?;
} }
} }