From 9e394115b569e78e1dc10243ddcbe7cc006f95a8 Mon Sep 17 00:00:00 2001 From: danielxiangzl Date: Thu, 17 Aug 2023 16:29:17 -0700 Subject: [PATCH 1/3] adding randomness field in dag node --- consensus/src/consensusdb/consensusdb_test.rs | 10 +++++++++- consensus/src/dag/dag_driver.rs | 1 + consensus/src/dag/tests/helpers.rs | 12 ++++++++++-- consensus/src/dag/tests/types_test.rs | 2 ++ consensus/src/dag/types.rs | 15 +++++++++++++-- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/consensus/src/consensusdb/consensusdb_test.rs b/consensus/src/consensusdb/consensusdb_test.rs index 3add1e81e1781..628e205dec95c 100644 --- a/consensus/src/consensusdb/consensusdb_test.rs +++ b/consensus/src/consensusdb/consensusdb_test.rs @@ -93,7 +93,15 @@ fn test_dag() { let tmp_dir = TempPath::new(); let db = ConsensusDB::new(&tmp_dir); - let node = Node::new(1, 1, Author::random(), 123, Payload::empty(false), vec![]); + let node = Node::new( + 1, + 1, + Author::random(), + 123, + Payload::empty(false), + vec![], + vec![], + ); test_dag_type::::Key>(node.digest(), node.clone(), &db); let certified_node = CertifiedNode::new(node.clone(), AggregateSignature::empty()); diff --git a/consensus/src/dag/dag_driver.rs b/consensus/src/dag/dag_driver.rs index 4ccb4b760c555..3413e6fa2e7a2 100644 --- a/consensus/src/dag/dag_driver.rs +++ b/consensus/src/dag/dag_driver.rs @@ -124,6 +124,7 @@ impl DagDriver { timestamp.as_micros() as u64, payload, strong_links, + vec![], // TODO: add real randomness shares ); self.storage .save_node(&new_node) diff --git a/consensus/src/dag/tests/helpers.rs b/consensus/src/dag/tests/helpers.rs index b6eb7a1c9bad8..ae6fac99f576a 100644 --- a/consensus/src/dag/tests/helpers.rs +++ b/consensus/src/dag/tests/helpers.rs @@ -9,7 +9,7 @@ pub(crate) fn new_certified_node( author: Author, parents: Vec, ) -> CertifiedNode { - let node = Node::new(1, round, author, 0, Payload::empty(false), parents); + let node = Node::new(1, round, author, 0, Payload::empty(false), parents, vec![]); CertifiedNode::new(node, AggregateSignature::empty()) } @@ -19,5 +19,13 @@ pub(crate) fn new_node( author: Author, parents: Vec, ) -> Node { - Node::new(0, round, author, timestamp, Payload::empty(false), parents) + Node::new( + 0, + round, + author, + timestamp, + Payload::empty(false), + parents, + vec![], + ) } diff --git a/consensus/src/dag/tests/types_test.rs b/consensus/src/dag/tests/types_test.rs index a08d13a9d9122..9633ccb48f638 100644 --- a/consensus/src/dag/tests/types_test.rs +++ b/consensus/src/dag/tests/types_test.rs @@ -24,6 +24,7 @@ fn test_node_verify() { NodeMetadata::new_for_test(0, 0, signers[0].author(), 0, HashValue::random()), Payload::empty(false), vec![], + vec![], ); assert_eq!( invalid_node @@ -64,6 +65,7 @@ fn test_certified_node_verify() { NodeMetadata::new_for_test(0, 0, signers[0].author(), 0, HashValue::random()), Payload::empty(false), vec![], + vec![], ); let invalid_certified_node = CertifiedNode::new(invalid_node, AggregateSignature::empty()); assert_eq!( diff --git a/consensus/src/dag/types.rs b/consensus/src/dag/types.rs index 5621334c05969..2c8e075a11823 100644 --- a/consensus/src/dag/types.rs +++ b/consensus/src/dag/types.rs @@ -46,6 +46,7 @@ struct NodeWithoutDigest<'a> { timestamp: u64, payload: &'a Payload, parents: &'a Vec, + shares: &'a Vec, // serialized bytes of randomness shares } impl<'a> CryptoHash for NodeWithoutDigest<'a> { @@ -68,6 +69,7 @@ impl<'a> From<&'a Node> for NodeWithoutDigest<'a> { timestamp: node.metadata.timestamp, payload: &node.payload, parents: &node.parents, + shares: &node.shares, } } } @@ -131,6 +133,7 @@ pub struct Node { metadata: NodeMetadata, payload: Payload, parents: Vec, + shares: Vec, // serialized bytes of randomness shares } impl Node { @@ -141,9 +144,11 @@ impl Node { timestamp: u64, payload: Payload, parents: Vec, + shares: Vec, ) -> Self { - let digest = - Self::calculate_digest_internal(epoch, round, author, timestamp, &payload, &parents); + let digest = Self::calculate_digest_internal( + epoch, round, author, timestamp, &payload, &parents, &shares, + ); Self { metadata: NodeMetadata { @@ -157,6 +162,7 @@ impl Node { }, payload, parents, + shares, } } @@ -165,11 +171,13 @@ impl Node { metadata: NodeMetadata, payload: Payload, parents: Vec, + shares: Vec, ) -> Self { Self { metadata, payload, parents, + shares, } } @@ -181,6 +189,7 @@ impl Node { timestamp: u64, payload: &Payload, parents: &Vec, + shares: &Vec, ) -> HashValue { let node_with_out_digest = NodeWithoutDigest { epoch, @@ -189,6 +198,7 @@ impl Node { timestamp, payload, parents, + shares, }; node_with_out_digest.hash() } @@ -201,6 +211,7 @@ impl Node { self.metadata.timestamp, &self.payload, &self.parents, + &self.shares, ) } From a2f49641f45c3a7bb0067c08301f1c70a9026ca0 Mon Sep 17 00:00:00 2001 From: danielxiangzl Date: Mon, 21 Aug 2023 18:33:28 -0700 Subject: [PATCH 2/3] use enum --- consensus/src/consensusdb/consensusdb_test.rs | 4 +- consensus/src/dag/dag_driver.rs | 4 +- consensus/src/dag/mod.rs | 2 +- consensus/src/dag/tests/helpers.rs | 14 +++++-- consensus/src/dag/tests/types_test.rs | 8 ++-- consensus/src/dag/types.rs | 40 ++++++++++++++----- 6 files changed, 49 insertions(+), 23 deletions(-) diff --git a/consensus/src/consensusdb/consensusdb_test.rs b/consensus/src/consensusdb/consensusdb_test.rs index 628e205dec95c..c8d73d2060922 100644 --- a/consensus/src/consensusdb/consensusdb_test.rs +++ b/consensus/src/consensusdb/consensusdb_test.rs @@ -3,7 +3,7 @@ // SPDX-License-Identifier: Apache-2.0 use super::*; -use crate::dag::{CertifiedNode, Node, Vote}; +use crate::dag::{CertifiedNode, Extensions, Node, Vote}; use aptos_consensus_types::{ block::block_test_utils::certificate_for_genesis, common::{Author, Payload}, @@ -100,7 +100,7 @@ fn test_dag() { 123, Payload::empty(false), vec![], - vec![], + Extensions::empty(), ); test_dag_type::::Key>(node.digest(), node.clone(), &db); diff --git a/consensus/src/dag/dag_driver.rs b/consensus/src/dag/dag_driver.rs index 3413e6fa2e7a2..ae3d398121bc8 100644 --- a/consensus/src/dag/dag_driver.rs +++ b/consensus/src/dag/dag_driver.rs @@ -5,7 +5,7 @@ use super::{ dag_fetcher::FetchRequester, order_rule::OrderRule, storage::DAGStorage, - types::{CertifiedAck, DAGMessage}, + types::{CertifiedAck, DAGMessage, Extensions}, RpcHandler, }; use crate::{ @@ -124,7 +124,7 @@ impl DagDriver { timestamp.as_micros() as u64, payload, strong_links, - vec![], // TODO: add real randomness shares + Extensions::empty(), ); self.storage .save_node(&new_node) diff --git a/consensus/src/dag/mod.rs b/consensus/src/dag/mod.rs index 7b96f4384e315..507f7731cdd20 100644 --- a/consensus/src/dag/mod.rs +++ b/consensus/src/dag/mod.rs @@ -17,4 +17,4 @@ mod tests; mod types; pub use dag_network::RpcHandler; -pub use types::{CertifiedNode, DAGNetworkMessage, Node, NodeId, Vote}; +pub use types::{CertifiedNode, DAGNetworkMessage, Extensions, Node, NodeId, Vote}; diff --git a/consensus/src/dag/tests/helpers.rs b/consensus/src/dag/tests/helpers.rs index ae6fac99f576a..84cad58ce6de1 100644 --- a/consensus/src/dag/tests/helpers.rs +++ b/consensus/src/dag/tests/helpers.rs @@ -1,6 +1,6 @@ // Copyright © Aptos Foundation -use crate::dag::types::{CertifiedNode, Node, NodeCertificate}; +use crate::dag::types::{CertifiedNode, Extensions, Node, NodeCertificate}; use aptos_consensus_types::common::{Author, Payload, Round}; use aptos_types::aggregate_signature::AggregateSignature; @@ -9,7 +9,15 @@ pub(crate) fn new_certified_node( author: Author, parents: Vec, ) -> CertifiedNode { - let node = Node::new(1, round, author, 0, Payload::empty(false), parents, vec![]); + let node = Node::new( + 1, + round, + author, + 0, + Payload::empty(false), + parents, + Extensions::empty(), + ); CertifiedNode::new(node, AggregateSignature::empty()) } @@ -26,6 +34,6 @@ pub(crate) fn new_node( timestamp, Payload::empty(false), parents, - vec![], + Extensions::empty(), ) } diff --git a/consensus/src/dag/tests/types_test.rs b/consensus/src/dag/tests/types_test.rs index 9633ccb48f638..786a91da9d0ba 100644 --- a/consensus/src/dag/tests/types_test.rs +++ b/consensus/src/dag/tests/types_test.rs @@ -4,8 +4,8 @@ use super::helpers::new_node; use crate::dag::{ tests::helpers::new_certified_node, types::{ - CertifiedNode, DagSnapshotBitmask, Node, NodeCertificate, NodeMetadata, RemoteFetchRequest, - TDAGMessage, + CertifiedNode, DagSnapshotBitmask, Extensions, Node, NodeCertificate, NodeMetadata, + RemoteFetchRequest, TDAGMessage, }, }; use aptos_consensus_types::common::Payload; @@ -24,7 +24,7 @@ fn test_node_verify() { NodeMetadata::new_for_test(0, 0, signers[0].author(), 0, HashValue::random()), Payload::empty(false), vec![], - vec![], + Extensions::empty(), ); assert_eq!( invalid_node @@ -65,7 +65,7 @@ fn test_certified_node_verify() { NodeMetadata::new_for_test(0, 0, signers[0].author(), 0, HashValue::random()), Payload::empty(false), vec![], - vec![], + Extensions::empty(), ); let invalid_certified_node = CertifiedNode::new(invalid_node, AggregateSignature::empty()); assert_eq!( diff --git a/consensus/src/dag/types.rs b/consensus/src/dag/types.rs index 2c8e075a11823..be66b5a5e1993 100644 --- a/consensus/src/dag/types.rs +++ b/consensus/src/dag/types.rs @@ -38,6 +38,18 @@ impl TDAGMessage for CertifiedAck { } } +#[derive(Clone, Serialize, Deserialize, CryptoHasher, Debug, PartialEq)] +pub enum Extensions { + Empty, + // Reserved for future extensions such as randomness shares +} + +impl Extensions { + pub fn empty() -> Self { + Self::Empty + } +} + #[derive(Serialize)] struct NodeWithoutDigest<'a> { epoch: u64, @@ -46,7 +58,7 @@ struct NodeWithoutDigest<'a> { timestamp: u64, payload: &'a Payload, parents: &'a Vec, - shares: &'a Vec, // serialized bytes of randomness shares + extensions: &'a Extensions, } impl<'a> CryptoHash for NodeWithoutDigest<'a> { @@ -69,7 +81,7 @@ impl<'a> From<&'a Node> for NodeWithoutDigest<'a> { timestamp: node.metadata.timestamp, payload: &node.payload, parents: &node.parents, - shares: &node.shares, + extensions: &node.extensions, } } } @@ -133,7 +145,7 @@ pub struct Node { metadata: NodeMetadata, payload: Payload, parents: Vec, - shares: Vec, // serialized bytes of randomness shares + extensions: Extensions, } impl Node { @@ -144,10 +156,16 @@ impl Node { timestamp: u64, payload: Payload, parents: Vec, - shares: Vec, + extensions: Extensions, ) -> Self { let digest = Self::calculate_digest_internal( - epoch, round, author, timestamp, &payload, &parents, &shares, + epoch, + round, + author, + timestamp, + &payload, + &parents, + &extensions, ); Self { @@ -162,7 +180,7 @@ impl Node { }, payload, parents, - shares, + extensions, } } @@ -171,13 +189,13 @@ impl Node { metadata: NodeMetadata, payload: Payload, parents: Vec, - shares: Vec, + extensions: Extensions, ) -> Self { Self { metadata, payload, parents, - shares, + extensions, } } @@ -189,7 +207,7 @@ impl Node { timestamp: u64, payload: &Payload, parents: &Vec, - shares: &Vec, + extensions: &Extensions, ) -> HashValue { let node_with_out_digest = NodeWithoutDigest { epoch, @@ -198,7 +216,7 @@ impl Node { timestamp, payload, parents, - shares, + extensions, }; node_with_out_digest.hash() } @@ -211,7 +229,7 @@ impl Node { self.metadata.timestamp, &self.payload, &self.parents, - &self.shares, + &self.extensions, ) } From a0c0082ccfba2a02dfd48662f23ce31ce432dcf7 Mon Sep 17 00:00:00 2001 From: danielxiangzl Date: Tue, 22 Aug 2023 10:34:32 -0700 Subject: [PATCH 3/3] lint --- consensus/src/dag/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consensus/src/dag/mod.rs b/consensus/src/dag/mod.rs index 7f99f94578617..eeddccfe9b07a 100644 --- a/consensus/src/dag/mod.rs +++ b/consensus/src/dag/mod.rs @@ -17,4 +17,4 @@ mod tests; mod types; pub use dag_network::{RpcHandler, RpcWithFallback, TDAGNetworkSender}; -pub use types::{CertifiedNode, DAGMessage, DAGNetworkMessage, Extensions, Node, NodeId, Vote}; \ No newline at end of file +pub use types::{CertifiedNode, DAGMessage, DAGNetworkMessage, Extensions, Node, NodeId, Vote};