core: use package rlp to encode blocks

This also changes the chain export format so there is no
enclosing list around the blocks, which enables streaming export.
This commit is contained in:
Felix Lange
2015-03-18 13:36:48 +01:00
parent c298148a7f
commit b5b83db450
2 changed files with 34 additions and 30 deletions

View File

@ -23,14 +23,15 @@ package utils
import (
"fmt"
"io"
"os"
"os/signal"
"regexp"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/rlp"
)
@ -152,29 +153,35 @@ func ImportChain(chainmgr *core.ChainManager, fn string) error {
}
defer fh.Close()
var blocks types.Blocks
if err := rlp.Decode(fh, &blocks); err != nil {
return err
}
chainmgr.Reset()
if err := chainmgr.InsertChain(blocks); err != nil {
return err
stream := rlp.NewStream(fh)
var i int
for ; ; i++ {
var b types.Block
err := stream.Decode(&b)
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("at block %d: %v", i, err)
}
if err := chainmgr.InsertChain(types.Blocks{b}); err != nil {
return fmt.Errorf("invalid block %d: %v", i, err)
}
}
fmt.Printf("imported %d blocks\n", len(blocks))
fmt.Printf("imported %d blocks\n", i)
return nil
}
func ExportChain(chainmgr *core.ChainManager, fn string) error {
fmt.Printf("exporting blockchain '%s'\n", fn)
data := chainmgr.Export()
if err := common.WriteFile(fn, data); err != nil {
fh, err := os.OpenFile(fn, os.O_WRONLY|os.O_TRUNC, os.ModePerm)
if err != nil {
return err
}
defer fh.Close()
if err := chainmgr.Export(fh); err != nil {
return err
}
fmt.Printf("exported blockchain\n")
return nil
}