Skip to content

Commit c2ee4b3

Browse files
authored
feat: Add get_traced_entrypoints endpoint. (#581)
2 parents f271e0c + 7e67f53 commit c2ee4b3

File tree

9 files changed

+770
-38
lines changed

9 files changed

+770
-38
lines changed

postgres.Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
FROM ghcr.io/dbsystel/postgresql-partman:15-5
66
ARG PGCRON_VERSION="1.6.2"
77
USER root
8+
RUN apt-get update && apt-get install -y wget build-essential
89
RUN cd /tmp \
910
&& wget "https://github.com/citusdata/pg_cron/archive/refs/tags/v${PGCRON_VERSION}.tar.gz" \
1011
&& tar zxf v${PGCRON_VERSION}.tar.gz \

tycho-common/src/dto.rs

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ impl ProtocolStateRequestResponse {
12841284
}
12851285
}
12861286

1287-
#[derive(Clone, PartialEq, Hash, Eq)]
1287+
#[derive(Serialize, Clone, PartialEq, Hash, Eq)]
12881288
pub struct ProtocolComponentId {
12891289
pub chain: Chain,
12901290
pub system: String,
@@ -1322,6 +1322,117 @@ impl ProtocolSystemsRequestResponse {
13221322
}
13231323
}
13241324

1325+
#[derive(Serialize, Deserialize, Debug, Default, PartialEq, ToSchema, Eq, Hash, Clone)]
1326+
pub struct TracedEntryPointRequestBody {
1327+
#[serde(default)]
1328+
pub chain: Chain,
1329+
/// Filters by protocol, required to correctly apply unconfirmed state from
1330+
/// ReorgBuffers
1331+
pub protocol_system: String,
1332+
/// Filter by component ids
1333+
pub component_ids: Option<Vec<String>>,
1334+
/// Max page size supported is 100
1335+
#[serde(default)]
1336+
pub pagination: PaginationParams,
1337+
}
1338+
1339+
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, ToSchema, Eq, Hash)]
1340+
pub struct EntryPoint {
1341+
#[schema(example = "0xEdf63cce4bA70cbE74064b7687882E71ebB0e988:getRate()")]
1342+
/// Entry point id.
1343+
pub external_id: String,
1344+
#[schema(value_type=String, example="0x8f4E8439b970363648421C692dd897Fb9c0Bd1D9")]
1345+
#[serde(with = "hex_bytes")]
1346+
/// The address of the contract to trace.
1347+
pub target: Bytes,
1348+
#[schema(example = "getRate()")]
1349+
/// The signature of the function to trace.
1350+
pub signature: String,
1351+
}
1352+
1353+
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, ToSchema, Eq, Hash)]
1354+
pub struct RPCTracerParams {
1355+
/// The caller address of the transaction, if not provided tracing uses the default value
1356+
/// for an address defined by the VM.
1357+
#[schema(value_type=Option<String>)]
1358+
#[serde(with = "hex_bytes_option", default)]
1359+
pub caller: Option<Bytes>,
1360+
/// The call data used for the tracing call, this needs to include the function selector
1361+
#[schema(value_type=String, example="0x679aefce")]
1362+
#[serde(with = "hex_bytes")]
1363+
pub calldata: Bytes,
1364+
}
1365+
1366+
impl From<models::blockchain::RPCTracerParams> for RPCTracerParams {
1367+
fn from(value: models::blockchain::RPCTracerParams) -> Self {
1368+
RPCTracerParams { caller: value.caller, calldata: value.calldata }
1369+
}
1370+
}
1371+
1372+
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
1373+
#[serde(tag = "method", rename_all = "lowercase")]
1374+
pub enum TracingParams {
1375+
/// Uses RPC calls to retrieve the called addresses and retriggers
1376+
RPCTracer(RPCTracerParams),
1377+
}
1378+
1379+
impl From<models::blockchain::TracingParams> for TracingParams {
1380+
fn from(value: models::blockchain::TracingParams) -> Self {
1381+
match value {
1382+
models::blockchain::TracingParams::RPCTracer(params) => {
1383+
TracingParams::RPCTracer(params.into())
1384+
}
1385+
}
1386+
}
1387+
}
1388+
1389+
impl From<models::blockchain::EntryPoint> for EntryPoint {
1390+
fn from(value: models::blockchain::EntryPoint) -> Self {
1391+
Self { external_id: value.external_id, target: value.target, signature: value.signature }
1392+
}
1393+
}
1394+
1395+
#[derive(Serialize, Deserialize, Debug, PartialEq, ToSchema, Eq, Clone)]
1396+
pub struct EntryPointWithTracingParams {
1397+
/// The entry point object
1398+
pub entry_point: EntryPoint,
1399+
/// The parameters used
1400+
pub params: TracingParams,
1401+
}
1402+
1403+
impl From<models::blockchain::EntryPointWithTracingParams> for EntryPointWithTracingParams {
1404+
fn from(value: models::blockchain::EntryPointWithTracingParams) -> Self {
1405+
Self { entry_point: value.entry_point.into(), params: value.params.into() }
1406+
}
1407+
}
1408+
1409+
#[derive(Serialize, Deserialize, Debug, Default, PartialEq, ToSchema, Eq, Clone)]
1410+
pub struct TracingResult {
1411+
#[schema(value_type=HashSet<(String, String)>)]
1412+
pub retriggers: HashSet<(Bytes, Bytes)>,
1413+
#[schema(value_type=HashSet<String>)]
1414+
pub called_addresses: HashSet<Bytes>,
1415+
}
1416+
1417+
impl From<models::blockchain::TracingResult> for TracingResult {
1418+
fn from(value: models::blockchain::TracingResult) -> Self {
1419+
TracingResult { retriggers: value.retriggers, called_addresses: value.called_addresses }
1420+
}
1421+
}
1422+
1423+
#[derive(Serialize, PartialEq, ToSchema, Eq, Clone)]
1424+
pub struct TracedEntryPointRequestResponse {
1425+
/// Map of protocol component id to a list of a tuple containing each entry point with its
1426+
/// tracing parameters and its corresponding tracing results.
1427+
pub traced_entry_points: HashMap<String, Vec<(EntryPointWithTracingParams, TracingResult)>>,
1428+
pub pagination: PaginationResponse,
1429+
}
1430+
1431+
pub struct AddEntrypointRequestBody {
1432+
pub entrypoints_with_tracing_data: Vec<(String, Vec<EntryPointWithTracingParams>)>,
1433+
pub chain: Chain,
1434+
}
1435+
13251436
#[cfg(test)]
13261437
mod test {
13271438
use std::str::FromStr;

tycho-common/src/models/blockchain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ pub enum BlockTag {
369369
}
370370
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
371371
pub struct EntryPoint {
372-
/// The id of the protocol component that the entry point belongs to.
372+
/// Entry point id
373373
pub external_id: String,
374374
/// The address of the contract to trace.
375375
pub target: Address,

tycho-common/src/storage.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ impl EntryPointFilter {
513513
pub trait EntryPointGateway {
514514
/// Inserts a list of entry points into the database.
515515
///
516+
/// # Arguments
516517
/// * `entry_points` - The map of component ids to their entry points to insert.
517518
///
518519
/// Note: This function ignores conflicts on inserts.
@@ -523,6 +524,7 @@ pub trait EntryPointGateway {
523524

524525
/// Inserts a list of entry points with their tracing params into the database.
525526
///
527+
/// # Arguments
526528
/// * `entry_points_params` - The map of entry points to their tracing params to insert and
527529
/// optionally a component id used for debugging only.
528530
///
@@ -534,9 +536,13 @@ pub trait EntryPointGateway {
534536

535537
/// Retrieves a map of component ids to a set of entry points from the database.
536538
///
539+
/// # Arguments
537540
/// * `filter` - The EntryPointFilter to apply to the query.
538541
/// * `pagination_params` - The pagination parameters to apply to the query, if None, all
539542
/// results are returned.
543+
///
544+
/// # Returns
545+
/// A map of component ids to a set of entry points.
540546
async fn get_entry_points(
541547
&self,
542548
filter: EntryPointFilter,
@@ -546,9 +552,13 @@ pub trait EntryPointGateway {
546552
/// Retrieves a map of component ids to a set of entry points with their tracing data from the
547553
/// database.
548554
///
555+
/// # Arguments
549556
/// * `filter` - The EntryPointFilter to apply to the query.
550557
/// * `pagination_params` - The pagination parameters to apply to the query, if None, all
551558
/// results are returned.
559+
///
560+
/// # Returns
561+
/// A map of component ids to a set of entry points with their tracing data.
552562
async fn get_entry_points_tracing_params(
553563
&self,
554564
filter: EntryPointFilter,
@@ -558,6 +568,7 @@ pub trait EntryPointGateway {
558568
/// Upserts a list of traced entry points into the database. Updates the result if it already
559569
/// exists for the same entry point and tracing params.
560570
///
571+
/// # Arguments
561572
/// * `traced_entry_points` - The list of traced entry points to upsert.
562573
async fn upsert_traced_entry_points(
563574
&self,
@@ -566,11 +577,15 @@ pub trait EntryPointGateway {
566577

567578
/// Retrieves all tracing results for a set of entry points from the database.
568579
///
580+
/// # Arguments
569581
/// * `entry_points` - The set of entry points to retrieve tracing results for.
582+
///
583+
/// # Returns
584+
/// A map of entry point ids to a map of tracing params to tracing results.
570585
async fn get_traced_entry_points(
571586
&self,
572587
entry_points: &HashSet<EntryPointId>,
573-
) -> Result<HashMap<EntryPointId, Vec<TracingResult>>, StorageError>;
588+
) -> Result<HashMap<EntryPointId, HashMap<TracingParams, TracingResult>>, StorageError>;
574589
}
575590

576591
/// Manage contracts and their state in storage.
@@ -753,6 +768,7 @@ pub trait Gateway:
753768
+ ExtractionStateGateway
754769
+ ProtocolGateway
755770
+ ContractStateGateway
771+
+ EntryPointGateway
756772
+ Send
757773
+ Sync
758774
{

tycho-indexer/src/services/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use tycho_common::{
1717
ProtocolComponentsRequestBody, ProtocolId, ProtocolStateDelta, ProtocolStateRequestBody,
1818
ProtocolStateRequestResponse, ProtocolSystemsRequestBody, ProtocolSystemsRequestResponse,
1919
ResponseAccount, ResponseProtocolState, ResponseToken, StateRequestBody,
20-
StateRequestResponse, TokensRequestBody, TokensRequestResponse, VersionParam,
20+
StateRequestResponse, TokensRequestBody, TokensRequestResponse,
21+
TracedEntryPointRequestBody, TracedEntryPointRequestResponse, VersionParam,
2122
},
2223
storage::Gateway,
2324
};
@@ -106,6 +107,7 @@ where
106107
rpc::protocol_systems,
107108
rpc::tokens,
108109
rpc::protocol_components,
110+
rpc::traced_entry_points,
109111
rpc::protocol_state,
110112
rpc::contract_state,
111113
),
@@ -126,6 +128,8 @@ where
126128
schemas(ProtocolComponentRequestResponse),
127129
schemas(ProtocolComponent),
128130
schemas(ProtocolStateRequestBody),
131+
schemas(TracedEntryPointRequestBody),
132+
schemas(TracedEntryPointRequestResponse),
129133
schemas(ProtocolStateRequestResponse),
130134
schemas(AccountUpdate),
131135
schemas(ProtocolId),
@@ -243,6 +247,10 @@ where
243247
web::resource(format!("/{}/protocol_components", self.prefix))
244248
.route(web::post().to(rpc::protocol_components::<G>)),
245249
)
250+
.service(
251+
web::resource(format!("/{}/traced_entry_points", self.prefix))
252+
.route(web::post().to(rpc::traced_entry_points::<G>)),
253+
)
246254
.service(
247255
web::resource(format!("/{}/health", self.prefix))
248256
.route(web::get().to(rpc::health)),

0 commit comments

Comments
 (0)