Skip to content

Commit f11fe50

Browse files
committed
fix(coprocessor): use block hash to identify transaction
fixes a bug where 2 transactions are swapped in a reorg but keeping the same tx id and being dependent on each other the 4 apparent tx are merged to 2 tx and create a cycle
1 parent ef080b9 commit f11fe50

17 files changed

+162
-85
lines changed

coprocessor/fhevm-engine/.sqlx/query-9a71466b2a069b1f23002c8e3e2368eb9067669b008dc7d1c80b11d75cbe9897.json renamed to coprocessor/fhevm-engine/.sqlx/query-2c0cbd163c2be49aa72f7501e7d9932e14c467b7d9e91ace65c7da0cbeff399d.json

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

coprocessor/fhevm-engine/.sqlx/query-abf5e9cde25bc541a81b63750c3464c633a9b0d724d094e0355455e0d80de3c1.json renamed to coprocessor/fhevm-engine/.sqlx/query-51b8e3aa98b5e3b05d6b689513337a2f44ff9cb933e848e198e9b4da27ee0c33.json

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

coprocessor/fhevm-engine/.sqlx/query-571a684cbff1241ec33dda67bd02697aa95adc548f114c5bb009248c84f304b2.json renamed to coprocessor/fhevm-engine/.sqlx/query-c16c82c56c7b1dfdd564095e7a11e35d4ade73c57a1f64fdfba0b3d7cccf2c16.json

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

coprocessor/fhevm-engine/.sqlx/query-a4c38217a203643b4890c03be19520eea8c05c2a29d4953a05742478ba3c35e0.json renamed to coprocessor/fhevm-engine/.sqlx/query-e94b1ae0deacdc1d532d6d5ff4761fa1ed397da7ca4c7aec62b62fd2a397ccf8.json

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

coprocessor/fhevm-engine/Dockerfile.workspace

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ WORKDIR /app
107107

108108
# Install sqlx-cli
109109
RUN --mount=type=cache,target=/usr/local/cargo/registry,sharing=locked \
110-
cargo install sqlx-cli --version 0.7.2 \
110+
cargo install sqlx-cli --version 0.8.6 \
111111
--no-default-features --features postgres --locked
112112

113113
# =============================================================================

coprocessor/fhevm-engine/db-migration/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ WORKDIR /app
77

88
# Install sqlx-cli
99
RUN --mount=type=cache,target=/usr/local/cargo/registry,sharing=locked \
10-
cargo install sqlx-cli --version 0.7.2 \
10+
cargo install sqlx-cli --version 0.8.6 \
1111
--no-default-features --features postgres --locked
1212

1313
# Copy migrations and initialization script
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
ALTER TABLE computations
2+
ADD COLUMN IF NOT EXISTS block_hash BYTEA NOT NULL DEFAULT '\x00'::BYTEA;
3+
-- Default value ensures a basic rollback does not fail, but it is not ideal.
4+
-- We should remove the default value in the next release.
5+
6+
-- Alter the primary key to include block_hash
7+
ALTER TABLE computations DROP CONSTRAINT computations_pkey;
8+
ALTER TABLE computations
9+
ADD CONSTRAINT computations_pkey
10+
PRIMARY KEY (tenant_id, output_handle, transaction_id, block_hash);
11+
-- Note: this is non reversible except manually:
12+
-- * dropping the primary key
13+
-- * either removing all reorg out blocks or deduplicating handle with different block_hash but same tx_id
14+
15+
-- Alter the unique primary key to include block_hash
16+
DROP INDEX IF EXISTS idx_computations_no_tenant;
17+
CREATE UNIQUE INDEX IF NOT EXISTS idx_computations_no_tenant
18+
ON computations (output_handle, transaction_id, block_hash);
19+
-- Note: this is non reversible except manually:
20+
-- * dropping the primary key
21+
-- * either removing all reorg out blocks or deduplicating handle with different block_hash but same tx_id
22+
23+
-- For next release, we should remove the default values for block_hash and block_number.
24+
-- ALTER TABLE computations ALTER COLUMN block_hash DROP DEFAULT;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,9 @@ mod tests {
474474
use crate::contracts::TfheContract as C;
475475
use crate::contracts::TfheContract::TfheContractEvents as E;
476476
use crate::database::dependence_chains::dependence_chains;
477-
use crate::database::tfhe_event_propagate::{Chain, ChainCache, LogTfhe};
477+
use crate::database::tfhe_event_propagate::{
478+
Chain, ChainCache, ChainHash, LogTfhe,
479+
};
478480
use crate::database::tfhe_event_propagate::{
479481
ClearConst, Handle, TransactionHash,
480482
};
@@ -502,6 +504,7 @@ mod tests {
502504
event: tfhe_event(e),
503505
is_allowed,
504506
block_number: 0,
507+
block_hash: ChainHash::ZERO,
505508
block_timestamp: sqlx::types::time::PrimitiveDateTime::MIN,
506509
transaction_hash: Some(tx),
507510
dependence_chain: TransactionHash::ZERO,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ pub async fn ingest_block_logs(
209209
transaction_hash: log.transaction_hash,
210210
block_number,
211211
block_timestamp,
212+
block_hash,
212213
// updated in the next loop and dependence_chains
213214
is_allowed: false,
214215
dependence_chain: Default::default(),

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub struct LogTfhe {
122122
pub transaction_hash: Option<TransactionHash>,
123123
pub is_allowed: bool,
124124
pub block_number: u64,
125+
pub block_hash: BlockHash,
125126
pub block_timestamp: PrimitiveDateTime,
126127
pub tx_depth_size: u64,
127128
pub dependence_chain: TransactionHash,
@@ -375,10 +376,11 @@ impl Database {
375376
schedule_order,
376377
is_completed,
377378
host_chain_id,
379+
block_hash,
378380
block_number
379381
)
380-
VALUES ($1, $2, $3, $4, $5, $6, $7, NOW(), $8::timestamp, $9, $10, $11)
381-
ON CONFLICT (output_handle, transaction_id) DO NOTHING
382+
VALUES ($1, $2, $3, $4, $5, $6, $7, NOW(), $8::timestamp, $9, $10, $11, $12)
383+
ON CONFLICT (output_handle, transaction_id, block_hash) DO NOTHING
382384
"#,
383385
output_handle,
384386
&dependencies,
@@ -393,7 +395,8 @@ impl Database {
393395
)),
394396
!log.is_allowed,
395397
self.chain_id.as_i64(),
396-
log.block_number as i64
398+
log.block_hash.as_slice(),
399+
log.block_number as i64,
397400
);
398401
query
399402
.execute(tx.deref_mut())

0 commit comments

Comments
 (0)