Skip to content

Commit

Permalink
Eth-bytecode-db - search event signatures (#698)
Browse files Browse the repository at this point in the history
* Add event descriptions search

* Insertion of the event signatures into database implementation

* Add tests

* Add BatchSearchEventDescriptions endpoint definition. Add test cases for batch-search

* Add BatchSearchEventDescriptions implementation
  • Loading branch information
rimrakhimov authored Dec 20, 2023
1 parent d912133 commit ff9c358
Show file tree
Hide file tree
Showing 30 changed files with 1,208 additions and 87 deletions.
437 changes: 399 additions & 38 deletions eth-bytecode-db/Cargo.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ config_version: 3

http:
rules:
#################### SolidityVerifier ####################
#################### Database ####################

- selector: blockscout.ethBytecodeDb.v2.Database.SearchSources
post: /api/v2/bytecodes/sources:search
Expand All @@ -17,6 +17,14 @@ http:
post: /api/v2/bytecodes/sources:search-all
body: "*"

- selector: blockscout.ethBytecodeDb.v2.Database.SearchEventDescriptions
post: /api/v2/event-descriptions:search
body: "*"

- selector: blockscout.ethBytecodeDb.v2.Database.BatchSearchEventDescriptions
post: /api/v2/event-descriptions:batch-search
body: "*"

#################### SolidityVerifier ####################

- selector: blockscout.ethBytecodeDb.v2.SolidityVerifier.VerifyMultiPart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ service Database {
rpc SearchSourcifySources(SearchSourcifySourcesRequest) returns (SearchSourcesResponse) {}

rpc SearchAllSources(SearchAllSourcesRequest) returns (SearchAllSourcesResponse) {}

rpc SearchEventDescriptions(SearchEventDescriptionsRequest) returns (SearchEventDescriptionsResponse) {}

rpc BatchSearchEventDescriptions(BatchSearchEventDescriptionsRequest) returns (BatchSearchEventDescriptionsResponse) {}
}

service SolidityVerifier {
Expand Down Expand Up @@ -231,6 +235,27 @@ message SearchAllSourcesResponse {
repeated Source sourcify_sources = 2;
}

message SearchEventDescriptionsRequest {
/// For non-anonymous events, this is a bytes32 value
/// containing the keccak256 hash of the event signature,
/// as used in the default topic.
string selector = 1;
}

message SearchEventDescriptionsResponse {
repeated EventDescription event_descriptions = 1;
}

message BatchSearchEventDescriptionsRequest {
// The selectors of the events to look for.
// A maximum of 100 selectors can be retrieved in a batch.
repeated string selectors = 1;
}

message BatchSearchEventDescriptionsResponse {
repeated SearchEventDescriptionsResponse responses = 1;
}

message VerifySourcifyRequest {
/// Address of the contract to be verified
string address = 1;
Expand Down Expand Up @@ -260,3 +285,12 @@ message ListCompilerVersionsResponse {
/// Compiler versions available
repeated string compiler_versions = 1;
}

message EventDescription {
/// Will always be "event"
string type = 1;
/// The name of the event
string name = 2;
/// Json encoded array of objects each describing one of the event arguments
string inputs = 3;
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,46 @@ paths:
$ref: '#/definitions/v2SearchSourcifySourcesRequest'
tags:
- Database
/api/v2/event-descriptions:batch-search:
post:
operationId: Database_BatchSearchEventDescriptions
responses:
"200":
description: A successful response.
schema:
$ref: '#/definitions/v2BatchSearchEventDescriptionsResponse'
default:
description: An unexpected error response.
schema:
$ref: '#/definitions/googlerpcStatus'
parameters:
- name: body
in: body
required: true
schema:
$ref: '#/definitions/v2BatchSearchEventDescriptionsRequest'
tags:
- Database
/api/v2/event-descriptions:search:
post:
operationId: Database_SearchEventDescriptions
responses:
"200":
description: A successful response.
schema:
$ref: '#/definitions/v2SearchEventDescriptionsResponse'
default:
description: An unexpected error response.
schema:
$ref: '#/definitions/googlerpcStatus'
parameters:
- name: body
in: body
required: true
schema:
$ref: '#/definitions/v2SearchEventDescriptionsRequest'
tags:
- Database
/api/v2/verifier/solidity/sources:verify-multi-part:
post:
operationId: SolidityVerifier_VerifyMultiPart
Expand Down Expand Up @@ -289,13 +329,43 @@ definitions:
'@type':
type: string
additionalProperties: {}
v2BatchSearchEventDescriptionsRequest:
type: object
properties:
selectors:
type: array
items:
type: string
description: |-
The selectors of the events to look for.
A maximum of 100 selectors can be retrieved in a batch.
v2BatchSearchEventDescriptionsResponse:
type: object
properties:
responses:
type: array
items:
type: object
$ref: '#/definitions/v2SearchEventDescriptionsResponse'
v2BytecodeType:
type: string
enum:
- BYTECODE_TYPE_UNSPECIFIED
- CREATION_INPUT
- DEPLOYED_BYTECODE
default: BYTECODE_TYPE_UNSPECIFIED
v2EventDescription:
type: object
properties:
type:
type: string
title: / Will always be "event"
name:
type: string
title: / The name of the event
inputs:
type: string
title: / Json encoded array of objects each describing one of the event arguments
v2HealthCheckResponse:
type: object
properties:
Expand Down Expand Up @@ -337,6 +407,23 @@ definitions:
items:
type: object
$ref: '#/definitions/v2Source'
v2SearchEventDescriptionsRequest:
type: object
properties:
selector:
type: string
description: |-
/ For non-anonymous events, this is a bytes32 value
/ containing the keccak256 hash of the event signature,
/ as used in the default topic.
v2SearchEventDescriptionsResponse:
type: object
properties:
eventDescriptions:
type: array
items:
type: object
$ref: '#/definitions/v2EventDescription'
v2SearchSourcesRequest:
type: object
properties:
Expand Down
12 changes: 7 additions & 5 deletions eth-bytecode-db/eth-bytecode-db-server/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ pub use eth_bytecode_db_proto::blockscout::eth_bytecode_db::v2::{
database_actix, database_server, health_actix, health_check_response, health_server,
solidity_verifier_actix, solidity_verifier_server, source, sourcify_verifier_actix,
sourcify_verifier_server, verify_response, vyper_verifier_actix, vyper_verifier_server,
BytecodeType, HealthCheckRequest, HealthCheckResponse, ListCompilerVersionsRequest,
BatchSearchEventDescriptionsRequest, BatchSearchEventDescriptionsResponse, BytecodeType,
EventDescription, HealthCheckRequest, HealthCheckResponse, ListCompilerVersionsRequest,
ListCompilerVersionsResponse, SearchAllSourcesRequest, SearchAllSourcesResponse,
SearchSourcesRequest, SearchSourcesResponse, SearchSourcifySourcesRequest, Source,
VerificationMetadata, VerifyFromEtherscanSourcifyRequest, VerifyResponse,
VerifySolidityMultiPartRequest, VerifySolidityStandardJsonRequest, VerifySourcifyRequest,
VerifyVyperMultiPartRequest, VerifyVyperStandardJsonRequest,
SearchEventDescriptionsRequest, SearchEventDescriptionsResponse, SearchSourcesRequest,
SearchSourcesResponse, SearchSourcifySourcesRequest, Source, VerificationMetadata,
VerifyFromEtherscanSourcifyRequest, VerifyResponse, VerifySolidityMultiPartRequest,
VerifySolidityStandardJsonRequest, VerifySourcifyRequest, VerifyVyperMultiPartRequest,
VerifyVyperStandardJsonRequest,
};
69 changes: 67 additions & 2 deletions eth-bytecode-db/eth-bytecode-db-server/src/services/database.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::{
proto::{
database_server::Database, BytecodeType, SearchAllSourcesRequest, SearchAllSourcesResponse,
database_server::Database, BatchSearchEventDescriptionsRequest,
BatchSearchEventDescriptionsResponse, BytecodeType, SearchAllSourcesRequest,
SearchAllSourcesResponse, SearchEventDescriptionsRequest, SearchEventDescriptionsResponse,
SearchSourcesRequest, SearchSourcesResponse, SearchSourcifySourcesRequest, Source,
VerifyResponse,
},
types::{BytecodeTypeWrapper, SourceWrapper, VerifyResponseWrapper},
types::{BytecodeTypeWrapper, EventDescriptionWrapper, SourceWrapper, VerifyResponseWrapper},
};
use amplify::Wrapper;
use async_trait::async_trait;
Expand All @@ -14,6 +16,7 @@ use eth_bytecode_db::{
verification,
verification::sourcify_from_etherscan,
};
use ethers::types::H256;
use std::str::FromStr;
use tracing::instrument;

Expand Down Expand Up @@ -133,6 +136,57 @@ impl Database for DatabaseService {

Ok(tonic::Response::new(response))
}

async fn search_event_descriptions(
&self,
request: tonic::Request<SearchEventDescriptionsRequest>,
) -> Result<tonic::Response<SearchEventDescriptionsResponse>, tonic::Status> {
let request = request.into_inner();
let selector = H256::from_str(&request.selector).map_err(|err| {
tonic::Status::invalid_argument(format!("selector is not valid: {err}"))
})?;

let event_descriptions =
search::find_event_descriptions(self.client.db_client.as_ref(), vec![selector])
.await
.remove(0)
.map_err(|err| tonic::Status::internal(err.to_string()))?;

Ok(tonic::Response::new(event_descriptions_to_search_response(
event_descriptions,
)))
}

async fn batch_search_event_descriptions(
&self,
request: tonic::Request<BatchSearchEventDescriptionsRequest>,
) -> Result<tonic::Response<BatchSearchEventDescriptionsResponse>, tonic::Status> {
const BATCH_LIMIT: usize = 100;

let request = request.into_inner();
let selectors = request
.selectors
.into_iter()
.take(BATCH_LIMIT)
.map(|selector| {
H256::from_str(&selector).map_err(|err| {
tonic::Status::invalid_argument(format!("selector is not valid: {err}"))
})
})
.collect::<Result<Vec<_>, _>>()?;

let responses: Vec<_> =
search::find_event_descriptions(self.client.db_client.as_ref(), selectors)
.await
.into_iter()
.map(|event_descriptions| event_descriptions.unwrap_or_default())
.map(event_descriptions_to_search_response)
.collect();

Ok(tonic::Response::new(BatchSearchEventDescriptionsResponse {
responses,
}))
}
}

impl DatabaseService {
Expand Down Expand Up @@ -232,3 +286,14 @@ fn process_sourcify_error(
}
}
}

fn event_descriptions_to_search_response(
event_descriptions: Vec<eth_bytecode_db::search::EventDescription>,
) -> SearchEventDescriptionsResponse {
SearchEventDescriptionsResponse {
event_descriptions: event_descriptions
.into_iter()
.map(|event| EventDescriptionWrapper::from(event).into())
.collect(),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::proto;
use amplify::{From, Wrapper};
use eth_bytecode_db::search;

#[derive(Wrapper, From, Clone, Debug, PartialEq)]
pub struct EventDescriptionWrapper(proto::EventDescription);

impl From<search::EventDescription> for EventDescriptionWrapper {
fn from(value: search::EventDescription) -> Self {
EventDescriptionWrapper(proto::EventDescription {
r#type: "event".to_string(),
name: value.name,
inputs: value.inputs.to_string(),
})
}
}
2 changes: 2 additions & 0 deletions eth-bytecode-db/eth-bytecode-db-server/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod enums;
mod event_description;
mod source;
mod verification_metadata;
mod verify_response;

pub use enums::{BytecodeTypeWrapper, MatchTypeWrapper, SourceTypeWrapper};
pub use event_description::EventDescriptionWrapper;
pub use source::SourceWrapper;
pub use verification_metadata::VerificationMetadataWrapper;
pub use verify_response::VerifyResponseWrapper;
Loading

0 comments on commit ff9c358

Please sign in to comment.