Skip to content

Commit

Permalink
add l1 start height in config to avoid to use TxSearch (#32)
Browse files Browse the repository at this point in the history
* add l1 start height in config to avoid to use TxSearch

* fmt

* fix indentation on readme
  • Loading branch information
sh-cha authored Oct 16, 2024
1 parent 19d59b9 commit 61574f4
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 51 deletions.
7 changes: 6 additions & 1 deletion challenger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The Challenger is responsible for

## Config

To configure the Executor, fill in the values in the `~/.opinit/executor.json` file.
To configure the Challenger, fill in the values in the `~/.opinit/challenger.json` file.

```json
{
Expand All @@ -28,6 +28,11 @@ To configure the Executor, fill in the values in the `~/.opinit/executor.json` f
"bech32_prefix": "init",
"rpc_address": "tcp://localhost:27657",
},
// L1StartHeight is the height to start the l1 node. If it is 0, it will finds the optimal height and sets it automatically.
// However, if you do not want to use this feature, set it to a non-zero value.
// There is no need for modification under normal circumstances, because it
// is automatically determined when you set the l2 start height,
"l1_start_height": 0,
// L2StartHeight is the height to start the l2 node. If it is 0, it will start from the latest height.
// If the latest height stored in the db is not 0, this config is ignored.
// L2 starts from the last submitted output l2 block number + 1 before L2StartHeight.
Expand Down
51 changes: 31 additions & 20 deletions challenger/challenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,43 +193,54 @@ func (c *Challenger) RegisterQuerier() {
}

func (c *Challenger) getProcessedHeights(ctx context.Context, bridgeId uint64) (l1ProcessedHeight int64, l2ProcessedHeight int64, processedOutputIndex uint64, err error) {
// get the bridge start height from the host
l1ProcessedHeight, err = c.host.QueryCreateBridgeHeight(ctx, bridgeId)
if err != nil {
return 0, 0, 0, err
}

var outputL1BlockNumber int64
// get the last submitted output height before the start height from the host
if c.cfg.L2StartHeight != 0 {
output, err := c.host.QueryLastFinalizedOutput(ctx, bridgeId)
if err != nil {
return 0, 0, 0, err
} else if output != nil {
l1ProcessedHeight = types.MustUint64ToInt64(output.OutputProposal.L1BlockNumber)
outputL1BlockNumber = types.MustUint64ToInt64(output.OutputProposal.L1BlockNumber)
l2ProcessedHeight = types.MustUint64ToInt64(output.OutputProposal.L2BlockNumber)
processedOutputIndex = output.OutputIndex
}
}
if l2ProcessedHeight > 0 {
// get the last deposit tx height from the host
l1Sequence, err := c.child.QueryNextL1Sequence(ctx, l2ProcessedHeight-1)

if c.cfg.L1StartHeight == 0 {
// get the bridge start height from the host
l1ProcessedHeight, err = c.host.QueryCreateBridgeHeight(ctx, bridgeId)
if err != nil {
return 0, 0, 0, err
}
// query l1Sequence tx height
depositTxHeight, err := c.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence)
if err != nil {
return 0, 0, 0, err
} else if depositTxHeight == 0 && l1Sequence > 1 {
// query l1Sequence - 1 tx height
depositTxHeight, err = c.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence-1)

if l2ProcessedHeight > 0 {
l1Sequence, err := c.child.QueryNextL1Sequence(ctx, l2ProcessedHeight-1)
if err != nil {
return 0, 0, 0, err
}
// query l1Sequence tx height
depositTxHeight, err := c.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence)
if err != nil {
return 0, 0, 0, err
} else if depositTxHeight == 0 && l1Sequence > 1 {
// query l1Sequence - 1 tx height
depositTxHeight, err = c.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence-1)
if err != nil {
return 0, 0, 0, err
}
}

if depositTxHeight > l1ProcessedHeight {
l1ProcessedHeight = depositTxHeight
}
if outputL1BlockNumber < l1ProcessedHeight {
l1ProcessedHeight = outputL1BlockNumber
}
}
if depositTxHeight >= 1 && depositTxHeight-1 < l1ProcessedHeight {
l1ProcessedHeight = depositTxHeight - 1
}
} else {
l1ProcessedHeight = c.cfg.L1StartHeight
}
l1ProcessedHeight--

return l1ProcessedHeight, l2ProcessedHeight, processedOutputIndex, err
}
16 changes: 15 additions & 1 deletion challenger/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ type Config struct {
// L2Node is the configuration for the l2 node.
L2Node NodeConfig `json:"l2_node"`

// L1StartHeight is the height to start the l1 node. If it is 0, it will finds the optimal height and sets it automatically.
// However, if you do not want to use this feature, set it to a non-zero value.
// There is no need for modification under normal circumstances, because it
// is automatically determined when you set the l2 start height,
L1StartHeight int64 `json:"l1_start_height"`
// L2StartHeight is the height to start the l2 node. If it is 0, it will start from the latest height.
// If the latest height stored in the db is not 0, this config is ignored.
// L2 starts from the last submitted output l2 block number + 1 before L2StartHeight.
// L1 starts from the block number of the output tx + 1
L2StartHeight uint64 `json:"l2_start_height"`
L2StartHeight int64 `json:"l2_start_height"`
}

func DefaultConfig() *Config {
Expand All @@ -60,6 +65,7 @@ func DefaultConfig() *Config {
Bech32Prefix: "init",
RPCAddress: "tcp://localhost:27657",
},
L1StartHeight: 0,
L2StartHeight: 0,
}
}
Expand All @@ -84,6 +90,14 @@ func (cfg Config) Validate() error {
if err := cfg.L2Node.Validate(); err != nil {
return err
}

if cfg.L1StartHeight < 0 {
return errors.New("l1 start height must be greater than or equal to 0")
}

if cfg.L2StartHeight < 0 {
return errors.New("l2 start height must be greater than or equal to 0")
}
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions executor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ To configure the Executor, fill in the values in the `~/.opinit/executor.json` f
"max_chunk_size": 300000,
// MaxSubmissionTime is the maximum time to submit a batch.
"max_submission_time": 3600,
// L1StartHeight is the height to start the l1 node. If it is 0, it will finds the optimal height and sets it automatically.
// However, if you do not want to use this feature, set it to a non-zero value.
// There is no need for modification under normal circumstances, because it
// is automatically determined when you set the l2 start height,
"l1_start_height": 0,
// L2StartHeight is the height to start the l2 node. If it is 0, it will start from the latest height.
// If the latest height stored in the db is not 0, this config is ignored.
// L2 starts from the last submitted output l2 block number + 1 before L2StartHeight.
Expand Down
3 changes: 2 additions & 1 deletion executor/batch/noop_da.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package batch
import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
executortypes "github.com/initia-labs/opinit-bots/executor/types"
btypes "github.com/initia-labs/opinit-bots/node/broadcaster/types"
nodetypes "github.com/initia-labs/opinit-bots/node/types"
"github.com/initia-labs/opinit-bots/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

var _ executortypes.DANode = &NoopDA{}
Expand Down
62 changes: 34 additions & 28 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,46 +214,52 @@ func (ex *Executor) makeDANode(ctx context.Context, bridgeInfo opchildtypes.Brid
}

func (ex *Executor) getProcessedHeights(ctx context.Context, bridgeId uint64) (l1ProcessedHeight int64, l2ProcessedHeight int64, processedOutputIndex uint64, batchProcessedHeight int64, err error) {
// get the bridge start height from the host
l1ProcessedHeight, err = ex.host.QueryCreateBridgeHeight(ctx, bridgeId)
if err != nil {
return 0, 0, 0, 0, err
var outputL1BlockNumber int64
// get the last submitted output height before the start height from the host
if ex.cfg.L2StartHeight != 0 {
output, err := ex.host.QueryOutputByL2BlockNumber(ctx, bridgeId, ex.cfg.L2StartHeight)
if err != nil {
return 0, 0, 0, 0, err
} else if output != nil {
outputL1BlockNumber = types.MustUint64ToInt64(output.OutputProposal.L1BlockNumber)
l2ProcessedHeight = types.MustUint64ToInt64(output.OutputProposal.L2BlockNumber)
processedOutputIndex = output.OutputIndex
}
}

l1Sequence, err := ex.child.QueryNextL1Sequence(ctx, 0)
if err != nil {
return 0, 0, 0, 0, err
}
if ex.cfg.L1StartHeight == 0 {
// get the bridge start height from the host
l1ProcessedHeight, err = ex.host.QueryCreateBridgeHeight(ctx, bridgeId)
if err != nil {
return 0, 0, 0, 0, err
}

// query l1Sequence tx height
depositTxHeight, err := ex.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence)
if err != nil {
return 0, 0, 0, 0, err
} else if depositTxHeight == 0 && l1Sequence > 1 {
// query l1Sequence - 1 tx height
depositTxHeight, err = ex.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence-1)
l1Sequence, err := ex.child.QueryNextL1Sequence(ctx, 0)
if err != nil {
return 0, 0, 0, 0, err
}
}
if depositTxHeight >= 1 && depositTxHeight-1 > l1ProcessedHeight {
l1ProcessedHeight = depositTxHeight - 1
}

// get the last submitted output height before the start height from the host
if ex.cfg.L2StartHeight != 0 {
output, err := ex.host.QueryOutputByL2BlockNumber(ctx, bridgeId, ex.cfg.L2StartHeight)
// query l1Sequence tx height
depositTxHeight, err := ex.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence)
if err != nil {
return 0, 0, 0, 0, err
} else if output != nil {
l1BlockNumber := types.MustUint64ToInt64(output.OutputProposal.L1BlockNumber)
if l1BlockNumber < l1ProcessedHeight {
l1ProcessedHeight = l1BlockNumber
} else if depositTxHeight == 0 && l1Sequence > 1 {
// query l1Sequence - 1 tx height
depositTxHeight, err = ex.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence-1)
if err != nil {
return 0, 0, 0, 0, err
}
l2ProcessedHeight = types.MustUint64ToInt64(output.OutputProposal.L2BlockNumber)
processedOutputIndex = output.OutputIndex
}
if depositTxHeight > l1ProcessedHeight {
l1ProcessedHeight = depositTxHeight
}
if outputL1BlockNumber < l1ProcessedHeight {
l1ProcessedHeight = outputL1BlockNumber
}
} else {
l1ProcessedHeight = ex.cfg.L1StartHeight
}
l1ProcessedHeight--

if ex.cfg.BatchStartHeight > 0 {
batchProcessedHeight = ex.cfg.BatchStartHeight - 1
Expand Down
10 changes: 10 additions & 0 deletions executor/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ type Config struct {
// MaxSubmissionTime is the maximum time to submit a batch.
MaxSubmissionTime int64 `json:"max_submission_time"` // seconds

// L1StartHeight is the height to start the l1 node. If it is 0, it will finds the optimal height and sets it automatically.
// However, if you do not want to use this feature, set it to a non-zero value.
// There is no need for modification under normal circumstances, because it
// is automatically determined when you set the l2 start height,
L1StartHeight int64 `json:"l1_start_height"`
// L2StartHeight is the height to start the l2 node. If it is 0, it will start from the latest height.
// If the latest height stored in the db is not 0, this config is ignored.
// L2 starts from the last submitted output l2 block number + 1 before L2StartHeight.
Expand Down Expand Up @@ -117,6 +122,7 @@ func DefaultConfig() *Config {
MaxChunkSize: 300000, // 300KB
MaxSubmissionTime: 60 * 60, // 1 hour

L1StartHeight: 0,
L2StartHeight: 0,
BatchStartHeight: 0,
}
Expand Down Expand Up @@ -159,6 +165,10 @@ func (cfg Config) Validate() error {
return errors.New("max submission time must be greater than 0")
}

if cfg.L1StartHeight < 0 {
return errors.New("l1 start height must be greater than or equal to 0")
}

if cfg.L2StartHeight < 0 {
return errors.New("l2 start height must be greater than or equal to 0")
}
Expand Down

0 comments on commit 61574f4

Please sign in to comment.