Skip to content
This repository was archived by the owner on May 11, 2026. It is now read-only.

Commit cdc191a

Browse files
authored
feat: add a cutoff date for aggregated topic processing (#116)
* feat: add dry run mode to skip contract calls Signed-off-by: Tomás Migone <tomas@edgeandnode.com> * ci: formatter was sad Signed-off-by: Tomás Migone <tomas@edgeandnode.com> * fix: revert previous hacky fix to ignore pre-horizon data Signed-off-by: Tomás Migone <tomas@edgeandnode.com> * feat: add cutoff date for aggregated topic Signed-off-by: Tomás Migone <tomas@edgeandnode.com> --------- Signed-off-by: Tomás Migone <tomas@edgeandnode.com>
1 parent 24a3724 commit cdc191a

3 files changed

Lines changed: 23 additions & 18 deletions

File tree

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ pub struct Kafka {
4747
pub config: BTreeMap<String, String>,
4848
pub realtime_topic: String,
4949
pub aggregated_topic: Option<String>,
50+
/// Cutoff timestamp (unix milliseconds) for aggregated topic data.
51+
/// Aggregated records older than this are ignored.
52+
pub aggregated_cutoff_timestamp: Option<i64>,
5053
}

src/kafka.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,22 @@ mod receipts {
6666
.with_context(|| anyhow!("missing payload at {partition} {offset}"))?;
6767
let msg = IndexerFeesHourlyProtobuf::decode(payload)?;
6868
latest_aggregated_timestamp = latest_aggregated_timestamp.max(msg.timestamp);
69+
if let Some(cutoff) = config.aggregated_cutoff_timestamp {
70+
if msg.timestamp < cutoff {
71+
continue;
72+
}
73+
}
6974
for aggregation in &msg.aggregations {
7075
if !signers.contains(&Address::from_slice(&aggregation.signer)) {
7176
continue;
7277
}
73-
// Aggregated topic doesn't include allocation, skip these entries
74-
// as we can't determine if they're from legacy allocations.
75-
// The realtime topic will provide allocation-level data.
76-
let _ = (msg.timestamp, &aggregation.receiver, aggregation.fee_grt);
78+
let update = Update {
79+
timestamp: DateTime::from_timestamp_millis(msg.timestamp)
80+
.context("timestamp out of range")?,
81+
indexer: Address::from_slice(&aggregation.receiver),
82+
fee: (aggregation.fee_grt * 1e18) as u128,
83+
};
84+
db.send(update).await.unwrap();
7785
}
7886

7987
if latest_aggregated_offsets.get(&partition).unwrap() == &offset {
@@ -133,9 +141,6 @@ mod receipts {
133141
/// 20 bytes (address)
134142
#[prost(bytes, tag = "1")]
135143
indexer: Vec<u8>,
136-
/// 20 bytes (address)
137-
#[prost(bytes, tag = "3")]
138-
allocation: Vec<u8>,
139144
#[prost(double, tag = "6")]
140145
fee_grt: f64,
141146
}
@@ -177,7 +182,7 @@ mod receipts {
177182
for indexer_query in payload.indexer_queries {
178183
let update = Update {
179184
timestamp,
180-
allocation: Address::from_slice(&indexer_query.allocation),
185+
indexer: Address::from_slice(&indexer_query.indexer),
181186
fee: (indexer_query.fee_grt * 1e18) as u128,
182187
};
183188
let _ = db.send(update).await;
@@ -189,11 +194,12 @@ mod receipts {
189194

190195
pub struct Update {
191196
pub timestamp: DateTime<Utc>,
192-
pub allocation: Address,
197+
pub indexer: Address,
193198
pub fee: u128,
194199
}
195200

196201
pub struct DB {
202+
// indexer debts, aggregated per hour
197203
data: BTreeMap<Address, BTreeMap<i64, u128>>,
198204
window: Duration,
199205
tx: watch::Sender<BTreeMap<Address, u128>>,
@@ -239,7 +245,7 @@ mod receipts {
239245
}
240246
let entry = self
241247
.data
242-
.entry(update.allocation)
248+
.entry(update.indexer)
243249
.or_default()
244250
.entry(hourly_timestamp(update.timestamp))
245251
.or_default();
@@ -257,7 +263,7 @@ mod receipts {
257263
fn snapshot(&self) -> BTreeMap<Address, u128> {
258264
self.data
259265
.iter()
260-
.map(|(allocation, entries)| (*allocation, entries.values().sum()))
266+
.map(|(indexer, entries)| (*indexer, entries.values().sum()))
261267
.collect()
262268
}
263269
}

src/main.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,24 +149,20 @@ async fn main() -> anyhow::Result<()> {
149149
tracing::debug!(receivers = receivers.len());
150150

151151
let mut indexer_ravs: BTreeMap<Address, u128> = Default::default();
152-
let mut indexer_receipts: BTreeMap<Address, u128> = Default::default();
153152
{
154153
let allocation_ravs = ravs.borrow();
155-
let allocation_receipts = receipts.borrow();
156-
for allocation in &allocations {
154+
for allocation in allocations {
157155
if let Some(value) = allocation_ravs.get(&allocation.id) {
158156
*indexer_ravs.entry(allocation.indexer).or_default() += *value;
159157
}
160-
if let Some(value) = allocation_receipts.get(&allocation.id) {
161-
*indexer_receipts.entry(allocation.indexer).or_default() += *value;
162-
}
163158
}
164159
}
165160

166161
let mut debts: BTreeMap<Address, u128> = Default::default();
167162
{
163+
let receipts = receipts.borrow();
168164
for receiver in &receivers {
169-
let receipts = *indexer_receipts.get(receiver).unwrap_or(&0);
165+
let receipts = *receipts.get(receiver).unwrap_or(&0);
170166
let ravs = *indexer_ravs.get(receiver).unwrap_or(&0);
171167
debts.insert(*receiver, u128::max(receipts, ravs));
172168
tracing::info!(

0 commit comments

Comments
 (0)