diff --git a/api/api_errors.go b/api/api_errors.go index 95ae9f6183..012c3a57a9 100644 --- a/api/api_errors.go +++ b/api/api_errors.go @@ -5,6 +5,7 @@ import ( "reflect" "github.com/filecoin-project/go-jsonrpc" + "golang.org/x/xerrors" ) const executionRevertedDefaultMsg = "execution reverted" @@ -43,14 +44,15 @@ var ( // should back off and try again later. ErrF3NotReady = errF3NotReady{} - _ error = (*ErrOutOfGas)(nil) - _ error = (*ErrActorNotFound)(nil) - _ error = (*errF3Disabled)(nil) - _ error = (*errF3ParticipationTicketInvalid)(nil) - _ error = (*errF3ParticipationTicketExpired)(nil) - _ error = (*errF3ParticipationIssuerMismatch)(nil) - _ error = (*errF3NotReady)(nil) - _ error = (*ErrExecutionRevertedWithData)(nil) + _ error = (*ErrOutOfGas)(nil) + _ error = (*ErrActorNotFound)(nil) + _ error = (*errF3Disabled)(nil) + _ error = (*errF3ParticipationTicketInvalid)(nil) + _ error = (*errF3ParticipationTicketExpired)(nil) + _ error = (*errF3ParticipationIssuerMismatch)(nil) + _ error = (*errF3NotReady)(nil) + _ error = (*ErrExecutionRevertedWithData)(nil) + _ jsonrpc.ErrorCodec = (*ErrExecutionRevertedWithData)(nil) ) func init() { @@ -117,20 +119,42 @@ type errF3NotReady struct{} func (errF3NotReady) Error() string { return "f3 isn't yet ready to participate" } type ErrExecutionRevertedWithData struct { - message string - data string + Message string + Data string } // Error returns the error message. -func (e *ErrExecutionRevertedWithData) Error() string { return e.message } +func (e *ErrExecutionRevertedWithData) Error() string { return e.Message } -// ErrorData returns the error data. -func (e *ErrExecutionRevertedWithData) ErrorData() interface{} { return e.data } +// FromJSONRPCError converts a JSONRPCError to ErrExecutionRevertedWithData. +func (e *ErrExecutionRevertedWithData) FromJSONRPCError(jerr jsonrpc.JSONRPCError) error { + if jerr.Code != EExecutionRevertedWithData { + return nil + } + + data, ok := jerr.Data.(string) + if !ok { + return xerrors.Errorf("expected string data in execution reverted error, got %T", jerr.Data) + } + + e.Message = jerr.Message + e.Data = data + return nil +} + +// ToJSONRPCError converts ErrExecutionRevertedWithData to a JSONRPCError. +func (e *ErrExecutionRevertedWithData) ToJSONRPCError() (jsonrpc.JSONRPCError, error) { + return jsonrpc.JSONRPCError{ + Code: EExecutionRevertedWithData, + Message: e.Message, + Data: e.Data, + }, nil +} // NewErrExecutionRevertedWithData creates a new ErrExecutionRevertedWithData. func NewErrExecutionRevertedWithData(data string) *ErrExecutionRevertedWithData { return &ErrExecutionRevertedWithData{ - message: executionRevertedDefaultMsg, - data: data, + Message: executionRevertedDefaultMsg, + Data: data, } } diff --git a/go.mod b/go.mod index 6811212a94..80000f42f7 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ replace github.com/filecoin-project/test-vectors => ./extern/test-vectors // pro replace github.com/filecoin-project/filecoin-ffi => ./extern/filecoin-ffi // provided via a git submodule -replace github.com/filecoin-project/go-jsonrpc => github.com/virajbhartiya/go-jsonrpc v0.0.0-20241016130229-4e33c9f29a6a +replace github.com/filecoin-project/go-jsonrpc => github.com/virajbhartiya/go-jsonrpc v0.0.0-20241022104347-ef506b1630b0 require ( contrib.go.opencensus.io/exporter/prometheus v0.4.2 diff --git a/go.sum b/go.sum index 587e0192a6..b8b6aacfef 100644 --- a/go.sum +++ b/go.sum @@ -1279,8 +1279,8 @@ github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8W github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU= github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= -github.com/virajbhartiya/go-jsonrpc v0.0.0-20241016130229-4e33c9f29a6a h1:7iKYazcDTV1Gs3PZ3KM36ecW86eHXwf1hXhjt+obUt8= -github.com/virajbhartiya/go-jsonrpc v0.0.0-20241016130229-4e33c9f29a6a/go.mod h1:lAUpS8BSVtKaA8+/CFUMA5dokMiSM7n0ehf8bHOFdpE= +github.com/virajbhartiya/go-jsonrpc v0.0.0-20241022104347-ef506b1630b0 h1:V+4+u8WmKprL4cvaYhy8S5DzMBp4JAt3qm9ZuBq8r5E= +github.com/virajbhartiya/go-jsonrpc v0.0.0-20241022104347-ef506b1630b0/go.mod h1:lAUpS8BSVtKaA8+/CFUMA5dokMiSM7n0ehf8bHOFdpE= github.com/warpfork/go-testmark v0.12.1 h1:rMgCpJfwy1sJ50x0M0NgyphxYYPMOODIJHhsXyEHU0s= github.com/warpfork/go-testmark v0.12.1/go.mod h1:kHwy7wfvGSPh1rQJYKayD4AbtNaeyZdcGi9tNJTaa5Y= github.com/warpfork/go-wish v0.0.0-20180510122957-5ad1f5abf436/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw= diff --git a/itests/fevm_test.go b/itests/fevm_test.go index 6aca0850e2..e2fef79bb9 100644 --- a/itests/fevm_test.go +++ b/itests/fevm_test.go @@ -7,7 +7,6 @@ import ( "encoding/binary" "encoding/hex" "encoding/json" - "errors" "fmt" "os" "testing" @@ -16,7 +15,6 @@ import ( "github.com/stretchr/testify/require" "github.com/filecoin-project/go-address" - "github.com/filecoin-project/go-jsonrpc" "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" builtintypes "github.com/filecoin-project/go-state-types/builtin" @@ -1043,19 +1041,13 @@ func TestFEVMErrorParsing(t *testing.T) { t.Run(sig, func(t *testing.T) { entryPoint := kit.CalcFuncSignature(sig) t.Run("EthCall", func(t *testing.T) { - _, err := e.EthCall(ctx, ethtypes.EthCall{ + _, err = e.EthCall(ctx, ethtypes.EthCall{ To: &contractAddrEth, Data: entryPoint, }, ethtypes.NewEthBlockNumberOrHashFromPredefined("latest")) require.Error(t, err) - var dataErr jsonrpc.ErrorWithData - if errors.As(err, &dataErr) { - errData := dataErr.ErrorData() - require.Contains(t, errData, expected, "Error data should contain the expected error") - } else { - t.Fatalf("Expected error to implement jsonrpc.ErrorWithData") - } + require.Contains(t, err.Error(), expected, "Error data should contain the expected error") }) t.Run("EthEstimateGas", func(t *testing.T) { gasParams, err := json.Marshal(ethtypes.EthEstimateGasParams{Tx: ethtypes.EthCall{ @@ -1066,14 +1058,7 @@ func TestFEVMErrorParsing(t *testing.T) { _, err = e.EthEstimateGas(ctx, gasParams) require.Error(t, err) - - var dataErr jsonrpc.ErrorWithData - if errors.As(err, &dataErr) { - errData := dataErr.ErrorData() - require.Contains(t, errData, expected, "Error data should contain the expected error") - } else { - t.Fatalf("Expected error to implement jsonrpc.ErrorWithData") - } + require.Contains(t, err.Error(), expected, "Error data should contain the expected error") }) }) } @@ -1490,13 +1475,12 @@ func TestEthCall(t *testing.T) { _, err = client.EthCall(ctx, callParams, ethtypes.NewEthBlockNumberOrHashFromNumber(latestBlock)) require.Error(t, err) - var dataErr jsonrpc.ErrorWithData - ok := errors.As(err, &dataErr) - require.True(t, ok, "Expected error to implement jsonrpc.DataError") - require.Contains(t, dataErr.Error(), "execution reverted", "Expected 'execution reverted' message") + var dataErr *api.ErrExecutionRevertedWithData + require.ErrorAs(t, err, &dataErr, "Expected error to be ErrExecutionRevertedWithData") + require.Contains(t, dataErr.Message, "execution reverted", "Expected 'execution reverted' message") // Get the error data - require.Equal(t, dataErr.ErrorData(), "DivideByZero()", "Expected error data to contain 'DivideByZero()'") + require.Equal(t, dataErr.Data, "DivideByZero()", "Expected error data to contain 'DivideByZero()'") }) } @@ -1543,10 +1527,10 @@ func TestEthEstimateGas(t *testing.T) { if tc.expectedError != "" { require.Error(t, err) - var dataErr jsonrpc.ErrorWithData - require.ErrorAs(t, err, &dataErr, "Expected error to implement jsonrpc.DataError") - require.Equal(t, tc.expectedErrMsg, dataErr.Error()) - require.Contains(t, tc.expectedError, dataErr.ErrorData()) + var dataErr *api.ErrExecutionRevertedWithData + require.ErrorAs(t, err, &dataErr, "Expected error to be ErrExecutionRevertedWithData") + require.Equal(t, tc.expectedErrMsg, dataErr.Message) + require.Contains(t, tc.expectedError, dataErr.Data) } }) }