/
steward.go
104 lines (88 loc) · 3.2 KB
/
steward.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
// 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/postage"
"github.com/ethersphere/bee/v2/pkg/retrieval"
"github.com/ethersphere/bee/v2/pkg/storage"
storer "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) error
// IsRetrievable checks whether the content
// on the given address is retrievable.
IsRetrievable(context.Context, swarm.Address) (bool, error)
}
type steward struct {
netStore storer.NetStore
traverser traversal.Traverser
netTraverser traversal.Traverser
}
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),
}
}
// 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) 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())
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); err != nil {
return errors.Join(
fmt.Errorf("traversal of %s failed: %w", root.String(), err),
uploaderSession.Cleanup(),
)
}
return uploaderSession.Done(root)
}
// IsRetrievable implements Interface.IsRetrievable method.
func (s *steward) IsRetrievable(ctx context.Context, root swarm.Address) (bool, error) {
noop := func(leaf swarm.Address) error { return nil }
switch err := s.netTraverser.Traverse(ctx, root, noop); {
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)
}