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 3 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/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
82 changes: 68 additions & 14 deletions abci/src/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,20 +318,24 @@

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 Down Expand Up @@ -416,11 +420,12 @@
}

#[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 +442,27 @@
assert_eq!(expect_sign_bytes, actual);
}

#[test]
fn vote_extension_threshold_raw_sign_bytes() {
const EXTENSION: &[u8] = &[1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8];
let ve = VoteExtension {
extension: EXTENSION.to_vec(),
r#type: VoteExtensionType::ThresholdRecoverRaw.into(),
signature: Default::default(),
sign_request_id: Some("dpe-sign-request-id".as_bytes().to_vec()),
};

let chain_id = "some-chain".to_string();
let height = 1;
let round = 2;

let expect_sign_bytes = EXTENSION.to_vec();

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

assert_eq!(expect_sign_bytes, actual);
}

#[test]
fn test_sign_digest() {
let quorum_hash: [u8; 32] =
Expand All @@ -459,4 +485,32 @@
let sign_id = super::sign_digest(100, &quorum_hash, request_id, &sign_bytes_hash);
assert_eq!(expect_sign_id, sign_id); // 194,4
}

#[test]
fn test_raw_extension_sign_digest() {
const QUORUM_TYPE: u8 = 106;

let quorum_hash: [u8; 32] =
hex::decode("dddabfe1c883dd8a2c71c4281a4212c3715a61f87d62a99aaed0f65a0506c053")
.unwrap()
.try_into()
.unwrap();

let request_id =
hex::decode("922a8fc39b6e265ca761eaaf863387a5e2019f4795a42260805f5562699fd9fa")
.unwrap();
let request_id = request_id[..].try_into().unwrap();

let sign_bytes_hash =
hex::decode("7dfb2432d37f004c4eb2b9aebf601ba4ad59889b81d2e8c7029dce3e0bf8381c")
.unwrap();

let mut expect_sign_id =
hex::decode("6d98f773cef8484432c4946c6b96e04aab39fd119c77de2f21d668dd17d5d2f6")
.unwrap();
expect_sign_id.reverse();

let sign_id = super::sign_digest(QUORUM_TYPE, &quorum_hash, request_id, &sign_bytes_hash);
assert_eq!(expect_sign_id, sign_id);
}
}
10 changes: 6 additions & 4 deletions abci/tests/common/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,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 +215,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 +269,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
6 changes: 3 additions & 3 deletions abci/tests/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tenderdash_abci::proto;
fn test_ipv4_server() {
// we assume the host uses default Docker network configuration, with the host
// using 172.17.0.1
let bind_address = format!("tcp://172.17.0.1:1234");
let bind_address = "tcp://172.17.0.1:1234".to_string();

tcp_server_test("v4", bind_address.as_str());
}
Expand All @@ -27,7 +27,7 @@ fn test_ipv4_server() {
fn test_ipv6_server() {
// we assume the host uses default Docker network configuration, with the host
// using 172.17.0.1. This is IPv6 notation of the IPv4 address.
let bind_address = format!("tcp://[::ffff:ac11:1]:5678");
let bind_address = "tcp://[::ffff:ac11:1]:5678".to_string();

tcp_server_test("v6", bind_address.as_str());
}
Expand All @@ -50,7 +50,7 @@ fn tcp_server_test(test_name: &str, bind_address: &str) {

let app = TestDispatcher {};

let server = ServerBuilder::new(app, &bind_address)
let server = ServerBuilder::new(app, bind_address)
.build()
.expect("server failed");
let socket_uri = bind_address.to_string();
Expand Down
9 changes: 8 additions & 1 deletion proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ use std::env;
fn main() {
let version = env!("CARGO_PKG_VERSION");

env::set_var("TENDERDASH_COMMITISH", "v".to_owned() + version);
// check if TENDERDASH_COMMITISH is alrady set; if not, set it to the current
// version
let commitish = env::var("TENDERDASH_COMMITISH").unwrap_or_default();
if commitish.is_empty() {
env::set_var("TENDERDASH_COMMITISH", "v".to_owned() + version);
}

tenderdash_proto_compiler::proto_compile();

println!("cargo:rerun-if-changed=../proto-compiler/src");
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-env-changed=CARGO_PKG_VERSION");
println!("cargo:rerun-if-env-changed=TENDERDASH_COMMITISH");
}
Loading