Skip to content

Commit

Permalink
Various refactors and fixes (#381)
Browse files Browse the repository at this point in the history
* feat: point to miden base's next and fix errors

The fixes can be separated into 2:

- compilation errors (most of them were because the old `Account::new` became `Account::from_parts` and the addition of `OutputNote::Partial` which needed to be handled in some pattern matchings.
- start storing the partial output notes (currently being discarded), this also came with small refactors.

* fix: filter out partial notes

* fix: only store in scripts table if output note record has a script

* fix: add new storage type to node config file from #365

* Update the `TransactionAuthenticator` imports

* Remove duplicate `get_falcon_signature`

* fix: update note script root hashes

* Use `&mut rng` for note creation in transactions

* Fix usage of new `InputNote`

* fix: fix after rebase

* cargo: point to node's next branch

* Change node ref

* test: avoid reruns on genesis cli tests

test will fail on retry anyways since the genesis account stays is the same every time.

* add looser sleep times to ensure blocks are included

* test: add helper to wait until notes get committed/consumed

* deps: point to miden-node's next branch and fix compilation errors

* Test transaction ordering

* make: remove dependency for node to avoid duplication on CI

* Rollback node branch

* fix: fix breaking change from base

* fix: update swap note script root

* Various refactors and simplifications

* Lints

* Lints

* Re-add parameter

* Reviews

* Fixes

* Test fix

* Remove file again

---------

Co-authored-by: Martin Fraga <[email protected]>
Co-authored-by: tomyrd <[email protected]>
  • Loading branch information
3 people authored Jun 18, 2024
1 parent afd0844 commit 36bd0db
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 139 deletions.
18 changes: 14 additions & 4 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fs::File, io::Write, path::PathBuf};
use clap::Parser;
use miden_client::config::Endpoint;

use crate::cli::config::CliConfig;
use crate::cli::{config::CliConfig, CLIENT_CONFIG_FILE_NAME};

// Init COMMAND
// ================================================================================================
Expand All @@ -24,7 +24,16 @@ pub struct InitCmd {

impl InitCmd {
pub fn execute(&self, config_file_path: PathBuf) -> Result<(), String> {
if config_file_path.exists() {
return Err(format!(
"The file \"{}\" already exists in the working directory.",
CLIENT_CONFIG_FILE_NAME
)
.to_string());
}

let mut cli_config = CliConfig::default();

if let Some(endpoint) = &self.rpc {
let endpoint = Endpoint::try_from(endpoint.as_str()).map_err(|err| err.to_string())?;

Expand All @@ -36,16 +45,17 @@ impl InitCmd {
}

let config_as_toml_string = toml::to_string_pretty(&cli_config)
.map_err(|err| format!("error formatting config: {err}"))?;
.map_err(|err| format!("Error formatting config: {err}"))?;

let mut file_handle = File::options()
.write(true)
.create_new(true)
.open(&config_file_path)
.map_err(|err| format!("error opening the file: {err}"))?;
.map_err(|err| format!("Error opening the file: {err}"))?;

file_handle
.write(config_as_toml_string.as_bytes())
.map_err(|err| format!("error writing to file: {err}"))?;
.map_err(|err| format!("Error writing to file: {err}"))?;

println!("Config file successfully created at: {:?}", config_file_path);

Expand Down
9 changes: 7 additions & 2 deletions src/client/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ impl<N: NodeRpcClient, R: FeltRng, S: Store, A: TransactionAuthenticator> Client
{
// Add the inclusion proof to the imported note
info!("Requesting MMR data for past block num {}", inclusion_details.block_num);
let block_header =
self.get_and_store_authenticated_block(inclusion_details.block_num).await?;
let mut current_partial_mmr = maybe_await!(self.build_current_partial_mmr(true))?;
let block_header = self
.get_and_store_authenticated_block(
inclusion_details.block_num,
&mut current_partial_mmr,
)
.await?;

let built_inclusion_proof = NoteInclusionProof::new(
inclusion_details.block_num,
Expand Down
50 changes: 13 additions & 37 deletions src/client/rpc/tonic_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ use miden_node_proto::{
use miden_objects::{
accounts::{Account, AccountId},
crypto::merkle::{MerklePath, MmrProof},
notes::{Note, NoteId, NoteMetadata, NoteTag, NoteType},
notes::{Note, NoteId, NoteTag},
transaction::ProvenTransaction,
utils::Deserializable,
BlockHeader, Digest, Felt,
BlockHeader, Digest,
};
use miden_tx::utils::Serializable;
use tonic::transport::Channel;
use tracing::info;

use super::{
AccountDetails, AccountUpdateSummary, CommittedNote, NodeRpcClient, NodeRpcClientEndpoint,
Expand Down Expand Up @@ -96,6 +97,8 @@ impl NodeRpcClient for TonicRpcClient {
include_mmr_proof: Some(include_mmr_proof),
};

info!("Calling GetBlockHeaderByNumber: {:?}", request);

let rpc_api = self.rpc_api().await?;
let api_response = rpc_api.get_block_header_by_number(request).await.map_err(|err| {
RpcError::RequestError(
Expand Down Expand Up @@ -149,12 +152,6 @@ impl NodeRpcClient for TonicRpcClient {
let rpc_notes = api_response.into_inner().notes;
let mut response_notes = Vec::with_capacity(rpc_notes.len());
for note in rpc_notes {
let sender_id = note
.metadata
.clone()
.and_then(|metadata| metadata.sender)
.ok_or(RpcError::ExpectedFieldMissing("Metadata.Sender".into()))?;

let inclusion_details = {
let merkle_path = note
.merkle_path
Expand All @@ -173,16 +170,12 @@ impl NodeRpcClient for TonicRpcClient {
},
// Off-chain notes do not have details
None => {
let tag =
note.metadata.ok_or(RpcError::ExpectedFieldMissing("Metadata".into()))?.tag;
let note_tag = NoteTag::from(tag).validate(NoteType::OffChain)?;
let note_metadata = NoteMetadata::new(
sender_id.try_into()?,
NoteType::OffChain,
note_tag,
Felt::default(),
)?;
let note_id: miden_objects::Digest = note
let note_metadata = note
.metadata
.ok_or(RpcError::ExpectedFieldMissing("Metadata".into()))?
.try_into()?;

let note_id: Digest = note
.note_id
.ok_or(RpcError::ExpectedFieldMissing("Notes.NoteId".into()))?
.try_into()?;
Expand Down Expand Up @@ -328,28 +321,11 @@ impl TryFrom<SyncStateResponse> for StateSyncInfo {
.ok_or(RpcError::ExpectedFieldMissing("Notes.MerklePath".into()))?
.try_into()?;

let sender_account_id = note
let metadata = note
.metadata
.clone()
.and_then(|m| m.sender)
.ok_or(RpcError::ExpectedFieldMissing("Notes.Metadata.Sender".into()))?
.ok_or(RpcError::ExpectedFieldMissing("Metadata".into()))?
.try_into()?;

let tag = note
.metadata
.clone()
.ok_or(RpcError::ExpectedFieldMissing("Notes.Metadata".into()))?
.tag;

let note_type = note
.metadata
.ok_or(RpcError::ExpectedFieldMissing("Notes.Metadata".into()))?
.note_type;

let note_type = NoteType::try_from(note_type)?;
let metadata =
NoteMetadata::new(sender_account_id, note_type, tag.into(), Default::default())?;

let committed_note =
CommittedNote::new(note_id, note.note_index, merkle_path, metadata);

Expand Down
Loading

0 comments on commit 36bd0db

Please sign in to comment.