Skip to content

Commit

Permalink
feat: consensus messages (#563)
Browse files Browse the repository at this point in the history
Co-authored-by: Faulty Tolly <@faulttolerance.net>
  • Loading branch information
faultytolly authored Oct 8, 2024
1 parent 82c4d5f commit baa5f01
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
26 changes: 26 additions & 0 deletions server/consensus/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package consensus

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
)

// AdmissionHandler is a function used to validate if a Consensus Message is valid
type AdmissionHandler func(ctx sdk.Context, msg sdk.Msg) error

// AllowedMessagesHandler returns an AdmissionHandler that only allows messages with the given names
func AllowedMessagesHandler(messageNames []string) AdmissionHandler {
return func(ctx sdk.Context, msg sdk.Msg) error {
msgName := proto.MessageName(msg)

for _, handler := range messageNames {
if msgName == handler {
return nil
}
}

return fmt.Errorf("consensus message is not allowed: %s", msgName)
}
}
55 changes: 55 additions & 0 deletions server/consensus/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package consensus

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
types3 "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"

"github.com/dymensionxyz/dymension-rdk/x/sequencers/types"
)

func TestMapAdmissionHandler(t *testing.T) {
allowedMessages := []string{
proto.MessageName(&types.MsgCreateSequencer{}),
proto.MessageName(&types.MsgUpdateSequencer{}),
}

handler := AllowedMessagesHandler(allowedMessages)

tests := []struct {
name string
message sdk.Msg
wantErr bool
}{
{
name: "Allowed message 1",
message: &types.MsgCreateSequencer{},
wantErr: false,
},
{
name: "Allowed message 2",
message: &types.MsgUpdateSequencer{},
wantErr: false,
},
{
name: "Not allowed message",
message: &types3.MsgSend{},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := handler(sdk.Context{}, tt.message)
if tt.wantErr {
assert.Error(t, err)
assert.Contains(t, err.Error(), "is not allowed")
} else {
assert.NoError(t, err)
}
})
}
}

0 comments on commit baa5f01

Please sign in to comment.