Merge pull request #10173 from filecoin-project/gstuart/check-params
chore: cli: Confirm tooling that assumes knowledge of method params / returns don't break
This commit is contained in:
commit
725f22a344
@ -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
|
||||
|
@ -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) },
|
||||
@ -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)
|
||||
@ -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
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
|
124
itests/decode_params_test.go
Normal file
124
itests/decode_params_test.go
Normal file
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user