Skip to content

Commit 2c110c8

Browse files
jpeletierholiman
authored andcommitted
Swarm MRUs: Adaptive frequency / Predictable lookups / API simplification (#17559)
* swarm/storage/mru: Adaptive Frequency swarm/storage/mru/lookup: fixed getBaseTime Added NewEpoch constructor swarm/api/client: better error handling in GetResource() swarm/storage/mru: Renamed structures. Renamed ResourceMetadata to ResourceID. Renamed ResourceID.Name to ResourceID.Topic swarm/storage/mru: Added binarySerializer interface and test tools swarm/storage/mru/lookup: Changed base time to time and + marshallers swarm/storage/mru: Added ResourceID (former resourceMetadata) swarm/storage/mru: Added ResourceViewId and serialization tests swarm/storage/mru/lookup: fixed epoch unmarshaller. Added Epoch Equals swarm/storage/mru: Fixes as per review comments cmd/swarm: reworded resource create/update help text regarding topic swarm/storage/mru: Added UpdateLookup and serializer tests swarm/storage/mru: Added UpdateHeader, serializers and tests swarm/storage/mru: changed UpdateAddr / epoch to Base() swarm/storage/mru: Added resourceUpdate serializer and tests swarm/storage/mru: Added SignedResourceUpdate tests and serializers swarm/storage/mru/lookup: fixed GetFirstEpoch bug swarm/storage/mru: refactor, comments, cleanup Also added tests for Topic swarm/storage/mru: handler tests pass swarm/storage/mru: all resource package tests pass swarm/storage/mru: resource test pass after adding timestamp checking support swarm/storage/mru: Added JSON serializers to ResourceIDView structures swarm/storage/mru: Sever, client, API test pass swarm/storage/mru: server test pass swarm/storage/mru: Added topic length check swarm/storage/mru: removed some literals, improved "previous lookup" test case swarm/storage/mru: some fixes and comments as per review swarm/storage/mru: first working version without metadata chunk swarm/storage/mru: Various fixes as per review swarm/storage/mru: client test pass swarm/storage/mru: resource query strings and manifest-less queries swarm/storage/mru: simplify naming swarm/storage/mru: first autofreq working version swarm/storage/mru: renamed ToValues to AppendValues swarm/resource/mru: Added ToValues / FromValues for URL query strings swarm/storage/mru: Changed POST resource to work with query strings. No more JSON. swarm/storage/mru: removed resourceid swarm/storage/mru: Opened up structures swarm/storage/mru: Merged Request and SignedResourceUpdate swarm/storage/mru: removed initial data from CLI resource create swarm/storage/mru: Refactor Topic as a direct fixed-length array swarm/storage/mru/lookup: Comprehensive GetNextLevel tests swarm/storage/mru: Added comments Added length checks in Topic swarm/storage/mru: fixes in tests and some code comments swarm/storage/mru/lookup: new optimized lookup algorithm swarm/api: moved getResourceView to api out of server swarm/storage/mru: Lookup algorithm working swarm/storage/mru: comments and renamed NewLookupParams Deleted commented code swarm/storage/mru/lookup: renamed Epoch.LaterThan to After swarm/storage/mru/lookup: Comments and tidying naming swarm/storage/mru: fix lookup algorithm swarm/storage/mru: exposed lookup hint removed updateheader swarm/storage/mru/lookup: changed GetNextEpoch for initial values swarm/storage/mru: resource tests pass swarm/storage/mru: valueSerializer interface and tests swarm/storage/mru/lookup: Comments, improvements, fixes, more tests swarm/storage/mru: renamed UpdateLookup to ID, LookupParams to Query swarm/storage/mru: renamed query receiver var swarm/cmd: MRU CLI tests * cmd/swarm: remove rogue fmt * swarm/storage/mru: Add version / header for future use * swarm/storage/mru: Fixes/comments as per review cmd/swarm: remove rogue fmt swarm/storage/mru: Add version / header for future use- * swarm/storage/mru: fix linter errors * cmd/swarm: Speeded up TestCLIResourceUpdate
1 parent 0da3b17 commit 2c110c8

44 files changed

Lines changed: 3307 additions & 2992 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/main.go‎

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,21 +203,29 @@ var (
203203
Usage: "Number of recent chunks cached in memory (default 5000)",
204204
EnvVar: SWARM_ENV_STORE_CACHE_CAPACITY,
205205
}
206-
SwarmResourceMultihashFlag = cli.BoolFlag{
207-
Name: "multihash",
208-
Usage: "Determines how to interpret data for a resource update. If not present, data will be interpreted as raw, literal data that will be included in the resource",
206+
SwarmCompressedFlag = cli.BoolFlag{
207+
Name: "compressed",
208+
Usage: "Prints encryption keys in compressed form",
209209
}
210210
SwarmResourceNameFlag = cli.StringFlag{
211211
Name: "name",
212-
Usage: "User-defined name for the new resource",
212+
Usage: "User-defined name for the new resource, limited to 32 characters. If combined with topic, the resource will be a subtopic with this name",
213+
}
214+
SwarmResourceTopicFlag = cli.StringFlag{
215+
Name: "topic",
216+
Usage: "User-defined topic this resource is tracking, hex encoded. Limited to 64 hexadecimal characters",
213217
}
214218
SwarmResourceDataOnCreateFlag = cli.StringFlag{
215219
Name: "data",
216220
Usage: "Initializes the resource with the given hex-encoded data. Data must be prefixed by 0x",
217221
}
218-
SwarmCompressedFlag = cli.BoolFlag{
219-
Name: "compressed",
220-
Usage: "Prints encryption keys in compressed form",
222+
SwarmResourceManifestFlag = cli.StringFlag{
223+
Name: "manifest",
224+
Usage: "Refers to the resource through a manifest",
225+
}
226+
SwarmResourceUserFlag = cli.StringFlag{
227+
Name: "user",
228+
Usage: "Indicates the user who updates the resource",
221229
}
222230
)
223231

@@ -347,27 +355,53 @@ func init() {
347355
Action: resourceCreate,
348356
CustomHelpTemplate: helpTemplate,
349357
Name: "create",
350-
Usage: "creates a new Mutable Resource",
351-
ArgsUsage: "<frequency>",
352-
Description: "creates a new Mutable Resource",
353-
Flags: []cli.Flag{SwarmResourceNameFlag, SwarmResourceDataOnCreateFlag, SwarmResourceMultihashFlag},
358+
Usage: "creates and publishes a new Mutable Resource manifest",
359+
Description: `creates and publishes a new Mutable Resource manifest pointing to a specified user's updates about a particular topic.
360+
The resource topic can be built in the following ways:
361+
* use --topic to set the topic to an arbitrary binary hex string.
362+
* use --name to set the topic to a human-readable name.
363+
For example --name could be set to "profile-picture", meaning this Mutable Resource allows to get this user's current profile picture.
364+
* use both --topic and --name to create named subtopics.
365+
For example, --topic could be set to an Ethereum contract address and --name could be set to "comments", meaning
366+
the Mutable Resource tracks a discussion about that contract.
367+
The --user flag allows to have this manifest refer to a user other than yourself. If not specified,
368+
it will then default to your local account (--bzzaccount)`,
369+
Flags: []cli.Flag{SwarmResourceNameFlag, SwarmResourceTopicFlag, SwarmResourceUserFlag},
354370
},
355371
{
356372
Action: resourceUpdate,
357373
CustomHelpTemplate: helpTemplate,
358374
Name: "update",
359375
Usage: "updates the content of an existing Mutable Resource",
360-
ArgsUsage: "<Manifest Address or ENS domain> <0x Hex data>",
361-
Description: "updates the content of an existing Mutable Resource",
362-
Flags: []cli.Flag{SwarmResourceMultihashFlag},
376+
ArgsUsage: "<0x Hex data>",
377+
Description: `publishes a new update on the specified topic
378+
The resource topic can be built in the following ways:
379+
* use --topic to set the topic to an arbitrary binary hex string.
380+
* use --name to set the topic to a human-readable name.
381+
For example --name could be set to "profile-picture", meaning this Mutable Resource allows to get this user's current profile picture.
382+
* use both --topic and --name to create named subtopics.
383+
For example, --topic could be set to an Ethereum contract address and --name could be set to "comments", meaning
384+
the Mutable Resource tracks a discussion about that contract.
385+
386+
If you have a manifest, you can specify it with --manifest to refer to the resource,
387+
instead of using --topic / --name
388+
`,
389+
Flags: []cli.Flag{SwarmResourceManifestFlag, SwarmResourceNameFlag, SwarmResourceTopicFlag},
363390
},
364391
{
365392
Action: resourceInfo,
366393
CustomHelpTemplate: helpTemplate,
367394
Name: "info",
368395
Usage: "obtains information about an existing Mutable Resource",
369-
ArgsUsage: "<Manifest Address or ENS domain>",
370-
Description: "obtains information about an existing Mutable Resource",
396+
Description: `obtains information about an existing Mutable Resource
397+
The topic can be specified directly with the --topic flag as an hex string
398+
If no topic is specified, the default topic (zero) will be used
399+
The --name flag can be used to specify subtopics with a specific name.
400+
The --user flag allows to refer to a user other than yourself. If not specified,
401+
it will then default to your local account (--bzzaccount)
402+
If you have a manifest, you can specify it with --manifest instead of --topic / --name / ---user
403+
to refer to the resource`,
404+
Flags: []cli.Flag{SwarmResourceManifestFlag, SwarmResourceNameFlag, SwarmResourceTopicFlag, SwarmResourceUserFlag},
371405
},
372406
},
373407
},

‎cmd/swarm/mru.go‎

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ package main
1919

2020
import (
2121
"fmt"
22-
"strconv"
2322
"strings"
2423

24+
"github.com/ethereum/go-ethereum/common"
2525
"github.com/ethereum/go-ethereum/common/hexutil"
26+
"github.com/ethereum/go-ethereum/crypto"
2627

2728
"github.com/ethereum/go-ethereum/cmd/utils"
2829
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
@@ -34,62 +35,38 @@ func NewGenericSigner(ctx *cli.Context) mru.Signer {
3435
return mru.NewGenericSigner(getPrivKey(ctx))
3536
}
3637

38+
func getTopic(ctx *cli.Context) (topic mru.Topic) {
39+
var name = ctx.String(SwarmResourceNameFlag.Name)
40+
var relatedTopic = ctx.String(SwarmResourceTopicFlag.Name)
41+
var relatedTopicBytes []byte
42+
var err error
43+
44+
if relatedTopic != "" {
45+
relatedTopicBytes, err = hexutil.Decode(relatedTopic)
46+
if err != nil {
47+
utils.Fatalf("Error parsing topic: %s", err)
48+
}
49+
}
50+
51+
topic, err = mru.NewTopic(name, relatedTopicBytes)
52+
if err != nil {
53+
utils.Fatalf("Error parsing topic: %s", err)
54+
}
55+
return topic
56+
}
57+
3758
// swarm resource create <frequency> [--name <name>] [--data <0x Hexdata> [--multihash=false]]
3859
// swarm resource update <Manifest Address or ENS domain> <0x Hexdata> [--multihash=false]
3960
// swarm resource info <Manifest Address or ENS domain>
4061

4162
func resourceCreate(ctx *cli.Context) {
42-
args := ctx.Args()
43-
4463
var (
45-
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
46-
client = swarm.NewClient(bzzapi)
47-
multihash = ctx.Bool(SwarmResourceMultihashFlag.Name)
48-
initialData = ctx.String(SwarmResourceDataOnCreateFlag.Name)
49-
name = ctx.String(SwarmResourceNameFlag.Name)
64+
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
65+
client = swarm.NewClient(bzzapi)
5066
)
5167

52-
if len(args) < 1 {
53-
fmt.Println("Incorrect number of arguments")
54-
cli.ShowCommandHelpAndExit(ctx, "create", 1)
55-
return
56-
}
57-
signer := NewGenericSigner(ctx)
58-
frequency, err := strconv.ParseUint(args[0], 10, 64)
59-
if err != nil {
60-
fmt.Printf("Frequency formatting error: %s\n", err.Error())
61-
cli.ShowCommandHelpAndExit(ctx, "create", 1)
62-
return
63-
}
64-
65-
metadata := mru.ResourceMetadata{
66-
Name: name,
67-
Frequency: frequency,
68-
Owner: signer.Address(),
69-
}
70-
71-
var newResourceRequest *mru.Request
72-
if initialData != "" {
73-
initialDataBytes, err := hexutil.Decode(initialData)
74-
if err != nil {
75-
fmt.Printf("Error parsing data: %s\n", err.Error())
76-
cli.ShowCommandHelpAndExit(ctx, "create", 1)
77-
return
78-
}
79-
newResourceRequest, err = mru.NewCreateUpdateRequest(&metadata)
80-
if err != nil {
81-
utils.Fatalf("Error creating new resource request: %s", err)
82-
}
83-
newResourceRequest.SetData(initialDataBytes, multihash)
84-
if err = newResourceRequest.Sign(signer); err != nil {
85-
utils.Fatalf("Error signing resource update: %s", err.Error())
86-
}
87-
} else {
88-
newResourceRequest, err = mru.NewCreateRequest(&metadata)
89-
if err != nil {
90-
utils.Fatalf("Error creating new resource request: %s", err)
91-
}
92-
}
68+
newResourceRequest := mru.NewFirstRequest(getTopic(ctx))
69+
newResourceRequest.View.User = resourceGetUser(ctx)
9370

9471
manifestAddress, err := client.CreateResource(newResourceRequest)
9572
if err != nil {
@@ -104,32 +81,43 @@ func resourceUpdate(ctx *cli.Context) {
10481
args := ctx.Args()
10582

10683
var (
107-
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
108-
client = swarm.NewClient(bzzapi)
109-
multihash = ctx.Bool(SwarmResourceMultihashFlag.Name)
84+
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
85+
client = swarm.NewClient(bzzapi)
86+
manifestAddressOrDomain = ctx.String(SwarmResourceManifestFlag.Name)
11087
)
11188

112-
if len(args) < 2 {
89+
if len(args) < 1 {
11390
fmt.Println("Incorrect number of arguments")
11491
cli.ShowCommandHelpAndExit(ctx, "update", 1)
11592
return
11693
}
94+
11795
signer := NewGenericSigner(ctx)
118-
manifestAddressOrDomain := args[0]
119-
data, err := hexutil.Decode(args[1])
96+
97+
data, err := hexutil.Decode(args[0])
12098
if err != nil {
12199
utils.Fatalf("Error parsing data: %s", err.Error())
122100
return
123101
}
124102

103+
var updateRequest *mru.Request
104+
var query *mru.Query
105+
106+
if manifestAddressOrDomain == "" {
107+
query = new(mru.Query)
108+
query.User = signer.Address()
109+
query.Topic = getTopic(ctx)
110+
111+
}
112+
125113
// Retrieve resource status and metadata out of the manifest
126-
updateRequest, err := client.GetResourceMetadata(manifestAddressOrDomain)
114+
updateRequest, err = client.GetResourceMetadata(query, manifestAddressOrDomain)
127115
if err != nil {
128116
utils.Fatalf("Error retrieving resource status: %s", err.Error())
129117
}
130118

131119
// set the new data
132-
updateRequest.SetData(data, multihash)
120+
updateRequest.SetData(data)
133121

134122
// sign update
135123
if err = updateRequest.Sign(signer); err != nil {
@@ -146,17 +134,19 @@ func resourceUpdate(ctx *cli.Context) {
146134

147135
func resourceInfo(ctx *cli.Context) {
148136
var (
149-
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
150-
client = swarm.NewClient(bzzapi)
137+
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
138+
client = swarm.NewClient(bzzapi)
139+
manifestAddressOrDomain = ctx.String(SwarmResourceManifestFlag.Name)
151140
)
152-
args := ctx.Args()
153-
if len(args) < 1 {
154-
fmt.Println("Incorrect number of arguments.")
155-
cli.ShowCommandHelpAndExit(ctx, "info", 1)
156-
return
141+
142+
var query *mru.Query
143+
if manifestAddressOrDomain == "" {
144+
query = new(mru.Query)
145+
query.Topic = getTopic(ctx)
146+
query.User = resourceGetUser(ctx)
157147
}
158-
manifestAddressOrDomain := args[0]
159-
metadata, err := client.GetResourceMetadata(manifestAddressOrDomain)
148+
149+
metadata, err := client.GetResourceMetadata(query, manifestAddressOrDomain)
160150
if err != nil {
161151
utils.Fatalf("Error retrieving resource metadata: %s", err.Error())
162152
return
@@ -167,3 +157,16 @@ func resourceInfo(ctx *cli.Context) {
167157
}
168158
fmt.Println(string(encodedMetadata))
169159
}
160+
161+
func resourceGetUser(ctx *cli.Context) common.Address {
162+
var user = ctx.String(SwarmResourceUserFlag.Name)
163+
if user != "" {
164+
return common.HexToAddress(user)
165+
}
166+
pk := getPrivKey(ctx)
167+
if pk == nil {
168+
utils.Fatalf("Cannot read private key. Must specify --user or --bzzaccount")
169+
}
170+
return crypto.PubkeyToAddress(pk.PublicKey)
171+
172+
}

0 commit comments

Comments
 (0)