1
1
package batch
2
2
3
3
import (
4
+ "compress/gzip"
4
5
"context"
5
6
"errors"
6
- "io"
7
7
"os"
8
8
"sync"
9
- "time"
10
9
11
10
"go.uber.org/zap"
12
11
13
12
opchildtypes "github.com/initia-labs/OPinit/x/opchild/types"
14
13
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
15
14
16
- dbtypes "github.com/initia-labs/opinit-bots/db/types"
17
15
executortypes "github.com/initia-labs/opinit-bots/executor/types"
18
16
"github.com/initia-labs/opinit-bots/node"
19
17
btypes "github.com/initia-labs/opinit-bots/node/broadcaster/types"
@@ -26,14 +24,6 @@ type hostNode interface {
26
24
QueryBatchInfos (context.Context , uint64 ) (* ophosttypes.QueryBatchInfosResponse , error )
27
25
}
28
26
29
- type compressionFunc interface {
30
- Write ([]byte ) (int , error )
31
- Reset (io.Writer )
32
- Close () error
33
- }
34
-
35
- var SubmissionKey = []byte ("submission_time" )
36
-
37
27
type BatchSubmitter struct {
38
28
version uint8
39
29
@@ -50,19 +40,17 @@ type BatchSubmitter struct {
50
40
51
41
opchildQueryClient opchildtypes.QueryClient
52
42
53
- batchInfoMu * sync.Mutex
54
- batchInfos []ophosttypes.BatchInfoWithOutput
55
- batchWriter compressionFunc
56
- batchFile * os.File
57
- batchHeader * executortypes.BatchHeader
43
+ batchInfoMu * sync.Mutex
44
+ batchInfos []ophosttypes.BatchInfoWithOutput
45
+ batchWriter * gzip. Writer
46
+ batchFile * os.File
47
+ localBatchInfo * executortypes.LocalBatchInfo
58
48
59
49
processedMsgs []btypes.ProcessedMsgs
60
50
61
51
chainID string
62
52
homePath string
63
53
64
- lastSubmissionTime time.Time
65
-
66
54
// status info
67
55
LastBatchEndBlockNumber uint64
68
56
}
@@ -98,7 +86,8 @@ func NewBatchSubmitterV0(
98
86
99
87
opchildQueryClient : opchildtypes .NewQueryClient (node .GetRPCClient ()),
100
88
101
- batchInfoMu : & sync.Mutex {},
89
+ batchInfoMu : & sync.Mutex {},
90
+ localBatchInfo : & executortypes.LocalBatchInfo {},
102
91
103
92
processedMsgs : make ([]btypes.ProcessedMsgs , 0 ),
104
93
homePath : homePath ,
@@ -131,17 +120,26 @@ func (bs *BatchSubmitter) Initialize(ctx context.Context, startHeight uint64, ho
131
120
}
132
121
133
122
fileFlag := os .O_CREATE | os .O_RDWR
134
- // if the node has already processed blocks, append to the file
135
- if ! bs .node .HeightInitialized () {
123
+ if bs .node .HeightInitialized () {
124
+ bs .localBatchInfo .Start = bs .node .GetHeight ()
125
+ bs .localBatchInfo .End = 0
126
+ bs .localBatchInfo .BatchFileSize = 0
127
+
128
+ err = bs .saveLocalBatchInfo ()
129
+ if err != nil {
130
+ return err
131
+ }
132
+ } else {
133
+ // if the node has already processed blocks, append to the file
136
134
fileFlag |= os .O_APPEND
137
135
}
138
136
139
137
bs .batchFile , err = os .OpenFile (bs .homePath + "/batch" , fileFlag , 0666 )
140
138
if err != nil {
141
139
return err
142
140
}
143
-
144
- err = bs . LoadSubmissionInfo ( )
141
+ // linux command gzip use level 6 as default
142
+ bs . batchWriter , err = gzip . NewWriterLevel ( bs . batchFile , 6 )
145
143
if err != nil {
146
144
return err
147
145
}
@@ -168,25 +166,6 @@ func (bs *BatchSubmitter) SetBridgeInfo(bridgeInfo opchildtypes.BridgeInfo) {
168
166
bs .bridgeInfo = bridgeInfo
169
167
}
170
168
171
- func (bs * BatchSubmitter ) LoadSubmissionInfo () error {
172
- val , err := bs .db .Get (SubmissionKey )
173
- if err != nil {
174
- if err == dbtypes .ErrNotFound {
175
- return nil
176
- }
177
- return err
178
- }
179
- bs .lastSubmissionTime = time .Unix (0 , dbtypes .ToInt64 (val ))
180
- return nil
181
- }
182
-
183
- func (bs * BatchSubmitter ) SubmissionInfoToRawKV (timestamp int64 ) types.RawKV {
184
- return types.RawKV {
185
- Key : bs .db .PrefixedKey (SubmissionKey ),
186
- Value : dbtypes .FromInt64 (timestamp ),
187
- }
188
- }
189
-
190
169
func (bs * BatchSubmitter ) ChainID () string {
191
170
return bs .chainID
192
171
}
0 commit comments