Skip to content

Extend Payment Channel Test Framework #423

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ on:
release:

env:
go-version: 1.18
go-version: 1.22

jobs:
check-copyright:
name: Copyright
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Authors
Expand All @@ -26,20 +26,20 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Check vanity import
run: .scripts/check-vanity-imports.sh $GITHUB_WORKSPACE

- name: Setup Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: ${{ env.go-version }}

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v7
with:
version: v1.45
version: v2.1

- name: Lint proto files
uses: plexsystems/protolint-action@v0.6.0
Expand Down
103 changes: 103 additions & 0 deletions .golangci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"formatters": {
"enable": [
"gofmt",
"gofumpt",
"goimports"
],
"exclusions": {
"generated": "lax",
"paths": [
"third_party$",
"builtin$",
"examples$"
]
}
},
"linters": {
"default": "all",
"disable": [
"err113",
"errorlint",
"gochecknoglobals",
"gochecknoinits",
"ireturn",
"lll",
"nlreturn",
"paralleltest",
"promlinter",
"tparallel",
"varnamelen",
"wrapcheck",
"wsl",
"testifylint",
"revive",
"recvcheck",
"prealloc",
"depguard",
"nonamedreturns",
"inamedparam",
"exhaustruct"
],
"exclusions": {
"generated": "lax",
"paths": [
"third_party$",
"builtin$",
"examples$"
],
"presets": [
"common-false-positives",
"legacy",
"std-error-handling"
],
"rules": [
{
"linters": [
"err113",
"forcetypeassert",
"funlen"
],
"path": "test"
},
{
"linters": [
"revive"
],
"path": "test",
"text": "context-as-argument"
}
]
},
"settings": {
"cyclop": {
"max-complexity": 15
},
"forbidigo": {
"forbid": [
{
"pattern": "^[Ee]quals$"
},
{
"pattern": "^print.*$"
},
{
"pattern": "fmt\\.Print.*"
}
]
},
"goheader": {
"template-path": ".scripts/copyright-notice",
"values": {
"regexp": {
"ANY_YEAR": "20(19|2\\d)"
}
}
}
}
},
"run": {
"modules-download-mode": "readonly"
},
"version": "2"
}
81 changes: 0 additions & 81 deletions .golangci.yml

This file was deleted.

10 changes: 5 additions & 5 deletions apps/payment/app_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestApp_ValidInit(t *testing.T) {
assert.Panics(func() { app.ValidInit(nil, wrongdata) }) //nolint:errcheck

data := &channel.State{Data: Data()}
assert.Nil(app.ValidInit(nil, data))
assert.NoError(app.ValidInit(nil, data))
}

func TestApp_ValidTransition(t *testing.T) {
Expand Down Expand Up @@ -93,9 +93,9 @@ func TestApp_ValidTransition(t *testing.T) {
test.WithBalances(asBalances(tt.from...)...),
)
numParticipants := len(tt.from[0])
for i := 0; i < numParticipants; i++ {
for i := range numParticipants {
// valid self-transition
assert.NoError(app.ValidTransition(nil, from, from, channel.Index(i)))
assert.NoError(app.ValidTransition(nil, from, from, channel.Index(i))) //nolint:gosec
}

for _, tto := range tt.tos {
Expand All @@ -104,8 +104,8 @@ func TestApp_ValidTransition(t *testing.T) {
test.WithAppData(Data()),
test.WithBalances(asBalances(tto.alloc...)...),
)
for i := 0; i < numParticipants; i++ {
err := app.ValidTransition(nil, from, to, channel.Index(i))
for i := range numParticipants {
err := app.ValidTransition(nil, from, to, channel.Index(i)) //nolint:gosec
if i == tto.valid {
assert.NoError(err)
} else {
Expand Down
3 changes: 2 additions & 1 deletion apps/payment/randomizer_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

_ "perun.network/go-perun/backend/sim" // backend init
"perun.network/go-perun/channel"
Expand All @@ -31,7 +32,7 @@ func TestRandomizer(t *testing.T) {
app := r.NewRandomApp(rng, channel.TestBackendID)
channel.RegisterApp(app)
regApp, err := channel.Resolve(app.Def())
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, app.Def().Equal(regApp.Def()))
assert.True(t, IsData(r.NewRandomData(rng)))
}
4 changes: 2 additions & 2 deletions apps/payment/resolver_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestResolver(t *testing.T) {
channel.RegisterAppResolver(def.Equal, &Resolver{})

app, err := channel.Resolve(def)
assert.NoError(err)
require.NoError(err)
require.NotNil(app)
assert.True(def.Equal(app.Def()))
}
Expand All @@ -47,7 +47,7 @@ func TestData(t *testing.T) {
assert.NotPanics(func() {
data := Data()
_, err := data.MarshalBinary()
assert.Nil(err)
assert.NoError(err)
})

assert.NotPanics(func() {
Expand Down
12 changes: 6 additions & 6 deletions backend/sim/channel/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type AppID struct {
*wallet.Address
}

// NewRandomAppID generates a new random app identifier.
func NewRandomAppID(rng *rand.Rand) AppID {
addr := wallet.NewRandomAddress(rng)
return AppID{Address: addr}
}

// Equal returns whether the object is equal to the given object.
func (id AppID) Equal(b channel.AppID) bool {
bTyped, ok := b.(AppID)
Expand All @@ -44,9 +50,3 @@ func (id AppID) Key() channel.AppIDKey {
}
return channel.AppIDKey(b)
}

// NewRandomAppID generates a new random app identifier.
func NewRandomAppID(rng *rand.Rand) AppID {
addr := wallet.NewRandomAddress(rng)
return AppID{Address: addr}
}
13 changes: 11 additions & 2 deletions backend/sim/channel/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package channel
import (
"encoding/binary"
"fmt"
"math"
"math/rand"

"github.com/pkg/errors"
"perun.network/go-perun/channel"
)

Expand All @@ -44,16 +46,23 @@ func NewRandomAsset(rng *rand.Rand) *Asset {
// MarshalBinary marshals the address into its binary representation.
func (a Asset) MarshalBinary() ([]byte, error) {
data := make([]byte, assetLen)
if a.ID < 0 {
return nil, errors.New("asset ID must be non-negative")
}
byteOrder.PutUint64(data, uint64(a.ID))
return data, nil
}

// UnmarshalBinary unmarshals the asset from its binary representation.
func (a *Asset) UnmarshalBinary(data []byte) error {
if len(data) != assetLen {
return fmt.Errorf("unexpected length %d, want %d", len(data), assetLen) //nolint:goerr113 // We do not want to define this as constant error.
return fmt.Errorf("unexpected length %d, want %d", len(data), assetLen) // We do not want to define this as constant error.
}
id := byteOrder.Uint64(data)
if id > math.MaxInt64 {
return fmt.Errorf("asset ID %d is too large", id)
}
a.ID = int64(byteOrder.Uint64(data))
a.ID = int64(id)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion backend/sim/channel/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func Test_Asset_GenericMarshaler(t *testing.T) {
rng := pkgtest.Prng(t)
for n := 0; n < 10; n++ {
for range 10 {
test.GenericMarshalerTest(t, &channel.Asset{ID: rng.Int63()})
}
}
Loading