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

refactor: split input output note register structs #199

Merged
merged 63 commits into from
Mar 15, 2024

Conversation

mFragaBA
Copy link
Contributor

@mFragaBA mFragaBA commented Mar 4, 2024

addresses #197. This PR achieves 2 things:

I suggest checking this PR in 3 steps:

  • Check the files sqlite_store/notes.rs, store/mod.rs, store/store.sql
    • first check that all input/output note functions and structs have their own implementation and are not shared between the two
    • then see the changes of the inner structure of XNoteRecord
  • Lastly check the other files as almost all (if not all) changes are made to accomodate to the new note record interface

Summary of changes

  • It splits the InputNoteRecord into InputNoteRecord and OutputNoteRecord structs. While they store similar fields, they support slightly different fields and as discussed on Split/Refactor NoteRecord related logic to handle possible flows #197, this approach would be more flexible to refactor in the future. As a consecuence, we now have separate:
    • SerializedInputNoteData and SerializedOutputNoteData
    • SerializedInputNoteParts and SerializedOutputNoteParts
    • parse_input_note_columns and parse_output_columns functions
    • parse_input_note and parse_output_note functions
    • serialize_input_note and serialize_output_note functions
  • For both XNoteRecord structs, we replace the Note with its fields as some of them might not be known depending on the flow. As a consecuence:
    • For some fields we stored String or Vec<u8> since the actual type does not implement serde's Serialize and Deserialize traits. We added the necessary casting. It should be discussed whether we want the interface of the struct to return these String or Vec or we want to return the proper types (perhaps we can keep it as is but add From implementations).
    • The serialize_X_note, parse_X_note_column and parse_X_note functions are now slightly different since we need to adjust to the new note record fields
    • Uses of InputNoteRecord in the client and the cli had to be accordingly updated
    • I also added a Struct for the note status NoteStatus. While it's quite declarative, i don't like that some queries now do for example INSERT ... status = '"Committed"' ... though to account for serde's way of serializing the enum as a json string. Any suggestions are more than welcome

mFragaBA and others added 30 commits January 29, 2024 18:15
I still have pending polishing the changes (abstracting some deserialization and removing debugs, etc.) and we should also add some kind of validation as well as having the proper NULL / NOT NULL columns (now everything is nullable)
…onMiden/miden-client into mFragaBA-serialize-fields-as-json
There were two different kind of errors:

* When we queried for less fields than what parse_input_notes_columns expected (this happened on the function to get peding input note nullifiers)
* When we serialized the note status. Apparently serde serializes the note as `\"Variant\"` instead of `variant`.
@mFragaBA mFragaBA changed the base branch from main to mFragaBA-serialize-fields-as-json March 5, 2024 13:47
@mFragaBA mFragaBA changed the title refactor: split input output note structs refactor: split input output note register structs Mar 5, 2024
@mFragaBA mFragaBA marked this pull request as ready for review March 5, 2024 18:30
Base automatically changed from mFragaBA-serialize-fields-as-json to main March 13, 2024 15:12
@mFragaBA mFragaBA force-pushed the mFragaBA-split-input-output-note-structs branch from 9626b2f to b5574b1 Compare March 13, 2024 16:46
Copy link
Collaborator

@igamigo igamigo left a comment

Choose a reason for hiding this comment

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

LGTM! I left a couple of comments and questions. I think with the differentiation now of how input notes and output notes are treated, we might want to make sure we have tests covering the new code.

src/store/mod.rs Outdated
match value {
0 => NoteStatus::Pending,
1 => NoteStatus::Committed,
_ => NoteStatus::Consumed,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps this should be a TryFrom implementation? I think it's worth it to match only for 2 here, and error out if it's something else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! I used the existing DeserializationError for its error type

src/store/mod.rs Outdated
inclusion_proof: Option<NoteInclusionProof>,
metadata: Option<NoteMetadata>,
recipient: Digest,
status: NoteStatus,
}

impl InputNoteRecord {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since these structs are getting large with the impl blocks, should we move them to their own files?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like the idea!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

change made on 21fc72e

src/store/mod.rs Outdated
);
Ok(InputNote::new(note, proof.clone()))
}
// TODO: should we use a better Error for these two?
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think these errors look OK. An alternative could be to make it a specific variant for ClientError, since the problem might be indicate that the client has not synced.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the message within the string is OK, but the error variant InvalidOriginIndex is what seems off to me. But perhaps for now it's enough

Comment on lines +269 to +276
let note_metadata: Option<NoteMetadata> = if let Some(metadata_as_json_str) = note_metadata {
Some(
serde_json::from_str(&metadata_as_json_str)
.map_err(StoreError::JsonDataDeserializationError)?,
)
} else {
None
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this could be written a little bit more succinctly like this:

let note_metadata: Option<NoteMetadata> =  note_metadata.map(serde_json::from_str...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did that at first but the problem is that I wouldn't be able to use the ? to propagate the error

Comment on lines 316 to 322
// FIXME: This removal is to accomodate a problem with how the node constructs paths where
// they are constructed using note ID instead of authentication hash, so for now we remove the first
// node here.
//
// Note: once removed we can also stop creating a new `NoteInclusionProof`
//
// See: https://github.com/0xPolygonMiden/miden-node/blob/main/store/src/state.rs#L274
Copy link
Collaborator

Choose a reason for hiding this comment

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

@bobbinth we have been dragging this FIXME for a while now, is this getting tracked somewhere? I recall it being mentioned a couple of times but I just quickly searched for it in related issues and did not find anything

Copy link
Contributor

Choose a reason for hiding this comment

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

It is supposed to be a part of 0xPolygonMiden/miden-base#464 - but I haven't been able to finish it yet.

src/store/sqlite_store/notes.rs Show resolved Hide resolved
src/store/sqlite_store/store.sql Show resolved Hide resolved
@bobbinth bobbinth changed the base branch from main to next March 15, 2024 18:36
Copy link
Collaborator

@igamigo igamigo left a comment

Choose a reason for hiding this comment

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

LGTM!

@igamigo igamigo merged commit f05a3f8 into next Mar 15, 2024
6 checks passed
@igamigo igamigo deleted the mFragaBA-split-input-output-note-structs branch March 15, 2024 20:27
bobbinth pushed a commit that referenced this pull request Apr 13, 2024
* split input notes

* make enum for notes tables

* rename recipients variable and add more NoteTable uses

* run fmt

* change to_string for to_hex

* correct nullable fields and reorder fields

* remove unnecesarry note filter

* remove unnecessary commit_height field

* move related rows into metadata row for notes

* remove dbg statements

* switch to_string for to_hex

* Revert "move related rows into metadata row for notes"

This reverts commit bf46c0a.

* restore filtering by pending notes

* store grouped json fields

* add json fields with serialization/deserialization

I still have pending polishing the changes (abstracting some deserialization and removing debugs, etc.) and we should also add some kind of validation as well as having the proper NULL / NOT NULL columns (now everything is nullable)

* remove dbg calls and set nullable/non-nullable cols accordingly

* apply clippy suggestion

* add helper functions to work with json values returned as strings

* add documentation for handling json with sqlite

* fix incorrect function use to fetch output notes

* ignore doc tests

* add json validity constraints

* use structs to represent json columns

* remove dbg statements

* use propper serialization

* use existing NoteMetadata instead of NoteRecordMetadata

* also remove NoteRecordInclusionProof and use NoteInclusionProof instead

* add implementations for output notes

* change input note record struct

TODO: fix tests

* Fix test failures

There were two different kind of errors:

* When we queried for less fields than what parse_input_notes_columns expected (this happened on the function to get peding input note nullifiers)
* When we serialized the note status. Apparently serde serializes the note as `\"Variant\"` instead of `variant`.

* update doc comments

* cleanup struct

* remove dbg statements

* fix serialization for output notes

* change from to tryfrom for notestatus

* move note record structs to its own module

* remove todo comment

* remove quotes from note status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants