From 419d1f13759e42cbd924b9682066bb41d3f13c4b Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Tue, 12 Nov 2024 13:44:03 -0500 Subject: [PATCH] inline FileContract --- explorer/types.go | 28 ++++++++++------------------ internal/testutil/check.go | 27 +++++++++++++-------------- persist/sqlite/consensus_test.go | 10 +++++----- persist/sqlite/contracts.go | 14 +++++++------- persist/sqlite/transactions.go | 12 ++++++------ 5 files changed, 41 insertions(+), 50 deletions(-) diff --git a/explorer/types.go b/explorer/types.go index 26586170..cfea0770 100644 --- a/explorer/types.go +++ b/explorer/types.go @@ -129,23 +129,6 @@ type ContractSiacoinOutput struct { types.SiacoinOutput } -// A FileContract is a storage agreement between a renter and a host. It -// serves as a bidirectional payment channel that resolves as either "valid" -// or "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"` - WindowEnd uint64 `json:"windowEnd"` - Payout types.Currency `json:"payout"` - ValidProofOutputs []ContractSiacoinOutput `json:"validProofOutputs"` - MissedProofOutputs []ContractSiacoinOutput `json:"missedProofOutputs"` - UnlockHash types.Hash256 `json:"unlockHash"` - RevisionNumber uint64 `json:"revisionNumber"` -} - // A ExtendedFileContract is a FileContract with added fields for // resolved/valid state, and when the transaction was confirmed and proved. type ExtendedFileContract struct { @@ -160,7 +143,16 @@ type ExtendedFileContract struct { ProofIndex *types.ChainIndex `json:"proofIndex"` ProofTransactionID *types.TransactionID `json:"proofTransactionID"` - FileContract + ID types.FileContractID `json:"id"` + Filesize uint64 `json:"filesize"` + FileMerkleRoot types.Hash256 `json:"fileMerkleRoot"` + WindowStart uint64 `json:"windowStart"` + WindowEnd uint64 `json:"windowEnd"` + Payout types.Currency `json:"payout"` + ValidProofOutputs []ContractSiacoinOutput `json:"validProofOutputs"` + MissedProofOutputs []ContractSiacoinOutput `json:"missedProofOutputs"` + UnlockHash types.Hash256 `json:"unlockHash"` + RevisionNumber uint64 `json:"revisionNumber"` } // A FileContractRevision is a FileContract with extra fields for revision diff --git a/internal/testutil/check.go b/internal/testutil/check.go index 1862ca80..e5cdf880 100644 --- a/internal/testutil/check.go +++ b/internal/testutil/check.go @@ -140,24 +140,23 @@ func CheckFC(t *testing.T, revision, resolved, valid bool, expected types.FileCo Equal(t, "resolved state", resolved, got.Resolved) Equal(t, "valid state", valid, got.Valid) - gotFC := got.FileContract - Equal(t, "filesize", expected.Filesize, gotFC.Filesize) - Equal(t, "file merkle root", expected.FileMerkleRoot, gotFC.FileMerkleRoot) - Equal(t, "window start", expected.WindowStart, gotFC.WindowStart) - Equal(t, "window end", expected.WindowEnd, gotFC.WindowEnd) + Equal(t, "filesize", expected.Filesize, got.Filesize) + Equal(t, "file merkle root", expected.FileMerkleRoot, got.FileMerkleRoot) + Equal(t, "window start", expected.WindowStart, got.WindowStart) + Equal(t, "window end", expected.WindowEnd, got.WindowEnd) if !revision { - Equal(t, "payout", expected.Payout, gotFC.Payout) + Equal(t, "payout", expected.Payout, got.Payout) } - Equal(t, "unlock hash", expected.UnlockHash, gotFC.UnlockHash) - Equal(t, "revision number", expected.RevisionNumber, gotFC.RevisionNumber) - Equal(t, "valid proof outputs", len(expected.ValidProofOutputs), len(gotFC.ValidProofOutputs)) + Equal(t, "unlock hash", expected.UnlockHash, got.UnlockHash) + Equal(t, "revision number", expected.RevisionNumber, got.RevisionNumber) + Equal(t, "valid proof outputs", len(expected.ValidProofOutputs), len(got.ValidProofOutputs)) for i := range expected.ValidProofOutputs { - Equal(t, "valid proof output address", expected.ValidProofOutputs[i].Address, gotFC.ValidProofOutputs[i].Address) - Equal(t, "valid proof output value", expected.ValidProofOutputs[i].Value, gotFC.ValidProofOutputs[i].Value) + Equal(t, "valid proof output address", expected.ValidProofOutputs[i].Address, got.ValidProofOutputs[i].Address) + Equal(t, "valid proof output value", expected.ValidProofOutputs[i].Value, got.ValidProofOutputs[i].Value) } - Equal(t, "missed proof outputs", len(expected.MissedProofOutputs), len(gotFC.MissedProofOutputs)) + Equal(t, "missed proof outputs", len(expected.MissedProofOutputs), len(got.MissedProofOutputs)) for i := range expected.MissedProofOutputs { - Equal(t, "missed proof output address", expected.MissedProofOutputs[i].Address, gotFC.MissedProofOutputs[i].Address) - Equal(t, "missed proof output value", expected.MissedProofOutputs[i].Value, gotFC.MissedProofOutputs[i].Value) + Equal(t, "missed proof output address", expected.MissedProofOutputs[i].Address, got.MissedProofOutputs[i].Address) + Equal(t, "missed proof output value", expected.MissedProofOutputs[i].Value, got.MissedProofOutputs[i].Value) } } diff --git a/persist/sqlite/consensus_test.go b/persist/sqlite/consensus_test.go index 8efc4e3f..92e0d693 100644 --- a/persist/sqlite/consensus_test.go +++ b/persist/sqlite/consensus_test.go @@ -87,24 +87,24 @@ func CheckFCRevisions(t *testing.T, confirmationIndex types.ChainIndex, confirma testutil.Equal(t, "number of revisions", len(revisionNumbers), len(fcs)) for i := range revisionNumbers { - testutil.Equal(t, "revision number", revisionNumbers[i], fcs[i].FileContract.RevisionNumber) + testutil.Equal(t, "revision number", revisionNumbers[i], fcs[i].RevisionNumber) testutil.Equal(t, "confirmation index", confirmationIndex, *fcs[i].ConfirmationIndex) testutil.Equal(t, "confirmation transaction ID", confirmationTransactionID, *fcs[i].ConfirmationTransactionID) - testutil.Equal(t, "valid proof outputs", len(valid), len(fcs[i].FileContract.ValidProofOutputs)) + testutil.Equal(t, "valid proof outputs", len(valid), len(fcs[i].ValidProofOutputs)) for j := range valid { expected := valid[j] - got := fcs[i].FileContract.ValidProofOutputs[j] + got := fcs[i].ValidProofOutputs[j] testutil.Equal(t, "id", fcs[i].ID.ValidOutputID(j), got.ID) testutil.Equal(t, "value", expected.Value, got.Value) testutil.Equal(t, "address", expected.Address, got.Address) } - testutil.Equal(t, "missed proof outputs", len(missed), len(fcs[i].FileContract.MissedProofOutputs)) + testutil.Equal(t, "missed proof outputs", len(missed), len(fcs[i].MissedProofOutputs)) for j := range missed { expected := missed[j] - got := fcs[i].FileContract.MissedProofOutputs[j] + got := fcs[i].MissedProofOutputs[j] testutil.Equal(t, "id", fcs[i].ID.MissedOutputID(j), got.ID) testutil.Equal(t, "value", expected.Value, got.Value) diff --git a/persist/sqlite/contracts.go b/persist/sqlite/contracts.go index 495460e6..c6b14fab 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 confirmationIndex, proofIndex types.ChainIndex var confirmationTransactionID, proofTransactionID types.TransactionID - 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)) + err = s.Scan(&contractID, decode(&fc.ID), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decodeNull(&confirmationIndex), decodeNull(&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)) if confirmationIndex != (types.ChainIndex{}) { fc.ConfirmationIndex = &confirmationIndex @@ -70,8 +70,8 @@ func (s *Store) Contracts(ids []types.FileContractID) (result []explorer.Extende } for contractID, output := range proofOutputs { fc := idContract[contractID] - fc.FileContract.ValidProofOutputs = output.valid - fc.FileContract.MissedProofOutputs = output.missed + fc.ValidProofOutputs = output.valid + fc.MissedProofOutputs = output.missed result = append(result, fc) } @@ -127,8 +127,8 @@ func (s *Store) ContractRevisions(id types.FileContractID) (revisions []explorer return fmt.Errorf("missing proof outputs for contract %v", contractIDs[i]) } revisions[i] = revision.FileContract - revisions[i].FileContract.ValidProofOutputs = output.valid - revisions[i].FileContract.MissedProofOutputs = output.missed + revisions[i].ValidProofOutputs = output.valid + revisions[i].MissedProofOutputs = output.missed } if len(revisions) == 0 { @@ -170,8 +170,8 @@ func (s *Store) ContractsKey(key types.PublicKey) (result []explorer.ExtendedFil } for contractID, output := range proofOutputs { fc := idContract[contractID] - fc.FileContract.ValidProofOutputs = output.valid - fc.FileContract.MissedProofOutputs = output.missed + fc.ValidProofOutputs = output.valid + fc.MissedProofOutputs = output.missed result = append(result, fc) } diff --git a/persist/sqlite/transactions.go b/persist/sqlite/transactions.go index c71a78e1..cc874662 100644 --- a/persist/sqlite/transactions.go +++ b/persist/sqlite/transactions.go @@ -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), &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.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) } @@ -334,8 +334,8 @@ ORDER BY ts.transaction_order ASC` } for contractID, output := range proofOutputs { index := contractTransaction[contractID] - result[index.txnID][index.transactionOrder].FileContract.ValidProofOutputs = output.valid - result[index.txnID][index.transactionOrder].FileContract.MissedProofOutputs = output.missed + result[index.txnID][index.transactionOrder].ValidProofOutputs = output.valid + result[index.txnID][index.transactionOrder].MissedProofOutputs = output.missed } return result, nil @@ -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), &fc.Resolved, &fc.Valid, decode(&fc.TransactionID), decode(&fc.ExtendedFileContract.FileContract.Filesize), decode(&fc.ExtendedFileContract.FileContract.FileMerkleRoot), decode(&fc.ExtendedFileContract.FileContract.WindowStart), decode(&fc.ExtendedFileContract.FileContract.WindowEnd), decode(&fc.ExtendedFileContract.FileContract.Payout), decode(&fc.ExtendedFileContract.FileContract.UnlockHash), decode(&fc.ExtendedFileContract.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.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) } @@ -395,8 +395,8 @@ ORDER BY ts.transaction_order ASC` } for contractID, output := range proofOutputs { index := contractTransaction[contractID] - result[index.txnID][index.transactionOrder].ExtendedFileContract.FileContract.ValidProofOutputs = output.valid - result[index.txnID][index.transactionOrder].ExtendedFileContract.FileContract.MissedProofOutputs = output.missed + result[index.txnID][index.transactionOrder].ExtendedFileContract.ValidProofOutputs = output.valid + result[index.txnID][index.transactionOrder].ExtendedFileContract.MissedProofOutputs = output.missed } return result, nil