cmd/utils, eth: Add gzip support for chain dump and restore

This commit is contained in:
Nick Johnson 2016-12-12 15:08:23 +00:00
parent ee445a2ba4
commit 9ba9fe818d
2 changed files with 45 additions and 5 deletions

View File

@ -18,12 +18,14 @@
package utils package utils
import ( import (
"compress/gzip"
"fmt" "fmt"
"io" "io"
"os" "os"
"os/signal" "os/signal"
"regexp" "regexp"
"runtime" "runtime"
"strings"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
@ -133,7 +135,15 @@ func ImportChain(chain *core.BlockChain, fn string) error {
return err return err
} }
defer fh.Close() defer fh.Close()
stream := rlp.NewStream(fh, 0)
var reader io.Reader = fh
if strings.HasSuffix(fn, ".gz") {
if reader, err = gzip.NewReader(reader); err != nil {
return err
}
}
stream := rlp.NewStream(reader, 0)
// Run actual the import. // Run actual the import.
blocks := make(types.Blocks, importBatchSize) blocks := make(types.Blocks, importBatchSize)
@ -195,10 +205,18 @@ func ExportChain(blockchain *core.BlockChain, fn string) error {
return err return err
} }
defer fh.Close() defer fh.Close()
if err := blockchain.Export(fh); err != nil {
var writer io.Writer = fh
if strings.HasSuffix(fn, ".gz") {
writer = gzip.NewWriter(writer)
defer writer.(*gzip.Writer).Close()
}
if err := blockchain.Export(writer); err != nil {
return err return err
} }
glog.Infoln("Exported blockchain to ", fn) glog.Infoln("Exported blockchain to ", fn)
return nil return nil
} }
@ -210,7 +228,14 @@ func ExportAppendChain(blockchain *core.BlockChain, fn string, first uint64, las
return err return err
} }
defer fh.Close() defer fh.Close()
if err := blockchain.ExportN(fh, first, last); err != nil {
var writer io.Writer = fh
if strings.HasSuffix(fn, ".gz") {
writer = gzip.NewWriter(writer)
defer writer.(*gzip.Writer).Close()
}
if err := blockchain.ExportN(writer, first, last); err != nil {
return err return err
} }
glog.Infoln("Exported blockchain to ", fn) glog.Infoln("Exported blockchain to ", fn)

View File

@ -18,6 +18,7 @@ package eth
import ( import (
"bytes" "bytes"
"compress/gzip"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -25,6 +26,7 @@ import (
"math/big" "math/big"
"os" "os"
"runtime" "runtime"
"strings"
"time" "time"
"github.com/ethereum/ethash" "github.com/ethereum/ethash"
@ -217,8 +219,14 @@ func (api *PrivateAdminAPI) ExportChain(file string) (bool, error) {
} }
defer out.Close() defer out.Close()
var writer io.Writer = out
if strings.HasSuffix(file, ".gz") {
writer = gzip.NewWriter(writer)
defer writer.(*gzip.Writer).Close()
}
// Export the blockchain // Export the blockchain
if err := api.eth.BlockChain().Export(out); err != nil { if err := api.eth.BlockChain().Export(writer); err != nil {
return false, err return false, err
} }
return true, nil return true, nil
@ -243,8 +251,15 @@ func (api *PrivateAdminAPI) ImportChain(file string) (bool, error) {
} }
defer in.Close() defer in.Close()
var reader io.Reader = in
if strings.HasSuffix(file, ".gz") {
if reader, err = gzip.NewReader(reader); err != nil {
return false, err
}
}
// Run actual the import in pre-configured batches // Run actual the import in pre-configured batches
stream := rlp.NewStream(in, 0) stream := rlp.NewStream(reader, 0)
blocks, index := make([]*types.Block, 0, 2500), 0 blocks, index := make([]*types.Block, 0, 2500), 0
for batch := 0; ; batch++ { for batch := 0; ; batch++ {