-
Notifications
You must be signed in to change notification settings - Fork 6
Add account nonce management tracking in server state (RUN-75) #271
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
DOBEN
merged 12 commits into
feature/RUN-36-credential-verification-service
from
account-sequence-number-management
Dec 4, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ff3e60f
Add account nonce management tracking in server state
DOBEN e884442
Outline the account sequence number management
DOBEN ecbef40
Complete account nonce management in verification anchor flow
DOBEN 77ea45c
Use proper parameters
DOBEN f6b2e77
Some optimization and clean-up
DOBEN 7e07c20
Update readme
DOBEN 3f2f46f
Update claim request type
DOBEN c364513
Re-submit transaction after refreshing account nonce
DOBEN 86a1d38
Handle request timeouts
DOBEN 53958db
Merge branch 'feature/RUN-36-credential-verification-service' into ac…
DOBEN 4308d71
Update submodule link
DOBEN 7c243f6
Update time-out values
DOBEN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| ## Unreleased | ||
|
|
||
| - Added account nonce management to the service. | ||
| - Added the logic of the `/verifiable-presentations/create-verification-request` api endpoint flow. This endpoint submits the `verification-request-anchor (VRA)` on-chain. | ||
| - Initial service. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
credential-verification-service/src/api/verification_request.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| //! Handler for create-verification-request endpoint. | ||
| use crate::{ | ||
| api_types::CreateVerificationRequest, | ||
| types::{ServerError, Service}, | ||
| }; | ||
| use axum::{Json, extract::State}; | ||
| use concordium_rust_sdk::{ | ||
| base::web3id::v1::anchor::{ | ||
| LabeledContextProperty, UnfilledContextInformationBuilder, VerificationRequest, | ||
| VerificationRequestDataBuilder, | ||
| }, | ||
| common::types::TransactionTime, | ||
| v2::{QueryError, RPCError}, | ||
| web3id::v1::{ | ||
| AnchorTransactionMetadata, CreateAnchorError::Query, | ||
| create_verification_request_and_submit_request_anchor, | ||
| }, | ||
| }; | ||
| use std::sync::Arc; | ||
|
|
||
| pub async fn create_verification_request( | ||
| State(state): State<Arc<Service>>, | ||
| Json(params): Json<CreateVerificationRequest>, | ||
| ) -> Result<Json<VerificationRequest>, ServerError> { | ||
| let context = UnfilledContextInformationBuilder::new_simple( | ||
| params.nonce, | ||
| params.connection_id, | ||
| params.context_string, | ||
| ) | ||
| .given(LabeledContextProperty::ResourceId(params.rescource_id)) | ||
| .build(); | ||
|
|
||
| let mut builder = VerificationRequestDataBuilder::new(context); | ||
| for claim in params.requested_claims { | ||
| builder = builder.subject_claim(claim); | ||
| } | ||
| let verification_request_data = builder.build(); | ||
|
|
||
| // Transaction should expiry after some seconds. | ||
| let expiry = TransactionTime::seconds_after(state.transaction_expiry_secs); | ||
|
|
||
| let mut node_client = state.node_client.clone(); | ||
|
|
||
| // Get the current nonce for the backend wallet and lock it. This is necessary | ||
| // since it is possible that API requests come in parallel. The nonce is | ||
| // increased by 1 and its lock is released after the transaction is submitted to | ||
| // the blockchain. | ||
| let mut account_sequence_number = state.nonce.lock().await; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need a timeout on this async operation (locking), like we have for calling the node. |
||
|
|
||
| let anchor_transaction_metadata = AnchorTransactionMetadata { | ||
| signer: &state.account_keys, | ||
| sender: state.account_keys.address, | ||
| account_sequence_number: *account_sequence_number, | ||
| expiry, | ||
| }; | ||
|
|
||
| let verification_request = create_verification_request_and_submit_request_anchor( | ||
| &mut node_client, | ||
| anchor_transaction_metadata, | ||
| verification_request_data.clone(), | ||
| None, | ||
| ) | ||
| .await; | ||
|
|
||
| match verification_request { | ||
| Ok(req) => { | ||
| // If the submission of the anchor transaction was successful, | ||
| // increase the account_sequence_number tracked in this service. | ||
| *account_sequence_number = account_sequence_number.next(); | ||
| Ok(Json(req)) | ||
| } | ||
|
|
||
| Err(e) => { | ||
| // If the error is due to an account sequence number mismatch, | ||
| // refresh the value in the state and try to resubmit the transaction. | ||
| if let Query(QueryError::RPCError(RPCError::CallError(ref err))) = e { | ||
| let msg = err.message(); | ||
| let is_nonce_err = msg == "Duplicate nonce" || msg == "Nonce too large"; | ||
|
|
||
| if is_nonce_err { | ||
| tracing::warn!( | ||
| "Unable to submit transaction on-chain successfully due to account nonce mismatch: {}. | ||
| Account nonce will be re-freshed and transaction will be re-submitted.", | ||
| msg | ||
| ); | ||
|
|
||
| // Refresh nonce | ||
| let nonce_response = node_client | ||
| .get_next_account_sequence_number(&state.account_keys.address) | ||
| .await | ||
| .map_err(|e| ServerError::SubmitAnchorTransaction(e.into()))?; | ||
|
|
||
| *account_sequence_number = nonce_response.nonce; | ||
|
|
||
| tracing::info!("Refreshed account nonce successfully."); | ||
|
|
||
| // Retry anchor transaction. | ||
| let meta = AnchorTransactionMetadata { | ||
| signer: &state.account_keys, | ||
| sender: state.account_keys.address, | ||
| account_sequence_number: nonce_response.nonce, | ||
| expiry, | ||
| }; | ||
|
|
||
| let verification_request = | ||
| create_verification_request_and_submit_request_anchor( | ||
| &mut node_client, | ||
| meta, | ||
| verification_request_data, | ||
| None, | ||
| ) | ||
| .await?; | ||
|
|
||
| tracing::info!( | ||
| "Successfully submitted anchor transaction after the account nonce was refreshed." | ||
| ); | ||
|
|
||
| *account_sequence_number = account_sequence_number.next(); | ||
|
|
||
| return Ok(Json(verification_request)); | ||
| } | ||
| } | ||
|
|
||
| Err(ServerError::SubmitAnchorTransaction(e)) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| //! Handlers for verification endpoints. | ||
| //! Handler for the verification endpoints. | ||
| use crate::types::Service; | ||
| use axum::{Json, extract::State}; | ||
| use std::sync::Arc; | ||
|
|
||
| pub async fn verify() -> Result<String, String> { | ||
| Ok("Verified".to_owned()) | ||
| pub async fn verify(_state: State<Arc<Service>>, Json(_payload): Json<bool>) -> Json<String> { | ||
| Json("ok".to_string()) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.