Skip to content

Commit 44774fa

Browse files
archive bridges to bridge_archive table and handle reorgs
1 parent a9c7035 commit 44774fa

File tree

2 files changed

+114
-3
lines changed

2 files changed

+114
-3
lines changed
Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,75 @@
11
-- +migrate Down
22
DROP TABLE IF EXISTS backward_let;
33

4+
DROP TRIGGER IF EXISTS archive_bridge_before_delete;
5+
6+
DROP TABLE IF EXISTS bridge_archive;
7+
48
-- +migrate Up
5-
CREATE TABLE
6-
backward_let (
9+
CREATE TABLE IF NOT EXISTS backward_let (
710
block_num INTEGER NOT NULL REFERENCES block (num) ON DELETE CASCADE,
811
block_pos INTEGER NOT NULL,
912
previous_deposit_count TEXT NOT NULL,
1013
previous_root VARCHAR NOT NULL,
1114
new_deposit_count TEXT NOT NULL,
1215
new_root VARCHAR NOT NULL,
1316
PRIMARY KEY (block_num, block_pos)
14-
);
17+
);
18+
19+
------------------------------------------------------------------------------
20+
-- Create archive table
21+
------------------------------------------------------------------------------
22+
CREATE TABLE IF NOT EXISTS bridge_archive (
23+
deposit_count INTEGER PRIMARY KEY,
24+
block_num INTEGER NOT NULL,
25+
block_pos INTEGER NOT NULL,
26+
leaf_type INTEGER NOT NULL,
27+
origin_network INTEGER NOT NULL,
28+
origin_address VARCHAR NOT NULL,
29+
destination_network INTEGER NOT NULL,
30+
destination_address VARCHAR NOT NULL,
31+
amount TEXT NOT NULL,
32+
metadata BLOB,
33+
tx_hash VARCHAR,
34+
block_timestamp INTEGER,
35+
txn_sender VARCHAR
36+
);
37+
38+
------------------------------------------------------------------------------
39+
-- Create BEFORE DELETE trigger: archive only deleted rows
40+
------------------------------------------------------------------------------
41+
CREATE TRIGGER IF NOT EXISTS archive_bridge_before_delete
42+
BEFORE DELETE ON bridge
43+
FOR EACH ROW
44+
BEGIN
45+
INSERT INTO bridge_archive (
46+
deposit_count,
47+
block_num,
48+
block_pos,
49+
leaf_type,
50+
origin_network,
51+
origin_address,
52+
destination_network,
53+
destination_address,
54+
amount,
55+
metadata,
56+
tx_hash,
57+
block_timestamp,
58+
txn_sender
59+
)
60+
VALUES (
61+
OLD.deposit_count,
62+
OLD.block_num,
63+
OLD.block_pos,
64+
OLD.leaf_type,
65+
OLD.origin_network,
66+
OLD.origin_address,
67+
OLD.destination_network,
68+
OLD.destination_address,
69+
OLD.amount,
70+
OLD.metadata,
71+
OLD.tx_hash,
72+
OLD.block_timestamp,
73+
OLD.txn_sender
74+
);
75+
END;

bridgesync/processor.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ var (
121121
WHERE block_num >= $1 AND block_num <= $2
122122
ORDER BY block_num ASC, block_pos ASC;
123123
`, bridgeTableName)
124+
125+
// bridgeRestoreSQL is SQL query that moves rows back from bridge_archive to bridge table
126+
bridgeRestoreSQL = fmt.Sprintf(`
127+
INSERT INTO %s (
128+
block_num, block_pos, leaf_type, origin_network, origin_address,
129+
destination_network, destination_address, amount, metadata,
130+
tx_hash, block_timestamp, txn_sender, deposit_count
131+
)
132+
SELECT
133+
block_num, block_pos, leaf_type, origin_network, origin_address,
134+
destination_network, destination_address, amount, metadata,
135+
tx_hash, block_timestamp, txn_sender, deposit_count
136+
FROM bridge_archive
137+
WHERE deposit_count > $1 AND deposit_count <= $2
138+
`, bridgeTableName)
124139
)
125140

126141
// Bridge is the representation of a bridge event
@@ -1172,11 +1187,46 @@ func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error {
11721187
}
11731188
}()
11741189

1190+
// ---------------------------------------------------------------------
1191+
// 1. Load affected BackwardLETs BEFORE deleting blocks, bridges and BackwardLET entries
1192+
// ---------------------------------------------------------------------
1193+
backwardLETsQuery := `
1194+
SELECT previous_deposit_count, new_deposit_count
1195+
FROM backward_let
1196+
WHERE block_num >= $1`
1197+
var backwardLETs []*BackwardLET
1198+
if err := meddler.QueryAll(tx, &backwardLETs, backwardLETsQuery, firstReorgedBlock); err != nil {
1199+
return fmt.Errorf("failed to retrieve the affected backward LETs: %w", err)
1200+
}
1201+
1202+
// ---------------------------------------------------------------------
1203+
// 2. Restore bridge rows from archive for each interval
1204+
// ---------------------------------------------------------------------
1205+
for _, backwardLET := range backwardLETs {
1206+
if backwardLET.PreviousDepositCount.Cmp(backwardLET.NewDepositCount) <= 0 {
1207+
continue // malformed but safe to skip
1208+
}
1209+
1210+
if _, err := tx.Exec(bridgeRestoreSQL, backwardLET.NewDepositCount, backwardLET.NewDepositCount); err != nil {
1211+
return fmt.Errorf("failed to restore bridges from bridge archive (range %d..%d): %w",
1212+
backwardLET.NewDepositCount, backwardLET.PreviousDepositCount, err)
1213+
}
1214+
1215+
// Remove restored rows from archive
1216+
_, err := tx.Exec(`DELETE FROM bridge_archive
1217+
WHERE deposit_count > $1 AND deposit_count <= $2`, backwardLET.NewDepositCount, backwardLET.PreviousDepositCount)
1218+
if err != nil {
1219+
return fmt.Errorf("failed to delete restored rows from archive (range %d..%d): %w",
1220+
backwardLET.NewDepositCount, backwardLET.PreviousDepositCount, err)
1221+
}
1222+
}
1223+
11751224
blocksRes, err := tx.Exec(`DELETE FROM block WHERE num >= $1;`, firstReorgedBlock)
11761225
if err != nil {
11771226
p.log.Errorf("failed to delete blocks during reorg: %v", err)
11781227
return err
11791228
}
1229+
11801230
rowsAffected, err := blocksRes.RowsAffected()
11811231
if err != nil {
11821232
p.log.Errorf("failed to get rows affected during reorg: %v", err)

0 commit comments

Comments
 (0)