Skip to content

Commit

Permalink
Merge pull request #166 from SiaFoundation/nate/clear-decoder-buffer
Browse files Browse the repository at this point in the history
Clear decoder buffer
  • Loading branch information
n8maninger authored Jun 7, 2024
2 parents b6076d9 + 5c81d31 commit c89664e
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 71 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
go-version: [ '1.20', '1.21' ]
go-version: [ '1.21', '1.22' ]
runs-on: ${{ matrix.os }}
timeout-minutes: 120
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: test
uses: n8maninger/action-golang-test@v1
uses: n8maninger/action-golang-test@v2
with:
args: "-count=50;-timeout=30m;-failfast"
skip-go-install: true
show-package-output: true
- name: test-race
uses: n8maninger/action-golang-test@v1
uses: n8maninger/action-golang-test@v2
with:
args: "-race;-count=50;-timeout=30m;-failfast"
skip-go-install: true
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
go-version: [ '1.20', '1.21' ]
go-version: [ '1.21', '1.22' ]
runs-on: ${{ matrix.os }}
timeout-minutes: 10
steps:
- name: Configure git # required for golangci-lint on Windows
shell: bash
run: git config --global core.autocrlf false
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v4
with:
skip-cache: true
- name: test
uses: n8maninger/action-golang-test@v1
uses: n8maninger/action-golang-test@v2
with:
args: "-race"
skip-go-install: true
Expand Down
66 changes: 26 additions & 40 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,8 @@ 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

# which files to skip: they will be analyzed, but issues from them
# won't be reported. Default value is empty list, but there is
# no need to include all autogenerated files, we confidently recognize
# autogenerated files. If it's not please let us know.
skip-files: []

# 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 @@ -50,7 +30,6 @@ linters-settings:
## Enabled linters:
govet:
# report about shadowed variables
check-shadowing: false
disable-all: false

tagliatelle:
Expand All @@ -65,24 +44,31 @@ linters-settings:
# 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.
disable-all: true
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
- emptyStringTest
- exitAfterDefer
- ifElseChain
- importShadow
- initClause
- nestingReduce
- octalLiteral
- paramTypeCombine
- ptrToRefParam
- stringsCompare
- tooManyResultsChecker
- typeDefFirst
- typeUnparen
- unlabelStmt
- unnamedResult
- whyNoLint
revive:
ignore-generated-header: true
rules:
Expand All @@ -91,7 +77,7 @@ linters-settings:
- name: bool-literal-in-expr
disabled: false
- name: confusing-naming
disabled: true
disabled: false
- name: confusing-results
disabled: false
- name: constant-logical-expr
Expand Down
2 changes: 1 addition & 1 deletion consensus/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func validateCurrencyOverflow(ms *MidState, txn types.Transaction) error {
return nil
}

func validateMinimumValues(ms *MidState, txn types.Transaction) error {
func validateMinimumValues(_ *MidState, txn types.Transaction) error {
zero := false
for _, sco := range txn.SiacoinOutputs {
zero = zero || sco.Value.IsZero()
Expand Down
12 changes: 6 additions & 6 deletions consensus/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (db *consensusDB) supplementTipBlock(b types.Block) (bs V1BlockSupplement)
return bs
}

func (db *consensusDB) ancestorTimestamp(id types.BlockID) time.Time {
func (db *consensusDB) ancestorTimestamp(types.BlockID) time.Time {
return time.Time{}
}

Expand Down Expand Up @@ -409,14 +409,14 @@ func TestValidateBlock(t *testing.T) {
"siafund outputs exceed inputs",
func(b *types.Block) {
txn := &b.Transactions[0]
txn.SiafundOutputs[0].Value = txn.SiafundOutputs[0].Value + 1
txn.SiafundOutputs[0].Value++
},
},
{
"siafund outputs less than inputs",
func(b *types.Block) {
txn := &b.Transactions[0]
txn.SiafundOutputs[0].Value = txn.SiafundOutputs[0].Value - 1
txn.SiafundOutputs[0].Value--
},
},
{
Expand Down Expand Up @@ -591,7 +591,7 @@ func TestValidateBlock(t *testing.T) {
func(b *types.Block) {
txn := &b.Transactions[0]
txn.SiafundInputs = append(txn.SiafundInputs, txn.SiafundInputs[0])
txn.SiafundOutputs[0].Value = txn.SiafundOutputs[0].Value + 100
txn.SiafundOutputs[0].Value += 100
},
},
{
Expand Down Expand Up @@ -965,14 +965,14 @@ func TestValidateV2Block(t *testing.T) {
"siafund outputs exceed inputs",
func(b *types.Block) {
txn := &b.V2.Transactions[0]
txn.SiafundOutputs[0].Value = txn.SiafundOutputs[0].Value + 1
txn.SiafundOutputs[0].Value++
},
},
{
"siafund outputs less than inputs",
func(b *types.Block) {
txn := &b.V2.Transactions[0]
txn.SiafundOutputs[0].Value = txn.SiafundOutputs[0].Value - 1
txn.SiafundOutputs[0].Value--
},
},
{
Expand Down
6 changes: 1 addition & 5 deletions gateway/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,7 @@ func (r *RPCSendBlocks) encodeResponse(e *types.Encoder) {
}
}
func (r *RPCSendBlocks) decodeResponse(d *types.Decoder) {
if n := d.ReadPrefix(); n <= cap(r.Blocks) {
r.Blocks = r.Blocks[:n]
} else {
r.Blocks = make([]types.Block, n)
}
r.Blocks = make([]types.Block, d.ReadPrefix())
for i := range r.Blocks {
(*types.V1Block)(&r.Blocks[i]).DecodeFrom(d)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module go.sia.tech/core

go 1.20
go 1.21.8

require (
go.sia.tech/mux v1.2.0
Expand Down
8 changes: 2 additions & 6 deletions types/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,9 @@ func (d *Decoder) Err() error { return d.err }
func (d *Decoder) Read(p []byte) (int, error) {
n := 0
for len(p[n:]) > 0 && d.err == nil {
want := len(p[n:])
if want > len(d.buf) {
want = len(d.buf)
}
var read int
read, d.err = io.ReadFull(&d.lr, d.buf[:want])
read, err := io.ReadFull(&d.lr, d.buf[:min(len(p[n:]), len(d.buf))])
n += copy(p[n:], d.buf[:read])
d.SetErr(err)
}
return n, d.err
}
Expand Down
46 changes: 46 additions & 0 deletions types/encoding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package types

import (
"io"
"testing"
)

type readErrorer struct {
err error
r io.Reader
}

func (r *readErrorer) Read(p []byte) (n int, err error) {
if r.err != nil {
return 0, r.err
}
return r.r.Read(p)
}

func TestDecoderError(t *testing.T) {
r, w := io.Pipe()
re := &readErrorer{r: r}
enc := NewEncoder(w)
d := NewDecoder(io.LimitedReader{R: re, N: 1e6})

go func() {
// writing to the pipe blocks until we read from it
enc.WritePrefix(1000)
enc.Flush()
}()

// read the value from the encoder
n := d.ReadPrefix()
if n != 1000 {
t.Fatalf("expected 1000, got %d", n)
}

// set the error and try to read again
re.err = io.EOF

// should return 0 since the decoder errored
n = d.ReadPrefix()
if n != 0 {
t.Fatalf("expected 0, got %d", n)
}
}
4 changes: 2 additions & 2 deletions types/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,14 @@ func TestSatisfiedPolicyMarshalJSON(t *testing.T) {
name: "PolicyWithSignature",
sp: PolicyPublicKey(publicKey),
signatures: []Signature{signature},
exp: fmt.Sprintf(`{"policy":"pk(0x%x)","signatures":["%s"]}`, publicKey[:], signature),
exp: fmt.Sprintf(`{"policy":"pk(0x%x)","signatures":[%q]}`, publicKey[:], signature),
},
{
name: "PolicyWithSignaturesAndPreimages",
sp: PolicyThreshold(1, []SpendPolicy{PolicyPublicKey(publicKey), PolicyHash(hash)}),
signatures: []Signature{signature},
preimages: [][]byte{{1, 2, 3}},
exp: fmt.Sprintf(`{"policy":"thresh(1,[pk(0x%x),h(0x%x)])","signatures":["%s"],"preimages":["010203"]}`, publicKey[:], hash[:], signature),
exp: fmt.Sprintf(`{"policy":"thresh(1,[pk(0x%x),h(0x%x)])","signatures":[%q],"preimages":["010203"]}`, publicKey[:], hash[:], signature),
},
{
name: "PolicyWithPreimagesOnly",
Expand Down

0 comments on commit c89664e

Please sign in to comment.