Skip to content

Commit

Permalink
test decodePayload
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Feb 7, 2024
1 parent fefa8f3 commit 41caa29
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
40 changes: 40 additions & 0 deletions node/impl/full/eth_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package full

import (
"bytes"
"encoding/hex"
"testing"

"github.com/ipfs/go-cid"
"github.com/multiformats/go-multicodec"
"github.com/stretchr/testify/require"
cbg "github.com/whyrusleeping/cbor-gen"

"github.com/filecoin-project/go-state-types/big"

Expand Down Expand Up @@ -177,3 +180,40 @@ func TestABIEncoding(t *testing.T) {

require.Equal(t, expectedBytes, encodeAsABIHelper(22, 81, dataBytes))
}

func TestDecodePayload(t *testing.T) {
// "empty"
b, err := decodePayload(nil, 0)
require.NoError(t, err)
require.Empty(t, b)

// raw empty
_, err = decodePayload(nil, uint64(multicodec.Raw))
require.NoError(t, err)
require.Empty(t, b)

// raw non-empty
b, err = decodePayload([]byte{1}, uint64(multicodec.Raw))
require.NoError(t, err)
require.EqualValues(t, b, []byte{1})

// Invalid cbor bytes
_, err = decodePayload(nil, uint64(multicodec.DagCbor))
require.Error(t, err)

// valid cbor bytes
var w bytes.Buffer
cbg.WriteByteArray(&w, []byte{1})
b, err = decodePayload(w.Bytes(), uint64(multicodec.DagCbor))
require.NoError(t, err)
require.EqualValues(t, b, []byte{1})

// regular cbor also works.
b, err = decodePayload(w.Bytes(), uint64(multicodec.Cbor))
require.NoError(t, err)
require.EqualValues(t, b, []byte{1})

// random codec should fail
_, err = decodePayload(w.Bytes(), 42)
require.Error(t, err)
}
4 changes: 0 additions & 4 deletions node/impl/full/eth_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ import (

// decodePayload is a utility function which decodes the payload using the given codec
func decodePayload(payload []byte, codec uint64) (ethtypes.EthBytes, error) {
if len(payload) == 0 {
return nil, nil
}

switch multicodec.Code(codec) {
case multicodec.Identity:
return nil, nil
Expand Down

0 comments on commit 41caa29

Please sign in to comment.