Skip to content

Commit e032e6b

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 cbdd141 commit e032e6b

18 files changed

+166
-85
lines changed

.github/workflows/test-suite-e2e-tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ jobs:
264264
working-directory: test-suite/fhevm
265265
if: always()
266266
run: |
267+
echo "::group::db-migration Logs"
268+
./fhevm-cli logs coprocessor-db-migration
267269
echo "::group::Relayer Logs"
268270
./fhevm-cli logs fhevm-relayer
269271
echo "::endgroup::"

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
@@ -109,7 +109,7 @@ WORKDIR /app
109109

110110
# Install sqlx-cli
111111
RUN --mount=type=cache,target=/usr/local/cargo/registry,sharing=locked \
112-
cargo install sqlx-cli --version 0.7.2 \
112+
cargo install sqlx-cli --version 0.8.6 \
113113
--no-default-features --features postgres --locked
114114

115115
# =============================================================================

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

0 commit comments

Comments
 (0)