-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathsync_dag.go
More file actions
157 lines (132 loc) · 5.04 KB
/
Copy pathsync_dag.go
File metadata and controls
157 lines (132 loc) · 5.04 KB
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
// Copyright 2025 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package p2p
import (
"context"
"time"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/sourcenetwork/corekv/blockstore"
"github.com/sourcenetwork/immutable"
"github.com/sourcenetwork/defradb/errors"
coreblock "github.com/sourcenetwork/defradb/internal/core/block"
"github.com/sourcenetwork/defradb/internal/datastore"
"github.com/sourcenetwork/defradb/internal/encryption"
)
// blockSyncTimeoutCtxKey is the context key under which a per-request per-block sync timeout
// override is carried down to loadBlockLinks.
type blockSyncTimeoutCtxKey struct{}
// WithBlockSyncTimeout returns a context carrying a per-block sync timeout override that takes
// precedence over the node default for the DAG sync it drives.
func WithBlockSyncTimeout(ctx context.Context, timeout time.Duration) context.Context {
return context.WithValue(ctx, blockSyncTimeoutCtxKey{}, timeout)
}
// blockSyncTimeout returns the per-block fetch timeout to use: the per-request override carried
// on ctx if one was set (and positive), otherwise the node default.
func (p *P2P) blockSyncTimeout(ctx context.Context) time.Duration {
if v, ok := ctx.Value(blockSyncTimeoutCtxKey{}).(time.Duration); ok && v > 0 {
return v
}
return p.syncBlockLinkTimeout
}
func makeLinkSystem(blockService blockstore.IPLDStore) linking.LinkSystem {
linkSys := cidlink.DefaultLinkSystem()
linkSys.SetWriteStorage(blockService)
linkSys.SetReadStorage(blockService)
linkSys.TrustedStorage = true
return linkSys
}
// syncDAG synchronizes the DAG starting with the given block
// using the blockservice to fetch remote blocks.
//
// This process walks the entire DAG until the issue below is resolved.
// https://github.com/sourcenetwork/defradb/issues/2722
func (p *P2P) syncDAG(ctx context.Context, block *coreblock.Block) error {
sessionCtx, cancelSession := context.WithCancel(ctx)
defer cancelSession()
// use a session to make remote fetches more efficient
sessionCtx = p.host.ContextWithSession(sessionCtx)
linkSystem := makeLinkSystem(p.host.IPLDStore())
// Store the block in the DAG store
_, err := linkSystem.Store(linking.LinkContext{Ctx: sessionCtx}, coreblock.GetLinkPrototype(), block.GenerateNode())
if err != nil {
return NewErrStoreBlockDAGSync(err)
}
return p.loadBlockLinks(sessionCtx, &linkSystem, block)
}
// loadBlockLinks loads the links of a block recursively.
//
// The function returns immediately on the first error encountered.
func (p *P2P) loadBlockLinks(ctx context.Context, linkSys *linking.LinkSystem, block *coreblock.Block) error {
link, err := block.GenerateLink()
if err != nil {
return NewErrGenerateBlockLink(err)
}
bstore := datastore.BlockstoreFrom(p.db.Rootstore(), immutable.None[int]())
merged, err := bstore.IsMerged(ctx, link.Cid)
if err != nil {
return NewErrCheckBlockMerged(err)
}
if merged {
return nil
}
// TODO: this part is not tested yet because there is not easy way of doing it at the moment.
// https://github.com/sourcenetwork/defradb/issues/3525
if block.Signature != nil {
// we deliberately ignore the first returned value, which indicates whether the signature
// the block was actually verified or not, because we don't handle it any different here.
// But we want to keep the API of VerifyBlockSignature explicit about the results.
_, err := coreblock.VerifyBlockSignature(block, linkSys)
if err != nil {
return NewErrVerifyBlockSig(err)
}
}
var encResults *encryption.Results
if block.IsEncrypted() {
results, err := p.kms.GetKeys(ctx, *block.Encryption)
if err != nil {
return NewErrGetEncKeysForBlock(err)
}
encResults = results
}
for _, lnk := range block.AllLinks() {
if ctx.Err() != nil {
return ctx.Err()
}
ctxWithTimeout, cancel := context.WithTimeout(ctx, p.blockSyncTimeout(ctx))
nd, err := linkSys.Load(linking.LinkContext{Ctx: ctxWithTimeout}, lnk, coreblock.BlockSchemaPrototype)
cancel()
if err != nil {
// Distinguish "the peer did not serve this block in time" from other load failures.
// Only the per-block timeout is attributed here; a deadline on the parent ctx is a
// caller-level cancellation and is reported as-is.
if errors.Is(err, context.DeadlineExceeded) && ctx.Err() == nil {
return NewErrBlockSyncTimeout(err, lnk.String())
}
return NewErrLoadLinkedBlock(err)
}
linkBlock, err := coreblock.GetFromNode(nd)
if err != nil {
return NewErrDecodeLinkedBlock(err)
}
err = p.loadBlockLinks(ctx, linkSys, linkBlock)
if err != nil {
return NewErrProcessLinkedBlock(err)
}
}
if encResults != nil {
for res := range encResults.Get() {
if res.Error != nil {
return NewErrRetrieveEncKey(res.Error)
}
}
}
return nil
}