Skip to content

Commit

Permalink
Fix parsing of JSONs with "quorumSet": null
Browse files Browse the repository at this point in the history
Also slight refactoring.
  • Loading branch information
marfl committed Sep 20, 2023
1 parent 4f7148c commit 2c44ba4
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/io/core_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ pub(crate) struct RawFbas(pub(crate) Vec<RawNode>);
#[serde(rename_all = "camelCase")]
pub(crate) struct RawNode {
pub(crate) public_key: PublicKey,
// If no quorum set is given, we assume that the node is unsatisfiable, i.e., broken.
#[serde(default = "RawQuorumSet::new_unsatisfiable")]
pub(crate) quorum_set: RawQuorumSet,
pub(crate) quorum_set: Option<RawQuorumSet>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) isp: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -24,15 +22,6 @@ pub(crate) struct RawQuorumSet {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub(crate) inner_quorum_sets: Vec<RawQuorumSet>,
}
impl RawQuorumSet {
fn new_unsatisfiable() -> Self {
Self {
threshold: 1,
validators: vec![],
inner_quorum_sets: vec![],
}
}
}
#[serde_as]
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -104,13 +93,18 @@ impl Node {
fn from_raw(raw_node: RawNode, pk_to_id: &HashMap<PublicKey, NodeId>) -> Self {
Node {
public_key: raw_node.public_key,
quorum_set: QuorumSet::from_raw(raw_node.quorum_set, pk_to_id),
// If no quorum set is given, we assume that the node is unsatisfiable, i.e., broken.
quorum_set: if let Some(raw_quorum_set) = raw_node.quorum_set {
QuorumSet::from_raw(raw_quorum_set, pk_to_id)
} else {
QuorumSet::new_unsatisfiable()
},
}
}
fn to_raw(&self, fbas: &Fbas) -> RawNode {
RawNode {
public_key: self.public_key.clone(),
quorum_set: self.quorum_set.to_raw(fbas),
quorum_set: Some(self.quorum_set.to_raw(fbas)),
isp: None,
geo_data: None,
}
Expand Down Expand Up @@ -210,16 +204,16 @@ mod tests {
let expected_quorum_sets = vec![
QuorumSet {
threshold: 1,
validators: vec![].into_iter().collect(),
validators: vec![],
inner_quorum_sets: vec![QuorumSet {
threshold: 2,
validators: vec![0, 1, 2].into_iter().collect(),
validators: vec![0, 1, 2],
inner_quorum_sets: vec![],
}],
},
QuorumSet {
threshold: 3,
validators: vec![0, 1, 2].into_iter().collect(),
validators: vec![0, 1, 2],
inner_quorum_sets: vec![],
},
QuorumSet::new_unsatisfiable(),
Expand Down Expand Up @@ -256,7 +250,7 @@ mod tests {
let expected_quorum_sets = vec![
QuorumSet {
threshold: 2,
validators: vec![0, 1].into_iter().collect(),
validators: vec![0, 1],
inner_quorum_sets: Default::default(),
},
QuorumSet::new(vec![], vec![], 1),
Expand Down Expand Up @@ -293,6 +287,23 @@ mod tests {
assert_eq!(expected, actual);
}

#[test]
fn from_json_parses_null_quorum_sets() {
let input = r#"[
{
"publicKey": "GCGB2",
"quorumSet": null
}]"#;

let expected_quorum_sets = vec![QuorumSet::new(vec![], vec![], 1)];

let fbas = Fbas::from_json_str(input);
let actual_quorum_sets: Vec<QuorumSet> =
fbas.nodes.into_iter().map(|x| x.quorum_set).collect();

assert_eq!(expected_quorum_sets, actual_quorum_sets);
}

#[test]
fn to_json_and_back_results_in_identical_fbas() {
let original = Fbas::new_generic_unconfigured(7);
Expand All @@ -306,7 +317,7 @@ mod tests {
let fbas = Fbas::new();
let quorum_set = QuorumSet {
threshold: 2,
validators: vec![0, 1].into_iter().collect(),
validators: vec![0, 1],
inner_quorum_sets: Default::default(),
};
let expected = RawQuorumSet {
Expand Down

0 comments on commit 2c44ba4

Please sign in to comment.