@@ -25,13 +25,13 @@ import (
25
25
"sort"
26
26
"testing"
27
27
28
+ "github.com/ethereum/go-ethereum/swarm/storage"
28
29
"github.com/ethereum/go-ethereum/swarm/storage/feed/lookup"
29
30
30
31
"github.com/ethereum/go-ethereum/common"
31
32
"github.com/ethereum/go-ethereum/crypto"
32
33
"github.com/ethereum/go-ethereum/swarm/api"
33
34
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
34
- "github.com/ethereum/go-ethereum/swarm/multihash"
35
35
"github.com/ethereum/go-ethereum/swarm/storage/feed"
36
36
)
37
37
@@ -368,58 +368,99 @@ func newTestSigner() (*feed.GenericSigner, error) {
368
368
return feed .NewGenericSigner (privKey ), nil
369
369
}
370
370
371
- // test the transparent resolving of multihash feed updates with bzz:// scheme
371
+ // Test the transparent resolving of feed updates with bzz:// scheme
372
372
//
373
- // first upload data, and store the multihash to the resulting manifest in a feed update
374
- // retrieving the update with the multihash should return the manifest pointing directly to the data
373
+ // First upload data to bzz:, and store the Swarm hash to the resulting manifest in a feed update.
374
+ // This effectively uses a feed to store a pointer to content rather than the content itself
375
+ // Retrieving the update with the Swarm hash should return the manifest pointing directly to the data
375
376
// and raw retrieve of that hash should return the data
376
- func TestClientCreateFeedMultihash (t * testing.T ) {
377
+ func TestClientBzzWithFeed (t * testing.T ) {
377
378
378
379
signer , _ := newTestSigner ()
379
380
381
+ // Initialize a Swarm test server
380
382
srv := swarmhttp .NewTestSwarmServer (t , serverFunc , nil )
381
- client := NewClient (srv .URL )
383
+ swarmClient := NewClient (srv .URL )
382
384
defer srv .Close ()
383
385
384
- // add the data our multihash aliased manifest will point to
385
- databytes := []byte ("bar" )
386
-
387
- swarmHash , err := client .UploadRaw (bytes .NewReader (databytes ), int64 (len (databytes )), false )
386
+ // put together some data for our test:
387
+ dataBytes := []byte (`
388
+ //
389
+ // Create some data our manifest will point to. Data that could be very big and wouldn't fit in a feed update.
390
+ // So what we are going to do is upload it to Swarm bzz:// and obtain a **manifest hash** pointing to it:
391
+ //
392
+ // MANIFEST HASH --> DATA
393
+ //
394
+ // Then, we store that **manifest hash** into a Swarm Feed update. Once we have done this,
395
+ // we can use the **feed manifest hash** in bzz:// instead, this way: bzz://feed-manifest-hash.
396
+ //
397
+ // FEED MANIFEST HASH --> MANIFEST HASH --> DATA
398
+ //
399
+ // Given that we can update the feed at any time with a new **manifest hash** but the **feed manifest hash**
400
+ // stays constant, we have effectively created a fixed address to changing content. (Applause)
401
+ //
402
+ // FEED MANIFEST HASH (the same) --> MANIFEST HASH(2) --> DATA(2)
403
+ //
404
+ ` )
405
+
406
+ // Create a virtual File out of memory containing the above data
407
+ f := & File {
408
+ ReadCloser : ioutil .NopCloser (bytes .NewReader (dataBytes )),
409
+ ManifestEntry : api.ManifestEntry {
410
+ ContentType : "text/plain" ,
411
+ Mode : 0660 ,
412
+ Size : int64 (len (dataBytes )),
413
+ },
414
+ }
415
+
416
+ // upload data to bzz:// and retrieve the content-addressed manifest hash, hex-encoded.
417
+ manifestAddressHex , err := swarmClient .Upload (f , "" , false )
388
418
if err != nil {
389
- t .Fatalf ("Error uploading raw test data : %s" , err )
419
+ t .Fatalf ("Error creating manifest : %s" , err )
390
420
}
391
421
392
- s := common .FromHex (swarmHash )
393
- mh := multihash .ToMultihash (s )
422
+ // convert the hex-encoded manifest hash to a 32-byte slice
423
+ manifestAddress := common .FromHex (manifestAddressHex )
424
+
425
+ if len (manifestAddress ) != storage .AddressLength {
426
+ t .Fatalf ("Something went wrong. Got a hash of an unexpected length. Expected %d bytes. Got %d" , storage .AddressLength , len (manifestAddress ))
427
+ }
394
428
395
- // our feed topic
396
- topic , _ := feed .NewTopic ("foo.eth " , nil )
429
+ // Now create a ** feed manifest**. For that, we need a topic:
430
+ topic , _ := feed .NewTopic ("interesting topic indeed " , nil )
397
431
398
- createRequest := feed .NewFirstRequest (topic )
432
+ // Build a feed request to update data
433
+ request := feed .NewFirstRequest (topic )
399
434
400
- createRequest .SetData (mh )
401
- if err := createRequest .Sign (signer ); err != nil {
435
+ // Put the 32-byte address of the manifest into the feed update
436
+ request .SetData (manifestAddress )
437
+
438
+ // Sign the update
439
+ if err := request .Sign (signer ); err != nil {
402
440
t .Fatalf ("Error signing update: %s" , err )
403
441
}
404
442
405
- feedManifestHash , err := client . CreateFeedWithManifest ( createRequest )
406
-
443
+ // Publish the update and at the same time request a **feed manifest** to be created
444
+ feedManifestAddressHex , err := swarmClient . CreateFeedWithManifest ( request )
407
445
if err != nil {
408
446
t .Fatalf ("Error creating feed manifest: %s" , err )
409
447
}
410
448
411
- correctManifestAddrHex := "bb056a5264c295c2b0f613c8409b9c87ce9d71576ace02458160df4cc894210b"
412
- if feedManifestHash != correctManifestAddrHex {
413
- t .Fatalf ("Response feed manifest mismatch, expected '%s', got '%s'" , correctManifestAddrHex , feedManifestHash )
449
+ // Check we have received the exact **feed manifest** to be expected
450
+ // given the topic and user signing the updates:
451
+ correctFeedManifestAddrHex := "747c402e5b9dc715a25a4393147512167bab018a007fad7cdcd9adc7fce1ced2"
452
+ if feedManifestAddressHex != correctFeedManifestAddrHex {
453
+ t .Fatalf ("Response feed manifest mismatch, expected '%s', got '%s'" , correctFeedManifestAddrHex , feedManifestAddressHex )
414
454
}
415
455
416
456
// Check we get a not found error when trying to get feed updates with a made-up manifest
417
- _ , err = client .QueryFeed (nil , "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" )
457
+ _ , err = swarmClient .QueryFeed (nil , "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" )
418
458
if err != ErrNoFeedUpdatesFound {
419
459
t .Fatalf ("Expected to receive ErrNoFeedUpdatesFound error. Got: %s" , err )
420
460
}
421
461
422
- reader , err := client .QueryFeed (nil , correctManifestAddrHex )
462
+ // If we query the feed directly we should get **manifest hash** back:
463
+ reader , err := swarmClient .QueryFeed (nil , correctFeedManifestAddrHex )
423
464
if err != nil {
424
465
t .Fatalf ("Error retrieving feed updates: %s" , err )
425
466
}
@@ -428,10 +469,27 @@ func TestClientCreateFeedMultihash(t *testing.T) {
428
469
if err != nil {
429
470
t .Fatal (err )
430
471
}
431
- if ! bytes .Equal (mh , gotData ) {
432
- t .Fatalf ("Expected: %v, got %v" , mh , gotData )
472
+
473
+ //Check that indeed the **manifest hash** is retrieved
474
+ if ! bytes .Equal (manifestAddress , gotData ) {
475
+ t .Fatalf ("Expected: %v, got %v" , manifestAddress , gotData )
476
+ }
477
+
478
+ // Now the final test we were looking for: Use bzz://<feed-manifest> and that should resolve all manifests
479
+ // and return the original data directly:
480
+ f , err = swarmClient .Download (feedManifestAddressHex , "" )
481
+ if err != nil {
482
+ t .Fatal (err )
483
+ }
484
+ gotData , err = ioutil .ReadAll (f )
485
+ if err != nil {
486
+ t .Fatal (err )
433
487
}
434
488
489
+ // Check that we get back the original data:
490
+ if ! bytes .Equal (dataBytes , gotData ) {
491
+ t .Fatalf ("Expected: %v, got %v" , manifestAddress , gotData )
492
+ }
435
493
}
436
494
437
495
// TestClientCreateUpdateFeed will check that feeds can be created and updated via the HTTP client.
0 commit comments