From 7a26b3b77c42ab73e73140a16593e2146dc8a243 Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Fri, 27 Sep 2024 14:02:38 -0400 Subject: [PATCH] give each file_contract_element a block_id --- persist/sqlite/consensus.go | 12 ++++++------ persist/sqlite/init.sql | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/persist/sqlite/consensus.go b/persist/sqlite/consensus.go index f57109e0..5e9b5beb 100644 --- a/persist/sqlite/consensus.go +++ b/persist/sqlite/consensus.go @@ -713,8 +713,8 @@ func deleteBlock(tx *txn, bid types.BlockID) error { } func updateFileContractElements(tx *txn, revert bool, b types.Block, fces []explorer.FileContractUpdate) (map[explorer.DBFileContract]int64, error) { - stmt, err := tx.Prepare(`INSERT INTO file_contract_elements(contract_id, leaf_index, resolved, valid, filesize, file_merkle_root, window_start, window_end, payout, unlock_hash, revision_number) - VALUES (?, ?, FALSE, FALSE, ?, ?, ?, ?, ?, ?, ?) + stmt, err := tx.Prepare(`INSERT INTO file_contract_elements(contract_id, block_id, leaf_index, resolved, valid, filesize, file_merkle_root, window_start, window_end, payout, unlock_hash, revision_number) + VALUES (?, ?, ?, FALSE, FALSE, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT (contract_id, revision_number) DO UPDATE SET resolved = ?, valid = ?, leaf_index = ? RETURNING id;`) @@ -779,7 +779,7 @@ func updateFileContractElements(tx *txn, revert bool, b types.Block, fces []expl addFC := func(fcID types.FileContractID, leafIndex uint64, fc types.FileContract, resolved, valid, lastRevision bool) error { var dbID int64 dbFC := explorer.DBFileContract{ID: fcID, RevisionNumber: fc.RevisionNumber} - err := stmt.QueryRow(encode(fcID), encode(leafIndex), encode(fc.Filesize), encode(fc.FileMerkleRoot), encode(fc.WindowStart), encode(fc.WindowEnd), encode(fc.Payout), encode(fc.UnlockHash), encode(fc.RevisionNumber), resolved, valid, encode(leafIndex)).Scan(&dbID) + err := stmt.QueryRow(encode(fcID), encode(b.ID()), encode(leafIndex), encode(fc.Filesize), encode(fc.FileMerkleRoot), encode(fc.WindowStart), encode(fc.WindowEnd), encode(fc.Payout), encode(fc.UnlockHash), encode(fc.RevisionNumber), resolved, valid, encode(leafIndex)).Scan(&dbID) if err != nil { return fmt.Errorf("failed to execute file_contract_elements statement: %w", err) } @@ -1021,9 +1021,9 @@ func (ut *updateTx) RevertIndex(state explorer.UpdateState) error { return fmt.Errorf("RevertIndex: failed to delete block: %w", err) } else if err := updateStateTree(ut.tx, state.TreeUpdates); err != nil { return fmt.Errorf("RevertIndex: failed to update state tree: %w", err) - } else if err := cleanupUnusedFileContracts(ut.tx, state.FileContractElements); err != nil { - return fmt.Errorf("RevertIndex: failed to remove unused file contract elements: %w", err) - } + } // else if err := cleanupUnusedFileContracts(ut.tx, state.FileContractElements); err != nil { + // return fmt.Errorf("RevertIndex: failed to remove unused file contract elements: %w", err) + // } return nil } diff --git a/persist/sqlite/init.sql b/persist/sqlite/init.sql index 82e9658d..f5a0b088 100644 --- a/persist/sqlite/init.sql +++ b/persist/sqlite/init.sql @@ -75,6 +75,7 @@ CREATE INDEX siafund_elements_address_spent_index ON siafund_elements(address, s CREATE TABLE file_contract_elements ( id INTEGER PRIMARY KEY, + block_id BLOB REFERENCES blocks(id) ON DELETE CASCADE NOT NULL, contract_id BLOB NOT NULL, leaf_index BLOB NOT NULL, @@ -97,7 +98,7 @@ CREATE TABLE last_contract_revision ( contract_id BLOB PRIMARY KEY NOT NULL, ed25519_renter_key BLOB, ed25519_host_key BLOB, - contract_element_id INTEGER UNIQUE REFERENCES file_contract_elements(id) NOT NULL + contract_element_id INTEGER UNIQUE REFERENCES file_contract_elements(id) ON DELETE CASCADE NOT NULL ); CREATE TABLE file_contract_valid_proof_outputs (