Skip to content

Commit

Permalink
Merge pull request #10173 from filecoin-project/gstuart/check-params
Browse files Browse the repository at this point in the history
chore: cli: Confirm tooling that assumes knowledge of method params / returns don't break
  • Loading branch information
arajasek authored Feb 6, 2023
2 parents 3e30e7a + ffc1c33 commit 725f22a
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ workflows:
suite: itest-deals
target: "./itests/deals_test.go"

- test:
name: test-itest-decode_params
suite: itest-decode_params
target: "./itests/decode_params_test.go"

- test:
name: test-itest-dup_mpool_messages
suite: itest-dup_mpool_messages
Expand Down
8 changes: 4 additions & 4 deletions cli/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ func ComputeStateHTMLTempl(w io.Writer, ts *types.TipSet, o *api.ComputeStateOut
"GetMethod": getMethod,
"ToFil": toFil,
"JsonParams": JsonParams,
"JsonReturn": jsonReturn,
"JsonReturn": JsonReturn,
"IsSlow": isSlow,
"IsVerySlow": isVerySlow,
"IntExit": func(i exitcode.ExitCode) int64 { return int64(i) },
Expand Down Expand Up @@ -1440,7 +1440,7 @@ func JsonParams(code cid.Cid, method abi.MethodNum, params []byte) (string, erro
return string(b), err
}

func jsonReturn(code cid.Cid, method abi.MethodNum, ret []byte) (string, error) {
func JsonReturn(code cid.Cid, method abi.MethodNum, ret []byte) (string, error) {
methodMeta, found := filcns.NewActorRegistry().Methods[code][method] // TODO: use remote
if !found {
return "", fmt.Errorf("method %d not found on actor %s", method, code)
Expand Down Expand Up @@ -1549,7 +1549,7 @@ func printReceiptReturn(ctx context.Context, api v0api.FullNode, m *types.Messag
return err
}

jret, err := jsonReturn(act.Code, m.Method, r.Return)
jret, err := JsonReturn(act.Code, m.Method, r.Return)
if err != nil {
return err
}
Expand Down Expand Up @@ -1689,7 +1689,7 @@ var StateCallCmd = &cli.Command{
return xerrors.Errorf("getting actor: %w", err)
}

retStr, err := jsonReturn(act.Code, abi.MethodNum(method), ret.MsgRct.Return)
retStr, err := JsonReturn(act.Code, abi.MethodNum(method), ret.MsgRct.Return)
if err != nil {
return xerrors.Errorf("decoding return: %w", err)
}
Expand Down
124 changes: 124 additions & 0 deletions itests/decode_params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// stm: #integration
package itests

import (
"bytes"
"encoding/json"
"testing"

"github.com/stretchr/testify/require"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
actorstypes "github.com/filecoin-project/go-state-types/actors"
"github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/builtin/v10/eam"
"github.com/filecoin-project/go-state-types/cbor"
"github.com/filecoin-project/go-state-types/manifest"

"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/cli"
)

type marshalable interface {
cbor.Marshaler
cbor.Unmarshaler
}

type testCase struct {
ActorKey string
MethodNum abi.MethodNum
retVal marshalable
}

// Used './lotus state replay --show-trace <msg-cid>' to get params/return to decode.
func TestDecodeParams(t *testing.T) {
testCborBytes := abi.CborBytes([]byte{1, 2, 3})

testCases := []testCase{
{
ActorKey: manifest.EvmKey,
MethodNum: builtin.MethodsEVM.InvokeContract,
retVal: &testCborBytes,
},
{
ActorKey: manifest.EamKey,
MethodNum: builtin.MethodsEAM.CreateExternal,
retVal: &testCborBytes,
},
}

for _, _tc := range testCases {
tc := _tc
t.Run(tc.ActorKey+" "+tc.MethodNum.String(), func(t *testing.T) {
av, err := actorstypes.VersionForNetwork(build.TestNetworkVersion)
require.NoError(t, err)
actorCodeCid, found := actors.GetActorCodeID(av, tc.ActorKey)
require.True(t, found)

buf := bytes.NewBuffer(nil)
if err := tc.retVal.MarshalCBOR(buf); err != nil {
t.Fatal(err)
}

paramString, err := cli.JsonParams(actorCodeCid, tc.MethodNum, buf.Bytes())
require.NoError(t, err)

jsonParams, err := json.MarshalIndent(tc.retVal, "", " ")
require.NoError(t, err)
require.Equal(t, string(jsonParams), paramString)
})
}
}

func TestDecodeReturn(t *testing.T) {
testCborBytes := abi.CborBytes([]byte{1, 2, 3})

robustAddr, err := address.NewIDAddress(12345)
require.NoError(t, err)

//ethAddr, err := ethtypes.ParseEthAddress("d4c5fb16488Aa48081296299d54b0c648C9333dA")
//require.NoError(t, err)

testReturn := eam.CreateExternalReturn{
ActorID: 12345,
RobustAddress: &robustAddr,
EthAddress: [20]byte{},
}

testCases := []testCase{
{
ActorKey: manifest.EvmKey,
MethodNum: builtin.MethodsEVM.InvokeContract,
retVal: &testCborBytes,
},
{
ActorKey: manifest.EamKey,
MethodNum: builtin.MethodsEAM.CreateExternal,
retVal: &testReturn,
},
}

for _, _tc := range testCases {
tc := _tc
t.Run(tc.ActorKey+" "+tc.MethodNum.String(), func(t *testing.T) {
av, err := actorstypes.VersionForNetwork(build.TestNetworkVersion)
require.NoError(t, err)
actorCodeCid, found := actors.GetActorCodeID(av, tc.ActorKey)
require.True(t, found)

buf := bytes.NewBuffer(nil)
if err := tc.retVal.MarshalCBOR(buf); err != nil {
t.Fatal(err)
}

returnString, err := cli.JsonReturn(actorCodeCid, tc.MethodNum, buf.Bytes())
require.NoError(t, err)

jsonReturn, err := json.MarshalIndent(tc.retVal, "", " ")
require.NoError(t, err)
require.Equal(t, string(jsonReturn), returnString)
})
}
}

0 comments on commit 725f22a

Please sign in to comment.