From e5a5fcb19db312cd007176cd3b796a0a5f7b0e41 Mon Sep 17 00:00:00 2001 From: Manoranjith Date: Mon, 6 Dec 2021 14:26:22 +0530 Subject: [PATCH] :recycle: Update other pkgs to use App,Action as binary (Un)marshaler Signed-off-by: Manoranjith --- apps/payment/app.go | 8 +++----- apps/payment/resolver_internal_test.go | 7 ++++--- backend/ethereum/channel/backend.go | 7 ++++--- channel/actionmachine.go | 7 ++++--- channel/state.go | 3 ++- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/apps/payment/app.go b/apps/payment/app.go index a704ad542..4bd8a35fd 100644 --- a/apps/payment/app.go +++ b/apps/payment/app.go @@ -16,8 +16,6 @@ package payment // import "perun.network/go-perun/apps/payment" import ( - "io" - "github.com/pkg/errors" "perun.network/go-perun/channel" @@ -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 diff --git a/apps/payment/resolver_internal_test.go b/apps/payment/resolver_internal_test.go index e4c2d7242..1b61cccd1 100644 --- a/apps/payment/resolver_internal_test.go +++ b/apps/payment/resolver_internal_test.go @@ -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)) }) diff --git a/backend/ethereum/channel/backend.go b/backend/ethereum/channel/backend.go index c4ec3507e..396a3d996 100644 --- a/backend/ethereum/channel/backend.go +++ b/backend/ethereum/channel/backend.go @@ -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" ) @@ -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{ @@ -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) } diff --git a/channel/actionmachine.go b/channel/actionmachine.go index e0df5cf2d..650f95516 100644 --- a/channel/actionmachine.go +++ b/channel/actionmachine.go @@ -20,6 +20,7 @@ import ( "github.com/pkg/errors" + perunio "perun.network/go-perun/pkg/io" "perun.network/go-perun/wallet" ) @@ -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 diff --git a/channel/state.go b/channel/state.go index 4c49a213b..7d6963b59 100644 --- a/channel/state.go +++ b/channel/state.go @@ -15,6 +15,7 @@ package channel import ( + "encoding" "io" "github.com/pkg/errors" @@ -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")