Skip to content

Commit

Permalink
remove tbc Server.port
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaytonNorthey92 committed Jun 24, 2024
1 parent 430dc30 commit 02e80fa
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 32 deletions.
101 changes: 71 additions & 30 deletions service/tbc/tbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,55 @@ const (
var (
zeroHash = new(chainhash.Hash) // used to check if a hash is invalid

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

Expand Down Expand Up @@ -149,6 +182,11 @@ func sliceChainHash(ch chainhash.Hash) []byte {
return ch[:]
}

type Seed struct {
Host string
Port string
}

type Config struct {
AutoIndex bool
BlockSanity bool
Expand All @@ -160,7 +198,7 @@ type Config struct {
PeersWanted int
PrometheusListenAddress string
PprofListenAddress string
Seeds []string
Seeds []Seed
}

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

peers map[string]*peer // active but not necessarily connected
blocks *ttl.TTL // outstanding block downloads [hash]when/where
Expand Down Expand Up @@ -251,17 +288,14 @@ 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
Expand Down Expand Up @@ -315,25 +349,32 @@ 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 []Seed
for _, v := range s.seeds {
ips, err := resolver.LookupIP(ctx, "ip", net.JoinHostPort(v.Host, v.Port))
if err != nil {
log.Errorf("lookup: %v", err)
errorsSeen++
continue
}
addrs = append(addrs, ips...)

for _, ip := range ips {
moreSeeds = append(moreSeeds, Seed{
Host: ip.String(),
Port: v.Port,
})
}
}

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

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

Expand Down Expand Up @@ -486,7 +527,7 @@ func (s *Server) localPeerManager(ctx context.Context) error {

peersWanted := 1
peerC := make(chan string, peersWanted)
address := net.JoinHostPort(s.seeds[0], s.port)
address := net.JoinHostPort(s.seeds[0].Host, s.seeds[0].Port)
peer, err := NewPeer(s.wireNet, address)
if err != nil {
return fmt.Errorf("new peer: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion service/tbc/tbc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +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{"127.0.0.1"}
cfg.Seeds = []Seed{{Host: "127.0.0.1", Port: localnetPort}}
tbcServer, err := NewServer(cfg)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion service/tbc/tbcfork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func TestFork(t *testing.T) {
Network: networkLocalnet,
PeersWanted: 1,
PrometheusListenAddress: "",
Seeds: []string{"127.0.0.1"},
Seeds: []Seed{{Host: "127.0.0.1", Port: localnetPort}},
}
_ = loggo.ConfigureLoggers(cfg.LogLevel)
s, err := NewServer(cfg)
Expand Down

0 comments on commit 02e80fa

Please sign in to comment.