Skip to content

Commit 3ac52fe

Browse files
committed
fix(coprocessor): support multichain
1 parent 974fe41 commit 3ac52fe

13 files changed

+213
-58
lines changed

coprocessor/fhevm-engine/.sqlx/query-51b0ba894dbdd2b26c9ad13e1a5b3d4657af9aa912bbe652eabeae2959588589.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coprocessor/fhevm-engine/.sqlx/query-641036eba016313ea7cf191d71f2b69c1def70ea46139dd02fb510581b6322c2.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

coprocessor/fhevm-engine/.sqlx/query-740ca88502667c575985e65d9a600221db69925024f9822e80ba9dd0677044fc.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coprocessor/fhevm-engine/.sqlx/query-e007c4af2864544c0eaa5d27f456f611b3d9f9909a845f78f85cdd69787c7106.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coprocessor/fhevm-engine/.sqlx/query-e81237a8c29c0dd0790b37ae3e72662afd3ea367dc5337fe721d42d96de814e3.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coprocessor/fhevm-engine/.sqlx/query-fb8986ef0add0975068fd2f4ee9edd02645ded4a717b96c7b2c164651a79ad07.json

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ALTER TABLE IF EXISTS host_chain_blocks_valid
2+
ADD COLUMN IF NOT EXISTS block_status TEXT NOT NULL DEFAULT 'unknown' CHECK (block_status IN ('pending', 'unknown', 'finalized', 'orphaned'));
3+
4+
ALTER TABLE IF EXISTS host_chain_blocks_valid
5+
ALTER COLUMN block_status DROP DEFAULT;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub const DEFAULT_DEPENDENCE_BY_CONNEXITY: bool = false;
4040
pub const DEFAULT_DEPENDENCE_CROSS_BLOCK: bool = true;
4141

4242
const TIMEOUT_REQUEST_ON_WEBSOCKET: u64 = 15;
43+
pub const MINIMUM_DELAY_FOR_FINALIZATION_IN_BLOCKS: u64 = 5;
4344

4445
#[derive(Parser, Debug, Clone)]
4546
#[command(version, about, long_about = None)]
@@ -459,6 +460,7 @@ impl InfiniteLogIter {
459460
logs: std::mem::take(&mut current_logs),
460461
summary,
461462
catchup: true,
463+
finalized: self.catchup_finalization_in_blocks > MINIMUM_DELAY_FOR_FINALIZATION_IN_BLOCKS,
462464
};
463465
blocks_logs.push(block_logs);
464466
}
@@ -731,6 +733,7 @@ impl InfiniteLogIter {
731733
logs,
732734
summary: missing_block,
733735
catchup: true,
736+
finalized: false, // let catchups with finality conditions do the finalize later
734737
});
735738
self.block_history.add_block(missing_block);
736739
}
@@ -825,6 +828,7 @@ impl InfiniteLogIter {
825828
logs: self.get_logs_at_hash(block_header.hash).await?,
826829
summary: block_header.into(),
827830
catchup: false,
831+
finalized: false,
828832
})
829833
}
830834

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct BlockLogs<T> {
1919
pub logs: Vec<T>,
2020
pub summary: BlockSummary,
2121
pub catchup: bool,
22+
pub finalized: bool,
2223
}
2324

2425
#[derive(Copy, Clone, Debug)]
@@ -309,8 +310,7 @@ pub async fn ingest_block_logs(
309310
info!(block_number, catchup_insertion, "Catchup inserted events");
310311
}
311312
}
312-
313-
db.mark_block_as_valid(&mut tx, &block_logs.summary).await?;
313+
db.mark_block_as_valid(&mut tx, &block_logs.summary, block_logs.finalized).await?;
314314
if at_least_one_insertion {
315315
db.update_dependence_chain(
316316
&mut tx,

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,19 +495,44 @@ impl Database {
495495
&self,
496496
tx: &mut Transaction<'_>,
497497
block_summary: &BlockSummary,
498+
finalized: bool,
498499
) -> Result<(), SqlxError> {
500+
let status = if finalized { "finalized" } else { "pending" };
501+
// 1. Insert if not exists (never overwrites existing row)
499502
sqlx::query!(
500503
r#"
501-
INSERT INTO host_chain_blocks_valid (chain_id, block_hash, block_number)
502-
VALUES ($1, $2, $3)
503-
ON CONFLICT (chain_id, block_hash) DO NOTHING;
504+
INSERT INTO host_chain_blocks_valid (chain_id, block_hash, block_number, block_status)
505+
VALUES ($1, $2, $3, $4)
506+
ON CONFLICT (chain_id, block_hash) DO NOTHING
507+
;
504508
"#,
505509
self.chain_id.as_i64(),
506510
block_summary.hash.to_vec(),
507511
block_summary.number as i64,
512+
status,
508513
)
509514
.execute(tx.deref_mut())
510515
.await?;
516+
517+
// 2. Update to finalized or orphan if needed
518+
if finalized {
519+
sqlx::query!(
520+
r#"
521+
UPDATE host_chain_blocks_valid
522+
SET block_status = CASE
523+
WHEN block_hash = $2
524+
THEN 'finalized'
525+
ELSE 'orphaned'
526+
END
527+
WHERE block_status = 'pending' AND block_number = $3 AND chain_id = $1
528+
"#,
529+
self.chain_id.as_i64(),
530+
block_summary.hash.to_vec(),
531+
block_summary.number as i64,
532+
)
533+
.execute(tx.deref_mut())
534+
.await?;
535+
}
511536
Ok(())
512537
}
513538

0 commit comments

Comments
 (0)