From 9332e1a209fefcf9d6d03722a53527cc31e220a4 Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Tue, 6 Aug 2024 15:30:29 +0900 Subject: [PATCH] fix launch config to receive time duration in more human readable unit --- contrib/launchtools/config.go | 64 +++++++++++++++++++++++++-- contrib/launchtools/config_test.go | 31 +++++++++++++ contrib/launchtools/steps/opbridge.go | 2 +- 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 contrib/launchtools/config_test.go diff --git a/contrib/launchtools/config.go b/contrib/launchtools/config.go index 660fca5..5586b8f 100644 --- a/contrib/launchtools/config.go +++ b/contrib/launchtools/config.go @@ -116,7 +116,7 @@ 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 { @@ -124,8 +124,8 @@ func (opBridge *OpBridge) Finalize() error { 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 { @@ -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"` diff --git a/contrib/launchtools/config_test.go b/contrib/launchtools/config_test.go new file mode 100644 index 0000000..27a8e21 --- /dev/null +++ b/contrib/launchtools/config_test.go @@ -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) +} diff --git a/contrib/launchtools/steps/opbridge.go b/contrib/launchtools/steps/opbridge.go index 30ca1d3..5e2a2d1 100644 --- a/contrib/launchtools/steps/opbridge.go +++ b/contrib/launchtools/steps/opbridge.go @@ -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,