package filler import ( "context" "net" "time" "github.com/btcsuite/btcd/chainhash/v2" "github.com/btcsuite/btcd/wire/v2" "git.smesh.lol/btcd-lite/internal/v2peer" ) // conn is one peer connection owned by a lane: btcd's handshake and btcd's // BIP324 transport through internal/v2peer, and one block request. // // Nothing here speaks the peer protocol itself. The frame, the encoding and // the transport are btcd's; a lane makes a connection, asks for blocks and // closes it. type conn struct { peer *v2peer.Peer // block is the last block answered for, in its wire form. block []byte } // connFrom wraps a connection a caller already has open, which is how an // instance that synced headers over it keeps it for the bodies rather than // asking the node for a second connection. func connFrom(p *v2peer.Peer) *conn { return &conn{peer: p} } // connect dials a peer and completes the handshake. func connect(ctx context.Context, cfg dialConfig, addr string, lastBlock int32, timeout time.Duration) (*conn, error) { dial := func(ctx context.Context, a string) (net.Conn, error) { if cfg.dial != nil { return cfg.dial(ctx, a) } var d net.Dialer return d.DialContext(ctx, "tcp", a) } if !cfg.v2 { nc, err := dial(ctx, addr) if err != nil { return nil, err } p, err := v2peer.Dial(v2peer.Config{ Conn: nc, Net: cfg.net, UserAgent: cfg.ua, Services: cfg.services, LastBlock: lastBlock, Timeout: timeout, Remote: addr, Logf: cfg.logf, }) if err != nil { _ = nc.Close() return nil, err } return &conn{peer: p}, nil } p, err := v2peer.DialWithFallback(ctx, dial, addr, v2peer.Config{ Net: cfg.net, UserAgent: cfg.ua, Services: cfg.services, LastBlock: lastBlock, Timeout: timeout, Remote: addr, Logf: cfg.logf, }) if err != nil { return nil, err } return &conn{peer: p}, nil } // request asks for one block and keeps the bytes. func (c *conn) request(hash chainhash.Hash, timeout time.Duration) error { raw, err := c.peer.BlockFor(hash, timeout) if err != nil { return err } c.block = raw return nil } func (c *conn) userAgent() string { return c.peer.Agent() } func (c *conn) close() error { return c.peer.Close() } // dialConfig is what a pool hands a lane to make a connection. type dialConfig struct { net wire.BitcoinNet ua string services wire.ServiceFlag dial func(ctx context.Context, addr string) (net.Conn, error) logf func(string, ...any) v2 bool }