Skip to content

Commit

Permalink
add begin blocker test
Browse files Browse the repository at this point in the history
  • Loading branch information
Faulty Tolly committed Sep 26, 2024
1 parent bd42e72 commit 5aa752a
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 39 deletions.
62 changes: 49 additions & 13 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ func NewRollapp(
// Admission handler for consensus messages
app.setAdmissionHandler(consensus.MapAdmissionHandler(
[]string{
proto.MessageName(&evmtypes.MsgEthereumTx{}),
proto.MessageName(&banktypes.MsgSend{}),
},
))

Expand Down Expand Up @@ -859,42 +859,78 @@ func (app *App) Name() string { return app.BaseApp.Name() }

// BeginBlocker application updates every begin block
func (app *App) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
err := app.processConsensusMessage(ctx, req.ConsensusMessages)
if err != nil {
panic(err)
}
consensusResponses := app.processConsensusMessage(ctx, req.ConsensusMessages)

resp := app.mm.BeginBlock(ctx, req)
resp.ConsensusMessagesResponses = consensusResponses

return app.mm.BeginBlock(ctx, req)
return resp
}

func (app *App) processConsensusMessage(ctx sdk.Context, consensusMsgs []*prototypes.Any) error {
func (app *App) processConsensusMessage(ctx sdk.Context, consensusMsgs []*prototypes.Any) []*abci.ConsensusMessageResponse {
var responses []*abci.ConsensusMessageResponse

for _, anyMsg := range consensusMsgs {
sdkAny := &types.Any{
TypeUrl: anyMsg.TypeUrl,
TypeUrl: "/" + anyMsg.TypeUrl,
Value: anyMsg.Value,
}

var msg sdk.Msg
err := app.appCodec.UnpackAny(sdkAny, &msg)
if err != nil {
return fmt.Errorf("failed to unpack consensus message: %w", err)
responses = append(responses, &abci.ConsensusMessageResponse{
Response: &abci.ConsensusMessageResponse_Error{
Error: fmt.Errorf("failed to unpack consensus message: %w", err).Error(),
},
})

continue
}

cacheCtx, writeCache := ctx.CacheContext()
err = app.consensusMessageAdmissionHandler(cacheCtx, msg)
if err != nil {
return fmt.Errorf("consensus message admission failed: %w", err)
responses = append(responses, &abci.ConsensusMessageResponse{
Response: &abci.ConsensusMessageResponse_Error{
Error: fmt.Errorf("consensus message admission failed: %w", err).Error(),
},
})

continue
}

resp, err := app.MsgServiceRouter().Handler(msg)(ctx, msg)
if err != nil {
responses = append(responses, &abci.ConsensusMessageResponse{
Response: &abci.ConsensusMessageResponse_Error{
Error: fmt.Errorf("failed to execute consensus message: %w", err).Error(),
},
})

continue
}

_, err = app.MsgServiceRouter().Handler(msg)(ctx, msg)
theType, err := proto.Marshal(resp)
if err != nil {
return fmt.Errorf("failed to execute consensus message: %w", err)
return nil
}

anyResp := &prototypes.Any{
TypeUrl: proto.MessageName(resp),
Value: theType,
}

responses = append(responses, &abci.ConsensusMessageResponse{
Response: &abci.ConsensusMessageResponse_Ok{
Ok: anyResp,
},
})

writeCache()
}

return nil
return responses
}

// EndBlocker application updates every end block
Expand Down
100 changes: 77 additions & 23 deletions app/app_test.go
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")
}
}
})
}
}
8 changes: 5 additions & 3 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"encoding/json"
"fmt"
appcodec "github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/codec"
Expand Down Expand Up @@ -50,8 +51,8 @@ func setup(withGenesis bool, invCheckPeriod uint) (*App, GenesisState) {
return app, GenesisState{}
}

// Setup initializes a new App. A Nop logger is set in App.
func Setup(t *testing.T, isCheckTx bool) *App {
// SetupWithOneValidator initializes a new App. A Nop logger is set in App.
func SetupWithOneValidator(t *testing.T) (*App, authtypes.AccountI) {
t.Helper()

privVal := mock.NewPV()
Expand All @@ -69,10 +70,11 @@ func Setup(t *testing.T, isCheckTx bool) *App {
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))),
}
fmt.Printf("address: %s\n", acc.GetAddress().String())

app := SetupWithGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, balance)

return app
return app, acc
}

// SetupWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts
Expand Down

0 comments on commit 5aa752a

Please sign in to comment.