Skip to content

Commit

Permalink
Merge pull request ethereum#17796 from epiclabs-io/mru-feeds
Browse files Browse the repository at this point in the history
swarm/storage/feeds: Renamed MRU to Swarm Feeds
  • Loading branch information
zelig authored Oct 3, 2018
2 parents 303b996 + de01178 commit e567711
Show file tree
Hide file tree
Showing 44 changed files with 740 additions and 812 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ swarm/services @zelig
swarm/state @justelad
swarm/storage/encryption @gbalint @zelig @nagydani
swarm/storage/mock @janos
swarm/storage/mru @nolash
swarm/storage/feed @nolash @jpeletier
swarm/testutil @lmars
whisper/ @gballet @gluk256
72 changes: 36 additions & 36 deletions cmd/swarm/mru.go → cmd/swarm/feeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

// Command resource allows the user to create and update signed mutable resource updates
// Command feed allows the user to create and update signed Swarm feeds
package main

import (
Expand All @@ -27,17 +27,17 @@ import (

"github.com/ethereum/go-ethereum/cmd/utils"
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
"github.com/ethereum/go-ethereum/swarm/storage/mru"
"github.com/ethereum/go-ethereum/swarm/storage/feed"
"gopkg.in/urfave/cli.v1"
)

func NewGenericSigner(ctx *cli.Context) mru.Signer {
return mru.NewGenericSigner(getPrivKey(ctx))
func NewGenericSigner(ctx *cli.Context) feed.Signer {
return feed.NewGenericSigner(getPrivKey(ctx))
}

func getTopic(ctx *cli.Context) (topic mru.Topic) {
var name = ctx.String(SwarmResourceNameFlag.Name)
var relatedTopic = ctx.String(SwarmResourceTopicFlag.Name)
func getTopic(ctx *cli.Context) (topic feed.Topic) {
var name = ctx.String(SwarmFeedNameFlag.Name)
var relatedTopic = ctx.String(SwarmFeedTopicFlag.Name)
var relatedTopicBytes []byte
var err error

Expand All @@ -48,42 +48,42 @@ func getTopic(ctx *cli.Context) (topic mru.Topic) {
}
}

topic, err = mru.NewTopic(name, relatedTopicBytes)
topic, err = feed.NewTopic(name, relatedTopicBytes)
if err != nil {
utils.Fatalf("Error parsing topic: %s", err)
}
return topic
}

// swarm resource create <frequency> [--name <name>] [--data <0x Hexdata> [--multihash=false]]
// swarm resource update <Manifest Address or ENS domain> <0x Hexdata> [--multihash=false]
// swarm resource info <Manifest Address or ENS domain>
// swarm feed create <frequency> [--name <name>] [--data <0x Hexdata> [--multihash=false]]
// swarm feed update <Manifest Address or ENS domain> <0x Hexdata> [--multihash=false]
// swarm feed info <Manifest Address or ENS domain>

func resourceCreate(ctx *cli.Context) {
func feedCreateManifest(ctx *cli.Context) {
var (
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
client = swarm.NewClient(bzzapi)
)

newResourceRequest := mru.NewFirstRequest(getTopic(ctx))
newResourceRequest.View.User = resourceGetUser(ctx)
newFeedUpdateRequest := feed.NewFirstRequest(getTopic(ctx))
newFeedUpdateRequest.Feed.User = feedGetUser(ctx)

manifestAddress, err := client.CreateResource(newResourceRequest)
manifestAddress, err := client.CreateFeedWithManifest(newFeedUpdateRequest)
if err != nil {
utils.Fatalf("Error creating resource: %s", err.Error())
utils.Fatalf("Error creating feed manifest: %s", err.Error())
return
}
fmt.Println(manifestAddress) // output manifest address to the user in a single line (useful for other commands to pick up)

}

func resourceUpdate(ctx *cli.Context) {
func feedUpdate(ctx *cli.Context) {
args := ctx.Args()

var (
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
client = swarm.NewClient(bzzapi)
manifestAddressOrDomain = ctx.String(SwarmResourceManifestFlag.Name)
manifestAddressOrDomain = ctx.String(SwarmFeedManifestFlag.Name)
)

if len(args) < 1 {
Expand All @@ -100,55 +100,55 @@ func resourceUpdate(ctx *cli.Context) {
return
}

var updateRequest *mru.Request
var query *mru.Query
var updateRequest *feed.Request
var query *feed.Query

if manifestAddressOrDomain == "" {
query = new(mru.Query)
query = new(feed.Query)
query.User = signer.Address()
query.Topic = getTopic(ctx)

}

// Retrieve resource status and metadata out of the manifest
updateRequest, err = client.GetResourceMetadata(query, manifestAddressOrDomain)
// Retrieve a feed update request
updateRequest, err = client.GetFeedRequest(query, manifestAddressOrDomain)
if err != nil {
utils.Fatalf("Error retrieving resource status: %s", err.Error())
utils.Fatalf("Error retrieving feed status: %s", err.Error())
}

// set the new data
updateRequest.SetData(data)

// sign update
if err = updateRequest.Sign(signer); err != nil {
utils.Fatalf("Error signing resource update: %s", err.Error())
utils.Fatalf("Error signing feed update: %s", err.Error())
}

// post update
err = client.UpdateResource(updateRequest)
err = client.UpdateFeed(updateRequest)
if err != nil {
utils.Fatalf("Error updating resource: %s", err.Error())
utils.Fatalf("Error updating feed: %s", err.Error())
return
}
}

func resourceInfo(ctx *cli.Context) {
func feedInfo(ctx *cli.Context) {
var (
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
client = swarm.NewClient(bzzapi)
manifestAddressOrDomain = ctx.String(SwarmResourceManifestFlag.Name)
manifestAddressOrDomain = ctx.String(SwarmFeedManifestFlag.Name)
)

var query *mru.Query
var query *feed.Query
if manifestAddressOrDomain == "" {
query = new(mru.Query)
query = new(feed.Query)
query.Topic = getTopic(ctx)
query.User = resourceGetUser(ctx)
query.User = feedGetUser(ctx)
}

metadata, err := client.GetResourceMetadata(query, manifestAddressOrDomain)
metadata, err := client.GetFeedRequest(query, manifestAddressOrDomain)
if err != nil {
utils.Fatalf("Error retrieving resource metadata: %s", err.Error())
utils.Fatalf("Error retrieving feed metadata: %s", err.Error())
return
}
encodedMetadata, err := metadata.MarshalJSON()
Expand All @@ -158,8 +158,8 @@ func resourceInfo(ctx *cli.Context) {
fmt.Println(string(encodedMetadata))
}

func resourceGetUser(ctx *cli.Context) common.Address {
var user = ctx.String(SwarmResourceUserFlag.Name)
func feedGetUser(ctx *cli.Context) common.Address {
var user = ctx.String(SwarmFeedUserFlag.Name)
if user != "" {
return common.HexToAddress(user)
}
Expand Down
44 changes: 22 additions & 22 deletions cmd/swarm/mru_test.go → cmd/swarm/feeds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ import (
"testing"

"github.com/ethereum/go-ethereum/swarm/api"
"github.com/ethereum/go-ethereum/swarm/storage/mru/lookup"
"github.com/ethereum/go-ethereum/swarm/storage/feed/lookup"
"github.com/ethereum/go-ethereum/swarm/testutil"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/swarm/storage/mru"
"github.com/ethereum/go-ethereum/swarm/storage/feed"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
)

func TestCLIResourceUpdate(t *testing.T) {
func TestCLIFeedUpdate(t *testing.T) {

srv := testutil.NewTestSwarmServer(t, func(api *api.API) testutil.TestServer {
return swarmhttp.NewServer(api, "")
}, nil)
log.Info("starting 1 node cluster")
log.Info("starting a test swarm server")
defer srv.Close()

// create a private key file for signing
Expand All @@ -65,7 +65,7 @@ func TestCLIResourceUpdate(t *testing.T) {
}

// compose a topic. We'll be doing quotes about Miguel de Cervantes
var topic mru.Topic
var topic feed.Topic
subject := []byte("Miguel de Cervantes")
copy(topic[:], subject[:])
name := "quotes"
Expand All @@ -77,13 +77,13 @@ func TestCLIResourceUpdate(t *testing.T) {
flags := []string{
"--bzzapi", srv.URL,
"--bzzaccount", pkfile.Name(),
"resource", "update",
"feed", "update",
"--topic", topic.Hex(),
"--name", name,
hexData}

// create an update and expect an exit without errors
log.Info(fmt.Sprintf("updating a resource with 'swarm resource update'"))
log.Info(fmt.Sprintf("updating a feed with 'swarm feed update'"))
cmd := runSwarm(t, flags...)
cmd.ExpectExit()

Expand All @@ -95,22 +95,22 @@ func TestCLIResourceUpdate(t *testing.T) {

// build the same topic as before, this time
// we use NewTopic to create a topic automatically.
topic, err = mru.NewTopic(name, subject)
topic, err = feed.NewTopic(name, subject)
if err != nil {
t.Fatal(err)
}

// View configures whose updates we will be looking up.
view := mru.View{
// Feed configures whose updates we will be looking up.
fd := feed.Feed{
Topic: topic,
User: address,
}

// Build a query to get the latest update
query := mru.NewQueryLatest(&view, lookup.NoClue)
query := feed.NewQueryLatest(&fd, lookup.NoClue)

// retrieve content!
reader, err := client.GetResource(query, "")
reader, err := client.QueryFeed(query, "")
if err != nil {
t.Fatal(err)
}
Expand All @@ -128,45 +128,45 @@ func TestCLIResourceUpdate(t *testing.T) {
// Now retrieve info for the next update
flags = []string{
"--bzzapi", srv.URL,
"resource", "info",
"feed", "info",
"--topic", topic.Hex(),
"--user", address.Hex(),
}

log.Info(fmt.Sprintf("getting resource info with 'swarm resource info'"))
log.Info(fmt.Sprintf("getting feed info with 'swarm feed info'"))
cmd = runSwarm(t, flags...)
_, matches := cmd.ExpectRegexp(`.*`) // regex hack to extract stdout
cmd.ExpectExit()

// verify we can deserialize the result as a valid JSON
var request mru.Request
var request feed.Request
err = json.Unmarshal([]byte(matches[0]), &request)
if err != nil {
t.Fatal(err)
}

// make sure the retrieved view is the same
if request.View != view {
t.Fatalf("Expected view to be: %s, got %s", view, request.View)
// make sure the retrieved feed is the same
if request.Feed != fd {
t.Fatalf("Expected feed to be: %s, got %s", fd, request.Feed)
}

// test publishing a manifest
flags = []string{
"--bzzapi", srv.URL,
"--bzzaccount", pkfile.Name(),
"resource", "create",
"feed", "create",
"--topic", topic.Hex(),
}

log.Info(fmt.Sprintf("Publishing manifest with 'swarm resource create'"))
log.Info(fmt.Sprintf("Publishing manifest with 'swarm feed create'"))
cmd = runSwarm(t, flags...)
_, matches = cmd.ExpectRegexp(`[a-f\d]{64}`) // regex hack to extract stdout
cmd.ExpectExit()

manifestAddress := matches[0] // read the received resource manifest
manifestAddress := matches[0] // read the received feed manifest

// now attempt to lookup the latest update using a manifest instead
reader, err = client.GetResource(nil, manifestAddress)
reader, err = client.QueryFeed(nil, manifestAddress)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading

0 comments on commit e567711

Please sign in to comment.