Skip to content

Commit

Permalink
gdk_rust: switch back to serde_cbor
Browse files Browse the repository at this point in the history
we switched to ciborium because serde_cbor it's unmantained.
However, serde_cbor never gave us trouble, while ciborium had the
"invalid type: bytes, expected bytes" bug, which has been attempted to be solved
upstream in enarx/ciborium#97
Apart that we have no input on the MR yet, in trying to use the patched version
we encountered the error:
"invalid type: string, expected map"
which is probably due to the fact the patched version is on master and not just
the bug fix from the tagged version we use.
While this later approach could work, ciborium doesn't look that reliable so we
decided to switch back to serde_cbor for the time being.
  • Loading branch information
RCasatta committed Oct 26, 2023
1 parent 5021276 commit 3d3aa33
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 78 deletions.
39 changes: 11 additions & 28 deletions subprojects/gdk_rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions subprojects/gdk_rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
]

exclude = [ "gdk_test" ]
resolver = "2"

[profile.release]
lto = true
Expand Down
2 changes: 1 addition & 1 deletion subprojects/gdk_rust/gdk_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ testing = []
aes = "0.7.0"
aes-gcm-siv = "0.10"
bitcoin = { version = "0.30", features = ["serde"] }
ciborium = "0.2.1"
serde_cbor = "0.11.1"
elements = { version = "0.22", features = ["serde"] }
libc = "0.2"
log = "0.4.8"
Expand Down
4 changes: 2 additions & 2 deletions subprojects/gdk_rust/gdk_common/src/be/blockheader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ mod test {
#[test]
fn test_cbor_header() {
let header = block_header_dynafed();
let vec = crate::util::ciborium_to_vec(&header).unwrap();
let back: BlockHeader = ciborium::from_reader(&vec[..]).unwrap();
let vec = serde_cbor::to_vec(&header).unwrap();
let back: BlockHeader = serde_cbor::from_slice(&vec).unwrap();
assert_eq!(header, back);
}

Expand Down
2 changes: 1 addition & 1 deletion subprojects/gdk_rust/gdk_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub mod aes {
}

pub use bitcoin;
pub use ciborium;
pub use electrum_client;
pub use elements;
pub use error::*;
Expand All @@ -29,6 +28,7 @@ pub use miniscript;
pub use network::*;
pub use once_cell;
pub use rand;
pub use serde_cbor;
pub use state::State;
pub use ureq;

Expand Down
9 changes: 0 additions & 9 deletions subprojects/gdk_rust/gdk_common/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,3 @@ pub fn now() -> u64 {
// Realistic timestamps can be converted to u64
u64::try_from(since_the_epoch.as_micros()).unwrap_or(u64::MAX)
}

pub fn ciborium_to_vec<T>(value: &T) -> Result<Vec<u8>, ciborium::ser::Error<std::io::Error>>
where
T: serde::ser::Serialize,
{
let mut v = Vec::new();
ciborium::ser::into_writer(value, &mut v)?;
Ok(v)
}
11 changes: 4 additions & 7 deletions subprojects/gdk_rust/gdk_electrum/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::BETxid;
use gdk_common::bitcoin::bip32::ExtendedPubKey;
use gdk_common::bitcoin::sighash;
use gdk_common::error::Error as CommonError;
use gdk_common::{bitcoin, ciborium, electrum_client, elements, ureq};
use gdk_common::{bitcoin, electrum_client, elements, serde_cbor, ureq};
use serde::ser::Serialize;
use std::convert::From;
use std::path::PathBuf;
Expand Down Expand Up @@ -75,6 +75,9 @@ pub enum Error {
#[error(transparent)]
JSON(#[from] serde_json::error::Error),

#[error(transparent)]
SerdeCbor(#[from] serde_cbor::Error),

#[error("insufficient funds")]
InsufficientFunds,

Expand Down Expand Up @@ -163,12 +166,6 @@ pub enum Error {
#[error("sendall error")]
SendAll,

#[error(transparent)]
DeserializeCBORError(#[from] ciborium::de::Error<std::io::Error>),

#[error(transparent)]
SerializeCBORError(#[from] ciborium::ser::Error<std::io::Error>),

#[error(transparent)]
SliceConversionError(#[from] std::array::TryFromSliceError),

Expand Down
7 changes: 3 additions & 4 deletions subprojects/gdk_rust/gdk_electrum/src/headers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ use gdk_common::aes::aead::NewAead;
use gdk_common::aes::{Aes256GcmSiv, Key};
use gdk_common::be::{BETxid, BETxidConvert};
use gdk_common::bitcoin::hashes::{sha256, sha256d, Hash};
use gdk_common::electrum_client;
use gdk_common::elements;
use gdk_common::log::{debug, info, warn};
use gdk_common::model::{
SPVCommonParams, SPVDownloadHeadersParams, SPVDownloadHeadersResult, SPVVerifyTxParams,
SPVVerifyTxResult,
};
use gdk_common::store::{Decryptable, Encryptable};
use gdk_common::util::ciborium_to_vec;
use gdk_common::NetworkId;
use gdk_common::{electrum_client, serde_cbor};
use std::collections::HashSet;
use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -243,7 +242,7 @@ impl VerifiedCache {
) -> Result<HashSet<(BETxid, u32)>, Error> {
let mut file = File::open(&filepath)?;
let plaintext = file.decrypt(cipher)?;
Ok(gdk_common::ciborium::from_reader(&plaintext[..])?)
Ok(gdk_common::serde_cbor::from_reader(&plaintext[..])?)
}

fn contains(&self, txid: &BETxid, height: u32) -> Result<bool, Error> {
Expand All @@ -263,7 +262,7 @@ impl VerifiedCache {

fn flush(&mut self) -> Result<(), Error> {
if let Some(store) = &self.store {
let plaintext = ciborium_to_vec(&self.set)?;
let plaintext = serde_cbor::to_vec(&self.set)?;
let (nonce_bytes, ciphertext) = plaintext.encrypt(&store.cipher)?;
let mut file = File::create(&store.filepath)?;
file.write_all(&nonce_bytes)?;
Expand Down
23 changes: 11 additions & 12 deletions subprojects/gdk_rust/gdk_electrum/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ use gdk_common::be::{
use gdk_common::bitcoin::bip32::{DerivationPath, ExtendedPubKey};
use gdk_common::bitcoin::hashes::{sha256, Hash};
use gdk_common::bitcoin::Txid;
use gdk_common::ciborium;
use gdk_common::elements;
use gdk_common::elements::TxOutSecrets;
use gdk_common::log::{info, log, Level};
use gdk_common::model::{AccountSettings, FeeEstimate, SPVVerifyTxResult, Settings};
use gdk_common::serde_cbor;
use gdk_common::store::{Decryptable, Encryptable, ToCipher};
use gdk_common::util::ciborium_to_vec;
use gdk_common::wally::MasterBlindingKey;
use gdk_common::NetworkId;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -211,7 +210,7 @@ impl RawCache {

fn try_new<P: AsRef<Path>>(path: P, cipher: &Aes256GcmSiv) -> Result<Self, Error> {
let decrypted = load_decrypt(Kind::Cache, path, cipher)?;
let store = ciborium::from_reader(&decrypted[..])?;
let store = serde_cbor::from_reader(&decrypted[..])?;
Ok(store)
}

Expand Down Expand Up @@ -253,7 +252,7 @@ impl RawStore {

fn try_new<P: AsRef<Path>>(path: P, cipher: &Aes256GcmSiv) -> Result<Self, Error> {
let decrypted = load_decrypt(Kind::Store, path, cipher)?;
let store = ciborium::from_reader(&decrypted[..])?;
let store = serde_cbor::from_reader(&decrypted[..])?;
Ok(store)
}
}
Expand Down Expand Up @@ -334,8 +333,8 @@ impl StoreMeta {
let now = Instant::now();

let plaintext = match kind {
Kind::Store => ciborium_to_vec(&self.store),
Kind::Cache => ciborium_to_vec(&self.cache),
Kind::Store => serde_cbor::to_vec(&self.store),
Kind::Cache => serde_cbor::to_vec(&self.cache),
}?;

let hash = sha256::Hash::hash(&plaintext);
Expand Down Expand Up @@ -698,14 +697,14 @@ mod tests {
},
};

let blob = ciborium_to_vec(&store_v0).unwrap();
let store_v1: RawStoreV1 = ciborium::from_reader(&blob[..]).unwrap();
let blob = serde_cbor::to_vec(&store_v0).unwrap();
let store_v1: RawStoreV1 = serde_cbor::from_reader(&blob[..]).unwrap();

assert_eq!(store_v0.settings, store_v1.settings);
assert_eq!(store_v0.memos, store_v1.memos);

let blob = ciborium_to_vec(&store_v1).unwrap();
let store_v0: RawStoreV0 = ciborium::from_reader(&blob[..]).unwrap();
let blob = serde_cbor::to_vec(&store_v1).unwrap();
let store_v0: RawStoreV0 = serde_cbor::from_reader(&blob[..]).unwrap();
assert_eq!(store_v0.settings, store_v1.settings);
assert_eq!(store_v0.memos, store_v1.memos);
}
Expand Down Expand Up @@ -736,8 +735,8 @@ mod tests {
bip44_discovered: Default::default(),
};

let blob = ciborium_to_vec(&cache_v0).unwrap();
let cache_v1: RawAccountCacheV1 = ciborium::from_reader(&blob[..])
let blob = serde_cbor::to_vec(&cache_v0).unwrap();
let cache_v1: RawAccountCacheV1 = serde_cbor::from_reader(&blob[..])
.expect("cache compatibility broke, not critical but think twice");

assert_eq!(cache_v0.xpub, cache_v1.xpub);
Expand Down
6 changes: 3 additions & 3 deletions subprojects/gdk_rust/gdk_registry/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use gdk_common::bitcoin::hashes::{sha256, Hash};
use gdk_common::elements::AssetId;
use gdk_common::log::{debug, warn};
use gdk_common::once_cell::sync::{Lazy, OnceCell};
use gdk_common::serde_cbor;
use gdk_common::store::{Decryptable, Encryptable, ToCipher};
use gdk_common::util::ciborium_to_vec;
use serde::{Deserialize, Serialize};

use crate::registry_infos::{RegistryAssets, RegistryIcons};
Expand Down Expand Up @@ -128,7 +128,7 @@ impl Cache {
let get_cache = |file: &mut File| -> Result<Self> {
let cipher = xpub.to_cipher()?;
let decrypted = file.decrypt(&cipher)?;
gdk_common::ciborium::from_reader(&decrypted[..]).map_err(Into::into)
gdk_common::serde_cbor::from_reader(&decrypted[..]).map_err(Into::into)
};

let mut cache = match cache_files.get_mut(&hash_xpub(xpub)) {
Expand Down Expand Up @@ -178,7 +178,7 @@ impl Cache {
pub(crate) fn update(&self, cache_files: &mut CacheFiles) -> Result<()> {
let xpub = self.xpub.unwrap();

let plain_text = ciborium_to_vec(self)?;
let plain_text = serde_cbor::to_vec(self)?;
let cipher = xpub.to_cipher()?;
let (nonce, rest) = plain_text.encrypt(&cipher)?;

Expand Down
14 changes: 5 additions & 9 deletions subprojects/gdk_rust/gdk_registry/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use gdk_common::{bitcoin, ciborium, elements, ureq};
use gdk_common::{bitcoin, elements, serde_cbor, ureq};
use std::sync::{MutexGuard, PoisonError, TryLockError};

/// Result type alias of the `gdk_registry` crate.
Expand Down Expand Up @@ -69,18 +69,14 @@ pub enum Error {
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),

/// Wraps errors happened when serializing CBORs.
#[error(transparent)]
SerializeCbor(#[from] ciborium::ser::Error<std::io::Error>),

/// Wraps errors happened when deserializing CBORs.
#[error(transparent)]
DeserializeCbor(#[from] ciborium::de::Error<std::io::Error>),

/// Wraps http errors.
#[error(transparent)]
Ureq(#[from] ureq::Error),

/// Wraps serde error.
#[error(transparent)]
SerdeCbor(#[from] serde_cbor::Error),

/// A generic error.
#[error("{0}")]
Generic(String),
Expand Down
4 changes: 2 additions & 2 deletions subprojects/gdk_rust/gdk_registry/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) fn read<V: DeserializeOwned>(file: &mut File) -> Result<V> {
info!("file {:?} size {}", &file, file.metadata()?.len());
}
let buffered = BufReader::new(file);
Ok(gdk_common::ciborium::from_reader(buffered)?)
Ok(gdk_common::serde_cbor::from_reader(buffered)?)
}

pub(crate) fn write<V: Serialize>(value: &V, file: &mut File) -> Result<()> {
Expand All @@ -24,7 +24,7 @@ pub(crate) fn write<V: Serialize>(value: &V, file: &mut File) -> Result<()> {

file.seek(std::io::SeekFrom::Start(0))?;
let buffered = BufWriter::new(file);
Ok(gdk_common::ciborium::ser::into_writer(value, buffered)?)
Ok(gdk_common::serde_cbor::to_writer(buffered, value)?)
}

#[cfg(test)]
Expand Down

0 comments on commit 3d3aa33

Please sign in to comment.