Skip to content

Commit

Permalink
♻️ Update other pkgs to use App,Action as binary (Un)marshaler
Browse files Browse the repository at this point in the history
Signed-off-by: Manoranjith <[email protected]>
  • Loading branch information
Manoranjith committed Dec 6, 2021
1 parent e6853ba commit e5a5fcb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
8 changes: 3 additions & 5 deletions apps/payment/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package payment // import "perun.network/go-perun/apps/payment"

import (
"io"

"github.com/pkg/errors"

"perun.network/go-perun/channel"
Expand All @@ -35,9 +33,9 @@ func (a *App) Def() wallet.Address {
return a.Addr
}

// DecodeData does not read anything from the reader and returns new NoData.
func (a *App) DecodeData(io.Reader) (channel.Data, error) {
return Data(), nil
// NewData returns an instance of noData.
func (a *App) NewData() channel.Data {
return Data()
}

// ValidTransition checks that money flows only from the actor to the other
Expand Down
7 changes: 4 additions & 3 deletions apps/payment/resolver_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ func TestData(t *testing.T) {

assert.NotPanics(func() {
data := Data()
assert.Nil(data.Encode(nil))
_, err := data.MarshalBinary()
assert.Nil(err)
})

assert.NotPanics(func() {
app := new(App)
data, err := app.DecodeData(nil)
assert.NoError(err)
data := app.NewData()
assert.NoError(data.UnmarshalBinary(nil))
assert.NotNil(data)
assert.True(IsData(data))
})
Expand Down
7 changes: 4 additions & 3 deletions backend/ethereum/channel/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"perun.network/go-perun/backend/ethereum/bindings/adjudicator"
ethwallet "perun.network/go-perun/backend/ethereum/wallet"
"perun.network/go-perun/channel"
perunio "perun.network/go-perun/pkg/io"
"perun.network/go-perun/wallet"
)

Expand Down Expand Up @@ -197,7 +198,7 @@ func ToEthState(s *channel.State) adjudicator.ChannelState {
log.Panic("invalid allocation dimensions")
}
appData := new(bytes.Buffer)
if err := s.Data.Encode(appData); err != nil {
if err := perunio.Encode(appData, s.Data); err != nil {
log.Panicf("error encoding app data: %v", err)
}
return adjudicator.ChannelState{
Expand Down Expand Up @@ -262,8 +263,8 @@ func FromEthState(app channel.App, s *adjudicator.ChannelState) channel.State {
log.Panic("invalid allocation dimensions")
}

data, err := app.DecodeData(bytes.NewReader(s.AppData))
if err != nil {
data := app.NewData()
if err := perunio.Decode(bytes.NewReader(s.AppData), data); err != nil {
log.Panicf("decoding app data: %v", err)
}

Expand Down
7 changes: 4 additions & 3 deletions channel/actionmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/pkg/errors"

perunio "perun.network/go-perun/pkg/io"
"perun.network/go-perun/wallet"
)

Expand Down Expand Up @@ -128,11 +129,11 @@ func (m *ActionMachine) Clone() *ActionMachine {
for i, action := range m.stagingActions {
if action != nil {
var buff bytes.Buffer
if err := action.Encode(&buff); err != nil {
if err := perunio.Encode(&buff, action); err != nil {
log.Panicf("Could not encode action: %v", err)
}
clonedAction, err := m.app.DecodeAction(&buff)
if err != nil {
clonedAction := m.app.NewAction()
if err := perunio.Decode(&buff, clonedAction); err != nil {
log.Panicf("App could not decode Action: %v", err)
}
clonedActions[i] = clonedAction
Expand Down
3 changes: 2 additions & 1 deletion channel/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package channel

import (
"encoding"
"io"

"github.com/pkg/errors"
Expand Down Expand Up @@ -139,7 +140,7 @@ func (s *State) Equal(t *State) error {
if err := s.Allocation.Equal(&t.Allocation); err != nil {
return errors.WithMessage(err, "different Allocations")
}
if ok, err := perunio.EqualEncoding(s.Data, t.Data); err != nil {
if ok, err := perunio.EqualBinary(s.Data, t.Data); err != nil {
return errors.WithMessage(err, "comparing App data encoding")
} else if !ok {
return errors.Errorf("different App data")
Expand Down

0 comments on commit e5a5fcb

Please sign in to comment.