Skip to content

Commit

Permalink
Add queries by payload hash
Browse files Browse the repository at this point in the history
  • Loading branch information
jbearer committed Jun 3, 2024
1 parent 515fe23 commit b9fc39c
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions sequencer/src/bin/nasty-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,43 +339,69 @@ trait Queryable: DeserializeOwned + Debug + Eq {
/// URL segment used to indicate that we want to fetch this resource by block hash.
const HASH_URL_SEGMENT: &'static str;

/// URL segment used to indicate that we want to fetch this resource by payload hash.
///
/// This may be none if the resource does not support fetching by payload hash.
const PAYLOAD_HASH_URL_SEGMENT: Option<&'static str>;

fn hash(&self) -> String;
fn payload_hash(&self) -> String;
}

impl Queryable for BlockQueryData<SeqTypes> {
const RESOURCE: Resource = Resource::Blocks;
const HASH_URL_SEGMENT: &'static str = "hash";
const PAYLOAD_HASH_URL_SEGMENT: Option<&'static str> = Some("payload-hash");

fn hash(&self) -> String {
self.hash().to_string()
}

fn payload_hash(&self) -> String {
self.payload_hash().to_string()
}
}

impl Queryable for LeafQueryData<SeqTypes> {
const RESOURCE: Resource = Resource::Leaves;
const HASH_URL_SEGMENT: &'static str = "hash";
const PAYLOAD_HASH_URL_SEGMENT: Option<&'static str> = None;

fn hash(&self) -> String {
self.hash().to_string()
}

fn payload_hash(&self) -> String {
self.payload_hash().to_string()
}
}

impl Queryable for Header {
const RESOURCE: Resource = Resource::Headers;
const HASH_URL_SEGMENT: &'static str = "hash";
const PAYLOAD_HASH_URL_SEGMENT: Option<&'static str> = Some("payload-hash");

fn hash(&self) -> String {
self.commit().to_string()
}

fn payload_hash(&self) -> String {
self.payload_commitment.to_string()
}
}

impl Queryable for PayloadQueryData<SeqTypes> {
const RESOURCE: Resource = Resource::Payloads;
const HASH_URL_SEGMENT: &'static str = "block-hash";
const PAYLOAD_HASH_URL_SEGMENT: Option<&'static str> = Some("hash");

fn hash(&self) -> String {
self.block_hash().to_string()
}

fn payload_hash(&self) -> String {
self.hash().to_string()
}
}

type Connection<T> = socket::Connection<T, socket::Unsupported, ClientError, SequencerVersion>;
Expand Down Expand Up @@ -535,6 +561,37 @@ impl<T: Queryable> ResourceManager<T> {
)
);

// Query by payload hash and check consistency.
if let Some(segment) = T::PAYLOAD_HASH_URL_SEGMENT {
let payload_hash = obj.payload_hash();
let by_payload_hash = self
.retry(
info_span!(
"query by payload hash",
resource = Self::singular(),
at,
payload_hash
),
|| async {
self.get::<T>(format!(
"availability/{}/{segment}/{payload_hash}",
Self::singular(),
))
.await
},
)
.await?;
// We might not get the exact object this time, due to non-uniqueness of payloads, but we
// should get an object with the same payload.
ensure!(
payload_hash == by_payload_hash.payload_hash(),
format!(
"query for {} {at} by payload hash {payload_hash} is not consistent",
Self::singular()
)
);
}

self.metrics.query_actions[&T::RESOURCE].add(1);
Ok(())
}
Expand Down

0 comments on commit b9fc39c

Please sign in to comment.