-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathsteward.go
More file actions
127 lines (108 loc) · 4.14 KB
/
Copy pathsteward.go
File metadata and controls
127 lines (108 loc) · 4.14 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
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package stewardess provides convenience methods
// for reseeding content on Swarm.
package steward
import (
"context"
"errors"
"fmt"
"github.com/ethersphere/bee/v2/pkg/file/redundancy"
"github.com/ethersphere/bee/v2/pkg/postage"
"github.com/ethersphere/bee/v2/pkg/replicas"
"github.com/ethersphere/bee/v2/pkg/retrieval"
"github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storer"
"github.com/ethersphere/bee/v2/pkg/swarm"
"github.com/ethersphere/bee/v2/pkg/topology"
"github.com/ethersphere/bee/v2/pkg/traversal"
)
type Interface interface {
// Reupload root hash and all of its underlying
// associated chunks to the network.
Reupload(context.Context, swarm.Address, postage.Stamper, redundancy.Level) error
// IsRetrievable checks whether the content
// on the given address is retrievable.
IsRetrievable(context.Context, swarm.Address, redundancy.Level) (bool, error)
}
type steward struct {
netStore storer.NetStore
traverser traversal.Traverser
netTraverser traversal.Traverser
netGetter retrieval.Interface
}
func New(ns storer.NetStore, r retrieval.Interface, joinerPutter storage.Putter) Interface {
return &steward{
netStore: ns,
traverser: traversal.New(ns.Download(true), joinerPutter),
netTraverser: traversal.New(&netGetter{r}, joinerPutter),
netGetter: r,
}
}
// Reupload content with the given root hash to the network.
// The service will automatically dereference and traverse all
// addresses and push every chunk individually to the network.
// It assumes all chunks are available locally. It is therefore
// advisable to pin the content locally before trying to reupload it.
func (s *steward) Reupload(ctx context.Context, root swarm.Address, stamper postage.Stamper, rLevel redundancy.Level) error {
uploaderSession := s.netStore.DirectUpload()
getter := s.netStore.Download(false)
fn := func(addr swarm.Address) error {
c, err := getter.Get(ctx, addr)
if err != nil {
return err
}
stamp, err := stamper.Stamp(c.Address(), c.Address())
if err != nil {
return fmt.Errorf("stamping chunk %s: %w", c.Address(), err)
}
return uploaderSession.Put(ctx, c.WithStamp(stamp))
}
if err := s.traverser.Traverse(ctx, root, fn, rLevel); err != nil {
return errors.Join(
fmt.Errorf("traversal of %s failed: %w", root.String(), err),
uploaderSession.Cleanup(),
)
}
if rLevel != redundancy.NONE {
rootChunk, err := getter.Get(ctx, root)
if err != nil {
return errors.Join(fmt.Errorf("get root chunk for dispersed replicas: %w", err), uploaderSession.Cleanup())
}
stamp, err := stamper.Stamp(rootChunk.Address(), rootChunk.Address())
if err != nil {
return errors.Join(fmt.Errorf("stamping root chunk for dispersed replicas: %w", err), uploaderSession.Cleanup())
}
if err := replicas.NewPutter(uploaderSession, rLevel).Put(ctx, rootChunk.WithStamp(stamp)); err != nil {
return errors.Join(fmt.Errorf("re-uploading dispersed replicas: %w", err), uploaderSession.Cleanup())
}
}
return uploaderSession.Done(root)
}
// IsRetrievable implements Interface.IsRetrievable method.
func (s *steward) IsRetrievable(ctx context.Context, root swarm.Address, rLevel redundancy.Level) (bool, error) {
fn := func(a swarm.Address) error {
_, err := s.netGetter.RetrieveChunk(ctx, a, swarm.ZeroAddress)
return err
}
switch err := s.netTraverser.Traverse(ctx, root, fn, rLevel); {
case errors.Is(err, storage.ErrNotFound):
return false, nil
case errors.Is(err, topology.ErrNotFound):
return false, nil
case err != nil:
return false, fmt.Errorf("traversal of %q failed: %w", root, err)
default:
return true, nil
}
}
// netGetter implements the storage Getter.Get method in a way
// that it will try to retrieve the chunk only from the network.
type netGetter struct {
retrieval retrieval.Interface
}
// Get implements the storage Getter.Get interface.
func (ng *netGetter) Get(ctx context.Context, addr swarm.Address) (swarm.Chunk, error) {
return ng.retrieval.RetrieveChunk(ctx, addr, swarm.ZeroAddress)
}