-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(sequencers): added MsgUpsertSequencer #568
Changes from all commits
01b4b8d
b3475bf
8febb33
a8aeac9
f7263d5
3d493d1
3d2a86e
39edbe8
a7e8f4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
syntax = "proto3"; | ||
package rollapp.sequencers.types; | ||
|
||
import "gogoproto/gogo.proto"; | ||
import "cosmos/staking/v1beta1/staking.proto"; | ||
import "cosmos/msg/v1/msg.proto"; | ||
import "google/protobuf/any.proto"; | ||
|
||
option go_package = "github.com/dymensionxyz/dymension-rdk/x/sequencers/types"; | ||
|
||
message EventUpsertSequencer { | ||
// Operator is the bech32-encoded address of the actor sending the update | ||
string operator = 1; | ||
// ConsAddr is a tendermint consensus address | ||
string cons_addr = 2; | ||
// RewardAddr is the bech32-encoded sequencer's reward address | ||
string reward_addr = 3; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package addressutils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/bech32" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/dymensionxyz/dymension-rdk/testutil/utils" | ||
"github.com/dymensionxyz/dymension-rdk/utils/addressutils" | ||
) | ||
|
||
func TestBech32ToAddr(t *testing.T) { | ||
testCases := []struct { | ||
validPrefix string | ||
}{ | ||
{validPrefix: "cosmosvaloper"}, | ||
{validPrefix: "dymval"}, | ||
{validPrefix: "cosmos"}, | ||
{validPrefix: "dym"}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.validPrefix, func(t *testing.T) { | ||
// generate an address with a random prefix | ||
expectedBytes := utils.AccAddress().Bytes() | ||
randomPrefixedAddr, err := bech32.ConvertAndEncode(tc.validPrefix, expectedBytes) | ||
require.NoError(t, err) | ||
|
||
// convert the random-prefixed bech32 to the current val oper address | ||
valOperAddr, err := addressutils.Bech32ToAddr[sdk.ValAddress](randomPrefixedAddr) | ||
require.NoError(t, err) | ||
|
||
// check results | ||
// verify format | ||
err = sdk.VerifyAddressFormat(valOperAddr) | ||
require.NoError(t, err) | ||
|
||
// cast valOperAddr to string and verify the prefix and bytes | ||
actualPrefix, actualBytes, err := bech32.DecodeAndConvert(valOperAddr.String()) | ||
require.NoError(t, err) | ||
expectedPrefix := sdk.GetConfig().GetBech32ValidatorAddrPrefix() | ||
require.Equal(t, expectedPrefix, actualPrefix) | ||
require.Equal(t, expectedBytes, actualBytes) | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Package uevent is a copy of https://github.com/dymensionxyz/sdk-utils/tree/main/utils/uevent. | ||
// TODO: import sdk-utils directly when the RDK is updated to SDK v0.47 and further. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have sdk-util's branch dedicated for the RDK (based on v0.46) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's deeply outdated :( |
||
package uevent | ||
|
||
import ( | ||
"encoding/json" | ||
"slices" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/gogoproto/proto" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
"golang.org/x/exp/maps" | ||
) | ||
|
||
// EmitTypedEvent takes a typed event and emits it. | ||
// The original EmitTypedEvent from cosmos-sdk adds double quotes around the string attributes, | ||
// which makes it difficult, if not impossible to query/subscribe to those events. | ||
// See https://github.com/cosmos/cosmos-sdk/issues/12592 and | ||
// https://github.com/dymensionxyz/sdk-utils/pull/5#discussion_r1724688379 | ||
func EmitTypedEvent(ctx sdk.Context, tev proto.Message) error { | ||
event, err := TypedEventToEvent(tev) | ||
if err != nil { | ||
return err | ||
} | ||
ctx.EventManager().EmitEvent(event) | ||
return nil | ||
} | ||
|
||
// TypedEventToEvent takes typed event and converts to Event object | ||
func TypedEventToEvent(tev proto.Message) (ev sdk.Event, err error) { | ||
evtType := proto.MessageName(tev) | ||
|
||
var evtJSON []byte | ||
evtJSON, err = codec.ProtoMarshalJSON(tev, nil) | ||
if err != nil { | ||
return | ||
} | ||
|
||
var attrMap map[string]json.RawMessage | ||
if err = json.Unmarshal(evtJSON, &attrMap); err != nil { | ||
return | ||
} | ||
|
||
// sort the keys to ensure the order is always the same | ||
keys := maps.Keys(attrMap) | ||
slices.Sort(keys) | ||
|
||
attrs := make([]abci.EventAttribute, 0, len(attrMap)) | ||
for _, k := range keys { | ||
v := attrMap[k] | ||
attrs = append(attrs, abci.EventAttribute{ | ||
Key: []byte(k), | ||
Value: []byte(removeSurroundingQuotes(v)), | ||
}) | ||
} | ||
|
||
ev = sdk.Event{ | ||
Type: evtType, | ||
Attributes: attrs, | ||
} | ||
return | ||
} | ||
|
||
func removeSurroundingQuotes(bz []byte) string { | ||
const dquote = 34 | ||
if len(bz) > 1 && bz[0] == dquote && bz[len(bz)-1] == dquote { | ||
return string(bz[1 : len(bz)-1]) | ||
} | ||
return string(bz) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,14 @@ package keeper | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/dymensionxyz/dymension-rdk/x/sequencers/types" | ||
"github.com/dymensionxyz/gerr-cosmos/gerrc" | ||
|
||
"github.com/dymensionxyz/dymension-rdk/utils/uevent" | ||
"github.com/dymensionxyz/dymension-rdk/x/sequencers/types" | ||
) | ||
|
||
var _ types.MsgServer = msgServer{} | ||
|
@@ -43,6 +46,32 @@ func (m msgServer) CreateSequencer(goCtx context.Context, msg *types.MsgCreateSe | |
return &types.MsgCreateSequencerResponse{}, nil | ||
} | ||
|
||
func (m msgServer) UpsertSequencer(goCtx context.Context, msg *types.ConsensusMsgUpsertSequencer) (*types.ConsensusMsgUpsertSequencerResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
// all must-methods are safe to use since they're validated in ValidateBasic | ||
|
||
v := msg.MustValidator() | ||
m.SetSequencer(ctx, v) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. validate it's not already exists? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the next PR (for Whitelisted Relayer ADR) we'll assume that the hub is the source of truth, so the upsert will become an honest upsert independently of the record existence. i'll keep this code as it is for now if you don't mind, just not to do the extra work of reverting it in the next PR. |
||
m.SetRewardAddr(ctx, v, msg.MustRewardAddr()) | ||
|
||
consAddr, err := v.GetConsAddr() | ||
if err != nil { | ||
return nil, fmt.Errorf("get validator consensus addr: %w", err) | ||
} | ||
|
||
err = uevent.EmitTypedEvent(ctx, &types.EventUpsertSequencer{ | ||
Operator: msg.MustOperatorAddr().String(), | ||
ConsAddr: consAddr.String(), | ||
RewardAddr: msg.MustRewardAddr().String(), | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("emit event: %w", err) | ||
} | ||
|
||
return &types.ConsensusMsgUpsertSequencerResponse{}, nil | ||
} | ||
|
||
func (m msgServer) UpdateSequencer(goCtx context.Context, msg *types.MsgUpdateSequencer) (*types.MsgUpdateSequencerResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
operator := msg.MustOperatorAddr() // checked in validate basic | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CreateSequencer
should be removed if there's no use case for itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'll remove it in the next PR as well if you don't mind. i'm gonna refactor the whole msg server there.