Skip to content

Commit

Permalink
refactor(proto): delete SignContext
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Apr 14, 2023
1 parent d08bf1e commit f2287f8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 51 deletions.
4 changes: 3 additions & 1 deletion abci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ name = "echo_socket"
required-features = ["server"]

[dependencies]
tenderdash-proto = { version = "0.12.0-dev.1", path = "../proto", default-features = false, features = [
"crypto",
] }
bytes = { version = "1.0" }
prost = { version = "0.11" }
tenderdash-proto = { version = "0.12.0-dev.1", default-features = false, path = "../proto" }
tracing = { version = "0.1", default-features = false }
tracing-subscriber = { version = "0.3", optional = true, default-features = false }
thiserror = "1.0.39"
Expand Down
90 changes: 40 additions & 50 deletions proto/src/signatures.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Digital signature processing
use alloc::{string::String, vec::Vec};
use alloc::{
string::{String, ToString},
vec::Vec,
};

use bytes::BufMut;
use prost::Message;
Expand All @@ -12,29 +15,21 @@ use crate::{
Error,
};

#[derive(Clone, Debug)]
pub struct SignContext {
pub chain_id: String,
height: i64,
round: i32,
}

impl SignContext {}
pub trait SignBytes {
/// Generate bytes that will be signed.
fn sign_bytes(&self, ctx: &SignContext) -> Result<Vec<u8>, Error>;
fn sign_bytes(&self, chain_id: &str, height: i64, round: i32) -> Result<Vec<u8>, Error>;

/// Generate hash of data to sign
fn sha256(&self, ctx: &SignContext) -> Result<Vec<u8>, Error> {
fn sha256(&self, chain_id: &str, height: i64, round: i32) -> Result<Vec<u8>, Error> {
// todo!()
let sb = self.sign_bytes(ctx)?;
let sb = self.sign_bytes(chain_id, height, round)?;
let result = lhash::sha256(&sb);
Ok(Vec::from(result))
}
}

impl SignBytes for StateId {
fn sign_bytes(&self, _ctx: &SignContext) -> Result<Vec<u8>, Error> {
fn sign_bytes(&self, _chain_id: &str, _height: i64, _round: i32) -> Result<Vec<u8>, Error> {
let mut buf = Vec::new();
self.encode_length_delimited(&mut buf)
.map_err(Error::encode_message)?;
Expand All @@ -44,7 +39,7 @@ impl SignBytes for StateId {
}

impl SignBytes for BlockId {
fn sign_bytes(&self, _ctx: &SignContext) -> Result<Vec<u8>, Error> {
fn sign_bytes(&self, _chain_id: &str, _height: i64, _round: i32) -> Result<Vec<u8>, Error> {
let part_set_header = self.part_set_header.clone().unwrap_or_default();

let block_id = CanonicalBlockId {
Expand All @@ -64,8 +59,8 @@ impl SignBytes for BlockId {
}

impl SignBytes for Vote {
fn sign_bytes(&self, ctx: &SignContext) -> Result<Vec<u8>, Error> {
if ctx.height != self.height || ctx.round != self.round {
fn sign_bytes(&self, chain_id: &str, height: i64, round: i32) -> Result<Vec<u8>, Error> {
if height != self.height || round != self.round {
return Err(Error::create_canonical(String::from(
"vote height/round mismatch",
)));
Expand All @@ -78,13 +73,13 @@ impl SignBytes for Vote {
"missing vote.block id",
)))?;

vote_sign_bytes(ctx, block_id, self.r#type())
vote_sign_bytes(block_id, self.r#type(), chain_id, height, round)
}
}

impl SignBytes for Commit {
fn sign_bytes(&self, ctx: &SignContext) -> Result<Vec<u8>, Error> {
if ctx.height != self.height || ctx.round != self.round {
fn sign_bytes(&self, chain_id: &str, height: i64, round: i32) -> Result<Vec<u8>, Error> {
if height != self.height || round != self.round {
return Err(Error::create_canonical(String::from(
"commit height/round mismatch",
)));
Expand All @@ -97,22 +92,22 @@ impl SignBytes for Commit {
"missing vote.block id",
)))?;

vote_sign_bytes(ctx, block_id, SignedMsgType::Precommit)
vote_sign_bytes(block_id, SignedMsgType::Precommit, chain_id, height, round)
}
}

impl SignBytes for VoteExtension {
fn sign_bytes(&self, ctx: &SignContext) -> Result<Vec<u8>, Error> {
fn sign_bytes(&self, chain_id: &str, height: i64, round: i32) -> Result<Vec<u8>, Error> {
if self.r#type() != VoteExtensionType::ThresholdRecover {
return Err(Error::create_canonical(String::from(
"only ThresholdRecover vote extensions can be signed",
)));
}
let ve = CanonicalVoteExtension {
chain_id: ctx.chain_id.clone(),
chain_id: chain_id.to_string(),
extension: self.extension.clone(),
height: ctx.height,
round: ctx.round as i64,
height,
round: round as i64,
r#type: self.r#type,
};

Expand All @@ -124,23 +119,25 @@ impl SignBytes for VoteExtension {
///
/// See https://github.com/dashpay/tenderdash/blob/bcb623bcf002ac54b26ed1324b98116872dd0da7/proto/tendermint/types/types.go#L56
fn vote_sign_bytes(
ctx: &SignContext,
block_id: BlockId,
vote_type: SignedMsgType,
chain_id: &str,
height: i64,
round: i32,
) -> Result<Vec<u8>, Error> {
// we just use some rough guesstimate of intial capacity
let mut buf = Vec::with_capacity(80);

let state_id = block_id.state_id.clone();
let block_id = block_id.sha256(ctx)?;
let block_id = block_id.sha256(chain_id, height, round)?;

buf.put_i32_le(vote_type.into());
buf.put_i64_le(ctx.height);
buf.put_i64_le(ctx.round as i64);
buf.put_i64_le(height);
buf.put_i64_le(round as i64);

buf.extend(block_id);
buf.extend(state_id);
buf.put(ctx.chain_id.as_bytes());
buf.put(chain_id.as_bytes());

Ok(buf.to_vec())
}
Expand All @@ -150,9 +147,8 @@ pub mod tests {
use alloc::{string::ToString, vec::Vec};

use super::SignBytes;
use crate::{
signatures::SignContext,
types::{Commit, PartSetHeader, SignedMsgType, Vote, VoteExtension, VoteExtensionType},
use crate::types::{
Commit, PartSetHeader, SignedMsgType, Vote, VoteExtension, VoteExtensionType,
};

#[test]
Expand Down Expand Up @@ -183,13 +179,11 @@ pub mod tests {
}),
..Default::default()
};
let ctx = super::SignContext {
chain_id: "some-chain".to_string(),
height: vote.height,
round: vote.round,
};
let chain_id = "some-chain".to_string();
let height = vote.height;
let round = vote.round;

let actual = vote.sign_bytes(&ctx).unwrap();
let actual = vote.sign_bytes(&chain_id, height, round).unwrap();

assert_eq!(expect_sign_bytes, actual);
}
Expand Down Expand Up @@ -219,13 +213,11 @@ pub mod tests {
}),
..Default::default()
};
let ctx = super::SignContext {
chain_id: "some-chain".to_string(),
height: commit.height,
round: commit.round,
};
let chain_id = "some-chain".to_string();
let height = commit.height;
let round = commit.round;

let actual = commit.sign_bytes(&ctx).unwrap();
let actual = commit.sign_bytes(&chain_id, height, round).unwrap();

assert_eq!(expect_sign_bytes, actual);
}
Expand All @@ -238,18 +230,16 @@ pub mod tests {
signature: Default::default(),
};

let ctx = SignContext {
chain_id: "some-chain".to_string(),
height: 1,
round: 2,
};
let chain_id = "some-chain".to_string();
let height = 1;
let round = 2;

let expect_sign_bytes = hex::decode(
"2a0a080102030405060708110100000000000000190200000000000000220a736f6d652d636861696e2801",
)
.unwrap();

let actual = ve.sign_bytes(&ctx).unwrap();
let actual = ve.sign_bytes(&chain_id, height, round).unwrap();

assert_eq!(expect_sign_bytes, actual);
}
Expand Down

0 comments on commit f2287f8

Please sign in to comment.