Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: support ThresholdRecoverRaw in tenderdash v0.14 #43

Merged
merged 14 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion abci/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
version = "0.14.0-dev.5"
version = "0.14.0-dev.6"
name = "tenderdash-abci"
edition = "2021"
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion abci/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl<'a, App: RequestDispatcher + 'a> ServerBuilder<App> {
let _guard = server_runtime.handle.enter();

// No cancel is defined, so we add some "mock"
let cancel = self.cancel.unwrap_or(CancellationToken::new());
let cancel = self.cancel.unwrap_or_default();

let server = match bind_address.scheme() {
#[cfg(feature = "tcp")]
Expand Down
12 changes: 6 additions & 6 deletions abci/src/server/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,12 @@ mod test {
let codec = tokio_util::codec::Framed::new(server, super::Coder {});

let worker_cancel = cancel.clone();
let hdl = tokio::spawn(
async move {
super::Codec::process_worker_queues(codec, request_tx, response_rx, worker_cancel)
}
.await,
);
let hdl = tokio::spawn(super::Codec::process_worker_queues(
codec,
request_tx,
response_rx,
worker_cancel,
));

// We send 2 requests over the wire
for n_requests in 0..5 {
Expand Down
173 changes: 142 additions & 31 deletions abci/src/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,32 @@ const VOTE_EXTENSION_REQUEST_ID_PREFIX: &str = "dpevote";
/// SignDigest returns message digest that should be provided directly to a
/// signing/verification function (aka Sign ID)
pub trait SignDigest {
#[deprecated = "replaced with sign_hash() to unify naming between core, platform and tenderdash"]
fn sign_digest(
shumkov marked this conversation as resolved.
Show resolved Hide resolved
&self,
chain_id: &str,
quorum_type: u8,
quorum_hash: &[u8; 32],
height: i64,
round: i32,
) -> Result<Vec<u8>, Error> {
self.sign_hash(chain_id, quorum_type, quorum_hash, height, round)
}

/// Returns message digest that should be provided directly to a
/// signing/verification function.
fn sign_hash(
&self,
chain_id: &str,
quorum_type: u8,
quorum_hash: &[u8; 32],
height: i64,
round: i32,
) -> Result<Vec<u8>, Error>;
}

impl SignDigest for Commit {
fn sign_digest(
fn sign_hash(
&self,
chain_id: &str,
quorum_type: u8,
Expand All @@ -60,7 +74,7 @@ impl SignDigest for Commit {
let request_id = sign_request_id(VOTE_REQUEST_ID_PREFIX, height, round);
let sign_bytes_hash = self.sha256(chain_id, height, round)?;

let digest = sign_digest(
let digest = sign_hash(
quorum_type,
quorum_hash,
request_id[..]
Expand All @@ -81,7 +95,7 @@ impl SignDigest for Commit {
}

impl SignDigest for CanonicalVote {
fn sign_digest(
fn sign_hash(
&self,
chain_id: &str,
quorum_type: u8,
Expand All @@ -93,7 +107,7 @@ impl SignDigest for CanonicalVote {
let request_id = sign_request_id(VOTE_REQUEST_ID_PREFIX, height, round);
let sign_bytes_hash = self.sha256(chain_id, height, round)?;

let digest = sign_digest(
let digest = sign_hash(
quorum_type,
quorum_hash,
request_id[..]
Expand All @@ -114,23 +128,61 @@ impl SignDigest for CanonicalVote {
}

impl SignDigest for VoteExtension {
fn sign_digest(
fn sign_hash(
&self,
chain_id: &str,
quorum_type: u8,
quorum_hash: &[u8; 32],
height: i64,
round: i32,
) -> Result<Vec<u8>, Error> {
let request_id = sign_request_id(VOTE_EXTENSION_REQUEST_ID_PREFIX, height, round);
let sign_bytes_hash = self.sha256(chain_id, height, round)?;

Ok(sign_digest(
let (request_id, sign_bytes_hash) = match self.r#type() {
VoteExtensionType::ThresholdRecover => {
let request_id = sign_request_id(VOTE_EXTENSION_REQUEST_ID_PREFIX, height, round);
let sign_bytes_hash = self.sha256(chain_id, height, round)?;

(request_id, sign_bytes_hash)
},

VoteExtensionType::ThresholdRecoverRaw => {
let mut sign_bytes_hash = self.extension.clone();
sign_bytes_hash.reverse();

let request_id = self.sign_request_id.clone().unwrap_or_default();
let request_id = if request_id.is_empty() {
sign_request_id(VOTE_EXTENSION_REQUEST_ID_PREFIX, height, round)
} else {
// we do double-sha256, and then reverse bytes
let mut request_id = lhash::sha256(&lhash::sha256(&request_id));
request_id.reverse();
request_id.to_vec()
};

(request_id, sign_bytes_hash)
},

VoteExtensionType::Default => unimplemented!(
"vote extension of type {:?} cannot be signed",
self.r#type()
),
};
let sign_hash = sign_hash(
quorum_type,
quorum_hash,
request_id[..].try_into().unwrap(),
request_id[..]
.try_into()
.expect("invalid request ID length"),
&sign_bytes_hash,
))
);

tracing::trace!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are gonna remove it before merge right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added comment to remove once withdrawals are done

digest=hex::encode(&sign_hash),
?quorum_type,
quorum_hash=hex::encode(quorum_hash),
request_id=hex::encode(request_id),
vote_extension=?self, "vote extension sign hash");

Ok(sign_hash)
}
}

Expand All @@ -142,7 +194,7 @@ fn sign_request_id(prefix: &str, height: i64, round: i32) -> Vec<u8> {
lhash::sha256(&buf).to_vec()
}

fn sign_digest(
fn sign_hash(
quorum_type: u8,
quorum_hash: &[u8; 32],
request_id: &[u8; 32],
Expand Down Expand Up @@ -318,20 +370,24 @@ impl SignBytes for CanonicalVote {

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

Ok(ve.encode_length_delimited_to_vec())
},
VoteExtensionType::ThresholdRecoverRaw => Ok(self.extension.to_vec()),
Fixed Show fixed Hide fixed
_ => Err(Error::Canonical(format!(
"unimplemented: vote extension of type {:?} cannot be signed",
self.r#type()
))),
}
let ve = CanonicalVoteExtension {
chain_id: chain_id.to_string(),
extension: self.extension.clone(),
height,
round: round as i64,
r#type: self.r#type,
};

Ok(ve.encode_length_delimited_to_vec())
}
}

Expand All @@ -340,8 +396,11 @@ pub mod tests {
use std::{string::ToString, vec::Vec};

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

#[test]
Expand Down Expand Up @@ -416,11 +475,12 @@ pub mod tests {
}

#[test]
fn vote_extension_sign_bytes() {
fn vote_extension_threshold_sign_bytes() {
let ve = VoteExtension {
extension: Vec::from([1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8]),
r#type: VoteExtensionType::ThresholdRecover.into(),
signature: Default::default(),
sign_request_id: None,
};

let chain_id = "some-chain".to_string();
Expand All @@ -437,6 +497,40 @@ pub mod tests {
assert_eq!(expect_sign_bytes, actual);
}

/// test vector for threshold-raw vote extensions
///
/// Returns expected sig hash and vote extension
fn ve_threshold_raw() -> ([u8; 32], VoteExtension) {
let ve = VoteExtension {
extension: [1, 2, 3, 4, 5, 6, 7, 8].repeat(4),
r#type: VoteExtensionType::ThresholdRecoverRaw.into(),
signature: Default::default(),
sign_request_id: Some("dpevote-someSignRequestID".as_bytes().to_vec()),
};
let expected_sign_hash: [u8; 32] = [
0xe, 0x88, 0x8d, 0xa8, 0x97, 0xf1, 0xc0, 0xfd, 0x6a, 0xe8, 0x3b, 0x77, 0x9b, 0x5, 0xdd,
0x28, 0xc, 0xe2, 0x58, 0xf6, 0x4c, 0x86, 0x1, 0x34, 0xfa, 0x4, 0x27, 0xe1, 0xaa, 0xab,
0x1a, 0xde,
];

(expected_sign_hash, ve)
}

#[test]
fn test_ve_threshold_raw_sign_bytes() {
let (_, ve) = ve_threshold_raw();
let expected_sign_bytes = ve.extension.clone();

// chain_id, height and round are unused
let chain_id = String::new();
let height = -1;
let round = -1;

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

assert_eq!(expected_sign_bytes, actual);
}

#[test]
fn test_sign_digest() {
let quorum_hash: [u8; 32] =
Expand All @@ -452,11 +546,28 @@ pub mod tests {
hex::decode("0CA3D5F42BDFED0C4FDE7E6DE0F046CC76CDA6CEE734D65E8B2EE0E375D4C57D")
.unwrap();

let expect_sign_id =
let expect_sign_hash =
hex::decode("DA25B746781DDF47B5D736F30B1D9D0CC86981EEC67CBE255265C4361DEF8C2E")
.unwrap();

let sign_id = super::sign_digest(100, &quorum_hash, request_id, &sign_bytes_hash);
assert_eq!(expect_sign_id, sign_id); // 194,4
let sign_hash = super::sign_hash(100, &quorum_hash, request_id, &sign_bytes_hash);
assert_eq!(expect_sign_hash, sign_hash); // 194,4
}

#[test]
fn test_ve_threshold_raw_sign_digest() {
const QUORUM_TYPE: u8 = 106;
let quorum_hash: [u8; 32] = [8u8, 7, 6, 5, 4, 3, 2, 1]
.repeat(4)
.try_into()
.expect("invalid quorum hash length");
let (expected_sign_hash, ve) = ve_threshold_raw();

// height, round, chain id are not used in sign digest for threshold-raw
let sign_hash = ve
.sign_hash("", QUORUM_TYPE, &quorum_hash, -1, -1)
.expect("sign digest failed");

assert_eq!(sign_hash, expected_sign_hash);
}
}
17 changes: 9 additions & 8 deletions abci/tests/common/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ impl TenderdashDocker {
app_address: &str,
) -> TenderdashDocker {
// let tag = String::from(tenderdash_proto::VERSION);
let tag = match tag {
None => tenderdash_proto::meta::TENDERDASH_VERSION,
Some("") => tenderdash_proto::meta::TENDERDASH_VERSION,
Some(tag) => tag,
let tag = match tag.unwrap_or_default() {
"" => tenderdash_proto::meta::TENDERDASH_VERSION,
tag => tag,
};

let app_address = url::Url::parse(app_address).expect("invalid app address");
Expand Down Expand Up @@ -153,14 +152,14 @@ impl TenderdashDocker {
None
};

let app_address = app_address.to_string().replace("/", "\\/");
let app_address = app_address.to_string().replace('/', "\\/");

debug!("Tenderdash will connect to ABCI address: {}", app_address);
let container_config = Config {
image: Some(self.image.clone()),
env: Some(vec![format!("PROXY_APP={}", app_address)]),
host_config: Some(HostConfig {
binds: binds,
binds,
..Default::default()
}),
..Default::default()
Expand Down Expand Up @@ -215,7 +214,7 @@ impl TenderdashDocker {
let mut dest = tokio::io::BufWriter::new(stderror);

let mut logs = docker.logs(
&id,
id,
Some(bollard::container::LogsOptions {
follow: false,
stdout: true,
Expand Down Expand Up @@ -269,6 +268,8 @@ impl Drop for TenderdashDocker {
pub fn setup_td_logs_panic(td_docker: &Arc<TenderdashDocker>) {
let weak_ref = Arc::downgrade(td_docker);
std::panic::set_hook(Box::new(move |_| {
weak_ref.upgrade().map(|td| td.print_logs());
if let Some(td) = weak_ref.upgrade() {
td.print_logs()
}
}));
}
4 changes: 2 additions & 2 deletions abci/tests/kvstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod common;

use std::{
collections::{BTreeMap, BTreeSet},
mem,
ops::Deref,
sync::{RwLock, RwLockWriteGuard},
};
Expand Down Expand Up @@ -95,7 +94,7 @@ impl KVStore {
}

pub(crate) fn commit(&mut self) {
let pending_operations = mem::replace(&mut self.pending_operations, BTreeSet::new());
let pending_operations = std::mem::take(&mut self.pending_operations);
pending_operations
.into_iter()
.for_each(|op| op.apply(&mut self.persisted_state));
Expand Down Expand Up @@ -321,6 +320,7 @@ impl Application for KVStoreABCI<'_> {
vote_extensions: vec![proto::abci::ExtendVoteExtension {
r#type: proto::types::VoteExtensionType::ThresholdRecover as i32,
extension: height,
sign_request_id: None,
}],
})
}
Expand Down
Loading
Loading