Skip to content

Commit

Permalink
Merge pull request #50 from SiaFoundation/fix-too-many-variables-upda…
Browse files Browse the repository at this point in the history
…teBalances

Fix too many variables error in updateBalance
  • Loading branch information
chris124567 authored Jul 8, 2024
2 parents a3d536f + 1f120ee commit 77cc829
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions persist/sqlite/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,26 +360,20 @@ func updateBalances(tx *txn, height uint64, spentSiacoinElements, newSiacoinElem
addresses[sfe.SiafundOutput.Address] = balance{}
}

var addressList []any
for address := range addresses {
addressList = append(addressList, encode(address))
}

rows, err := tx.Query(`SELECT address, siacoin_balance, immature_siacoin_balance, siafund_balance
FROM address_balance
WHERE address IN (`+queryPlaceHolders(len(addressList))+`)`, addressList...)
balanceRowsStmt, err := tx.Prepare(`SELECT siacoin_balance, immature_siacoin_balance, siafund_balance
FROM address_balance
WHERE address = ?`)
if err != nil {
return fmt.Errorf("updateBalances: failed to query address_balance: %w", err)
return fmt.Errorf("updateBalances: failed to prepare address_balance statement: %w", err)
}
defer rows.Close()
defer balanceRowsStmt.Close()

for rows.Next() {
for addr := range addresses {
var bal balance
var address types.Address
if err := rows.Scan(decode(&address), decode(&bal.sc), decode(&bal.immatureSC), decode(&bal.sf)); err != nil {
return err
if err := balanceRowsStmt.QueryRow(encode(addr)).Scan(decode(&bal.sc), decode(&bal.immatureSC), decode(&bal.sf)); err != sql.ErrNoRows && err != nil {
return fmt.Errorf("updateBalances: failed to scan balance: %w", err)
}
addresses[address] = bal
addresses[addr] = bal
}

for _, sce := range newSiacoinElements {
Expand Down

0 comments on commit 77cc829

Please sign in to comment.