-
Notifications
You must be signed in to change notification settings - Fork 77
/
dump.go
85 lines (77 loc) · 1.94 KB
/
dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package chaindump
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util"
)
// DumperRestorer is an interface to get/add blocks from/to.
type DumperRestorer interface {
AddBlock(block *block.Block) error
GetBlock(hash util.Uint256) (*block.Block, error)
GetConfig() config.ProtocolConfiguration
GetHeaderHash(int) util.Uint256
}
// Dump writes count blocks from start to the provided writer.
// Note: header needs to be written separately by a client.
func Dump(bc DumperRestorer, w *io.BinWriter, start, count uint32) error {
for i := start; i < start+count; i++ {
bh := bc.GetHeaderHash(int(i))
b, err := bc.GetBlock(bh)
if err != nil {
return err
}
buf := io.NewBufBinWriter()
b.EncodeBinary(buf.BinWriter)
bytes := buf.Bytes()
w.WriteU32LE(uint32(len(bytes)))
w.WriteBytes(bytes)
if w.Err != nil {
return w.Err
}
}
return nil
}
// Restore restores blocks from the provided reader.
// f is called after addition of every block.
func Restore(bc DumperRestorer, r *io.BinReader, skip, count uint32, f func(b *block.Block) error) error {
readBlock := func(r *io.BinReader) ([]byte, error) {
var size = r.ReadU32LE()
buf := make([]byte, size)
r.ReadBytes(buf)
return buf, r.Err
}
i := uint32(0)
for ; i < skip; i++ {
_, err := readBlock(r)
if err != nil {
return err
}
}
stateRootInHeader := bc.GetConfig().StateRootInHeader
for ; i < skip+count; i++ {
buf, err := readBlock(r)
if err != nil {
return err
}
b := block.New(stateRootInHeader)
r := io.NewBinReaderFromBuf(buf)
b.DecodeBinary(r)
if r.Err != nil {
return r.Err
}
if b.Index != 0 || i != 0 || skip != 0 {
err = bc.AddBlock(b)
if err != nil {
return fmt.Errorf("failed to add block %d: %w", i, err)
}
}
if f != nil {
if err := f(b); err != nil {
return err
}
}
}
return nil
}