From 7b6756cb7eb593adfee2bb308c04a7494d2d3e5c Mon Sep 17 00:00:00 2001 From: Vsevolod Brekelov Date: Mon, 11 Nov 2019 13:15:34 +0300 Subject: [PATCH] cli: compatible with NGD reading/writing blockchain dump --- cli/server/server.go | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/cli/server/server.go b/cli/server/server.go index ba469b319..b7290c4b0 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -174,7 +174,11 @@ func dumpDB(ctx *cli.Context) error { if err != nil { return cli.NewExitError(fmt.Errorf("failed to get block %d: %s", i, err), 1) } - b.EncodeBinary(writer) + buf := io.NewBufBinWriter() + b.EncodeBinary(buf.BinWriter) + bytes := buf.Bytes() + writer.WriteLE(uint32(len(bytes))) + writer.WriteLE(bytes) if writer.Err != nil { return cli.NewExitError(err, 1) } @@ -221,19 +225,20 @@ func restoreDB(ctx *cli.Context) error { } i := uint32(0) for ; i < skip; i++ { - b := &core.Block{} - b.DecodeBinary(reader) - if reader.Err != nil { + _, err := readBlock(reader) + if err != nil { return cli.NewExitError(err, 1) } } for ; i < skip+count; i++ { - b := &core.Block{} - b.DecodeBinary(reader) - if reader.Err != nil { + bytes, err := readBlock(reader) + block := &core.Block{} + newReader := io.NewBinReaderFromBuf(bytes) + block.DecodeBinary(newReader) + if err != nil { return cli.NewExitError(err, 1) } - err := chain.AddBlock(b) + err = chain.AddBlock(block) if err != nil { return cli.NewExitError(fmt.Errorf("failed to add block %d: %s", i, err), 1) } @@ -243,6 +248,18 @@ func restoreDB(ctx *cli.Context) error { return nil } +// readBlock performs reading of block size and then bytes with the length equal to that size. +func readBlock(reader *io.BinReader) ([]byte, error) { + var size uint32 + reader.ReadLE(&size) + bytes := make([]byte, size) + reader.ReadLE(bytes) + if reader.Err != nil { + return nil, reader.Err + } + return bytes, nil +} + func startServer(ctx *cli.Context) error { cfg, err := getConfigFromContext(ctx) if err != nil {