Skip to content

Commit

Permalink
add update bridge metadata message
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Apr 23, 2024
1 parent 2174936 commit e4cf065
Show file tree
Hide file tree
Showing 10 changed files with 2,053 additions and 221 deletions.
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
14 changes: 14 additions & 0 deletions x/ophost/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,20 @@ func (h *bridgeHook) BridgeBatchInfoUpdated(
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
}

Check warning on line 495 in x/ophost/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/msg_server.go#L494-L495

Added lines #L494 - L495 were not covered by tests

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

Check warning on line 501 in x/ophost/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/msg_server.go#L500-L501

Added lines #L500 - L501 were not covered by tests

// gov or current proposer can update batch info.
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
}

Check warning on line 511 in x/ophost/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/msg_server.go#L510-L511

Added lines #L510 - L511 were not covered by tests

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

Check warning on line 515 in x/ophost/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/msg_server.go#L514-L515

Added lines #L514 - L515 were not covered by tests

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

Check warning on line 520 in x/ophost/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/msg_server.go#L519-L520

Added lines #L519 - L520 were not covered by tests

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
}

Check warning on line 105 in x/ophost/types/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/types/hooks.go#L101-L105

Added lines #L101 - L105 were not covered by tests
}

return nil

Check warning on line 108 in x/ophost/types/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/types/hooks.go#L108

Added line #L108 was not covered by tests
}
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)
}

Check warning on line 79 in x/ophost/types/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/types/tx.go#L78-L79

Added lines #L78 - L79 were not covered by tests

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
}

Check warning on line 389 in x/ophost/types/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/types/tx.go#L388-L389

Added lines #L388 - L389 were not covered by tests

if msg.BridgeId == 0 {
return ErrInvalidBridgeId
}

Check warning on line 393 in x/ophost/types/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/types/tx.go#L392-L393

Added lines #L392 - L393 were not covered by tests

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

Check warning on line 397 in x/ophost/types/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/types/tx.go#L396-L397

Added lines #L396 - L397 were not covered by tests

return nil
}

/* MsgUpdateParams */

// NewMsgUpdateParams returns a new MsgUpdateParams instance
Expand Down
Loading

0 comments on commit e4cf065

Please sign in to comment.