Skip to content

Commit c749e1a

Browse files
authored
Merge pull request #342 from perun-network/satisfy-linter
Satisfy linter
2 parents 1e496bd + 886f36e commit c749e1a

File tree

13 files changed

+413
-358
lines changed

13 files changed

+413
-358
lines changed

channel/mock_app.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,52 @@ func (a MockApp) NewData() Data {
116116

117117
// ValidTransition checks the transition for validity.
118118
func (a MockApp) ValidTransition(params *Params, from, to *State, actor Index) error {
119-
return a.execMockOp(from.Data.(*MockOp))
119+
op, ok := from.Data.(*MockOp)
120+
if !ok {
121+
return fmt.Errorf("wrong data type: expected *MockOp, got %T", from.Data)
122+
}
123+
return a.execMockOp(op)
120124
}
121125

122126
// ValidInit checks the initial state for validity.
123127
func (a MockApp) ValidInit(params *Params, state *State) error {
124-
return a.execMockOp(state.Data.(*MockOp))
128+
op, ok := state.Data.(*MockOp)
129+
if !ok {
130+
return fmt.Errorf("wrong data type: expected *MockOp, got %T", state.Data)
131+
}
132+
return a.execMockOp(op)
125133
}
126134

127135
// ValidAction checks the action for validity.
128136
func (a MockApp) ValidAction(params *Params, state *State, part Index, act Action) error {
129-
return a.execMockOp(act.(*MockOp))
137+
op, ok := act.(*MockOp)
138+
if !ok {
139+
return fmt.Errorf("wrong data type: expected *MockOp, got %T", act)
140+
}
141+
return a.execMockOp(op)
130142
}
131143

132144
// ApplyActions applies the actions unto a copy of state and returns the result or an error.
133145
func (a MockApp) ApplyActions(params *Params, state *State, acts []Action) (*State, error) {
134146
ret := state.Clone()
135147
ret.Version++
136148

137-
return ret, a.execMockOp(acts[0].(*MockOp))
149+
op, ok := acts[0].(*MockOp)
150+
if !ok {
151+
return nil, fmt.Errorf("wrong data type: expected *MockOp, got %T", acts[0])
152+
}
153+
154+
return ret, a.execMockOp(op)
138155
}
139156

140157
// InitState Checks for the validity of the passed arguments as initial state.
141158
func (a MockApp) InitState(params *Params, rawActs []Action) (Allocation, Data, error) {
142-
return Allocation{}, nil, a.execMockOp(rawActs[0].(*MockOp))
159+
op, ok := rawActs[0].(*MockOp)
160+
if !ok {
161+
return Allocation{}, nil, fmt.Errorf("wrong data type: expected *MockOp, got %T", rawActs[0])
162+
}
163+
164+
return Allocation{}, nil, a.execMockOp(op)
143165
}
144166

145167
// execMockOp executes the operation indicated by the MockOp from the MockOp.

channel/persistence/keyvalue/restorer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func eatExpect(r io.Reader, tok string) error {
160160
return errors.WithMessage(err, "reading")
161161
}
162162
if string(buf) != tok {
163-
return errors.Errorf("expected %s, got %s.", tok, string(buf))
163+
return errors.Errorf("expected %s, got %s", tok, string(buf))
164164
}
165165
return nil
166166
}

channel/persistence/test/channel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Channel struct {
3737
*persistence.StateMachine
3838

3939
pr persistence.PersistRestorer
40-
ctx context.Context
40+
ctx context.Context //nolint:containedctx // This is just done for testing. Could be revised.
4141
}
4242

4343
// NewRandomChannel creates a random channel with the requested persister and

channel/persistence/test/persistrestorertest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Client struct {
3636

3737
rng *rand.Rand
3838
pr persistence.PersistRestorer
39-
ctx context.Context
39+
ctx context.Context //nolint:containedctx // This is just done for testing. Could be revised.
4040
}
4141

4242
// number of peers in a channel that are used for the tests.

client/channelconn.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,14 @@ func newChannelConn(id channel.ID, peers []wire.Address, idx channel.Index, sub
6060
}()
6161

6262
isUpdateRes := func(e *wire.Envelope) bool {
63-
ok := e.Msg.Type() == wire.ChannelUpdateAcc || e.Msg.Type() == wire.ChannelUpdateRej
64-
return ok && e.Msg.(ChannelMsg).ID() == id
63+
switch msg := e.Msg.(type) {
64+
case *ChannelUpdateAccMsg:
65+
return msg.ID() == id
66+
case *ChannelUpdateRejMsg:
67+
return msg.ID() == id
68+
default:
69+
return false
70+
}
6571
}
6672

6773
if err = sub.Subscribe(relay, isUpdateRes); err != nil {
@@ -157,5 +163,9 @@ func (r *channelMsgRecv) Next(ctx context.Context) (channel.Index, ChannelMsg, e
157163
if idx == -1 {
158164
return 0, nil, errors.Errorf("channel connection received message from unexpected peer %v", env.Sender)
159165
}
160-
return channel.Index(idx), env.Msg.(ChannelMsg), nil // predicate must guarantee that the conversion is safe
166+
msg, ok := env.Msg.(ChannelMsg)
167+
if !ok {
168+
return 0, nil, errors.Errorf("unexpected message type: expected ChannelMsg, got %T", env.Msg)
169+
}
170+
return channel.Index(idx), msg, nil // predicate must guarantee that the conversion is safe
161171
}

client/proposal.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,14 @@ func (c *Client) proposeTwoPartyChannel(
315315

316316
proposalID := proposal.Base().ProposalID
317317
isResponse := func(e *wire.Envelope) bool {
318-
acc, isAcc := e.Msg.(ChannelProposalAccept)
319-
return (isAcc && acc.Base().ProposalID == proposalID) ||
320-
(e.Msg.Type() == wire.ChannelProposalRej &&
321-
e.Msg.(*ChannelProposalRejMsg).ProposalID == proposalID)
318+
switch msg := e.Msg.(type) {
319+
case ChannelProposalAccept:
320+
return msg.Base().ProposalID == proposalID
321+
case ChannelProposalRejMsg:
322+
return msg.ProposalID == proposalID
323+
default:
324+
return false
325+
}
322326
}
323327
receiver := wire.NewReceiver()
324328
defer receiver.Close()
@@ -606,20 +610,23 @@ func (c *Client) mpcppParts(
606610
) (parts []wallet.Address) {
607611
switch p := prop.(type) {
608612
case *LedgerChannelProposalMsg:
609-
parts = participants(
610-
p.Participant,
611-
acc.(*LedgerChannelProposalAccMsg).Participant)
613+
ledgerAcc, ok := acc.(*LedgerChannelProposalAccMsg)
614+
if !ok {
615+
c.log.Panicf("unexpected message type: expected *LedgerChannelProposalAccMsg, got %T", acc)
616+
}
617+
parts = participants(p.Participant, ledgerAcc.Participant)
612618
case *SubChannelProposalMsg:
613619
ch, ok := c.channels.Channel(p.Parent)
614620
if !ok {
615621
c.log.Panic("unknown parent channel ID")
616622
}
617623
parts = ch.Params().Parts
618624
case *VirtualChannelProposalMsg:
619-
parts = participants(
620-
p.Proposer,
621-
acc.(*VirtualChannelProposalAccMsg).Responder,
622-
)
625+
virtualAcc, ok := acc.(*VirtualChannelProposalAccMsg)
626+
if !ok {
627+
c.log.Panicf("unexpected message type: expected *VirtualChannelProposalAccMsg, got %T", acc)
628+
}
629+
parts = participants(p.Proposer, virtualAcc.Responder)
623630
default:
624631
c.log.Panicf("unhandled %T", p)
625632
}
@@ -697,8 +704,8 @@ func (c *Client) fundSubchannel(ctx context.Context, prop *SubChannelProposalMsg
697704
// enableVer0Cache enables caching of incoming version 0 signatures.
698705
func enableVer0Cache(c wire.Cacher) *wire.Predicate {
699706
p := func(m *wire.Envelope) bool {
700-
return m.Msg.Type() == wire.ChannelUpdateAcc &&
701-
m.Msg.(*ChannelUpdateAccMsg).Version == 0
707+
msg, ok := m.Msg.(*ChannelUpdateAccMsg)
708+
return ok && msg.Version == 0
702709
}
703710
c.Cache(&p)
704711
return &p

client/proposalopts.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,23 @@ var optNames = struct{ nonce, app, appData, fundingAgreement string }{nonce: "no
3232
// App returns the option's configured app.
3333
func (o ProposalOpts) App() channel.App {
3434
if v := o[optNames.app]; v != nil {
35-
return v.(channel.App)
35+
app, ok := v.(channel.App)
36+
if !ok {
37+
log.Panicf("wrong type: expected channel.App, got %T", v)
38+
}
39+
return app
3640
}
3741
return channel.NoApp()
3842
}
3943

4044
// AppData returns the option's configured app data.
4145
func (o ProposalOpts) AppData() channel.Data {
4246
if v := o[optNames.appData]; v != nil {
43-
return v.(channel.Data)
47+
data, ok := v.(channel.Data)
48+
if !ok {
49+
log.Panicf("wrong type: expected channel.Data, got %T", v)
50+
}
51+
return data
4452
}
4553
return channel.NoData()
4654
}
@@ -63,7 +71,11 @@ func (o ProposalOpts) fundingAgreement() channel.Balances {
6371
if !ok {
6472
panic("Option FundingAgreement not set")
6573
}
66-
return a.(channel.Balances)
74+
bals, ok := a.(channel.Balances)
75+
if !ok {
76+
log.Panicf("wrong type: expected channel.Balances, got %T", a)
77+
}
78+
return bals
6779
}
6880

6981
// nonce returns the option's configured nonce share, or a random nonce share.
@@ -73,7 +85,11 @@ func (o ProposalOpts) nonce() NonceShare {
7385
n = WithRandomNonce().nonce()
7486
o[optNames.nonce] = n
7587
}
76-
return n.(NonceShare)
88+
share, ok := n.(NonceShare)
89+
if !ok {
90+
log.Panicf("wrong type: expected NonceShare, got %T", n)
91+
}
92+
return share
7793
}
7894

7995
// isNonce returns whether a ProposalOpts contains a manually set nonce.

client/sync.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ func (c *Client) syncChannel(ctx context.Context, ch *persistence.Channel, p wir
7373
defer recv.Close() // ignore error
7474
id := ch.ID()
7575
err = c.conn.Subscribe(recv, func(m *wire.Envelope) bool {
76-
return m.Msg.Type() == wire.ChannelSync && m.Msg.(ChannelMsg).ID() == id
76+
msg, ok := m.Msg.(*ChannelSyncMsg)
77+
return ok && msg.ID() == id
7778
})
7879
if err != nil {
7980
return errors.WithMessage(err, "subscribing on relay")

client/test/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func (b *MockBackend) removeSubscription(ch channel.ID, sub *MockSubscription) {
396396

397397
// MockSubscription is a subscription for MockBackend.
398398
type MockSubscription struct {
399-
ctx context.Context
399+
ctx context.Context //nolint:containedctx // This is just done for testing. Could be revised.
400400
events chan channel.AdjudicatorEvent
401401
err chan error
402402
onClose func()

client/update.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,7 @@ func (c *Channel) OnUpdate(cb func(from, to *channel.State)) {
411411
// * Sub-allocations do not change.
412412
func (c *Channel) validTwoPartyUpdate(up ChannelUpdate, sigIdx channel.Index) error {
413413
if up.ActorIdx != sigIdx {
414-
return errors.Errorf(
415-
"Currently, only update proposals with the proposing peer as actor are allowed.")
414+
return errors.New("invalid proposer")
416415
}
417416
if err := channel.SubAllocsAssertEqual(c.machine.State().Locked, up.State.Locked); err != nil {
418417
return errors.WithMessage(err, "sub-allocation changed")

0 commit comments

Comments
 (0)