Skip to content

Commit

Permalink
Merge pull request #84 from SiaFoundation/nate/fix-copy-pasta
Browse files Browse the repository at this point in the history
Fix panic when creating missed contract events
  • Loading branch information
lukechampine committed Mar 25, 2024
2 parents 45a92cc + af713dc commit 032fa37
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 95 deletions.
105 changes: 33 additions & 72 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ run:
# list of build tags, all linters use it. Default is empty list.
build-tags: []

# which dirs to skip: issues from them won't be reported;
# can use regexp here: generated.*, regexp is applied on full path;
# default value is empty list, but default dirs are skipped independently
# from this option's value (see skip-dirs-use-default).
skip-dirs:
- cover

# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: true
Expand All @@ -36,9 +29,6 @@ run:

# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
format: colored-line-number

# print lines of code with issue, default is true
print-issued-lines: true

Expand All @@ -53,91 +43,62 @@ linters-settings:
check-shadowing: false
disable-all: false

tagliatelle:
case:
rules:
json: goCamel
yaml: goCamel

golint:
min-confidence: 1.0

gocritic:
# Which checks should be enabled; can't be combined with 'disabled-checks';
# See https://go-critic.github.io/overview#checks-overview
# To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run`
# By default list of stable checks is used.
enabled-checks:
- argOrder # Diagnostic options
- badCond
- caseOrder
- dupArg
- dupBranchBody
- dupCase
- dupSubExpr
- nilValReturn
- offBy1
- weakCond
- boolExprSimplify # Style options here and below.
- builtinShadow
- emptyFallthrough
- hexLiteral
- underef
- equalFold
enabled-tags:
- diagnostic
- style
disabled-checks:
# diagnostic
- commentedOutCode
- uncheckedInlineErr

# style
- exitAfterDefer
- ifElseChain
- importShadow
- octalLiteral
- paramTypeCombine
- ptrToRefParam
- stringsCompare
- tooManyResultsChecker
- typeDefFirst
- typeUnparen
- unlabelStmt
- unnamedResult
- whyNoLint
revive:
ignore-generated-header: true
rules:
- name: blank-imports
disabled: false
- name: bool-literal-in-expr
disabled: false
- name: confusing-results
disabled: false
- name: constant-logical-expr
disabled: false
- name: context-as-argument
disabled: false
- name: exported
disabled: false
- name: errorf
disabled: false
- name: if-return
disabled: false
- name: increment-decrement
disabled: false
- name: modifies-value-receiver
disabled: false
- name: optimize-operands-order
disabled: false
- name: range-val-in-closure
disabled: false
- name: struct-tag
disabled: false
- name: superfluous-else
disabled: false
- name: time-equal
disabled: false
- name: unexported-naming
disabled: false
- name: unexported-return
disabled: false
- name: unnecessary-stmt
disabled: false
- name: unreachable-code
disabled: false
- name: package-comments
disabled: true

tagliatelle:
case:
rules:
json: goCamel
yaml: goCamel

linters:
disable-all: true
fast: false
enable:
- tagliatelle
- gocritic
- gofmt
- revive
- govet
- misspell
- typecheck
- whitespace
- tagliatelle
- unused
- unparam

issues:
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
Expand Down
7 changes: 3 additions & 4 deletions persist/sqlite/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *Store) AddressEvents(address types.Address, offset, limit int) (events
// AddressSiacoinOutputs returns the unspent siacoin outputs for an address.
func (s *Store) AddressSiacoinOutputs(address types.Address, offset, limit int) (siacoins []types.SiacoinElement, err error) {
err = s.transaction(func(tx *txn) error {
const query = `SELECT se.id, se.leaf_index, se.merkle_proof, se.siacoin_value, sa.sia_address, se.maturity_height
const query = `SELECT se.id, se.siacoin_value, se.merkle_proof, se.leaf_index, se.maturity_height, sa.sia_address
FROM siacoin_elements se
INNER JOIN sia_addresses sa ON (se.address_id = sa.id)
WHERE sa.sia_address=$1
Expand All @@ -63,8 +63,7 @@ func (s *Store) AddressSiacoinOutputs(address types.Address, offset, limit int)
defer rows.Close()

for rows.Next() {
var siacoin types.SiacoinElement
err := rows.Scan(decode(&siacoin.ID), &siacoin.LeafIndex, decodeSlice[types.Hash256](&siacoin.MerkleProof), decode(&siacoin.SiacoinOutput.Value), decode(&siacoin.SiacoinOutput.Address), &siacoin.MaturityHeight)
siacoin, err := scanSiacoinElement(rows)
if err != nil {
return fmt.Errorf("failed to scan siacoin element: %w", err)
}
Expand Down Expand Up @@ -95,7 +94,7 @@ func (s *Store) AddressSiafundOutputs(address types.Address, offset, limit int)
var siafund types.SiafundElement
err := rows.Scan(decode(&siafund.ID), &siafund.LeafIndex, decodeSlice(&siafund.MerkleProof), &siafund.SiafundOutput.Value, decode(&siafund.ClaimStart), decode(&siafund.SiafundOutput.Address))
if err != nil {
return fmt.Errorf("failed to scan siacoin element: %w", err)
return fmt.Errorf("failed to scan siafund element: %w", err)
}
siafunds = append(siafunds, siafund)
}
Expand Down
9 changes: 2 additions & 7 deletions persist/sqlite/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ func scanStateElement(s scanner) (se types.StateElement, err error) {
return
}

func scanSiacoinElement(s scanner) (se types.SiacoinElement, err error) {
err = s.Scan(decode(&se.ID), decode(&se.SiacoinOutput.Value), decodeSlice(&se.MerkleProof), &se.LeafIndex, &se.MaturityHeight, decode(&se.SiacoinOutput.Address))
return
}

func scanAddress(s scanner) (ab addressRef, err error) {
err = s.Scan(&ab.ID, decode(&ab.Balance.Siacoins), decode(&ab.Balance.ImmatureSiacoins), &ab.Balance.Siafunds)
return
Expand Down Expand Up @@ -151,7 +146,7 @@ WHERE maturity_height=$1`
var value types.Currency

if err := rows.Scan(&addressID, decode(&value)); err != nil {
return fmt.Errorf("failed to scan siacoin elements: %w", err)
return fmt.Errorf("failed to scan siacoin balance: %w", err)
}
balanceDelta[addressID] = balanceDelta[addressID].Add(value)
}
Expand Down Expand Up @@ -206,7 +201,7 @@ func (ut *updateTx) RevertMatureSiacoinBalance(index types.ChainIndex) error {
var value types.Currency

if err := rows.Scan(&addressID, decode(&value)); err != nil {
return fmt.Errorf("failed to scan siacoin elements: %w", err)
return fmt.Errorf("failed to scan siacoin balance: %w", err)
}
balanceDelta[addressID] = balanceDelta[addressID].Add(value)
}
Expand Down
6 changes: 3 additions & 3 deletions persist/sqlite/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestReorg(t *testing.T) {
for i := 0; i < 5; i++ {
blocks = append(blocks, mineBlock(state, nil, types.VoidAddress))
state.Index.ID = blocks[len(blocks)-1].ID()
state.Index.Height = state.Index.Height + 1
state.Index.Height++
}
if err := cm.AddBlocks(blocks); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestReorg(t *testing.T) {
for i := 0; i < 10; i++ {
blocks = append(blocks, mineBlock(state, nil, types.VoidAddress))
state.Index.ID = blocks[len(blocks)-1].ID()
state.Index.Height = state.Index.Height + 1
state.Index.Height++
}
if err := cm.AddBlocks(blocks); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -451,7 +451,7 @@ func TestEphemeralBalance(t *testing.T) {
for i := 0; i < 2; i++ {
blocks = append(blocks, mineBlock(state, nil, types.VoidAddress))
state.Index.ID = blocks[len(blocks)-1].ID()
state.Index.Height = state.Index.Height + 1
state.Index.Height++
}
if err := cm.AddBlocks(blocks); err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions persist/sqlite/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ func doTransaction(db *sql.DB, log *zap.Logger, fn func(tx *txn) error) error {
Tx: dbtx,
log: log,
}
if err = fn(tx); err != nil {
if err := fn(tx); err != nil {
return err
} else if err = tx.Commit(); err != nil {
} else if err := tx.Commit(); err != nil {
return fmt.Errorf("failed to commit transaction: %w", err)
}
return nil
Expand Down
14 changes: 9 additions & 5 deletions persist/sqlite/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"go.sia.tech/walletd/wallet"
)

func scanSiacoinElement(s scanner) (se types.SiacoinElement, err error) {
err = s.Scan(decode(&se.ID), decode(&se.SiacoinOutput.Value), decodeSlice(&se.MerkleProof), &se.LeafIndex, &se.MaturityHeight, decode(&se.SiacoinOutput.Address))
return
}

func insertAddress(tx *txn, addr types.Address) (id int64, err error) {
const query = `INSERT INTO sia_addresses (sia_address, siacoin_balance, immature_siacoin_balance, siafund_balance)
VALUES ($1, $2, $2, 0) ON CONFLICT (sia_address) DO UPDATE SET sia_address=EXCLUDED.sia_address
Expand Down Expand Up @@ -83,7 +88,7 @@ func getWalletEvents(tx *txn, id wallet.ID, offset, limit int) (events []wallet.
events = append(events, event)
eventIDs = append(eventIDs, eventID)
}
if err = rows.Err(); err != nil {
if err := rows.Err(); err != nil {
return nil, nil, err
}
return
Expand Down Expand Up @@ -287,7 +292,7 @@ func (s *Store) WalletSiacoinOutputs(id wallet.ID, offset, limit int) (siacoins
return err
}

const query = `SELECT se.id, se.leaf_index, se.merkle_proof, se.siacoin_value, sa.sia_address, se.maturity_height
const query = `SELECT se.id, se.siacoin_value, se.merkle_proof, se.leaf_index, se.maturity_height, sa.sia_address
FROM siacoin_elements se
INNER JOIN sia_addresses sa ON (se.address_id = sa.id)
WHERE se.address_id IN (SELECT address_id FROM wallet_addresses WHERE wallet_id=$1)
Expand All @@ -300,8 +305,7 @@ func (s *Store) WalletSiacoinOutputs(id wallet.ID, offset, limit int) (siacoins
defer rows.Close()

for rows.Next() {
var siacoin types.SiacoinElement
err := rows.Scan(decode(&siacoin.ID), &siacoin.LeafIndex, decodeSlice[types.Hash256](&siacoin.MerkleProof), decode(&siacoin.SiacoinOutput.Value), decode(&siacoin.SiacoinOutput.Address), &siacoin.MaturityHeight)
siacoin, err := scanSiacoinElement(rows)
if err != nil {
return fmt.Errorf("failed to scan siacoin element: %w", err)
}
Expand Down Expand Up @@ -336,7 +340,7 @@ func (s *Store) WalletSiafundOutputs(id wallet.ID, offset, limit int) (siafunds
var siafund types.SiafundElement
err := rows.Scan(decode(&siafund.ID), &siafund.LeafIndex, decodeSlice(&siafund.MerkleProof), &siafund.SiafundOutput.Value, decode(&siafund.ClaimStart), decode(&siafund.SiafundOutput.Address))
if err != nil {
return fmt.Errorf("failed to scan siacoin element: %w", err)
return fmt.Errorf("failed to scan siafund element: %w", err)
}
siafunds = append(siafunds, siafund)
}
Expand Down
4 changes: 2 additions & 2 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f
}
} else {
for i := range fce.FileContract.MissedProofOutputs {
if !relevant(fce.FileContract.ValidProofOutputs[i].Address) {
if !relevant(fce.FileContract.MissedProofOutputs[i].Address) {
continue
}

Expand All @@ -628,7 +628,7 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant f
FileContract: fce,
SiacoinOutput: sces[outputID],
Missed: true,
}, []types.Address{fce.FileContract.ValidProofOutputs[i].Address})
}, []types.Address{fce.FileContract.MissedProofOutputs[i].Address})
}
}
})
Expand Down

0 comments on commit 032fa37

Please sign in to comment.