Skip to content

Commit

Permalink
Merge pull request #36 from DragonDev1906/asset-unmarshal
Browse files Browse the repository at this point in the history
Fix `Asset.UnmarshallBinary` not working

Signed-off-by: Steffen Rattay <[email protected]>
  • Loading branch information
RmbRT authored Jan 6, 2023
2 parents 67177a4 + fd8b6cf commit 5bbe33a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
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)
}

0 comments on commit 5bbe33a

Please sign in to comment.