Skip to content

Commit 8e0bfbc

Browse files
authored
Merge pull request #837 from OffchainLabs/fix-download-init-div-zero
Fix division by zero in downloadInit and improve archive extraction
2 parents 3a2b6be + a3110d3 commit 8e0bfbc

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

cmd/nitro/nitro.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,17 @@ func downloadInit(ctx context.Context, initConfig *InitConfig) (string, error) {
139139
if err != nil {
140140
panic(err)
141141
}
142-
resp := grabclient.Do(req)
142+
resp := grabclient.Do(req.WithContext(ctx))
143143
firstPrintTime := time.Now().Add(time.Second * 2)
144144
updateLoop:
145145
for {
146146
select {
147147
case <-printTicker.C:
148148
if time.Now().After(firstPrintTime) {
149149
bps := resp.BytesPerSecond()
150+
if bps == 0 {
151+
bps = 1 // avoid division by zero
152+
}
150153
done := resp.BytesComplete()
151154
total := resp.Size()
152155
timeRemaining := (time.Second * time.Duration(total-done)) / time.Duration(bps)
@@ -163,6 +166,7 @@ func downloadInit(ctx context.Context, initConfig *InitConfig) (string, error) {
163166
fmt.Printf("\033[2K\r attempt %d failed: %v", attempt, err)
164167
break updateLoop
165168
}
169+
fmt.Printf("\n")
166170
log.Info("Download done", "filename", resp.Filename, "duration", resp.Duration())
167171
fmt.Println()
168172
return resp.Filename, nil
@@ -230,6 +234,11 @@ func openInitializeChainDb(ctx context.Context, stack *node.Node, initConfig *In
230234
if err != nil {
231235
return nil, nil, fmt.Errorf("couln't open init '%v' archive: %w", initFile, err)
232236
}
237+
stat, err := reader.Stat()
238+
if err != nil {
239+
return nil, nil, err
240+
}
241+
log.Info("extracting downloaded init archive", "size", fmt.Sprintf("%dMB", stat.Size()/1024/1024))
233242
err = extract.Archive(context.Background(), reader, stack.InstanceDir(), nil)
234243
if err != nil {
235244
return nil, nil, fmt.Errorf("couln't extract init archive '%v' err:%w", initFile, err)

0 commit comments

Comments
 (0)