diff --git a/persist/sqlite/consensus.go b/persist/sqlite/consensus.go index b88e5d3..ee65dbb 100644 --- a/persist/sqlite/consensus.go +++ b/persist/sqlite/consensus.go @@ -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) } @@ -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) } } @@ -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) } @@ -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) } } diff --git a/persist/sqlite/contracts.go b/persist/sqlite/contracts.go index 036c034..8428c03 100644 --- a/persist/sqlite/contracts.go +++ b/persist/sqlite/contracts.go @@ -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 @@ -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)) + `)` @@ -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 = ? @@ -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 = ?` diff --git a/persist/sqlite/init.sql b/persist/sqlite/init.sql index e7c0278..2741689 100644 --- a/persist/sqlite/init.sql +++ b/persist/sqlite/init.sql @@ -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 @@ -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 diff --git a/persist/sqlite/transactions.go b/persist/sqlite/transactions.go index a0499a7..58fe5d1 100644 --- a/persist/sqlite/transactions.go +++ b/persist/sqlite/transactions.go @@ -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 @@ -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) } @@ -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 @@ -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) } diff --git a/persist/sqlite/v2consensus.go b/persist/sqlite/v2consensus.go index 7006877..d6d8413 100644 --- a/persist/sqlite/v2consensus.go +++ b/persist/sqlite/v2consensus.go @@ -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) } @@ -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) } } @@ -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) } @@ -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) } } diff --git a/persist/sqlite/v2contracts.go b/persist/sqlite/v2contracts.go index 81b2c68..c7e4944 100644 --- a/persist/sqlite/v2contracts.go +++ b/persist/sqlite/v2contracts.go @@ -12,7 +12,7 @@ func scanV2FileContract(s scanner) (fce explorer.V2FileContract, err error) { var resolutionTransactionID types.TransactionID fc := &fce.V2FileContractElement.V2FileContract - if err = s.Scan(decode(&fce.TransactionID), decode(&fce.ConfirmationIndex), decode(&fce.ConfirmationTransactionID), decodeNull(&resolutionIndex), decodeNull(&resolutionTransactionID), decode(&fce.V2FileContractElement.ID), decode(&fce.V2FileContractElement.StateElement.LeafIndex), decode(&fc.Capacity), decode(&fc.Filesize), decode(&fc.FileMerkleRoot), decode(&fc.ProofHeight), decode(&fc.ExpirationHeight), decode(&fc.RenterOutput.Address), decode(&fc.RenterOutput.Value), decode(&fc.HostOutput.Address), decode(&fc.HostOutput.Value), decode(&fc.MissedHostValue), decode(&fc.TotalCollateral), decode(&fc.RenterPublicKey), decode(&fc.HostPublicKey), decode(&fc.RevisionNumber), decode(&fc.RenterSignature), decode(&fc.HostSignature)); err != nil { + if err = s.Scan(decode(&fce.TransactionID), decode(&fce.ConfirmationIndex.Height), decode(&fce.ConfirmationIndex.ID), decode(&fce.ConfirmationTransactionID), decodeNull(&resolutionIndex.Height), decodeNull(&resolutionIndex.ID), decodeNull(&resolutionTransactionID), decode(&fce.V2FileContractElement.ID), decode(&fce.V2FileContractElement.StateElement.LeafIndex), decode(&fc.Capacity), decode(&fc.Filesize), decode(&fc.FileMerkleRoot), decode(&fc.ProofHeight), decode(&fc.ExpirationHeight), decode(&fc.RenterOutput.Address), decode(&fc.RenterOutput.Value), decode(&fc.HostOutput.Address), decode(&fc.HostOutput.Value), decode(&fc.MissedHostValue), decode(&fc.TotalCollateral), decode(&fc.RenterPublicKey), decode(&fc.HostPublicKey), decode(&fc.RevisionNumber), decode(&fc.RenterSignature), decode(&fc.HostSignature)); err != nil { return } @@ -29,7 +29,7 @@ func scanV2FileContract(s scanner) (fce explorer.V2FileContract, err error) { // V2Contracts implements explorer.Store. func (s *Store) V2Contracts(ids []types.FileContractID) (result []explorer.V2FileContract, err error) { err = s.transaction(func(tx *txn) error { - stmt, err := tx.Prepare(`SELECT fc.transaction_id, rev.confirmation_index, rev.confirmation_transaction_id, rev.resolution_index, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature + stmt, err := tx.Prepare(`SELECT fc.transaction_id, rev.confirmation_height, rev.confirmation_block_id, rev.confirmation_transaction_id, rev.resolution_height, rev.resolution_block_id, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature FROM v2_last_contract_revision rev INNER JOIN v2_file_contract_elements fc ON rev.contract_element_id = fc.id WHERE rev.contract_id = ? @@ -56,7 +56,7 @@ WHERE rev.contract_id = ? // V2ContractRevisions implements explorer.Store. func (s *Store) V2ContractRevisions(id types.FileContractID) (revisions []explorer.V2FileContract, err error) { err = s.transaction(func(tx *txn) error { - query := `SELECT fc.transaction_id, rev.confirmation_index, rev.confirmation_transaction_id, rev.resolution_index, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature + query := `SELECT fc.transaction_id, rev.confirmation_height, rev.confirmation_block_id, rev.confirmation_transaction_id, rev.resolution_height, rev.resolution_block_id, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature FROM v2_file_contract_elements fc INNER JOIN v2_last_contract_revision rev ON rev.contract_id = fc.contract_id WHERE fc.contract_id = ? @@ -89,11 +89,11 @@ ORDER BY fc.revision_number ASC func (s *Store) V2ContractsKey(key types.PublicKey) (result []explorer.V2FileContract, err error) { err = s.transaction(func(tx *txn) error { encoded := encode(key) - rows, err := tx.Query(`SELECT fc.transaction_id, rev.confirmation_index, rev.confirmation_transaction_id, rev.resolution_index, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature + rows, err := tx.Query(`SELECT fc.transaction_id, rev.confirmation_height, rev.confirmation_block_id, rev.confirmation_transaction_id, rev.resolution_height, rev.resolution_block_id, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature FROM v2_last_contract_revision rev INNER JOIN v2_file_contract_elements fc ON rev.contract_element_id = fc.id WHERE fc.renter_public_key = ? OR fc.host_public_key = ? -ORDER BY rev.confirmation_index ASC +ORDER BY rev.confirmation_height ASC `, encoded, encoded) if err != nil { return err diff --git a/persist/sqlite/v2transactions.go b/persist/sqlite/v2transactions.go index 4d290cf..59f18a3 100644 --- a/persist/sqlite/v2transactions.go +++ b/persist/sqlite/v2transactions.go @@ -322,7 +322,7 @@ ORDER BY ts.transaction_order ASC`) // fillV2TransactionFileContracts fills in the file contracts for each // transaction. func fillV2TransactionFileContracts(tx *txn, dbIDs []int64, txns []explorer.V2Transaction) error { - stmt, err := tx.Prepare(`SELECT fc.transaction_id, rev.confirmation_index, rev.confirmation_transaction_id, rev.resolution_index, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature + stmt, err := tx.Prepare(`SELECT fc.transaction_id, rev.confirmation_height, rev.confirmation_block_id, rev.confirmation_transaction_id, rev.resolution_height, rev.resolution_block_id, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature FROM v2_file_contract_elements fc INNER JOIN v2_transaction_file_contracts ts ON (ts.contract_id = fc.id) INNER JOIN v2_last_contract_revision rev ON (rev.contract_id = fc.contract_id) @@ -361,7 +361,7 @@ ORDER BY ts.transaction_order ASC`) // fillV2TransactionFileContractRevisions fills in the file contract revisions // for each transaction. func fillV2TransactionFileContractRevisions(tx *txn, dbIDs []int64, txns []explorer.V2Transaction) error { - parentStmt, err := tx.Prepare(`SELECT fc.transaction_id, rev.confirmation_index, rev.confirmation_transaction_id, rev.resolution_index, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature + parentStmt, err := tx.Prepare(`SELECT fc.transaction_id, rev.confirmation_height, rev.confirmation_block_id, rev.confirmation_transaction_id, rev.resolution_height, rev.resolution_block_id, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature FROM v2_file_contract_elements fc INNER JOIN v2_transaction_file_contract_revisions ts ON (ts.parent_contract_id = fc.id) INNER JOIN v2_last_contract_revision rev ON (rev.contract_id = fc.contract_id) @@ -372,7 +372,7 @@ ORDER BY ts.transaction_order ASC`) } defer parentStmt.Close() - revisionStmt, err := tx.Prepare(`SELECT fc.transaction_id, rev.confirmation_index, rev.confirmation_transaction_id, rev.resolution_index, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature + revisionStmt, err := tx.Prepare(`SELECT fc.transaction_id, rev.confirmation_height, rev.confirmation_block_id, rev.confirmation_transaction_id, rev.resolution_height, rev.resolution_block_id, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature FROM v2_file_contract_elements fc INNER JOIN v2_transaction_file_contract_revisions ts ON (ts.revision_contract_id = fc.id) INNER JOIN v2_last_contract_revision rev ON (rev.contract_id = fc.contract_id) @@ -448,7 +448,7 @@ func fillV2TransactionFileContractResolutions(tx *txn, dbIDs []int64, txns []exp defer consolidatedStmt.Close() // get a v2 FC by id - fcStmt, err := tx.Prepare(`SELECT fc.transaction_id, rev.confirmation_index, rev.confirmation_transaction_id, rev.resolution_index, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature + fcStmt, err := tx.Prepare(`SELECT fc.transaction_id, rev.confirmation_height, rev.confirmation_block_id, rev.confirmation_transaction_id, rev.resolution_height, rev.resolution_block_id, rev.resolution_transaction_id, fc.contract_id, fc.leaf_index, fc.capacity, fc.filesize, fc.file_merkle_root, fc.proof_height, fc.expiration_height, fc.renter_output_address, fc.renter_output_value, fc.host_output_address, fc.host_output_value, fc.missed_host_value, fc.total_collateral, fc.renter_public_key, fc.host_public_key, fc.revision_number, fc.renter_signature, fc.host_signature FROM v2_file_contract_elements fc INNER JOIN v2_last_contract_revision rev ON (rev.contract_id = fc.contract_id) WHERE fc.id = ?`)