Skip to content

Commit

Permalink
[PLA-1455] Connect to relay&matrix blockchains (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio authored Apr 29, 2024
1 parent 9934f26 commit 088292f
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PLATFORM_KEY=example
KEY_PASS=example
CONFIG_FILE=/opt/app/config.json
RUST_LOG=wallet_lib=warn
RUST_LOG=wallet_lib=warn
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
target/
node_modules
build/
store/
.store/
19 changes: 18 additions & 1 deletion bin/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,41 @@ async fn main() {
let (poll_job, sign_processor) = create_job_pair(
graphql_endpoint.clone(),
String::from(token.clone()),
String::from("matrix"),
Duration::from_millis(POLL_TRANSACTION_MS),
Arc::clone(&wallet_pair),
TRANSACTION_PAGE_SIZE.try_into().unwrap(),
);

let (poll_wallet_job, derive_wallet_processor) = create_wallet_job_pair(
graphql_endpoint,
graphql_endpoint.clone(),
String::from(token.clone()),
Duration::from_millis(POLL_ACCOUNT_MS),
Arc::clone(&wallet_pair),
ACCOUNT_PAGE_SIZE.try_into().unwrap(),
);

let (relay_pair, graphql, auth) =
load_relay_wallet::<DefaultConfig>(load_config().clone()).await;
let relay_pair = Arc::new(relay_pair);

let (relay_poll_job, relay_sign_processor) = create_job_pair(
graphql.clone(),
String::from(auth.clone()),
String::from("relay"),
Duration::from_millis(POLL_TRANSACTION_MS),
Arc::clone(&relay_pair),
TRANSACTION_PAGE_SIZE.try_into().unwrap(),
);

poll_job.start_job();
sign_processor.start_job();

poll_wallet_job.start_job();
derive_wallet_processor.start_job();

relay_poll_job.start_job();
relay_sign_processor.start_job();

signal::ctrl_c().await.expect("Failed to listen for ctrl c");
}
5 changes: 3 additions & 2 deletions config-test.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"node": "wss://rpc.matrix.canary.enjin.io:443",
"api": "http://localhost:8000/graphql",
"master_key": "../store"
"relay_node": "wss://rpc.relay.canary.enjin.io:443",
"api": "https://platform.canary.enjin.io/graphql",
"master_key": "tests/store"
}
7 changes: 4 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"node": "wss://rpc.matrix.blockchain.enjin.io:443",
"api": "https://platform.enjin.io/graphql",
"master_key": "/opt/app/storage"
"node": "wss://rpc.matrix.canary.enjin.io:443",
"relay_node": "wss://rpc.relay.canary.enjin.io:443",
"api": "https://platform.canary.enjin.io/graphql",
"master_key": "store"
}
37 changes: 33 additions & 4 deletions lib/src/config_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,22 @@ pub type PairSig<T> = PairSigner<T, Sr25519Pair>;
/// See the `config.json` file on the root directory to see an example.
#[derive(Deserialize, Clone, Debug, PartialEq)]
pub struct Configuration {
/// Url of the node to connect to.
/// Url of the matrix.
node: String,
/// Url of the relay.
relay_node: String,
/// Stored key path
master_key: PathBuf,
/// Endpoint for the graphql api
/// Platform GraphQL endpoint.
api: String,
}

#[cfg(test)]
impl Configuration {
pub(crate) fn new(node: String, master_key: PathBuf, api: String) -> Self {
pub(crate) fn new(node: String, relay_node: String, master_key: PathBuf, api: String) -> Self {
Self {
node,
relay_node,
master_key,
api,
}
Expand Down Expand Up @@ -181,7 +184,6 @@ fn get_password(env_name: &str) -> SecretString {
let password = SecretString::new(
env::var(env_name).unwrap_or_else(|_| panic!("Password {} not loaded in memory", env_name)),
);
env::remove_var(env_name);
password
}

Expand Down Expand Up @@ -225,6 +227,33 @@ where
load_wallet_with_connections(config, context_provider, connection).await
}

pub async fn load_relay_wallet<T>(
config: Configuration,
) -> (
WalletConnectionPair<T, ChainConnector<T>, ChainConnector<T>>,
String,
String,
)
where
T: subxt::Config + Sync + Send,
T::Signature: From<<Sr25519Pair as Pair>::Signature> + Verify + From<sp_core::ecdsa::Signature>,
<T::Signature as Verify>::Signer:
From<<Sr25519Pair as Pair>::Public> + IdentifyAccount<AccountId = T::AccountId>,
T::AccountId: Into<<T as subxt::Config>::Address>
+ std::fmt::Display
+ From<subxt::sp_runtime::AccountId32>,
T::Address: From<T::AccountId> + Send + Sync,
T::Extrinsic: Send,
T::BlockNumber: Into<u64>,
{
let context_provider = Arc::new(Connector::<Client<T>, String, subxt::BasicError>::new(
config.relay_node.to_owned(),
));
let connection = Arc::clone(&context_provider);

load_wallet_with_connections(config, context_provider, connection).await
}

pub(crate) async fn load_wallet_with_connections<T, ChainConnection, ContextProvider>(
config: Configuration,
context_provider: Arc<ContextProvider>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
mutation MarkAndListPendingTransactions($after: String, $first: Int) {
MarkAndListPendingTransactions(after: $after, first: $first) {
mutation MarkAndListPendingTransactions($network: String, $after: String, $first: Int) {
MarkAndListPendingTransactions(
network: $network
after: $after
first: $first
) {
edges {
cursor
node {
id
encodedData
network
wallet {
externalId
managed
Expand Down
4 changes: 3 additions & 1 deletion lib/src/graphql_schemas/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Transaction {
transactionHash: String
wallet: Wallet
encodedData: String!
network: String!
}

type Account {
Expand Down Expand Up @@ -66,7 +67,8 @@ type Query {
}

type Mutation {
MarkAndListPendingTransactions(after: String, first: Int): TransactionConnection

MarkAndListPendingTransactions(network: String, after: String, first: Int): TransactionConnection

UpdateTransaction(
id: Int!
Expand Down
29 changes: 21 additions & 8 deletions lib/src/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ pub struct PollJob<Client> {
url: Arc<String>,
/// Static token for authorization
token: Arc<String>,
///
network: Arc<String>,
/// Page size(pagination)
limit: i64,
}
Expand All @@ -343,6 +345,7 @@ pub struct PollJob<Client> {
pub fn create_job_pair<T>(
graphql_endpoint: String,
token: String,
network: String,
delay: Duration,
wallet_connection_pair: Arc<WalletConnectionPair<T, ChainConnector<T>, ChainConnector<T>>>,
page_size: i64,
Expand All @@ -364,6 +367,7 @@ pub fn create_job_pair<T>(
create_job_pair_with_executor(
graphql_endpoint,
token,
network,
delay,
wallet_connection_pair,
page_size,
Expand All @@ -375,6 +379,7 @@ pub fn create_job_pair<T>(
pub(crate) fn create_job_pair_with_executor<T, Client, ChainConnection, ContextProvider>(
graphql_endpoint: String,
token: String,
network: String,
delay: Duration,
wallet_connection_pair: Arc<WalletConnectionPair<T, ChainConnection, ContextProvider>>,
page_size: i64,
Expand Down Expand Up @@ -413,9 +418,12 @@ pub(crate) fn create_job_pair_with_executor<T, Client, ChainConnection, ContextP
let (tx, rx) = tokio::sync::mpsc::channel(50_000);
let graphql_endpoint = Arc::new(graphql_endpoint);
let token = Arc::new(token);
let network = Arc::new(network);

let poll_job = PollJob::new(
Arc::clone(&graphql_endpoint),
Arc::clone(&token),
Arc::clone(&network),
delay,
tx,
client.clone(),
Expand All @@ -441,8 +449,10 @@ where
{
#[cfg(not(tarpaulin_include))]
fn build_request(&self, limit: Option<i64>) -> Request {
let network = &*self.network;
let request_body = MarkAndListPendingTransactions::build_query(
mark_and_list_pending_transactions::Variables {
network: Some(network.clone().to_string()),
after: None,
first: limit,
},
Expand All @@ -460,6 +470,7 @@ where
pub fn new(
url: Arc<String>,
token: Arc<String>,
network: Arc<String>,
delay: Duration,
tx: Sender<Vec<SignRequest>>,
client: Arc<reqwest::Client>,
Expand All @@ -473,6 +484,7 @@ where
tx,
url,
token,
network,
limit,
}
}
Expand Down Expand Up @@ -1169,8 +1181,8 @@ where
let transaction = transaction.clone();
let wallet = &wallet_connection_pair.wallet;

/// This can be improved by making the daemon process the transactions in sequence
/// Since it process in parallel and we can have a transaction that deletes the account
// This can be improved by making the daemon process the transactions in sequence
// Since it process in parallel and we can have a transaction that deletes the account
Self::reset_wallet_nonce(wallet, &external_id).await;

let connection = &wallet_connection_pair.connection;
Expand Down Expand Up @@ -1288,7 +1300,7 @@ for SignRequest
.encoded_data
.split('x')
.nth(1)
.ok_or("No '0x' at the beggining")?;
.ok_or("No '0x' at the beginning")?;
let transaction = hex::decode(data).map_err(|e| {
tracing::error!("Error decoding: {:?}", e);
e
Expand Down Expand Up @@ -1350,12 +1362,13 @@ pub struct PaginationInput {
///
/// ## The query (Can be found in the `query_path`)
/// ```ignore
/// mutation MarkAndListPendingTransactions($after: String, $first: Int) {
/// MarkAndListPendingTransactions(after: $after, first: $first) {
/// mutation MarkAndListPendingTransactions($network: String, $after: String, $first: Int) {
/// MarkAndListPendingTransactions(network: $network, after: $after, first: $first) {
/// edges {
/// node {
/// encodedData
/// id
/// encodedData
/// network
/// wallet {
/// externalId
/// managed
Expand All @@ -1373,7 +1386,7 @@ pub struct PaginationInput {
)]
pub struct MarkAndListPendingTransactions;

/// GraphQL mutation to submit the received txHash.
/// GraphQL's mutation to submit the received txHash.
///
/// ## The query
/// ```ignore
Expand All @@ -1395,7 +1408,7 @@ pub struct MarkAndListPendingTransactions;
)]
pub struct UpdateTransaction;

/// GraphQL mutation to submit the received txHash.
/// GraphQL's mutation to submit the received txHash.
///
/// ## The query
/// ```ignore
Expand Down
3 changes: 2 additions & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
//! let (poll_job, sign_processor) = create_job_pair(
//! graphql_endpoint,
//! token,
//! String::from("matrix"),
//! Duration::from_millis(6000),
//! Arc::new(connection_pair),
//! 10,
Expand Down Expand Up @@ -51,7 +52,7 @@ pub(crate) use connection::{chain_connection, client_connection, connection_hand
pub(crate) use types::{SignedExtra, UncheckedExtrinsic};

pub use crate::wallet_trait::EfinityWallet;
pub use config_loader::{load_config, load_wallet};
pub use config_loader::{load_config, load_relay_wallet, load_wallet};
pub use jobs::{
create_job_pair, create_wallet_job_pair, DeriveWalletProcessor, PollJob, PollWalletJob,
SignProcessor,
Expand Down
1 change: 1 addition & 0 deletions lib/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl RequestExecutor for MockClient {
"node": {
"id": 1,
"encodedData": "0x2800016400000000000000016400000000000000000000000000000000",
"network": "matrix",
"wallet": {
"externalId": null,
"managed": true
Expand Down
Loading

0 comments on commit 088292f

Please sign in to comment.