Skip to content

Commit

Permalink
tbcd: clean up peer and peermanager (#273)
Browse files Browse the repository at this point in the history
* Start cleaning up peer and peermanager

* Rewrite a new peer manager

* Make new peer manager mostly work

* Please linter

* Add some prometheus crap

* bring back reset clause

* Make peer manager parallel

* This seems to make providing seeds work correctly

* Missing defer in mutex, thanks Joshua

* Remove debug

* Joshua suggestions

* Catch up to go 1.23 to get nifty new tcp dial options and fix tests

* Fix test

* Bring back ping all peers

* Deal with signals

* fix seeding, don't run test that connects to testnet3

* fix make race; do not use same host and port for each test

* Make it the same as bssd

* Oops, forgot to commit this

* sundry joshua nits

* Fix error

---------

Co-authored-by: ClaytonNorthey92 <clayton.northey@gmail.com>
  • Loading branch information
marcopeereboom and ClaytonNorthey92 authored Oct 11, 2024
1 parent ce74681 commit 88ca612
Show file tree
Hide file tree
Showing 9 changed files with 690 additions and 516 deletions.
12 changes: 6 additions & 6 deletions cmd/tbcd/tbcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,6 @@ func HandleSignals(ctx context.Context, cancel context.CancelFunc, callback func
os.Exit(2)
}

func init() {
version.Component = "tbcd"
welcome = "Hemi Tiny Bitcoin Daemon " + version.BuildInfo()
}

func _main() error {
// Parse configuration from environment
if err := config.Parse(cm); err != nil {
Expand Down Expand Up @@ -205,6 +200,11 @@ func _main() error {
return nil
}

func init() {
version.Component = "tbcd"
welcome = "Hemi Tiny Bitcoin Daemon " + version.BuildInfo()
}

func main() {
if len(os.Args) != 1 {
fmt.Fprintf(os.Stderr, "%v\n", welcome)
Expand All @@ -216,7 +216,7 @@ func main() {
}

if err := _main(); err != nil {
log.Errorf("%v", err)
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/hemilabs/heminetwork

go 1.22
go 1.23

toolchain go1.23.0

Expand Down
2 changes: 1 addition & 1 deletion service/tbc/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ func (s *Server) SyncIndexersToHash(ctx context.Context, hash *chainhash.Hash) e
return
}
// get a random peer
p, err := s.randomPeer(ctx)
p, err := s.pm.Random()
if err != nil {
s.mtx.Unlock()
log.Errorf("sync indexers random peer: %v", err)
Expand Down
32 changes: 24 additions & 8 deletions service/tbc/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type peer struct {
connected time.Time

address string
id int

protocolVersion uint32
network wire.BitcoinNet
Expand All @@ -47,19 +48,27 @@ type peer struct {
addrV2 bool
}

func NewPeer(network wire.BitcoinNet, address string) (*peer, error) {
// XXX parse address and return failure if it's wrong
func NewPeer(network wire.BitcoinNet, id int, address string) (*peer, error) {
_, _, err := net.SplitHostPort(address)
if err != nil {
return nil, fmt.Errorf("%v: %w", address, err)
}
return &peer{
protocolVersion: wire.ProtocolVersion,
network: network,
address: address,
id: id,
}, nil
}

func (p *peer) String() string {
return p.address
}

func (p *peer) Id() int {
return p.id
}

func (p *peer) write(timeout time.Duration, msg wire.Message) error {
p.mtx.Lock()
conn := p.conn
Expand Down Expand Up @@ -182,16 +191,20 @@ func (p *peer) connect(ctx context.Context) error {
p.mtx.Unlock()

d := net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 9 * time.Second,
Deadline: time.Now().Add(5 * time.Second),
KeepAliveConfig: net.KeepAliveConfig{
Enable: true,
Idle: 7 * time.Second,
Interval: 7 * time.Second,
Count: 2,
},
}

log.Debugf("dialing %s", p.address)
conn, err := d.DialContext(ctx, "tcp", p.address)
if err != nil {
return fmt.Errorf("dial %v: %w", p.address, err)
}
log.Debugf("done dialing %s", p.address)

err = p.handshake(ctx, conn)
if err != nil {
Expand All @@ -212,9 +225,12 @@ func (p *peer) close() error {
defer log.Tracef("close exit")

p.mtx.Lock()
defer p.mtx.Unlock()
if p.conn != nil {
return p.conn.Close()
conn := p.conn
p.conn = nil
p.isDialing = true // mark not connected
p.mtx.Unlock()
if conn != nil {
return conn.Close()
}
return net.ErrClosed
}
Expand Down
Loading

0 comments on commit 88ca612

Please sign in to comment.