Skip to content
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

Merged
merged 9 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions proto/sequencers/events.proto
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;

}
22 changes: 17 additions & 5 deletions proto/sequencers/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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";

Expand All @@ -12,6 +11,8 @@ option go_package = "github.com/dymensionxyz/dymension-rdk/x/sequencers/types";
service Msg {
rpc CreateSequencer(MsgCreateSequencer) returns (MsgCreateSequencerResponse);
Copy link
Collaborator

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 it

Copy link
Contributor Author

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.

rpc UpdateSequencer(MsgUpdateSequencer) returns (MsgUpdateSequencerResponse);

rpc UpsertSequencer(ConsensusMsgUpsertSequencer) returns (ConsensusMsgUpsertSequencerResponse);
}

message MsgCreateSequencer {
Expand All @@ -24,18 +25,29 @@ message MsgCreateSequencer {
bytes signature = 3;
}

message MsgCreateSequencerResponse {

}
message MsgCreateSequencerResponse {}

message MsgUpdateSequencer {
option (cosmos.msg.v1.signer) = "operator";
// Operator is the bech32-encoded address of the actor sending the update - must be val addr
string operator = 1;
// Field no.2 is missing
reserved 2;
// RewardAddr is a bech32 encoded sdk acc address
string reward_addr = 3;
}

message MsgUpdateSequencerResponse {
message MsgUpdateSequencerResponse {}

// ConsensusMsgUpsertSequencer is a consensus message to upsert the sequencer.
message ConsensusMsgUpsertSequencer {
option (cosmos.msg.v1.signer) = "operator";
// Operator is the bech32-encoded address of the actor sending the update
string operator = 1;
// ConsPubKey is a tendermint consensus pub key
google.protobuf.Any cons_pub_key = 2;
// RewardAddr is the bech32-encoded sequencer's reward address
string reward_addr = 3;
}

message ConsensusMsgUpsertSequencerResponse {}
4 changes: 2 additions & 2 deletions server/commands/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/dymensionxyz/dymint/block"
dymintconf "github.com/dymensionxyz/dymint/config"
dymintconv "github.com/dymensionxyz/dymint/conv"

"github.com/dymensionxyz/dymint/store"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/node"
Expand All @@ -17,8 +16,9 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/types"
"github.com/dymensionxyz/dymension-rdk/utils"
"github.com/spf13/cobra"

"github.com/dymensionxyz/dymension-rdk/utils"
)

// RollbackCmd rollbacks the app multistore to specific height and updates dymint state according to it
Expand Down
16 changes: 14 additions & 2 deletions utils/addressutils/utils.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package addressutils

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/tendermint/tendermint/crypto"
)

type AddressType int32

const (
// the 32 bytes length address type of ADR 028.
// AddressType32Bytes is the 32 bytes length address type of ADR 028.
AddressType32Bytes AddressType = 0
// the default 20 bytes length address type.
// AddressType20Bytes is the default 20 bytes length address type.
AddressType20Bytes AddressType = 1
)

Expand All @@ -27,3 +30,12 @@ func DeriveAddress(addressType AddressType, moduleName, name string) sdk.AccAddr
return sdk.AccAddress{}
}
}

// Bech32ToAddr casts an arbitrary-prefixed bech32 string to either sdk.AccAddress or sdk.ValAddress.
func Bech32ToAddr[T sdk.AccAddress | sdk.ValAddress](addr string) (T, error) {
keruch marked this conversation as resolved.
Show resolved Hide resolved
_, bytes, err := bech32.DecodeAndConvert(addr)
if err != nil {
return nil, fmt.Errorf("decoding bech32 addr: %w", err)
}
return bytes, nil
}
48 changes: 48 additions & 0 deletions utils/addressutils/utils_test.go
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)
})
}
}
71 changes: 71 additions & 0 deletions utils/uevent/events.go
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
}
31 changes: 30 additions & 1 deletion x/sequencers/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validate it's not already exists?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down
5 changes: 3 additions & 2 deletions x/sequencers/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dymensionxyz/gerr-cosmos/gerrc"
"github.com/stretchr/testify/require"

testkeepers "github.com/dymensionxyz/dymension-rdk/testutil/keepers"
"github.com/dymensionxyz/dymension-rdk/testutil/utils"
"github.com/dymensionxyz/dymension-rdk/x/sequencers/keeper"
"github.com/dymensionxyz/dymension-rdk/x/sequencers/types"
"github.com/dymensionxyz/gerr-cosmos/gerrc"
"github.com/stretchr/testify/require"
)

func TestCreateUpdateHappyPath(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions x/sequencers/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
(*sdk.Msg)(nil),
&MsgCreateSequencer{},
&MsgUpdateSequencer{},
&ConsensusMsgUpsertSequencer{},
)

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
Loading
Loading