Skip to content

Commit fdb8edf

Browse files
authored
Merge pull request #3427 from Arachnid/gzipdump
cmd/utils, eth: Add gzip support for chain dump and restore
2 parents 157a4bd + 9ba9fe8 commit fdb8edf

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

cmd/utils/cmd.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
package utils
1919

2020
import (
21+
"compress/gzip"
2122
"fmt"
2223
"io"
2324
"os"
2425
"os/signal"
2526
"regexp"
2627
"runtime"
28+
"strings"
2729

2830
"github.com/ethereum/go-ethereum/common"
2931
"github.com/ethereum/go-ethereum/core"
@@ -133,7 +135,15 @@ func ImportChain(chain *core.BlockChain, fn string) error {
133135
return err
134136
}
135137
defer fh.Close()
136-
stream := rlp.NewStream(fh, 0)
138+
139+
var reader io.Reader = fh
140+
if strings.HasSuffix(fn, ".gz") {
141+
if reader, err = gzip.NewReader(reader); err != nil {
142+
return err
143+
}
144+
}
145+
146+
stream := rlp.NewStream(reader, 0)
137147

138148
// Run actual the import.
139149
blocks := make(types.Blocks, importBatchSize)
@@ -195,10 +205,18 @@ func ExportChain(blockchain *core.BlockChain, fn string) error {
195205
return err
196206
}
197207
defer fh.Close()
198-
if err := blockchain.Export(fh); err != nil {
208+
209+
var writer io.Writer = fh
210+
if strings.HasSuffix(fn, ".gz") {
211+
writer = gzip.NewWriter(writer)
212+
defer writer.(*gzip.Writer).Close()
213+
}
214+
215+
if err := blockchain.Export(writer); err != nil {
199216
return err
200217
}
201218
glog.Infoln("Exported blockchain to ", fn)
219+
202220
return nil
203221
}
204222

@@ -210,7 +228,14 @@ func ExportAppendChain(blockchain *core.BlockChain, fn string, first uint64, las
210228
return err
211229
}
212230
defer fh.Close()
213-
if err := blockchain.ExportN(fh, first, last); err != nil {
231+
232+
var writer io.Writer = fh
233+
if strings.HasSuffix(fn, ".gz") {
234+
writer = gzip.NewWriter(writer)
235+
defer writer.(*gzip.Writer).Close()
236+
}
237+
238+
if err := blockchain.ExportN(writer, first, last); err != nil {
214239
return err
215240
}
216241
glog.Infoln("Exported blockchain to ", fn)

eth/api.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ package eth
1818

1919
import (
2020
"bytes"
21+
"compress/gzip"
2122
"errors"
2223
"fmt"
2324
"io"
2425
"io/ioutil"
2526
"math/big"
2627
"os"
2728
"runtime"
29+
"strings"
2830
"time"
2931

3032
"github.com/ethereum/ethash"
@@ -217,8 +219,14 @@ func (api *PrivateAdminAPI) ExportChain(file string) (bool, error) {
217219
}
218220
defer out.Close()
219221

222+
var writer io.Writer = out
223+
if strings.HasSuffix(file, ".gz") {
224+
writer = gzip.NewWriter(writer)
225+
defer writer.(*gzip.Writer).Close()
226+
}
227+
220228
// Export the blockchain
221-
if err := api.eth.BlockChain().Export(out); err != nil {
229+
if err := api.eth.BlockChain().Export(writer); err != nil {
222230
return false, err
223231
}
224232
return true, nil
@@ -243,8 +251,15 @@ func (api *PrivateAdminAPI) ImportChain(file string) (bool, error) {
243251
}
244252
defer in.Close()
245253

254+
var reader io.Reader = in
255+
if strings.HasSuffix(file, ".gz") {
256+
if reader, err = gzip.NewReader(reader); err != nil {
257+
return false, err
258+
}
259+
}
260+
246261
// Run actual the import in pre-configured batches
247-
stream := rlp.NewStream(in, 0)
262+
stream := rlp.NewStream(reader, 0)
248263

249264
blocks, index := make([]*types.Block, 0, 2500), 0
250265
for batch := 0; ; batch++ {

0 commit comments

Comments
 (0)