cmd/bzzd: swarm daemon fixes (#3359)

* cmd/bzzd: add missing p2p/discovery flags
* cmd/bzzd: fix two bugs crashing bzzd if bootnodes flag given
* cmd/bzzd: make no swap default, renamed flag bzznoswap->bzzswap
* internal/web3ext: correct methods for bzz IPC module
* cmd/bzzd: ethapi param not mandatory. Warning if no blockchain
* cmd/bzzd: correct default IPC modules in help string
* cmd/utils: fix help description for networkId - add Ropsten
* cmd/bzz, swarm/api, swarm/network: add swarm networkId flag
* cmd/bzzd: change nosync flag to sync and BootTFlag
pull/3361/head
Viktor Trón 8 years ago committed by Felix Lange
parent 8d0108fc5d
commit e949a2ed2f
  1. 51
      cmd/bzzd/main.go
  2. 2
      cmd/utils/flags.go
  3. 16
      internal/web3ext/web3ext.go
  4. 4
      swarm/api/config.go
  5. 7
      swarm/api/config_test.go
  6. 18
      swarm/network/protocol.go
  7. 4
      swarm/swarm.go

@ -23,6 +23,7 @@ import (
"os" "os"
"runtime" "runtime"
"strconv" "strconv"
"strings"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/cmd/utils"
@ -38,6 +39,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/swarm" "github.com/ethereum/go-ethereum/swarm"
bzzapi "github.com/ethereum/go-ethereum/swarm/api" bzzapi "github.com/ethereum/go-ethereum/swarm/api"
"github.com/ethereum/go-ethereum/swarm/network"
"gopkg.in/urfave/cli.v1" "gopkg.in/urfave/cli.v1"
) )
@ -61,17 +63,22 @@ var (
Name: "bzzport", Name: "bzzport",
Usage: "Swarm local http api port", Usage: "Swarm local http api port",
} }
SwarmNetworkIdFlag = cli.IntFlag{
Name: "bzznetworkid",
Usage: "Network identifier (integer, default 322=swarm testnet)",
Value: network.NetworkId,
}
SwarmConfigPathFlag = cli.StringFlag{ SwarmConfigPathFlag = cli.StringFlag{
Name: "bzzconfig", Name: "bzzconfig",
Usage: "Swarm config file path (datadir/bzz)", Usage: "Swarm config file path (datadir/bzz)",
} }
SwarmSwapDisabled = cli.BoolFlag{ SwarmSwapEnabled = cli.BoolFlag{
Name: "bzznoswap", Name: "swap",
Usage: "Swarm SWAP disabled (default false)", Usage: "Swarm SWAP enabled (default false)",
} }
SwarmSyncDisabled = cli.BoolFlag{ SwarmSyncEnabled = cli.BoolTFlag{
Name: "bzznosync", Name: "sync",
Usage: "Swarm Syncing disabled (default false)", Usage: "Swarm Syncing enabled (default true)",
} }
EthAPI = cli.StringFlag{ EthAPI = cli.StringFlag{
Name: "ethapi", Name: "ethapi",
@ -86,6 +93,7 @@ func init() {
// Override flag defaults so bzzd can run alongside geth. // Override flag defaults so bzzd can run alongside geth.
utils.ListenPortFlag.Value = 30399 utils.ListenPortFlag.Value = 30399
utils.IPCPathFlag.Value = utils.DirectoryString{Value: "bzzd.ipc"} utils.IPCPathFlag.Value = utils.DirectoryString{Value: "bzzd.ipc"}
utils.IPCApiFlag.Value = "admin, bzz, chequebook, debug, rpc, web3"
// Set up the cli app. // Set up the cli app.
app.Commands = nil app.Commands = nil
@ -96,21 +104,24 @@ func init() {
utils.BootnodesFlag, utils.BootnodesFlag,
utils.KeyStoreDirFlag, utils.KeyStoreDirFlag,
utils.ListenPortFlag, utils.ListenPortFlag,
utils.NoDiscoverFlag,
utils.DiscoveryV5Flag,
utils.NetrestrictFlag, utils.NetrestrictFlag,
utils.MaxPeersFlag,
utils.NATFlag,
utils.NodeKeyFileFlag, utils.NodeKeyFileFlag,
utils.NodeKeyHexFlag, utils.NodeKeyHexFlag,
utils.MaxPeersFlag,
utils.NATFlag,
utils.IPCDisabledFlag, utils.IPCDisabledFlag,
utils.IPCApiFlag, utils.IPCApiFlag,
utils.IPCPathFlag, utils.IPCPathFlag,
// bzzd-specific flags // bzzd-specific flags
EthAPI, EthAPI,
SwarmConfigPathFlag, SwarmConfigPathFlag,
SwarmSwapDisabled, SwarmSwapEnabled,
SwarmSyncDisabled, SwarmSyncEnabled,
SwarmPortFlag, SwarmPortFlag,
SwarmAccountFlag, SwarmAccountFlag,
SwarmNetworkIdFlag,
ChequebookAddrFlag, ChequebookAddrFlag,
} }
app.Flags = append(app.Flags, debug.Flags...) app.Flags = append(app.Flags, debug.Flags...)
@ -138,7 +149,8 @@ func bzzd(ctx *cli.Context) error {
// Add bootnodes as initial peers. // Add bootnodes as initial peers.
if ctx.GlobalIsSet(utils.BootnodesFlag.Name) { if ctx.GlobalIsSet(utils.BootnodesFlag.Name) {
injectBootnodes(stack.Server(), ctx.GlobalStringSlice(utils.BootnodesFlag.Name)) bootnodes := strings.Split(ctx.GlobalString(utils.BootnodesFlag.Name), ",")
injectBootnodes(stack.Server(), bootnodes)
} else { } else {
injectBootnodes(stack.Server(), defaultBootnodes) injectBootnodes(stack.Server(), defaultBootnodes)
} }
@ -155,7 +167,7 @@ func registerBzzService(ctx *cli.Context, stack *node.Node) {
if bzzdir == "" { if bzzdir == "" {
bzzdir = stack.InstanceDir() bzzdir = stack.InstanceDir()
} }
bzzconfig, err := bzzapi.NewConfig(bzzdir, chbookaddr, prvkey) bzzconfig, err := bzzapi.NewConfig(bzzdir, chbookaddr, prvkey, ctx.GlobalUint64(SwarmNetworkIdFlag.Name))
if err != nil { if err != nil {
utils.Fatalf("unable to configure swarm: %v", err) utils.Fatalf("unable to configure swarm: %v", err)
} }
@ -163,16 +175,18 @@ func registerBzzService(ctx *cli.Context, stack *node.Node) {
if len(bzzport) > 0 { if len(bzzport) > 0 {
bzzconfig.Port = bzzport bzzconfig.Port = bzzport
} }
swapEnabled := !ctx.GlobalBool(SwarmSwapDisabled.Name) swapEnabled := ctx.GlobalBool(SwarmSwapEnabled.Name)
syncEnabled := !ctx.GlobalBool(SwarmSyncDisabled.Name) syncEnabled := ctx.GlobalBoolT(SwarmSyncEnabled.Name)
ethapi := ctx.GlobalString(EthAPI.Name) ethapi := ctx.GlobalString(EthAPI.Name)
if ethapi == "" {
utils.Fatalf("Option %q must not be empty", EthAPI.Name)
}
boot := func(ctx *node.ServiceContext) (node.Service, error) { boot := func(ctx *node.ServiceContext) (node.Service, error) {
client, err := ethclient.Dial(ethapi) var client *ethclient.Client
if ethapi == "" {
err = fmt.Errorf("use ethapi flag to connect to a an eth client and talk to the blockchain")
} else {
client, err = ethclient.Dial(ethapi)
}
if err != nil { if err != nil {
utils.Fatalf("Can't connect: %v", err) utils.Fatalf("Can't connect: %v", err)
} }
@ -241,6 +255,7 @@ func injectBootnodes(srv *p2p.Server, nodes []string) {
n, err := discover.ParseNode(url) n, err := discover.ParseNode(url)
if err != nil { if err != nil {
glog.Errorf("invalid bootnode %q", err) glog.Errorf("invalid bootnode %q", err)
continue
} }
srv.AddPeer(n) srv.AddPeer(n)
} }

@ -116,7 +116,7 @@ var (
} }
NetworkIdFlag = cli.IntFlag{ NetworkIdFlag = cli.IntFlag{
Name: "networkid", Name: "networkid",
Usage: "Network identifier (integer, 0=Olympic, 1=Frontier, 2=Morden)", Usage: "Network identifier (integer, 0=Olympic (disused), 1=Frontier, 2=Morden (disused), 3=Ropsten)",
Value: eth.NetworkId, Value: eth.NetworkId,
} }
OlympicFlag = cli.BoolFlag{ OlympicFlag = cli.BoolFlag{

@ -37,12 +37,6 @@ web3._extend({
property: 'bzz', property: 'bzz',
methods: methods:
[ [
new web3._extend.Method({
name: 'blockNetworkRead',
call: 'bzz_blockNetworkRead',
params: 1,
inputFormatter: [null]
}),
new web3._extend.Method({ new web3._extend.Method({
name: 'syncEnabled', name: 'syncEnabled',
call: 'bzz_syncEnabled', call: 'bzz_syncEnabled',
@ -68,17 +62,11 @@ web3._extend({
inputFormatter: [null, null] inputFormatter: [null, null]
}), }),
new web3._extend.Method({ new web3._extend.Method({
name: 'retrieve', name: 'resolve',
call: 'bzz_retrieve', call: 'bzz_resolve',
params: 1, params: 1,
inputFormatter: [null] inputFormatter: [null]
}), }),
new web3._extend.Method({
name: 'store',
call: 'bzz_store',
params: 2,
inputFormatter: [null]
}),
new web3._extend.Method({ new web3._extend.Method({
name: 'get', name: 'get',
call: 'bzz_get', call: 'bzz_get',

@ -54,11 +54,12 @@ type Config struct {
PublicKey string PublicKey string
BzzKey string BzzKey string
EnsRoot common.Address EnsRoot common.Address
NetworkId uint64
} }
// config is agnostic to where private key is coming from // config is agnostic to where private key is coming from
// so managing accounts is outside swarm and left to wrappers // so managing accounts is outside swarm and left to wrappers
func NewConfig(path string, contract common.Address, prvKey *ecdsa.PrivateKey) (self *Config, err error) { func NewConfig(path string, contract common.Address, prvKey *ecdsa.PrivateKey, networkId uint64) (self *Config, err error) {
address := crypto.PubkeyToAddress(prvKey.PublicKey) // default beneficiary address address := crypto.PubkeyToAddress(prvKey.PublicKey) // default beneficiary address
dirpath := filepath.Join(path, "bzz-"+common.Bytes2Hex(address.Bytes())) dirpath := filepath.Join(path, "bzz-"+common.Bytes2Hex(address.Bytes()))
err = os.MkdirAll(dirpath, os.ModePerm) err = os.MkdirAll(dirpath, os.ModePerm)
@ -82,6 +83,7 @@ func NewConfig(path string, contract common.Address, prvKey *ecdsa.PrivateKey) (
PublicKey: pubkeyhex, PublicKey: pubkeyhex,
BzzKey: keyhex, BzzKey: keyhex,
EnsRoot: toyNetEnsRoot, EnsRoot: toyNetEnsRoot,
NetworkId: networkId,
} }
data, err = ioutil.ReadFile(confpath) data, err = ioutil.ReadFile(confpath)
if err != nil { if err != nil {

@ -83,7 +83,8 @@ var (
"Port": "8500", "Port": "8500",
"PublicKey": "0x045f5cfd26692e48d0017d380349bcf50982488bc11b5145f3ddf88b24924299048450542d43527fbe29a5cb32f38d62755393ac002e6bfdd71b8d7ba725ecd7a3", "PublicKey": "0x045f5cfd26692e48d0017d380349bcf50982488bc11b5145f3ddf88b24924299048450542d43527fbe29a5cb32f38d62755393ac002e6bfdd71b8d7ba725ecd7a3",
"BzzKey": "0xe861964402c0b78e2d44098329b8545726f215afa737d803714a4338552fcb81", "BzzKey": "0xe861964402c0b78e2d44098329b8545726f215afa737d803714a4338552fcb81",
"EnsRoot": "0xd344889e0be3e9ef6c26b0f60ef66a32e83c1b69" "EnsRoot": "0xd344889e0be3e9ef6c26b0f60ef66a32e83c1b69",
"NetworkId": 323
}` }`
) )
@ -95,7 +96,7 @@ func TestConfigWriteRead(t *testing.T) {
defer os.RemoveAll(tmp) defer os.RemoveAll(tmp)
prvkey := crypto.ToECDSA(common.Hex2Bytes(hexprvkey)) prvkey := crypto.ToECDSA(common.Hex2Bytes(hexprvkey))
orig, err := NewConfig(tmp, common.Address{}, prvkey) orig, err := NewConfig(tmp, common.Address{}, prvkey, 323)
if err != nil { if err != nil {
t.Fatalf("expected no error, got %v", err) t.Fatalf("expected no error, got %v", err)
} }
@ -109,7 +110,7 @@ func TestConfigWriteRead(t *testing.T) {
t.Fatalf("default config mismatch:\nexpected: %v\ngot: %v", exp, string(data)) t.Fatalf("default config mismatch:\nexpected: %v\ngot: %v", exp, string(data))
} }
conf, err := NewConfig(tmp, common.Address{}, prvkey) conf, err := NewConfig(tmp, common.Address{}, prvkey, 323)
if err != nil { if err != nil {
t.Fatalf("expected no error, got %v", err) t.Fatalf("expected no error, got %v", err)
} }

@ -95,6 +95,7 @@ type bzz struct {
errors *errs.Errors // errors table errors *errs.Errors // errors table
backend chequebook.Backend backend chequebook.Backend
lastActive time.Time lastActive time.Time
NetworkId uint64
swap *swap.Swap // swap instance for the peer connection swap *swap.Swap // swap instance for the peer connection
swapParams *bzzswap.SwapParams // swap settings both local and remote swapParams *bzzswap.SwapParams // swap settings both local and remote
@ -126,7 +127,7 @@ on each peer connection
The Run function of the Bzz protocol class creates a bzz instance The Run function of the Bzz protocol class creates a bzz instance
which will represent the peer for the swarm hive and all peer-aware components which will represent the peer for the swarm hive and all peer-aware components
*/ */
func Bzz(cloud StorageHandler, backend chequebook.Backend, hive *Hive, dbaccess *DbAccess, sp *bzzswap.SwapParams, sy *SyncParams) (p2p.Protocol, error) { func Bzz(cloud StorageHandler, backend chequebook.Backend, hive *Hive, dbaccess *DbAccess, sp *bzzswap.SwapParams, sy *SyncParams, networkId uint64) (p2p.Protocol, error) {
// a single global request db is created for all peer connections // a single global request db is created for all peer connections
// this is to persist delivery backlog and aid syncronisation // this is to persist delivery backlog and aid syncronisation
@ -134,13 +135,15 @@ func Bzz(cloud StorageHandler, backend chequebook.Backend, hive *Hive, dbaccess
if err != nil { if err != nil {
return p2p.Protocol{}, fmt.Errorf("error setting up request db: %v", err) return p2p.Protocol{}, fmt.Errorf("error setting up request db: %v", err)
} }
if networkId == 0 {
networkId = NetworkId
}
return p2p.Protocol{ return p2p.Protocol{
Name: "bzz", Name: "bzz",
Version: Version, Version: Version,
Length: ProtocolLength, Length: ProtocolLength,
Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error { Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
return run(requestDb, cloud, backend, hive, dbaccess, sp, sy, p, rw) return run(requestDb, cloud, backend, hive, dbaccess, sp, sy, networkId, p, rw)
}, },
}, nil }, nil
} }
@ -157,7 +160,7 @@ the main protocol loop that
* whenever the loop terminates, the peer will disconnect with Subprotocol error * whenever the loop terminates, the peer will disconnect with Subprotocol error
* whenever handlers return an error the loop terminates * whenever handlers return an error the loop terminates
*/ */
func run(requestDb *storage.LDBDatabase, depo StorageHandler, backend chequebook.Backend, hive *Hive, dbaccess *DbAccess, sp *bzzswap.SwapParams, sy *SyncParams, p *p2p.Peer, rw p2p.MsgReadWriter) (err error) { func run(requestDb *storage.LDBDatabase, depo StorageHandler, backend chequebook.Backend, hive *Hive, dbaccess *DbAccess, sp *bzzswap.SwapParams, sy *SyncParams, networkId uint64, p *p2p.Peer, rw p2p.MsgReadWriter) (err error) {
self := &bzz{ self := &bzz{
storage: depo, storage: depo,
@ -175,6 +178,7 @@ func run(requestDb *storage.LDBDatabase, depo StorageHandler, backend chequebook
syncParams: sy, syncParams: sy,
swapEnabled: hive.swapEnabled, swapEnabled: hive.swapEnabled,
syncEnabled: true, syncEnabled: true,
NetworkId: networkId,
} }
// handle handshake // handle handshake
@ -340,7 +344,7 @@ func (self *bzz) handleStatus() (err error) {
Version: uint64(Version), Version: uint64(Version),
ID: "honey", ID: "honey",
Addr: self.selfAddr(), Addr: self.selfAddr(),
NetworkId: uint64(NetworkId), NetworkId: uint64(self.NetworkId),
Swap: &bzzswap.SwapProfile{ Swap: &bzzswap.SwapProfile{
Profile: self.swapParams.Profile, Profile: self.swapParams.Profile,
PayProfile: self.swapParams.PayProfile, PayProfile: self.swapParams.PayProfile,
@ -372,8 +376,8 @@ func (self *bzz) handleStatus() (err error) {
return self.protoError(ErrDecode, " %v: %v", msg, err) return self.protoError(ErrDecode, " %v: %v", msg, err)
} }
if status.NetworkId != NetworkId { if status.NetworkId != self.NetworkId {
return self.protoError(ErrNetworkIdMismatch, "%d (!= %d)", status.NetworkId, NetworkId) return self.protoError(ErrNetworkIdMismatch, "%d (!= %d)", status.NetworkId, self.NetworkId)
} }
if Version != status.Version { if Version != status.Version {

@ -209,7 +209,7 @@ func (self *Swarm) Stop() error {
// implements the node.Service interface // implements the node.Service interface
func (self *Swarm) Protocols() []p2p.Protocol { func (self *Swarm) Protocols() []p2p.Protocol {
proto, err := network.Bzz(self.depo, self.backend, self.hive, self.dbAccess, self.config.Swap, self.config.SyncParams) proto, err := network.Bzz(self.depo, self.backend, self.hive, self.dbAccess, self.config.Swap, self.config.SyncParams, self.config.NetworkId)
if err != nil { if err != nil {
return nil return nil
} }
@ -279,7 +279,7 @@ func NewLocalSwarm(datadir, port string) (self *Swarm, err error) {
return return
} }
config, err := api.NewConfig(datadir, common.Address{}, prvKey) config, err := api.NewConfig(datadir, common.Address{}, prvKey, network.NetworkId)
if err != nil { if err != nil {
return return
} }

Loading…
Cancel
Save