-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Consolidating codec.go registrations. Moving Acknowledgement result/error to its own file * Updating CHANGELOG.md with #7949 improvement as requested * revert removing acknowledgement proto to its own file * update changelog * remove unnecessary pb.go file Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
- Loading branch information
1 parent
3832860
commit 0792db7
Showing
6 changed files
with
116 additions
and
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package types | ||
|
||
import ( | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
// NewResultAcknowledgement returns a new instance of Acknowledgement using an Acknowledgement_Result | ||
// type in the Response field. | ||
func NewResultAcknowledgement(result []byte) Acknowledgement { | ||
return Acknowledgement{ | ||
Response: &Acknowledgement_Result{ | ||
Result: result, | ||
}, | ||
} | ||
} | ||
|
||
// NewErrorAcknowledgement returns a new instance of Acknowledgement using an Acknowledgement_Error | ||
// type in the Response field. | ||
func NewErrorAcknowledgement(err string) Acknowledgement { | ||
return Acknowledgement{ | ||
Response: &Acknowledgement_Error{ | ||
Error: err, | ||
}, | ||
} | ||
} | ||
|
||
// GetBytes is a helper for serialising acknowledgements | ||
func (ack Acknowledgement) GetBytes() []byte { | ||
return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&ack)) | ||
} | ||
|
||
// ValidateBasic performs a basic validation of the acknowledgement | ||
func (ack Acknowledgement) ValidateBasic() error { | ||
switch resp := ack.Response.(type) { | ||
case *Acknowledgement_Result: | ||
if len(resp.Result) == 0 { | ||
return sdkerrors.Wrap(ErrInvalidAcknowledgement, "acknowledgement result cannot be empty") | ||
} | ||
case *Acknowledgement_Error: | ||
if strings.TrimSpace(resp.Error) == "" { | ||
return sdkerrors.Wrap(ErrInvalidAcknowledgement, "acknowledgement error cannot be empty") | ||
} | ||
default: | ||
return sdkerrors.Wrapf(ErrInvalidAcknowledgement, "unsupported acknowledgement response field type %T", resp) | ||
} | ||
return nil | ||
} |
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,63 @@ | ||
package types_test | ||
|
||
import "github.com/cosmos/cosmos-sdk/x/ibc/core/04-channel/types" | ||
|
||
// tests acknowledgement.ValidateBasic and acknowledgement.GetBytes | ||
func (suite TypesTestSuite) TestAcknowledgement() { | ||
testCases := []struct { | ||
name string | ||
ack types.Acknowledgement | ||
expPass bool | ||
}{ | ||
{ | ||
"valid successful ack", | ||
types.NewResultAcknowledgement([]byte("success")), | ||
true, | ||
}, | ||
{ | ||
"valid failed ack", | ||
types.NewErrorAcknowledgement("error"), | ||
true, | ||
}, | ||
{ | ||
"empty successful ack", | ||
types.NewResultAcknowledgement([]byte{}), | ||
false, | ||
}, | ||
{ | ||
"empty faied ack", | ||
types.NewErrorAcknowledgement(" "), | ||
false, | ||
}, | ||
{ | ||
"nil response", | ||
types.Acknowledgement{ | ||
Response: nil, | ||
}, | ||
false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
suite.Run(tc.name, func() { | ||
suite.SetupTest() | ||
|
||
err := tc.ack.ValidateBasic() | ||
|
||
if tc.expPass { | ||
suite.Require().NoError(err) | ||
} else { | ||
suite.Require().Error(err) | ||
} | ||
|
||
// expect all acks to be able to be marshaled | ||
suite.NotPanics(func() { | ||
bz := tc.ack.GetBytes() | ||
suite.Require().NotNil(bz) | ||
}) | ||
}) | ||
} | ||
|
||
} |
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