@@ -25,13 +25,13 @@ import (
2525 "sort"
2626 "testing"
2727
28+ "github.com/ethereum/go-ethereum/swarm/storage"
2829 "github.com/ethereum/go-ethereum/swarm/storage/feed/lookup"
2930
3031 "github.com/ethereum/go-ethereum/common"
3132 "github.com/ethereum/go-ethereum/crypto"
3233 "github.com/ethereum/go-ethereum/swarm/api"
3334 swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
34- "github.com/ethereum/go-ethereum/swarm/multihash"
3535 "github.com/ethereum/go-ethereum/swarm/storage/feed"
3636)
3737
@@ -368,58 +368,99 @@ func newTestSigner() (*feed.GenericSigner, error) {
368368 return feed .NewGenericSigner (privKey ), nil
369369}
370370
371- // test the transparent resolving of multihash feed updates with bzz:// scheme
371+ // Test the transparent resolving of feed updates with bzz:// scheme
372372//
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
375376// and raw retrieve of that hash should return the data
376- func TestClientCreateFeedMultihash (t * testing.T ) {
377+ func TestClientBzzWithFeed (t * testing.T ) {
377378
378379 signer , _ := newTestSigner ()
379380
381+ // Initialize a Swarm test server
380382 srv := swarmhttp .NewTestSwarmServer (t , serverFunc , nil )
381- client := NewClient (srv .URL )
383+ swarmClient := NewClient (srv .URL )
382384 defer srv .Close ()
383385
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 )
388418 if err != nil {
389- t .Fatalf ("Error uploading raw test data : %s" , err )
419+ t .Fatalf ("Error creating manifest : %s" , err )
390420 }
391421
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+ }
394428
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 )
397431
398- createRequest := feed .NewFirstRequest (topic )
432+ // Build a feed request to update data
433+ request := feed .NewFirstRequest (topic )
399434
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 {
402440 t .Fatalf ("Error signing update: %s" , err )
403441 }
404442
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 )
407445 if err != nil {
408446 t .Fatalf ("Error creating feed manifest: %s" , err )
409447 }
410448
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 )
414454 }
415455
416456 // 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" )
418458 if err != ErrNoFeedUpdatesFound {
419459 t .Fatalf ("Expected to receive ErrNoFeedUpdatesFound error. Got: %s" , err )
420460 }
421461
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 )
423464 if err != nil {
424465 t .Fatalf ("Error retrieving feed updates: %s" , err )
425466 }
@@ -428,10 +469,27 @@ func TestClientCreateFeedMultihash(t *testing.T) {
428469 if err != nil {
429470 t .Fatal (err )
430471 }
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 )
433487 }
434488
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+ }
435493}
436494
437495// TestClientCreateUpdateFeed will check that feeds can be created and updated via the HTTP client.
0 commit comments