diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-06-30 00:09:01 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-30 00:09:01 +0800 |
commit | 41318f3776f3ecc09c2f482070bddc7433e00b14 (patch) | |
tree | ba33155b697da13a9585e97ce73a9d424dc9d432 /cmd | |
parent | 65b96dc4b95722df81d664d22fd06550a4c0b8a8 (diff) | |
parent | f3359d5e585eef09648c7a06a6d3911704f61e73 (diff) | |
download | dexon-41318f3776f3ecc09c2f482070bddc7433e00b14.tar.gz dexon-41318f3776f3ecc09c2f482070bddc7433e00b14.tar.zst dexon-41318f3776f3ecc09c2f482070bddc7433e00b14.zip |
Merge pull request #14646 from ethersphere/swarm-ens-api
cmd/swarm: Support using Mainnet for resolving ENS names
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/swarm/main.go | 105 |
1 files changed, 97 insertions, 8 deletions
diff --git a/cmd/swarm/main.go b/cmd/swarm/main.go index 71149c310..f0680c487 100644 --- a/cmd/swarm/main.go +++ b/cmd/swarm/main.go @@ -17,21 +17,25 @@ package main import ( + "context" "crypto/ecdsa" "fmt" "io/ioutil" + "math/big" "os" "os/signal" "runtime" "strconv" "strings" "syscall" + "time" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/console" + "github.com/ethereum/go-ethereum/contracts/ens" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/internal/debug" @@ -40,6 +44,7 @@ import ( "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/swarm" bzzapi "github.com/ethereum/go-ethereum/swarm/api" "gopkg.in/urfave/cli.v1" @@ -87,15 +92,27 @@ var ( Name: "swap", Usage: "Swarm SWAP enabled (default false)", } + SwarmSwapAPIFlag = cli.StringFlag{ + Name: "swap-api", + Usage: "URL of the Ethereum API provider to use to settle SWAP payments", + } SwarmSyncEnabledFlag = cli.BoolTFlag{ Name: "sync", Usage: "Swarm Syncing enabled (default true)", } EthAPIFlag = cli.StringFlag{ Name: "ethapi", - Usage: "URL of the Ethereum API provider", + Usage: "DEPRECATED: please use --ens-api and --swap-api", + } + EnsAPIFlag = cli.StringFlag{ + Name: "ens-api", + Usage: "URL of the Ethereum API provider to use for ENS record lookups", Value: node.DefaultIPCEndpoint("geth"), } + EnsAddrFlag = cli.StringFlag{ + Name: "ens-addr", + Usage: "ENS contract address (default is detected as testnet or mainnet using --ens-api)", + } SwarmApiFlag = cli.StringFlag{ Name: "bzzapi", Usage: "Swarm HTTP endpoint", @@ -250,8 +267,11 @@ Cleans database of corrupted entries. // bzzd-specific flags CorsStringFlag, EthAPIFlag, + EnsAPIFlag, + EnsAddrFlag, SwarmConfigPathFlag, SwarmSwapEnabledFlag, + SwarmSwapAPIFlag, SwarmSyncEnabledFlag, SwarmListenAddrFlag, SwarmPortFlag, @@ -333,6 +353,38 @@ func bzzd(ctx *cli.Context) error { return nil } +// detectEnsAddr determines the ENS contract address by getting both the +// version and genesis hash using the client and matching them to either +// mainnet or testnet addresses +func detectEnsAddr(client *rpc.Client) (common.Address, error) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + var version string + if err := client.CallContext(ctx, &version, "net_version"); err != nil { + return common.Address{}, err + } + + block, err := ethclient.NewClient(client).BlockByNumber(ctx, big.NewInt(0)) + if err != nil { + return common.Address{}, err + } + + switch { + + case version == "1" && block.Hash() == params.MainNetGenesisHash: + log.Info("using Mainnet ENS contract address", "addr", ens.MainNetAddress) + return ens.MainNetAddress, nil + + case version == "3" && block.Hash() == params.TestNetGenesisHash: + log.Info("using Testnet ENS contract address", "addr", ens.TestNetAddress) + return ens.TestNetAddress, nil + + default: + return common.Address{}, fmt.Errorf("unknown version and genesis hash: %s %s", version, block.Hash()) + } +} + func registerBzzService(ctx *cli.Context, stack *node.Node) { prvkey := getAccount(ctx, stack) @@ -357,19 +409,56 @@ func registerBzzService(ctx *cli.Context, stack *node.Node) { syncEnabled := ctx.GlobalBoolT(SwarmSyncEnabledFlag.Name) ethapi := ctx.GlobalString(EthAPIFlag.Name) + if ethapi != "" { + log.Warn("DEPRECATED: --ethapi is deprecated and will be removed in a future version, please use --ens-api and --swap-api") + } + + swapapi := ctx.GlobalString(SwarmSwapAPIFlag.Name) + if swapEnabled && swapapi == "" { + utils.Fatalf("SWAP is enabled but --swap-api is not set") + } + + ensapi := ctx.GlobalString(EnsAPIFlag.Name) + // use the deprecated --ethapi if --ens-api is not set + if ensapi == "" { + ensapi = ethapi + } + ensAddr := ctx.GlobalString(EnsAddrFlag.Name) + cors := ctx.GlobalString(CorsStringFlag.Name) boot := func(ctx *node.ServiceContext) (node.Service, error) { - var client *ethclient.Client - if len(ethapi) > 0 { - client, err = ethclient.Dial(ethapi) + var swapClient *ethclient.Client + if swapapi != "" { + log.Info("connecting to SWAP API", "url", swapapi) + swapClient, err = ethclient.Dial(swapapi) if err != nil { - utils.Fatalf("Can't connect: %v", err) + return nil, fmt.Errorf("error connecting to SWAP API %s: %s", swapapi, err) } - } else { - swapEnabled = false } - return swarm.NewSwarm(ctx, client, bzzconfig, swapEnabled, syncEnabled, cors) + + var ensClient *ethclient.Client + if ensapi != "" { + log.Info("connecting to ENS API", "url", ensapi) + client, err := rpc.Dial(ensapi) + if err != nil { + return nil, fmt.Errorf("error connecting to ENS API %s: %s", ensapi, err) + } + ensClient = ethclient.NewClient(client) + + if ensAddr != "" { + bzzconfig.EnsRoot = common.HexToAddress(ensAddr) + } else { + ensAddr, err := detectEnsAddr(client) + if err == nil { + bzzconfig.EnsRoot = ensAddr + } else { + log.Warn(fmt.Sprintf("could not determine ENS contract address, using default %s", bzzconfig.EnsRoot), "err", err) + } + } + } + + return swarm.NewSwarm(ctx, swapClient, ensClient, bzzconfig, swapEnabled, syncEnabled, cors) } if err := stack.Register(boot); err != nil { utils.Fatalf("Failed to register the Swarm service: %v", err) |