Persistent tracking of dapps (#4302)

* Tests for RPC

* Extracting dapp_id from Origin and x-parity-origin

* Separate type for DappId

* Persistent tracking of recent dapps

* Fixing tests

* Exposing dapp timestamps

* Fixing import to work on stable

* Fixing test again
This commit is contained in:
Tomasz Drwięga
2017-01-30 10:59:46 +01:00
committed by Gav Wood
parent 47e1c5e2f1
commit cf348dae60
15 changed files with 336 additions and 127 deletions

View File

@@ -29,9 +29,9 @@ macro_rules! impl_hash {
#[derive(Default, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Clone)]
pub struct $name(pub $inner);
impl Into<$inner> for $name {
fn into(self) -> $inner {
self.0
impl From<$name> for $inner {
fn from(other: $name) -> $inner {
other.0
}
}

View File

@@ -16,14 +16,10 @@
//! Misc deserialization.
use std::io::{Read, Write};
use std::collections::HashMap;
use serde_json;
use util;
use hash;
/// Collected account metadata
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct AccountMeta {
/// The name of the account.
pub name: String,
@@ -33,26 +29,4 @@ pub struct AccountMeta {
pub uuid: Option<String>,
}
impl Default for AccountMeta {
fn default() -> Self {
AccountMeta {
name: String::new(),
meta: "{}".to_owned(),
uuid: None,
}
}
}
impl AccountMeta {
/// Read a hash map of Address -> AccountMeta.
pub fn read_address_map<R>(reader: R) -> Result<HashMap<util::Address, AccountMeta>, serde_json::Error> where R: Read {
serde_json::from_reader(reader).map(|ok: HashMap<hash::Address, AccountMeta>|
ok.into_iter().map(|(a, m)| (a.into(), m)).collect()
)
}
/// Write a hash map of Address -> AccountMeta.
pub fn write_address_map<W>(m: &HashMap<util::Address, AccountMeta>, writer: &mut W) -> Result<(), serde_json::Error> where W: Write {
serde_json::to_writer(writer, &m.iter().map(|(a, m)| (a.clone().into(), m)).collect::<HashMap<hash::Address, _>>())
}
}
impl_serialization!(hash::Address => AccountMeta);

View File

@@ -16,13 +16,8 @@
//! Dapps settings de/serialization.
use std::io;
use std::collections::HashMap;
use serde_json;
use hash;
type DappId = String;
/// Settings for specific dapp.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DappsSettings {
@@ -30,26 +25,17 @@ pub struct DappsSettings {
pub accounts: Vec<hash::Address>,
}
impl DappsSettings {
/// Read a hash map of DappId -> DappsSettings
pub fn read_dapps_settings<R, S>(reader: R) -> Result<HashMap<DappId, S>, serde_json::Error> where
R: io::Read,
S: From<DappsSettings> + Clone,
{
serde_json::from_reader(reader).map(|ok: HashMap<DappId, DappsSettings>|
ok.into_iter().map(|(a, m)| (a.into(), m.into())).collect()
)
}
impl_serialization!(String => DappsSettings);
/// Write a hash map of DappId -> DappsSettings
pub fn write_dapps_settings<W, S>(m: &HashMap<DappId, S>, writer: &mut W) -> Result<(), serde_json::Error> where
W: io::Write,
S: Into<DappsSettings> + Clone,
{
serde_json::to_writer(writer, &m.iter().map(|(a, m)| (a.clone().into(), m.clone().into())).collect::<HashMap<DappId, DappsSettings>>())
}
/// History for specific dapp.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DappsHistory {
/// Last accessed timestamp
pub last_accessed: u64,
}
impl_serialization!(String => DappsHistory);
/// Accounts policy for new dapps.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum NewDappsPolicy {
@@ -59,22 +45,4 @@ pub enum NewDappsPolicy {
Whitelist(Vec<hash::Address>),
}
impl NewDappsPolicy {
/// Read a hash map of `String -> NewDappsPolicy`
pub fn read_new_dapps_policy<R, S>(reader: R) -> Result<HashMap<String, S>, serde_json::Error> where
R: io::Read,
S: From<NewDappsPolicy> + Clone,
{
serde_json::from_reader(reader).map(|ok: HashMap<String, NewDappsPolicy>|
ok.into_iter().map(|(a, m)| (a.into(), m.into())).collect()
)
}
/// Write a hash map of `String -> NewDappsPolicy`
pub fn write_new_dapps_policy<W, S>(m: &HashMap<String, S>, writer: &mut W) -> Result<(), serde_json::Error> where
W: io::Write,
S: Into<NewDappsPolicy> + Clone,
{
serde_json::to_writer(writer, &m.iter().map(|(a, m)| (a.clone().into(), m.clone().into())).collect::<HashMap<String, NewDappsPolicy>>())
}
}
impl_serialization!(String => NewDappsPolicy);

View File

@@ -16,8 +16,39 @@
//! Misc deserialization.
macro_rules! impl_serialization {
($key: ty => $name: ty) => {
impl $name {
/// Read a hash map of DappId -> $name
pub fn read<R, S, D>(reader: R) -> Result<::std::collections::HashMap<D, S>, ::serde_json::Error> where
R: ::std::io::Read,
D: From<$key> + ::std::hash::Hash + Eq,
S: From<$name> + Clone,
{
::serde_json::from_reader(reader).map(|ok: ::std::collections::HashMap<$key, $name>|
ok.into_iter().map(|(a, m)| (a.into(), m.into())).collect()
)
}
/// Write a hash map of DappId -> $name
pub fn write<W, S, D>(m: &::std::collections::HashMap<D, S>, writer: &mut W) -> Result<(), ::serde_json::Error> where
W: ::std::io::Write,
D: Into<$key> + ::std::hash::Hash + Eq + Clone,
S: Into<$name> + Clone,
{
::serde_json::to_writer(
writer,
&m.iter()
.map(|(a, m)| (a.clone().into(), m.clone().into()))
.collect::<::std::collections::HashMap<$key, $name>>()
)
}
}
}
}
mod account_meta;
mod dapps_settings;
pub use self::dapps_settings::{DappsSettings, NewDappsPolicy};
pub use self::dapps_settings::{DappsSettings, DappsHistory, NewDappsPolicy};
pub use self::account_meta::AccountMeta;