Skip to content

Commit

Permalink
updates for cross-chain bootnodes (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimalinux authored Jun 19, 2023
1 parent 8941163 commit 7619b9a
Show file tree
Hide file tree
Showing 50 changed files with 359 additions and 355 deletions.
24 changes: 10 additions & 14 deletions bootnode/bootnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ import (
"github.com/athanorlabs/atomic-swap/rpc"

"github.com/hashicorp/go-multierror"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
)

var log = logging.Logger("bootnode")

// Config provides the configuration for a bootnode.
type Config struct {
Env common.Environment
DataDir string
Bootnodes []string
P2PListenIP string
Expand All @@ -36,17 +35,15 @@ type Config struct {
// shut down. Typically, shutdown happens because a signal handler cancels the
// passed in context, or when the shutdown RPC method is called.
func RunBootnode(ctx context.Context, cfg *Config) error {
chainID := common.ChainIDFromEnv(cfg.Env)
host, err := net.NewHost(&net.Config{
Ctx: ctx,
DataDir: cfg.DataDir,
Port: cfg.Libp2pPort,
KeyFile: cfg.Libp2pKeyFile,
Bootnodes: cfg.Bootnodes,
ProtocolID: fmt.Sprintf("%s/%d", net.ProtocolID, chainID),
ListenIP: cfg.P2PListenIP,
IsRelayer: false,
IsBootnodeOnly: true,
Ctx: ctx,
Env: common.Bootnode,
DataDir: cfg.DataDir,
Port: cfg.Libp2pPort,
KeyFile: cfg.Libp2pKeyFile,
Bootnodes: cfg.Bootnodes,
ListenIP: cfg.P2PListenIP,
IsRelayer: false,
})
if err != nil {
return err
Expand All @@ -63,7 +60,7 @@ func RunBootnode(ctx context.Context, cfg *Config) error {

rpcServer, err := rpc.NewServer(&rpc.Config{
Ctx: ctx,
Env: cfg.Env,
Env: common.Bootnode,
Address: fmt.Sprintf("127.0.0.1:%d", cfg.RPCPort),
Net: host,
XMRTaker: nil,
Expand All @@ -74,7 +71,6 @@ func RunBootnode(ctx context.Context, cfg *Config) error {
rpc.DaemonNamespace: {},
rpc.NetNamespace: {},
},
IsBootnodeOnly: true,
})
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cliutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package cliutil
import (
"fmt"

logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
"github.com/urfave/cli/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion cliutil/signal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"syscall"
"time"

logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
)

// SignalHandler handles OS signals and shuts down the program if necessary.
Expand Down
2 changes: 1 addition & 1 deletion cliutil/signal_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"
"testing"

logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
)

func TestDaemon_signalHandler(_ *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cliutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
"github.com/urfave/cli/v2"

"github.com/athanorlabs/atomic-swap/common"
Expand Down
40 changes: 13 additions & 27 deletions cmd/bootnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/athanorlabs/atomic-swap/cliutil"
"github.com/athanorlabs/atomic-swap/common"

logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
"github.com/urfave/cli/v2"
)

Expand All @@ -29,7 +29,7 @@ const (
flagLibp2pPort = "libp2p-port"
flagBootnodes = "bootnodes"
flagRPCPort = "rpc-port"
flagEnv = "env"
flagLibp2pIP = "libp2p-ip"
)

var log = logging.Logger("cmd")
Expand All @@ -46,7 +46,7 @@ func cliApp() *cli.App {
&cli.StringFlag{
Name: flagDataDir,
Usage: "Path to store swap artifacts",
Value: "{HOME}/.atomicswap/{ENV}/bootnode", // For --help only, actual default replaces variables
Value: "{HOME}/.atomicswap/bootnode", // For --help only, actual default replaces variables
},
&cli.StringFlag{
Name: flagLibp2pKey,
Expand All @@ -72,10 +72,9 @@ func cliApp() *cli.App {
EnvVars: []string{"SWAPD_RPC_PORT"},
},
&cli.StringFlag{
Name: flagEnv,
Usage: "Environment to use: one of mainnet, stagenet, or dev. Default: mainnet",
EnvVars: []string{"SWAPD_ENV"},
Value: "mainnet",
Name: flagLibp2pIP,
Usage: "Libp2p bind IP, can set to 127.0.0.1 for testing",
Value: "0.0.0.0",
},
&cli.StringFlag{
Name: cliutil.FlagLogLevel,
Expand Down Expand Up @@ -109,7 +108,7 @@ func runBootnode(c *cli.Context) error {
return err
}

config, err := getEnvConfig(c)
config, err := getBootnodeConfig(c)
if err != nil {
return err
}
Expand All @@ -126,33 +125,21 @@ func runBootnode(c *cli.Context) error {
libp2pKeyFile = path.Join(config.DataDir, common.DefaultLibp2pKeyFileName)
}

libp2pPort := uint16(c.Uint(flagLibp2pPort))

hostListenIP := "0.0.0.0"
if config.Env == common.Development {
hostListenIP = "127.0.0.1"
}
log.Infof("starting bootnode")

rpcPort := uint16(c.Uint(flagRPCPort))
return bootnode.RunBootnode(c.Context, &bootnode.Config{
Env: config.Env,
DataDir: config.DataDir,
Bootnodes: config.Bootnodes,
P2PListenIP: hostListenIP,
Libp2pPort: libp2pPort,
P2PListenIP: c.String(flagLibp2pIP),
Libp2pPort: uint16(c.Uint(flagLibp2pPort)),
Libp2pKeyFile: libp2pKeyFile,
RPCPort: rpcPort,
})
}

func getEnvConfig(c *cli.Context) (*common.Config, error) {
env, err := common.NewEnv(c.String(flagEnv))
if err != nil {
return nil, err
}
log.Infof("starting bootnode, environment: %s", env)

conf := common.ConfigDefaultsForEnv(env)
func getBootnodeConfig(c *cli.Context) (*common.Config, error) {
conf := common.BootnodeConfig()

// cfg.DataDir already has a default set, so only override if the user explicitly set the flag
if c.IsSet(flagDataDir) {
Expand All @@ -162,8 +149,7 @@ func getEnvConfig(c *cli.Context) (*common.Config, error) {
}
}

conf.DataDir = path.Join(conf.DataDir, "bootnode")
if err = common.MakeDir(conf.DataDir); err != nil {
if err := common.MakeDir(conf.DataDir); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/bootnode/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestBootnode(t *testing.T) {

flags := []string{
"bootnode",
fmt.Sprintf("--%s=dev", flagEnv),
fmt.Sprintf("--%s=127.0.0.1", flagLibp2pIP),
fmt.Sprintf("--%s=debug", cliutil.FlagLogLevel),
fmt.Sprintf("--%s=%s", flagDataDir, dataDir),
fmt.Sprintf("--%s=%d", flagRPCPort, rpcPort),
Expand Down
6 changes: 5 additions & 1 deletion cmd/swapd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"path"

ethcommon "github.com/ethereum/go-ethereum/common"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
"github.com/urfave/cli/v2"

"github.com/athanorlabs/atomic-swap/cliutil"
Expand Down Expand Up @@ -301,6 +301,10 @@ func getEnvConfig(c *cli.Context, devXMRMaker bool, devXMRTaker bool) (*common.C
return nil, err
}

if env == common.Bootnode {
return nil, fmt.Errorf("%q is not a valid environment for swapd", env.String())
}

if devXMRMaker || devXMRTaker {
env = common.Development
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/swapd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ func TestDaemon_BadFlags(t *testing.T) {
},
expectErr: fmt.Sprintf("unknown command %q", flagContractAddress),
},
{
description: "using bootnode environment",
extraFlags: []string{
// last flag wins, so this will override
fmt.Sprintf("--%s=%s", flagEnv, common.Bootnode),
},
expectErr: `"bootnode" is not a valid environment for swapd`,
},
}

for _, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion coins/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"math/big"

"github.com/cockroachdb/apd/v3"
logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion coins/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package coins
import (
"testing"

logging "github.com/ipfs/go-log"
logging "github.com/ipfs/go-log/v2"
"github.com/stretchr/testify/assert"
)

Expand Down
65 changes: 41 additions & 24 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package common

import (
"math/big"
"os"
"path"
"time"
Expand All @@ -26,6 +25,22 @@ const (
var homeDir, _ = os.UserHomeDir()
var baseDir = path.Join(homeDir, ".atomicswap")

// publicBootnodes are bootnodes with public IP addresses that are used in every
// environment other than development.
var publicBootnodes = []string{
"/ip4/109.105.198.218/tcp/9909/p2p/12D3KooWMYfJHQAjL1F6EVk2s9CJvsEvdYMW8CD7uBMs15sdFJzd",
"/ip4/134.122.115.208/tcp/9900/p2p/12D3KooWHZ2G9XscjDGvG7p8uPBoYerDc9kWYnc8oJFGfFxS6gfq",
"/ip4/143.198.123.27/tcp/9909/p2p/12D3KooWDCE2ukB1Sw88hmLFk5BZRRViyYLeuAKPuu59nYyFWAec",
"/ip4/161.35.110.210/tcp/9900/p2p/12D3KooWS8iKxqsGTiL3Yc1VaAfg99U5km1AE7bWYQiuavXj3Yz6",
"/ip4/164.92.103.159/tcp/9900/p2p/12D3KooWSNQF1eNyapxC2zA3jJExgLX7jWhEyw8B3k7zMW5ZRvQz",
"/ip4/164.92.123.10/tcp/9900/p2p/12D3KooWG8z9fXVTB72XL8hQbahpfEjutREL9vbBQ4FzqtDKzTBu",
"/ip4/185.130.46.66/tcp/9909/p2p/12D3KooWDKf2FJG1AWTJthbs7fcCcsQa26f4pmCR25cktRg2X2aY",
"/ip4/31.220.60.19/tcp/9909/p2p/12D3KooWSvfyLUVoHqSdKpAaD45cHEq5Kqe759LMvS3Bq7y1XBuo",
"/ip4/67.205.131.11/tcp/9909/p2p/12D3KooWGpCLC4y42rf6aR3cguVFJAruzFXT6mUEyp7C32jTsyJd",
"/ip4/67.207.89.83/tcp/9909/p2p/12D3KooWED1Y5nfno34Qhz2Xj9ubmwi4hv2qd676pH6Jb7ui36CR",
"/ip4/93.95.228.200/tcp/9909/p2p/12D3KooWJParpZ1zHDspoV4kogkBsHKrGxMeq3UGFxQUm6TZPojn",
}

// MoneroNode represents the host and port of monerod's RPC endpoint
type MoneroNode struct {
Host string
Expand Down Expand Up @@ -67,15 +82,7 @@ func MainnetConfig() *Config {
},
},
SwapCreatorAddr: ethcommon.HexToAddress("0x377ed3a60007048DF00135637521170628De89E5"),
Bootnodes: []string{
"/ip4/67.205.131.11/tcp/9909/p2p/12D3KooWGpCLC4y42rf6aR3cguVFJAruzFXT6mUEyp7C32jTsyJd",
"/ip4/143.198.123.27/tcp/9909/p2p/12D3KooWDCE2ukB1Sw88hmLFk5BZRRViyYLeuAKPuu59nYyFWAec",
"/ip4/67.207.89.83/tcp/9909/p2p/12D3KooWED1Y5nfno34Qhz2Xj9ubmwi4hv2qd676pH6Jb7ui36CR",
"/ip4/109.105.198.218/tcp/9909/p2p/12D3KooWRbAV4AU75JXVUedmwakiM1yzzDzDCdmZWzGdXGQ4Yqdv",
"/ip4/185.130.46.66/tcp/9909/p2p/12D3KooWDKf2FJG1AWTJthbs7fcCcsQa26f4pmCR25cktRg2X2aY",
"/ip4/93.95.228.200/tcp/9909/p2p/12D3KooWJParpZ1zHDspoV4kogkBsHKrGxMeq3UGFxQUm6TZPojn",
"/ip4/31.220.60.19/tcp/9909/p2p/12D3KooWLksqqtzwA4Epg5eCxA2EaJ3Q34RW554HSqUDp2bgromP",
},
Bootnodes: publicBootnodes,
}
}

Expand All @@ -100,15 +107,7 @@ func StagenetConfig() *Config {
},
},
SwapCreatorAddr: ethcommon.HexToAddress("0x377ed3a60007048DF00135637521170628De89E5"),
Bootnodes: []string{
"/ip4/134.122.115.208/tcp/9900/p2p/12D3KooWHZ2G9XscjDGvG7p8uPBoYerDc9kWYnc8oJFGfFxS6gfq",
"/ip4/143.198.123.27/tcp/9900/p2p/12D3KooWGzExs5zqebnDvqkUAKaiUuxF3DNbrfJ4prbfkxjXb366",
"/ip4/67.207.89.83/tcp/9900/p2p/12D3KooWLidMFT3WJaBK7cEzmpDEcuRcNTux89SVrZmEiGLhTWoe",
"/ip4/67.205.131.11/tcp/9900/p2p/12D3KooWJxA1tPLugao43gPvE4rCcgG7xwQ4dhbLCUQehAiohJk2",
"/ip4/164.92.103.159/tcp/9900/p2p/12D3KooWSNQF1eNyapxC2zA3jJExgLX7jWhEyw8B3k7zMW5ZRvQz",
"/ip4/164.92.123.10/tcp/9900/p2p/12D3KooWG8z9fXVTB72XL8hQbahpfEjutREL9vbBQ4FzqtDKzTBu",
"/ip4/161.35.110.210/tcp/9900/p2p/12D3KooWS8iKxqsGTiL3Yc1VaAfg99U5km1AE7bWYQiuavXj3Yz6",
},
Bootnodes: publicBootnodes,
}
}

Expand All @@ -127,6 +126,18 @@ func DevelopmentConfig() *Config {
}
}

// BootnodeConfig is environment for bootnodes, which act across multiple environments
func BootnodeConfig() *Config {
return &Config{
Env: Bootnode,
DataDir: path.Join(baseDir, "bootnode"),
EthEndpoint: "",
MoneroNodes: nil,
SwapCreatorAddr: ethcommon.Address{},
Bootnodes: publicBootnodes,
}
}

// MoneroWalletPath returns the path to the wallet file, whose default value
// depends on current value of the data dir.
func (c Config) MoneroWalletPath() string {
Expand Down Expand Up @@ -154,6 +165,8 @@ func ConfigDefaultsForEnv(env Environment) *Config {
return StagenetConfig()
case Development:
return DevelopmentConfig()
case Bootnode:
return BootnodeConfig()
default:
panic("invalid environment")
}
Expand Down Expand Up @@ -186,16 +199,20 @@ func DefaultMoneroPortFromEnv(env Environment) uint {
}
}

// ChainIDFromEnv returns the expected chainID that we should find on the
// ChainNameFromEnv returns the expected chainID that we should find on the
// ethereum endpoint when running int the passed environment.
func ChainIDFromEnv(env Environment) *big.Int {
func ChainNameFromEnv(env Environment) string {
//nolint:goconst
switch env {
case Development:
return big.NewInt(GanacheChainID)
return "ganache"
case Stagenet:
return big.NewInt(SepoliaChainID)
return "sepolia"
case Mainnet:
return big.NewInt(MainnetChainID)
return "mainnet"
case Bootnode:
// bootnodes work across chains, so they get their own name
return "bootnode"
default:
panic("invalid environment")
}
Expand Down
Loading

0 comments on commit 7619b9a

Please sign in to comment.