-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcef632
commit 107d34c
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package sqlite | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.sia.tech/core/types" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func checkContractAccountFunding(tx txn, log *zap.Logger) error { | ||
rows, err := tx.Query(`SELECT contract_id, amount FROM contract_account_funding`) | ||
if err != nil { | ||
return fmt.Errorf("failed to query contract account funding: %w", err) | ||
} | ||
defer rows.Close() | ||
|
||
contractFunding := make(map[int64]types.Currency) | ||
for rows.Next() { | ||
var contractID int64 | ||
var amount types.Currency | ||
if err := rows.Scan(&contractID, (*sqlCurrency)(&amount)); err != nil { | ||
return fmt.Errorf("failed to scan contract account funding: %w", err) | ||
} | ||
contractFunding[contractID] = contractFunding[contractID].Add(amount) | ||
} | ||
|
||
if err := rows.Err(); err != nil { | ||
return fmt.Errorf("failed to iterate contract account funding: %w", err) | ||
} else if err := rows.Close(); err != nil { | ||
return fmt.Errorf("failed to close contract account funding: %w", err) | ||
} | ||
|
||
for contractID, amount := range contractFunding { | ||
var actualAmount types.Currency | ||
err := tx.QueryRow(`SELECT account_funding FROM contracts WHERE id=$1`, contractID).Scan((*sqlCurrency)(&actualAmount)) | ||
if err != nil { | ||
return fmt.Errorf("failed to query contract account funding: %w", err) | ||
} | ||
|
||
if !actualAmount.Equals(amount) { | ||
log.Debug("incorrect contract account funding", zap.Int64("contractID", contractID), zap.Stringer("expected", amount), zap.Stringer("actual", actualAmount)) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func recalcContractAccountFunding(tx txn, _ *zap.Logger) error { | ||
rows, err := tx.Query(`SELECT contract_id, amount FROM contract_account_funding`) | ||
if err != nil { | ||
return fmt.Errorf("failed to query contract account funding: %w", err) | ||
} | ||
defer rows.Close() | ||
|
||
contractFunding := make(map[int64]types.Currency) | ||
for rows.Next() { | ||
var contractID int64 | ||
var amount types.Currency | ||
if err := rows.Scan(&contractID, (*sqlCurrency)(&amount)); err != nil { | ||
return fmt.Errorf("failed to scan contract account funding: %w", err) | ||
} | ||
contractFunding[contractID] = contractFunding[contractID].Add(amount) | ||
} | ||
|
||
if err := rows.Err(); err != nil { | ||
return fmt.Errorf("failed to iterate contract account funding: %w", err) | ||
} else if err := rows.Close(); err != nil { | ||
return fmt.Errorf("failed to close contract account funding: %w", err) | ||
} | ||
|
||
for contractID, amount := range contractFunding { | ||
res, err := tx.Exec(`UPDATE contracts SET account_funding=$1 WHERE id=$2`, sqlCurrency(amount), contractID) | ||
if err != nil { | ||
return fmt.Errorf("failed to query contract account funding: %w", err) | ||
} else if rowsAffected, err := res.RowsAffected(); err != nil { | ||
return fmt.Errorf("failed to query contract account funding: %w", err) | ||
} else if rowsAffected != 1 { | ||
return fmt.Errorf("failed to update contract account funding: %w", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s *Store) CheckContractAccountFunding() error { | ||
Check warning on line 83 in persist/sqlite/recalc.go GitHub Actions / test (ubuntu-latest, 1.21)
Check warning on line 83 in persist/sqlite/recalc.go GitHub Actions / test
|
||
return s.transaction(func(tx txn) error { | ||
return checkContractAccountFunding(tx, s.log) | ||
}) | ||
} | ||
|
||
func (s *Store) RecalcContractAccountFunding() error { | ||
Check warning on line 89 in persist/sqlite/recalc.go GitHub Actions / test (ubuntu-latest, 1.21)
Check warning on line 89 in persist/sqlite/recalc.go GitHub Actions / test
|
||
return s.transaction(func(tx txn) error { | ||
return recalcContractAccountFunding(tx, s.log) | ||
}) | ||
} |