Skip to content

Commit

Permalink
Handle bioauth transaction status and properly process it
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylavrenov committed Apr 29, 2024
1 parent a7ee0aa commit 35fe830
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 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 Down
11 changes: 10 additions & 1 deletion crates/bioauth-flow-rpc/src/errors/authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use rpc_validator_key_logic::Error as ValidatorKeyError;
use sp_api::ApiError;
use sp_runtime::transaction_validity::InvalidTransaction;

use super::{api_error_code, sign::Error as SignError};
use super::{
api_error_code, sign::Error as SignError, tx_not_finalized::Error as TxNotFinalizedError,
};
use crate::error_data::{self, BioauthTxErrorDetails};

/// The `authenticate` method error kinds.
Expand All @@ -20,6 +22,8 @@ pub enum Error<TxPoolError: sc_transaction_pool_api::error::IntoPoolError> {
RuntimeApi(ApiError),
/// An error that can occur with transaction pool logic.
BioauthTx(TxPoolError),
/// An error that can occur with transaction finalization logic.
BioauthTxNotFinalized(TxNotFinalizedError),
}

impl<TxPoolError> From<Error<TxPoolError>> for jsonrpsee::core::Error
Expand Down Expand Up @@ -61,6 +65,11 @@ where
let (message, data) = map_txpool_error(err);
rpc_error_response::raw(api_error_code::TRANSACTION, message, data)
}
Error::BioauthTxNotFinalized(err) => rpc_error_response::data(
api_error_code::TRANSACTION,
err.to_string(),
error_data::ShouldRetry,
),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/bioauth-flow-rpc/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod get_facetec_device_sdk_params;
pub mod get_facetec_session_token;
pub mod sign;
pub mod status;
pub mod tx_not_finalized;

/// Custom rpc error codes.
pub mod api_error_code {
Expand Down
23 changes: 23 additions & 0 deletions crates/bioauth-flow-rpc/src/errors/tx_not_finalized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! The bioauth transaction not finalized related error kinds.

/// The bioauth transaction not finalized related error kinds.
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Transaction is no longer valid in the current state.
#[error("transaction is no longer valid in the current state")]
Invalid,
/// Transaction has been dropped from the pool because of the limit.
#[error("transaction has been dropped from the pool because of the limit")]
Dropped,
/// Transaction has been replaced in the pool, by another transaction
/// that provides the same tags. (e.g. same (sender, nonce)).
#[error("transaction has been replaced in the pool, by another transaction")]
Usurped,
/// The block this transaction was included in has been retracted.
#[error("the block this transaction was included in has been retracted")]
Retracted,
/// Maximum number of finality watchers has been reached,
/// old watchers are being removed.
#[error("finality timeout")]
FinalityTimeout,
}
37 changes: 33 additions & 4 deletions crates/bioauth-flow-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ use errors::{
authenticate::Error as AuthenticateError, enroll::Error as EnrollError,
get_facetec_device_sdk_params::Error as GetFacetecDeviceSdkParamsError,
get_facetec_session_token::Error as GetFacetecSessionToken, sign::Error as SignError,
status::Error as StatusError,
status::Error as StatusError, tx_not_finalized::Error as TxNotFinalizedError,
};
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};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use sp_api::{BlockT, Decode, Encode, ProvideRuntimeApi};
Expand Down Expand Up @@ -366,14 +367,42 @@ where
)
.map_err(AuthenticateError::RuntimeApi).map_err(errtype)?;

self.pool
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)?;
.map_err(AuthenticateError::BioauthTx).map_err(errtype)?.fuse();

loop {
let tx_status = watch.select_next_some().await;

match tx_status {
TransactionStatus::Finalized(_)=> break,
TransactionStatus::Retracted(_) => Err(
errtype(AuthenticateError::BioauthTxNotFinalized(TxNotFinalizedError::Retracted))
)?,
TransactionStatus::Usurped(_) => Err(
errtype(AuthenticateError::BioauthTxNotFinalized(TxNotFinalizedError::Usurped))
)?,
TransactionStatus::Dropped => Err(
errtype(AuthenticateError::BioauthTxNotFinalized(TxNotFinalizedError::Invalid))
)?,
TransactionStatus::FinalityTimeout(_) => Err(
errtype(AuthenticateError::BioauthTxNotFinalized(TxNotFinalizedError::FinalityTimeout))
)?,
TransactionStatus::Invalid => Err(
errtype(AuthenticateError::BioauthTxNotFinalized(TxNotFinalizedError::Invalid))
)?,
// Valid and expected statuses of transaction to be finalized.
TransactionStatus::Ready
| TransactionStatus::Broadcast(_)
| TransactionStatus::InBlock(_)
| TransactionStatus::Future => {},
}
}

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

Expand Down

0 comments on commit 35fe830

Please sign in to comment.