generated from dymensionxyz/rollapp
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Faulty Tolly
committed
Sep 26, 2024
1 parent
bd42e72
commit 5aa752a
Showing
3 changed files
with
131 additions
and
39 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 |
---|---|---|
@@ -1,46 +1,100 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
types2 "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
"github.com/gogo/protobuf/proto" | ||
prototypes "github.com/gogo/protobuf/types" | ||
"github.com/stretchr/testify/require" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
"github.com/tendermint/tendermint/proto/tendermint/types" | ||
"testing" | ||
) | ||
|
||
func TestBeginBlocker(t *testing.T) { | ||
app := Setup(t, true) | ||
app, valAccount := SetupWithOneValidator(t) | ||
ctx := app.NewUncachedContext(true, types.Header{ | ||
Height: 1, | ||
ChainID: "testchain_9000-1", | ||
}) | ||
|
||
// Simula mensajes de consenso si es necesario | ||
consensusMsgs := []*prototypes.Any{ | ||
// Agrega tus mensajes aquí | ||
bankSend := &types2.MsgSend{ | ||
FromAddress: valAccount.GetAddress().String(), | ||
ToAddress: valAccount.GetAddress().String(), | ||
Amount: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100))), | ||
} | ||
msgBz, err := proto.Marshal(bankSend) | ||
require.NoError(t, err) | ||
|
||
goodMessage := &prototypes.Any{ | ||
TypeUrl: proto.MessageName(&types2.MsgSend{}), | ||
Value: msgBz, | ||
} | ||
|
||
req := abci.RequestBeginBlock{ | ||
Header: types.Header{ | ||
Height: 1, | ||
Time: ctx.BlockTime(), | ||
ChainID: "testchain_9000-1", | ||
testCases := []struct { | ||
name string | ||
consensusMsgs []*prototypes.Any | ||
expectError bool | ||
}{ | ||
{ | ||
name: "ValidConsensusMessage", | ||
consensusMsgs: []*prototypes.Any{ | ||
goodMessage, | ||
}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "InvalidUnpackMessage", | ||
consensusMsgs: []*prototypes.Any{ | ||
{ | ||
TypeUrl: "/path.to.InvalidMsg", | ||
Value: []byte("invalid unpack data"), | ||
}, | ||
}, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "InvalidExecutionMessage", | ||
consensusMsgs: []*prototypes.Any{ | ||
{ | ||
TypeUrl: "/path.to.ExecErrorMsg", | ||
Value: []byte("execution error data"), | ||
}, | ||
}, | ||
expectError: true, | ||
}, | ||
LastCommitInfo: abci.LastCommitInfo{}, | ||
ByzantineValidators: []abci.Evidence{}, | ||
ConsensusMessages: consensusMsgs, | ||
} | ||
|
||
res := app.BeginBlocker(ctx, req) | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
req := abci.RequestBeginBlock{ | ||
Header: types.Header{ | ||
Height: 1, | ||
Time: ctx.BlockTime(), | ||
ChainID: "testchain_9000-1", | ||
}, | ||
LastCommitInfo: abci.LastCommitInfo{}, | ||
ByzantineValidators: []abci.Evidence{}, | ||
ConsensusMessages: tc.consensusMsgs, | ||
} | ||
|
||
// Verifica el resultado (ajusta según tus necesidades) | ||
require.NotNil(t, res) | ||
fmt.Println("Response:", res) | ||
res := app.BeginBlocker(ctx, req) | ||
require.NotNil(t, res) | ||
|
||
// Verifica cambios en el estado o eventos | ||
// Por ejemplo: | ||
// expectedValue := ... | ||
// actualValue := app.YourKeeper.GetSomething(ctx) | ||
// require.Equal(t, expectedValue, actualValue) | ||
if tc.expectError { | ||
require.NotEmpty(t, res.ConsensusMessagesResponses) | ||
for _, response := range res.ConsensusMessagesResponses { | ||
_, isError := response.Response.(*abci.ConsensusMessageResponse_Error) | ||
require.True(t, isError, "Expected an error response but got a success") | ||
} | ||
} else { | ||
require.NotEmpty(t, res.ConsensusMessagesResponses) | ||
for _, response := range res.ConsensusMessagesResponses { | ||
_, isOk := response.Response.(*abci.ConsensusMessageResponse_Ok) | ||
require.True(t, isOk, "Expected a success response but got an error") | ||
} | ||
} | ||
}) | ||
} | ||
} |
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