-
Notifications
You must be signed in to change notification settings - Fork 111
refactor(validator): refactor gRPC server implementation #1959
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cfc00de
refactor(validator): refactor gRPC server implementation
kkovaacs 3da29dc
chore: add changelog
kkovaacs b89e884
Update CHANGELOG.md
kkovaacs 889e3fd
Apply suggestion from @Copilot
kkovaacs 0f1fdf4
Merge branch 'next' into krisztian/refactor-validator-grpc-server
kkovaacs 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| use std::sync::atomic::Ordering; | ||
|
|
||
| use miden_node_proto::generated as grpc; | ||
| use miden_node_utils::ErrorReport; | ||
| use miden_protocol::block::ProposedBlock; | ||
| use miden_protocol::crypto::dsa::ecdsa_k256_keccak::Signature; | ||
| use miden_tx::utils::serde::{Deserializable, Serializable}; | ||
|
|
||
| use crate::block_validation::validate_block; | ||
| use crate::db::{load_chain_tip, upsert_block_header}; | ||
| use crate::server::ValidatorServer; | ||
|
|
||
| #[tonic::async_trait] | ||
| impl grpc::server::validator_api::SignBlock for ValidatorServer { | ||
| type Input = ProposedBlock; | ||
| type Output = Signature; | ||
|
|
||
| fn decode(request: grpc::blockchain::ProposedBlock) -> tonic::Result<Self::Input> { | ||
| ProposedBlock::read_from_bytes(&request.proposed_block).map_err(|err| { | ||
| tonic::Status::invalid_argument( | ||
| err.as_report_context("Failed to deserialize proposed block"), | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| fn encode(output: Self::Output) -> tonic::Result<grpc::blockchain::BlockSignature> { | ||
| Ok(grpc::blockchain::BlockSignature { signature: output.to_bytes() }) | ||
| } | ||
|
|
||
| async fn handle(&self, proposed_block: Self::Input) -> tonic::Result<Self::Output> { | ||
| // Serialize sign_block requests to prevent race conditions between loading the | ||
| // chain tip and persisting the validated block header. | ||
| let _permit = self.sign_block_semaphore.acquire().await.map_err(|err| { | ||
| tonic::Status::internal(format!("sign_block semaphore closed: {err}")) | ||
| })?; | ||
|
|
||
| // Load the current chain tip from the database. | ||
| let chain_tip = self | ||
| .db | ||
| .query("load_chain_tip", load_chain_tip) | ||
| .await | ||
| .map_err(|err| { | ||
| tonic::Status::internal(format!("Failed to load chain tip: {}", err.as_report())) | ||
| })? | ||
| .ok_or_else(|| tonic::Status::internal("Chain tip not found in database"))?; | ||
|
|
||
| // Validate the block against the current chain tip. | ||
| let (signature, header) = validate_block(proposed_block, &self.signer, &self.db, chain_tip) | ||
| .await | ||
| .map_err(|err| { | ||
| tonic::Status::invalid_argument(format!( | ||
| "Failed to validate block: {}", | ||
| err.as_report() | ||
| )) | ||
| })?; | ||
|
|
||
| // Persist the validated block header. | ||
| let new_block_num = header.block_num().as_u32(); | ||
| self.db | ||
| .transact("upsert_block_header", move |conn| upsert_block_header(conn, &header)) | ||
| .await | ||
| .map_err(|err| { | ||
| tonic::Status::internal(format!( | ||
| "Failed to persist block header: {}", | ||
| err.as_report() | ||
| )) | ||
| })?; | ||
|
|
||
| // Update the in-memory counters after successful persistence. | ||
| self.chain_tip.store(new_block_num, Ordering::Relaxed); | ||
| self.signed_blocks_count.fetch_add(1, Ordering::Relaxed); | ||
|
|
||
| Ok(signature) | ||
| } | ||
| } | ||
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,33 @@ | ||
| use std::sync::atomic::Ordering; | ||
|
|
||
| use miden_node_proto::generated as grpc; | ||
|
|
||
| use crate::server::ValidatorServer; | ||
|
|
||
| #[tonic::async_trait] | ||
| impl grpc::server::validator_api::Status for ValidatorServer { | ||
| type Input = (); | ||
| type Output = (); | ||
|
|
||
| async fn full(&self, _request: ()) -> tonic::Result<grpc::validator::ValidatorStatus> { | ||
| Ok(grpc::validator::ValidatorStatus { | ||
| version: env!("CARGO_PKG_VERSION").to_string(), | ||
| status: "OK".to_string(), | ||
| chain_tip: self.chain_tip.load(Ordering::Relaxed), | ||
| validated_transactions_count: self.validated_transactions_count.load(Ordering::Relaxed), | ||
| signed_blocks_count: self.signed_blocks_count.load(Ordering::Relaxed), | ||
| }) | ||
|
kkovaacs marked this conversation as resolved.
|
||
| } | ||
|
|
||
| async fn handle(&self, _input: Self::Input) -> tonic::Result<Self::Output> { | ||
| unimplemented!() | ||
| } | ||
|
|
||
| fn decode(_request: ()) -> tonic::Result<Self::Input> { | ||
| unimplemented!() | ||
| } | ||
|
|
||
| fn encode(_output: Self::Output) -> tonic::Result<grpc::validator::ValidatorStatus> { | ||
| unimplemented!() | ||
| } | ||
|
kkovaacs marked this conversation as resolved.
|
||
| } | ||
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,62 @@ | ||
| use std::sync::atomic::Ordering; | ||
|
|
||
| use miden_node_proto::generated as grpc; | ||
| use miden_node_utils::ErrorReport; | ||
| use miden_node_utils::tracing::OpenTelemetrySpanExt; | ||
| use miden_protocol::transaction::{ProvenTransaction, TransactionInputs}; | ||
| use miden_tx::utils::serde::Deserializable; | ||
| use tonic::Status; | ||
|
|
||
| use crate::db::insert_transaction; | ||
| use crate::server::ValidatorServer; | ||
| use crate::tx_validation::validate_transaction; | ||
|
|
||
| #[tonic::async_trait] | ||
| impl grpc::server::validator_api::SubmitProvenTransaction for ValidatorServer { | ||
| type Input = Input; | ||
| type Output = (); | ||
|
|
||
| async fn handle(&self, input: Self::Input) -> tonic::Result<Self::Output> { | ||
| tracing::Span::current().set_attribute("transaction.id", input.tx.id()); | ||
|
|
||
| // Validate the transaction. | ||
| let tx_info = validate_transaction(input.tx, input.inputs).await.map_err(|err| { | ||
| Status::invalid_argument(err.as_report_context("Invalid transaction")) | ||
| })?; | ||
|
|
||
| // Store the validated transaction. | ||
| let count = self | ||
| .db | ||
| .transact("insert_transaction", move |conn| insert_transaction(conn, &tx_info)) | ||
| .await | ||
| .map_err(|err| { | ||
| Status::internal(err.as_report_context("Failed to insert transaction")) | ||
| })?; | ||
|
|
||
| self.validated_transactions_count.fetch_add(count as u64, Ordering::Relaxed); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn decode(request: grpc::transaction::ProvenTransaction) -> tonic::Result<Self::Input> { | ||
| let tx = ProvenTransaction::read_from_bytes(&request.transaction).map_err(|err| { | ||
| Status::invalid_argument(err.as_report_context("Invalid proven transaction")) | ||
| })?; | ||
| let inputs = request | ||
| .transaction_inputs | ||
| .ok_or(Status::invalid_argument("Missing transaction inputs"))?; | ||
| let inputs = TransactionInputs::read_from_bytes(&inputs).map_err(|err| { | ||
| Status::invalid_argument(err.as_report_context("Invalid transaction inputs")) | ||
| })?; | ||
|
|
||
| Ok(Self::Input { tx, inputs }) | ||
| } | ||
|
|
||
| fn encode(output: Self::Output) -> tonic::Result<()> { | ||
| Ok(output) | ||
| } | ||
| } | ||
|
|
||
| pub struct Input { | ||
| tx: ProvenTransaction, | ||
| inputs: TransactionInputs, | ||
| } |
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.