From b5b83db450974f70f4bc25f280cc6ec9193f8e19 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 18 Mar 2015 13:36:48 +0100 Subject: 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. --- cmd/utils/cmd.go | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'cmd/utils') diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 74fd5334e..66e89abef 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -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 } -- cgit From 064279c0ec2f048cbdd965c095ea332bb8666f94 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 18 Mar 2015 14:04:19 +0100 Subject: cmd/ethereum, cmd/utils: partial fix for chain import --- cmd/utils/cmd.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'cmd/utils') diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 66e89abef..feea29d64 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -158,13 +158,12 @@ func ImportChain(chainmgr *core.ChainManager, fn string) error { var i int for ; ; i++ { var b types.Block - err := stream.Decode(&b) - if err == io.EOF { + if err := stream.Decode(&b); 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 { + if err := chainmgr.InsertChain(types.Blocks{&b}); err != nil { return fmt.Errorf("invalid block %d: %v", i, err) } } @@ -174,7 +173,7 @@ func ImportChain(chainmgr *core.ChainManager, fn string) error { func ExportChain(chainmgr *core.ChainManager, fn string) error { fmt.Printf("exporting blockchain '%s'\n", fn) - fh, err := os.OpenFile(fn, os.O_WRONLY|os.O_TRUNC, os.ModePerm) + fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) if err != nil { return err } -- cgit