From 6c89008412ff141b5ad0c459aa57bb25aba3db07 Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Wed, 6 Nov 2024 16:50:02 -0500 Subject: [PATCH] use simplified type instead of types.FileContractElement --- explorer/types.go | 11 ++--------- persist/sqlite/contracts.go | 8 ++++---- persist/sqlite/transactions.go | 8 ++++---- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/explorer/types.go b/explorer/types.go index dea4ee43..cd04463f 100644 --- a/explorer/types.go +++ b/explorer/types.go @@ -133,6 +133,7 @@ type ContractSiacoinOutput struct { // "missed" depending on whether a valid StorageProof is submitted for the // contract. type FileContract struct { + ID types.FileContractID `json:"id"` Filesize uint64 `json:"filesize"` FileMerkleRoot types.Hash256 `json:"fileMerkleRoot"` WindowStart uint64 `json:"windowStart"` @@ -144,14 +145,6 @@ type FileContract struct { RevisionNumber uint64 `json:"revisionNumber"` } -// A FileContractElement is a record of a FileContract within the state -// accumulator. -type FileContractElement struct { - ID types.FileContractID `json:"id"` - StateElement types.StateElement `json:"stateElement"` - FileContract FileContract `json:"fileContract"` -} - // A EnhancedFileContract is a FileContractElement with added fields for // resolved/valid state, and when the transaction was confirmed and proved. type EnhancedFileContract struct { @@ -166,7 +159,7 @@ type EnhancedFileContract struct { ProofIndex *types.ChainIndex `json:"proofIndex"` ProofTransactionID *types.TransactionID `json:"proofTransactionID"` - FileContractElement + FileContract } // A FileContractRevision is a FileContract with extra fields for revision diff --git a/persist/sqlite/contracts.go b/persist/sqlite/contracts.go index b13d8836..51b743a4 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.EnhancedFileContract, err error) { var confirmationIndex, proofIndex types.ChainIndex var confirmationTransactionID, proofTransactionID types.TransactionID - err = s.Scan(&contractID, decode(&fc.ID), decode(&fc.StateElement.LeafIndex), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decodeNull(&confirmationIndex), decodeNull(&confirmationTransactionID), decodeNull(&proofIndex), decodeNull(&proofTransactionID), decode(&fc.FileContract.Filesize), decode(&fc.FileContract.FileMerkleRoot), decode(&fc.FileContract.WindowStart), decode(&fc.FileContract.WindowEnd), decode(&fc.FileContract.Payout), decode(&fc.FileContract.UnlockHash), decode(&fc.FileContract.RevisionNumber)) + err = s.Scan(&contractID, decode(&fc.ID), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decodeNull(&confirmationIndex), decodeNull(&confirmationTransactionID), decodeNull(&proofIndex), decodeNull(&proofTransactionID), decode(&fc.FileContract.Filesize), decode(&fc.FileContract.FileMerkleRoot), decode(&fc.FileContract.WindowStart), decode(&fc.FileContract.WindowEnd), decode(&fc.FileContract.Payout), decode(&fc.FileContract.UnlockHash), decode(&fc.FileContract.RevisionNumber)) if confirmationIndex != (types.ChainIndex{}) { fc.ConfirmationIndex = &confirmationIndex @@ -39,7 +39,7 @@ func scanFileContract(s scanner) (contractID int64, fc explorer.EnhancedFileCont // Contracts implements explorer.Store. func (s *Store) Contracts(ids []types.FileContractID) (result []explorer.EnhancedFileContract, err error) { err = s.transaction(func(tx *txn) error { - query := `SELECT fc1.id, fc1.contract_id, fc1.leaf_index, 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_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 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)) + `)` @@ -84,7 +84,7 @@ func (s *Store) Contracts(ids []types.FileContractID) (result []explorer.Enhance // ContractRevisions implements explorer.Store. func (s *Store) ContractRevisions(id types.FileContractID) (revisions []explorer.EnhancedFileContract, err error) { err = s.transaction(func(tx *txn) error { - query := `SELECT fc.id, fc.contract_id, fc.leaf_index, 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_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 FROM file_contract_elements fc JOIN last_contract_revision rev ON (rev.contract_id = fc.contract_id) WHERE fc.contract_id = ? @@ -142,7 +142,7 @@ func (s *Store) ContractRevisions(id types.FileContractID) (revisions []explorer // ContractsKey implements explorer.Store. func (s *Store) ContractsKey(key types.PublicKey) (result []explorer.EnhancedFileContract, err error) { err = s.transaction(func(tx *txn) error { - query := `SELECT fc1.id, fc1.contract_id, fc1.leaf_index, 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_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 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/transactions.go b/persist/sqlite/transactions.go index 92902e6e..de383ff5 100644 --- a/persist/sqlite/transactions.go +++ b/persist/sqlite/transactions.go @@ -282,7 +282,7 @@ type contractOrder struct { // transactionFileContracts returns the file contracts for each transaction. func transactionFileContracts(tx *txn, txnIDs []int64) (map[int64][]explorer.EnhancedFileContract, 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.leaf_index, 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_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 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) @@ -305,7 +305,7 @@ ORDER BY ts.transaction_order ASC` var confirmationIndex, proofIndex types.ChainIndex var confirmationTransactionID, proofTransactionID types.TransactionID - if err := rows.Scan(&txnID, &contractID, decodeNull(&confirmationIndex), decodeNull(&confirmationTransactionID), decodeNull(&proofIndex), decodeNull(&proofTransactionID), decode(&fc.ID), decode(&fc.StateElement.LeafIndex), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decode(&fc.FileContract.Filesize), decode(&fc.FileContract.FileMerkleRoot), decode(&fc.FileContract.WindowStart), decode(&fc.FileContract.WindowEnd), decode(&fc.FileContract.Payout), decode(&fc.FileContract.UnlockHash), decode(&fc.FileContract.RevisionNumber)); err != nil { + if err := rows.Scan(&txnID, &contractID, decodeNull(&confirmationIndex), decodeNull(&confirmationTransactionID), decodeNull(&proofIndex), decodeNull(&proofTransactionID), decode(&fc.ID), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decode(&fc.FileContract.Filesize), decode(&fc.FileContract.FileMerkleRoot), decode(&fc.FileContract.WindowStart), decode(&fc.FileContract.WindowEnd), decode(&fc.FileContract.Payout), decode(&fc.FileContract.UnlockHash), decode(&fc.FileContract.RevisionNumber)); err != nil { return nil, fmt.Errorf("failed to scan file contract: %w", err) } @@ -343,7 +343,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.leaf_index, 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_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 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) @@ -366,7 +366,7 @@ ORDER BY ts.transaction_order ASC` var confirmationIndex, proofIndex types.ChainIndex var confirmationTransactionID, proofTransactionID types.TransactionID - if err := rows.Scan(&txnID, &contractID, decodeNull(&confirmationIndex), decodeNull(&confirmationTransactionID), decodeNull(&proofIndex), decodeNull(&proofTransactionID), decode(&fc.ParentID), decode(&fc.UnlockConditions), decode(&fc.ID), decode(&fc.StateElement.LeafIndex), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decode(&fc.EnhancedFileContract.FileContract.Filesize), decode(&fc.EnhancedFileContract.FileContract.FileMerkleRoot), decode(&fc.EnhancedFileContract.FileContract.WindowStart), decode(&fc.EnhancedFileContract.FileContract.WindowEnd), decode(&fc.EnhancedFileContract.FileContract.Payout), decode(&fc.EnhancedFileContract.FileContract.UnlockHash), decode(&fc.EnhancedFileContract.FileContract.RevisionNumber)); err != nil { + if err := rows.Scan(&txnID, &contractID, decodeNull(&confirmationIndex), decodeNull(&confirmationTransactionID), decodeNull(&proofIndex), decodeNull(&proofTransactionID), decode(&fc.ParentID), decode(&fc.UnlockConditions), decode(&fc.ID), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decode(&fc.EnhancedFileContract.FileContract.Filesize), decode(&fc.EnhancedFileContract.FileContract.FileMerkleRoot), decode(&fc.EnhancedFileContract.FileContract.WindowStart), decode(&fc.EnhancedFileContract.FileContract.WindowEnd), decode(&fc.EnhancedFileContract.FileContract.Payout), decode(&fc.EnhancedFileContract.FileContract.UnlockHash), decode(&fc.EnhancedFileContract.FileContract.RevisionNumber)); err != nil { return nil, fmt.Errorf("failed to scan file contract: %w", err) }