-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Faulty Tolly <@faulttolerance.net>
- Loading branch information
1 parent
82c4d5f
commit baa5f01
Showing
2 changed files
with
81 additions
and
0 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
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) | ||
} | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |