Skip to content

Commit

Permalink
channel: Use marshal instead of encoding
Browse files Browse the repository at this point in the history
- For converting data to/from binary form, in order to make a deep copy
  of it.

Signed-off-by: Manoranjith <[email protected]>
  • Loading branch information
Manoranjith committed Jan 19, 2022
1 parent 5f49e74 commit 539e1e8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
8 changes: 3 additions & 5 deletions channel/actionmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
package channel

import (
"bytes"
"log"

"github.com/pkg/errors"

"perun.network/go-perun/wallet"
"perun.network/go-perun/wire/perunio"
)

// An ActionMachine is the channel pushdown automaton around an ActionApp.
Expand Down Expand Up @@ -128,12 +126,12 @@ func (m *ActionMachine) Clone() *ActionMachine {
clonedActions := make([]Action, m.N())
for i, action := range m.stagingActions {
if action != nil {
var buff bytes.Buffer
if err := perunio.Encode(&buff, action); err != nil {
marshalledAction, err := action.MarshalBinary()
if err != nil {
log.Panicf("Could not encode action: %v", err)
}
clonedAction := m.app.NewAction()
if err := perunio.Decode(&buff, clonedAction); err != nil {
if err := clonedAction.UnmarshalBinary(marshalledAction); err != nil {
log.Panicf("App could not decode Action: %v", err)
}
clonedActions[i] = clonedAction
Expand Down
9 changes: 4 additions & 5 deletions channel/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package channel

import (
"bytes"
stdio "io"
"math/big"

Expand Down Expand Up @@ -148,14 +147,14 @@ func NewParamsUnsafe(challengeDuration uint64, parts []wallet.Address, app App,
// Clone returns a deep copy of Params.
func (p *Params) Clone() *Params {
clonedParts := make([]wallet.Address, len(p.Parts))
for i, v := range p.Parts {
var buff bytes.Buffer
if err := perunio.Encode(&buff, v); err != nil {
for i, part := range p.Parts {
marshalledAddr, err := part.MarshalBinary()
if err != nil {
log.WithError(err).Panic("Could not encode part")
}

addr := wallet.NewAddress()
if err := perunio.Decode(&buff, addr); err != nil {
if err := addr.UnmarshalBinary(marshalledAddr); err != nil {
log.WithError(err).Panic("Could not clone params' addresses")
}
clonedParts[i] = addr
Expand Down

0 comments on commit 539e1e8

Please sign in to comment.