Skip to content

Commit f6e90c0

Browse files
committed
add comments
1 parent 9d3481b commit f6e90c0

File tree

8 files changed

+88
-31
lines changed

8 files changed

+88
-31
lines changed

executor/batch/batch.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
opchildtypes "github.com/initia-labs/OPinit/x/opchild/types"
1414
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
1515
executortypes "github.com/initia-labs/opinit-bots-go/executor/types"
16-
"github.com/initia-labs/opinit-bots-go/merkle"
1716
nodetypes "github.com/initia-labs/opinit-bots-go/node/types"
1817
"github.com/initia-labs/opinit-bots-go/types"
1918
"go.uber.org/zap"
@@ -44,7 +43,6 @@ type BatchSubmitter struct {
4443
node *node.Node
4544
host hostNode
4645
da executortypes.DANode
47-
mk *merkle.Merkle
4846

4947
bridgeInfo opchildtypes.BridgeInfo
5048

@@ -115,7 +113,7 @@ func (bs *BatchSubmitter) Initialize(host hostNode, da executortypes.DANode, bri
115113
if len(bs.batchInfos) == 1 || batchInfo.Output.L2BlockNumber >= bs.node.GetHeight() {
116114
break
117115
}
118-
bs.PopBatchInfo()
116+
bs.DequeueBatchInfo()
119117
}
120118
// TODO: set da and key that match the current batch info
121119
bs.da = da

executor/batch/handler.go

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"io"
1010
"time"
1111

12+
"github.com/pkg/errors"
13+
1214
sdk "github.com/cosmos/cosmos-sdk/types"
1315
executortypes "github.com/initia-labs/opinit-bots-go/executor/types"
1416
nodetypes "github.com/initia-labs/opinit-bots-go/node/types"
@@ -27,92 +29,115 @@ func (bs *BatchSubmitter) rawBlockHandler(args nodetypes.RawBlockArgs) error {
2729
pbb := new(cmtproto.Block)
2830
err := proto.Unmarshal(args.BlockBytes, pbb)
2931
if err != nil {
30-
return err
32+
return errors.Wrap(err, "failed to unmarshal block")
3133
}
3234

3335
err = bs.prepareBatch(args.BlockHeight, pbb.Header.Time)
3436
if err != nil {
35-
return err
37+
return errors.Wrap(err, "failed to prepare batch")
3638
}
39+
3740
err = bs.handleBatch(args.BlockBytes)
3841
if err != nil {
39-
return err
42+
return errors.Wrap(err, "failed to handle batch")
4043
}
4144

4245
err = bs.checkBatch(args.BlockHeight, pbb.Header.Time)
4346
if err != nil {
44-
return err
47+
return errors.Wrap(err, "failed to check batch")
4548
}
4649

50+
// store the processed state into db with batch operation
4751
batchKVs := make([]types.RawKV, 0)
4852
batchKVs = append(batchKVs, bs.node.SyncInfoToRawKV(args.BlockHeight))
49-
batchMsgkvs, err := bs.da.ProcessedMsgsToRawKV(bs.processedMsgs, false)
53+
batchMsgKVs, err := bs.da.ProcessedMsgsToRawKV(bs.processedMsgs, false)
5054
if err != nil {
51-
return err
55+
return errors.Wrap(err, "failed to convert processed messages to raw key value")
5256
}
53-
batchKVs = append(batchKVs, batchMsgkvs...)
54-
if len(batchMsgkvs) > 0 {
57+
batchKVs = append(batchKVs, batchMsgKVs...)
58+
if len(batchMsgKVs) > 0 {
5559
batchKVs = append(batchKVs, bs.SubmissionInfoToRawKV(pbb.Header.Time.UnixNano()))
5660
}
5761
err = bs.db.RawBatchSet(batchKVs...)
5862
if err != nil {
59-
return err
63+
return errors.Wrap(err, "failed to set raw batch")
6064
}
65+
66+
// broadcast processed messages
6167
for _, processedMsg := range bs.processedMsgs {
6268
bs.da.BroadcastMsgs(processedMsg)
6369
}
70+
71+
// clear processed messages
6472
bs.processedMsgs = bs.processedMsgs[:0]
73+
6574
return nil
6675
}
6776

6877
func (bs *BatchSubmitter) prepareBatch(blockHeight uint64, blockTime time.Time) error {
78+
79+
// check whether the requested block height is reached to the l2 block number of the next batch info.
6980
if nextBatchInfo := bs.NextBatchInfo(); nextBatchInfo != nil && nextBatchInfo.Output.L2BlockNumber < blockHeight {
81+
82+
// if the next batch info is reached, finalize the current batch and update the batch info.
7083
if bs.batchWriter != nil {
7184
err := bs.batchWriter.Close()
7285
if err != nil {
73-
return err
86+
return errors.Wrap(err, "failed to close batch writer")
7487
}
7588
}
7689
err := bs.batchFile.Truncate(0)
7790
if err != nil {
78-
return err
91+
return errors.Wrap(err, "failed to truncate batch file")
7992
}
8093
_, err = bs.batchFile.Seek(0, 0)
8194
if err != nil {
82-
return err
95+
return errors.Wrap(err, "failed to seek batch file")
8396
}
97+
98+
// save sync info
8499
err = bs.node.SaveSyncInfo(nextBatchInfo.Output.L2BlockNumber)
85100
if err != nil {
86-
return err
101+
return errors.Wrap(err, "failed to save sync info")
87102
}
103+
88104
// set last processed block height to l2 block number
89105
bs.node.SetSyncInfo(nextBatchInfo.Output.L2BlockNumber)
90-
bs.PopBatchInfo()
106+
bs.DequeueBatchInfo()
91107

92108
// error will restart block process from nextBatchInfo.Output.L2BlockNumber + 1
93109
return fmt.Errorf("batch info updated: reset from %d", nextBatchInfo.Output.L2BlockNumber)
94110
}
95111

96112
if bs.batchHeader != nil {
113+
// if the batch header end is not set, it means the batch is not finalized yet.
97114
if bs.batchHeader.End == 0 {
98115
return nil
99116
}
117+
100118
err := bs.finalizeBatch(blockHeight)
101119
if err != nil {
102-
return err
120+
return errors.Wrap(err, "failed to finalize batch")
103121
}
122+
123+
// update last submission time
104124
bs.lastSubmissionTime = blockTime
105125
}
106-
bs.batchHeader = &executortypes.BatchHeader{}
126+
127+
// reset batch header
107128
var err error
129+
bs.batchHeader = &executortypes.BatchHeader{}
130+
108131
// linux command gzip use level 6 as default
109132
bs.batchWriter, err = gzip.NewWriterLevel(bs.batchFile, 6)
110133
if err != nil {
111134
return err
112135
}
136+
113137
return nil
114138
}
115139

140+
// write block bytes to batch file
116141
func (bs *BatchSubmitter) handleBatch(blockBytes []byte) error {
117142
encodedBlockBytes := base64.StdEncoding.EncodeToString(blockBytes)
118143
_, err := bs.batchWriter.Write(append([]byte(encodedBlockBytes), ','))
@@ -122,23 +147,27 @@ func (bs *BatchSubmitter) handleBatch(blockBytes []byte) error {
122147
return nil
123148
}
124149

150+
// finalize batch and create batch messages
125151
func (bs *BatchSubmitter) finalizeBatch(blockHeight uint64) error {
152+
153+
// write last block's commit to batch file
126154
rawCommit, err := bs.node.QueryRawCommit(int64(blockHeight))
127155
if err != nil {
128-
return err
156+
return errors.Wrap(err, "failed to query raw commit")
129157
}
130158
encodedRawCommit := base64.StdEncoding.EncodeToString(rawCommit)
131159
_, err = bs.batchWriter.Write([]byte(encodedRawCommit))
132160
if err != nil {
133-
return err
161+
return errors.Wrap(err, "failed to write raw commit")
134162
}
135163
err = bs.batchWriter.Close()
136164
if err != nil {
137-
return err
165+
return errors.Wrap(err, "failed to close batch writer")
138166
}
139167

140168
batchBuffer := make([]byte, bs.batchCfg.MaxChunkSize)
141169
checksums := make([][]byte, 0)
170+
142171
// room for batch header
143172
bs.processedMsgs = append(bs.processedMsgs, nodetypes.ProcessedMsgs{
144173
Timestamp: time.Now().UnixNano(),
@@ -152,7 +181,9 @@ func (bs *BatchSubmitter) finalizeBatch(blockHeight uint64) error {
152181
} else if readLength == 0 {
153182
break
154183
}
155-
batchBuffer = batchBuffer[:readLength]
184+
185+
// trim the buffer to the actual read length
186+
batchBuffer := batchBuffer[:readLength]
156187
msg, err := bs.createBatchMsg(batchBuffer)
157188
if err != nil {
158189
return err
@@ -168,6 +199,8 @@ func (bs *BatchSubmitter) finalizeBatch(blockHeight uint64) error {
168199
break
169200
}
170201
}
202+
203+
// update batch header
171204
bs.batchHeader.Chunks = checksums
172205
headerBytes, err := json.Marshal(bs.batchHeader)
173206
if err != nil {
@@ -178,6 +211,8 @@ func (bs *BatchSubmitter) finalizeBatch(blockHeight uint64) error {
178211
return err
179212
}
180213
bs.processedMsgs[0].Msgs = []sdk.Msg{msg}
214+
215+
// reset batch file
181216
err = bs.batchFile.Truncate(0)
182217
if err != nil {
183218
return err
@@ -186,23 +221,32 @@ func (bs *BatchSubmitter) finalizeBatch(blockHeight uint64) error {
186221
if err != nil {
187222
return err
188223
}
224+
189225
return nil
190226
}
191227

192228
func (bs *BatchSubmitter) checkBatch(blockHeight uint64, blockTime time.Time) error {
193229
info, err := bs.batchFile.Stat()
194230
if err != nil {
195-
return err
231+
return errors.Wrap(err, "failed to get batch file stat")
196232
}
197233

234+
// if the block time is after the last submission time + submission interval * 2/3
235+
// or the block time is after the last submission time + max submission time
236+
// or the batch file size is greater than (max chunks - 1) * max chunk size
237+
// then finalize the batch
198238
if blockTime.After(bs.lastSubmissionTime.Add(bs.bridgeInfo.BridgeConfig.SubmissionInterval*2/3)) ||
199239
blockTime.After(bs.lastSubmissionTime.Add(time.Duration(bs.batchCfg.MaxSubmissionTime)*time.Second)) ||
200240
info.Size() > (bs.batchCfg.MaxChunks-1)*bs.batchCfg.MaxChunkSize {
241+
242+
// finalize the batch
201243
bs.batchHeader.End = blockHeight
202244
}
245+
203246
return nil
204247
}
205248

249+
// TODO: support celestia
206250
func (bs *BatchSubmitter) createBatchMsg(batchBytes []byte) (sdk.Msg, error) {
207251
submitter, err := bs.da.GetAddressStr()
208252
if err != nil {
@@ -216,6 +260,7 @@ func (bs *BatchSubmitter) createBatchMsg(batchBytes []byte) (sdk.Msg, error) {
216260
), nil
217261
}
218262

263+
// UpdateBatchInfo appends the batch info with the given chain, submitter, output index, and l2 block number
219264
func (bs *BatchSubmitter) UpdateBatchInfo(chain string, submitter string, outputIndex uint64, l2BlockNumber uint64) {
220265
bs.batchInfoMu.Lock()
221266
defer bs.batchInfoMu.Unlock()
@@ -231,13 +276,15 @@ func (bs *BatchSubmitter) UpdateBatchInfo(chain string, submitter string, output
231276
})
232277
}
233278

279+
// BatchInfo returns the current batch info
234280
func (bs *BatchSubmitter) BatchInfo() *ophosttypes.BatchInfoWithOutput {
235281
bs.batchInfoMu.Lock()
236282
defer bs.batchInfoMu.Unlock()
237283

238284
return &bs.batchInfos[0]
239285
}
240286

287+
// NextBatchInfo returns the next batch info in the queue
241288
func (bs *BatchSubmitter) NextBatchInfo() *ophosttypes.BatchInfoWithOutput {
242289
bs.batchInfoMu.Lock()
243290
defer bs.batchInfoMu.Unlock()
@@ -247,7 +294,8 @@ func (bs *BatchSubmitter) NextBatchInfo() *ophosttypes.BatchInfoWithOutput {
247294
return &bs.batchInfos[1]
248295
}
249296

250-
func (bs *BatchSubmitter) PopBatchInfo() {
297+
// DequeueBatchInfo removes the first batch info from the queue
298+
func (bs *BatchSubmitter) DequeueBatchInfo() {
251299
bs.batchInfoMu.Lock()
252300
defer bs.batchInfoMu.Unlock()
253301

executor/executor.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server, logg
5353
db.WithPrefix([]byte(executortypes.ChildNodeName)),
5454
logger.Named(executortypes.ChildNodeName), cdc, txConfig,
5555
),
56-
batch: batch.NewBatchSubmitter(cfg.Version, cfg.DANodeConfig(), cfg.BatchConfig(), db.WithPrefix([]byte(executortypes.BatchNodeName)), logger.Named(executortypes.BatchNodeName), cdc, txConfig, homePath),
56+
batch: batch.NewBatchSubmitter(
57+
cfg.Version, cfg.DANodeConfig(), cfg.BatchConfig(),
58+
db.WithPrefix([]byte(executortypes.BatchNodeName)),
59+
logger.Named(executortypes.BatchNodeName), cdc, txConfig, homePath,
60+
),
5761

5862
cfg: cfg,
5963
db: db,

executor/types/batch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type DANode interface {
99
GetAddressStr() (string, error)
1010
HasKey() bool
1111
BroadcastMsgs(nodetypes.ProcessedMsgs)
12-
ProcessedMsgsToRawKV([]nodetypes.ProcessedMsgs, bool) ([]types.RawKV, error)
12+
ProcessedMsgsToRawKV(processedMsgs []nodetypes.ProcessedMsgs, delete bool) ([]types.RawKV, error)
1313
}
1414

1515
type BatchHeader struct {

executor/types/config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ type Config struct {
4646
// RelayOracle is the flag to enable the oracle relay feature.
4747
RelayOracle bool `json:"relay_oracle"`
4848

49-
MaxChunks int64 `json:"max_chunks"`
50-
MaxChunkSize int64 `json:"max_chunk_size"`
49+
// MaxChunks is the maximum number of chunks in a batch.
50+
MaxChunks int64 `json:"max_chunks"`
51+
// MaxChunkSize is the maximum size of a chunk in a batch.
52+
MaxChunkSize int64 `json:"max_chunk_size"`
53+
// MaxSubmissionTime is the maximum time to submit a batch.
5154
MaxSubmissionTime int64 `json:"max_submission_time"` // seconds
5255
}
5356

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ require (
178178
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a // indirect
179179
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
180180
github.com/petermattis/goid v0.0.0-20231207134359-e60b3f734c67 // indirect
181-
github.com/pkg/errors v0.9.1 // indirect
181+
github.com/pkg/errors v0.9.1
182182
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
183183
github.com/prometheus/client_golang v1.19.0 // indirect
184184
github.com/prometheus/client_model v0.6.1 // indirect

node/query.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,14 @@ func GetQueryContext(height uint64) (context.Context, context.CancelFunc) {
153153
return ctx, cancel
154154
}
155155

156+
// QueryRawCommit queries the raw commit at a given height.
156157
func (n *Node) QueryRawCommit(height int64) ([]byte, error) {
157158
ctx, cancel := GetQueryContext(uint64(height))
158159
defer cancel()
159160
return n.RawCommit(ctx, &height)
160161
}
161162

163+
// QueryBlockBulk queries blocks in bulk.
162164
func (n *Node) QueryBlockBulk(start uint64, end uint64) ([][]byte, error) {
163165
ctx, cancel := GetQueryContext(0)
164166
defer cancel()

node/tx.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
"time"
1111

12+
"github.com/pkg/errors"
13+
1214
sdkerrors "cosmossdk.io/errors"
1315
abci "github.com/cometbft/cometbft/abci/types"
1416
comettypes "github.com/cometbft/cometbft/types"
@@ -55,7 +57,7 @@ func (n *Node) txBroadcastLooper(ctx context.Context) error {
5557
}
5658

5759
if err != nil {
58-
panic(err)
60+
return errors.Wrap(err, "failed to handle processed msgs")
5961
}
6062
}
6163
}

0 commit comments

Comments
 (0)