-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10173 from filecoin-project/gstuart/check-params
chore: cli: Confirm tooling that assumes knowledge of method params / returns don't break
- Loading branch information
Showing
3 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |