bitcoindevkit / bdk

A modern, lightweight, descriptor-based wallet library written in Rust!
Other
868 stars 312 forks source link

Compact block filter Blockchain backend #81

Open notmandatory opened 4 years ago

notmandatory commented 4 years ago

This is an overview issue (based on same issue for murmel) for discussion and tracking the addition of a fully functional compact filters Blockchain backend to bdk. Ideally we'll have additional issues and/or PRs for each missing feature to link to this issue and we check off each feature as it's completed.

The below BIP-157/158 protocol features are based on the BIP-157 Client Operation section. Please suggest additions and corrections in the comments.

Missing protocol features for bdk are indicated below with an unchecked box [ ], completed features with a checked box [x].

Terminology

Peer Discovery

    • [x] Allow user to specify peer addresses
    • [ ] Receive peer addresses via [addr] messages from connected peers
    • [ ] Store new peer addresses
    • [ ] If no provided or previously stored peers, find seed peer addresses via DNS

Connect to Peers

    • [ ] Allow user to specify number of peers to connect to
    • [ ] Allow user to specify minimum number of BIP-157 peers to connect to
    • [x] Connect to peers and request signalling bits via [version] message
    • [ ] Keep track of peers signalling service bit NODE_COMPACT_FILTERS
    • [ ] Connect to specified number of peers
    • [ ] Connect to minimum number of BIP-157 peers

Request Block Headers

    • [x] Request block headers via [getheaders] message to connected peers
    • [x] Request block headers from genesis to most work block, 1 to 2000 blocks per request
    • [ ] Optional: Request block headers from stored checkpoint block instead of genesis block

Receive Block Headers

    • [x] Receive block headers [headers] from peers
    • [x] Build chain of block headers from stored and received new block headers
    • [x] Store new block headers and new block header tip
    • [ ] Disconnect peers whose longest chain has significantly less work than known tip
    • [ ] Request block filter headers from all peers for any new block header received

Request Filter Headers

    • [x] Allow user to specify start block height for block filter sync
    • [x] Request filter headers via [getcfheaders] message to peers
    • [x] Request from start block height to end block hash (1-1000 per request)
    • [ ] Request filter headers for the same block from more than one peer
    • [ ] Optional: First request filter headers at intervals of 1000 via [getcfcheckpt] message

Receive Filter Headers

    • [x] Receive filter headers [cfheaders] from peers
    • [ ] Verify filter headers from multiple peers are the same
    • [ ] If any received filter headers for a block are different,
        • [ ] request full block and compute correct block filter and filter header
        • [ ] ban peers that provided incorrect filter header
    • [x] Build chain of filter headers from stored and received new filter headers
    • [x] Store verified filter headers, at least to depth of 100
    • [x] Request block filter for any new verified filter header
    • [ ] Optional: store more than 100 verified filter headers

Request Block Filters

    • [x] Request block filters after all filter headers are received and verified
    • [x] Request block filters from start block via [getcfilters] message to peers
    • [x] Request from start block height to end block hash (1-1000 per request)
    • [ ] Optional: Check if filter header is for empty block filter if so skip request
    • [ ] Optional: Request different block filters in parallel from multiple peers

Receive Block Filters

    • [x] Receive block filter [cfilter] from peers
    • [x] Verify block filters sequentially, must link to corresponding filter header
    • [ ] Ban any peer that sends an invalid block filter
    • [x] Check block filters for relevance
        • [x] Previous output scripts for transaction inputs
        • [x] ScriptPubKey for transaction outputs

Fetch Full Blocks

    • [ ] If fetching full blocks, download from random peers
    • [ ] Fetch full blocks from peers even if they don’t support compact block filters
    • [x] Fetch full blocks that correspond to relevant block filters
thomaseizinger commented 3 years ago

There is https://github.com/cloudhead/nakamoto which should help implementing this.

notmandatory commented 3 years ago

Thanks having a chat with that team on Monday.

johncantrell97 commented 3 years ago

Any updated thoughts on this after talking with Nakamoto folk? I'm interested in working on this whether it's from scratch or implementing Nakamoto as a blockchain backend for bdk.

notmandatory commented 3 years ago

The team is certainly open to PRs to integrate Nakamoto as an optional blockchain backend for bdk. I briefly discussed it with Alexis here: https://bitdevsla.org/socratic-seminar-18/

Or you can build on the existing bdk::blockchain::compact_filters module which currently supports connecting to a single peer (multiple times if you like to improve performance). We'd also love any PRs to improve this module to add multiple peer support and Sybil resistance. For now I've updated the above issue description to reflect what is implemented and integrated with bdk.

notmandatory commented 3 months ago

Current plan is to integrate Kyoto CBF node with BDK.

https://github.com/rustaceanrob/kyoto