Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Commit

Permalink
- channels: Display instance for PublicKeyMap
Browse files Browse the repository at this point in the history
- core: reexport print macro

- channels: gen_next_msg_id
  • Loading branch information
semenov-vladyslav committed Sep 17, 2020
1 parent 0544af0 commit 9a7cb80
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 26 deletions.
4 changes: 3 additions & 1 deletion iota-streams-app-channels/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description = "A rust implementation of the IOTA Streams Channels Application"
[features]
default = ["std", "async", "tangle", "client"]
# Enable `std` feature in dependencies
std = ["iota-streams-core/std", "iota-streams-core-edsig/std", "iota-streams-ddml/std", "iota-streams-app/std", "anyhow/std"]
std = ["iota-streams-core/std", "iota-streams-core-edsig/std", "iota-streams-ddml/std", "iota-streams-app/std", "anyhow/std", "hex/std"]
async = ["iota-streams-app/async"]
tangle = ["iota-streams-app/tangle"]
client = ["iota-streams-app/client"]
Expand All @@ -28,5 +28,7 @@ iota-streams-ddml = { version = "0.2.0", path = "../iota-streams-ddml", default-
iota-streams-app = { version = "0.2.0", path = "../iota-streams-app", default-features = false }
anyhow = { version = "1.0.26", default-features = false }

hex = { version = "0.4.2", default-features = false, optional = true }

[dev-dependencies]
iota-core = { git = "https://github.com/iotaledger/iota.rs", rev = "03cf531" }
13 changes: 12 additions & 1 deletion iota-streams-app-channels/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::fmt;

use iota_streams_app::message::{
HasLink,
LinkGenerator,
Expand Down Expand Up @@ -32,7 +34,7 @@ where
{
}

pub type SequencingState<Link> = (Link, usize);
pub struct SequencingState<Link>(pub Link, pub usize);

pub trait PublicKeyStore<Info>: Default {
fn filter<'a>(&'a self, pks: &'a Vec<ed25519::PublicKey>) -> Vec<(&'a ed25519::PublicKey, &'a x25519::PublicKey)>;
Expand Down Expand Up @@ -96,6 +98,15 @@ impl<Info> PublicKeyStore<Info> for PublicKeyMap<Info> {
}
}

impl<Info: fmt::Display> fmt::Display for PublicKeyMap<Info> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (k, (_x, i)) in self.pks.iter() {
writeln!(f, " <{}> => {}", hex::encode(k.0.as_bytes()), i)?;
}
Ok(())
}
}

pub trait PresharedKeyStore: Default {
fn filter<'a>(&'a self, psk_ids: &'_ psk::PskIds) -> Vec<psk::IPsk<'a>>;
fn get<'a>(&'a self, pskid: &'_ psk::PskId) -> Option<&'a psk::Psk>;
Expand Down
9 changes: 8 additions & 1 deletion iota-streams-app-channels/src/api/tangle/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::{
anyhow,
Result,
};
use core::fmt;

use super::*;
use crate::api::user::User;
Expand Down Expand Up @@ -195,7 +196,7 @@ impl Author {
// }
// ids
// }
pub fn gen_next_msg_ids(&mut self, branching: bool) -> Vec<(ed25519::PublicKey, (Address, usize))> {
pub fn gen_next_msg_ids(&mut self, branching: bool) -> Vec<(ed25519::PublicKey, SeqState)> {
self.imp.gen_next_msg_ids(branching)
}
pub fn store_state(&mut self, pk: ed25519::PublicKey, link: Address) {
Expand All @@ -205,3 +206,9 @@ impl Author {
self.imp.store_state_for_all(link, seq_num)
}
}

impl fmt::Display for Author {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<{}>\n{}", hex::encode(self.imp.sig_kp.public.as_bytes()), self.imp.pk_store)
}
}
10 changes: 9 additions & 1 deletion iota-streams-app-channels/src/api/tangle/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Default parameters for Author and Subscriber types.
use core::fmt;
use super::{
PresharedKeyMap,
PublicKeyMap,
SequencingState,
};
use iota_streams_app::{
message,
Expand Down Expand Up @@ -35,10 +37,16 @@ pub type Message = message::BinaryMessage<DefaultF, Address>;
/// Message type with parsed header.
pub type Preparsed<'a> = message::PreparsedMessage<'a, DefaultF, Address>;

pub type SeqState = (TangleAddress, usize);
pub type SeqState = SequencingState<TangleAddress>;
pub type PkStore = PublicKeyMap<SeqState>;
pub type PskStore = PresharedKeyMap;

impl fmt::Display for SeqState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<{},{}>", self.0.msgid, self.1)
}
}

/// Link Generator specifies algorithm for generating new message addressed.
pub type LinkGen = DefaultTangleLinkGenerator<DefaultF>;

Expand Down
9 changes: 8 additions & 1 deletion iota-streams-app-channels/src/api/tangle/subscriber.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Customize Subscriber with default parameters for use over the Tangle.
use anyhow::Result;
use core::fmt;

use super::*;
use crate::api::user::User;
Expand Down Expand Up @@ -201,7 +202,7 @@ impl Subscriber {
// }
// ids
// }
pub fn gen_next_msg_ids(&mut self, branching: bool) -> Vec<(ed25519::PublicKey, (Address, usize))> {
pub fn gen_next_msg_ids(&mut self, branching: bool) -> Vec<(ed25519::PublicKey, SeqState)> {
self.imp.gen_next_msg_ids(branching)
}
pub fn store_state(&mut self, pk: ed25519::PublicKey, link: Address) {
Expand All @@ -211,3 +212,9 @@ impl Subscriber {
self.imp.store_state_for_all(link, seq_num)
}
}

impl fmt::Display for Subscriber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<{}>\n{}", hex::encode(self.imp.sig_kp.public.as_bytes()), self.imp.pk_store)
}
}
44 changes: 24 additions & 20 deletions iota-streams-app-channels/src/api/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ where
"Can't create channel: a channel already created/registered."
);
let appinst = self.link_gen.link_from((&self.sig_kp.public, channel_idx));
self.pk_store.insert(self.sig_kp.public.clone(), SequencingState(appinst.clone(), 2_usize));
self.appinst = Some(appinst);
Ok(())
}
Expand Down Expand Up @@ -201,7 +202,7 @@ where
// TODO: Verify appinst (address) == public key.
// At the moment the Author is free to choose any address, not tied to PK.

self.pk_store.insert(content.sig_pk.clone(), (link.clone(), 0));
self.pk_store.insert(content.sig_pk.clone(), SequencingState(link.clone(), 0));
// Reset link_gen
let _appinst = self.link_gen.link_from(link.base().clone());
self.appinst = Some(link);
Expand Down Expand Up @@ -267,12 +268,10 @@ where
let content = self
.unwrap_subscribe(preparsed)?
.commit(self.link_store.borrow_mut(), info)?;
// TODO: trust content.subscriber_ntru_pk and add to the list of subscribers only if trusted.
// TODO: trust content.subscriber_sig_pk
let subscriber_sig_pk = content.subscriber_sig_pk;
// let subscriber_ke_pk = x25519::public_from_ed25519(&subscriber_sig_pk);
// self.ke_pks.insert(x25519::PublicKeyWrap(subscriber_ke_pk));
let ref_link = self.appinst.as_ref().unwrap().clone();
self.pk_store.insert(subscriber_sig_pk, (ref_link, SEQ_MESSAGE_NUM));
self.pk_store.insert(subscriber_sig_pk, SequencingState(ref_link, SEQ_MESSAGE_NUM));
// Unwrapped unsubscribe_key is not used explicitly.
Ok(())
}
Expand Down Expand Up @@ -442,7 +441,7 @@ where
for ke_pk in content.ke_pks {
if self.pk_store.get(&ke_pk).is_none() {
// Store at state 2 since 0 and 1 are reserved states
self.pk_store.insert(ke_pk, (appinst.clone(), 2));
self.pk_store.insert(ke_pk, SequencingState(appinst.clone(), 2));
}
}
}
Expand Down Expand Up @@ -608,7 +607,7 @@ where
info: <LS as LinkStore<F, <Link as HasLink>::Rel>>::Info,
) -> Result<Option<BinaryMessage<F, Link>>> {
match self.pk_store.get_mut(&self.sig_kp.public) {
Some((link_to, seq_num)) => {
Some(SequencingState(link_to, seq_num)) => {
if (self.flags & FLAG_BRANCHING_MASK) != 0 {
let msg_link = self
.link_gen
Expand Down Expand Up @@ -680,20 +679,25 @@ where
Ok(())
}

fn gen_next_msg_id(ids: &mut Vec<(ed25519::PublicKey, SequencingState<Link>)>, link_gen: &mut LG, pk_info: (&ed25519::PublicKey, &mut SequencingState<Link>), branching: bool) {
let (pk, SequencingState(seq_link, seq_num)) = pk_info;
if branching {
let msg_id = link_gen.link_from((seq_link.rel(), pk, 1_usize));
ids.push((pk.clone(), SequencingState(msg_id, 1)));
} else {
let msg_id = link_gen.link_from((seq_link.rel(), pk, *seq_num));
let msg_id1 = link_gen.link_from((seq_link.rel(), pk, *seq_num - 1));
ids.push((pk.clone(), SequencingState(msg_id, *seq_num)));
ids.push((pk.clone(), SequencingState(msg_id1, *seq_num - 1)));
}
}

pub fn gen_next_msg_ids(&mut self, branching: bool) -> Vec<(ed25519::PublicKey, SequencingState<Link>)> {
let mut ids = Vec::new();

// TODO: Do the same for self.sig_kp.public
for (pk, (seq_link, seq_num)) in self.pk_store.iter_mut() {
if branching {
let msg_id = self.link_gen.link_from((seq_link.rel(), pk, 1_usize));
ids.push((pk.clone(), (msg_id, 1)));
} else {
let msg_id = self.link_gen.link_from((seq_link.rel(), pk, *seq_num));
let msg_id1 = self.link_gen.link_from((seq_link.rel(), pk, *seq_num - 1));
ids.push((pk.clone(), (msg_id, *seq_num)));
ids.push((pk.clone(), (msg_id1, *seq_num - 1)));
}
for pk_info in self.pk_store.iter_mut() {
Self::gen_next_msg_id(&mut ids, &mut self.link_gen, pk_info, branching);
/*
let (seq_link, seq_num) = self.imp.get_seq_state(pk.0).unwrap();
if branching {
Expand All @@ -714,13 +718,13 @@ where

pub fn store_state(&mut self, pk: ed25519::PublicKey, link: Link) {
let seq_num = self.pk_store.get(&pk).unwrap().1;
self.pk_store.insert(pk, (link, seq_num + 1));
self.pk_store.insert(pk, SequencingState(link, seq_num + 1));
}

pub fn store_state_for_all(&mut self, link: Link, seq_num: usize) {
self.pk_store
.insert(self.sig_kp.public.clone(), (link.clone(), seq_num + 1));
for (_pk, (l, s)) in self.pk_store.iter_mut() {
.insert(self.sig_kp.public.clone(), SequencingState(link.clone(), seq_num + 1));
for (_pk, SequencingState(l, s)) in self.pk_store.iter_mut() {
*l = link.clone();
*s = seq_num + 1;
}
Expand Down
1 change: 0 additions & 1 deletion iota-streams-app/src/transport/tangle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use crate::message::{
BinaryMessage,
HasLink,
LinkGenerator,
HDF,
};

/// Number of bytes to be placed in each transaction (Maximum HDF Payload Count)
Expand Down
10 changes: 10 additions & 0 deletions iota-streams-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@ macro_rules! println {
($($arg:tt)*) => {};
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! print {
() => {};
($($arg:tt)*) => {};
}

// Reexport macro at the same level as `no_std`.
#[cfg(feature = "std")]
pub use std::println;

#[cfg(feature = "std")]
pub use std::print;

pub mod hash;
pub mod prelude;
pub mod prng;
Expand Down

0 comments on commit 9a7cb80

Please sign in to comment.