Skip to content
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

Add message compression and versioning #466

Merged
merged 40 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
a1ae816
compression bzip2 poc
Oct 18, 2021
5801821
add tests for snap, deflate, brotli
Oct 21, 2021
70000a3
small refactor
Oct 21, 2021
4482381
refactor
Oct 25, 2021
46eb086
implement versioning
Oct 30, 2021
fdc75d9
Merge branch 'dev' into feat/compression
Oct 30, 2021
26be6ef
remove compression benchmarks
Nov 1, 2021
7fe5e88
resolve clippy errors
Nov 1, 2021
c1098b3
Brotli C and Rust comparison
Nov 1, 2021
b3866af
remove brotli2 + fmt
Nov 1, 2021
6d24074
fix fmt and clippy issues
Nov 1, 2021
1aaf979
improve versioning
Nov 2, 2021
11df931
fix fmt and clippy
Nov 2, 2021
d087287
small improvements
Nov 2, 2021
0995859
add option to disable compression PoC
Nov 3, 2021
5f007e7
add WASM bindings for disabling compression
Nov 4, 2021
8c5c0c1
add disable option to WASM examples
Nov 4, 2021
b0c4bb4
disable compression in rust examples
Nov 4, 2021
bd49494
revert disabling compression by default
Nov 4, 2021
0bf0610
add example for disable_compression
Nov 4, 2021
3b25f9d
small improvements
Nov 4, 2021
328913e
Merge branch 'dev' into feat/compression
Nov 5, 2021
9af5507
fix brotli test
Nov 5, 2021
865c41a
fix clippy
Nov 5, 2021
65f65e7
rename variable
Nov 7, 2021
c0a7365
refactor disable compression
Nov 7, 2021
bcd39f2
fix IDE auto format
Nov 7, 2021
23e194c
reaname encoding.rs and message_version.rs
Nov 7, 2021
101f9ff
refactor message_version.rs and did_encoding.rs
Nov 8, 2021
de8d701
fix test in did_encoding.rs
Nov 8, 2021
7a67e6e
change compression state from bool to encoding
Nov 8, 2021
4cddeff
auto format
Nov 8, 2021
adc6771
fix resolver_history.rs
Nov 8, 2021
11cd748
refactor disable_compression() -> set_compression
Nov 8, 2021
8ac4d3e
refactor flag composition
Nov 8, 2021
48d9443
Refactor DID message encoding, version functions
cycraig Nov 9, 2021
2d05953
Add message sub-module, move around files
cycraig Nov 9, 2021
bb24b38
Change set_compression to set_encoding
cycraig Nov 9, 2021
dfc86f1
Add DIDMessageVersion::CURRENT
cycraig Nov 10, 2021
b6741d8
Add DID message magic bytes
cycraig Nov 10, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bindings/wasm/examples/src/private_tangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ async function createIdentityPrivateTangle(restURL, networkName) {
const config = new Config();
config.setNetwork(network);

// Uncomment the following line to disable compression.
// config.disableCompression();

// This URL points to the REST API of the locally running hornet node.
config.setNode(restURL || "http://127.0.0.1:14265/");

// Create a client instance from the configuration to publish messages to the Tangle.
const client = Client.fromConfig(config);

// For debugging, message compression can be disabled with the next line.
// client.disableCompression();

// Publish the Identity to the IOTA Network, this may take a few seconds to complete Proof-of-Work.
const receipt = await client.publishDocument(doc.toJSON());

Expand Down
5 changes: 5 additions & 0 deletions bindings/wasm/src/tangle/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ impl Config {
self.try_with_mut(|builder| builder.node(url).map_err(wasm_error))
}

#[wasm_bindgen(js_name = disableCompression)]
pub fn disable_compression(&mut self) -> Result<(), JsValue> {
self.with_mut(|builder| builder.disable_compression())
}

#[wasm_bindgen(js_name = setPrimaryNode)]
pub fn set_primary_node(
&mut self,
Expand Down
1 change: 1 addition & 0 deletions identity-iota/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ description = "An IOTA Tangle intergration for the identity-rs library."

[dependencies]
async-trait = { version = "0.1", default-features = false }
brotli = { version = "3.3", default-features = false, features = ["std"] }
dashmap = { version = "4.0" }
form_urlencoded = { version = "1.0" }
futures = { version = "0.3" }
Expand Down
4 changes: 4 additions & 0 deletions identity-iota/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ pub enum Error {
NoExplorerURLSet,
#[error("Invalid Explorer Url")]
InvalidExplorerURL,
#[error("compression error")]
CompressionError,
#[error("invalid message flags")]
InvalidMessageFlags,
cycraig marked this conversation as resolved.
Show resolved Hide resolved
}
21 changes: 19 additions & 2 deletions identity-iota/src/tangle/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use crate::did::IotaDID;
use crate::did::IotaDocument;
use crate::error::Error;
use crate::error::Result;
use crate::tangle::did_encoding;
use crate::tangle::did_encoding::DIDMessageEncoding;
use crate::tangle::did_message_versioning;
use crate::tangle::ClientBuilder;
use crate::tangle::Message;
use crate::tangle::MessageId;
Expand All @@ -30,6 +33,7 @@ use crate::tangle::TangleResolve;
pub struct Client {
pub(crate) client: IotaClient,
pub(crate) network: Network,
pub(crate) encoding: DIDMessageEncoding,
}

impl Client {
Expand Down Expand Up @@ -65,6 +69,7 @@ impl Client {
Ok(Self {
client: client.finish().await?,
network: builder.network,
encoding: builder.encoding,
})
}

Expand All @@ -84,13 +89,25 @@ impl Client {
self.publish_json(&IotaDocument::diff_index(message_id)?, diff).await
}

/// Publishes arbitrary JSON data to the specified index on the Tangle.
/// Compresses and Publishes arbitrary JSON data to the specified index on the Tangle.
pub async fn publish_json<T: ToJson>(&self, index: &str, data: &T) -> Result<Receipt> {
let encoded_message_data = match self.encoding {
DIDMessageEncoding::JsonBrotli => {
did_encoding::compress_message(&data.to_json()?, DIDMessageEncoding::JsonBrotli).map(|compressed_data| {
did_encoding::add_encoding_version_flag(compressed_data, DIDMessageEncoding::JsonBrotli)
})?
}
DIDMessageEncoding::Json => data.to_json_vec().map(|uncompressed_data| {
did_encoding::add_encoding_version_flag(uncompressed_data, DIDMessageEncoding::Json)
})?,
};
let message_data =
did_message_versioning::add_version_flag(encoded_message_data, did_message_versioning::CURRENT_MESSAGE_VERSION);
self
.client
.message()
.with_index(index)
.with_data(data.to_json_vec()?)
.with_data(message_data)
.finish()
.await
.map_err(Into::into)
Expand Down
9 changes: 9 additions & 0 deletions identity-iota/src/tangle/client_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use core::fmt::Result as FmtResult;
use std::time::Duration;

use crate::error::Result;
use crate::tangle::did_encoding::DIDMessageEncoding;
use crate::tangle::Client;
use crate::tangle::Network;

Expand All @@ -17,6 +18,7 @@ pub struct ClientBuilder {
pub(super) nodeset: bool,
pub(super) network: Network,
pub(super) builder: iota_client::ClientBuilder,
pub(super) encoding: DIDMessageEncoding,
}

impl ClientBuilder {
Expand All @@ -26,6 +28,7 @@ impl ClientBuilder {
nodeset: false,
network: Default::default(),
builder: iota_client::ClientBuilder::new().with_local_pow(DEFAULT_LOCAL_POW),
encoding: DIDMessageEncoding::JsonBrotli,
}
}

Expand All @@ -36,6 +39,12 @@ impl ClientBuilder {
self
}

/// Disables message compression.
pub fn disable_compression(mut self) -> Self {
self.encoding = DIDMessageEncoding::Json;
self
}
cycraig marked this conversation as resolved.
Show resolved Hide resolved

/// Adds an IOTA node by its URL.
pub fn node(mut self, url: &str) -> Result<Self> {
self.builder = self.builder.with_node(url)?;
Expand Down
48 changes: 48 additions & 0 deletions identity-iota/src/tangle/compression_brotli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use crate::error::Error;
use crate::error::Error::CompressionError;
use std::io::prelude::*;

const BUFFER_SIZE: usize = 4096;
const QUALITY: u32 = 5; // compression level
const WINDOWS_SIZE: u32 = 22;

pub(crate) fn compress_brotli<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, Error> {
let mut buf = Vec::new();
let mut compressor = brotli::CompressorReader::new(input.as_ref(), BUFFER_SIZE, QUALITY, WINDOWS_SIZE);
compressor.read_to_end(&mut buf).map_err(|_| Error::CompressionError)?;
Ok(buf)
}

pub(crate) fn decompress_brotli<T: AsRef<[u8]> + ?Sized>(input: &T) -> Result<Vec<u8>, Error> {
let mut decompressor = brotli::Decompressor::new(input.as_ref(), BUFFER_SIZE);
let mut buf = Vec::new();
decompressor.read_to_end(&mut buf).map_err(|_| CompressionError)?;
Ok(buf)
}

#[cfg(test)]
mod test {
use crate::did::IotaDocument;
use crate::tangle::compression_brotli::compress_brotli;
use crate::tangle::compression_brotli::decompress_brotli;
use identity_core::convert::ToJson;
use identity_core::crypto::KeyPair;

#[test]
fn test_brotli() {
let keypair: KeyPair = KeyPair::new_ed25519().unwrap();
let mut document: IotaDocument = IotaDocument::new(&keypair).unwrap();
document
.sign_self(keypair.private(), &document.default_signing_method().unwrap().id())
.unwrap();

let data = document.to_json().unwrap();
let compressed = compress_brotli(data.as_str()).unwrap();
let decompressed = decompress_brotli(&compressed).unwrap();

assert_eq!(decompressed, data.as_bytes());
}
}
53 changes: 53 additions & 0 deletions identity-iota/src/tangle/did_encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use crate::error::Error::CompressionError;
use crate::tangle::compression_brotli::compress_brotli;
use crate::tangle::compression_brotli::decompress_brotli;
use crate::Error;

#[derive(Debug, Copy, Clone)]
pub(crate) enum DIDMessageEncoding {
Json = 0,
JsonBrotli = 1,
}

/// Adds the current encoding flag at the beginning of arbitrary data.
pub(crate) fn add_encoding_version_flag(mut data: Vec<u8>, encoding: DIDMessageEncoding) -> Vec<u8> {
let encoding_version = encoding as u8;

data.splice(0..0, [encoding_version].iter().cloned());
data
}

/// Decompresses a message depending on the encoding flag.
pub(crate) fn decompress_message(encoding_flag: &u8, data: &[u8]) -> Result<Vec<u8>, Error> {
if *encoding_flag == DIDMessageEncoding::JsonBrotli as u8 {
decompress_brotli(data)
} else if *encoding_flag == DIDMessageEncoding::Json as u8 {
cycraig marked this conversation as resolved.
Show resolved Hide resolved
Ok(data.to_vec()) //todo prevent copying the slice.
} else {
Err(Error::InvalidMessageFlags)
}
}
cycraig marked this conversation as resolved.
Show resolved Hide resolved

/// Compresses a message depending on current encoding version.
pub(crate) fn compress_message<T: AsRef<[u8]>>(message: T, encoding: DIDMessageEncoding) -> Result<Vec<u8>, Error> {
match encoding {
DIDMessageEncoding::JsonBrotli => compress_brotli(message),
DIDMessageEncoding::Json => Err(CompressionError),
}
}

#[cfg(test)]
mod test {
use crate::tangle::did_encoding::add_encoding_version_flag;
use crate::tangle::did_encoding::DIDMessageEncoding;

#[test]
fn test_add_version_flag() {
let message: Vec<u8> = vec![10, 4, 5, 5];
let message_with_flag = add_encoding_version_flag(message, DIDMessageEncoding::JsonBrotli);
assert_eq!(message_with_flag, [DIDMessageEncoding::JsonBrotli as u8, 10, 4, 5, 5])
}
}
40 changes: 40 additions & 0 deletions identity-iota/src/tangle/did_message_versioning.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use crate::Error;

#[derive(Copy, Clone)]
pub(crate) enum DIDMessageVersion {
V1 = 1,
}

pub(crate) const CURRENT_MESSAGE_VERSION: DIDMessageVersion = DIDMessageVersion::V1;

/// Adds the current message version flag at the beginning of arbitrary data.
pub(crate) fn add_version_flag(mut data: Vec<u8>, message_version: DIDMessageVersion) -> Vec<u8> {
let version_flag = message_version as u8;
data.splice(0..0, [version_flag].iter().cloned());
data
}

/// Checks if flag matches message_version.
pub(crate) fn check_version_flag(flag: &u8, message_version: DIDMessageVersion) -> Result<(), Error> {
if message_version as u8 == *flag {
cycraig marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
} else {
Err(Error::InvalidMessageFlags)
}
}

#[cfg(test)]
mod test {
use crate::tangle::did_message_versioning::add_version_flag;
use crate::tangle::did_message_versioning::CURRENT_MESSAGE_VERSION;

#[test]
fn test_add_version_flag() {
let message: Vec<u8> = vec![10, 4, 5, 5];
let message_with_flag = add_version_flag(message, CURRENT_MESSAGE_VERSION);
assert_eq!(message_with_flag, [CURRENT_MESSAGE_VERSION as u8, 10, 4, 5, 5])
}
}
20 changes: 16 additions & 4 deletions identity-iota/src/tangle/message_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use crate::did::DocumentDiff;
use crate::did::IotaDID;
use crate::did::IotaDocument;
use crate::error::Result;
use crate::tangle::did_encoding;
use crate::tangle::did_message_versioning;
use crate::tangle::did_message_versioning::DIDMessageVersion;
use crate::tangle::TangleRef;

// TODO: Use MessageId when it has a const ctor
Expand Down Expand Up @@ -45,11 +48,20 @@ fn parse_payload<T: FromJson + TangleRef>(message_id: MessageId, payload: Option
}

fn parse_data<T: FromJson + TangleRef>(message_id: MessageId, data: &[u8]) -> Option<T> {
let mut resource: T = T::from_json_slice(data).ok()?;

resource.set_message_id(message_id);
let version_check = did_message_versioning::check_version_flag(&data[0], DIDMessageVersion::V1);
if version_check.is_err() {
return None;
}

Some(resource)
let compression_result = did_encoding::decompress_message(&data[1], &data[2..]);
match compression_result {
Ok(decompressed_message) => {
let mut resource: T = T::from_json_slice(decompressed_message.as_slice()).ok()?;
resource.set_message_id(message_id);
Some(resource)
}
_ => None,
}
}

pub trait MessageIdExt: Sized {
Expand Down
3 changes: 3 additions & 0 deletions identity-iota/src/tangle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub use self::traits::TangleResolve;
mod client;
mod client_builder;
mod client_map;
mod compression_brotli;
mod did_encoding;
mod did_message_versioning;
mod message_ext;
mod message_index;
mod network;
Expand Down