Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
feat(message): add Section PK to Messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoga07 committed Jun 1, 2021
1 parent ca2ea6a commit 9251792
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 60 deletions.
18 changes: 14 additions & 4 deletions src/agreement/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,13 @@ impl DkgCommand {
message,
} => {
let variant = Variant::DkgMessage { dkg_key, message };
let message =
RoutingMsg::single_src(node, DstLocation::DirectAndUnrouted, variant, None)?;
let message = RoutingMsg::single_src(
node,
DstLocation::DirectAndUnrouted,
variant,
key,
None,
)?;

Ok(Command::send_message_to_nodes(
recipients.clone(),
Expand Down Expand Up @@ -549,8 +554,13 @@ impl DkgCommand {
proof,
non_participants,
};
let message =
RoutingMsg::single_src(node, DstLocation::DirectAndUnrouted, variant, None)?;
let message = RoutingMsg::single_src(
node,
DstLocation::DirectAndUnrouted,
variant,
key,
None,
)?;

Ok(Command::send_message_to_nodes(
recipients.clone(),
Expand Down
77 changes: 45 additions & 32 deletions src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub trait RoutingMsgUtils {
src: SrcAuthority,
dst: DstLocation,
variant: Variant,
section_key: bls::PublicKey,
proof_chain: Option<SecuredLinkedList>,
) -> Result<RoutingMsg, Error>;

Expand All @@ -61,6 +62,7 @@ pub trait RoutingMsgUtils {
node: &Node,
dst: DstLocation,
variant: Variant,
section_key: bls::PublicKey,
proof_chain: Option<SecuredLinkedList>,
) -> Result<RoutingMsg>;

Expand Down Expand Up @@ -174,6 +176,7 @@ impl RoutingMsgUtils for RoutingMsg {
src: SrcAuthority,
dst: DstLocation,
variant: Variant,
section_pk: bls::PublicKey,
proof_chain: Option<SecuredLinkedList>,
) -> Result<RoutingMsg, Error> {
// Create message id from src authority signature
Expand All @@ -187,10 +190,11 @@ impl RoutingMsgUtils for RoutingMsg {
.unwrap_or_default();

let msg = RoutingMsg {
id,
src,
dst,
aggregation: Aggregation::None,
proof_chain,
section_pk,
variant,
proof_chain,
};
Expand All @@ -204,6 +208,7 @@ impl RoutingMsgUtils for RoutingMsg {
src_name: XorName,
dst: DstLocation,
variant: Variant,
section_pk: bls::PublicKey,
proof_chain: SecuredLinkedList,
) -> Result<RoutingMsg, Error> {
let serialized = bincode::serialize(&SignableView {
Expand All @@ -224,7 +229,7 @@ impl RoutingMsgUtils for RoutingMsg {
proof_share,
};

RoutingMsg::new_signed(src, dst, variant, Some(proof_chain))
RoutingMsg::new_signed(src, dst, variant, section_pk, Some(proof_chain))
}

/// Converts the message src authority from `BlsShare` to `Section` on successful accumulation.
Expand Down Expand Up @@ -274,8 +279,9 @@ impl RoutingMsgUtils for RoutingMsg {
node: &Node,
dst: DstLocation,
variant: Variant,
section_pk: bls::PublicKey,
proof_chain: Option<SecuredLinkedList>,
) -> Result<RoutingMsg> {
) -> Result<Self> {
let serialized = bincode::serialize(&SignableView {
dst: &dst,
variant: &variant,
Expand All @@ -288,14 +294,15 @@ impl RoutingMsgUtils for RoutingMsg {
signature,
};

RoutingMsg::new_signed(src, dst, variant, proof_chain)
RoutingMsg::new_signed(src, dst, variant, section_pk, proof_chain)
}

/// Creates a signed message from a section.
/// Note: `proof` isn't verified and is assumed valid.
fn section_src(
plain: PlainMessage,
proof: Proof,
section_pk: bls::PublicKey,
proof_chain: SecuredLinkedList,
) -> Result<RoutingMsg> {
RoutingMsg::new_signed(
Expand All @@ -305,6 +312,7 @@ impl RoutingMsgUtils for RoutingMsg {
},
plain.dst,
plain.variant,
section_pk,
Some(proof_chain),
)
}
Expand Down Expand Up @@ -415,33 +423,33 @@ impl RoutingMsgUtils for RoutingMsg {
self.proof_chain().map(|proof_chain| proof_chain.last_key())
}

// Extend the current message proof chain so it starts at `new_first_key` while keeping the
// last key (and therefore the signature) intact.
// NOTE: This operation doesn't invalidate the signatures because the proof chain is not part of
// the signed data.
fn extend_proof_chain(
mut self,
new_first_key: &bls::PublicKey,
full_chain: &SecuredLinkedList,
) -> Result<RoutingMsg, Error> {
let proof_chain = self
.proof_chain
.as_mut()
.ok_or(ExtendProofChainError::NoProofChain)?;

*proof_chain = match proof_chain.extend(new_first_key, full_chain) {
Ok(chain) => chain,
Err(SecuredLinkedListError::InvalidOperation) => {
// This means the tip of the proof chain is not reachable from `new_first_key`.
// Extend it from the root key of the full chain instead as that should be the
// genesis key which is implicitly trusted.
proof_chain.extend(full_chain.root_key(), full_chain)?
}
Err(error) => return Err(error.into()),
};

RoutingMsg::new_signed(self.src, self.dst, self.variant, self.proof_chain)
}
// // Extend the current message proof chain so it starts at `new_first_key` while keeping the
// // last key (and therefore the signature) intact.
// // NOTE: This operation doesn't invalidate the signatures because the proof chain is not part of
// // the signed data.
// fn extend_proof_chain(
// mut self,
// new_first_key: &bls::PublicKey,
// full_chain: &SecuredLinkedList,
// ) -> Result<RoutingMsg, Error> {
// let proof_chain = self
// .proof_chain
// .as_mut()
// .ok_or(ExtendProofChainError::NoProofChain)?;

// *proof_chain = match proof_chain.extend(new_first_key, full_chain) {
// Ok(chain) => chain,
// Err(SecuredLinkedListError::InvalidOperation) => {
// // This means the tip of the proof chain is not reachable from `new_first_key`.
// // Extend it from the root key of the full chain instead as that should be the
// // genesis key which is implicitly trusted.
// proof_chain.extend(full_chain.root_key(), full_chain)?
// }
// Err(error) => return Err(error.into()),
// };

// RoutingMsg::new_signed(self.src, self.dst, self.variant, self.proof_chain)
// }

fn verify_variant<'a, I>(
&self,
Expand Down Expand Up @@ -577,13 +585,18 @@ mod tests {
&node,
DstLocation::DirectAndUnrouted,
variant,
section_auth.value.section_key.clone(),
Some(full_proof_chain.truncate(1)),
)?;

assert_eq!(message.verify(iter::once(&pk1))?, VerifyStatus::Full);
assert_eq!(message.verify(iter::once(&pk0))?, VerifyStatus::Unknown);

let message = message.extend_proof_chain(&pk0, &full_proof_chain)?;
let message = message.extend_proof_chain(
&pk0,
section_auth.value.section_key.clone(),
&full_proof_chain,
)?;

assert_eq!(message.verify(iter::once(&pk0))?, VerifyStatus::Full);

Expand Down
14 changes: 11 additions & 3 deletions src/routing/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,13 @@ impl<'a> State<'a> {
info!("Sending {:?} to {:?}", join_request, recipients);

let variant = Variant::JoinRequest(Box::new(join_request));
let message =
RoutingMsg::single_src(&self.node, DstLocation::DirectAndUnrouted, variant, None)?;
let message = RoutingMsg::single_src(
&self.node,
DstLocation::DirectAndUnrouted,
variant,
section_key,
None,
)?;

let _ = self
.send_tx
Expand Down Expand Up @@ -792,9 +797,10 @@ mod tests {
DstLocation::DirectAndUnrouted,
Variant::NodeApproval {
genesis_key: pk,
section_auth,
section_auth: section_auth.clone(),
member_info,
},
section_auth.value.section_key,
Some(proof_chain),
)?;

Expand Down Expand Up @@ -1243,6 +1249,7 @@ mod tests {
section_auth: gen_section_authority_provider(bad_prefix, ELDER_SIZE).0,
section_key: bls::SecretKey::random().public_key(),
},
section_key,
None,
)?;

Expand All @@ -1266,6 +1273,7 @@ mod tests {
section_auth: gen_section_authority_provider(good_prefix, ELDER_SIZE).0,
section_key: bls::SecretKey::random().public_key(),
},
section_key,
None,
)?;

Expand Down
42 changes: 36 additions & 6 deletions src/routing/core/anti_entropy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub(crate) fn process(
last_known_key: Some(*key),
msg: Box::new(msg.clone()),
},
section.authority_provider().section_key,
None,
)?;
actions.send.push(msg);
Expand All @@ -75,6 +76,7 @@ pub(crate) fn process(
last_known_key: None,
msg: Box::new(msg.clone()),
},
section.authority_provider().section_key,
None,
)?;
actions.send.push(msg);
Expand Down Expand Up @@ -118,7 +120,13 @@ pub(crate) fn process(
src_info: (section_auth.clone(), chain),
msg: None,
};
let msg = RoutingMsg::single_src(node, dst, variant, None)?;
let msg = RoutingMsg::single_src(
node,
dst,
variant,
section.authority_provider().section_key,
None,
)?;
actions.send.push(msg);
return Ok((actions, true));
}
Expand Down Expand Up @@ -153,7 +161,11 @@ mod tests {
let env = Env::new(1)?;

let proof_chain = SecuredLinkedList::new(env.their_pk);
let msg = env.create_message(&env.their_prefix, proof_chain)?;
let msg = env.create_message(
&env.their_prefix,
env.section.authority_provider().section_key,
proof_chain,
)?;
let dest_info = DestInfo {
dest: XorName::random(),
dest_section_pk: *env.section.chain().last_key(),
Expand Down Expand Up @@ -191,7 +203,11 @@ mod tests {
.0,
)?;

let msg = env.create_message(&env.their_prefix, proof_chain)?;
let msg = env.create_message(
&env.their_prefix,
env.section.authority_provider().section_key,
proof_chain,
)?;
let dest_info = DestInfo {
dest: env.node.name(),
dest_section_pk: *env.section.chain().last_key(),
Expand Down Expand Up @@ -234,7 +250,11 @@ mod tests {
env.our_sk.sign(&bincode::serialize(&our_new_pk)?),
)?;

let msg = env.create_message(env.section.prefix(), proof_chain)?;
let msg = env.create_message(
env.section.prefix(),
env.section.authority_provider().section_key,
proof_chain,
)?;
let dest_info = DestInfo {
dest: env.node.name(),
dest_section_pk: our_new_pk,
Expand All @@ -260,7 +280,11 @@ mod tests {
let env = Env::new(2)?;

let proof_chain = SecuredLinkedList::new(env.their_pk);
let msg = env.create_message(&env.their_prefix, proof_chain)?;
let msg = env.create_message(
&env.their_prefix,
env.section.authority_provider().section_key,
proof_chain,
)?;
let dest_info = DestInfo {
dest: XorName::random(),
dest_section_pk: *env.section.chain().root_key(),
Expand Down Expand Up @@ -291,7 +315,11 @@ mod tests {
let env = Env::new(2)?;

let proof_chain = SecuredLinkedList::new(*env.section.chain().root_key());
let msg = env.create_message(env.section.prefix(), proof_chain)?;
let msg = env.create_message(
env.section.prefix(),
env.section.authority_provider().section_key,
proof_chain,
)?;
let dest_info = DestInfo {
dest: XorName::random(),
dest_section_pk: *env.section.chain().root_key(),
Expand Down Expand Up @@ -358,6 +386,7 @@ mod tests {
fn create_message(
&self,
src_section: &Prefix,
section_pk: bls::PublicKey,
proof_chain: SecuredLinkedList,
) -> Result<RoutingMsg> {
let sender = Node::new(
Expand All @@ -369,6 +398,7 @@ mod tests {
&sender,
DstLocation::Section(self.node.name()),
Variant::UserMessage(b"hello".to_vec()),
section_pk,
Some(proof_chain),
)?)
}
Expand Down
5 changes: 4 additions & 1 deletion src/routing/core/messaging/handling/agreement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl Core {
Ok(vec![self.handle_accumulate_at_src_agreement(
*message,
proof_chain,
self.section.authority_provider().section_key,
proof,
DestInfo {
dest: dest_name,
Expand Down Expand Up @@ -235,6 +236,7 @@ impl Core {
section: self.section.clone(),
network: self.network.clone(),
},
self.section.authority_provider().section_key,
None,
)?;
let len = sync_recipients.len();
Expand Down Expand Up @@ -298,11 +300,12 @@ impl Core {
fn handle_accumulate_at_src_agreement(
&self,
message: PlainMessage,
section_pk: bls::PublicKey,
proof_chain: SecuredLinkedList,
proof: Proof,
dest_info: DestInfo,
) -> Result<Command> {
let message = RoutingMsg::section_src(message, proof, proof_chain)?;
let message = RoutingMsg::section_src(message, proof, section_pk, proof_chain)?;

Ok(Command::HandleMessage {
message,
Expand Down

0 comments on commit 9251792

Please sign in to comment.