Skip to content

Commit

Permalink
feat: add prefix length to request
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyrd committed Jul 25, 2024
1 parent 36c32f1 commit 81225ce
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 24 deletions.
10 changes: 7 additions & 3 deletions crates/proto/src/generated/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ pub struct ApplyBlockRequest {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CheckNullifiersByPrefixRequest {
/// Nullifier prefixes the client is interested in.
#[prost(uint32, repeated, tag = "1")]
pub prefixes: ::prost::alloc::vec::Vec<u32>,
/// Number of bits used for nullifier prefix.
#[prost(uint32, tag = "1")]
pub prefix_len: u32,
/// List of nullifiers to check. Each nullifier is specified by its prefix with length equal
/// to prefix_len
#[prost(uint32, repeated, tag = "2")]
pub nullifiers: ::prost::alloc::vec::Vec<u32>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down
1 change: 1 addition & 0 deletions crates/proto/src/generated/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct CheckNullifiersResponse {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CheckNullifiersByPrefixResponse {
/// List of nullifiers matching the 16-bit prefixes specified in the request.
#[prost(message, repeated, tag = "1")]
pub nullifiers: ::prost::alloc::vec::Vec<NullifierUpdate>,
}
Expand Down
7 changes: 5 additions & 2 deletions crates/rpc-proto/proto/requests.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ message ApplyBlockRequest {

// Returns a list of nullifiers recorded in the node
message CheckNullifiersByPrefixRequest {
// Nullifier prefixes the client is interested in.
repeated uint32 prefixes = 1;
// Number of bits used for nullifier prefix.
uint32 prefix_len = 1;
// List of nullifiers to check. Each nullifier is specified by its prefix with length equal
// to prefix_len
repeated uint32 nullifiers = 2;
}

message CheckNullifiersRequest {
Expand Down
1 change: 1 addition & 0 deletions crates/rpc-proto/proto/responses.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ message CheckNullifiersResponse {
}

message CheckNullifiersByPrefixResponse {
// List of nullifiers matching the 16-bit prefixes specified in the request.
repeated NullifierUpdate nullifiers = 1;
}

Expand Down
5 changes: 3 additions & 2 deletions crates/store/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,13 @@ impl Db {

pub async fn select_nullifiers_by_prefix(
&self,
prefixes: Vec<u32>,
prefix_len: u32,
nullifier_prefixes: Vec<u32>,
) -> Result<Vec<NullifierInfo>> {
self.pool
.get()
.await?
.interact(move |conn| sql::select_nullifiers_by_prefix(conn, &prefixes))
.interact(move |conn| sql::select_nullifiers_by_prefix(conn, prefix_len, &nullifier_prefixes))
.await
.map_err(|err| {
DatabaseError::InteractError(format!(
Expand Down
1 change: 1 addition & 0 deletions crates/store/src/db/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ pub fn select_nullifiers_by_block_range(

pub fn select_nullifiers_by_prefix(
conn: &mut Connection,
_prefix_len: u32,
nullifier_prefixes: &[u32],
) -> Result<Vec<NullifierInfo>> {
let nullifier_prefixes: Vec<Value> =
Expand Down
33 changes: 22 additions & 11 deletions crates/store/src/db/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,9 @@ fn test_sql_select_nullifiers_by_block_range() {
#[test]
fn test_select_nullifiers_by_prefix() {
let mut conn = create_db();

const PREFIX_LEN: u32 = 16;
// test empty table
let nullifiers = sql::select_nullifiers_by_prefix(&mut conn, &[]).unwrap();
let nullifiers = sql::select_nullifiers_by_prefix(&mut conn, PREFIX_LEN, &[]).unwrap();
assert!(nullifiers.is_empty());

// test single item
Expand All @@ -502,9 +502,12 @@ fn test_select_nullifiers_by_prefix() {
sql::insert_nullifiers_for_block(&transaction, &[nullifier1], block_number1).unwrap();
transaction.commit().unwrap();

let nullifiers =
sql::select_nullifiers_by_prefix(&mut conn, &[sql::get_nullifier_prefix(&nullifier1)])
.unwrap();
let nullifiers = sql::select_nullifiers_by_prefix(
&mut conn,
PREFIX_LEN,
&[sql::get_nullifier_prefix(&nullifier1)],
)
.unwrap();
assert_eq!(
nullifiers,
vec![NullifierInfo {
Expand All @@ -526,19 +529,25 @@ fn test_select_nullifiers_by_prefix() {
assert_eq!(nullifiers, vec![(nullifier1, block_number1), (nullifier2, block_number2)]);

// only the nullifiers matching the prefix are included
let nullifiers =
sql::select_nullifiers_by_prefix(&mut conn, &[sql::get_nullifier_prefix(&nullifier1)])
.unwrap();
let nullifiers = sql::select_nullifiers_by_prefix(
&mut conn,
PREFIX_LEN,
&[sql::get_nullifier_prefix(&nullifier1)],
)
.unwrap();
assert_eq!(
nullifiers,
vec![NullifierInfo {
nullifier: nullifier1,
block_num: block_number1
}]
);
let nullifiers =
sql::select_nullifiers_by_prefix(&mut conn, &[sql::get_nullifier_prefix(&nullifier2)])
.unwrap();
let nullifiers = sql::select_nullifiers_by_prefix(
&mut conn,
PREFIX_LEN,
&[sql::get_nullifier_prefix(&nullifier2)],
)
.unwrap();
assert_eq!(
nullifiers,
vec![NullifierInfo {
Expand All @@ -550,6 +559,7 @@ fn test_select_nullifiers_by_prefix() {
// All matching nullifiers are included
let nullifiers = sql::select_nullifiers_by_prefix(
&mut conn,
PREFIX_LEN,
&[sql::get_nullifier_prefix(&nullifier1), sql::get_nullifier_prefix(&nullifier2)],
)
.unwrap();
Expand All @@ -570,6 +580,7 @@ fn test_select_nullifiers_by_prefix() {
// If a non-matching prefix is provided, no nullifiers are returned
let nullifiers = sql::select_nullifiers_by_prefix(
&mut conn,
PREFIX_LEN,
&[sql::get_nullifier_prefix(&num_to_nullifier(3 << 48))],
)
.unwrap();
Expand Down
7 changes: 5 additions & 2 deletions crates/store/src/server/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,14 @@ impl api_server::Api for StoreApi {
request: tonic::Request<CheckNullifiersByPrefixRequest>,
) -> Result<Response<CheckNullifiersByPrefixResponse>, Status> {
let request = request.into_inner();
let prefixes = request.prefixes;

if request.prefix_len != 16 {
return Err(Status::invalid_argument("Only 16-bit prefixes are supported"));
}

let nullifiers = self
.state
.check_nullifiers_by_prefix(prefixes)
.check_nullifiers_by_prefix(request.prefix_len, request.nullifiers)
.await
.map_err(internal_error)?
.into_iter()
Expand Down
5 changes: 3 additions & 2 deletions crates/store/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,10 @@ impl State {

pub async fn check_nullifiers_by_prefix(
&self,
prefixes: Vec<u32>,
prefix_len: u32,
nullifier_prefixes: Vec<u32>,
) -> Result<Vec<NullifierInfo>, DatabaseError> {
self.db.select_nullifiers_by_prefix(prefixes).await
self.db.select_nullifiers_by_prefix(prefix_len, nullifier_prefixes).await
}

/// Generates membership proofs for each one of the `nullifiers` against the latest nullifier
Expand Down
7 changes: 5 additions & 2 deletions proto/requests.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ message ApplyBlockRequest {

// Returns a list of nullifiers recorded in the node
message CheckNullifiersByPrefixRequest {
// Nullifier prefixes the client is interested in.
repeated uint32 prefixes = 1;
// Number of bits used for nullifier prefix.
uint32 prefix_len = 1;
// List of nullifiers to check. Each nullifier is specified by its prefix with length equal
// to prefix_len
repeated uint32 nullifiers = 2;
}

message CheckNullifiersRequest {
Expand Down
1 change: 1 addition & 0 deletions proto/responses.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ message CheckNullifiersResponse {
}

message CheckNullifiersByPrefixResponse {
// List of nullifiers matching the 16-bit prefixes specified in the request.
repeated NullifierUpdate nullifiers = 1;
}

Expand Down

0 comments on commit 81225ce

Please sign in to comment.