Skip to content

Commit

Permalink
give each file_contract_element a block_id
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Sep 27, 2024
1 parent f4df9c1 commit 7a26b3b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
12 changes: 6 additions & 6 deletions persist/sqlite/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;`)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion persist/sqlite/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 (
Expand Down

0 comments on commit 7a26b3b

Please sign in to comment.