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

Implement get_notes_by_id endpoint #298

Merged
merged 14 commits into from
Apr 8, 2024
Merged

Implement get_notes_by_id endpoint #298

merged 14 commits into from
Apr 8, 2024

Conversation

phklive
Copy link
Contributor

@phklive phklive commented Apr 4, 2024

In this PR I propose the addition of the get_notes_by_id endpoint on the RPC and Store of the Miden node

Closes: #286

Now making this request to the RPC:

{
 "note_ids": [
   {
     "d0": "405316859436263090",
     "d1": "10067701279746667862",
     "d2": "2555050322175287978",
     "d3": "14994459379969491785"
   },
   {
     "d0": "14343633916517334960",
     "d1": "4263043073880214079",
     "d2": "426630156430625624",
     "d3": "13403889746530043130"
   }
 ]
}

Returns this response:

{
  "notes": [
    {
      "block_num": 16,
      "note_index": 0,
      "note_id": {
        "d0": "405316859436263090",
        "d1": "10067701279746667862",
        "d2": "2555050322175287978",
        "d3": "14994459379969491785"
      },
      "sender": {
        "id": "11177765675888626144"
      },
      "tag": "6895328587244957060",
      "merkle_path": {
        "siblings": [
          {
            "d0": "6895328587244957060",
            "d1": "11177765675888626144",
            "d2": "0",
            "d3": "0"
          },
          // ... (omitted for brevity)
        ]
      }
    },
    {
      "block_num": 1186,
      "note_index": 0,
      "note_id": {
        "d0": "14343633916517334960",
        "d1": "4263043073880214079",
        "d2": "426630156430625624",
        "d3": "13403889746530043130"
      },
      "sender": {
        "id": "10737277274808842422"
      },
      "tag": "5202715297868241916",
      "merkle_path": {
        "siblings": [
          {
            "d0": "5202715297868241916",
            "d1": "10737277274808842422",
            "d2": "0",
            "d3": "0"
          },
          // ... (omitted for brevity)
        ]
      }
    }
  ]
}

Proving the functionality of the endpoint.

@phklive phklive marked this pull request as ready for review April 4, 2024 15:02
@phklive phklive requested review from hackaugusto, polydez and bobbinth and removed request for hackaugusto April 4, 2024 15:02
@igamigo igamigo linked an issue Apr 4, 2024 that may be closed by this pull request
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'm guessing the changes to the storage (for supporting on-chain note data) will also imply a couple of changes in the endpoint.

store/README.md Outdated Show resolved Hide resolved
Comment on lines +403 to +408
/// Select Note's matching the NoteId using the given [Connection].
///
/// # Returns
///
/// - Empty vector if no matching `note`.
/// - Otherwise, notes which `note_hash` matches the `NoteId` as bytes.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this include a description of how off-chain and on-chain notes are different in terms of the response? (or perhaps when the storage changes are made)


let placeholders = note_ids.iter().map(|_| "?").collect::<Vec<&str>>().join(", ");

let query = &format!("SELECT * FROM notes WHERE note_hash IN ({})", placeholders);
Copy link
Collaborator

Choose a reason for hiding this comment

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

The note_hash column name should likely be updated to note_id but not for this set of changes

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of formatting the SQL statement, this should use the rarray extension. Here is an example:

let mut stmt = conn.prepare(
"
SELECT
account_id, account_hash, block_num
FROM
accounts
WHERE
block_num > ?1 AND
block_num <= ?2 AND
account_id IN rarray(?3)
ORDER BY
block_num ASC
",
)?;

In general I'm against formatting SQL statements, for two reasons:

  • It is an easy way to get into a SQL injection vulnerability
  • It makes it harder to have caches of prepared statements

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed it using the rarray method.

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 note_hash column name should likely be updated to note_id but not for this set of changes

Opening an issue here: #301

@@ -99,6 +99,11 @@ message GetTransactionInputsResponse {

message SubmitProvenTransactionResponse {}

message GetNotesByIdResponse {
// Lists Note's returned by the database
repeated note.Note notes = 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems these are missing the details (i.e. the note's script/inputs/etc.):

message Note {
fixed32 block_num = 1;
uint32 note_index = 2;
digest.Digest note_id = 3;
account.AccountId sender = 4;
fixed64 tag = 5;
merkle.MerklePath merkle_path = 7;
}

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 don't think that this message is missing a details field.

This message represents getting a list of notes. Each note will have their own details field indeed. But the GetNoteById message should not return details but only a list of notes.

If you want to see the Note message with a details field, then see this PR when I make the modification to the db: #300


let placeholders = note_ids.iter().map(|_| "?").collect::<Vec<&str>>().join(", ");

let query = &format!("SELECT * FROM notes WHERE note_hash IN ({})", placeholders);
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of formatting the SQL statement, this should use the rarray extension. Here is an example:

let mut stmt = conn.prepare(
"
SELECT
account_id, account_hash, block_num
FROM
accounts
WHERE
block_num > ?1 AND
block_num <= ?2 AND
account_id IN rarray(?3)
ORDER BY
block_num ASC
",
)?;

In general I'm against formatting SQL statements, for two reasons:

  • It is an easy way to get into a SQL injection vulnerability
  • It makes it harder to have caches of prepared statements

proto/proto/rpc.proto Outdated Show resolved Hide resolved
@Dominik1999
Copy link
Contributor

merge?

@phklive
Copy link
Contributor Author

phklive commented Apr 5, 2024

merge?

Hey, yes soon. Still working on making sure that everything works correctly and addressing the small nits from the other teammates.

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left a couple of doc-related comments inline. Other than that, we should also resolve the merge conflicts (hopefully, shouldn't be too difficult) and then we can merge.

store/README.md Outdated Show resolved Hide resolved
rpc/README.md Outdated Show resolved Hide resolved
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

All looks good! Thank you!

@phklive phklive merged commit f9264ac into next Apr 8, 2024
5 checks passed
@phklive phklive deleted the phklive-get-notes-by-id branch April 8, 2024 19:51
bobbinth pushed a commit that referenced this pull request Apr 12, 2024
* Getting notes by id works

* lint and test

* Improved comments + Added conversion from digest::Digest to NoteId

* Added rpc validation for NoteId's

* Added documentation to Readmes

* Lint

* Fixed ci problems

* Added test for endpoint + refactored sql query + improved documentation

* Fixed lint

* Order rpc and store endpoints alphabitically

* Improved documentation with gh comments
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.

Implement GetNotesById endpoint
5 participants