mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 09:42:22 +00:00
Merge pull request #488 from nspcc-dev/dbdump_466
cli: compatible with NGD reading/writing blockchain dump
This commit is contained in:
commit
85d0bc4544
1 changed files with 25 additions and 8 deletions
|
@ -174,7 +174,11 @@ func dumpDB(ctx *cli.Context) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.NewExitError(fmt.Errorf("failed to get block %d: %s", i, err), 1)
|
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 {
|
if writer.Err != nil {
|
||||||
return cli.NewExitError(err, 1)
|
return cli.NewExitError(err, 1)
|
||||||
}
|
}
|
||||||
|
@ -221,19 +225,20 @@ func restoreDB(ctx *cli.Context) error {
|
||||||
}
|
}
|
||||||
i := uint32(0)
|
i := uint32(0)
|
||||||
for ; i < skip; i++ {
|
for ; i < skip; i++ {
|
||||||
b := &core.Block{}
|
_, err := readBlock(reader)
|
||||||
b.DecodeBinary(reader)
|
if err != nil {
|
||||||
if reader.Err != nil {
|
|
||||||
return cli.NewExitError(err, 1)
|
return cli.NewExitError(err, 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for ; i < skip+count; i++ {
|
for ; i < skip+count; i++ {
|
||||||
b := &core.Block{}
|
bytes, err := readBlock(reader)
|
||||||
b.DecodeBinary(reader)
|
block := &core.Block{}
|
||||||
if reader.Err != nil {
|
newReader := io.NewBinReaderFromBuf(bytes)
|
||||||
|
block.DecodeBinary(newReader)
|
||||||
|
if err != nil {
|
||||||
return cli.NewExitError(err, 1)
|
return cli.NewExitError(err, 1)
|
||||||
}
|
}
|
||||||
err := chain.AddBlock(b)
|
err = chain.AddBlock(block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.NewExitError(fmt.Errorf("failed to add block %d: %s", i, err), 1)
|
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
|
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 {
|
func startServer(ctx *cli.Context) error {
|
||||||
cfg, err := getConfigFromContext(ctx)
|
cfg, err := getConfigFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue