/
chunk.go
201 lines (174 loc) · 4.73 KB
/
chunk.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package checkpoint
import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"github.com/golang/snappy"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs"
db "github.com/oasisprotocol/oasis-core/go/storage/mkvs/db/api"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/node"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/syncer"
)
func createChunk(
ctx context.Context,
tree mkvs.Tree,
root node.Root,
offset node.Key,
chunkSize uint64,
w io.Writer,
) (
chunkHash hash.Hash,
nextOffset node.Key,
err error,
) {
it := tree.NewIterator(ctx, mkvs.WithProof(root.Hash))
defer it.Close()
// We build the chunk until the proof becomes too large or we have reached the end.
for it.Seek(offset); it.Valid() && it.GetProofBuilder().Size() < chunkSize; it.Next() {
// Check if context got cancelled while iterating to abort early.
if ctx.Err() != nil {
err = ctx.Err()
return
}
}
if it.Err() != nil {
err = fmt.Errorf("chunk: failed to iterate: %w", it.Err())
return
}
// Build our chunk.
proof, err := it.GetProof()
if err != nil {
err = fmt.Errorf("chunk: failed to build proof: %w", err)
return
}
// Determine the next offset (not included in proof).
it.Next()
nextOffset = it.Key()
hb := hash.NewBuilder()
sw := snappy.NewBufferedWriter(io.MultiWriter(w, hb))
enc := cbor.NewEncoder(sw)
for _, entry := range proof.Entries {
if err = enc.Encode(entry); err != nil {
err = fmt.Errorf("chunk: failed to encode chunk part: %w", err)
return
}
}
if err = sw.Close(); err != nil {
err = fmt.Errorf("chunk: failed to close chunk: %w", err)
return
}
chunkHash = hb.Build()
return
}
func restoreChunk(ctx context.Context, ndb db.NodeDB, chunk *ChunkMetadata, r io.Reader) error {
hb := hash.NewBuilder()
tr := io.TeeReader(r, hb)
sr := snappy.NewReader(tr)
dec := cbor.NewDecoder(sr)
// Reconstruct the proof.
var decodeErr error
var p syncer.Proof
for {
if ctx.Err() != nil {
return ctx.Err()
}
var entry []byte
if err := dec.Decode(&entry); err != nil {
if errors.Is(err, io.EOF) {
break
}
decodeErr = fmt.Errorf("failed to decode chunk: %w", err)
// Read everything until EOF so we can verify the overall chunk integrity.
_, _ = io.Copy(ioutil.Discard, tr)
break
}
p.Entries = append(p.Entries, entry)
}
p.UntrustedRoot = chunk.Root.Hash
// Verify overall chunk integrity.
chunkHash := hb.Build()
if !chunk.Digest.Equal(&chunkHash) {
return fmt.Errorf("%w: digest incorrect (expected: %s got: %s)",
ErrChunkCorrupted,
chunk.Digest,
chunkHash,
)
}
// Treat decode errors after integrity verification as proof verification failures.
if decodeErr != nil {
return fmt.Errorf("%w: %s", ErrChunkProofVerificationFailed, decodeErr.Error())
}
// Verify the proof.
var pv syncer.ProofVerifier
ptr, err := pv.VerifyProof(ctx, chunk.Root.Hash, &p)
if err != nil {
return fmt.Errorf("%w: %s", ErrChunkProofVerificationFailed, err.Error())
}
// Import chunk into the node database.
emptyRoot := node.Root{
Namespace: chunk.Root.Namespace,
Version: chunk.Root.Version,
Type: chunk.Root.Type,
}
emptyRoot.Hash.Empty()
batch, err := ndb.NewBatch(emptyRoot, chunk.Root.Version, true)
if err != nil {
return fmt.Errorf("chunk: failed to create batch: %w", err)
}
defer batch.Reset()
subtree := batch.MaybeStartSubtree(nil, 0, ptr)
if err = doRestoreChunk(ctx, batch, subtree, 0, ptr); err != nil {
return fmt.Errorf("chunk: node import failed: %w", err)
}
if err = subtree.Commit(); err != nil {
return fmt.Errorf("chunk: node import failed: %w", err)
}
if err = batch.Commit(chunk.Root); err != nil {
return fmt.Errorf("chunk: node import failed: %w", err)
}
return nil
}
func doRestoreChunk(
ctx context.Context,
batch db.Batch,
subtree db.Subtree,
depth node.Depth,
ptr *node.Pointer,
) (err error) {
if ptr == nil {
return
}
switch n := ptr.Node.(type) {
case nil:
case *node.InternalNode:
// Commit internal leaf (considered to be on the same depth as the internal node).
if err = doRestoreChunk(ctx, batch, subtree, depth, n.LeafNode); err != nil {
return
}
for _, subNode := range []*node.Pointer{n.Left, n.Right} {
newSubtree := batch.MaybeStartSubtree(subtree, depth+1, subNode)
if err = doRestoreChunk(ctx, batch, newSubtree, depth+1, subNode); err != nil {
return
}
if newSubtree != subtree {
if err = newSubtree.Commit(); err != nil {
return
}
}
}
// Store the node.
if err = subtree.PutNode(depth, ptr); err != nil {
return
}
case *node.LeafNode:
// Leaf node -- store the node.
if err = subtree.PutNode(depth, ptr); err != nil {
return
}
}
return
}