Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ContractRevisions bug where confirmation/proof index were not set in some cases #111

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading