Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions crates/block-producer/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::num::NonZeroU32;
use itertools::Itertools;
use miden_node_proto::clients::{Builder, StoreBlockProducerClient};
use miden_node_proto::domain::batch::BatchInputs;
use miden_node_proto::errors::{ConversionError, MissingFieldHelper};
use miden_node_proto::errors::{ConversionError, ConversionResultExt};
use miden_node_proto::{AccountState, generated as proto};
use miden_node_utils::formatting::format_opt;
use miden_protocol::Word;
Expand Down Expand Up @@ -72,19 +72,21 @@ impl TryFrom<proto::store::TransactionInputs> for TransactionInputs {
fn try_from(response: proto::store::TransactionInputs) -> Result<Self, Self::Error> {
let AccountState { account_id, account_commitment } = response
.account_state
.ok_or(proto::store::TransactionInputs::missing_field(stringify!(account_state)))?
.try_into()?;
.ok_or(ConversionError::missing_field::<proto::store::TransactionInputs>(stringify!(
account_state
)))?
.try_into()
.context("account_state")?;

let mut nullifiers = HashMap::new();
for nullifier_record in response.nullifiers {
let nullifier = nullifier_record
.nullifier
.ok_or(
proto::store::transaction_inputs::NullifierTransactionInputRecord::missing_field(
stringify!(nullifier),
),
)?
.try_into()?;
.ok_or(ConversionError::missing_field::<
proto::store::transaction_inputs::NullifierTransactionInputRecord,
>(stringify!(nullifier)))?
.try_into()
.context("nullifier")?;

// Note that this intentionally maps 0 to None as this is the definition used in
// protobuf.
Expand All @@ -95,7 +97,8 @@ impl TryFrom<proto::store::TransactionInputs> for TransactionInputs {
.found_unauthenticated_notes
.into_iter()
.map(Word::try_from)
.collect::<Result<_, ConversionError>>()?;
.collect::<Result<_, ConversionError>>()
.context("found_unauthenticated_notes")?;

let current_block_height = response.block_height.into();

Expand Down Expand Up @@ -148,11 +151,13 @@ impl StoreClient {
.await?
.into_inner()
.block_header
.ok_or(miden_node_proto::generated::blockchain::BlockHeader::missing_field(
"block_header",
))?;
.ok_or_else(|| {
StoreError::DeserializationError(ConversionError::missing_field::<
miden_node_proto::generated::blockchain::BlockHeader,
>("block_header"))
})?;

BlockHeader::try_from(response).map_err(Into::into)
BlockHeader::try_from(response).map_err(StoreError::DeserializationError)
}

#[instrument(target = COMPONENT, name = "store.client.get_tx_inputs", skip_all, err)]
Expand Down Expand Up @@ -219,7 +224,7 @@ impl StoreClient {

let store_response = self.client.clone().get_block_inputs(request).await?.into_inner();

store_response.try_into().map_err(Into::into)
store_response.try_into().map_err(StoreError::DeserializationError)
}

#[instrument(target = COMPONENT, name = "store.client.get_batch_inputs", skip_all, err)]
Expand All @@ -235,7 +240,7 @@ impl StoreClient {

let store_response = self.client.clone().get_batch_inputs(request).await?.into_inner();

store_response.try_into().map_err(Into::into)
store_response.try_into().map_err(StoreError::DeserializationError)
}

#[instrument(target = COMPONENT, name = "store.client.apply_block", skip_all, err)]
Expand Down
55 changes: 31 additions & 24 deletions crates/ntx-builder/src/clients/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::time::Duration;

use miden_node_proto::clients::{Builder, StoreNtxBuilderClient};
use miden_node_proto::domain::account::{AccountDetails, AccountResponse, NetworkAccountId};
use miden_node_proto::errors::ConversionError;
use miden_node_proto::errors::{ConversionError, ConversionResultExt};
use miden_node_proto::generated::rpc::BlockRange;
use miden_node_proto::generated::{self as proto};
use miden_node_proto::try_convert;
Expand Down Expand Up @@ -101,7 +101,10 @@ impl StoreClient {
match response.current_block_header {
// There are new blocks compared to the builder's latest state
Some(block) => {
let peaks = try_convert(response.current_peaks).collect::<Result<_, _>>()?;
let peaks: Vec<Word> = try_convert(response.current_peaks)
.collect::<Result<_, _>>()
.context("current_peaks")
.map_err(StoreError::DeserializationError)?;
let header =
BlockHeader::try_from(block).map_err(StoreError::DeserializationError)?;

Expand Down Expand Up @@ -140,9 +143,7 @@ impl StoreClient {
// which implies details being public, so OK to error otherwise
let account = match store_response.map(|acc| acc.details) {
Some(Some(details)) => Some(Account::read_from_bytes(&details).map_err(|err| {
StoreError::DeserializationError(ConversionError::deserialization_error(
"account", err,
))
StoreError::DeserializationError(ConversionError::from(err).context("details"))
})?),
_ => None,
};
Expand Down Expand Up @@ -185,7 +186,8 @@ impl StoreClient {
let account_details = account_response
.details
.ok_or(StoreError::MissingDetails("account details".into()))?;
let partial_account = build_minimal_foreign_account(&account_details)?;
let partial_account = build_minimal_foreign_account(&account_details)
.map_err(StoreError::DeserializationError)?;

Ok(AccountInputs::new(partial_account, account_response.witness))
}
Expand Down Expand Up @@ -216,7 +218,10 @@ impl StoreClient {

all_notes.reserve(resp.notes.len());
for note in resp.notes {
all_notes.push(AccountTargetNetworkNote::try_from(note)?);
all_notes.push(
AccountTargetNetworkNote::try_from(note)
.map_err(StoreError::DeserializationError)?,
);
}

match resp.next_token {
Expand Down Expand Up @@ -317,10 +322,9 @@ impl StoreClient {
.into_iter()
.map(|account_id| {
let account_id = AccountId::read_from_bytes(&account_id.id).map_err(|err| {
StoreError::DeserializationError(ConversionError::deserialization_error(
"account_id",
err,
))
StoreError::DeserializationError(
ConversionError::from(err).context("account_id"),
)
})?;
NetworkAccountId::try_from(account_id).map_err(|_| {
StoreError::MalformedResponse(
Expand All @@ -330,12 +334,9 @@ impl StoreClient {
})
.collect::<Result<Vec<NetworkAccountId>, StoreError>>()?;

let pagination_info = response.pagination_info.ok_or(
ConversionError::MissingFieldInProtobufRepresentation {
entity: "NetworkAccountIdList",
field_name: "pagination_info",
},
)?;
let pagination_info = response.pagination_info.ok_or(ConversionError::missing_field::<
proto::store::NetworkAccountIdList,
>("pagination_info"))?;

Ok((accounts, pagination_info))
}
Expand Down Expand Up @@ -406,8 +407,10 @@ impl StoreClient {
let smt_opening = asset_witness.proof.ok_or_else(|| {
StoreError::MalformedResponse("missing proof in vault asset witness".to_string())
})?;
let proof: SmtProof =
smt_opening.try_into().map_err(StoreError::DeserializationError)?;
let proof: SmtProof = smt_opening
.try_into()
.context("proof")
.map_err(StoreError::DeserializationError)?;
let witness = AssetWitness::new(proof)
.map_err(|err| StoreError::DeserializationError(ConversionError::from(err)))?;

Expand Down Expand Up @@ -445,7 +448,10 @@ impl StoreClient {
StoreError::MalformedResponse("missing proof in storage map witness".to_string())
})?;

let proof: SmtProof = smt_opening.try_into().map_err(StoreError::DeserializationError)?;
let proof: SmtProof = smt_opening
.try_into()
.context("proof")
.map_err(StoreError::DeserializationError)?;

// Create the storage map witness using the proof and raw map key.
let witness = StorageMapWitness::new(proof, [map_key]).map_err(|_err| {
Expand Down Expand Up @@ -482,10 +488,11 @@ pub fn build_minimal_foreign_account(
account_details: &AccountDetails,
) -> Result<PartialAccount, ConversionError> {
// Derive account code.
let account_code_bytes = account_details
.account_code
.as_ref()
.ok_or(ConversionError::AccountCodeMissing)?;
let account_code_bytes = account_details.account_code.as_ref().ok_or_else(|| {
ConversionError::missing_field::<proto::rpc::account_response::AccountDetails>(
"account_code",
)
})?;
let account_code = AccountCode::from_bytes(account_code_bytes)?;

// Derive partial storage. Storage maps are not required for foreign accounts.
Expand Down
Loading
Loading