Skip to content

Commit

Permalink
inline FileContract
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Nov 12, 2024
1 parent f5db0fe commit 419d1f1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 50 deletions.
28 changes: 10 additions & 18 deletions explorer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
27 changes: 13 additions & 14 deletions internal/testutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
10 changes: 5 additions & 5 deletions persist/sqlite/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 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 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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
12 changes: 6 additions & 6 deletions persist/sqlite/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 419d1f1

Please sign in to comment.