Skip to content

Commit 8596c6e

Browse files
authored
fix(coprocessor): host-listener, block time in computations.created_at & .schedule_order (#1409)
use block time instead of the default now() == transaction time
1 parent 64dd072 commit 8596c6e

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed
Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coprocessor/fhevm-engine/host-listener/src/cmd/mod.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,22 @@ use alloy::pubsub::SubscriptionStream;
55
use alloy::rpc::types::{Block, BlockNumberOrTag, Filter, Header, Log};
66
use alloy::sol_types::SolEventInterface;
77
use anyhow::{anyhow, Result};
8-
use fhevm_engine_common::telemetry;
8+
use clap::Parser;
99
use futures_util::stream::StreamExt;
10+
use rustls;
11+
use sqlx::types::time::{OffsetDateTime, PrimitiveDateTime};
1012
use sqlx::types::Uuid;
13+
use tokio::sync::RwLock;
14+
use tokio_util::sync::CancellationToken;
15+
use tracing::{error, info, warn, Level};
1116

1217
use std::collections::{HashSet, VecDeque};
1318
use std::str::FromStr;
1419
use std::sync::Arc;
1520
use std::time::Duration;
16-
use tokio::sync::RwLock;
17-
use tracing::{error, info, warn, Level};
18-
19-
use clap::Parser;
20-
21-
use rustls;
22-
23-
use tokio_util::sync::CancellationToken;
2421

2522
use fhevm_engine_common::healthz_server::HttpServer as HealthHttpServer;
23+
use fhevm_engine_common::telemetry;
2624
use fhevm_engine_common::types::{BlockchainProvider, Handle};
2725
use fhevm_engine_common::utils::{DatabaseURL, HeartBeat};
2826

@@ -878,6 +876,18 @@ async fn db_insert_block_no_retry(
878876
let block_hash = block_logs.summary.hash;
879877
let block_number = block_logs.summary.number;
880878
let mut catchup_insertion = 0;
879+
let block_timestamp = OffsetDateTime::from_unix_timestamp(
880+
block_logs.summary.timestamp as i64,
881+
)
882+
.unwrap_or_else(|_| {
883+
error!(
884+
timestamp = block_logs.summary.timestamp,
885+
"Invalid block timestamp, using now",
886+
);
887+
OffsetDateTime::now_utc()
888+
});
889+
let block_timestamp =
890+
PrimitiveDateTime::new(block_timestamp.date(), block_timestamp.time());
881891
for log in &block_logs.logs {
882892
let current_address = Some(log.inner.address);
883893
let is_acl_address = &current_address == acl_contract_address;
@@ -919,6 +929,7 @@ async fn db_insert_block_no_retry(
919929
transaction_hash: log.transaction_hash,
920930
is_allowed: false, // updated in the next loop
921931
block_number,
932+
block_timestamp,
922933
};
923934
tfhe_event_log.push(log);
924935
continue;

coprocessor/fhevm-engine/host-listener/src/database/tfhe_event_propagate.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub struct LogTfhe {
8787
pub transaction_hash: Option<TransactionHash>,
8888
pub is_allowed: bool,
8989
pub block_number: u64,
90+
pub block_timestamp: sqlx::types::time::PrimitiveDateTime,
9091
}
9192

9293
pub type Transaction<'l> = sqlx::Transaction<'l, Postgres>;
@@ -289,9 +290,11 @@ impl Database {
289290
is_scalar,
290291
dependence_chain_id,
291292
transaction_id,
292-
is_allowed
293+
is_allowed,
294+
created_at,
295+
schedule_order
293296
)
294-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
297+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $9)
295298
ON CONFLICT (tenant_id, output_handle, transaction_id) DO NOTHING
296299
"#,
297300
tenant_id as i32,
@@ -302,6 +305,7 @@ impl Database {
302305
bucket.to_vec(),
303306
log.transaction_hash.map(|txh| txh.to_vec()),
304307
log.is_allowed,
308+
log.block_timestamp,
305309
);
306310
query
307311
.execute(tx.deref_mut())

coprocessor/fhevm-engine/stress-test-generator/src/utils.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use host_listener::database::tfhe_event_propagate::{
66
ClearConst, Database as ListenerDatabase, Handle, LogTfhe, TransactionHash,
77
};
88
use rand::Rng;
9+
use sqlx::types::time::PrimitiveDateTime;
910
use sqlx::Postgres;
1011
use std::sync::Arc;
1112
use tracing::info;
@@ -231,6 +232,7 @@ pub async fn generate_trivial_encrypt(
231232
transaction_hash: Some(transaction_hash),
232233
is_allowed,
233234
block_number: 1,
235+
block_timestamp: PrimitiveDateTime::MAX,
234236
};
235237
let mut tx = listener_event_to_db.new_transaction().await?;
236238
listener_event_to_db
@@ -412,6 +414,7 @@ pub async fn insert_tfhe_event(
412414
transaction_hash: Some(transaction_hash),
413415
is_allowed,
414416
block_number: 1,
417+
block_timestamp: PrimitiveDateTime::MAX,
415418
};
416419
listener_event_to_db
417420
.insert_tfhe_event(&mut tx, &log)

coprocessor/fhevm-engine/tfhe-worker/src/tests/operators_from_events.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use alloy::primitives::{FixedBytes, Log};
22
use bigdecimal::num_bigint::BigInt;
3+
use sqlx::types::time::PrimitiveDateTime;
34

45
use fhevm_engine_common::types::AllowEvents;
56
use host_listener::contracts::TfheContract;
@@ -42,6 +43,7 @@ async fn insert_tfhe_event(
4243
transaction_hash: log.transaction_hash,
4344
is_allowed,
4445
block_number: log.block_number.unwrap_or(0),
46+
block_timestamp: PrimitiveDateTime::MAX,
4547
};
4648
db.insert_tfhe_event(tx, &event).await
4749
}

0 commit comments

Comments
 (0)