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 38 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
16 changes: 16 additions & 0 deletions bindings/wasm/docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
## Members

<dl>
<dt><a href="#DIDMessageEncoding">DIDMessageEncoding</a></dt>
<dd></dd>
<dt><a href="#Digest">Digest</a></dt>
<dd></dd>
<dt><a href="#KeyType">KeyType</a></dt>
Expand Down Expand Up @@ -211,6 +213,7 @@ Creates a new `Client` with default settings for the given `Network`.
* _instance_
* [.setNetwork(network)](#Config+setNetwork)
* [.setNode(url)](#Config+setNode)
* [.setEncoding(encoding)](#Config+setEncoding)
* [.setPrimaryNode(url, jwt, username, password)](#Config+setPrimaryNode)
* [.setPrimaryPoWNode(url, jwt, username, password)](#Config+setPrimaryPoWNode)
* [.setPermanode(url, jwt, username, password)](#Config+setPermanode)
Expand Down Expand Up @@ -244,6 +247,15 @@ Creates a new `Client` with default settings for the given `Network`.
| --- | --- |
| url | <code>string</code> |

<a name="Config+setEncoding"></a>

### config.setEncoding(encoding)
**Kind**: instance method of [<code>Config</code>](#Config)

| Param | Type |
| --- | --- |
| encoding | <code>number</code> |

<a name="Config+setPrimaryNode"></a>

### config.setPrimaryNode(url, jwt, username, password)
Expand Down Expand Up @@ -1757,6 +1769,10 @@ Deserializes a `VerificationMethod` object from a JSON object.
| --- | --- |
| value | <code>any</code> |

<a name="DIDMessageEncoding"></a>

## DIDMessageEncoding
**Kind**: global variable
<a name="Digest"></a>

## Digest
Expand Down
5 changes: 4 additions & 1 deletion bindings/wasm/examples/src/private_tangle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import {Client, Config, Document, KeyPair, KeyType, Network} from '@iota/identity-wasm';
import {Client, Config, Document, KeyPair, KeyType, Network, DIDMessageEncoding} from '@iota/identity-wasm';

/**
This example shows how a DID document can be created on a private tangle.
Expand Down Expand Up @@ -32,6 +32,9 @@ async function createIdentityPrivateTangle(restURL, networkName) {
// This URL points to the REST API of the locally running hornet node.
config.setNode(restURL || "http://127.0.0.1:14265/");

// Use DIDMessageEncoding.Json instead to publish plaintext messages to the Tangle for debugging.
config.setEncoding(DIDMessageEncoding.JsonBrotli);

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

Expand Down
6 changes: 6 additions & 0 deletions bindings/wasm/src/tangle/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::time::Duration;
use wasm_bindgen::prelude::*;

use crate::error::wasm_error;
use crate::tangle::WasmDIDMessageEncoding;
use crate::tangle::WasmNetwork;

fn to_duration(seconds: u32) -> Duration {
Expand Down Expand Up @@ -48,6 +49,11 @@ impl Config {
self.try_with_mut(|builder| builder.node(url).map_err(wasm_error))
}

#[wasm_bindgen(js_name = setEncoding)]
pub fn set_encoding(&mut self, encoding: WasmDIDMessageEncoding) -> Result<(), JsValue> {
self.with_mut(|builder| builder.encoding(encoding.into()))
}

#[wasm_bindgen(js_name = setPrimaryNode)]
pub fn set_primary_node(
&mut self,
Expand Down
30 changes: 30 additions & 0 deletions bindings/wasm/src/tangle/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity::iota::DIDMessageEncoding;
use wasm_bindgen::prelude::*;

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

impl From<DIDMessageEncoding> for WasmDIDMessageEncoding {
fn from(encoding: DIDMessageEncoding) -> Self {
match encoding {
DIDMessageEncoding::Json => Self::Json,
DIDMessageEncoding::JsonBrotli => Self::JsonBrotli,
}
}
}

impl From<WasmDIDMessageEncoding> for DIDMessageEncoding {
fn from(encoding: WasmDIDMessageEncoding) -> Self {
match encoding {
WasmDIDMessageEncoding::Json => Self::Json,
WasmDIDMessageEncoding::JsonBrotli => Self::JsonBrotli,
}
}
}
2 changes: 2 additions & 0 deletions bindings/wasm/src/tangle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

pub use self::client::*;
pub use self::config::*;
pub use self::message::*;
pub use self::network::*;

mod client;
mod config;
mod message;
mod network;
5 changes: 5 additions & 0 deletions examples/low-level-api/private_tangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//! cargo run --example private_tangle

use identity::iota::ClientBuilder;
use identity::iota::DIDMessageEncoding;
use identity::iota::IotaDID;
use identity::iota::Network;
use identity::iota::Receipt;
Expand All @@ -34,8 +35,12 @@ pub async fn main() -> Result<()> {
// In a locally running one-click tangle, this would often be `http://127.0.0.1:14265/`
let private_node_url = "https://api.lb-0.h.chrysalis-devnet.iota.cafe";

// Use DIDMessageEncoding::Json instead to publish plaintext messages to the Tangle for debugging.
let encoding = DIDMessageEncoding::JsonBrotli;

let client = ClientBuilder::new()
.network(network.clone())
.encoding(encoding)
.node(private_node_url)?
.build()
.await?;
Expand Down
3 changes: 3 additions & 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 All @@ -20,6 +21,8 @@ identity-credential = { version = "=0.4.0", path = "../identity-credential" }
identity-did = { version = "=0.4.0", path = "../identity-did" }
lazy_static = { version = "1.4", default-features = false }
log = { version = "0.4", default-features = false }
num-derive = { version = "0.3", default-features = false }
num-traits = { version = "0.2", default-features = false, features = ["std"] }
serde = { version = "1.0", default-features = false, features = ["std", "derive"] }
strum = { version = "0.21", features = ["derive"] }
thiserror = { version = "1.0", default-features = false }
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
}
8 changes: 6 additions & 2 deletions identity-iota/src/tangle/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::did::IotaDocument;
use crate::error::Error;
use crate::error::Result;
use crate::tangle::ClientBuilder;
use crate::tangle::DIDMessageEncoding;
use crate::tangle::Message;
use crate::tangle::MessageId;
use crate::tangle::Network;
Expand All @@ -30,6 +31,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 +67,7 @@ impl Client {
Ok(Self {
client: client.finish().await?,
network: builder.network,
encoding: builder.encoding,
})
}

Expand All @@ -84,13 +87,14 @@ 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 message_data: Vec<u8> = crate::tangle::pack_did_message(data, self.encoding)?;
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 @@ -8,6 +8,7 @@ use std::time::Duration;

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

const DEFAULT_LOCAL_POW: bool = false;
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
}

/// Sets the DID message encoding used when publishing to the Tangle.
pub fn encoding(mut self, encoding: DIDMessageEncoding) -> Self {
self.encoding = encoding;
self
}

/// 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
52 changes: 52 additions & 0 deletions identity-iota/src/tangle/message/compression_brotli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::io::Read;

use crate::error::Error;
use crate::error::Result;

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>> {
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>> {
let mut decompressor = brotli::Decompressor::new(input.as_ref(), BUFFER_SIZE);
let mut buf = Vec::new();
decompressor
.read_to_end(&mut buf)
.map_err(|_| Error::CompressionError)?;
Ok(buf)
}

#[cfg(test)]
mod test {
use identity_core::convert::ToJson;
use identity_core::crypto::KeyPair;

use crate::did::IotaDocument;

use super::*;

#[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());
}
}
21 changes: 21 additions & 0 deletions identity-iota/src/tangle/message/message_encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use num_traits::FromPrimitive;

use crate::Error;

/// Indicates the encoding and compression of a DID Message.
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, num_derive::FromPrimitive)]
pub enum DIDMessageEncoding {
Json = 0,
JsonBrotli = 1,
}

impl TryFrom<u8> for DIDMessageEncoding {
type Error = Error;

fn try_from(value: u8) -> Result<Self, Self::Error> {
FromPrimitive::from_u8(value).ok_or(Error::InvalidMessageFlags)
}
}
Loading