Skip to content

Commit

Permalink
Merge refresh and proposer set proposals and unify more flows. (#660)
Browse files Browse the repository at this point in the history
* Modify proposers to only come from validator set

* Fix more rust errors

* Updates

* Fix threshold in misbehaviour report

* Remove proposer work in prep for merging with refresh vote

* Remove all substrate proposals, remove separate refresh proposal flow, merge with all proposals

* Fix proposal decoding, opt for simpler trait def

* WIP impl OnSignedProposal trait for tuples to use over DKG metadata + proposals

* Get session rotation working

* Fix tests, clippy, fmt

* Fix tests

* Fix lint

* Fix test, linkt

* Merge master

* Fix merge conflicts

* Update dkg-primitives/src/types.rs

Co-authored-by: Thomas Braun <[email protected]>

* Update dkg-runtime-primitives/src/proposal.rs

Co-authored-by: Thomas Braun <[email protected]>

* Update dkg-runtime-primitives/src/proposal.rs

Co-authored-by: Thomas Braun <[email protected]>

* Remove unused events, use Self::LENGTH when applicable

* Address review comments

* Clippy

---------

Co-authored-by: Thomas Braun <[email protected]>
  • Loading branch information
drewstone and tbraun96 authored Jul 7, 2023
1 parent 7cc8fa7 commit a48d04d
Show file tree
Hide file tree
Showing 65 changed files with 1,071 additions and 2,302 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dkg-mock-blockchain = { path = "dkg-mock-blockchain", default-features = false }
dkg-test-orchestrator = { path = "dkg-test-orchestrator", default-features = false }

futures = "0.3.15"
ethabi = { version = "18.0.0", default-features = false }
clap = { version = "4.0.32", features = ["derive"] }
rand = "0.8.4"
hex-literal = { package = "hex-literal", version = "0.3.3" }
Expand Down
16 changes: 5 additions & 11 deletions dkg-gadget/src/async_protocols/blockchain_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ use crate::{
use codec::Encode;
use curv::{elliptic::curves::Secp256k1, BigInt};
use dkg_primitives::{
types::{
DKGError, DKGMessage, DKGPublicKeyMessage, DKGSignedPayload, SessionId, SignedDKGMessage,
},
types::{DKGError, DKGMessage, SessionId, SignedDKGMessage},
utils::convert_signature,
};
use dkg_runtime_primitives::{
crypto::{AuthorityId, Public},
gossip_messages::{DKGSignedPayload, PublicKeyMessage},
AggregatedPublicKeys, AuthoritySet, MaxAuthorities, MaxProposalLength, UnsignedProposal,
};
use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2020::{
Expand Down Expand Up @@ -67,7 +66,7 @@ pub trait BlockchainInterface: Send + Sync + Unpin {
batch_key: BatchKey,
message: BigInt,
) -> Result<(), DKGError>;
fn gossip_public_key(&self, key: DKGPublicKeyMessage) -> Result<(), DKGError>;
fn gossip_public_key(&self, key: PublicKeyMessage) -> Result<(), DKGError>;
fn store_public_key(
&self,
key: LocalKey<Secp256k1>,
Expand Down Expand Up @@ -220,12 +219,7 @@ where
let mut lock = self.vote_results.write();
let proposals_for_this_batch = lock.entry(batch_key).or_default();

if let Ok(Some(proposal)) = get_signed_proposal::<B, BE, MaxProposalLength>(
&self.backend,
finished_round,
payload_key,
&self.logger,
) {
if let Ok(Some(proposal)) = get_signed_proposal(finished_round, payload_key) {
proposals_for_this_batch.push(proposal);

if proposals_for_this_batch.len() == batch_key.len {
Expand Down Expand Up @@ -260,7 +254,7 @@ where
Ok(())
}

fn gossip_public_key(&self, key: DKGPublicKeyMessage) -> Result<(), DKGError> {
fn gossip_public_key(&self, key: PublicKeyMessage) -> Result<(), DKGError> {
let public_key = key.pub_key.clone();
gossip_public_key::<GE>(
&self.keystore,
Expand Down
8 changes: 4 additions & 4 deletions dkg-gadget/src/async_protocols/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use dkg_primitives::types::{DKGError, DKGMessage, DKGMsgPayload, SessionId, SignedDKGMessage};
use dkg_primitives::types::{DKGError, DKGMessage, NetworkMsgPayload, SessionId, SignedDKGMessage};
use dkg_runtime_primitives::{associated_block_id_acceptable, crypto::Public, MaxAuthorities};
use futures::Stream;
use round_based::Msg;
Expand Down Expand Up @@ -111,9 +111,9 @@ impl TransformIncoming for SignedDKGMessage<Public> {
{
logger.checkpoint_message_raw(self.msg.payload.payload(), "CP-2-incoming");
match (stream_type, &self.msg.payload) {
(ProtocolType::Keygen { .. }, DKGMsgPayload::Keygen(..)) |
(ProtocolType::Offline { .. }, DKGMsgPayload::Offline(..)) |
(ProtocolType::Voting { .. }, DKGMsgPayload::Vote(..)) => {
(ProtocolType::Keygen { .. }, NetworkMsgPayload::Keygen(..)) |
(ProtocolType::Offline { .. }, NetworkMsgPayload::Offline(..)) |
(ProtocolType::Voting { .. }, NetworkMsgPayload::Vote(..)) => {
logger.checkpoint_message_raw(self.msg.payload.payload(), "CP-2.1-incoming");
// only clone if the downstream receiver expects this type
let associated_block_id = stream_type.get_associated_block_id();
Expand Down
8 changes: 4 additions & 4 deletions dkg-gadget/src/async_protocols/keygen/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::{
debug_logger::DebugLogger,
};
use async_trait::async_trait;
use dkg_primitives::types::{DKGError, DKGMessage, DKGMsgPayload, DKGPublicKeyMessage};
use dkg_runtime_primitives::{crypto::Public, MaxAuthorities};
use dkg_primitives::types::{DKGError, DKGMessage, NetworkMsgPayload};
use dkg_runtime_primitives::{crypto::Public, gossip_messages::PublicKeyMessage, MaxAuthorities};
use futures::channel::mpsc::UnboundedSender;
use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2020::state_machine::keygen::{
Keygen, ProtocolMessage,
Expand All @@ -42,7 +42,7 @@ impl<BI: BlockchainInterface + 'static> StateMachineHandler<BI> for Keygen {
let DKGMessage { payload, session_id, .. } = msg.body;
// Send the payload to the appropriate AsyncProtocols
match payload {
DKGMsgPayload::Keygen(msg) => {
NetworkMsgPayload::Keygen(msg) => {
logger.info_keygen(format!(
"Handling Keygen inbound message from id={}, session={}",
msg.sender_id, session_id
Expand Down Expand Up @@ -86,7 +86,7 @@ impl<BI: BlockchainInterface + 'static> StateMachineHandler<BI> for Keygen {
// public_key_gossip.rs:gossip_public_key [2] store public key locally (public_keys.rs:
// store_aggregated_public_keys)
let session_id = params.session_id;
let pub_key_msg = DKGPublicKeyMessage {
let pub_key_msg = PublicKeyMessage {
session_id,
pub_key: local_key.public_key().to_bytes(true).to_vec(),
signature: vec![],
Expand Down
11 changes: 7 additions & 4 deletions dkg-gadget/src/async_protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ pub mod test_utils;
use curv::elliptic::curves::Secp256k1;
use dkg_primitives::{
crypto::Public,
types::{DKGError, DKGKeygenMessage, DKGMessage, DKGMsgPayload, DKGOfflineMessage, SessionId},
types::{DKGError, DKGMessage, NetworkMsgPayload, SessionId},
AuthoritySet,
};
use dkg_runtime_primitives::{MaxAuthorities, UnsignedProposal};
use dkg_runtime_primitives::{
gossip_messages::{DKGKeygenMessage, DKGOfflineMessage},
MaxAuthorities, UnsignedProposal,
};
use futures::{
channel::mpsc::{UnboundedReceiver, UnboundedSender},
Future, StreamExt,
Expand Down Expand Up @@ -599,13 +602,13 @@ where
None => None,
};
let payload = match &proto_ty {
ProtocolType::Keygen { .. } => DKGMsgPayload::Keygen(DKGKeygenMessage {
ProtocolType::Keygen { .. } => NetworkMsgPayload::Keygen(DKGKeygenMessage {
sender_id: party_id,
keygen_msg: serialized_body,
keygen_protocol_hash: keygen_protocol_hash.expect("This value should be set"),
}),
ProtocolType::Offline { unsigned_proposal, .. } =>
DKGMsgPayload::Offline(DKGOfflineMessage {
NetworkMsgPayload::Offline(DKGOfflineMessage {
key: Vec::from(
&unsigned_proposal.hash().expect("Cannot hash unsigned proposal!")
as &[u8],
Expand Down
4 changes: 2 additions & 2 deletions dkg-gadget/src/async_protocols/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use crate::{async_protocols::CurrentRoundBlame, debug_logger::DebugLogger};
use atomic::Atomic;
use dkg_primitives::types::{DKGError, DKGMsgPayload, SessionId, SignedDKGMessage};
use dkg_primitives::types::{DKGError, NetworkMsgPayload, SessionId, SignedDKGMessage};
use dkg_runtime_primitives::{crypto::Public, KEYGEN_TIMEOUT, SIGN_TIMEOUT};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -224,7 +224,7 @@ impl<C> AsyncProtocolRemote<C> {
let can_deliver =
status != MetaHandlerStatus::Complete && status != MetaHandlerStatus::Terminated;
if can_deliver {
if matches!(msg.msg.payload, DKGMsgPayload::Vote(..)) {
if matches!(msg.msg.payload, NetworkMsgPayload::Vote(..)) {
self.tx_voting.send(msg)
} else {
self.tx_keygen_signing.send(msg)
Expand Down
10 changes: 4 additions & 6 deletions dkg-gadget/src/async_protocols/sign/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use curv::{arithmetic::Converter, elliptic::curves::Secp256k1, BigInt};
use dkg_runtime_primitives::UnsignedProposal;
use dkg_runtime_primitives::{gossip_messages::DKGVoteMessage, UnsignedProposal};
use futures::StreamExt;
use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2020::state_machine::{
keygen::LocalKey,
Expand All @@ -32,9 +32,7 @@ use crate::async_protocols::{
ProtocolType, Threshold,
};
use dkg_logging::debug_logger::RoundsEventType;
use dkg_primitives::types::{
DKGError, DKGMessage, DKGMsgPayload, DKGVoteMessage, SignedDKGMessage,
};
use dkg_primitives::types::{DKGError, DKGMessage, NetworkMsgPayload, SignedDKGMessage};
use dkg_runtime_primitives::{crypto::Public, MaxAuthorities};
use futures::FutureExt;
use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2020::{
Expand Down Expand Up @@ -221,7 +219,7 @@ where
DKGError::GenericError { reason: "Partial signature is invalid".to_string() }
})?;

let payload = DKGMsgPayload::Vote(DKGVoteMessage {
let payload = NetworkMsgPayload::Vote(DKGVoteMessage {
party_ind: *offline_i.as_ref(),
// use the hash of proposal as "round key" ONLY for purposes of ensuring
// uniqueness We only want voting to happen amongst voters under the SAME
Expand Down Expand Up @@ -259,7 +257,7 @@ where
while let Some(msg) = incoming_wrapper.next().await {
let payload = msg.body.payload.payload().clone();
params.logger.checkpoint_message_raw(&payload, "CP-Voting-Received");
if let DKGMsgPayload::Vote(dkg_vote_msg) = msg.body.payload {
if let NetworkMsgPayload::Vote(dkg_vote_msg) = msg.body.payload {
// only process messages which are from the respective proposal
if dkg_vote_msg.round_key.as_slice() == hash_of_proposal {
params.logger.checkpoint_message_raw(&payload, "CP-Voting-Received-2");
Expand Down
4 changes: 2 additions & 2 deletions dkg-gadget/src/async_protocols/sign/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
debug_logger::DebugLogger,
};
use async_trait::async_trait;
use dkg_primitives::types::{DKGError, DKGMessage, DKGMsgPayload};
use dkg_primitives::types::{DKGError, DKGMessage, NetworkMsgPayload};
use dkg_runtime_primitives::{crypto::Public, MaxAuthorities, UnsignedProposal};
use futures::channel::mpsc::UnboundedSender;
use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2020::state_machine::sign::{
Expand Down Expand Up @@ -50,7 +50,7 @@ impl<BI: BlockchainInterface + 'static> StateMachineHandler<BI> for OfflineStage

// Send the payload to the appropriate AsyncProtocols
match payload {
DKGMsgPayload::Offline(msg) => {
NetworkMsgPayload::Offline(msg) => {
logger.checkpoint_message_raw(&payload_raw, "CP-2.7-incoming");
let message: Msg<OfflineProtocolMessage> =
match serde_json::from_slice(msg.offline_msg.as_slice()) {
Expand Down
12 changes: 7 additions & 5 deletions dkg-gadget/src/async_protocols/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use crate::{
use codec::Encode;
use curv::{elliptic::curves::Secp256k1, BigInt};
use dkg_primitives::{
types::{
DKGError, DKGMessage, DKGPublicKeyMessage, DKGSignedPayload, SessionId, SignedDKGMessage,
},
types::{DKGError, DKGMessage, SessionId, SignedDKGMessage},
utils::convert_signature,
};
use dkg_runtime_primitives::{crypto::Public, MaxProposalLength, UnsignedProposal};
use dkg_runtime_primitives::{
crypto::Public,
gossip_messages::{DKGSignedPayload, PublicKeyMessage},
MaxProposalLength, UnsignedProposal,
};
use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2020::{
party_i::SignatureRecid, state_machine::keygen::LocalKey,
};
Expand Down Expand Up @@ -88,7 +90,7 @@ impl BlockchainInterface for TestDummyIface {
Ok(())
}

fn gossip_public_key(&self, _key: DKGPublicKeyMessage) -> Result<(), DKGError> {
fn gossip_public_key(&self, _key: PublicKeyMessage) -> Result<(), DKGError> {
// we do not gossip the public key in the test interface
Ok(())
}
Expand Down
14 changes: 6 additions & 8 deletions dkg-gadget/src/gossip_messages/misbehaviour_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ use crate::{
Client,
};
use codec::Encode;
use dkg_primitives::types::{
DKGError, DKGMessage, DKGMisbehaviourMessage, DKGMsgPayload, SignedDKGMessage,
};
use dkg_primitives::types::{DKGError, DKGMessage, NetworkMsgPayload, SignedDKGMessage};
use dkg_runtime_primitives::{
crypto::AuthorityId, AggregatedMisbehaviourReports, DKGApi, MaxAuthorities, MaxProposalLength,
MaxReporters, MaxSignatureLength, MisbehaviourType,
crypto::AuthorityId, gossip_messages::MisbehaviourMessage, AggregatedMisbehaviourReports,
DKGApi, MaxAuthorities, MaxProposalLength, MaxReporters, MaxSignatureLength, MisbehaviourType,
};
use sc_client_api::Backend;
use sp_runtime::traits::{Block, Get, NumberFor};
Expand Down Expand Up @@ -52,7 +50,7 @@ where
return Err(DKGError::NoAuthorityAccounts)
}

if let DKGMsgPayload::MisbehaviourBroadcast(msg) = dkg_msg.payload {
if let NetworkMsgPayload::MisbehaviourBroadcast(msg) = dkg_msg.payload {
dkg_worker.logger.debug("Received misbehaviour report".to_string());

let is_main_round = {
Expand Down Expand Up @@ -116,7 +114,7 @@ where

pub(crate) async fn gossip_misbehaviour_report<B, BE, C, GE>(
dkg_worker: &DKGWorker<B, BE, C, GE>,
report: DKGMisbehaviourMessage,
report: MisbehaviourMessage,
) -> Result<(), DKGError>
where
B: Block,
Expand All @@ -140,7 +138,7 @@ where

if let Ok(signature) = dkg_worker.key_store.sign(&public.clone(), &payload) {
let encoded_signature = signature.encode();
let payload = DKGMsgPayload::MisbehaviourBroadcast(DKGMisbehaviourMessage {
let payload = NetworkMsgPayload::MisbehaviourBroadcast(MisbehaviourMessage {
signature: encoded_signature.clone(),
..report.clone()
});
Expand Down
11 changes: 5 additions & 6 deletions dkg-gadget/src/gossip_messages/public_key_gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ use crate::{
Client, DKGKeystore,
};
use codec::Encode;
use dkg_primitives::types::{
DKGError, DKGMessage, DKGMsgPayload, DKGPublicKeyMessage, SessionId, SignedDKGMessage,
};
use dkg_primitives::types::{DKGError, DKGMessage, NetworkMsgPayload, SessionId, SignedDKGMessage};
use dkg_runtime_primitives::{
crypto::{AuthorityId, Public},
gossip_messages::PublicKeyMessage,
AggregatedPublicKeys, DKGApi, MaxAuthorities, MaxProposalLength,
};
use sc_client_api::Backend;
Expand Down Expand Up @@ -53,7 +52,7 @@ where
return Err(DKGError::NoAuthorityAccounts)
}

if let DKGMsgPayload::PublicKeyBroadcast(msg) = dkg_msg.payload {
if let NetworkMsgPayload::PublicKeyBroadcast(msg) = dkg_msg.payload {
let is_genesis_round = msg.session_id == 0;

let tag = if is_genesis_round { "CURRENT" } else { "NEXT" };
Expand Down Expand Up @@ -127,7 +126,7 @@ pub(crate) fn gossip_public_key<GE>(
key_store: &DKGKeystore,
gossip_engine: Arc<GE>,
aggregated_public_keys: &mut HashMap<SessionId, AggregatedPublicKeys>,
msg: DKGPublicKeyMessage,
msg: PublicKeyMessage,
) where
GE: GossipEngineIface,
MaxProposalLength: Get<u32> + Clone + Send + Sync + 'static + std::fmt::Debug,
Expand All @@ -137,7 +136,7 @@ pub(crate) fn gossip_public_key<GE>(

if let Ok(signature) = key_store.sign(&public, &msg.pub_key) {
let encoded_signature = signature.encode();
let payload = DKGMsgPayload::PublicKeyBroadcast(DKGPublicKeyMessage {
let payload = NetworkMsgPayload::PublicKeyBroadcast(PublicKeyMessage {
signature: encoded_signature.clone(),
..msg.clone()
});
Expand Down
Loading

0 comments on commit a48d04d

Please sign in to comment.