Skip to content

Issue #336 Multi-chain support #472

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

Merged
merged 9 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 7 additions & 4 deletions adapter/src/adapter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::primitives::*;
use async_trait::async_trait;
use primitives::{ChainId, ChainOf, Channel};
use std::{marker::PhantomData, sync::Arc};

use crate::{
Expand Down Expand Up @@ -93,8 +94,10 @@ where
self.client.sign(state_root).map_err(Into::into)
}

fn get_auth(&self, intended_for: ValidatorId) -> Result<String, Error> {
self.client.get_auth(intended_for).map_err(Into::into)
fn get_auth(&self, for_chain: ChainId, intended_for: ValidatorId) -> Result<String, Error> {
self.client
.get_auth(for_chain, intended_for)
.map_err(Into::into)
}
}

Expand Down Expand Up @@ -134,11 +137,11 @@ where

async fn get_deposit(
&self,
channel: &Channel,
channel_context: &ChainOf<Channel>,
depositor_address: Address,
) -> Result<Deposit, Error> {
self.client
.get_deposit(channel, depositor_address)
.get_deposit(channel_context, depositor_address)
.await
.map_err(Into::into)
}
Expand Down
11 changes: 8 additions & 3 deletions adapter/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use crate::primitives::{Deposit, Session};
use async_trait::async_trait;
use primitives::{Address, Channel, ValidatorId};
use primitives::{Address, ChainId, ChainOf, Channel, ValidatorId};

#[async_trait]
/// Available methods for Locked clients.
Expand All @@ -30,7 +30,7 @@ pub trait Locked: Sync + Send {

async fn get_deposit(
&self,
channel: &Channel,
channel_context: &ChainOf<Channel>,
depositor_address: Address,
) -> Result<Deposit, Self::Error>;

Expand All @@ -48,14 +48,19 @@ pub trait Locked: Sync + Send {
}

/// Available methods for Unlocked clients.
///
/// Unlocked clients should also implement [`Locked`].
#[async_trait]
pub trait Unlocked: Locked {
// requires Unlocked
fn sign(&self, state_root: &str) -> Result<String, Self::Error>;

// requires Unlocked
fn get_auth(&self, intended_for: ValidatorId) -> Result<String, Self::Error>;
fn get_auth(
&self,
for_chain: ChainId,
intended_for: ValidatorId,
) -> Result<String, Self::Error>;
}

/// A client that can be `unlock()`ed
Expand Down
60 changes: 48 additions & 12 deletions adapter/src/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,28 @@ use crate::{
use async_trait::async_trait;
use dashmap::{mapref::entry::Entry, DashMap};

use primitives::{Address, Channel, ChannelId, ToETHChecksum, ValidatorId};
use once_cell::sync::Lazy;
use primitives::{
Address, Chain, ChainId, ChainOf, Channel, ChannelId, ToETHChecksum, ValidatorId,
};
use std::{collections::HashMap, sync::Arc};

pub type Adapter<S> = crate::Adapter<Dummy, S>;

/// The Dummy Chain to be used with this adapter
/// The Chain is not applicable to the adapter, however, it is required for
/// applications because of the `authentication` & [`Channel`] interactions.
pub static DUMMY_CHAIN: Lazy<Chain> = Lazy::new(|| Chain {
chain_id: ChainId::new(1),
rpc: "http://dummy.com".parse().expect("Should parse ApiUrl"),
outpace: "0x0000000000000000000000000000000000000000"
.parse()
.unwrap(),
sweeper: "0x0000000000000000000000000000000000000000"
.parse()
.unwrap(),
});

/// Dummy adapter implementation intended for testing.
#[derive(Debug, Clone)]
pub struct Dummy {
Expand Down Expand Up @@ -86,6 +103,9 @@ impl Locked for Dummy {
}

/// Verify, based on the signature & state_root, that the signer is the same
///
/// Splits the signature by `" "` (whitespace) and takes
/// the last part of it which contains the signer [`Address`].
fn verify(
&self,
signer: ValidatorId,
Expand All @@ -102,8 +122,8 @@ impl Locked for Dummy {
Ok(is_same)
}

/// Creates a `Session` from a provided Token by calling the Contract.
/// Does **not** cache the (`Token`, `Session`) pair.
/// Finds the authorization token from the configured values
/// and creates a [`Session`] out of it using a [`ChainId`] of `1`.
async fn session_from_token(&self, token: &str) -> Result<Session, crate::Error> {
let identity = self
.authorization_tokens
Expand All @@ -114,6 +134,7 @@ impl Locked for Dummy {
Some((address, _token)) => Ok(Session {
uid: *address,
era: 0,
chain: DUMMY_CHAIN.clone(),
}),
None => Err(Error::authentication(format!(
"No identity found that matches authentication token: {}",
Expand All @@ -124,11 +145,11 @@ impl Locked for Dummy {

async fn get_deposit(
&self,
channel: &Channel,
channel_context: &ChainOf<Channel>,
depositor_address: Address,
) -> Result<Deposit, crate::Error> {
self.deposits
.get_next_deposit(channel.id(), depositor_address)
.get_next_deposit(channel_context.context.id(), depositor_address)
.ok_or_else(|| {
Error::adapter(format!(
"No more mocked deposits found for depositor {:?}",
Expand All @@ -151,7 +172,7 @@ impl Unlocked for Dummy {
}

// requires Unlocked
fn get_auth(&self, _intended_for: ValidatorId) -> Result<String, Error> {
fn get_auth(&self, _for_chain: ChainId, _intended_for: ValidatorId) -> Result<String, Error> {
self.authorization_tokens
.get(&self.identity.to_address())
.cloned()
Expand All @@ -174,16 +195,31 @@ impl Unlockable for Dummy {

#[cfg(test)]
mod test {
use std::num::NonZeroU8;

use primitives::{
config::TokenInfo,
util::tests::prep_db::{ADDRESSES, DUMMY_CAMPAIGN, IDS},
BigNum,
BigNum, ChainOf, UnifiedNum,
};

use super::*;

#[tokio::test]
async fn test_deposits_calls() {
let channel = DUMMY_CAMPAIGN.channel;

let channel_context = ChainOf {
context: channel,
token: TokenInfo {
min_token_units_for_deposit: 1_u64.into(),
min_validator_fee: 1_u64.into(),
precision: NonZeroU8::new(UnifiedNum::PRECISION).expect("Non zero u8"),
address: channel.token,
},
chain: DUMMY_CHAIN.clone(),
};

let dummy_client = Dummy::init(Options {
dummy_identity: IDS["leader"],
dummy_auth_tokens: Default::default(),
Expand All @@ -193,7 +229,7 @@ mod test {

// no mocked deposit calls should cause an Error
{
let result = dummy_client.get_deposit(&channel, address).await;
let result = dummy_client.get_deposit(&channel_context, address).await;

assert!(result.is_err());
}
Expand All @@ -211,25 +247,25 @@ mod test {
dummy_client.add_deposit_call(channel.id(), address, deposits[1].clone());

let first_call = dummy_client
.get_deposit(&channel, address)
.get_deposit(&channel_context, address)
.await
.expect("Should get first mocked deposit");
assert_eq!(&deposits[0], &first_call);

// should not affect the Mocked deposit calls and should cause an error
let different_address_call = dummy_client
.get_deposit(&channel, ADDRESSES["leader"])
.get_deposit(&channel_context, ADDRESSES["leader"])
.await;
assert!(different_address_call.is_err());

let second_call = dummy_client
.get_deposit(&channel, address)
.get_deposit(&channel_context, address)
.await
.expect("Should get second mocked deposit");
assert_eq!(&deposits[1], &second_call);

// Third call should error, we've only mocked 2 calls!
let third_call = dummy_client.get_deposit(&channel, address).await;
let third_call = dummy_client.get_deposit(&channel_context, address).await;
assert!(third_call.is_err());
}
}
Expand Down
2 changes: 1 addition & 1 deletion adapter/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl fmt::Display for Inner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.source {
// Writes: "Kind: Error message here"
Some(source) => write!(f, "{}: {}", self.kind, source.to_string()),
Some(source) => write!(f, "{}: {}", self.kind, source),
// Writes: "Kind"
None => write!(f, "{}", self.kind),
}
Expand Down
Loading