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: add update bridge metadata message #57

Merged
merged 2 commits into from
Apr 23, 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
1,384 changes: 1,263 additions & 121 deletions api/opinit/ophost/v1/tx.pulsar.go

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions api/opinit/ophost/v1/tx_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions proto/opinit/ophost/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ service Msg {
// UpdateBatchInfo defines a rpc handler method for MsgUpdateBatchInfo.
rpc UpdateBatchInfo(MsgUpdateBatchInfo) returns (MsgUpdateBatchInfoResponse);

// UpdateMetadata defines a rpc handler method for MsgUpdateMetadata.
rpc UpdateMetadata(MsgUpdateMetadata) returns (MsgUpdateMetadataResponse);

// UpdateParams defines an operation for updating the
// x/opchild module parameters.
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
Expand Down Expand Up @@ -257,6 +260,26 @@ message MsgUpdateBatchInfoResponse {
uint64 l2_block_number = 2;
}

// MsgUpdateMetadata is a message to change metadata
message MsgUpdateMetadata {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "ophost/MsgUpdateMetadata";

// authority is the address that controls the module (defaults to x/gov unless overwritten)
// or the current challenger address.
string authority = 1 [(gogoproto.moretags) = "yaml:\"authority\"", (cosmos_proto.scalar) = "cosmos.AddressString"];
uint64 bridge_id = 2 [(gogoproto.moretags) = "yaml:\"bridge_id\""];
bytes metadata = 3 [(gogoproto.moretags) = "yaml:\"metadata\""];
}

// MsgUpdateMetadataResponse returns a message handle result.
message MsgUpdateMetadataResponse {
// last finalized output index
uint64 output_index = 1;
// last finalized l2 block number
uint64 l2_block_number = 2;
}

// MsgUpdateParams is a message to update parameters
message MsgUpdateParams {
option (cosmos.msg.v1.signer) = "authority";
Expand Down
17 changes: 14 additions & 3 deletions x/ophost/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ func (h *bridgeHook) BridgeChallengerUpdated(
return h.err
}

h.metadata = bridgeConfig.Metadata
h.challenger = bridgeConfig.Challenger

return nil
Expand All @@ -364,7 +363,6 @@ func (h *bridgeHook) BridgeProposerUpdated(
return h.err
}

h.metadata = bridgeConfig.Metadata
h.proposer = bridgeConfig.Proposer

return nil
Expand All @@ -379,12 +377,25 @@ func (h *bridgeHook) BridgeBatchInfoUpdated(
return h.err
}

h.metadata = bridgeConfig.Metadata
h.batchInfo = bridgeConfig.BatchInfo

return nil
}

func (h *bridgeHook) BridgeMetadataUpdated(
ctx context.Context,
bridgeId uint64,
bridgeConfig ophosttypes.BridgeConfig,
) error {
if h.err != nil {
return h.err
}

h.metadata = bridgeConfig.Metadata

return nil
}

var _ ophosttypes.CommunityPoolKeeper = &MockCommunityPoolKeeper{}

type MockCommunityPoolKeeper struct {
Expand Down
43 changes: 43 additions & 0 deletions x/ophost/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,49 @@ func (ms MsgServer) UpdateBatchInfo(ctx context.Context, req *types.MsgUpdateBat
}, nil
}

func (ms MsgServer) UpdateMetadata(ctx context.Context, req *types.MsgUpdateMetadata) (*types.MsgUpdateMetadataResponse, error) {
if err := req.Validate(ms.authKeeper.AddressCodec()); err != nil {
return nil, err
}

bridgeId := req.BridgeId
config, err := ms.GetBridgeConfig(ctx, bridgeId)
if err != nil {
return nil, err
}

// gov or current proposer can update metadata.
if ms.authority != req.Authority && config.Proposer != req.Authority {
return nil, govtypes.ErrInvalidSigner.Wrapf("invalid authority; expected %s or %s, got %s", ms.authority, config.Proposer, req.Authority)
}

config.Metadata = req.Metadata
if err := ms.Keeper.bridgeHook.BridgeMetadataUpdated(ctx, bridgeId, config); err != nil {
return nil, err
}

if err := ms.SetBridgeConfig(ctx, bridgeId, config); err != nil {
return nil, err
}

finalizedOutputIndex, finalizedOutput, err := ms.GetLastFinalizedOutput(ctx, bridgeId)
if err != nil {
return nil, err
}

sdk.UnwrapSDKContext(ctx).EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeUpdateBatchInfo,
sdk.NewAttribute(types.AttributeKeyBridgeId, strconv.FormatUint(bridgeId, 10)),
sdk.NewAttribute(types.AttributeKeyFinalizedOutputIndex, strconv.FormatUint(finalizedOutputIndex, 10)),
sdk.NewAttribute(types.AttributeKeyFinalizedL2BlockNumber, strconv.FormatUint(finalizedOutput.L2BlockNumber, 10)),
))

return &types.MsgUpdateMetadataResponse{
OutputIndex: finalizedOutputIndex,
L2BlockNumber: finalizedOutput.L2BlockNumber,
}, nil
}

// UpdateParams implements updating the parameters
func (ms MsgServer) UpdateParams(ctx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if err := req.Validate(ms.authKeeper.AddressCodec()); err != nil {
Expand Down
51 changes: 51 additions & 0 deletions x/ophost/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,57 @@ func Test_UpdateBatchInfo(t *testing.T) {
require.Error(t, err)
}

func Test_UpdateMetadata(t *testing.T) {
ctx, input := createDefaultTestInput(t)
ms := keeper.NewMsgServerImpl(input.OPHostKeeper)

config := types.BridgeConfig{
Proposer: addrsStr[0],
Challenger: addrsStr[1],
SubmissionInterval: time.Second * 10,
FinalizationPeriod: time.Second * 60,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{1, 2, 3},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
}

_, err := ms.CreateBridge(ctx, types.NewMsgCreateBridge(addrsStr[0], config))
require.NoError(t, err)

// gov signer
govAddr, err := input.AccountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov"))
require.NoError(t, err)
msg := types.NewMsgUpdateMetadata(govAddr, 1, []byte{4, 5, 6})
_, err = ms.UpdateMetadata(ctx, msg)
require.NoError(t, err)
_config, err := ms.GetBridgeConfig(ctx, 1)
require.NoError(t, err)
require.Equal(t, []byte{4, 5, 6}, _config.Metadata)
require.Equal(t, []byte{4, 5, 6}, input.BridgeHook.metadata)

// current challenger
msg = types.NewMsgUpdateMetadata(addrsStr[0], 1, []byte{7, 8, 9})
_, err = ms.UpdateMetadata(ctx, msg)
require.NoError(t, err)
_config, err = ms.GetBridgeConfig(ctx, 1)
require.NoError(t, err)
require.Equal(t, []byte{7, 8, 9}, _config.Metadata)
require.Equal(t, []byte{7, 8, 9}, input.BridgeHook.metadata)

// invalid signer
invalidAddr, err := input.AccountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress(types.ModuleName))
require.NoError(t, err)
msg = types.NewMsgUpdateMetadata(invalidAddr, 1, []byte{1, 2, 3})
require.NoError(t, err)

_, err = ms.UpdateMetadata(
ctx,
msg,
)
require.Error(t, err)

}

func Test_MsgServer_UpdateParams(t *testing.T) {
ctx, input := createDefaultTestInput(t)
ms := keeper.NewMsgServerImpl(input.OPHostKeeper)
Expand Down
1 change: 1 addition & 0 deletions x/ophost/types/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ var (
ErrFailedToVerifyWithdrawal = errorsmod.Register(ModuleName, 10, "failed to verify withdrawal tx")
ErrWithdrawalAlreadyFinalized = errorsmod.Register(ModuleName, 11, "withdrawal already finalized")
ErrEmptyBatchInfo = errorsmod.Register(ModuleName, 12, "empty batch info")
ErrInvalidBridgeMetadata = errorsmod.Register(ModuleName, 13, "invalid bridge metadata")
)
19 changes: 19 additions & 0 deletions x/ophost/types/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type BridgeHook interface {
bridgeId uint64,
bridgeConfig BridgeConfig,
) error
BridgeMetadataUpdated(
ctx context.Context,
bridgeId uint64,
bridgeConfig BridgeConfig,
) error
}

type BridgeHooks []BridgeHook
Expand Down Expand Up @@ -88,3 +93,17 @@ func (hooks BridgeHooks) BridgeBatchInfoUpdated(

return nil
}

func (hooks BridgeHooks) BridgeMetadataUpdated(
ctx context.Context,
bridgeId uint64,
bridgeConfig BridgeConfig,
) error {
for _, h := range hooks {
if err := h.BridgeMetadataUpdated(ctx, bridgeId, bridgeConfig); err != nil {
return err
}
}

return nil
}
41 changes: 41 additions & 0 deletions x/ophost/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ var (
_ sdk.Msg = &MsgUpdateProposer{}
_ sdk.Msg = &MsgUpdateChallenger{}
_ sdk.Msg = &MsgUpdateBatchInfo{}
_ sdk.Msg = &MsgUpdateMetadata{}
_ sdk.Msg = &MsgUpdateParams{}
)

const (
MaxMetadataLength = 1024 * 5
)

/* MsgRecordBatch */

// NewMsgRecordBatch creates a new MsgRecordBatch instance.
Expand Down Expand Up @@ -69,6 +74,10 @@ func (msg MsgCreateBridge) Validate(ac address.Codec) error {
return err
}

if len(msg.Config.Metadata) > MaxMetadataLength {
return ErrInvalidBridgeMetadata.Wrapf("metadata length exceeds %d", MaxMetadataLength)
}

return nil
}

Expand Down Expand Up @@ -358,6 +367,38 @@ func (msg MsgUpdateBatchInfo) Validate(accAddressCodec address.Codec) error {
return nil
}

/* MsgUpdateMetadata */

// NewMsgUpdateMetadata creates a new MsgUpdateMetadata instance.
func NewMsgUpdateMetadata(
authority string,
bridgeId uint64,
metadata []byte,
) *MsgUpdateMetadata {
return &MsgUpdateMetadata{
Authority: authority,
BridgeId: bridgeId,
Metadata: metadata,
}
}

// Validate performs basic MsgUpdateMetadata message validation.
func (msg MsgUpdateMetadata) Validate(accAddressCodec address.Codec) error {
if _, err := accAddressCodec.StringToBytes(msg.Authority); err != nil {
return err
}

if msg.BridgeId == 0 {
return ErrInvalidBridgeId
}

if len(msg.Metadata) > MaxMetadataLength {
return ErrInvalidBridgeMetadata.Wrapf("metadata length exceeds %d", MaxMetadataLength)
}

return nil
}

/* MsgUpdateParams */

// NewMsgUpdateParams returns a new MsgUpdateParams instance
Expand Down
Loading
Loading