Skip to content

Commit

Permalink
store index fields separately
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 authored and n8maninger committed Nov 21, 2024
1 parent 37a6b85 commit c34649d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 41 deletions.
20 changes: 10 additions & 10 deletions persist/sqlite/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,10 @@ func updateFileContractElements(tx *txn, revert bool, index types.ChainIndex, b
}
defer stmt.Close()

revisionStmt, err := tx.Prepare(`INSERT INTO last_contract_revision(contract_id, contract_element_id, ed25519_renter_key, ed25519_host_key, confirmation_index, confirmation_transaction_id)
VALUES (?, ?, ?, ?, COALESCE(?, X'aa'), COALESCE(?, X'aa'))
revisionStmt, err := tx.Prepare(`INSERT INTO last_contract_revision(contract_id, contract_element_id, ed25519_renter_key, ed25519_host_key, confirmation_height, confirmation_block_id, confirmation_transaction_id)
VALUES (?, ?, ?, ?, COALESCE(?, X''), COALESCE(?, X''), COALESCE(?, X''))
ON CONFLICT (contract_id)
DO UPDATE SET contract_element_id = ?, ed25519_renter_key = COALESCE(?, ed25519_renter_key), ed25519_host_key = COALESCE(?, ed25519_host_key), confirmation_index = COALESCE(?, confirmation_index), confirmation_transaction_id = COALESCE(?, confirmation_transaction_id)`)
DO UPDATE SET contract_element_id = ?, ed25519_renter_key = COALESCE(?, ed25519_renter_key), ed25519_host_key = COALESCE(?, ed25519_host_key), confirmation_height = COALESCE(?, confirmation_height), confirmation_block_id = COALESCE(?, confirmation_block_id), confirmation_transaction_id = COALESCE(?, confirmation_transaction_id)`)
if err != nil {
return nil, fmt.Errorf("updateFileContractElements: failed to prepare last_contract_revision statement: %w", err)
}
Expand Down Expand Up @@ -857,14 +857,14 @@ func updateFileContractElements(tx *txn, revert bool, index types.ChainIndex, b
encodedHostKey = encode(keys[1]).([]byte)
}

var encodedChainIndex []byte
var encodedConfirmationTransactionID []byte
var encodedHeight, encodedBlockID, encodedConfirmationTransactionID []byte
if confirmationTransactionID != nil {
encodedChainIndex = encode(index).([]byte)
encodedHeight = encode(index.Height).([]byte)
encodedBlockID = encode(index.ID).([]byte)
encodedConfirmationTransactionID = encode(*confirmationTransactionID).([]byte)
}

if _, err := revisionStmt.Exec(encode(fcID), dbID, encodedRenterKey, encodedHostKey, encodedChainIndex, encodedConfirmationTransactionID, dbID, encodedRenterKey, encodedHostKey, encodedChainIndex, encodedConfirmationTransactionID); err != nil {
if _, err := revisionStmt.Exec(encode(fcID), dbID, encodedRenterKey, encodedHostKey, encodedHeight, encodedBlockID, encodedConfirmationTransactionID, dbID, encodedRenterKey, encodedHostKey, encodedHeight, encodedBlockID, encodedConfirmationTransactionID); err != nil {
return fmt.Errorf("failed to update last revision number: %w", err)
}
}
Expand Down Expand Up @@ -950,7 +950,7 @@ func updateFileContractElements(tx *txn, revert bool, index types.ChainIndex, b
}

func updateFileContractIndices(tx *txn, revert bool, index types.ChainIndex, fces []explorer.FileContractUpdate) error {
proofIndexStmt, err := tx.Prepare(`UPDATE last_contract_revision SET proof_index = ?, proof_transaction_id = ? WHERE contract_id = ?`)
proofIndexStmt, err := tx.Prepare(`UPDATE last_contract_revision SET proof_height = ?, proof_block_id = ?, proof_transaction_id = ? WHERE contract_id = ?`)
if err != nil {
return fmt.Errorf("updateFileContractIndices: failed to prepare proof index statement: %w", err)
}
Expand All @@ -962,13 +962,13 @@ func updateFileContractIndices(tx *txn, revert bool, index types.ChainIndex, fce

if revert {
if update.ProofTransactionID != nil {
if _, err := proofIndexStmt.Exec(nil, nil, encode(fcID)); err != nil {
if _, err := proofIndexStmt.Exec(nil, nil, nil, encode(fcID)); err != nil {
return fmt.Errorf("updateFileContractIndices: failed to update proof index: %w", err)
}
}
} else {
if update.ProofTransactionID != nil {
if _, err := proofIndexStmt.Exec(encode(index), encode(update.ProofTransactionID), encode(fcID)); err != nil {
if _, err := proofIndexStmt.Exec(encode(index.Height), encode(index.ID), encode(update.ProofTransactionID), encode(fcID)); err != nil {
return fmt.Errorf("updateFileContractIndices: failed to update proof index: %w", err)
}
}
Expand Down
8 changes: 4 additions & 4 deletions persist/sqlite/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func encodedIDs(ids []types.FileContractID) []any {
func scanFileContract(s scanner) (contractID int64, fc explorer.ExtendedFileContract, err error) {
var proofIndex types.ChainIndex
var proofTransactionID types.TransactionID
err = s.Scan(&contractID, decode(&fc.ID), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decode(&fc.ConfirmationIndex), decode(&fc.ConfirmationTransactionID), decodeNull(&proofIndex), decodeNull(&proofTransactionID), decode(&fc.Filesize), decode(&fc.FileMerkleRoot), decode(&fc.WindowStart), decode(&fc.WindowEnd), decode(&fc.Payout), decode(&fc.UnlockHash), decode(&fc.RevisionNumber))
err = s.Scan(&contractID, decode(&fc.ID), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decode(&fc.ConfirmationIndex.Height), decode(&fc.ConfirmationIndex.ID), decode(&fc.ConfirmationTransactionID), decodeNull(&proofIndex.Height), decodeNull(&proofIndex.ID), decodeNull(&proofTransactionID), decode(&fc.Filesize), decode(&fc.FileMerkleRoot), decode(&fc.WindowStart), decode(&fc.WindowEnd), decode(&fc.Payout), decode(&fc.UnlockHash), decode(&fc.RevisionNumber))

if proofIndex != (types.ChainIndex{}) {
fc.ProofIndex = &proofIndex
Expand All @@ -33,7 +33,7 @@ func scanFileContract(s scanner) (contractID int64, fc explorer.ExtendedFileCont
// Contracts implements explorer.Store.
func (s *Store) Contracts(ids []types.FileContractID) (result []explorer.ExtendedFileContract, err error) {
err = s.transaction(func(tx *txn) error {
query := `SELECT fc1.id, fc1.contract_id, fc1.resolved, fc1.valid, fc1.transaction_id, rev.confirmation_index, rev.confirmation_transaction_id, rev.proof_index, rev.proof_transaction_id, fc1.filesize, fc1.file_merkle_root, fc1.window_start, fc1.window_end, fc1.payout, fc1.unlock_hash, fc1.revision_number
query := `SELECT fc1.id, fc1.contract_id, fc1.resolved, fc1.valid, fc1.transaction_id, rev.confirmation_height, rev.confirmation_block_id, rev.confirmation_transaction_id, rev.proof_height, rev.proof_block_id, rev.proof_transaction_id, fc1.filesize, fc1.file_merkle_root, fc1.window_start, fc1.window_end, fc1.payout, fc1.unlock_hash, fc1.revision_number
FROM file_contract_elements fc1
INNER JOIN last_contract_revision rev ON rev.contract_element_id = fc1.id
WHERE rev.contract_id IN (` + queryPlaceHolders(len(ids)) + `)`
Expand Down Expand Up @@ -78,7 +78,7 @@ func (s *Store) Contracts(ids []types.FileContractID) (result []explorer.Extende
// ContractRevisions implements explorer.Store.
func (s *Store) ContractRevisions(id types.FileContractID) (revisions []explorer.ExtendedFileContract, err error) {
err = s.transaction(func(tx *txn) error {
query := `SELECT fc.id, fc.contract_id, fc.resolved, fc.valid, fc.transaction_id, rev.confirmation_index, rev.confirmation_transaction_id, rev.proof_index, rev.proof_transaction_id, fc.filesize, fc.file_merkle_root, fc.window_start, fc.window_end, fc.payout, fc.unlock_hash, fc.revision_number
query := `SELECT fc.id, fc.contract_id, fc.resolved, fc.valid, fc.transaction_id, rev.confirmation_height, rev.confirmation_block_id, rev.confirmation_transaction_id, rev.proof_height, rev.proof_block_id, rev.proof_transaction_id, fc.filesize, fc.file_merkle_root, fc.window_start, fc.window_end, fc.payout, fc.unlock_hash, fc.revision_number
FROM file_contract_elements fc
JOIN last_contract_revision rev ON rev.contract_id = fc.contract_id
WHERE fc.contract_id = ?
Expand Down Expand Up @@ -136,7 +136,7 @@ func (s *Store) ContractRevisions(id types.FileContractID) (revisions []explorer
// ContractsKey implements explorer.Store.
func (s *Store) ContractsKey(key types.PublicKey) (result []explorer.ExtendedFileContract, err error) {
err = s.transaction(func(tx *txn) error {
query := `SELECT fc1.id, fc1.contract_id, fc1.resolved, fc1.valid, fc1.transaction_id, rev.confirmation_index, rev.confirmation_transaction_id, rev.proof_index, rev.proof_transaction_id, fc1.filesize, fc1.file_merkle_root, fc1.window_start, fc1.window_end, fc1.payout, fc1.unlock_hash, fc1.revision_number
query := `SELECT fc1.id, fc1.contract_id, fc1.resolved, fc1.valid, fc1.transaction_id, rev.confirmation_height, rev.confirmation_block_id, rev.confirmation_transaction_id, rev.proof_height, rev.proof_block_id, rev.proof_transaction_id, fc1.filesize, fc1.file_merkle_root, fc1.window_start, fc1.window_end, fc1.payout, fc1.unlock_hash, fc1.revision_number
FROM file_contract_elements fc1
INNER JOIN last_contract_revision rev ON rev.contract_element_id = fc1.id
WHERE rev.ed25519_renter_key = ? OR rev.ed25519_host_key = ?`
Expand Down
12 changes: 8 additions & 4 deletions persist/sqlite/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ CREATE TABLE last_contract_revision (
ed25519_renter_key BLOB,
ed25519_host_key BLOB,

confirmation_index BLOB NOT NULL,
confirmation_height BLOB NOT NULL,
confirmation_block_id BLOB NOT NULL REFERENCES blocks(id) ON DELETE CASCADE,
confirmation_transaction_id BLOB NOT NULL REFERENCES transactions(transaction_id),

proof_index BLOB,
proof_height BLOB,
proof_block_id BLOB,
proof_transaction_id BLOB REFERENCES transactions(transaction_id),

contract_element_id INTEGER UNIQUE REFERENCES file_contract_elements(id) ON DELETE CASCADE NOT NULL
Expand Down Expand Up @@ -452,10 +454,12 @@ CREATE INDEX v2_file_contract_elements_contract_id_revision_number_index ON v2_f
CREATE TABLE v2_last_contract_revision (
contract_id BLOB PRIMARY KEY NOT NULL,

confirmation_index BLOB NOT NULL,
confirmation_height BLOB NOT NULL,
confirmation_block_id BLOB NOT NULL REFERENCES blocks(id) ON DELETE CASCADE,
confirmation_transaction_id BLOB NOT NULL REFERENCES v2_transactions(transaction_id),

resolution_index BLOB,
resolution_height BLOB,
resolution_block_id BLOB,
resolution_transaction_id BLOB REFERENCES v2_transactions(transaction_id),

contract_element_id INTEGER UNIQUE REFERENCES v2_file_contract_elements(id) ON DELETE CASCADE NOT NULL
Expand Down
8 changes: 4 additions & 4 deletions persist/sqlite/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ type contractOrder struct {

// transactionFileContracts returns the file contracts for each transaction.
func transactionFileContracts(tx *txn, txnIDs []int64) (map[int64][]explorer.ExtendedFileContract, error) {
query := `SELECT ts.transaction_id, fc.id, rev.confirmation_index, rev.confirmation_transaction_id, rev.proof_index, rev.proof_transaction_id, fc.contract_id, fc.resolved, fc.valid, fc.transaction_id, fc.filesize, fc.file_merkle_root, fc.window_start, fc.window_end, fc.payout, fc.unlock_hash, fc.revision_number
query := `SELECT ts.transaction_id, fc.id, rev.confirmation_height, rev.confirmation_block_id, rev.confirmation_transaction_id, rev.proof_height, rev.proof_block_id, rev.proof_transaction_id, fc.contract_id, fc.resolved, fc.valid, fc.transaction_id, fc.filesize, fc.file_merkle_root, fc.window_start, fc.window_end, fc.payout, fc.unlock_hash, fc.revision_number
FROM file_contract_elements fc
INNER JOIN transaction_file_contracts ts ON ts.contract_id = fc.id
INNER JOIN last_contract_revision rev ON rev.contract_id = fc.contract_id
Expand All @@ -307,7 +307,7 @@ ORDER BY ts.transaction_order ASC`

var proofIndex types.ChainIndex
var proofTransactionID types.TransactionID
if err := rows.Scan(&txnID, &contractID, decode(&fc.ConfirmationIndex), decode(&fc.ConfirmationTransactionID), decodeNull(&proofIndex), decodeNull(&proofTransactionID), decode(&fc.ID), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decode(&fc.Filesize), decode(&fc.FileMerkleRoot), decode(&fc.WindowStart), decode(&fc.WindowEnd), decode(&fc.Payout), decode(&fc.UnlockHash), decode(&fc.RevisionNumber)); err != nil {
if err := rows.Scan(&txnID, &contractID, decode(&fc.ConfirmationIndex.Height), decode(&fc.ConfirmationIndex.ID), decode(&fc.ConfirmationTransactionID), decodeNull(&proofIndex.Height), decodeNull(&proofIndex.ID), decodeNull(&proofTransactionID), decode(&fc.ID), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decode(&fc.Filesize), decode(&fc.FileMerkleRoot), decode(&fc.WindowStart), decode(&fc.WindowEnd), decode(&fc.Payout), decode(&fc.UnlockHash), decode(&fc.RevisionNumber)); err != nil {
return nil, fmt.Errorf("failed to scan file contract: %w", err)
}

Expand Down Expand Up @@ -338,7 +338,7 @@ ORDER BY ts.transaction_order ASC`

// transactionFileContracts returns the file contract revisions for each transaction.
func transactionFileContractRevisions(tx *txn, txnIDs []int64) (map[int64][]explorer.FileContractRevision, error) {
query := `SELECT ts.transaction_id, fc.id, rev.confirmation_index, rev.confirmation_transaction_id, rev.proof_index, rev.proof_transaction_id, ts.parent_id, ts.unlock_conditions, fc.contract_id, fc.resolved, fc.valid, fc.transaction_id, fc.filesize, fc.file_merkle_root, fc.window_start, fc.window_end, fc.payout, fc.unlock_hash, fc.revision_number
query := `SELECT ts.transaction_id, fc.id, rev.confirmation_height, rev.confirmation_block_id, rev.confirmation_transaction_id, rev.proof_height, rev.proof_block_id, rev.proof_transaction_id, ts.parent_id, ts.unlock_conditions, fc.contract_id, fc.resolved, fc.valid, fc.transaction_id, fc.filesize, fc.file_merkle_root, fc.window_start, fc.window_end, fc.payout, fc.unlock_hash, fc.revision_number
FROM file_contract_elements fc
INNER JOIN transaction_file_contract_revisions ts ON ts.contract_id = fc.id
INNER JOIN last_contract_revision rev ON rev.contract_id = fc.contract_id
Expand All @@ -361,7 +361,7 @@ ORDER BY ts.transaction_order ASC`

var proofIndex types.ChainIndex
var proofTransactionID types.TransactionID
if err := rows.Scan(&txnID, &contractID, decode(&fc.ConfirmationIndex), decode(&fc.ConfirmationTransactionID), decodeNull(&proofIndex), decodeNull(&proofTransactionID), decode(&fc.ParentID), decode(&fc.UnlockConditions), decode(&fc.ID), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decode(&fc.ExtendedFileContract.Filesize), decode(&fc.ExtendedFileContract.FileMerkleRoot), decode(&fc.ExtendedFileContract.WindowStart), decode(&fc.ExtendedFileContract.WindowEnd), decode(&fc.ExtendedFileContract.Payout), decode(&fc.ExtendedFileContract.UnlockHash), decode(&fc.ExtendedFileContract.RevisionNumber)); err != nil {
if err := rows.Scan(&txnID, &contractID, decode(&fc.ConfirmationIndex.Height), decode(&fc.ConfirmationIndex.ID), decode(&fc.ConfirmationTransactionID), decodeNull(&proofIndex.Height), decodeNull(&proofIndex.ID), decodeNull(&proofTransactionID), decode(&fc.ParentID), decode(&fc.UnlockConditions), decode(&fc.ID), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decode(&fc.ExtendedFileContract.Filesize), decode(&fc.ExtendedFileContract.FileMerkleRoot), decode(&fc.ExtendedFileContract.WindowStart), decode(&fc.ExtendedFileContract.WindowEnd), decode(&fc.ExtendedFileContract.Payout), decode(&fc.ExtendedFileContract.UnlockHash), decode(&fc.ExtendedFileContract.RevisionNumber)); err != nil {
return nil, fmt.Errorf("failed to scan file contract: %w", err)
}

Expand Down
20 changes: 10 additions & 10 deletions persist/sqlite/v2consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ func updateV2FileContractElements(tx *txn, revert bool, index types.ChainIndex,
}
defer stmt.Close()

revisionStmt, err := tx.Prepare(`INSERT INTO v2_last_contract_revision(contract_id, contract_element_id, confirmation_index, confirmation_transaction_id)
VALUES (?, ?, COALESCE(?, X'aa'), COALESCE(?, X'aa'))
revisionStmt, err := tx.Prepare(`INSERT INTO v2_last_contract_revision(contract_id, contract_element_id, confirmation_height, confirmation_block_id, confirmation_transaction_id)
VALUES (?, ?, COALESCE(?, X''), COALESCE(?, X''), COALESCE(?, X''))
ON CONFLICT (contract_id)
DO UPDATE SET contract_element_id = ?, confirmation_index = COALESCE(?, confirmation_index), confirmation_transaction_id = COALESCE(?, confirmation_transaction_id)`)
DO UPDATE SET contract_element_id = ?, confirmation_height = COALESCE(?, confirmation_height), confirmation_block_id = COALESCE(?, confirmation_block_id), confirmation_transaction_id = COALESCE(?, confirmation_transaction_id)`)
if err != nil {
return nil, fmt.Errorf("updateV2FileContractElements: failed to prepare last_contract_revision statement: %w", err)
}
Expand Down Expand Up @@ -131,14 +131,14 @@ func updateV2FileContractElements(tx *txn, revert bool, index types.ChainIndex,
// only update if it's the most recent revision which will come from
// running ForEachFileContractElement on the update
if lastRevision {
var encodedChainIndex []byte
var encodedConfirmationTransactionID []byte
var encodedHeight, encodedBlockID, encodedConfirmationTransactionID []byte
if confirmationTransactionID != nil {
encodedChainIndex = encode(index).([]byte)
encodedHeight = encode(index.Height).([]byte)
encodedBlockID = encode(index.ID).([]byte)
encodedConfirmationTransactionID = encode(*confirmationTransactionID).([]byte)
}

if _, err := revisionStmt.Exec(encode(fcID), dbID, encodedChainIndex, encodedConfirmationTransactionID, dbID, encodedChainIndex, encodedConfirmationTransactionID); err != nil {
if _, err := revisionStmt.Exec(encode(fcID), dbID, encodedHeight, encodedBlockID, encodedConfirmationTransactionID, dbID, encodedHeight, encodedBlockID, encodedConfirmationTransactionID); err != nil {
return fmt.Errorf("failed to update last revision number: %w", err)
}
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func updateV2FileContractElements(tx *txn, revert bool, index types.ChainIndex,
}

func updateV2FileContractIndices(tx *txn, revert bool, index types.ChainIndex, fces []explorer.V2FileContractUpdate) error {
resolutionIndexStmt, err := tx.Prepare(`UPDATE v2_last_contract_revision SET resolution_index = ?, resolution_transaction_id = ? WHERE contract_id = ?`)
resolutionIndexStmt, err := tx.Prepare(`UPDATE v2_last_contract_revision SET resolution_height = ?, resolution_block_id = ?, resolution_transaction_id = ? WHERE contract_id = ?`)
if err != nil {
return fmt.Errorf("updateV2FileContractIndices: failed to prepare resolution index statement: %w", err)
}
Expand All @@ -291,13 +291,13 @@ func updateV2FileContractIndices(tx *txn, revert bool, index types.ChainIndex, f

if revert {
if update.ResolutionTransactionID != nil {
if _, err := resolutionIndexStmt.Exec(nil, nil, encode(fcID)); err != nil {
if _, err := resolutionIndexStmt.Exec(nil, nil, nil, encode(fcID)); err != nil {
return fmt.Errorf("updateV2FileContractIndices: failed to update resolution index: %w", err)
}
}
} else {
if update.ResolutionTransactionID != nil {
if _, err := resolutionIndexStmt.Exec(encode(index), encode(update.ResolutionTransactionID), encode(fcID)); err != nil {
if _, err := resolutionIndexStmt.Exec(encode(index.Height), encode(index.ID), encode(update.ResolutionTransactionID), encode(fcID)); err != nil {
return fmt.Errorf("updateV2FileContractIndices: failed to update resolution index: %w", err)
}
}
Expand Down
Loading

0 comments on commit c34649d

Please sign in to comment.