Skip to content

Commit

Permalink
tbc: allow seeds to be overwritten (hemilabs#158)
Browse files Browse the repository at this point in the history
* allow TBC seeds to be overwritten

if cfg seeds are set, use those, otherwise use the defaults for the network chosen

* fixed missing seeding

* remove tbc Server.port

* using string for seeds

* use host for lookupip

* added TBC_SEEDS env config

* Update service/tbc/tbc.go

Co-authored-by: Joshua Sing <joshua@bloq.com>

* simplify NewPeer in localPeerManager

* Update cmd/tbcd/tbcd.go

Co-authored-by: Joshua Sing <joshua@bloq.com>

* use AppendSlice

* removed ports from variables

* fix reflect handling of seeds

* Update config/config.go

Co-authored-by: Joshua Sing <joshua@bloq.com>

* using 127.0.0.1 as default IP/host for localnet

---------

Co-authored-by: Joshua Sing <joshua@bloq.com>
  • Loading branch information
ClaytonNorthey92 and joshuasing authored Jun 26, 2024
1 parent dc66d42 commit ecf963b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 30 deletions.
7 changes: 7 additions & 0 deletions cmd/tbcd/tbcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ var (
Help: "address and port tbcd pprof listens on (open <address>/debug/pprof to see available profiles)",
Print: config.PrintAll,
},
"TBC_SEEDS": config.Config{
Value: &cfg.Seeds,
DefaultValue: []string{},
Help: "list of seed domains for Bitcoin P2P, in the format '<host>:<port>' (for localnet, must be a single host:port)",
Print: config.PrintAll,
},
}
)

Expand Down Expand Up @@ -139,6 +145,7 @@ func _main() error {
if err != nil {
return fmt.Errorf("create tbc server: %w", err)
}

// XXX remove, this is an illustration of calling the direct API of server
// go func() {
// for {
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"reflect"
"sort"
"strconv"
"strings"
)

type PrintMode int
Expand Down Expand Up @@ -88,6 +89,9 @@ func Parse(c CfgMap) error {
}

reflect.ValueOf(v.Value).Elem().SetBool(val)
case reflect.Slice:
value := reflect.ValueOf(v.Value).Elem()
value.Set(reflect.AppendSlice(value, reflect.ValueOf(strings.Split(envValue, ","))))

default:
return fmt.Errorf("unsuported type for %v: %v",
Expand Down
73 changes: 44 additions & 29 deletions service/tbc/tbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ const (

promSubsystem = "tbc_service" // Prometheus

mainnetPort = "8333"
testnetPort = "18333"
localnetPort = "18444"

defaultPeersWanted = 64
defaultPendingBlocks = 128 // 128 * ~4MB max memory use

Expand All @@ -67,19 +63,22 @@ const (
var (
zeroHash = new(chainhash.Hash) // used to check if a hash is invalid

localnetSeeds = []string{
"127.0.0.1:18444",
}
testnetSeeds = []string{
"testnet-seed.bitcoin.jonasschnelli.ch",
"seed.tbtc.petertodd.org",
"seed.testnet.bitcoin.sprovoost.nl",
"testnet-seed.bluematt.me",
"testnet-seed.bitcoin.jonasschnelli.ch:18333",
"seed.tbtc.petertodd.org:18333",
"seed.testnet.bitcoin.sprovoost.nl:18333",
"testnet-seed.bluematt.me:18333",
}
mainnetSeeds = []string{
"seed.bitcoin.sipa.be",
"dnsseed.bluematt.me",
"dnsseed.bitcoin.dashjr.org",
"seed.bitcoinstats.com",
"seed.bitnodes.io",
"seed.bitcoin.jonasschnelli.ch",
"seed.bitcoin.sipa.be:8333",
"dnsseed.bluematt.me:8333",
"dnsseed.bitcoin.dashjr.org:8333",
"seed.bitcoinstats.com:8333",
"seed.bitnodes.io:8333",
"seed.bitcoin.jonasschnelli.ch:8333",
}
)

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

func NewDefaultConfig() *Config {
Expand Down Expand Up @@ -184,7 +184,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 +246,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 +307,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 {
host, port, err := net.SplitHostPort(v)
if err != nil {
log.Errorf("Failed to parse host/port: %v", err)
errorsSeen++
continue
}
ips, err := resolver.LookupIP(ctx, "ip", host)
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,10 +482,14 @@ 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)
peer, err := NewPeer(s.wireNet, address)

peer, err := NewPeer(s.wireNet, s.seeds[0])
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 @@ -1032,5 +1032,5 @@ func createBitcoindWithInitialBlocks(ctx context.Context, t *testing.T, blocks u
t.Fatal(err)
}

return bitcoindContainer, nat.Port(localnetPort)
return bitcoindContainer, nat.Port("18444")
}

0 comments on commit ecf963b

Please sign in to comment.