Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/start height #8

Merged
merged 25 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ To reset the bot database, use the following command:
opinitd reset-db [bot-name]
```

### Query status
```bash
curl localhost:3000/status
```

### Query withdrawals
```bash
curl localhost:3000/withdrawal/{sequence} | jq . > ./withdrawal-info.json
Expand Down
1 change: 1 addition & 0 deletions bot/types/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

type Bot interface {
Initialize(context.Context) error
Start(context.Context) error
Close()
}
2 changes: 1 addition & 1 deletion cmd/opinitd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func resetDBCmd(ctx *cmdContext) *cobra.Command {
if err != nil {
return err
}
err = os.Remove(path.Join(ctx.homePath, "batch"))
err = os.RemoveAll(path.Join(ctx.homePath, "batch"))
if err != nil {
return err
}
Expand Down
21 changes: 20 additions & 1 deletion cmd/opinitd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"

"github.com/initia-labs/opinit-bots-go/bot"
bottypes "github.com/initia-labs/opinit-bots-go/bot/types"
"github.com/initia-labs/opinit-bots-go/types"
)

const (
flagPollingInterval = "polling-interval"
)

func startCmd(ctx *cmdContext) *cobra.Command {
Expand All @@ -37,11 +44,23 @@ Currently supported bots:
cmdCtx, botDone := context.WithCancel(cmd.Context())
gracefulShutdown(botDone)

return bot.Start(cmdCtx)
errGrp, ctx := errgroup.WithContext(cmdCtx)
ctx = types.WithErrGrp(ctx, errGrp)
interval, err := cmd.Flags().GetDuration(flagPollingInterval)
if err != nil {
return err
}
ctx = types.WithPollingInterval(ctx, interval)
err = bot.Initialize(ctx)
if err != nil {
return err
}
return bot.Start(ctx)
},
}

cmd = configFlag(ctx.v, cmd)
cmd.Flags().Duration(flagPollingInterval, 100*time.Millisecond, "Polling interval in milliseconds")
return cmd
}

Expand Down
131 changes: 104 additions & 27 deletions executor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,126 @@ To configure the Executor, fill in the values in the `~/.opinit/executor.json` f
{
// Version is the version used to build output root.
"version": 1,

// ListenAddress is the address to listen for incoming requests.
"listen_address": "localhost:3000",

"l1_rpc_address": "tcp://localhost:26657",
"l2_rpc_address": "tcp://localhost:27657",
"da_rpc_address": "tcp://localhost:27657",

"l1_gas_price": "0.15uinit",
"l2_gas_price": "",
"da_gas_price": "",

"l1_chain_id": "testnet-l1-1",
"l2_chain_id": "testnet-l2-1",
"da_chain_id": "testnet-da-1",

"l1_bech32_prefix": "init",
"l2_bech32_prefix": "init",
"da_bech32_prefix": "init",

"l1_node": {
"chain_id": "testnet-l1-1",
"bech32_prefix": "init",
"rpc_address": "tcp://localhost:26657",
"gas_price": "0.15uinit",
"gas_adjustment": 1.5,
"tx_timeout": 60
},
"l2_node": {
"chain_id": "testnet-l2-1",
"bech32_prefix": "init",
"rpc_address": "tcp://localhost:27657",
"gas_price": "",
"gas_adjustment": 1.5,
"tx_timeout": 60
},
"da_node": {
"chain_id": "testnet-l1-1",
"bech32_prefix": "init",
"rpc_address": "tcp://localhost:26657",
"gas_price": "0.15uinit",
"gas_adjustment": 1.5,
"tx_timeout": 60
},
// OutputSubmitter is the key name in the keyring for the output submitter,
// which is used to relay the output transaction from l2 to l1.
//
// If you don't want to use the output submitter feature, you can leave it empty.
"output_submitter": "output_submitter",
"output_submitter": "",

// BridgeExecutor is the key name in the keyring for the bridge executor,
// which is used to relay initiate token bridge transaction from l1 to l2.
//
// If you don't want to use the bridge executor feature, you can leave it empty.
"bridge_executor": "bridge_executor",

"bridge_executor": "",
// RelayOracle is the flag to enable the oracle relay feature.
"relay_oracle": true,

// MaxChunks is the maximum number of chunks in a batch.
"max_chunks": 5000,
// MaxChunkSize is the maximum size of a chunk in a batch.
"max_chunk_size": 300000,
// MaxSubmissionTime is the maximum time to submit a batch.
"max_submission_time": 3600, // seconds
"max_submission_time": 3600,
// 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
"l2_start_height": 0,
// StartBatchHeight is the height to start the batch. 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.
"batch_start_height": 0
}
```

### Start height config examples
If the latest height stored in the db is not 0, start height config is ignored.

```
Output tx 1
- L1BlockNumber: 10
- L2BlockNumber: 100

Output tx 2
- L1BlockNumber: 20
- L2BlockNumber: 200

InitializeTokenDeposit tx 1
- Height: 5
- L1Sequence: 1

InitializeTokenDeposit tx 2
- Height: 15
- L1Sequence: 2

FinalizedTokenDeposit tx 1
- L1Sequence: 1

FinalizedTokenDeposit tx 2
- L1Sequence: 2
```

#### Config 1
```json
{
l2_start_height: 150,
batch_start_height: 0
}
```
When Child's last l1 Sequence is `2`,
- L1 starts from the height 10 + 1 = 11
- L2 starts from the height 100 + 1 = 101
- Batch starts from the height 1

#### Config 2
```json
{
l2_start_height: 150,
batch_start_height: 150
}
```
When Child's last l1 Sequence is `2`,
- L1 starts from the height 10 + 1 = 11
- L2 starts from the height 100 + 1 = 101
- Batch starts from the height 150

#### Config 3
```json
{
l2_start_height: 150,
batch_start_height: 150
}
```
When Child's last l1 Sequence is `1`,
- L1 starts from the height 5 + 1 = 6
- L2 starts from the height 100 + 1 = 101
- Batch starts from the height 150


## Handler rules for the components of the Executor
For registered events or tx handlers, work processed in a block is atomically saved as ProcessedMsg. Therfore, if ProcessedMsgs or Txs cannot be processed due to an interrupt or error, it is guaranteed to be read from the DB and processed.

Expand Down Expand Up @@ -149,10 +225,11 @@ If the batch info registered in the chain is changed to change the account or DA

```go
{
DARPCAddress string `json:"da_rpc_address"`
DAGasPrice string `json:"da_gas_price"`
DAChainID string `json:"da_chain_id"`
DABech32Prefix string `json:"da_bech32_prefix"`
RPCAddress string `json:"rpc_address"`
GasPrice string `json:"gas_price"`
GasAdjustment string `json:"gas_adjustment"`
ChainID string `json:"chain_id"`
Bech32Prefix string `json:"bech32_prefix"`
}
```
## Sync from the beginning
Expand Down
21 changes: 17 additions & 4 deletions executor/batch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

type hostNode interface {
QueryBatchInfos() (*ophosttypes.QueryBatchInfosResponse, error)
QueryBatchInfos(context.Context, uint64) (*ophosttypes.QueryBatchInfosResponse, error)
}

type compressionFunc interface {
Expand Down Expand Up @@ -62,6 +62,9 @@ type BatchSubmitter struct {
homePath string

lastSubmissionTime time.Time

// status info
LastBatchEndBlockNumber uint64
}

func NewBatchSubmitter(
Expand Down Expand Up @@ -104,11 +107,15 @@ func NewBatchSubmitter(
return ch
}

func (bs *BatchSubmitter) Initialize(host hostNode, bridgeInfo opchildtypes.BridgeInfo) error {
func (bs *BatchSubmitter) Initialize(ctx context.Context, startHeight uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error {
err := bs.node.Initialize(startHeight)
if err != nil {
return err
}
bs.host = host
bs.bridgeInfo = bridgeInfo

res, err := bs.host.QueryBatchInfos()
res, err := bs.host.QueryBatchInfos(ctx, bridgeInfo.BridgeId)
if err != nil {
return err
}
Expand All @@ -123,7 +130,13 @@ func (bs *BatchSubmitter) Initialize(host hostNode, bridgeInfo opchildtypes.Brid
bs.DequeueBatchInfo()
}

bs.batchFile, err = os.OpenFile(bs.homePath+"/batch", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
fileFlag := os.O_CREATE | os.O_RDWR
// if the node has already processed blocks, append to the file
if !bs.node.HeightInitialized() {
fileFlag |= os.O_APPEND
}

bs.batchFile, err = os.OpenFile(bs.homePath+"/batch", fileFlag, 0666)
if err != nil {
return err
}
Expand Down
Loading
Loading