Skip to content

Commit

Permalink
fix launch config to receive time duration in more human readable unit
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Aug 6, 2024
1 parent 807186f commit 9332e1a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
64 changes: 61 additions & 3 deletions contrib/launchtools/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,16 @@ type OpBridge struct {
OutputSubmissionStartHeight uint64 `json:"output_submission_start_height,omitempty"`

// batch submission setup
BatchSubmitTarget ophosttypes.BatchInfo_ChainType `json:"batch_submission_target"`
BatchSubmissionTarget ophosttypes.BatchInfo_ChainType `json:"batch_submission_target"`
}

func (opBridge *OpBridge) Finalize() error {
if opBridge.OutputSubmissionStartHeight == 0 {
opBridge.OutputSubmissionStartHeight = 1
}

if opBridge.BatchSubmitTarget == ophosttypes.BatchInfo_CHAIN_TYPE_UNSPECIFIED {
opBridge.BatchSubmitTarget = ophosttypes.BatchInfo_CHAIN_TYPE_INITIA
if opBridge.BatchSubmissionTarget == ophosttypes.BatchInfo_CHAIN_TYPE_UNSPECIFIED {
opBridge.BatchSubmissionTarget = ophosttypes.BatchInfo_CHAIN_TYPE_INITIA
}

if opBridge.OutputSubmissionInterval == nil {
Expand All @@ -141,6 +141,64 @@ func (opBridge *OpBridge) Finalize() error {
return nil
}

func (opBridge *OpBridge) UnmarshalJSON(data []byte) error {
var tmp struct {
OutputSubmissionInterval string `json:"output_submission_interval,omitempty"`
OutputFinalizationPeriod string `json:"output_finalization_period,omitempty"`
OutputSubmissionStartHeight uint64 `json:"output_submission_start_height,omitempty"`
BatchSubmissionTarget ophosttypes.BatchInfo_ChainType `json:"batch_submission_target,omitempty"`
}

if err := json.Unmarshal(data, &tmp); err != nil {
return err
}

if tmp.OutputSubmissionInterval != "" {
d, err := time.ParseDuration(tmp.OutputSubmissionInterval)
if err != nil {
return err
}

opBridge.OutputSubmissionInterval = &d
}

if tmp.OutputFinalizationPeriod != "" {
d, err := time.ParseDuration(tmp.OutputFinalizationPeriod)
if err != nil {
return err
}

opBridge.OutputFinalizationPeriod = &d
}

opBridge.OutputSubmissionStartHeight = tmp.OutputSubmissionStartHeight
opBridge.BatchSubmissionTarget = tmp.BatchSubmissionTarget

return nil
}

func (opBridge OpBridge) MarshalJSON() ([]byte, error) {
tmp := struct {
OutputSubmissionInterval string `json:"output_submission_interval,omitempty"`
OutputFinalizationPeriod string `json:"output_finalization_period,omitempty"`
OutputSubmissionStartHeight uint64 `json:"output_submission_start_height,omitempty"`
BatchSubmissionTarget ophosttypes.BatchInfo_ChainType `json:"batch_submission_target,omitempty"`
}{
OutputSubmissionStartHeight: opBridge.OutputSubmissionStartHeight,
BatchSubmissionTarget: opBridge.BatchSubmissionTarget,
}

if opBridge.OutputSubmissionInterval != nil {
tmp.OutputSubmissionInterval = opBridge.OutputSubmissionInterval.String()
}

if opBridge.OutputFinalizationPeriod != nil {
tmp.OutputFinalizationPeriod = opBridge.OutputFinalizationPeriod.String()
}

return json.Marshal(tmp)
}

type L1Config struct {
ChainID string `json:"chain_id,omitempty"`
RPC_URL string `json:"rpc_url,omitempty"`
Expand Down
31 changes: 31 additions & 0 deletions contrib/launchtools/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package launchtools

import (
"encoding/json"
"testing"
"time"

ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
"github.com/stretchr/testify/require"
)

func Test_opBridgeConfig_JSON(t *testing.T) {
submissionInterval := time.Second
finalizationPeriod := time.Second * 10

opBridgeConfig := &OpBridge{
OutputSubmissionInterval: &submissionInterval,
OutputFinalizationPeriod: &finalizationPeriod,
OutputSubmissionStartHeight: 1,
BatchSubmissionTarget: ophosttypes.BatchInfo_CHAIN_TYPE_CELESTIA,
}

bz, err := json.Marshal(opBridgeConfig)
require.NoError(t, err)
require.Equal(t, `{"output_submission_interval":"1s","output_finalization_period":"10s","output_submission_start_height":1,"batch_submission_target":"CELESTIA"}`, string(bz))

var opBridgeConfig2 OpBridge
err = json.Unmarshal(bz, &opBridgeConfig2)
require.NoError(t, err)
require.Equal(t, opBridgeConfig, &opBridgeConfig2)
}
2 changes: 1 addition & 1 deletion contrib/launchtools/steps/opbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func InitializeOpBridge(
config.SystemKeys.Challenger.L1Address,
config.SystemKeys.OutputSubmitter.L1Address,
config.SystemKeys.BatchSubmitter.L1Address,
config.OpBridge.BatchSubmitTarget,
config.OpBridge.BatchSubmissionTarget,
*config.OpBridge.OutputSubmissionInterval,
*config.OpBridge.OutputFinalizationPeriod,
config.OpBridge.OutputSubmissionStartHeight,
Expand Down

0 comments on commit 9332e1a

Please sign in to comment.