Skip to content

Commit f8acae3

Browse files
authored
Feat/start height (#8)
* enable to start l1, l2, batch at specific height * ignoring error bug fix * add batch start with l2 height flag to sync with l2 * tree starts specific index when initializing the height * bug fix starting tree index at 1 * change block height query from tx_search to output * remove unused code * bug fix searching output index * config refactor & make queries use context * lint * update readme * change retry sleep to timer * add logs * add msg types to pending tx & logs * delete l2 sync batch start height * add status * bump opinit v0.4.2 * lint & format * chore status change * update readme * change tree initialization function to sync once * add log when broadcaster is waiting for tx timeout * lint * retry log msg change * update readme node version
1 parent 048b675 commit f8acae3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+903
-317
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ To ensure compatibility with the node version, check the following versions:
1818

1919
| L1 Node | MiniMove | MiniWasm | MiniEVM |
2020
| ------- | -------- | -------- | ------- |
21-
| v0.4.1 | v0.4.1 | v0.4.1 | v0.4.1 |
21+
| v0.4.2 | v0.4.0 | v0.4.0 | v0.4.0 |
2222

2323
### Build and Configure
2424

@@ -64,6 +64,11 @@ To reset the bot database, use the following command:
6464
opinitd reset-db [bot-name]
6565
```
6666

67+
### Query status
68+
```bash
69+
curl localhost:3000/status
70+
```
71+
6772
### Query withdrawals
6873
```bash
6974
curl localhost:3000/withdrawal/{sequence} | jq . > ./withdrawal-info.json

bot/types/bot.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
)
66

77
type Bot interface {
8+
Initialize(context.Context) error
89
Start(context.Context) error
910
Close()
1011
}

cmd/opinitd/reset.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func resetDBCmd(ctx *cmdContext) *cobra.Command {
2525
if err != nil {
2626
return err
2727
}
28-
err = os.Remove(path.Join(ctx.homePath, "batch"))
28+
err = os.RemoveAll(path.Join(ctx.homePath, "batch"))
2929
if err != nil {
3030
return err
3131
}

cmd/opinitd/start.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ import (
66
"os"
77
"os/signal"
88
"syscall"
9+
"time"
910

1011
"github.com/spf13/cobra"
12+
"golang.org/x/sync/errgroup"
1113

1214
"github.com/initia-labs/opinit-bots-go/bot"
1315
bottypes "github.com/initia-labs/opinit-bots-go/bot/types"
16+
"github.com/initia-labs/opinit-bots-go/types"
17+
)
18+
19+
const (
20+
flagPollingInterval = "polling-interval"
1421
)
1522

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

40-
return bot.Start(cmdCtx)
47+
errGrp, ctx := errgroup.WithContext(cmdCtx)
48+
ctx = types.WithErrGrp(ctx, errGrp)
49+
interval, err := cmd.Flags().GetDuration(flagPollingInterval)
50+
if err != nil {
51+
return err
52+
}
53+
ctx = types.WithPollingInterval(ctx, interval)
54+
err = bot.Initialize(ctx)
55+
if err != nil {
56+
return err
57+
}
58+
return bot.Start(ctx)
4159
},
4260
}
4361

4462
cmd = configFlag(ctx.v, cmd)
63+
cmd.Flags().Duration(flagPollingInterval, 100*time.Millisecond, "Polling interval in milliseconds")
4564
return cmd
4665
}
4766

executor/README.md

+104-27
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,126 @@ To configure the Executor, fill in the values in the `~/.opinit/executor.json` f
1414
{
1515
// Version is the version used to build output root.
1616
"version": 1,
17-
1817
// ListenAddress is the address to listen for incoming requests.
1918
"listen_address": "localhost:3000",
20-
21-
"l1_rpc_address": "tcp://localhost:26657",
22-
"l2_rpc_address": "tcp://localhost:27657",
23-
"da_rpc_address": "tcp://localhost:27657",
24-
25-
"l1_gas_price": "0.15uinit",
26-
"l2_gas_price": "",
27-
"da_gas_price": "",
28-
29-
"l1_chain_id": "testnet-l1-1",
30-
"l2_chain_id": "testnet-l2-1",
31-
"da_chain_id": "testnet-da-1",
32-
33-
"l1_bech32_prefix": "init",
34-
"l2_bech32_prefix": "init",
35-
"da_bech32_prefix": "init",
36-
19+
"l1_node": {
20+
"chain_id": "testnet-l1-1",
21+
"bech32_prefix": "init",
22+
"rpc_address": "tcp://localhost:26657",
23+
"gas_price": "0.15uinit",
24+
"gas_adjustment": 1.5,
25+
"tx_timeout": 60
26+
},
27+
"l2_node": {
28+
"chain_id": "testnet-l2-1",
29+
"bech32_prefix": "init",
30+
"rpc_address": "tcp://localhost:27657",
31+
"gas_price": "",
32+
"gas_adjustment": 1.5,
33+
"tx_timeout": 60
34+
},
35+
"da_node": {
36+
"chain_id": "testnet-l1-1",
37+
"bech32_prefix": "init",
38+
"rpc_address": "tcp://localhost:26657",
39+
"gas_price": "0.15uinit",
40+
"gas_adjustment": 1.5,
41+
"tx_timeout": 60
42+
},
3743
// OutputSubmitter is the key name in the keyring for the output submitter,
3844
// which is used to relay the output transaction from l2 to l1.
3945
//
4046
// If you don't want to use the output submitter feature, you can leave it empty.
41-
"output_submitter": "output_submitter",
47+
"output_submitter": "",
4248

4349
// BridgeExecutor is the key name in the keyring for the bridge executor,
4450
// which is used to relay initiate token bridge transaction from l1 to l2.
4551
//
4652
// If you don't want to use the bridge executor feature, you can leave it empty.
47-
"bridge_executor": "bridge_executor",
48-
53+
"bridge_executor": "",
4954
// RelayOracle is the flag to enable the oracle relay feature.
5055
"relay_oracle": true,
51-
5256
// MaxChunks is the maximum number of chunks in a batch.
5357
"max_chunks": 5000,
5458
// MaxChunkSize is the maximum size of a chunk in a batch.
5559
"max_chunk_size": 300000,
5660
// MaxSubmissionTime is the maximum time to submit a batch.
57-
"max_submission_time": 3600, // seconds
61+
"max_submission_time": 3600,
62+
// L2StartHeight is the height to start the l2 node. If it is 0, it will start from the latest height.
63+
// If the latest height stored in the db is not 0, this config is ignored.
64+
// L2 starts from the last submitted output l2 block number + 1 before L2StartHeight.
65+
// L1 starts from the block number of the output tx + 1
66+
"l2_start_height": 0,
67+
// StartBatchHeight is the height to start the batch. If it is 0, it will start from the latest height.
68+
// If the latest height stored in the db is not 0, this config is ignored.
69+
"batch_start_height": 0
5870
}
5971
```
6072

73+
### Start height config examples
74+
If the latest height stored in the db is not 0, start height config is ignored.
75+
76+
```
77+
Output tx 1
78+
- L1BlockNumber: 10
79+
- L2BlockNumber: 100
80+
81+
Output tx 2
82+
- L1BlockNumber: 20
83+
- L2BlockNumber: 200
84+
85+
InitializeTokenDeposit tx 1
86+
- Height: 5
87+
- L1Sequence: 1
88+
89+
InitializeTokenDeposit tx 2
90+
- Height: 15
91+
- L1Sequence: 2
92+
93+
FinalizedTokenDeposit tx 1
94+
- L1Sequence: 1
95+
96+
FinalizedTokenDeposit tx 2
97+
- L1Sequence: 2
98+
```
99+
100+
#### Config 1
101+
```json
102+
{
103+
l2_start_height: 150,
104+
batch_start_height: 0
105+
}
106+
```
107+
When Child's last l1 Sequence is `2`,
108+
- L1 starts from the height 10 + 1 = 11
109+
- L2 starts from the height 100 + 1 = 101
110+
- Batch starts from the height 1
111+
112+
#### Config 2
113+
```json
114+
{
115+
l2_start_height: 150,
116+
batch_start_height: 150
117+
}
118+
```
119+
When Child's last l1 Sequence is `2`,
120+
- L1 starts from the height 10 + 1 = 11
121+
- L2 starts from the height 100 + 1 = 101
122+
- Batch starts from the height 150
123+
124+
#### Config 3
125+
```json
126+
{
127+
l2_start_height: 150,
128+
batch_start_height: 150
129+
}
130+
```
131+
When Child's last l1 Sequence is `1`,
132+
- L1 starts from the height 5 + 1 = 6
133+
- L2 starts from the height 100 + 1 = 101
134+
- Batch starts from the height 150
135+
136+
61137
## Handler rules for the components of the Executor
62138
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.
63139

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

150226
```go
151227
{
152-
DARPCAddress string `json:"da_rpc_address"`
153-
DAGasPrice string `json:"da_gas_price"`
154-
DAChainID string `json:"da_chain_id"`
155-
DABech32Prefix string `json:"da_bech32_prefix"`
228+
RPCAddress string `json:"rpc_address"`
229+
GasPrice string `json:"gas_price"`
230+
GasAdjustment string `json:"gas_adjustment"`
231+
ChainID string `json:"chain_id"`
232+
Bech32Prefix string `json:"bech32_prefix"`
156233
}
157234
```
158235
## Sync from the beginning

executor/batch/batch.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
type hostNode interface {
26-
QueryBatchInfos() (*ophosttypes.QueryBatchInfosResponse, error)
26+
QueryBatchInfos(context.Context, uint64) (*ophosttypes.QueryBatchInfosResponse, error)
2727
}
2828

2929
type compressionFunc interface {
@@ -62,6 +62,9 @@ type BatchSubmitter struct {
6262
homePath string
6363

6464
lastSubmissionTime time.Time
65+
66+
// status info
67+
LastBatchEndBlockNumber uint64
6568
}
6669

6770
func NewBatchSubmitter(
@@ -104,11 +107,15 @@ func NewBatchSubmitter(
104107
return ch
105108
}
106109

107-
func (bs *BatchSubmitter) Initialize(host hostNode, bridgeInfo opchildtypes.BridgeInfo) error {
110+
func (bs *BatchSubmitter) Initialize(ctx context.Context, startHeight uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error {
111+
err := bs.node.Initialize(startHeight)
112+
if err != nil {
113+
return err
114+
}
108115
bs.host = host
109116
bs.bridgeInfo = bridgeInfo
110117

111-
res, err := bs.host.QueryBatchInfos()
118+
res, err := bs.host.QueryBatchInfos(ctx, bridgeInfo.BridgeId)
112119
if err != nil {
113120
return err
114121
}
@@ -123,7 +130,13 @@ func (bs *BatchSubmitter) Initialize(host hostNode, bridgeInfo opchildtypes.Brid
123130
bs.DequeueBatchInfo()
124131
}
125132

126-
bs.batchFile, err = os.OpenFile(bs.homePath+"/batch", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
133+
fileFlag := os.O_CREATE | os.O_RDWR
134+
// if the node has already processed blocks, append to the file
135+
if !bs.node.HeightInitialized() {
136+
fileFlag |= os.O_APPEND
137+
}
138+
139+
bs.batchFile, err = os.OpenFile(bs.homePath+"/batch", fileFlag, 0666)
127140
if err != nil {
128141
return err
129142
}

0 commit comments

Comments
 (0)