Skip to content

Commit

Permalink
Merge branch 'master' into integrate-all-available-frontier-precompiles
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylavrenov authored May 6, 2024
2 parents 76273de + 22eda6a commit 61bac82
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/bioauth-flow-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rpc-error-response = { path = "../rpc-error-response" }
rpc-validator-key-logic = { path = "../rpc-validator-key-logic" }

async-trait = { workspace = true }
futures = { workspace = true }
jsonrpsee = { workspace = true, features = ["server", "macros"] }
sc-transaction-pool-api = { workspace = true }
serde = { workspace = true, features = ["default"] }
Expand All @@ -23,4 +24,5 @@ sp-api = { workspace = true }
sp-blockchain = { workspace = true }
sp-runtime = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
91 changes: 80 additions & 11 deletions crates/bioauth-flow-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ use errors::{
get_facetec_session_token::Error as GetFacetecSessionToken, sign::Error as SignError,
status::Error as StatusError,
};
use futures::StreamExt;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use primitives_liveness_data::{LivenessData, OpaqueLivenessData};
use robonode_client::{AuthenticateRequest, EnrollRequest};
use rpc_deny_unsafe::DenyUnsafe;
use sc_transaction_pool_api::TransactionPool as TransactionPoolT;
use sc_transaction_pool_api::{TransactionPool as TransactionPoolT, TransactionStatus, TxHash};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use sp_api::{BlockT, Decode, Encode, ProvideRuntimeApi};
Expand Down Expand Up @@ -93,7 +94,7 @@ impl<T> From<bioauth_flow_api::BioauthStatus<T>> for BioauthStatus<T> {

/// The API exposed via JSON-RPC.
#[rpc(server)]
pub trait Bioauth<Timestamp> {
pub trait Bioauth<Timestamp, TxHash> {
/// Get the configuration required for the Device SDK.
#[method(name = "bioauth_getFacetecDeviceSdkParams")]
async fn get_facetec_device_sdk_params(&self) -> RpcResult<FacetecDeviceSdkParams>;
Expand All @@ -112,7 +113,7 @@ pub trait Bioauth<Timestamp> {

/// Authenticate with provided liveness data.
#[method(name = "bioauth_authenticate")]
async fn authenticate(&self, liveness_data: LivenessData) -> RpcResult<()>;
async fn authenticate(&self, liveness_data: LivenessData) -> RpcResult<TxHash>;
}

/// The RPC implementation.
Expand Down Expand Up @@ -229,7 +230,7 @@ impl<
Block,
Timestamp,
TransactionPool,
> BioauthServer<Timestamp>
> BioauthServer<Timestamp, TxHash<TransactionPool>>
for Bioauth<
RobonodeClient,
ValidatorKeyExtractor,
Expand Down Expand Up @@ -329,7 +330,7 @@ where
Ok(())
}

async fn authenticate(&self, liveness_data: LivenessData) -> RpcResult<()> {
async fn authenticate(&self, liveness_data: LivenessData) -> RpcResult<TxHash<TransactionPool>> {
self.deny_unsafe.check_if_safe()?;

info!("Bioauth flow - authentication in progress");
Expand Down Expand Up @@ -366,17 +367,85 @@ where
)
.map_err(AuthenticateError::RuntimeApi).map_err(errtype)?;

self.pool
info!("Bioauth flow - submitting authenticate transaction");

let tx_hash = self.pool.hash_of(&ext);

let mut watch = self.pool
.submit_and_watch(
&sp_api::BlockId::Hash(at),
sp_runtime::transaction_validity::TransactionSource::Local,
ext,
)
.await
.map_err(AuthenticateError::BioauthTx).map_err(errtype)?;

info!("Bioauth flow - authenticate transaction complete");

Ok(())
.map_err(AuthenticateError::BioauthTx).map_err(errtype)?.fuse();

tokio::spawn(async move {
loop {
let maybe_tx_status = watch.next().await;

match maybe_tx_status {
Some(TransactionStatus::Finalized((block_hash, _)))=> {
info!(
message = "Bioauth flow - authenticate transaction is in finalized block",
%block_hash,
);
break
},
Some(TransactionStatus::Retracted(block_hash)) => {
error!(
message = "Bioauth flow - the block this transaction was included in has been retracted",
%block_hash,
);
break
},
Some(TransactionStatus::Usurped(_)) => {
error!(
"Bioauth flow - transaction has been replaced in the pool, by another transaction",
);
break
},
Some(TransactionStatus::Dropped) => {
error!(
"Bioauth flow - transaction has been dropped from the pool because of the limit",
);
break
},
Some(TransactionStatus::FinalityTimeout(_)) => {
error!(
"Bioauth flow - maximum number of finality watchers has been reached, old watchers are being removed",
);
break
},
Some(TransactionStatus::Invalid) => {
error!(
"Bioauth flow - transaction is no longer valid in the current state",
);
break
},
Some(TransactionStatus::Ready) => info!("Bioauth flow - authenticate transaction is in ready queue"),
Some(TransactionStatus::Broadcast(_)) => {
info!("Bioauth flow - authenticate transaction is broadcasted");
},
Some(TransactionStatus::InBlock((block_hash, _))) => {
info!(
message = "Bioauth flow - authenticate transaction is in block",
%block_hash,
);
},
Some(TransactionStatus::Future) => info!("Bioauth flow - authenticate transaction is in future queue"),
None => {
error!(
"Bioauth flow - unexpected transaction flow interruption",
);
break
}
}
}

info!("Bioauth flow - authenticate transaction complete");
});

Ok(tx_hash)
}
}

0 comments on commit 61bac82

Please sign in to comment.