Skip to content

Commit

Permalink
feat: add note type to NoteSyncRecord (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
igamigo authored Apr 10, 2024
1 parent 8aff212 commit 14a6b3e
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 60 deletions.
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
}
}
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
12 changes: 6 additions & 6 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 @@ -464,15 +464,15 @@ pub fn select_notes_since_block_by_tag_and_sender(
FROM
notes
WHERE
((tag >> 48) IN rarray(?1) OR sender IN rarray(?2)) AND
(tag IN rarray(?1) OR sender IN rarray(?2)) AND
block_num > ?3
ORDER BY
block_num ASC
LIMIT
1
) AND
-- filter the block's notes and return only the ones matching the requested tags
((tag >> 48) IN rarray(?1) OR sender IN rarray(?2));
(tag IN rarray(?1) OR sender IN rarray(?2));
",
)?;
let mut rows = stmt.query(params![Rc::new(tags), Rc::new(account_ids), block_num])?;
Expand All @@ -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
45 changes: 13 additions & 32 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 @@ -622,23 +621,14 @@ fn test_notes() {
assert!(res.is_empty());

// test no updates
let res = sql::select_notes_since_block_by_tag_and_sender(
&mut conn,
&[(tag >> 48) as u32],
&[],
block_num_1,
)
.unwrap();
let res = sql::select_notes_since_block_by_tag_and_sender(&mut conn, &[tag], &[], block_num_1)
.unwrap();
assert!(res.is_empty());

// test match
let res = sql::select_notes_since_block_by_tag_and_sender(
&mut conn,
&[(tag >> 48) as u32],
&[],
block_num_1 - 1,
)
.unwrap();
let res =
sql::select_notes_since_block_by_tag_and_sender(&mut conn, &[tag], &[], block_num_1 - 1)
.unwrap();
assert_eq!(res, vec![note.clone()]);

let block_num_2 = note.block_num + 1;
Expand All @@ -663,23 +653,14 @@ fn test_notes() {
transaction.commit().unwrap();

// only first note is returned
let res = sql::select_notes_since_block_by_tag_and_sender(
&mut conn,
&[(tag >> 48) as u32],
&[],
block_num_1 - 1,
)
.unwrap();
let res =
sql::select_notes_since_block_by_tag_and_sender(&mut conn, &[tag], &[], block_num_1 - 1)
.unwrap();
assert_eq!(res, vec![note.clone()]);

// only the second note is returned
let res = sql::select_notes_since_block_by_tag_and_sender(
&mut conn,
&[(tag >> 48) as u32],
&[],
block_num_1,
)
.unwrap();
let res = sql::select_notes_since_block_by_tag_and_sender(&mut conn, &[tag], &[], block_num_1)
.unwrap();
assert_eq!(res, vec![note2.clone()]);

// test query notes by id
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

0 comments on commit 14a6b3e

Please sign in to comment.