Skip to content

Commit b536460

Browse files
authored
Merge pull request #17231 from ethersphere/develop
swarm: client-side MRU signatures ; BMT fixes ; network simulation tests
2 parents afd8b84 + 14bdcde commit b536460

50 files changed

Lines changed: 6864 additions & 1940 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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ var (
182182
Usage: "Number of recent chunks cached in memory (default 5000)",
183183
EnvVar: SWARM_ENV_STORE_CACHE_CAPACITY,
184184
}
185+
SwarmResourceMultihashFlag = cli.BoolFlag{
186+
Name: "multihash",
187+
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",
188+
}
189+
SwarmResourceNameFlag = cli.StringFlag{
190+
Name: "name",
191+
Usage: "User-defined name for the new resource",
192+
}
193+
SwarmResourceDataOnCreateFlag = cli.StringFlag{
194+
Name: "data",
195+
Usage: "Initializes the resource with the given hex-encoded data. Data must be prefixed by 0x",
196+
}
185197
)
186198

187199
//declare a few constant error messages, useful for later error check comparisons in test
@@ -190,6 +202,15 @@ var (
190202
SWARM_ERR_SWAP_SET_NO_API = "SWAP is enabled but --swap-api is not set"
191203
)
192204

205+
// this help command gets added to any subcommand that does not define it explicitly
206+
var defaultSubcommandHelp = cli.Command{
207+
Action: func(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "", 1) },
208+
CustomHelpTemplate: helpTemplate,
209+
Name: "help",
210+
Usage: "shows this help",
211+
Hidden: true,
212+
}
213+
193214
var defaultNodeConfig = node.DefaultConfig
194215

195216
// This init function sets defaults so cmd/swarm can run alongside geth.
@@ -226,6 +247,41 @@ func init() {
226247
Flags: []cli.Flag{SwarmEncryptedFlag},
227248
Description: "uploads a file or directory to swarm using the HTTP API and prints the root hash",
228249
},
250+
{
251+
CustomHelpTemplate: helpTemplate,
252+
Name: "resource",
253+
Usage: "(Advanced) Create and update Mutable Resources",
254+
ArgsUsage: "<create|update|info>",
255+
Description: "Works with Mutable Resource Updates",
256+
Subcommands: []cli.Command{
257+
{
258+
Action: resourceCreate,
259+
CustomHelpTemplate: helpTemplate,
260+
Name: "create",
261+
Usage: "creates a new Mutable Resource",
262+
ArgsUsage: "<frequency>",
263+
Description: "creates a new Mutable Resource",
264+
Flags: []cli.Flag{SwarmResourceNameFlag, SwarmResourceDataOnCreateFlag, SwarmResourceMultihashFlag},
265+
},
266+
{
267+
Action: resourceUpdate,
268+
CustomHelpTemplate: helpTemplate,
269+
Name: "update",
270+
Usage: "updates the content of an existing Mutable Resource",
271+
ArgsUsage: "<Manifest Address or ENS domain> <0x Hex data>",
272+
Description: "updates the content of an existing Mutable Resource",
273+
Flags: []cli.Flag{SwarmResourceMultihashFlag},
274+
},
275+
{
276+
Action: resourceInfo,
277+
CustomHelpTemplate: helpTemplate,
278+
Name: "info",
279+
Usage: "obtains information about an existing Mutable Resource",
280+
ArgsUsage: "<Manifest Address or ENS domain>",
281+
Description: "obtains information about an existing Mutable Resource",
282+
},
283+
},
284+
},
229285
{
230286
Action: list,
231287
CustomHelpTemplate: helpTemplate,
@@ -377,6 +433,11 @@ pv(1) tool to get a progress bar:
377433
// See config.go
378434
DumpConfigCommand,
379435
}
436+
437+
// append a hidden help subcommand to all commands that have subcommands
438+
// if a help command was already defined above, that one will take precedence.
439+
addDefaultHelpSubcommands(app.Commands)
440+
380441
sort.Sort(cli.CommandsByName(app.Commands))
381442

382443
app.Flags = []cli.Flag{
@@ -549,6 +610,26 @@ func getAccount(bzzaccount string, ctx *cli.Context, stack *node.Node) *ecdsa.Pr
549610
return decryptStoreAccount(ks, bzzaccount, utils.MakePasswordList(ctx))
550611
}
551612

613+
// getPrivKey returns the private key of the specified bzzaccount
614+
// Used only by client commands, such as `resource`
615+
func getPrivKey(ctx *cli.Context) *ecdsa.PrivateKey {
616+
// booting up the swarm node just as we do in bzzd action
617+
bzzconfig, err := buildConfig(ctx)
618+
if err != nil {
619+
utils.Fatalf("unable to configure swarm: %v", err)
620+
}
621+
cfg := defaultNodeConfig
622+
if _, err := os.Stat(bzzconfig.Path); err == nil {
623+
cfg.DataDir = bzzconfig.Path
624+
}
625+
utils.SetNodeConfig(ctx, &cfg)
626+
stack, err := node.New(&cfg)
627+
if err != nil {
628+
utils.Fatalf("can't create node: %v", err)
629+
}
630+
return getAccount(bzzconfig.BzzAccount, ctx, stack)
631+
}
632+
552633
func decryptStoreAccount(ks *keystore.KeyStore, account string, passwords []string) *ecdsa.PrivateKey {
553634
var a accounts.Account
554635
var err error
@@ -613,3 +694,16 @@ func injectBootnodes(srv *p2p.Server, nodes []string) {
613694
srv.AddPeer(n)
614695
}
615696
}
697+
698+
// addDefaultHelpSubcommand scans through defined CLI commands and adds
699+
// a basic help subcommand to each
700+
// if a help command is already defined, it will take precedence over the default.
701+
func addDefaultHelpSubcommands(commands []cli.Command) {
702+
for i := range commands {
703+
cmd := &commands[i]
704+
if cmd.Subcommands != nil {
705+
cmd.Subcommands = append(cmd.Subcommands, defaultSubcommandHelp)
706+
addDefaultHelpSubcommands(cmd.Subcommands)
707+
}
708+
}
709+
}

‎cmd/swarm/mru.go‎

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Command resource allows the user to create and update signed mutable resource updates
18+
package main
19+
20+
import (
21+
"fmt"
22+
"strconv"
23+
"strings"
24+
25+
"github.com/ethereum/go-ethereum/common/hexutil"
26+
27+
"github.com/ethereum/go-ethereum/cmd/utils"
28+
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
29+
"github.com/ethereum/go-ethereum/swarm/storage/mru"
30+
"gopkg.in/urfave/cli.v1"
31+
)
32+
33+
func NewGenericSigner(ctx *cli.Context) mru.Signer {
34+
return mru.NewGenericSigner(getPrivKey(ctx))
35+
}
36+
37+
// swarm resource create <frequency> [--name <name>] [--data <0x Hexdata> [--multihash=false]]
38+
// swarm resource update <Manifest Address or ENS domain> <0x Hexdata> [--multihash=false]
39+
// swarm resource info <Manifest Address or ENS domain>
40+
41+
func resourceCreate(ctx *cli.Context) {
42+
args := ctx.Args()
43+
44+
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)
50+
)
51+
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+
}
93+
94+
manifestAddress, err := client.CreateResource(newResourceRequest)
95+
if err != nil {
96+
utils.Fatalf("Error creating resource: %s", err.Error())
97+
return
98+
}
99+
fmt.Println(manifestAddress) // output manifest address to the user in a single line (useful for other commands to pick up)
100+
101+
}
102+
103+
func resourceUpdate(ctx *cli.Context) {
104+
args := ctx.Args()
105+
106+
var (
107+
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
108+
client = swarm.NewClient(bzzapi)
109+
multihash = ctx.Bool(SwarmResourceMultihashFlag.Name)
110+
)
111+
112+
if len(args) < 2 {
113+
fmt.Println("Incorrect number of arguments")
114+
cli.ShowCommandHelpAndExit(ctx, "update", 1)
115+
return
116+
}
117+
signer := NewGenericSigner(ctx)
118+
manifestAddressOrDomain := args[0]
119+
data, err := hexutil.Decode(args[1])
120+
if err != nil {
121+
utils.Fatalf("Error parsing data: %s", err.Error())
122+
return
123+
}
124+
125+
// Retrieve resource status and metadata out of the manifest
126+
updateRequest, err := client.GetResourceMetadata(manifestAddressOrDomain)
127+
if err != nil {
128+
utils.Fatalf("Error retrieving resource status: %s", err.Error())
129+
}
130+
131+
// set the new data
132+
updateRequest.SetData(data, multihash)
133+
134+
// sign update
135+
if err = updateRequest.Sign(signer); err != nil {
136+
utils.Fatalf("Error signing resource update: %s", err.Error())
137+
}
138+
139+
// post update
140+
err = client.UpdateResource(updateRequest)
141+
if err != nil {
142+
utils.Fatalf("Error updating resource: %s", err.Error())
143+
return
144+
}
145+
}
146+
147+
func resourceInfo(ctx *cli.Context) {
148+
var (
149+
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
150+
client = swarm.NewClient(bzzapi)
151+
)
152+
args := ctx.Args()
153+
if len(args) < 1 {
154+
fmt.Println("Incorrect number of arguments.")
155+
cli.ShowCommandHelpAndExit(ctx, "info", 1)
156+
return
157+
}
158+
manifestAddressOrDomain := args[0]
159+
metadata, err := client.GetResourceMetadata(manifestAddressOrDomain)
160+
if err != nil {
161+
utils.Fatalf("Error retrieving resource metadata: %s", err.Error())
162+
return
163+
}
164+
encodedMetadata, err := metadata.MarshalJSON()
165+
if err != nil {
166+
utils.Fatalf("Error encoding metadata to JSON for display:%s", err)
167+
}
168+
fmt.Println(string(encodedMetadata))
169+
}

‎p2p/simulations/adapters/inproc.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,13 @@ func (sn *SimNode) Stop() error {
296296
return sn.node.Stop()
297297
}
298298

299+
// Service returns a running service by name
300+
func (sn *SimNode) Service(name string) node.Service {
301+
sn.lock.RLock()
302+
defer sn.lock.RUnlock()
303+
return sn.running[name]
304+
}
305+
299306
// Services returns a copy of the underlying services
300307
func (sn *SimNode) Services() []node.Service {
301308
sn.lock.RLock()
@@ -307,6 +314,17 @@ func (sn *SimNode) Services() []node.Service {
307314
return services
308315
}
309316

317+
// ServiceMap returns a map by names of the underlying services
318+
func (sn *SimNode) ServiceMap() map[string]node.Service {
319+
sn.lock.RLock()
320+
defer sn.lock.RUnlock()
321+
services := make(map[string]node.Service, len(sn.running))
322+
for name, service := range sn.running {
323+
services[name] = service
324+
}
325+
return services
326+
}
327+
310328
// Server returns the underlying p2p.Server
311329
func (sn *SimNode) Server() *p2p.Server {
312330
return sn.node.Server()

0 commit comments

Comments
 (0)