|
| 1 | +package types_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" |
| 7 | + "github.com/stretchr/testify/suite" |
| 8 | + abcitypes "github.com/tendermint/tendermint/abci/types" |
| 9 | + tmprotostate "github.com/tendermint/tendermint/proto/tendermint/state" |
| 10 | + tmstate "github.com/tendermint/tendermint/state" |
| 11 | + |
| 12 | + "github.com/cosmos/ibc-go/v2/modules/apps/transfer/types" |
| 13 | + ibctesting "github.com/cosmos/ibc-go/v2/testing" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + gasUsed = uint64(100) |
| 18 | + gasWanted = uint64(100) |
| 19 | +) |
| 20 | + |
| 21 | +type TypesTestSuite struct { |
| 22 | + suite.Suite |
| 23 | + |
| 24 | + coordinator *ibctesting.Coordinator |
| 25 | + |
| 26 | + chainA *ibctesting.TestChain |
| 27 | + chainB *ibctesting.TestChain |
| 28 | +} |
| 29 | + |
| 30 | +func (suite *TypesTestSuite) SetupTest() { |
| 31 | + suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) |
| 32 | + |
| 33 | + suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0)) |
| 34 | + suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) |
| 35 | +} |
| 36 | + |
| 37 | +func TestTypesTestSuite(t *testing.T) { |
| 38 | + suite.Run(t, new(TypesTestSuite)) |
| 39 | +} |
| 40 | + |
| 41 | +// The safety of including ABCI error codes in the acknowledgement rests |
| 42 | +// on the inclusion of these ABCI error codes in the abcitypes.ResposneDeliverTx |
| 43 | +// hash. If the ABCI codes get removed from consensus they must no longer be used |
| 44 | +// in the packet acknowledgement. |
| 45 | +// |
| 46 | +// This test acts as an indicator that the ABCI error codes may no longer be deterministic. |
| 47 | +func (suite *TypesTestSuite) TestABCICodeDeterminism() { |
| 48 | + // same ABCI error code used |
| 49 | + err := sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "error string 1") |
| 50 | + errSameABCICode := sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "error string 2") |
| 51 | + |
| 52 | + // different ABCI error code used |
| 53 | + errDifferentABCICode := sdkerrors.ErrNotFound |
| 54 | + |
| 55 | + deliverTx := sdkerrors.ResponseDeliverTx(err, gasUsed, gasWanted, false) |
| 56 | + responses := tmprotostate.ABCIResponses{ |
| 57 | + DeliverTxs: []*abcitypes.ResponseDeliverTx{ |
| 58 | + &deliverTx, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + deliverTxSameABCICode := sdkerrors.ResponseDeliverTx(errSameABCICode, gasUsed, gasWanted, false) |
| 63 | + responsesSameABCICode := tmprotostate.ABCIResponses{ |
| 64 | + DeliverTxs: []*abcitypes.ResponseDeliverTx{ |
| 65 | + &deliverTxSameABCICode, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + deliverTxDifferentABCICode := sdkerrors.ResponseDeliverTx(errDifferentABCICode, gasUsed, gasWanted, false) |
| 70 | + responsesDifferentABCICode := tmprotostate.ABCIResponses{ |
| 71 | + DeliverTxs: []*abcitypes.ResponseDeliverTx{ |
| 72 | + &deliverTxDifferentABCICode, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + hash := tmstate.ABCIResponsesResultsHash(&responses) |
| 77 | + hashSameABCICode := tmstate.ABCIResponsesResultsHash(&responsesSameABCICode) |
| 78 | + hashDifferentABCICode := tmstate.ABCIResponsesResultsHash(&responsesDifferentABCICode) |
| 79 | + |
| 80 | + suite.Require().Equal(hash, hashSameABCICode) |
| 81 | + suite.Require().NotEqual(hash, hashDifferentABCICode) |
| 82 | +} |
| 83 | + |
| 84 | +// TestAcknowledgementError will verify that only a constant string and |
| 85 | +// ABCI error code are used in constructing the acknowledgement error string |
| 86 | +func (suite *TypesTestSuite) TestAcknowledgementError() { |
| 87 | + // same ABCI error code used |
| 88 | + err := sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "error string 1") |
| 89 | + errSameABCICode := sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "error string 2") |
| 90 | + |
| 91 | + // different ABCI error code used |
| 92 | + errDifferentABCICode := sdkerrors.ErrNotFound |
| 93 | + |
| 94 | + ack := types.NewErrorAcknowledgement(err) |
| 95 | + ackSameABCICode := types.NewErrorAcknowledgement(errSameABCICode) |
| 96 | + ackDifferentABCICode := types.NewErrorAcknowledgement(errDifferentABCICode) |
| 97 | + |
| 98 | + suite.Require().Equal(ack, ackSameABCICode) |
| 99 | + suite.Require().NotEqual(ack, ackDifferentABCICode) |
| 100 | + |
| 101 | +} |
0 commit comments