Skip to content

Commit 3ff2f75

Browse files
gbalintjanosnonsensezelig
authored
swarm: Chunk refactor (#17659)
Co-authored-by: Janos Guljas <janos@resenje.org> Co-authored-by: Balint Gabor <balint.g@gmail.com> Co-authored-by: Anton Evangelatov <anton.evangelatov@gmail.com> Co-authored-by: Viktor Trón <viktor.tron@gmail.com>
1 parent ff3a5d2 commit 3ff2f75

55 files changed

Lines changed: 3149 additions & 1881 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cmd/swarm/hash.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func hash(ctx *cli.Context) {
3939
defer f.Close()
4040

4141
stat, _ := f.Stat()
42-
fileStore := storage.NewFileStore(storage.NewMapChunkStore(), storage.NewFileStoreParams())
42+
fileStore := storage.NewFileStore(&storage.FakeChunkStore{}, storage.NewFileStoreParams())
4343
addr, _, err := fileStore.Store(context.TODO(), f, stat.Size(), false)
4444
if err != nil {
4545
utils.Fatalf("%v\n", err)

‎cmd/swarm/swarm-smoke/main.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func main() {
4848
cli.StringFlag{
4949
Name: "cluster-endpoint",
5050
Value: "testing",
51-
Usage: "cluster to point to (open, or testing)",
51+
Usage: "cluster to point to (local, open or testing)",
5252
Destination: &cluster,
5353
},
5454
cli.IntFlag{
@@ -76,8 +76,8 @@ func main() {
7676
},
7777
cli.IntFlag{
7878
Name: "filesize",
79-
Value: 1,
80-
Usage: "file size for generated random file in MB",
79+
Value: 1024,
80+
Usage: "file size for generated random file in KB",
8181
Destination: &filesize,
8282
},
8383
}

‎cmd/swarm/swarm-smoke/upload_and_sync.go‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ import (
3939
func generateEndpoints(scheme string, cluster string, from int, to int) {
4040
if cluster == "prod" {
4141
cluster = ""
42+
} else if cluster == "local" {
43+
for port := from; port <= to; port++ {
44+
endpoints = append(endpoints, fmt.Sprintf("%s://localhost:%v", scheme, port))
45+
}
46+
return
4247
} else {
4348
cluster = cluster + "."
4449
}
@@ -53,13 +58,13 @@ func generateEndpoints(scheme string, cluster string, from int, to int) {
5358
}
5459

5560
func cliUploadAndSync(c *cli.Context) error {
56-
defer func(now time.Time) { log.Info("total time", "time", time.Since(now), "size", filesize) }(time.Now())
61+
defer func(now time.Time) { log.Info("total time", "time", time.Since(now), "size (kb)", filesize) }(time.Now())
5762

5863
generateEndpoints(scheme, cluster, from, to)
5964

6065
log.Info("uploading to " + endpoints[0] + " and syncing")
6166

62-
f, cleanup := generateRandomFile(filesize * 1000000)
67+
f, cleanup := generateRandomFile(filesize * 1000)
6368
defer cleanup()
6469

6570
hash, err := upload(f, endpoints[0])
@@ -76,12 +81,7 @@ func cliUploadAndSync(c *cli.Context) error {
7681

7782
log.Info("uploaded successfully", "hash", hash, "digest", fmt.Sprintf("%x", fhash))
7883

79-
if filesize < 10 {
80-
time.Sleep(35 * time.Second)
81-
} else {
82-
time.Sleep(15 * time.Second)
83-
time.Sleep(2 * time.Duration(filesize) * time.Second)
84-
}
84+
time.Sleep(3 * time.Second)
8585

8686
wg := sync.WaitGroup{}
8787
for _, endpoint := range endpoints {
@@ -109,7 +109,7 @@ func cliUploadAndSync(c *cli.Context) error {
109109
// fetch is getting the requested `hash` from the `endpoint` and compares it with the `original` file
110110
func fetch(hash string, endpoint string, original []byte, ruid string) error {
111111
log.Trace("sleeping", "ruid", ruid)
112-
time.Sleep(5 * time.Second)
112+
time.Sleep(3 * time.Second)
113113

114114
log.Trace("http get request", "ruid", ruid, "api", endpoint, "hash", hash)
115115
res, err := http.Get(endpoint + "/bzz:/" + hash + "/")

‎swarm/api/api.go‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,6 @@ func NewAPI(fileStore *storage.FileStore, dns Resolver, resourceHandler *mru.Han
250250
return
251251
}
252252

253-
// Upload to be used only in TEST
254-
func (a *API) Upload(ctx context.Context, uploadDir, index string, toEncrypt bool) (hash string, err error) {
255-
fs := NewFileSystem(a)
256-
hash, err = fs.Upload(uploadDir, index, toEncrypt)
257-
return hash, err
258-
}
259-
260253
// Retrieve FileStore reader API
261254
func (a *API) Retrieve(ctx context.Context, addr storage.Address) (reader storage.LazySectionReader, isEncrypted bool) {
262255
return a.fileStore.Retrieve(ctx, addr)

‎swarm/api/config.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type Config struct {
6262
NetworkID uint64
6363
SwapEnabled bool
6464
SyncEnabled bool
65+
SyncingSkipCheck bool
6566
DeliverySkipCheck bool
6667
LightNodeEnabled bool
6768
SyncUpdateDelay time.Duration
@@ -89,7 +90,8 @@ func NewConfig() (c *Config) {
8990
NetworkID: network.DefaultNetworkID,
9091
SwapEnabled: false,
9192
SyncEnabled: true,
92-
DeliverySkipCheck: false,
93+
SyncingSkipCheck: false,
94+
DeliverySkipCheck: true,
9395
SyncUpdateDelay: 15 * time.Second,
9496
SwapAPI: "",
9597
}

‎swarm/api/http/server_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,12 @@ func testBzzGetPath(encrypted bool, t *testing.T) {
477477
var wait func(context.Context) error
478478
ctx := context.TODO()
479479
addr[i], wait, err = srv.FileStore.Store(ctx, reader[i], int64(len(mf)), encrypted)
480-
for j := i + 1; j < len(testmanifest); j++ {
481-
testmanifest[j] = strings.Replace(testmanifest[j], fmt.Sprintf("<key%v>", i), addr[i].Hex(), -1)
482-
}
483480
if err != nil {
484481
t.Fatal(err)
485482
}
483+
for j := i + 1; j < len(testmanifest); j++ {
484+
testmanifest[j] = strings.Replace(testmanifest[j], fmt.Sprintf("<key%v>", i), addr[i].Hex(), -1)
485+
}
486486
err = wait(ctx)
487487
if err != nil {
488488
t.Fatal(err)

‎swarm/api/manifest.go‎

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ func (a *API) NewManifest(ctx context.Context, toEncrypt bool) (storage.Address,
6969
if err != nil {
7070
return nil, err
7171
}
72-
key, wait, err := a.Store(ctx, bytes.NewReader(data), int64(len(data)), toEncrypt)
73-
wait(ctx)
74-
return key, err
72+
addr, wait, err := a.Store(ctx, bytes.NewReader(data), int64(len(data)), toEncrypt)
73+
if err != nil {
74+
return nil, err
75+
}
76+
err = wait(ctx)
77+
return addr, err
7578
}
7679

7780
// Manifest hack for supporting Mutable Resource Updates from the bzz: scheme
@@ -87,8 +90,12 @@ func (a *API) NewResourceManifest(ctx context.Context, resourceAddr string) (sto
8790
if err != nil {
8891
return nil, err
8992
}
90-
key, _, err := a.Store(ctx, bytes.NewReader(data), int64(len(data)), false)
91-
return key, err
93+
addr, wait, err := a.Store(ctx, bytes.NewReader(data), int64(len(data)), false)
94+
if err != nil {
95+
return nil, err
96+
}
97+
err = wait(ctx)
98+
return addr, err
9299
}
93100

94101
// ManifestWriter is used to add and remove entries from an underlying manifest
@@ -106,21 +113,26 @@ func (a *API) NewManifestWriter(ctx context.Context, addr storage.Address, quitC
106113
return &ManifestWriter{a, trie, quitC}, nil
107114
}
108115

109-
// AddEntry stores the given data and adds the resulting key to the manifest
110-
func (m *ManifestWriter) AddEntry(ctx context.Context, data io.Reader, e *ManifestEntry) (key storage.Address, err error) {
116+
// AddEntry stores the given data and adds the resulting address to the manifest
117+
func (m *ManifestWriter) AddEntry(ctx context.Context, data io.Reader, e *ManifestEntry) (addr storage.Address, err error) {
111118
entry := newManifestTrieEntry(e, nil)
112119
if data != nil {
113-
key, _, err = m.api.Store(ctx, data, e.Size, m.trie.encrypted)
120+
var wait func(context.Context) error
121+
addr, wait, err = m.api.Store(ctx, data, e.Size, m.trie.encrypted)
122+
if err != nil {
123+
return nil, err
124+
}
125+
err = wait(ctx)
114126
if err != nil {
115127
return nil, err
116128
}
117-
entry.Hash = key.Hex()
129+
entry.Hash = addr.Hex()
118130
}
119131
if entry.Hash == "" {
120-
return key, errors.New("missing entry hash")
132+
return addr, errors.New("missing entry hash")
121133
}
122134
m.trie.addEntry(entry, m.quitC)
123-
return key, nil
135+
return addr, nil
124136
}
125137

126138
// RemoveEntry removes the given path from the manifest
@@ -129,7 +141,7 @@ func (m *ManifestWriter) RemoveEntry(path string) error {
129141
return nil
130142
}
131143

132-
// Store stores the manifest, returning the resulting storage key
144+
// Store stores the manifest, returning the resulting storage address
133145
func (m *ManifestWriter) Store() (storage.Address, error) {
134146
return m.trie.ref, m.trie.recalcAndStore()
135147
}
@@ -211,51 +223,51 @@ type manifestTrieEntry struct {
211223
subtrie *manifestTrie
212224
}
213225

214-
func loadManifest(ctx context.Context, fileStore *storage.FileStore, hash storage.Address, quitC chan bool, decrypt DecryptFunc) (trie *manifestTrie, err error) { // non-recursive, subtrees are downloaded on-demand
215-
log.Trace("manifest lookup", "key", hash)
226+
func loadManifest(ctx context.Context, fileStore *storage.FileStore, addr storage.Address, quitC chan bool, decrypt DecryptFunc) (trie *manifestTrie, err error) { // non-recursive, subtrees are downloaded on-demand
227+
log.Trace("manifest lookup", "addr", addr)
216228
// retrieve manifest via FileStore
217-
manifestReader, isEncrypted := fileStore.Retrieve(ctx, hash)
218-
log.Trace("reader retrieved", "key", hash)
219-
return readManifest(manifestReader, hash, fileStore, isEncrypted, quitC, decrypt)
229+
manifestReader, isEncrypted := fileStore.Retrieve(ctx, addr)
230+
log.Trace("reader retrieved", "addr", addr)
231+
return readManifest(manifestReader, addr, fileStore, isEncrypted, quitC, decrypt)
220232
}
221233

222-
func readManifest(mr storage.LazySectionReader, hash storage.Address, fileStore *storage.FileStore, isEncrypted bool, quitC chan bool, decrypt DecryptFunc) (trie *manifestTrie, err error) { // non-recursive, subtrees are downloaded on-demand
234+
func readManifest(mr storage.LazySectionReader, addr storage.Address, fileStore *storage.FileStore, isEncrypted bool, quitC chan bool, decrypt DecryptFunc) (trie *manifestTrie, err error) { // non-recursive, subtrees are downloaded on-demand
223235

224236
// TODO check size for oversized manifests
225237
size, err := mr.Size(mr.Context(), quitC)
226238
if err != nil { // size == 0
227239
// can't determine size means we don't have the root chunk
228-
log.Trace("manifest not found", "key", hash)
240+
log.Trace("manifest not found", "addr", addr)
229241
err = fmt.Errorf("Manifest not Found")
230242
return
231243
}
232244
if size > manifestSizeLimit {
233-
log.Warn("manifest exceeds size limit", "key", hash, "size", size, "limit", manifestSizeLimit)
245+
log.Warn("manifest exceeds size limit", "addr", addr, "size", size, "limit", manifestSizeLimit)
234246
err = fmt.Errorf("Manifest size of %v bytes exceeds the %v byte limit", size, manifestSizeLimit)
235247
return
236248
}
237249
manifestData := make([]byte, size)
238250
read, err := mr.Read(manifestData)
239251
if int64(read) < size {
240-
log.Trace("manifest not found", "key", hash)
252+
log.Trace("manifest not found", "addr", addr)
241253
if err == nil {
242254
err = fmt.Errorf("Manifest retrieval cut short: read %v, expect %v", read, size)
243255
}
244256
return
245257
}
246258

247-
log.Debug("manifest retrieved", "key", hash)
259+
log.Debug("manifest retrieved", "addr", addr)
248260
var man struct {
249261
Entries []*manifestTrieEntry `json:"entries"`
250262
}
251263
err = json.Unmarshal(manifestData, &man)
252264
if err != nil {
253-
err = fmt.Errorf("Manifest %v is malformed: %v", hash.Log(), err)
254-
log.Trace("malformed manifest", "key", hash)
265+
err = fmt.Errorf("Manifest %v is malformed: %v", addr.Log(), err)
266+
log.Trace("malformed manifest", "addr", addr)
255267
return
256268
}
257269

258-
log.Trace("manifest entries", "key", hash, "len", len(man.Entries))
270+
log.Trace("manifest entries", "addr", addr, "len", len(man.Entries))
259271

260272
trie = &manifestTrie{
261273
fileStore: fileStore,
@@ -406,12 +418,12 @@ func (mt *manifestTrie) recalcAndStore() error {
406418

407419
sr := bytes.NewReader(manifest)
408420
ctx := context.TODO()
409-
key, wait, err2 := mt.fileStore.Store(ctx, sr, int64(len(manifest)), mt.encrypted)
421+
addr, wait, err2 := mt.fileStore.Store(ctx, sr, int64(len(manifest)), mt.encrypted)
410422
if err2 != nil {
411423
return err2
412424
}
413425
err2 = wait(ctx)
414-
mt.ref = key
426+
mt.ref = addr
415427
return err2
416428
}
417429

‎swarm/fuse/swarmfs_test.go‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package fuse
2020

2121
import (
2222
"bytes"
23-
"context"
2423
"crypto/rand"
2524
"flag"
2625
"fmt"
@@ -111,7 +110,7 @@ func createTestFilesAndUploadToSwarm(t *testing.T, api *api.API, files map[strin
111110
}
112111

113112
//upload directory to swarm and return hash
114-
bzzhash, err := api.Upload(context.TODO(), uploadDir, "", toEncrypt)
113+
bzzhash, err := Upload(uploadDir, "", api, toEncrypt)
115114
if err != nil {
116115
t.Fatalf("Error uploading directory %v: %vm encryption: %v", uploadDir, err, toEncrypt)
117116
}
@@ -1695,3 +1694,9 @@ func TestFUSE(t *testing.T) {
16951694
t.Run("appendFileContentsToEndNonEncrypted", ta.appendFileContentsToEndNonEncrypted)
16961695
}
16971696
}
1697+
1698+
func Upload(uploadDir, index string, a *api.API, toEncrypt bool) (hash string, err error) {
1699+
fs := api.NewFileSystem(a)
1700+
hash, err = fs.Upload(uploadDir, index, toEncrypt)
1701+
return hash, err
1702+
}

0 commit comments

Comments
 (0)