Skip to content

Commit

Permalink
Merge pull request #342 from perun-network/satisfy-linter
Browse files Browse the repository at this point in the history
Satisfy linter
  • Loading branch information
cryptphil authored May 24, 2022
2 parents 1e496bd + 886f36e commit c749e1a
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 358 deletions.
32 changes: 27 additions & 5 deletions channel/mock_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,30 +116,52 @@ func (a MockApp) NewData() Data {

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

// ValidInit checks the initial state for validity.
func (a MockApp) ValidInit(params *Params, state *State) error {
return a.execMockOp(state.Data.(*MockOp))
op, ok := state.Data.(*MockOp)
if !ok {
return fmt.Errorf("wrong data type: expected *MockOp, got %T", state.Data)
}
return a.execMockOp(op)
}

// ValidAction checks the action for validity.
func (a MockApp) ValidAction(params *Params, state *State, part Index, act Action) error {
return a.execMockOp(act.(*MockOp))
op, ok := act.(*MockOp)
if !ok {
return fmt.Errorf("wrong data type: expected *MockOp, got %T", act)
}
return a.execMockOp(op)
}

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

return ret, a.execMockOp(acts[0].(*MockOp))
op, ok := acts[0].(*MockOp)
if !ok {
return nil, fmt.Errorf("wrong data type: expected *MockOp, got %T", acts[0])
}

return ret, a.execMockOp(op)
}

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

return Allocation{}, nil, a.execMockOp(op)
}

// execMockOp executes the operation indicated by the MockOp from the MockOp.
Expand Down
2 changes: 1 addition & 1 deletion channel/persistence/keyvalue/restorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func eatExpect(r io.Reader, tok string) error {
return errors.WithMessage(err, "reading")
}
if string(buf) != tok {
return errors.Errorf("expected %s, got %s.", tok, string(buf))
return errors.Errorf("expected %s, got %s", tok, string(buf))
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion channel/persistence/test/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Channel struct {
*persistence.StateMachine

pr persistence.PersistRestorer
ctx context.Context
ctx context.Context //nolint:containedctx // This is just done for testing. Could be revised.
}

// NewRandomChannel creates a random channel with the requested persister and
Expand Down
2 changes: 1 addition & 1 deletion channel/persistence/test/persistrestorertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Client struct {

rng *rand.Rand
pr persistence.PersistRestorer
ctx context.Context
ctx context.Context //nolint:containedctx // This is just done for testing. Could be revised.
}

// number of peers in a channel that are used for the tests.
Expand Down
16 changes: 13 additions & 3 deletions client/channelconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ func newChannelConn(id channel.ID, peers []wire.Address, idx channel.Index, sub
}()

isUpdateRes := func(e *wire.Envelope) bool {
ok := e.Msg.Type() == wire.ChannelUpdateAcc || e.Msg.Type() == wire.ChannelUpdateRej
return ok && e.Msg.(ChannelMsg).ID() == id
switch msg := e.Msg.(type) {
case *ChannelUpdateAccMsg:
return msg.ID() == id
case *ChannelUpdateRejMsg:
return msg.ID() == id
default:
return false
}
}

if err = sub.Subscribe(relay, isUpdateRes); err != nil {
Expand Down Expand Up @@ -157,5 +163,9 @@ func (r *channelMsgRecv) Next(ctx context.Context) (channel.Index, ChannelMsg, e
if idx == -1 {
return 0, nil, errors.Errorf("channel connection received message from unexpected peer %v", env.Sender)
}
return channel.Index(idx), env.Msg.(ChannelMsg), nil // predicate must guarantee that the conversion is safe
msg, ok := env.Msg.(ChannelMsg)
if !ok {
return 0, nil, errors.Errorf("unexpected message type: expected ChannelMsg, got %T", env.Msg)
}
return channel.Index(idx), msg, nil // predicate must guarantee that the conversion is safe
}
33 changes: 20 additions & 13 deletions client/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,14 @@ func (c *Client) proposeTwoPartyChannel(

proposalID := proposal.Base().ProposalID
isResponse := func(e *wire.Envelope) bool {
acc, isAcc := e.Msg.(ChannelProposalAccept)
return (isAcc && acc.Base().ProposalID == proposalID) ||
(e.Msg.Type() == wire.ChannelProposalRej &&
e.Msg.(*ChannelProposalRejMsg).ProposalID == proposalID)
switch msg := e.Msg.(type) {
case ChannelProposalAccept:
return msg.Base().ProposalID == proposalID
case ChannelProposalRejMsg:
return msg.ProposalID == proposalID
default:
return false
}
}
receiver := wire.NewReceiver()
defer receiver.Close()
Expand Down Expand Up @@ -606,20 +610,23 @@ func (c *Client) mpcppParts(
) (parts []wallet.Address) {
switch p := prop.(type) {
case *LedgerChannelProposalMsg:
parts = participants(
p.Participant,
acc.(*LedgerChannelProposalAccMsg).Participant)
ledgerAcc, ok := acc.(*LedgerChannelProposalAccMsg)
if !ok {
c.log.Panicf("unexpected message type: expected *LedgerChannelProposalAccMsg, got %T", acc)
}
parts = participants(p.Participant, ledgerAcc.Participant)
case *SubChannelProposalMsg:
ch, ok := c.channels.Channel(p.Parent)
if !ok {
c.log.Panic("unknown parent channel ID")
}
parts = ch.Params().Parts
case *VirtualChannelProposalMsg:
parts = participants(
p.Proposer,
acc.(*VirtualChannelProposalAccMsg).Responder,
)
virtualAcc, ok := acc.(*VirtualChannelProposalAccMsg)
if !ok {
c.log.Panicf("unexpected message type: expected *VirtualChannelProposalAccMsg, got %T", acc)
}
parts = participants(p.Proposer, virtualAcc.Responder)
default:
c.log.Panicf("unhandled %T", p)
}
Expand Down Expand Up @@ -697,8 +704,8 @@ func (c *Client) fundSubchannel(ctx context.Context, prop *SubChannelProposalMsg
// enableVer0Cache enables caching of incoming version 0 signatures.
func enableVer0Cache(c wire.Cacher) *wire.Predicate {
p := func(m *wire.Envelope) bool {
return m.Msg.Type() == wire.ChannelUpdateAcc &&
m.Msg.(*ChannelUpdateAccMsg).Version == 0
msg, ok := m.Msg.(*ChannelUpdateAccMsg)
return ok && msg.Version == 0
}
c.Cache(&p)
return &p
Expand Down
24 changes: 20 additions & 4 deletions client/proposalopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,23 @@ var optNames = struct{ nonce, app, appData, fundingAgreement string }{nonce: "no
// App returns the option's configured app.
func (o ProposalOpts) App() channel.App {
if v := o[optNames.app]; v != nil {
return v.(channel.App)
app, ok := v.(channel.App)
if !ok {
log.Panicf("wrong type: expected channel.App, got %T", v)
}
return app
}
return channel.NoApp()
}

// AppData returns the option's configured app data.
func (o ProposalOpts) AppData() channel.Data {
if v := o[optNames.appData]; v != nil {
return v.(channel.Data)
data, ok := v.(channel.Data)
if !ok {
log.Panicf("wrong type: expected channel.Data, got %T", v)
}
return data
}
return channel.NoData()
}
Expand All @@ -63,7 +71,11 @@ func (o ProposalOpts) fundingAgreement() channel.Balances {
if !ok {
panic("Option FundingAgreement not set")
}
return a.(channel.Balances)
bals, ok := a.(channel.Balances)
if !ok {
log.Panicf("wrong type: expected channel.Balances, got %T", a)
}
return bals
}

// nonce returns the option's configured nonce share, or a random nonce share.
Expand All @@ -73,7 +85,11 @@ func (o ProposalOpts) nonce() NonceShare {
n = WithRandomNonce().nonce()
o[optNames.nonce] = n
}
return n.(NonceShare)
share, ok := n.(NonceShare)
if !ok {
log.Panicf("wrong type: expected NonceShare, got %T", n)
}
return share
}

// isNonce returns whether a ProposalOpts contains a manually set nonce.
Expand Down
3 changes: 2 additions & 1 deletion client/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func (c *Client) syncChannel(ctx context.Context, ch *persistence.Channel, p wir
defer recv.Close() // ignore error
id := ch.ID()
err = c.conn.Subscribe(recv, func(m *wire.Envelope) bool {
return m.Msg.Type() == wire.ChannelSync && m.Msg.(ChannelMsg).ID() == id
msg, ok := m.Msg.(*ChannelSyncMsg)
return ok && msg.ID() == id
})
if err != nil {
return errors.WithMessage(err, "subscribing on relay")
Expand Down
2 changes: 1 addition & 1 deletion client/test/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (b *MockBackend) removeSubscription(ch channel.ID, sub *MockSubscription) {

// MockSubscription is a subscription for MockBackend.
type MockSubscription struct {
ctx context.Context
ctx context.Context //nolint:containedctx // This is just done for testing. Could be revised.
events chan channel.AdjudicatorEvent
err chan error
onClose func()
Expand Down
3 changes: 1 addition & 2 deletions client/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,7 @@ func (c *Channel) OnUpdate(cb func(from, to *channel.State)) {
// * Sub-allocations do not change.
func (c *Channel) validTwoPartyUpdate(up ChannelUpdate, sigIdx channel.Index) error {
if up.ActorIdx != sigIdx {
return errors.Errorf(
"Currently, only update proposals with the proposing peer as actor are allowed.")
return errors.New("invalid proposer")
}
if err := channel.SubAllocsAssertEqual(c.machine.State().Locked, up.State.Locked); err != nil {
return errors.WithMessage(err, "sub-allocation changed")
Expand Down
Loading

0 comments on commit c749e1a

Please sign in to comment.