Skip to content

Commit

Permalink
feat: support multi challenger
Browse files Browse the repository at this point in the history
  • Loading branch information
djm07073 committed Jun 15, 2024
1 parent 210b2c4 commit 47bf861
Show file tree
Hide file tree
Showing 24 changed files with 557 additions and 421 deletions.
8 changes: 4 additions & 4 deletions contrib/launchtools/steps/opbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ func createOpBridge(
return ophosttypes.NewMsgCreateBridge(
executorAddress,
ophosttypes.BridgeConfig{
Challenger: challengerAddress,
Proposer: outputAddress,
Challengers: []string{challengerAddress},
Proposer: outputAddress,
BatchInfo: ophosttypes.BatchInfo{
Submitter: submitterAddress,
Chain: submitTarget,
Submitters: []string{submitterAddress},
Chain: submitTarget,
},
SubmissionInterval: submissionInterval,
FinalizationPeriod: finalizationPeriod,
Expand Down
10 changes: 6 additions & 4 deletions proto/opinit/ophost/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,12 @@ message MsgUpdateChallenger {
option (amino.name) = "ophost/MsgUpdateChallenger";

// authority is the address that controls the module (defaults to x/gov unless overwritten)
// or the current challenger address.
// or the current challenger address.(supports a single challenger that can replace the existing challenger,
// excluding their own address)
string authority = 1 [(gogoproto.moretags) = "yaml:\"authority\"", (cosmos_proto.scalar) = "cosmos.AddressString"];
uint64 bridge_id = 2 [(gogoproto.moretags) = "yaml:\"bridge_id\""];
string new_challenger = 3
[(gogoproto.moretags) = "yaml:\"new_challenger\"", (cosmos_proto.scalar) = "cosmos.AddressString"];
repeated string new_challengers = 3
[(gogoproto.moretags) = "yaml:\"new_challengers\"", (cosmos_proto.scalar) = "cosmos.AddressString"];
}

// MsgUpdateChallengerResponse returns a message handle result.
Expand Down Expand Up @@ -267,7 +268,8 @@ message MsgUpdateMetadata {
option (amino.name) = "ophost/MsgUpdateMetadata";

// authority is the address that controls the module (defaults to x/gov unless overwritten)
// or the current challenger address.
// or the current challenger address.(supports a single challenger that can replace the existing challenger,
// excluding their own 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\""];
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 @@ -29,7 +29,7 @@ message Params {
// BridgeConfig defines the set of bridge config.
message BridgeConfig {
// The address of the challenger.
string challenger = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
repeated string challengers = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// The address of the proposer.
string proposer = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// The information about batch submission.
Expand Down Expand Up @@ -60,7 +60,7 @@ message BridgeConfig {
// BatchInfo defines the set of batch information.
message BatchInfo {
// The address of the batch submitter.
string submitter = 1;
repeated string submitters = 1;
// The target chain
string chain = 2;
}
Expand Down
12 changes: 8 additions & 4 deletions x/opchild/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func NewSetBridgeInfoCmd(ac address.Codec) *cobra.Command {
return err
}

origConfig := ophostcli.BridgeConfig{}
origConfig := ophostcli.BridgeCliConfig{}
err = json.Unmarshal(configBytes, &origConfig)
if err != nil {
return err
Expand All @@ -318,14 +318,18 @@ func NewSetBridgeInfoCmd(ac address.Codec) *cobra.Command {
}

bridgeConfig := ophosttypes.BridgeConfig{
Challenger: origConfig.Challenger,
Challengers: []string{origConfig.Challenger},
Proposer: origConfig.Proposer,
SubmissionInterval: submissionInterval,
FinalizationPeriod: finalizationPeriod,
SubmissionStartTime: submissionStartTime,
BatchInfo: origConfig.BatchInfo,
Metadata: []byte(origConfig.Metadata),
BatchInfo: ophosttypes.BatchInfo{
Chain: origConfig.BatchInfo.Chain,
Submitters: []string{origConfig.BatchInfo.Submitter},
},
Metadata: []byte(origConfig.Metadata),
}

if err = bridgeConfig.ValidateWithNoAddrValidation(); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions x/opchild/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func Test_GenesisImportExport(t *testing.T) {
BridgeId: 1,
BridgeAddr: addrsStr[1],
BridgeConfig: ophosttypes.BridgeConfig{
Challenger: addrsStr[2],
Proposer: addrsStr[3],
Challengers: []string{addrsStr[2]},
Proposer: addrsStr[3],
BatchInfo: ophosttypes.BatchInfo{
Submitter: addrsStr[4],
Chain: "l1",
Submitters: []string{addrsStr[4]},
Chain: "l1",
},
SubmissionInterval: time.Minute,
FinalizationPeriod: time.Hour,
Expand Down
16 changes: 8 additions & 8 deletions x/opchild/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,11 @@ func Test_MsgServer_Withdraw(t *testing.T) {
L1ChainId: "test-chain-id",
L1ClientId: "test-client-id",
BridgeConfig: ophosttypes.BridgeConfig{
Challenger: addrsStr[2],
Proposer: addrsStr[3],
Challengers: []string{addrsStr[2]},
Proposer: addrsStr[3],
BatchInfo: ophosttypes.BatchInfo{
Submitter: addrsStr[4],
Chain: "l1",
Submitters: []string{addrsStr[4]},
Chain: "l1",
},
SubmissionInterval: time.Minute,
FinalizationPeriod: time.Hour,
Expand Down Expand Up @@ -332,11 +332,11 @@ func Test_MsgServer_SetBridgeInfo(t *testing.T) {
L1ChainId: "test-chain-id",
L1ClientId: "test-client-id",
BridgeConfig: ophosttypes.BridgeConfig{
Challenger: addrsStr[2],
Proposer: addrsStr[3],
Challengers: []string{addrsStr[2]},
Proposer: addrsStr[3],
BatchInfo: ophosttypes.BatchInfo{
Submitter: addrsStr[4],
Chain: "l1",
Submitters: []string{addrsStr[4]},
Chain: "l1",
},
SubmissionInterval: time.Minute,
FinalizationPeriod: time.Hour,
Expand Down
8 changes: 4 additions & 4 deletions x/opchild/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ func Test_QuerySetBridgeInfo(t *testing.T) {
BridgeId: 1,
BridgeAddr: addrsStr[1],
BridgeConfig: ophosttypes.BridgeConfig{
Challenger: addrsStr[2],
Proposer: addrsStr[3],
Challengers: []string{addrsStr[2]},
Proposer: addrsStr[3],
BatchInfo: ophosttypes.BatchInfo{
Submitter: addrsStr[4],
Chain: "l1",
Submitters: []string{addrsStr[4]},
Chain: "l1",
},
SubmissionInterval: time.Minute,
FinalizationPeriod: time.Hour,
Expand Down
18 changes: 15 additions & 3 deletions x/ophost/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,47 +119,59 @@ func NewCreateBridge(ac address.Codec) *cobra.Command {
return err
}

origConfig := BridgeConfig{}
origConfig := BridgeCliConfig{}
err = json.Unmarshal(configBytes, &origConfig)
if err != nil {
fmt.Println("Error unmarshaling config file:", err)

Check warning on line 125 in x/ophost/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/client/cli/tx.go#L125

Added line #L125 was not covered by tests
return err
}

submissionInterval, err := time.ParseDuration(origConfig.SubmissionInterval)
if err != nil {
fmt.Println("Error parsing submission interval:", err)
return err
}

finalizationPeriod, err := time.ParseDuration(origConfig.FinalizationPeriod)
if err != nil {
fmt.Println("Error parsing finalization period:", err)

Check warning on line 137 in x/ophost/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/client/cli/tx.go#L137

Added line #L137 was not covered by tests
return err
}

submissionStartTime, err := time.Parse(time.RFC3339, origConfig.SubmissionStartTime)
if err != nil {
fmt.Println("Error parsing submission start time:", err)

Check warning on line 143 in x/ophost/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/client/cli/tx.go#L143

Added line #L143 was not covered by tests
return err
}

fmt.Println("Original Config:", origConfig)

config := types.BridgeConfig{
Challenger: origConfig.Challenger,
Challengers: []string{origConfig.Challenger}, // Ensure Challenger is properly assigned
Proposer: origConfig.Proposer,
BatchInfo: types.BatchInfo{Submitters: []string{origConfig.BatchInfo.Submitter}, Chain: origConfig.BatchInfo.Chain}, // Ensure Submitter is properly assigned
SubmissionInterval: submissionInterval,
FinalizationPeriod: finalizationPeriod,
SubmissionStartTime: submissionStartTime,
Metadata: []byte(origConfig.Metadata),
BatchInfo: origConfig.BatchInfo,
}

fmt.Println("Converted Config:", config)

if err = config.Validate(ac); err != nil {
fmt.Println("Error validating config:", err)

Check warning on line 162 in x/ophost/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/client/cli/tx.go#L162

Added line #L162 was not covered by tests
return err
}

fromAddr, err := ac.BytesToString(clientCtx.GetFromAddress())
if err != nil {
fmt.Println("Error getting from address:", err)

Check warning on line 168 in x/ophost/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/client/cli/tx.go#L168

Added line #L168 was not covered by tests
return err
}

msg := types.NewMsgCreateBridge(fromAddr, config)
if err = msg.Validate(ac); err != nil {
fmt.Println("Error validating message:", err)

Check warning on line 174 in x/ophost/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/client/cli/tx.go#L174

Added line #L174 was not covered by tests
return err
}

Expand Down
35 changes: 14 additions & 21 deletions x/ophost/client/cli/types.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
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
type BridgeConfig struct {
// The address of the challenger.
Challenger string `protobuf:"bytes,1,opt,name=challenger,proto3" json:"challenger,omitempty"`
// The address of the proposer.
Proposer string `protobuf:"bytes,2,opt,name=proposer,proto3" json:"proposer,omitempty"`
// The time interval at which checkpoints must be submitted.
// NOTE: this param is currently not used, but will be used for challenge in future.
SubmissionInterval string `protobuf:"bytes,3,opt,name=submission_interval,json=submissionInterval,proto3,stdduration" json:"submission_interval,omitempty"`
// The minium time duration that must elapse before a withdrawal can be finalized.
FinalizationPeriod string `protobuf:"bytes,4,opt,name=finalization_period,json=finalizationPeriod,proto3,stdduration" json:"finalization_period,omitempty"`
// The time of the first l2 block recorded.
// 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"`
// BatchInfo is the batch information for the bridge.
BatchInfo types.BatchInfo `json:"batch_info"`
// NOTE: it is a modified BridgeConfig from x/ophost/types/types.go to make unmarshal easier
type BridgeCliConfig struct {
Challenger string `json:"challenger"`
Proposer string `json:"proposer"`
SubmissionInterval string `json:"submission_interval"`
FinalizationPeriod string `json:"finalization_period"`
SubmissionStartTime string `json:"submission_start_time"`
Metadata string `json:"metadata"`
BatchInfo BatchCliInfo `json:"batch_info"`
}

type BatchCliInfo struct {
Submitter string `json:"submitter"`
Chain string `json:"chain"`
}

// MsgFinalizeTokenWithdrawal is a message to remove a validator from designated list
Expand Down
20 changes: 10 additions & 10 deletions x/ophost/keeper/batch_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
func Test_SetGetBatchInfo(t *testing.T) {
ctx, input := createDefaultTestInput(t)
batchInfo1 := types.BatchInfo{
Submitter: addrsStr[0],
Chain: "l1",
Submitters: []string{addrsStr[0]},
Chain: "l1",
}
output1 := types.Output{
OutputRoot: []byte{1, 2, 3},
Expand All @@ -24,8 +24,8 @@ func Test_SetGetBatchInfo(t *testing.T) {
require.NoError(t, err)

batchInfo2 := types.BatchInfo{
Submitter: addrsStr[1],
Chain: "ll1",
Submitters: []string{addrsStr[1]},
Chain: "ll1",
}
output2 := types.Output{
OutputRoot: []byte{4, 5, 6},
Expand All @@ -37,8 +37,8 @@ func Test_SetGetBatchInfo(t *testing.T) {
require.NoError(t, err)

batchInfo3 := types.BatchInfo{
Submitter: addrsStr[0],
Chain: "lll1",
Submitters: []string{addrsStr[0]},
Chain: "lll1",
}
output3 := types.Output{
OutputRoot: []byte{1, 2, 3},
Expand All @@ -50,8 +50,8 @@ func Test_SetGetBatchInfo(t *testing.T) {
require.NoError(t, err)

batchInfo4 := types.BatchInfo{
Submitter: addrsStr[1],
Chain: "llll1",
Submitters: []string{addrsStr[1]},
Chain: "llll1",
}
output4 := types.Output{
OutputRoot: []byte{4, 5, 6},
Expand All @@ -63,8 +63,8 @@ func Test_SetGetBatchInfo(t *testing.T) {
require.NoError(t, err)

batchInfo5 := types.BatchInfo{
Submitter: addrsStr[1],
Chain: "lllll1",
Submitters: []string{addrsStr[1]},
Chain: "lllll1",
}
output5 := types.Output{
OutputRoot: []byte{4, 5, 6},
Expand Down
12 changes: 6 additions & 6 deletions x/ophost/keeper/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
func Test_BridgeConfig(t *testing.T) {
ctx, input := createDefaultTestInput(t)
config := types.BridgeConfig{
Challenger: addrs[0].String(),
Challengers: []string{addrs[0].String()},
Proposer: addrs[1].String(),
SubmissionInterval: time.Second * 100,
FinalizationPeriod: time.Second * 10,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{1, 2, 3},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
BatchInfo: types.BatchInfo{Submitters: []string{addrsStr[0]}, Chain: "l1"},
}
require.NoError(t, input.OPHostKeeper.SetBridgeConfig(ctx, 1, config))
_config, err := input.OPHostKeeper.GetBridgeConfig(ctx, 1)
Expand All @@ -28,22 +28,22 @@ func Test_BridgeConfig(t *testing.T) {
func Test_IterateBridgeConfig(t *testing.T) {
ctx, input := createDefaultTestInput(t)
config1 := types.BridgeConfig{
Challenger: addrs[0].String(),
Challengers: []string{addrs[0].String()},
Proposer: addrs[1].String(),
SubmissionInterval: time.Second * 100,
FinalizationPeriod: time.Second * 10,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{1, 2, 3},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
BatchInfo: types.BatchInfo{Submitters: []string{addrsStr[0]}, Chain: "l1"},
}
config2 := types.BridgeConfig{
Challenger: addrs[2].String(),
Challengers: []string{addrs[2].String()},
Proposer: addrs[3].String(),
SubmissionInterval: time.Second * 100,
FinalizationPeriod: time.Second * 10,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte{3, 4, 5},
BatchInfo: types.BatchInfo{Submitter: addrsStr[0], Chain: "l1"},
BatchInfo: types.BatchInfo{Submitters: []string{addrsStr[0]}, Chain: "l1"},
}
require.NoError(t, input.OPHostKeeper.SetBridgeConfig(ctx, 1, config1))
require.NoError(t, input.OPHostKeeper.SetBridgeConfig(ctx, 2, config2))
Expand Down
14 changes: 7 additions & 7 deletions x/ophost/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,11 @@ func _createTestInput(
}

type bridgeHook struct {
proposer string
challenger string
batchInfo ophosttypes.BatchInfo
metadata []byte
err error
proposer string
challengers []string
batchInfo ophosttypes.BatchInfo
metadata []byte
err error
}

func (h *bridgeHook) BridgeCreated(
Expand All @@ -336,7 +336,7 @@ func (h *bridgeHook) BridgeCreated(

h.metadata = bridgeConfig.Metadata
h.proposer = bridgeConfig.Proposer
h.challenger = bridgeConfig.Challenger
h.challengers = bridgeConfig.Challengers
return nil
}

Expand All @@ -349,7 +349,7 @@ func (h *bridgeHook) BridgeChallengerUpdated(
return h.err
}

h.challenger = bridgeConfig.Challenger
h.challengers = bridgeConfig.Challengers

return nil
}
Expand Down
Loading

0 comments on commit 47bf861

Please sign in to comment.