cmd/bzz, swarm/api, swarm/network: add swarm networkId flag

This commit is contained in:
zelig 2016-11-28 10:06:20 +01:00
parent f77cc937c5
commit 00813e4faf
5 changed files with 32 additions and 20 deletions

View file

@ -39,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"
) )
@ -62,16 +63,21 @@ 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)",
} }
SwarmSwapEnabled = cli.BoolFlag{ SwarmSwapEnabled = cli.BoolFlag{
Name: "bzzswap", Name: "swap",
Usage: "Swarm SWAP enabled (default false)", Usage: "Swarm SWAP enabled (default false)",
} }
SwarmSyncDisabled = cli.BoolFlag{ SwarmSyncDisabled = cli.BoolFlag{
Name: "bzznosync", Name: "nosync",
Usage: "Swarm Syncing disabled (default false)", Usage: "Swarm Syncing disabled (default false)",
} }
EthAPI = cli.StringFlag{ EthAPI = cli.StringFlag{
@ -115,6 +121,7 @@ func init() {
SwarmSyncDisabled, SwarmSyncDisabled,
SwarmPortFlag, SwarmPortFlag,
SwarmAccountFlag, SwarmAccountFlag,
SwarmNetworkIdFlag,
ChequebookAddrFlag, ChequebookAddrFlag,
} }
app.Flags = append(app.Flags, debug.Flags...) app.Flags = append(app.Flags, debug.Flags...)
@ -160,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)
} }
@ -172,15 +179,13 @@ func registerBzzService(ctx *cli.Context, stack *node.Node) {
syncEnabled := !ctx.GlobalBool(SwarmSyncDisabled.Name) syncEnabled := !ctx.GlobalBool(SwarmSyncDisabled.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) {
var client *ethclient.Client
if ethapi == "" { if ethapi == "" {
err = fmt.Errorf("use ethapi flag to connect to a an eth client and talk to the blockchain") err = fmt.Errorf("use ethapi flag to connect to a an eth client and talk to the blockchain")
} else { } else {
client, err := ethclient.Dial(ethapi) 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)

View file

@ -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 {

View file

@ -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)
} }

View file

@ -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 {

View file

@ -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
} }