Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Asset.UnmarshallBinary not working #36

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions channel/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ func (a Asset) MapKey() AssetMapKey {
// MarshalBinary marshals the asset into its binary representation.
func (a Asset) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
err := perunio.Encode(&buf, &a.AssetHolder, a.ChainID)
err := perunio.Encode(&buf, a.ChainID, &a.AssetHolder)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

// UnmarshalBinary unmarshals the asset from its binary representation.
func (a Asset) UnmarshalBinary(data []byte) error {
func (a *Asset) UnmarshalBinary(data []byte) error {
buf := bytes.NewBuffer(data)
return perunio.Decode(buf, &a.ChainID, &a.AssetHolder)
}
Expand Down
18 changes: 18 additions & 0 deletions channel/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package channel_test

import (
"context"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/accounts"
Expand Down Expand Up @@ -124,3 +125,20 @@ func Test_Asset_GenericMarshaler(t *testing.T) {
wiretest.GenericMarshalerTest(t, &asset)
}
}

func TestMarshalling(t *testing.T) {
rng := pkgtest.Prng(t)
assetIn := ethchannel.Asset{
ChainID: ethchannel.ChainID{
big.NewInt(rng.Int63()),
},
AssetHolder: ethwallettest.NewRandomAddress(rng),
}
bytes, err := assetIn.MarshalBinary()
require.NoError(t, err)
var assetOut ethchannel.Asset
err = assetOut.UnmarshalBinary(bytes)
require.NoError(t, err)

require.Equal(t, assetIn, assetOut)
}