Skip to content

Commit

Permalink
Merge pull request #111 from SiaFoundation/fix-revisions
Browse files Browse the repository at this point in the history
Fix ContractRevisions bug where confirmation/proof index were not set in some cases
  • Loading branch information
n8maninger authored Oct 9, 2024
2 parents 4e4ec5a + 1b3d82f commit 9ccf6eb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
27 changes: 19 additions & 8 deletions persist/sqlite/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ func CheckChainIndices(t *testing.T, db explorer.Store, txnID types.TransactionI
}

// CheckFCRevisions checks that the revision numbers for the file contracts match.
func CheckFCRevisions(t *testing.T, revisionNumbers []uint64, fcs []explorer.FileContract) {
func CheckFCRevisions(t *testing.T, confirmationIndex types.ChainIndex, confirmationTransactionID types.TransactionID, revisionNumbers []uint64, fcs []explorer.FileContract) {
t.Helper()

testutil.Equal(t, "number of revisions", len(revisionNumbers), len(fcs))
for i := range revisionNumbers {
testutil.Equal(t, "confirmation index", confirmationIndex, *fcs[i].ConfirmationIndex)
testutil.Equal(t, "confirmation transaction ID", confirmationTransactionID, *fcs[i].ConfirmationTransactionID)
testutil.Equal(t, "revision number", revisionNumbers[i], fcs[i].FileContract.RevisionNumber)
}
}
Expand Down Expand Up @@ -572,6 +574,9 @@ func TestFileContract(t *testing.T) {
}
syncDB(t, db, cm)

confirmationIndex := cm.Tip()
confirmationTransactionID := txn.ID()

{
dbFCs, err := db.Contracts([]types.FileContractID{fcID})
if err != nil {
Expand All @@ -588,7 +593,7 @@ func TestFileContract(t *testing.T) {
if err != nil {
t.Fatal(err)
}
CheckFCRevisions(t, []uint64{0}, dbFCs)
CheckFCRevisions(t, confirmationIndex, confirmationTransactionID, []uint64{0}, dbFCs)
}

{
Expand Down Expand Up @@ -668,7 +673,7 @@ func TestFileContract(t *testing.T) {
if err != nil {
t.Fatal(err)
}
CheckFCRevisions(t, []uint64{0, 1}, dbFCs)
CheckFCRevisions(t, confirmationIndex, confirmationTransactionID, []uint64{0, 1}, dbFCs)
}

{
Expand Down Expand Up @@ -838,6 +843,9 @@ func TestEphemeralFileContract(t *testing.T) {
}
syncDB(t, db, cm)

confirmationIndex := cm.Tip()
confirmationTransactionID := txn.ID()

CheckMetrics(t, db, cm, explorer.Metrics{
TotalHosts: 0,
ActiveContracts: 1,
Expand Down Expand Up @@ -874,7 +882,7 @@ func TestEphemeralFileContract(t *testing.T) {
if err != nil {
t.Fatal(err)
}
CheckFCRevisions(t, []uint64{0, 1}, dbFCs)
CheckFCRevisions(t, confirmationIndex, confirmationTransactionID, []uint64{0, 1}, dbFCs)
}

{
Expand Down Expand Up @@ -954,7 +962,7 @@ func TestEphemeralFileContract(t *testing.T) {
if err != nil {
t.Fatal(err)
}
CheckFCRevisions(t, []uint64{0, 1, 2, 3}, dbFCs)
CheckFCRevisions(t, confirmationIndex, confirmationTransactionID, []uint64{0, 1, 2, 3}, dbFCs)
}

{
Expand Down Expand Up @@ -2212,6 +2220,9 @@ func TestMultipleReorgFileContract(t *testing.T) {
}
syncDB(t, db, cm)

confirmationIndex := cm.Tip()
confirmationTransactionID := txn.ID()

CheckMetrics(t, db, cm, explorer.Metrics{
TotalHosts: 0,
ActiveContracts: 1,
Expand All @@ -2235,7 +2246,7 @@ func TestMultipleReorgFileContract(t *testing.T) {
if err != nil {
t.Fatal(err)
}
CheckFCRevisions(t, []uint64{0}, dbFCs)
CheckFCRevisions(t, confirmationIndex, confirmationTransactionID, []uint64{0}, dbFCs)
}

{
Expand Down Expand Up @@ -2315,7 +2326,7 @@ func TestMultipleReorgFileContract(t *testing.T) {
if err != nil {
t.Fatal(err)
}
CheckFCRevisions(t, []uint64{0, 1}, dbFCs)
CheckFCRevisions(t, confirmationIndex, confirmationTransactionID, []uint64{0, 1}, dbFCs)
}

{
Expand Down Expand Up @@ -2371,7 +2382,7 @@ func TestMultipleReorgFileContract(t *testing.T) {
if err != nil {
t.Fatal(err)
}
CheckFCRevisions(t, []uint64{0}, dbFCs)
CheckFCRevisions(t, confirmationIndex, confirmationTransactionID, []uint64{0}, dbFCs)
}

// storage utilization should be back to testutil.ContractFilesize instead of
Expand Down
2 changes: 1 addition & 1 deletion persist/sqlite/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (s *Store) ContractRevisions(id types.FileContractID) (revisions []explorer
err = s.transaction(func(tx *txn) error {
query := `SELECT fc.id, fc.contract_id, fc.leaf_index, fc.resolved, fc.valid, 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
LEFT JOIN last_contract_revision rev ON (rev.contract_element_id = fc.id)
JOIN last_contract_revision rev ON (rev.contract_id = fc.contract_id)
WHERE fc.contract_id = ?
ORDER BY fc.revision_number ASC`
rows, err := tx.Query(query, encode(id))
Expand Down

0 comments on commit 9ccf6eb

Please sign in to comment.