From 815ff2313509c4ebe035cc73c7cc9d4da89f6bba Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Fri, 28 Jul 2023 08:27:18 -0500 Subject: [PATCH 1/3] sqlite: always attempt to resolve contracts during proof window --- persist/sqlite/contracts.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/persist/sqlite/contracts.go b/persist/sqlite/contracts.go index 7688c402..a060ac94 100644 --- a/persist/sqlite/contracts.go +++ b/persist/sqlite/contracts.go @@ -778,8 +778,8 @@ func revisionContractActions(tx txn, height uint64) (actions []contractAction, _ func resolveContractActions(tx txn, height uint64) (actions []contractAction, _ error) { // formation confirmed, resolution not confirmed, status active, in proof window - const query = `SELECT contract_id FROM contracts WHERE formation_confirmed=true AND resolution_height IS NULL AND window_start <= $1 AND window_end > $1 AND contract_status=$2` - rows, err := tx.Query(query, height, contracts.ContractStatusActive) + const query = `SELECT contract_id FROM contracts WHERE formation_confirmed=true AND resolution_height IS NULL AND window_start <= $1 AND window_end > $1` + rows, err := tx.Query(query, height) if err != nil { return nil, fmt.Errorf("failed to query contracts: %w", err) } From 81f67c53b86035818980fd0fb522e5ea07fafefe Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Fri, 28 Jul 2023 08:32:07 -0500 Subject: [PATCH 2/3] contracts: increase log level for contract state changes --- host/contracts/manager.go | 6 +++--- persist/sqlite/contracts.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/host/contracts/manager.go b/host/contracts/manager.go index 097feefc..b225eb80 100644 --- a/host/contracts/manager.go +++ b/host/contracts/manager.go @@ -352,7 +352,7 @@ func (cm *ContractManager) ProcessConsensusChange(cc modules.ConsensusChange) { return fmt.Errorf("failed to apply formation: %w", err) } - log.Debug("contract formation confirmed", zap.Stringer("contractID", applied.id), zap.Stringer("block", applied.index)) + log.Info("contract formation confirmed", zap.Stringer("contractID", applied.id), zap.Stringer("block", applied.index)) cm.alerts.Dismiss(types.Hash256(applied.id)) // dismiss any lifecycle alerts for this contract } @@ -365,7 +365,7 @@ func (cm *ContractManager) ProcessConsensusChange(cc modules.ConsensusChange) { return fmt.Errorf("failed to apply revision: %w", err) } - log.Debug("contract revision confirmed", zap.Stringer("contractID", applied.ParentID), zap.Uint64("revisionNumber", applied.RevisionNumber)) + log.Info("contract revision confirmed", zap.Stringer("contractID", applied.ParentID), zap.Uint64("revisionNumber", applied.RevisionNumber)) cm.alerts.Dismiss(types.Hash256(applied.ParentID)) // dismiss any lifecycle alerts for this contract } @@ -378,7 +378,7 @@ func (cm *ContractManager) ProcessConsensusChange(cc modules.ConsensusChange) { return fmt.Errorf("failed to apply proof: %w", err) } - log.Debug("contract resolution confirmed", zap.Stringer("contractID", applied.id), zap.Stringer("block", applied.index)) + log.Info("contract resolution confirmed", zap.Stringer("contractID", applied.id), zap.Stringer("block", applied.index)) cm.alerts.Dismiss(types.Hash256(applied.id)) // dismiss any lifecycle alerts for this contract } return nil diff --git a/persist/sqlite/contracts.go b/persist/sqlite/contracts.go index a060ac94..607a9c7c 100644 --- a/persist/sqlite/contracts.go +++ b/persist/sqlite/contracts.go @@ -70,9 +70,9 @@ func (u *updateContractsTxn) ConfirmFormation(id types.FileContractID) error { } } - // set the contract status to active only if the contract is pending or rejected - // if the contract is successful or failed, leave it as is. - if contract.Status == contracts.ContractStatusPending || contract.Status == contracts.ContractStatusRejected { + // skip updating the status for contracts that are already marked as + // successful or failed + if contract.Status != contracts.ContractStatusSuccessful && contract.Status != contracts.ContractStatusFailed { if err := setContractStatus(u.tx, id, contracts.ContractStatusActive); err != nil { return fmt.Errorf("failed to set contract status to active: %w", err) } From 0bc226cbd83ed13eba32fd33c46a7f84490075e8 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Mon, 31 Jul 2023 07:03:31 -0500 Subject: [PATCH 3/3] sqlite: explicitly defer rollback --- persist/sqlite/store.go | 8 ++------ persist/sqlite/store_test.go | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/persist/sqlite/store.go b/persist/sqlite/store.go index 6b30914c..3ac4920f 100644 --- a/persist/sqlite/store.go +++ b/persist/sqlite/store.go @@ -99,11 +99,7 @@ func (s *Store) transaction(ctx context.Context, fn func(txn) error) error { if err != nil { return fmt.Errorf("failed to begin transaction: %w", err) } - defer func() { - if err != nil { - tx.Rollback() - } - }() + defer tx.Rollback() ltx := &loggedTxn{ Tx: tx, log: s.log.Named("transaction"), @@ -144,7 +140,7 @@ func (s *Store) Close() error { func sqliteFilepath(fp string) string { params := []string{ - "_busy_timeout=5000", // 5s + "_busy_timeout=30000", // 30s "_foreign_keys=true", "_journal_mode=WAL", "_secure_delete=false", diff --git a/persist/sqlite/store_test.go b/persist/sqlite/store_test.go index fc741c6d..de2e09b3 100644 --- a/persist/sqlite/store_test.go +++ b/persist/sqlite/store_test.go @@ -24,12 +24,12 @@ func TestTransactionRetry(t *testing.T) { ch := make(chan struct{}, 1) // channel to synchronize the transaction goroutine - // start a transaction in a goroutine and hold it open for 8 seconds + // start a transaction in a goroutine and hold it open for 10 seconds // this should allow for the next transaction to be retried once go func() { err := db.transaction(context.Background(), func(tx txn) error { ch <- struct{}{} - time.Sleep(8 * time.Millisecond) + time.Sleep(10 * time.Second) return nil }) if err != nil {