package main import ( "context" "errors" "fmt" "sync" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/nostrino/pkg/chain" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/nostrino/pkg/neutrino" ) // p2pNode is the publisher's source when there is no node to ask over JSON-RPC. // // It follows the same peer the gateway proxies, through the same light client a wallet uses, and // answers the publisher's six questions from that client's verified store. That is what makes a // sidecar deployable beside a gateway instead of beside a full node: the chain data comes from the // peer the deployment already trusts for it, and nothing here needs an RPC endpoint, credentials or // a node's disk. // // The store is on disk, so a restart resumes the header and filter chain instead of downloading it // again. type p2pNode struct { client *neutrino.Client store neutrino.Store mu sync.Mutex // heights remembers where a block hash was seen, because the block service is asked by hash // and the store is indexed by height. A service asked for recent blocks only ever fills a // small part of it. heights map[chainhash.Hash]uint32 logf func(string, ...any) } // openP2PNode prepares the client, seeds the store's anchor and connects. func openP2PNode( ctx context.Context, profile *chain.Profile, storeDir, address string, logf func(string, ...any), ) (*p2pNode, error) { store, err := neutrino.OpenFileStore(storeDir) if err != nil { return nil, err } if _, ok := store.Anchor(); !ok { anchor := neutrino.Anchor{Height: 0, BlockHash: profile.GenesisHash, FilterHeight: -1} if profile.Checkpoint != nil { anchor = neutrino.Anchor{ Height: profile.Checkpoint.Height, BlockHash: profile.Checkpoint.BlockHash, FilterHeight: int32(profile.Checkpoint.Height), FilterHeader: profile.Checkpoint.FilterHeader, } } if err := store.SetAnchor(anchor); err != nil { return nil, err } } client, err := neutrino.New(neutrino.Config{ Profile: profile, Store: store, Address: address, Logf: logf, }) if err != nil { return nil, err } if err := client.Connect(ctx); err != nil { return nil, fmt.Errorf("peer %s: %w", address, err) } return &p2pNode{ client: client, store: store, heights: make(map[chainhash.Hash]uint32), logf: logf, }, nil } // tip brings the header chain up to the peer and reports where it ends. func (n *p2pNode) tip(ctx context.Context) (uint32, error) { if _, err := n.client.SyncHeaders(ctx); err != nil { return 0, err } tip, _, ok := n.store.HeaderTip() if !ok { return 0, errors.New("p2p: no header tip") } return tip, nil } // header returns one verified header from the store. func (n *p2pNode) header(_ context.Context, height uint32) (btcwire.BlockHeader, error) { header, ok := n.store.Header(height) if !ok { return btcwire.BlockHeader{}, fmt.Errorf("p2p: no header at %d", height) } return header, nil } // blockTime returns the header's own time, which is what the filter batch dates itself by. func (n *p2pNode) blockTime(ctx context.Context, height uint32) (int64, error) { header, err := n.header(ctx, height) if err != nil { return 0, err } return int64(header.Timestamp), nil } // filter returns one compact filter, fetching it from the peer when the store does not have it. func (n *p2pNode) filter(ctx context.Context, height uint32) ([]byte, chainhash.Hash, error) { if filter, ok := n.store.Filter(height); ok { if hash, ok := n.store.FilterHash(height); ok { return filter, hash, nil } } if _, _, err := n.client.SyncFilters(ctx, height, 1); err != nil { return nil, chainhash.Hash{}, fmt.Errorf("filter %d: %w", height, err) } filter, ok := n.store.Filter(height) if !ok { return nil, chainhash.Hash{}, fmt.Errorf("p2p: no filter at %d after the fetch", height) } hash, ok := n.store.FilterHash(height) if !ok { return nil, chainhash.Hash{}, fmt.Errorf("p2p: no filter hash at %d after the fetch", height) } return filter, hash, nil } // block returns one raw block through the client, which verifies its header before the caller // sees it. func (n *p2pNode) block(ctx context.Context, height uint32) ([]byte, error) { return n.client.Block(ctx, height) } // blockByHash resolves a hash to a height and reads the block there. // // The lookup walks down from the tip, which is where a block service's requests are: a client // asks for the block whose filter matched, and those are recent. The answer is remembered, so a // second request for the same block does not walk again. func (n *p2pNode) blockByHash(ctx context.Context, hash chainhash.Hash) ([]byte, error) { n.mu.Lock() height, known := n.heights[hash] n.mu.Unlock() if !known { tip, _, ok := n.store.HeaderTip() if !ok { return nil, errors.New("p2p: no header tip") } for h := tip; ; h-- { header, ok := n.store.Header(h) if ok && header.BlockHash() == hash { height, known = h, true break } if h == 0 { break } } if !known { return nil, fmt.Errorf("p2p: block %s is not in the header chain", hash.String()) } n.mu.Lock() n.heights[hash] = height n.mu.Unlock() } return n.block(ctx, height) } // close ends the peer connection. func (n *p2pNode) close() { _ = n.client.Close() }