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 panic when creating missed contract events #84

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
3 changes: 1 addition & 2 deletions persist/sqlite/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 0 additions & 5 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
6 changes: 3 additions & 3 deletions persist/sqlite/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
// check that the utxo was created
utxos, err := db.WalletSiacoinOutputs(w.ID, 0, 100)
if err != nil {
t.Fatal(err)

Check failure on line 146 in persist/sqlite/consensus_test.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.22)

Test go.sia.tech/walletd/persist/sqlite/TestReorg failed in 0.02s

consensus_test.go:146: transaction failed (attempt 1): failed to scan siacoin element: sql: Scan error on column index 1, name "leaf_index": cannot scan int64 to *types.Currency
} else if len(utxos) != 1 {
t.Fatalf("expected 1 output, got %v", len(utxos))
} else if utxos[0].SiacoinOutput.Value.Cmp(expectedPayout) != 0 {
Expand All @@ -158,7 +158,7 @@
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 @@
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 @@
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
10 changes: 7 additions & 3 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 @@ -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
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