-
Notifications
You must be signed in to change notification settings - Fork 13
/
binary-block.go
144 lines (126 loc) · 2.75 KB
/
binary-block.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2014-2018 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"encoding/binary"
"fmt"
"io"
"os"
"github.com/bitmark-inc/bitmarkd/block"
"github.com/bitmark-inc/bitmarkd/blockheader"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/genesis"
"github.com/bitmark-inc/bitmarkd/storage"
)
// save blocks above genesis block to a file
// record format:
// big endian record length (n) 8 bytes
// block data n bytes
func saveBinaryBlocks(filename string) error {
fh, err := os.Create(filename)
if nil != err {
return err
}
defer fh.Close()
started := false
if blockheader.Height() <= genesis.BlockNumber {
return fmt.Errorf("nothing to save")
}
loop:
for n := genesis.BlockNumber + 1; true; n += 1 {
if n%100 == 0 {
fmt.Printf("%d", n)
} else {
fmt.Printf(".")
}
buffer, err := getBinaryBlock(n)
if nil != err {
if started && fault.ErrBlockNotFound == err {
break loop
}
return err
}
started = true
l := make([]byte, 8)
binary.BigEndian.PutUint64(l, uint64(len(buffer)))
err = writeRecord(fh, l)
if nil != err {
return err
}
err = writeRecord(fh, buffer)
if nil != err {
return err
}
}
return nil
}
func writeRecord(fh *os.File, buffer []byte) error {
l := len(buffer)
k, err := fh.Write(buffer)
if nil != err {
return err
}
if l != k {
return fmt.Errorf("only wrote: %d of %d", k, l)
}
return nil
}
// get a binary record for a block
func getBinaryBlock(number uint64) ([]byte, error) {
// fetch block and compute digest
n := make([]byte, 8)
binary.BigEndian.PutUint64(n, number)
packed := storage.Pool.Blocks.Get(n)
if nil == packed {
return nil, fault.ErrBlockNotFound
}
return packed, nil
}
// restore binary blocks from a file
// record format: (as save above)
func restoreBinaryBlocks(filename string) error {
fh, err := os.Open(filename)
if nil != err {
return err
}
defer fh.Close()
if blockheader.Height() > genesis.BlockNumber {
return fmt.Errorf("not overwriting existing data")
}
loop:
for {
n := blockheader.Height()
if n%100 == 0 {
fmt.Printf("%d", n)
} else {
fmt.Printf(".")
}
l := make([]byte, 8)
err := readRecord(fh, l)
if err == io.EOF {
break loop
} else if nil != err {
return err
}
size := binary.BigEndian.Uint64(l)
buffer := make([]byte, size)
err = readRecord(fh, buffer)
if nil != err {
return err
}
block.StoreIncoming(buffer, block.NoRescanVerified)
}
return nil
}
func readRecord(fh *os.File, buffer []byte) error {
l := len(buffer)
k, err := fh.Read(buffer)
if nil != err {
return err
}
if k != l {
return fmt.Errorf("only read: %d of %d", k, l)
}
return nil
}