|
| 1 | +// Copyright 2026 Circle Internet Group, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +//! JSON types for commit certificates over HTTP or JSON-RPC. |
| 18 | +//! |
| 19 | +//! These structs match the minimal certificate payload returned by consensus |
| 20 | +//! `GET /commit?height=…` and by JSON-RPC methods such as `arc_getCertificate`. |
| 21 | +
|
| 22 | +use malachitebft_core_types::{CommitCertificate, CommitSignature, Round}; |
| 23 | +use serde::{Deserialize, Serialize}; |
| 24 | +use serde_with::{base64::Base64, serde_as}; |
| 25 | + |
| 26 | +use crate::signing::Signature; |
| 27 | +use crate::{Address, ArcContext, Height, ValueId}; |
| 28 | + |
| 29 | +/// Signature entry within a commit certificate (base64-encoded signature bytes). |
| 30 | +#[serde_as] |
| 31 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 32 | +pub struct HttpCommitSignature { |
| 33 | + pub address: Address, |
| 34 | + #[serde_as(as = "Base64")] |
| 35 | + pub signature: Vec<u8>, |
| 36 | +} |
| 37 | + |
| 38 | +impl From<CommitSignature<ArcContext>> for HttpCommitSignature { |
| 39 | + fn from(sig: CommitSignature<ArcContext>) -> Self { |
| 40 | + Self { |
| 41 | + address: sig.address, |
| 42 | + signature: sig.signature.to_bytes().to_vec(), |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// Minimal commit certificate JSON (height, round, value id, signatures). |
| 48 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 49 | +pub struct HttpCommitCertificate { |
| 50 | + pub height: u64, |
| 51 | + pub round: i64, |
| 52 | + pub block_hash: ValueId, |
| 53 | + pub signatures: Vec<HttpCommitSignature>, |
| 54 | +} |
| 55 | + |
| 56 | +impl HttpCommitCertificate { |
| 57 | + /// Convert wire JSON into a [`CommitCertificate`]. |
| 58 | + pub fn try_into_commit_certificate(self) -> eyre::Result<CommitCertificate<ArcContext>> { |
| 59 | + let signatures: eyre::Result<Vec<CommitSignature<ArcContext>>> = |
| 60 | + self.signatures |
| 61 | + .into_iter() |
| 62 | + .map(|sig| { |
| 63 | + let sig_bytes: [u8; 64] = sig.signature.try_into().map_err(|v: Vec<u8>| { |
| 64 | + eyre::eyre!("Invalid signature length: {}", v.len()) |
| 65 | + })?; |
| 66 | + Ok(CommitSignature { |
| 67 | + address: sig.address, |
| 68 | + signature: Signature::from_bytes(sig_bytes), |
| 69 | + }) |
| 70 | + }) |
| 71 | + .collect(); |
| 72 | + |
| 73 | + let round_u32: u32 = self.round.try_into().map_err(|e| { |
| 74 | + eyre::eyre!( |
| 75 | + "Invalid round value {}, cannot convert to u32: {}", |
| 76 | + self.round, |
| 77 | + e |
| 78 | + ) |
| 79 | + })?; |
| 80 | + |
| 81 | + Ok(CommitCertificate { |
| 82 | + height: Height::new(self.height), |
| 83 | + round: Round::new(round_u32), |
| 84 | + value_id: self.block_hash, |
| 85 | + commit_signatures: signatures?, |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(test)] |
| 91 | +mod tests { |
| 92 | + use super::*; |
| 93 | + use crate::{Address, BlockHash}; |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn http_commit_certificate_json_roundtrip() { |
| 97 | + let orig = HttpCommitCertificate { |
| 98 | + height: 42, |
| 99 | + round: 3, |
| 100 | + block_hash: ValueId::new(BlockHash::ZERO), |
| 101 | + signatures: vec![], |
| 102 | + }; |
| 103 | + let json = serde_json::to_string(&orig).unwrap(); |
| 104 | + let back: HttpCommitCertificate = serde_json::from_str(&json).unwrap(); |
| 105 | + assert_eq!(orig, back); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn try_into_commit_certificate_empty_signatures() { |
| 110 | + let wire = HttpCommitCertificate { |
| 111 | + height: 10, |
| 112 | + round: 2, |
| 113 | + block_hash: ValueId::new(BlockHash::ZERO), |
| 114 | + signatures: vec![], |
| 115 | + }; |
| 116 | + let cert = wire.try_into_commit_certificate().unwrap(); |
| 117 | + assert_eq!(cert.height.as_u64(), 10); |
| 118 | + assert_eq!(cert.round.as_i64(), 2); |
| 119 | + assert_eq!(cert.value_id, ValueId::new(BlockHash::ZERO)); |
| 120 | + assert!(cert.commit_signatures.is_empty()); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn try_into_rejects_short_signature() { |
| 125 | + let wire = HttpCommitCertificate { |
| 126 | + height: 1, |
| 127 | + round: 0, |
| 128 | + block_hash: ValueId::new(BlockHash::ZERO), |
| 129 | + signatures: vec![HttpCommitSignature { |
| 130 | + address: Address::default(), |
| 131 | + signature: vec![0u8; 32], |
| 132 | + }], |
| 133 | + }; |
| 134 | + assert!(wire.try_into_commit_certificate().is_err()); |
| 135 | + } |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn http_commit_signature_json_roundtrip() { |
| 139 | + let mut b = [0u8; 64]; |
| 140 | + b[0] = 1; |
| 141 | + b[63] = 2; |
| 142 | + let cs = CommitSignature { |
| 143 | + address: Address::default(), |
| 144 | + signature: Signature::from_bytes(b), |
| 145 | + }; |
| 146 | + let http = HttpCommitSignature::from(cs); |
| 147 | + let json = serde_json::to_string(&http).unwrap(); |
| 148 | + let back: HttpCommitSignature = serde_json::from_str(&json).unwrap(); |
| 149 | + assert_eq!(http, back); |
| 150 | + } |
| 151 | +} |
0 commit comments