-
Notifications
You must be signed in to change notification settings - Fork 225
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
CommitSig refactor #277
CommitSig refactor #277
Changes from 4 commits
f381650
f35cf8e
e6eb53e
b91bfaf
3125b77
d214e9d
a23d019
c634764
ad16064
fe15f62
dee63d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,119 @@ | ||
//! CommitSig within Commit | ||
|
||
use crate::serializers; | ||
use crate::serializers::BlockIDFlag; | ||
use crate::serializers::RawCommitSig; | ||
use crate::{account, Signature, Time}; | ||
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
/// BlockIDFlag is used to indicate the validator has voted either for nil, a particular BlockID or was absent. | ||
#[derive(Copy, Clone, Debug, PartialEq)] | ||
pub enum BlockIDFlag { | ||
/// BlockIDFlagAbsent - no vote was received from a validator. | ||
BlockIDFlagAbsent = 1, | ||
/// BlockIDFlagCommit - voted for the Commit.BlockID. | ||
BlockIDFlagCommit = 2, | ||
/// BlockIDFlagNil - voted for nil. | ||
BlockIDFlagNil = 3, | ||
} | ||
|
||
impl BlockIDFlag { | ||
/// Deserialize this type from a byte | ||
pub fn from_u8(byte: u8) -> Option<BlockIDFlag> { | ||
match byte { | ||
1 => Some(BlockIDFlag::BlockIDFlagAbsent), | ||
2 => Some(BlockIDFlag::BlockIDFlagCommit), | ||
3 => Some(BlockIDFlag::BlockIDFlagNil), | ||
_ => None, | ||
} | ||
} | ||
|
||
/// Serialize this type as a byte | ||
pub fn to_u8(self) -> u8 { | ||
self as u8 | ||
} | ||
|
||
/// Serialize this type as a 32-bit unsigned integer | ||
pub fn to_u32(self) -> u32 { | ||
self as u32 | ||
} | ||
} | ||
|
||
impl Serialize for BlockIDFlag { | ||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { | ||
self.to_u8().serialize(serializer) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for BlockIDFlag { | ||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { | ||
let byte = u8::deserialize(deserializer)?; | ||
BlockIDFlag::from_u8(byte) | ||
.ok_or_else(|| D::Error::custom(format!("invalid block ID flag: {}", byte))) | ||
} | ||
} | ||
use serde::{Deserialize, Serialize}; | ||
use std::convert::TryFrom; | ||
|
||
/// CommitSig represents a signature of a validator. | ||
/// It's a part of the Commit and can be used to reconstruct the vote set given the validator set. | ||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] | ||
pub struct CommitSig { | ||
/// Block ID FLag | ||
pub block_id_flag: BlockIDFlag, | ||
|
||
/// Validator address | ||
#[serde(deserialize_with = "serializers::parse_non_empty_id")] | ||
pub validator_address: Option<account::Id>, | ||
|
||
/// Timestamp | ||
pub timestamp: Time, | ||
|
||
/// Signature | ||
#[serde(deserialize_with = "serializers::parse_non_empty_signature")] | ||
pub signature: Option<Signature>, | ||
#[serde(try_from = "RawCommitSig")] | ||
pub enum CommitSig { | ||
/// no vote was received from a validator. | ||
// <<< Compatibility code for https://github.com/informalsystems/tendermint-rs/issues/260 | ||
BlockIDFlagAbsent, | ||
// === Real code after compatibility issue is resolved | ||
/* | ||
BlockIDFlagAbsent { | ||
/// Validator address | ||
validator_address: account::Id, | ||
}, | ||
*/ | ||
// >>> end of real code | ||
/// voted for the Commit.BlockID. | ||
BlockIDFlagCommit { | ||
/// Validator address | ||
validator_address: account::Id, | ||
/// Timestamp of vote | ||
timestamp: Time, | ||
/// Signature of vote | ||
signature: Signature, | ||
}, | ||
/// voted for nil. | ||
BlockIDFlagNil { | ||
/// Validator address | ||
validator_address: account::Id, | ||
/// Timestamp of vote | ||
timestamp: Time, | ||
/// Signature of vote | ||
signature: Signature, | ||
}, | ||
} | ||
|
||
impl CommitSig { | ||
/// Checks if a validator's vote is absent | ||
pub fn is_absent(&self) -> bool { | ||
self.block_id_flag == BlockIDFlag::BlockIDFlagAbsent | ||
impl TryFrom<RawCommitSig> for CommitSig { | ||
type Error = &'static str; | ||
|
||
// Todo: @melekes asked: where's validator_address validation? | ||
fn try_from(value: RawCommitSig) -> Result<Self, Self::Error> { | ||
// Validate CommitSig (strict) | ||
match value.block_id_flag { | ||
BlockIDFlag::BlockIDFlagAbsent => { | ||
if value.timestamp.is_some() { | ||
// <<< Compatibility code for https://github.com/informalsystems/tendermint-rs/issues/259 | ||
if value.timestamp.unwrap() | ||
!= Time::parse_from_rfc3339("0001-01-01T00:00:00Z").unwrap() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
{ | ||
return Err("timestamp is present for BlockIDFlagAbsent CommitSig"); | ||
} | ||
// === Real code after compatibility issue is resolved | ||
/* | ||
return Err("timestamp is present for BlockIDFlagAbsent CommitSig"); | ||
*/ | ||
// >>> end of real code | ||
} | ||
if value.signature.is_some() { | ||
return Err("signature is present for BlockIDFlagAbsent CommitSig"); | ||
} | ||
// <<< Compatibility code for https://github.com/informalsystems/tendermint-rs/issues/260 | ||
Ok(CommitSig::BlockIDFlagAbsent) | ||
// === Real code after compatibility issue is resolved | ||
/* | ||
Ok(CommitSig::BlockIDFlagAbsent { | ||
validator_address: value.validator_address, | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did we actually clarify if that will be changed on the go-side too? (will the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems to me that the issue was acknowledged on the Go side. It was tagged as a consensus-related issue and there's 73 of that open. Also, based on the note, the fix for this might be postponed or fully cancelled because of discussions on refactoring messaging.
|
||
*/ | ||
// >>> end of real code | ||
} | ||
BlockIDFlag::BlockIDFlagCommit => { | ||
if value.timestamp.is_none() { | ||
Err("timestamp is null for BlockIDFlagCommit CommitSig") | ||
greg-szabo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else if value.signature.is_none() { | ||
Err("signature is null for BlockIDFlagCommit CommitSig") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/null/missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} else { | ||
Ok(CommitSig::BlockIDFlagCommit { | ||
// <<< Compatibility code for https://github.com/informalsystems/tendermint-rs/issues/260 | ||
validator_address: value.validator_address.unwrap(), | ||
liamsi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// === Real code after compatibility issue is resolved | ||
/* | ||
validator_address: value.validator_address, | ||
*/ | ||
// >>> end of real code | ||
timestamp: value.timestamp.unwrap(), | ||
signature: value.signature.unwrap(), | ||
}) | ||
} | ||
} | ||
BlockIDFlag::BlockIDFlagNil => { | ||
if value.timestamp.is_none() { | ||
Err("timestamp is null for BlockIDFlagNil CommitSig") | ||
} else if value.signature.is_none() { | ||
Err("signature is null for BlockIDFlagNil CommitSig") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/missing/null/g There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} else { | ||
Ok(CommitSig::BlockIDFlagNil { | ||
// <<< Compatibility code for https://github.com/informalsystems/tendermint-rs/issues/260 | ||
validator_address: value.validator_address.unwrap(), | ||
liamsi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// === Real code after compatibility issue is resolved | ||
/* | ||
validator_address: value.validator_address, | ||
*/ | ||
// >>> end of real code | ||
timestamp: value.timestamp.unwrap(), | ||
signature: value.signature.unwrap(), | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I realize what he was asking: the validator_address is an account::Id which is deserialized from a hexstring into a 20 long byte array. That is restricted during deserialization. Anything else will panic. (Except in the case of CommitSig, where the Go code enables empty strings as no-address values.)
If that was the question and the answer is good enough, I'd like to remove the comment about it.