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

tbcd: clean up peer and peermanager #273

Merged
merged 21 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cmd/tbcd/tbcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,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 @@ -204,6 +199,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 @@ -215,7 +215,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
35 changes: 27 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 @@ -211,10 +224,16 @@ func (p *peer) close() error {
log.Tracef("close")
defer log.Tracef("close exit")

if p == nil {
panic("p")
}
marcopeereboom marked this conversation as resolved.
Show resolved Hide resolved
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