Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tbc: allow seeds to be overwritten #158

Merged
merged 14 commits into from
Jun 26, 2024
73 changes: 49 additions & 24 deletions service/tbc/tbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,22 @@ const (
var (
zeroHash = new(chainhash.Hash) // used to check if a hash is invalid

localnetSeeds = []string{
fmt.Sprintf("bitcoind:%s", localnetPort),
}
testnetSeeds = []string{
"testnet-seed.bitcoin.jonasschnelli.ch",
"seed.tbtc.petertodd.org",
"seed.testnet.bitcoin.sprovoost.nl",
"testnet-seed.bluematt.me",
fmt.Sprintf("testnet-seed.bitcoin.jonasschnelli.ch:%s", testnetPort),
fmt.Sprintf("seed.tbtc.petertodd.org:%s", testnetPort),
fmt.Sprintf("seed.testnet.bitcoin.sprovoost.nl:%s", testnetPort),
fmt.Sprintf("testnet-seed.bluematt.me:%s", testnetPort),
}
mainnetSeeds = []string{
"seed.bitcoin.sipa.be",
"dnsseed.bluematt.me",
"dnsseed.bitcoin.dashjr.org",
"seed.bitcoinstats.com",
"seed.bitnodes.io",
"seed.bitcoin.jonasschnelli.ch",
fmt.Sprintf("seed.bitcoin.sipa.be:%s", mainnetPort),
fmt.Sprintf("dnsseed.bluematt.me:%s", mainnetPort),
fmt.Sprintf("dnsseed.bitcoin.dashjr.org:%s", mainnetPort),
fmt.Sprintf("seed.bitcoinstats.com:%s", mainnetPort),
fmt.Sprintf("seed.bitnodes.io:%s", mainnetPort),
fmt.Sprintf("seed.bitcoin.jonasschnelli.ch:%s", mainnetPort),
}
)

Expand Down Expand Up @@ -157,6 +160,7 @@ type Config struct {
PeersWanted int
PrometheusListenAddress string
PprofListenAddress string
Seeds []string
}

func NewDefaultConfig() *Config {
Expand Down Expand Up @@ -184,7 +188,6 @@ type Server struct {
wireNet wire.BitcoinNet
chainParams *chaincfg.Params
timeSource blockchain.MedianTimeSource
port string
seeds []string

peers map[string]*peer // active but not necessarily connected
Expand Down Expand Up @@ -247,23 +250,25 @@ func NewServer(cfg *Config) (*Server, error) {

switch cfg.Network {
case "mainnet":
s.port = mainnetPort
s.wireNet = wire.MainNet
s.chainParams = &chaincfg.MainNetParams
s.seeds = mainnetSeeds
case "testnet3":
s.port = testnetPort
s.wireNet = wire.TestNet3
s.chainParams = &chaincfg.TestNet3Params
s.seeds = testnetSeeds
case networkLocalnet:
s.port = localnetPort
s.wireNet = wire.TestNet
s.chainParams = &chaincfg.RegressionNetParams
s.seeds = localnetSeeds
default:
return nil, fmt.Errorf("invalid network: %v", cfg.Network)
}

if len(cfg.Seeds) > 0 {
s.seeds = cfg.Seeds
}

return s, nil
}

Expand Down Expand Up @@ -306,26 +311,36 @@ func (s *Server) seed(pctx context.Context, peersWanted int) ([]tbcd.Peer, error
defer cancel()

errorsSeen := 0
var addrs []net.IP
for k := range s.seeds {
ips, err := resolver.LookupIP(ctx, "ip", s.seeds[k])
var moreSeeds []tbcd.Peer
for _, v := range s.seeds {
_, port, err := net.SplitHostPort(v)
if err != nil {
log.Errorf("SplitHostPort: %v", err)
ClaytonNorthey92 marked this conversation as resolved.
Show resolved Hide resolved
errorsSeen++
continue
}
ips, err := resolver.LookupIP(ctx, "ip", v)
joshuasing marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Errorf("lookup: %v", err)
errorsSeen++
continue
}
addrs = append(addrs, ips...)

for _, ip := range ips {
moreSeeds = append(moreSeeds, tbcd.Peer{
Host: ip.String(),
Port: port,
})
}
}

if errorsSeen == len(s.seeds) {
return nil, errors.New("could not seed")
}

// insert into peers table
for k := range addrs {
peers = append(peers, tbcd.Peer{
Host: addrs[k].String(),
Port: s.port,
})
for _, ms := range moreSeeds {
peers = append(peers, ms)
}

// return fake peers but don't save them to the database
Expand Down Expand Up @@ -471,9 +486,19 @@ func (s *Server) localPeerManager(ctx context.Context) error {
log.Tracef("localPeerManager")
defer log.Tracef("localPeerManager exit")

if len(s.seeds) != 1 {
return fmt.Errorf("expecting 1 seed, received %d", len(s.seeds))
}

peersWanted := 1
peerC := make(chan string, peersWanted)
address := net.JoinHostPort("127.0.0.1", s.port)

host, port, err := net.SplitHostPort(s.seeds[0])
if err != nil {
return err
}

address := net.JoinHostPort(host, port)
joshuasing marked this conversation as resolved.
Show resolved Hide resolved
peer, err := NewPeer(s.wireNet, address)
if err != nil {
return fmt.Errorf("new peer: %w", err)
Expand Down
1 change: 1 addition & 0 deletions service/tbc/tbc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ func createTbcServer(ctx context.Context, t *testing.T, mappedPeerPort nat.Port)
cfg.LevelDBHome = home
cfg.Network = networkLocalnet
cfg.ListenAddress = tcbListenAddress
cfg.Seeds = []string{fmt.Sprintf("127.0.0.1:%s", localnetPort)}
tbcServer, err := NewServer(cfg)
if err != nil {
t.Fatal(err)
Expand Down
1 change: 1 addition & 0 deletions service/tbc/tbcfork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ func TestFork(t *testing.T) {
Network: networkLocalnet,
PeersWanted: 1,
PrometheusListenAddress: "",
Seeds: []string{fmt.Sprintf("127.0.0.1:%s", localnetPort)},
}
_ = loggo.ConfigureLoggers(cfg.LogLevel)
s, err := NewServer(cfg)
Expand Down
Loading