using bincode serialization

This commit is contained in:
Jędrzej Stuczyński
2024-02-06 18:10:25 +00:00
parent 36242fa257
commit 7a7fbce8ea
7 changed files with 28 additions and 18 deletions
@@ -34,7 +34,7 @@ impl RecoveryStorage {
if let Ok(mut file) = File::open(&path) {
let mut buff = Vec::new();
if file.read_to_end(&mut buff).is_ok() {
match IssuanceBandwidthCredential::try_from_bytes(&buff) {
match IssuanceBandwidthCredential::try_from_recovered_bytes(&buff) {
Ok(voucher) => vouchers.push(voucher),
Err(err) => {
error!("failed to parse the voucher at {}: {err}", path.display())
+1
View File
@@ -8,6 +8,7 @@ license.workspace = true
[dependencies]
bls12_381 = { workspace = true, default-features = false, features = ["pairings", "alloc", "experimental"] }
bincode = "1.3.3"
cosmrs = { workspace = true }
thiserror = { workspace = true }
log = { workspace = true }
@@ -4,15 +4,12 @@
use crate::coconut::bandwidth::freepass::FreePassIssuanceData;
use crate::coconut::bandwidth::issued::IssuedBandwidthCredential;
use crate::coconut::bandwidth::voucher::BandwidthVoucherIssuanceData;
use crate::coconut::bandwidth::{
bandwidth_voucher_params, CredentialSigningData, CredentialSpendingData, CredentialType,
};
use crate::coconut::utils::scalar_serde_helper;
use crate::coconut::bandwidth::{bandwidth_voucher_params, CredentialSigningData, CredentialType};
use crate::coconut::utils::{make_bincode_serializer, scalar_serde_helper};
use crate::error::Error;
use nym_coconut_interface::{
aggregate_signature_shares, hash_to_scalar, prepare_blind_sign, prove_bandwidth_credential,
Attribute, Parameters, PrivateAttribute, PublicAttribute, Signature, SignatureShare,
VerificationKey,
aggregate_signature_shares, hash_to_scalar, prepare_blind_sign, Attribute, Parameters,
PrivateAttribute, PublicAttribute, Signature, SignatureShare, VerificationKey,
};
use nym_crypto::asymmetric::{encryption, identity};
use nym_validator_client::nyxd::{Coin, Hash};
@@ -240,12 +237,15 @@ impl IssuanceBandwidthCredential {
}
// TODO: is that actually needed?
pub fn to_bytes(&self) -> Vec<u8> {
todo!()
pub fn to_recovery_bytes(&self) -> Vec<u8> {
use bincode::Options;
// safety: our data format is stable and thus the serialization should not fail
make_bincode_serializer().serialize(self).unwrap()
}
// TODO: is that actually needed?
pub fn try_from_bytes(bytes: &[u8]) -> Result<Self, Error> {
todo!()
pub fn try_from_recovered_bytes(bytes: &[u8]) -> Result<Self, Error> {
use bincode::Options;
Ok(make_bincode_serializer().deserialize(bytes)?)
}
}
@@ -9,8 +9,8 @@ use crate::coconut::bandwidth::voucher::BandwidthVoucherIssuedData;
use crate::coconut::bandwidth::{bandwidth_voucher_params, CredentialSpendingData, CredentialType};
use crate::error::Error;
use nym_coconut_interface::{
prove_bandwidth_credential, Attribute, Parameters, PrivateAttribute, PublicAttribute,
Signature, VerificationKey,
prove_bandwidth_credential, Parameters, PrivateAttribute, PublicAttribute, Signature,
VerificationKey,
};
use serde::{Deserialize, Serialize};
use zeroize::{Zeroize, ZeroizeOnDrop};
+8 -2
View File
@@ -5,8 +5,7 @@ use crate::coconut::bandwidth::IssuanceBandwidthCredential;
use crate::error::Error;
use log::{debug, warn};
use nym_coconut_interface::{
aggregate_verification_keys, prove_bandwidth_credential, Attribute, Credential, Parameters,
Signature, SignatureShare, VerificationKey,
aggregate_verification_keys, Signature, SignatureShare, VerificationKey,
};
use nym_validator_client::client::CoconutApiClient;
@@ -96,3 +95,10 @@ pub(crate) mod scalar_serde_helper {
))
}
}
pub(crate) fn make_bincode_serializer() -> impl bincode::Options {
use bincode::Options;
bincode::DefaultOptions::new()
.with_big_endian()
.with_varint_encoding()
}
+3
View File
@@ -12,6 +12,9 @@ pub enum Error {
#[error("IO error")]
IOError(#[from] std::io::Error),
#[error("failed to (de)serialize credential structure: {0}")]
SerializationFailure(#[from] bincode::Error),
#[error("The detailed description is yet to be determined")]
BandwidthCredentialError,
+2 -2
View File
@@ -61,14 +61,14 @@ where
.await
.map_err(|reason| Error::UnconvertedDeposit {
reason,
voucher_blob: state.voucher.to_bytes(),
voucher_blob: state.voucher.to_recovery_bytes(),
})
}
/// In case of an error in the mid of the acquire process, this function should be used for
/// later retries to recover the bandwidth credential, either immediately or after some time.
pub async fn recover(&self, voucher_blob: &VoucherBlob) -> Result<()> {
let voucher = IssuanceBandwidthCredential::try_from_bytes(voucher_blob)
let voucher = IssuanceBandwidthCredential::try_from_recovered_bytes(voucher_blob)
.map_err(|_| Error::InvalidVoucherBlob)?;
let state = State::new(voucher);
nym_bandwidth_controller::acquire::get_credential(&state, &self.client, self.storage)