2023-02-01 21:44:44 +00:00
|
|
|
// stm: #integration
|
|
|
|
package itests
|
|
|
|
|
|
|
|
import (
|
2023-02-02 21:37:58 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-02-01 21:44:44 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-02-02 21:37:58 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2023-02-01 21:44:44 +00:00
|
|
|
"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"
|
2023-02-02 21:37:58 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/builtin/v10/eam"
|
|
|
|
"github.com/filecoin-project/go-state-types/cbor"
|
2023-02-01 21:44:44 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2023-02-02 21:37:58 +00:00
|
|
|
type marshalable interface {
|
|
|
|
cbor.Marshaler
|
|
|
|
cbor.Unmarshaler
|
|
|
|
}
|
|
|
|
|
2023-02-01 21:44:44 +00:00
|
|
|
type testCase struct {
|
|
|
|
ActorKey string
|
|
|
|
MethodNum abi.MethodNum
|
2023-02-02 21:37:58 +00:00
|
|
|
retVal marshalable
|
2023-02-01 21:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Used './lotus state replay --show-trace <msg-cid>' to get params/return to decode.
|
|
|
|
func TestDecodeParams(t *testing.T) {
|
2023-02-02 21:37:58 +00:00
|
|
|
testCborBytes := abi.CborBytes([]byte{1, 2, 3})
|
|
|
|
|
2023-02-01 21:44:44 +00:00
|
|
|
testCases := []testCase{
|
|
|
|
{
|
|
|
|
ActorKey: manifest.EvmKey,
|
|
|
|
MethodNum: builtin.MethodsEVM.InvokeContract,
|
2023-02-02 21:37:58 +00:00
|
|
|
retVal: &testCborBytes,
|
2023-02-01 21:44:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ActorKey: manifest.EamKey,
|
|
|
|
MethodNum: builtin.MethodsEAM.CreateExternal,
|
2023-02-02 21:37:58 +00:00
|
|
|
retVal: &testCborBytes,
|
2023-02-01 21:44:44 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-02-02 21:37:58 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
if err := tc.retVal.MarshalCBOR(buf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-02-01 21:44:44 +00:00
|
|
|
|
2023-02-02 21:37:58 +00:00
|
|
|
paramString, err := cli.JsonParams(actorCodeCid, tc.MethodNum, buf.Bytes())
|
2023-02-01 21:44:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-02 21:37:58 +00:00
|
|
|
jsonParams, err := json.MarshalIndent(tc.retVal, "", " ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(jsonParams), paramString)
|
2023-02-01 21:44:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeReturn(t *testing.T) {
|
2023-02-02 21:37:58 +00:00
|
|
|
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{},
|
|
|
|
}
|
|
|
|
|
2023-02-01 21:44:44 +00:00
|
|
|
testCases := []testCase{
|
|
|
|
{
|
|
|
|
ActorKey: manifest.EvmKey,
|
|
|
|
MethodNum: builtin.MethodsEVM.InvokeContract,
|
2023-02-02 21:37:58 +00:00
|
|
|
retVal: &testCborBytes,
|
2023-02-01 21:44:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ActorKey: manifest.EamKey,
|
|
|
|
MethodNum: builtin.MethodsEAM.CreateExternal,
|
2023-02-02 21:37:58 +00:00
|
|
|
retVal: &testReturn,
|
2023-02-01 21:44:44 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-02-02 21:37:58 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
if err := tc.retVal.MarshalCBOR(buf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-02-01 21:44:44 +00:00
|
|
|
|
2023-02-02 21:37:58 +00:00
|
|
|
returnString, err := cli.JsonReturn(actorCodeCid, tc.MethodNum, buf.Bytes())
|
2023-02-01 21:44:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-02 21:37:58 +00:00
|
|
|
jsonReturn, err := json.MarshalIndent(tc.retVal, "", " ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(jsonReturn), returnString)
|
2023-02-01 21:44:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|