Sign the ID directly instead of its SCALE encoding

For a hash, which is fixed-size, these should be the same yet this helps 
move past the dependency on SCALE. It also, for any type where the two 
values are different, smooths integration.
This commit is contained in:
Luke Parker 2022-10-17 08:16:01 -04:00
parent f28d412f78
commit 5724f52816
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6
3 changed files with 7 additions and 6 deletions

View file

@ -104,7 +104,7 @@ pub enum BlockError {
/// Trait representing a Block. /// Trait representing a Block.
pub trait Block: Send + Sync + Clone + PartialEq + Debug + Encode + Decode { pub trait Block: Send + Sync + Clone + PartialEq + Debug + Encode + Decode {
// Type used to identify blocks. Presumably a cryptographic hash of the block. // Type used to identify blocks. Presumably a cryptographic hash of the block.
type Id: Send + Sync + Copy + Clone + PartialEq + Debug + Encode + Decode; type Id: Send + Sync + Copy + Clone + PartialEq + AsRef<[u8]> + Debug + Encode + Decode;
/// Return the deterministic, unique ID for this block. /// Return the deterministic, unique ID for this block.
fn id(&self) -> Self::Id; fn id(&self) -> Self::Id;

View file

@ -301,6 +301,7 @@ impl<N: Network + 'static> TendermintMachine<N> {
// Check if it has gotten a sufficient amount of precommits // Check if it has gotten a sufficient amount of precommits
let (participants, weight) = self let (participants, weight) = self
.log .log
// Use a junk signature since message equality is irrelevant to the signature
.message_instances(round, Data::Precommit(Some((block.id(), self.signer.sign(&[]))))); .message_instances(round, Data::Precommit(Some((block.id(), self.signer.sign(&[])))));
let threshold = self.weights.threshold(); let threshold = self.weights.threshold();
@ -324,7 +325,7 @@ impl<N: Network + 'static> TendermintMachine<N> {
msg: Message<N::ValidatorId, N::Block, <N::SignatureScheme as SignatureScheme>::Signature>, msg: Message<N::ValidatorId, N::Block, <N::SignatureScheme as SignatureScheme>::Signature>,
) -> Result<Option<N::Block>, TendermintError<N::ValidatorId>> { ) -> Result<Option<N::Block>, TendermintError<N::ValidatorId>> {
if let Data::Precommit(Some((id, sig))) = &msg.data { if let Data::Precommit(Some((id, sig))) = &msg.data {
if !self.signer.verify(msg.sender, &id.encode(), sig.clone()) { if !self.signer.verify(msg.sender, id.as_ref(), sig.clone()) {
Err(TendermintError::Malicious(msg.sender))?; Err(TendermintError::Malicious(msg.sender))?;
} }
} }
@ -433,7 +434,7 @@ impl<N: Network + 'static> TendermintMachine<N> {
self self
.broadcast(Data::Precommit(Some(( .broadcast(Data::Precommit(Some((
block.id(), block.id(),
self.signer.sign(&block.id().encode()), self.signer.sign(block.id().as_ref()),
)))) ))))
.await, .await,
); );

View file

@ -7,7 +7,7 @@ use tokio::sync::RwLock;
use tendermint_machine::{ext::*, SignedMessage, TendermintMachine, TendermintHandle}; use tendermint_machine::{ext::*, SignedMessage, TendermintMachine, TendermintHandle};
type TestValidatorId = u16; type TestValidatorId = u16;
type TestBlockId = u32; type TestBlockId = [u8; 4];
struct TestSignatureScheme(u16); struct TestSignatureScheme(u16);
impl SignatureScheme for TestSignatureScheme { impl SignatureScheme for TestSignatureScheme {
@ -114,7 +114,7 @@ impl Network for TestNetwork {
dbg!("Adding ", &block); dbg!("Adding ", &block);
assert!(block.valid.is_ok()); assert!(block.valid.is_ok());
assert!(self.verify_commit(block.id(), &commit)); assert!(self.verify_commit(block.id(), &commit));
TestBlock { id: block.id + 1, valid: Ok(()) } TestBlock { id: (u32::from_le_bytes(block.id) + 1).to_le_bytes(), valid: Ok(()) }
} }
} }
@ -129,7 +129,7 @@ impl TestNetwork {
TestNetwork(i, arc.clone()), TestNetwork(i, arc.clone()),
i, i,
BlockNumber(1), BlockNumber(1),
TestBlock { id: 1, valid: Ok(()) }, TestBlock { id: 1u32.to_le_bytes(), valid: Ok(()) },
)); ));
} }
} }