Skip to content
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

Add note type to NoteSyncRecord #311

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion node/src/commands/genesis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn create_accounts(
let mut final_accounts = Vec::new();

for account in accounts {
// build account data from account inputs
// build offchain account data from account inputs
let mut account_data = match account {
AccountInput::BasicWallet(inputs) => {
print!("Creating basic wallet account...");
Expand Down
7 changes: 4 additions & 3 deletions proto/proto/note.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ message Note {
uint32 note_index = 2;
digest.Digest note_id = 3;
account.AccountId sender = 4;
fixed64 tag = 5;
fixed32 tag = 5;
merkle.MerklePath merkle_path = 7;
// This field will be present when the note is on-chain.
// details contain the `Note` in a serialized format.
Expand All @@ -21,7 +21,8 @@ message NoteSyncRecord {
uint32 note_index = 1;
digest.Digest note_id = 2;
account.AccountId sender = 3;
fixed64 tag = 4;
fixed32 tag = 4;
fixed32 note_type = 5;
merkle.MerklePath merkle_path = 6;
}

Expand All @@ -30,7 +31,7 @@ message NoteCreated {
uint32 note_index = 2;
digest.Digest note_id = 3;
account.AccountId sender = 4;
fixed64 tag = 5;
fixed32 tag = 5;
// This field will be present when the note is on-chain.
// details contain the `Note` in a serialized format.
optional bytes details = 6;
Expand Down
14 changes: 8 additions & 6 deletions proto/src/generated/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub struct Note {
pub note_id: ::core::option::Option<super::digest::Digest>,
#[prost(message, optional, tag = "4")]
pub sender: ::core::option::Option<super::account::AccountId>,
#[prost(fixed64, tag = "5")]
pub tag: u64,
#[prost(fixed32, tag = "5")]
pub tag: u32,
#[prost(message, optional, tag = "7")]
pub merkle_path: ::core::option::Option<super::merkle::MerklePath>,
/// This field will be present when the note is on-chain.
Expand All @@ -30,8 +30,10 @@ pub struct NoteSyncRecord {
pub note_id: ::core::option::Option<super::digest::Digest>,
#[prost(message, optional, tag = "3")]
pub sender: ::core::option::Option<super::account::AccountId>,
#[prost(fixed64, tag = "4")]
pub tag: u64,
#[prost(fixed32, tag = "4")]
pub tag: u32,
#[prost(fixed32, tag = "5")]
pub note_type: u32,
#[prost(message, optional, tag = "6")]
pub merkle_path: ::core::option::Option<super::merkle::MerklePath>,
}
Expand All @@ -47,8 +49,8 @@ pub struct NoteCreated {
pub note_id: ::core::option::Option<super::digest::Digest>,
#[prost(message, optional, tag = "4")]
pub sender: ::core::option::Option<super::account::AccountId>,
#[prost(fixed64, tag = "5")]
pub tag: u64,
#[prost(fixed32, tag = "5")]
pub tag: u32,
/// This field will be present when the note is on-chain.
/// details contain the `Note` in a serialized format.
#[prost(bytes = "vec", optional, tag = "6")]
Expand Down
15 changes: 12 additions & 3 deletions store/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use miden_node_proto::domain::accounts::{AccountInfo, AccountSummary, AccountUpd
use miden_objects::{
block::BlockNoteTree,
crypto::{hash::rpo::RpoDigest, merkle::MerklePath, utils::Deserializable},
notes::{NoteId, Nullifier},
notes::{NoteId, NoteType, Nullifier},
BlockHeader, GENESIS_BLOCK,
};
use rusqlite::vtab::array;
Expand Down Expand Up @@ -44,16 +44,25 @@ pub struct NoteCreated {
pub note_index: u32,
pub note_id: RpoDigest,
pub sender: AccountId,
pub tag: u64,
pub tag: u32,
pub details: Option<Vec<u8>>,
}

impl NoteCreated {
/// Returns the absolute position on the note tree based on the batch index
/// and local-to-the-subtree index
/// and local-to-the-subtree index.
pub fn absolute_note_index(&self) -> u32 {
BlockNoteTree::note_index(self.batch_index as usize, self.note_index as usize) as u32
}

// Returns the note type based on available details.
pub fn note_type(&self) -> NoteType {
if self.details.is_some() {
NoteType::Public
} else {
NoteType::OffChain
}
}
bobbinth marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
8 changes: 4 additions & 4 deletions store/src/db/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ pub fn select_notes(conn: &mut Connection) -> Result<Vec<Note>> {
note_index: row.get(2)?,
note_id,
sender: column_value_as_u64(row, 4)?,
tag: column_value_as_u64(row, 5)?,
tag: row.get(5)?,
details,
},
merkle_path,
Expand Down Expand Up @@ -413,7 +413,7 @@ pub fn insert_notes(
note.note_created.note_index,
note.note_created.note_id.to_bytes(),
u64_to_value(note.note_created.sender),
u64_to_value(note.note_created.tag),
note.note_created.tag,
note.merkle_path.to_bytes(),
details
])?;
Expand Down Expand Up @@ -485,7 +485,7 @@ pub fn select_notes_since_block_by_tag_and_sender(
let note_id_data = row.get_ref(3)?.as_blob()?;
let note_id = RpoDigest::read_from_bytes(note_id_data)?;
let sender = column_value_as_u64(row, 4)?;
let tag = column_value_as_u64(row, 5)?;
let tag = row.get(5)?;
let merkle_path_data = row.get_ref(6)?.as_blob()?;
let merkle_path = MerklePath::read_from_bytes(merkle_path_data)?;
let details_data = row.get_ref(7)?.as_blob_or_null()?;
Expand Down Expand Up @@ -558,7 +558,7 @@ pub fn select_notes_by_id(
details,
note_id: note_id.into(),
sender: column_value_as_u64(row, 4)?,
tag: column_value_as_u64(row, 5)?,
tag: row.get(5)?,
},
merkle_path,
})
Expand Down
15 changes: 7 additions & 8 deletions store/src/db/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn test_sql_select_notes() {
note_index: i,
note_id: num_to_rpo_digest(i as u64),
sender: i as u64,
tag: i as u64,
tag: i,
details: Some(vec![1, 2, 3]),
},
merkle_path: MerklePath::new(vec![]),
Expand Down Expand Up @@ -590,10 +590,9 @@ fn test_notes() {
let batch_index = 0u32;
let note_index = 2u32;
let note_id = num_to_rpo_digest(3);
let tag = 5u64;
let tag = 5u32;
let sender = AccountId::new_unchecked(Felt::new(ACCOUNT_ID_OFF_CHAIN_SENDER));
let note_metadata =
NoteMetadata::new(sender, NoteType::OffChain, (tag as u32).into(), ZERO).unwrap();
let note_metadata = NoteMetadata::new(sender, NoteType::OffChain, tag.into(), ZERO).unwrap();

let values = [(batch_index as usize, note_index as usize, (note_id, note_metadata))];
let notes_db = BlockNoteTree::with_entries(values.iter().cloned()).unwrap();
Expand Down Expand Up @@ -624,7 +623,7 @@ fn test_notes() {
// test no updates
let res = sql::select_notes_since_block_by_tag_and_sender(
&mut conn,
&[(tag >> 48) as u32],
&[(tag >> 16)],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need these shifts any more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep thinking we are persisting u16s. Fixed.

&[],
block_num_1,
)
Expand All @@ -634,7 +633,7 @@ fn test_notes() {
// test match
let res = sql::select_notes_since_block_by_tag_and_sender(
&mut conn,
&[(tag >> 48) as u32],
&[(tag >> 16)],
&[],
block_num_1 - 1,
)
Expand Down Expand Up @@ -665,7 +664,7 @@ fn test_notes() {
// only first note is returned
let res = sql::select_notes_since_block_by_tag_and_sender(
&mut conn,
&[(tag >> 48) as u32],
&[(tag >> 16)],
&[],
block_num_1 - 1,
)
Expand All @@ -675,7 +674,7 @@ fn test_notes() {
// only the second note is returned
let res = sql::select_notes_since_block_by_tag_and_sender(
&mut conn,
&[(tag >> 48) as u32],
&[(tag >> 16)],
&[],
block_num_1,
)
Expand Down
1 change: 1 addition & 0 deletions store/src/server/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl api_server::Api for StoreApi {
.into_iter()
.map(|note| NoteSyncRecord {
note_index: note.note_created.absolute_note_index(),
note_type: note.note_created.note_type() as u32,
note_id: Some(note.note_created.note_id.into()),
sender: Some(note.note_created.sender.into()),
tag: note.note_created.tag,
Expand Down
12 changes: 3 additions & 9 deletions store/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use miden_objects::{
merkle::{LeafIndex, Mmr, MmrDelta, MmrPeaks, SimpleSmt, SmtProof, ValuePath},
},
notes::{NoteId, NoteMetadata, NoteType, Nullifier},
AccountError, BlockHeader, NoteError, ACCOUNT_TREE_DEPTH, ZERO,
AccountError, BlockHeader, ACCOUNT_TREE_DEPTH, ZERO,
};
use tokio::{
sync::{oneshot, Mutex, RwLock},
Expand Down Expand Up @@ -502,14 +502,8 @@ pub fn build_note_tree(notes: &[NoteCreated]) -> Result<BlockNoteTree, ApplyBloc

for note in notes.iter() {
let note_type = NoteType::OffChain; // TODO: Provide correct note type
let note_metadata = NoteMetadata::new(
note.sender.try_into()?,
note_type,
note.tag
.try_into()
.map_err(|_| NoteError::InconsistentNoteTag(note_type, note.tag))?,
ZERO,
)?;
let note_metadata =
NoteMetadata::new(note.sender.try_into()?, note_type, note.tag.into(), ZERO)?;
entries.push((
note.batch_index as usize,
note.note_index as usize,
Expand Down
Loading