Skip to content

Commit

Permalink
enforce to set batch info
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Apr 2, 2024
1 parent a76437e commit 2ececb2
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 218 deletions.
12 changes: 6 additions & 6 deletions proto/opinit/ophost/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ message MsgUpdateProposer {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "ophost/MsgUpdateProposer";

// authority is the address that controls the module (defaults to x/gov unless overwritten)
// authority is the address that controls the module (defaults to x/gov unless overwritten)
// or the current proposer address.
string authority = 1 [(gogoproto.moretags) = "yaml:\"authority\"", (cosmos_proto.scalar) = "cosmos.AddressString"];
uint64 bridge_id = 2 [(gogoproto.moretags) = "yaml:\"bridge_id\""];
Expand All @@ -220,7 +220,7 @@ message MsgUpdateChallenger {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "ophost/MsgUpdateChallenger";

// authority is the address that controls the module (defaults to x/gov unless overwritten)
// 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\""];
Expand All @@ -241,12 +241,12 @@ message MsgUpdateBatchInfo {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "ophost/MsgUpdateBatchInfo";

// authority is the address that controls the module (defaults to x/gov unless overwritten)
// 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\""];
string authority = 1 [(gogoproto.moretags) = "yaml:\"authority\"", (cosmos_proto.scalar) = "cosmos.AddressString"];
uint64 bridge_id = 2 [(gogoproto.moretags) = "yaml:\"bridge_id\""];
BatchInfo new_batch_info = 3
[(gogoproto.moretags) = "yaml:\"new_batch_info\""];
[(gogoproto.moretags) = "yaml:\"new_batch_info\"", (gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}

// MsgUpdateBatchInfoResponse returns a message handle result.
Expand Down
4 changes: 2 additions & 2 deletions proto/opinit/ophost/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ message BridgeConfig {
// The address of the proposer.
string proposer = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// The information about batch submission.
BatchInfo batch_info = 3;
BatchInfo batch_info = 3 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
// The time interval at which checkpoints must be submitted.
// NOTE: this param is currently not used, but will be used for challenge in future.
google.protobuf.Duration submission_interval = 4 [
Expand All @@ -60,7 +60,7 @@ message BridgeConfig {
message BatchInfo {
// The address of the batch submitter.
string submitter = 1;
// The target chain
// The target chain
string chain = 2;
}

Expand Down
2 changes: 2 additions & 0 deletions x/ophost/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func NewCreateBridge(ac address.Codec) *cobra.Command {
"submission_interval": "duration",
"finalization_period": "duration",
"submission_start_time" : "rfc3339-datetime",
"batch_info": {"submitter": "bech32-address","chain": "l1|celestia"},
"metadata": "{\"perm_channels\":[{\"port_id\":\"transfer\", \"channel_id\":\"channel-0\"}, {\"port_id\":\"icqhost\", \"channel_id\":\"channel-1\"}]}"
}`, version.AppName,
),
Expand Down Expand Up @@ -146,6 +147,7 @@ func NewCreateBridge(ac address.Codec) *cobra.Command {
FinalizationPeriod: finalizationPeriod,
SubmissionStartTime: submissionStartTime,
Metadata: []byte(origConfig.Metadata),
BatchInfo: origConfig.BatchInfo,
}
if err = config.Validate(ac); err != nil {
return err
Expand Down
6 changes: 5 additions & 1 deletion x/ophost/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ func (s *CLITestSuite) TestNewCreateBridge() {
"submission_interval": "100s",
"finalization_period": "1000s",
"submission_start_time" : "2023-12-01T00:00:00Z",
"metadata": "channel-0"
"metadata": "channel-0",
"batch_info": {
"submitter": "init1q6jhwnarkw2j5qqgx3qlu20k8nrdglft5ksr0g",
"chain": "l1"
}
}`)

testCases := []struct {
Expand Down
5 changes: 4 additions & 1 deletion x/ophost/client/cli/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cli

import "github.com/initia-labs/OPinit/x/ophost/types"

// BridgeConfig defines the set of bridge config.
//
// NOTE: it is a modified BridgeConfig from x/ophost/types/types.pb.go to make unmarshal easier
Expand All @@ -17,7 +19,8 @@ type BridgeConfig struct {
// NOTE: this param is currently not used, but will be used for challenge in future.
SubmissionStartTime string `protobuf:"bytes,5,opt,name=submission_start_time,json=submissionStartTime,proto3,stdtime" json:"submission_start_time"`
// Normally it is IBC channelID for permissioned IBC relayer.
Metadata string `protobuf:"bytes,6,opt,name=metadata,proto3" json:"metadata,omitempty"`
Metadata string `protobuf:"bytes,6,opt,name=metadata,proto3" json:"metadata,omitempty"`
BatchInfo types.BatchInfo `json:"batch_info"`
}

// MsgFinalizeTokenWithdrawal is a message to remove a validator from designated list
Expand Down
3 changes: 3 additions & 0 deletions x/ophost/keeper/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func Test_BridgeConfig(t *testing.T) {
FinalizationPeriod: time.Second * 10,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{1, 2, 3},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
}
require.NoError(t, input.OPHostKeeper.SetBridgeConfig(ctx, 1, config))
_config, err := input.OPHostKeeper.GetBridgeConfig(ctx, 1)
Expand All @@ -33,6 +34,7 @@ func Test_IterateBridgeConfig(t *testing.T) {
FinalizationPeriod: time.Second * 10,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{1, 2, 3},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
}
config2 := types.BridgeConfig{
Challenger: addrs[2].String(),
Expand All @@ -41,6 +43,7 @@ func Test_IterateBridgeConfig(t *testing.T) {
FinalizationPeriod: time.Second * 10,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{3, 4, 5},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
}
require.NoError(t, input.OPHostKeeper.SetBridgeConfig(ctx, 1, config1))
require.NoError(t, input.OPHostKeeper.SetBridgeConfig(ctx, 2, config2))
Expand Down
2 changes: 1 addition & 1 deletion x/ophost/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func _createTestInput(
type bridgeHook struct {
proposer string
challenger string
batchInfo *ophosttypes.BatchInfo
batchInfo ophosttypes.BatchInfo
metadata []byte
err error
}
Expand Down
2 changes: 2 additions & 0 deletions x/ophost/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func Test_GenesisImportExport(t *testing.T) {
FinalizationPeriod: 100,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{1, 2, 3},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
}
config2 := types.BridgeConfig{
Challenger: addrsStr[2],
Expand All @@ -27,6 +28,7 @@ func Test_GenesisImportExport(t *testing.T) {
FinalizationPeriod: 200,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{3, 4, 5},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
}
require.NoError(t, input.OPHostKeeper.SetBridgeConfig(ctx, 1, config1))
require.NoError(t, input.OPHostKeeper.SetBridgeConfig(ctx, 2, config2))
Expand Down
10 changes: 2 additions & 8 deletions x/ophost/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,17 +461,11 @@ func (ms MsgServer) UpdateBatchInfo(ctx context.Context, req *types.MsgUpdateBat
return nil, err
}

newBatchChain, newBatchSubmitter := "", ""
if req.NewBatchInfo != nil {
newBatchChain = req.NewBatchInfo.Chain
newBatchSubmitter = req.NewBatchInfo.Submitter
}

sdk.UnwrapSDKContext(ctx).EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeUpdateBatchInfo,
sdk.NewAttribute(types.AttributeKeyBridgeId, strconv.FormatUint(bridgeId, 10)),
sdk.NewAttribute(types.AttributeKeyBatchChain, newBatchChain),
sdk.NewAttribute(types.AttributeKeyBatchSubmitter, newBatchSubmitter),
sdk.NewAttribute(types.AttributeKeyBatchChain, req.NewBatchInfo.Chain),
sdk.NewAttribute(types.AttributeKeyBatchSubmitter, req.NewBatchInfo.Submitter),
sdk.NewAttribute(types.AttributeKeyFinalizedOutputIndex, strconv.FormatUint(finalizedOutputIndex, 10)),
sdk.NewAttribute(types.AttributeKeyFinalizedL2BlockNumber, strconv.FormatUint(finalizedOutput.L2BlockNumber, 10)),
))
Expand Down
26 changes: 21 additions & 5 deletions x/ophost/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func Test_CreateBridge(t *testing.T) {
FinalizationPeriod: time.Second * 60,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{1, 2, 3},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
}
res, err := ms.CreateBridge(ctx, types.NewMsgCreateBridge(addrsStr[0], config))
require.NoError(t, err)
Expand All @@ -54,6 +55,7 @@ func Test_ProposeOutput(t *testing.T) {
FinalizationPeriod: time.Second * 60,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{1, 2, 3},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
}
createRes, err := ms.CreateBridge(ctx, types.NewMsgCreateBridge(addrsStr[0], config))
require.NoError(t, err)
Expand Down Expand Up @@ -91,6 +93,7 @@ func Test_DeleteOutput(t *testing.T) {
FinalizationPeriod: time.Second * 60,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{1, 2, 3},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
}
createReq := types.NewMsgCreateBridge(addrsStr[0], config)
createRes, err := ms.CreateBridge(ctx, createReq)
Expand Down Expand Up @@ -128,6 +131,7 @@ func Test_InitiateTokenDeposit(t *testing.T) {
FinalizationPeriod: time.Second * 60,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{1, 2, 3},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
}
createRes, err := ms.CreateBridge(ctx, types.NewMsgCreateBridge(addrsStr[0], config))
require.NoError(t, err)
Expand Down Expand Up @@ -155,6 +159,7 @@ func Test_FinalizeTokenWithdrawal(t *testing.T) {
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)
Expand Down Expand Up @@ -213,6 +218,7 @@ func Test_UpdateProposal(t *testing.T) {
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))
Expand Down Expand Up @@ -262,6 +268,7 @@ func Test_UpdateChallenger(t *testing.T) {
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))
Expand Down Expand Up @@ -311,7 +318,7 @@ func Test_UpdateBatchInfo(t *testing.T) {
FinalizationPeriod: time.Second * 60,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{1, 2, 3},
BatchInfo: &types.BatchInfo{
BatchInfo: types.BatchInfo{
Submitter: addrsStr[1],
Chain: "l1",
},
Expand All @@ -323,7 +330,7 @@ func Test_UpdateBatchInfo(t *testing.T) {
// gov signer
govAddr, err := input.AccountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov"))
require.NoError(t, err)
msg := types.NewMsgUpdateBatchInfo(govAddr, 1, &types.BatchInfo{
msg := types.NewMsgUpdateBatchInfo(govAddr, 1, types.BatchInfo{
Submitter: addrsStr[2],
Chain: "celestia",
})
Expand All @@ -333,19 +340,28 @@ func Test_UpdateBatchInfo(t *testing.T) {
require.NoError(t, err)
require.Equal(t, addrsStr[2], _config.BatchInfo.Submitter)
require.Equal(t, "celestia", _config.BatchInfo.Chain)
require.Equal(t, input.BridgeHook.batchInfo, _config.BatchInfo)

// current proposer signer
msg = types.NewMsgUpdateBatchInfo(addrsStr[0], 1, nil)
msg = types.NewMsgUpdateBatchInfo(addrsStr[0], 1, types.BatchInfo{
Submitter: addrsStr[3],
Chain: "l1",
})
_, err = ms.UpdateBatchInfo(ctx, msg)
require.NoError(t, err)
_config, err = ms.GetBridgeConfig(ctx, 1)
require.NoError(t, err)
require.Empty(t, _config.BatchInfo)
require.Equal(t, addrsStr[3], _config.BatchInfo.Submitter)
require.Equal(t, "l1", _config.BatchInfo.Chain)
require.Equal(t, input.BridgeHook.batchInfo, _config.BatchInfo)

// invalid signer
invalidAddr, err := input.AccountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress(types.ModuleName))
require.NoError(t, err)
msg = types.NewMsgUpdateBatchInfo(invalidAddr, 1, nil)
msg = types.NewMsgUpdateBatchInfo(invalidAddr, 1, types.BatchInfo{
Submitter: addrsStr[2],
Chain: "celestia",
})
require.NoError(t, err)

_, err = ms.UpdateBatchInfo(
Expand Down
2 changes: 2 additions & 0 deletions x/ophost/keeper/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func Test_IsFinalized(t *testing.T) {
SubmissionInterval: 100,
FinalizationPeriod: time.Second * 10,
SubmissionStartTime: time.Now().UTC(),
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
})
require.NoError(t, err)

Expand Down Expand Up @@ -142,6 +143,7 @@ func Test_GetLastFinalizedOutput(t *testing.T) {
SubmissionInterval: 100,
FinalizationPeriod: time.Second * 10,
SubmissionStartTime: time.Now().UTC(),
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
})
require.NoError(t, err)

Expand Down
4 changes: 4 additions & 0 deletions x/ophost/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func Test_QueryBridge(t *testing.T) {
FinalizationPeriod: time.Second * 60,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{1, 2, 3},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
}
err := input.OPHostKeeper.SetBridgeConfig(ctx, 1, config)
require.NoError(t, err)
Expand All @@ -45,6 +46,7 @@ func Test_QueryBridges(t *testing.T) {
FinalizationPeriod: time.Second * 60,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{1, 2, 3},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
}
config2 := types.BridgeConfig{
Challenger: addrs[1].String(),
Expand All @@ -53,6 +55,7 @@ func Test_QueryBridges(t *testing.T) {
FinalizationPeriod: time.Second * 60,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{3, 4, 5},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
}
require.NoError(t, input.OPHostKeeper.SetBridgeConfig(ctx, 1, config1))
require.NoError(t, input.OPHostKeeper.SetBridgeConfig(ctx, 2, config2))
Expand Down Expand Up @@ -183,6 +186,7 @@ func Test_QueryLastFinalizedOutput(t *testing.T) {
SubmissionInterval: 100,
FinalizationPeriod: time.Second * 10,
SubmissionStartTime: time.Now().UTC(),
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
})
require.NoError(t, err)

Expand Down
14 changes: 6 additions & 8 deletions x/ophost/types/bridge_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ func (config BridgeConfig) Validate(ac address.Codec) error {
return err
}

if config.BatchInfo != nil {
if config.BatchInfo.Chain == "" {
return errors.Wrapf(sdkerrors.ErrInvalidRequest, "target batch chain must be set")
}

if config.BatchInfo.Submitter == "" {
return errors.Wrapf(sdkerrors.ErrInvalidRequest, "batch submitter must be set")
}
if config.BatchInfo.Chain == "" {
return errors.Wrapf(sdkerrors.ErrInvalidRequest, "target batch chain must be set")
}

if config.BatchInfo.Submitter == "" {
return errors.Wrapf(sdkerrors.ErrInvalidRequest, "batch submitter must be set")
}

if config.FinalizationPeriod == time.Duration(0) {
Expand Down
4 changes: 2 additions & 2 deletions x/ophost/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (msg MsgUpdateChallenger) Validate(accAddressCodec address.Codec) error {
func NewMsgUpdateBatchInfo(
authority string,
bridgeId uint64,
newBatchInfo *BatchInfo,
newBatchInfo BatchInfo,
) *MsgUpdateBatchInfo {
return &MsgUpdateBatchInfo{
Authority: authority,
Expand All @@ -351,7 +351,7 @@ func (msg MsgUpdateBatchInfo) Validate(accAddressCodec address.Codec) error {
return ErrInvalidBridgeId
}

if msg.NewBatchInfo != nil && (msg.NewBatchInfo.Chain == "" || msg.NewBatchInfo.Submitter == "") {
if msg.NewBatchInfo.Chain == "" || msg.NewBatchInfo.Submitter == "" {
return ErrEmptyBatchInfo
}

Expand Down
Loading

0 comments on commit 2ececb2

Please sign in to comment.