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

Fixes #1665: #1670

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Fixes #1665: #1670

wants to merge 3 commits into from

Conversation

evanlinjin
Copy link
Member

Fixes #1665
Replaces #1659

Description

Implement an O(n) algorithm to determine the canonical set of transactions of TxGraph.

Notes to the reviewers

This is an initial, untested PR. Just here to show progress. Feedback welcome.

Changelog notice

TODO

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature

Bugfixes:

  • This pull request breaks the existing API
  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

@evanlinjin evanlinjin added the bug Something isn't working label Nov 3, 2024
@evanlinjin evanlinjin added this to the 1.0.0-beta milestone Nov 3, 2024
@evanlinjin evanlinjin self-assigned this Nov 3, 2024
spends: BTreeMap<OutPoint, HashSet<Txid>>,
anchors: BTreeSet<(A, Txid)>,
anchors: HashMap<Txid, BTreeSet<A>>,
last_seen: HashMap<Txid, u64>,

// This atrocity exists so that `TxGraph::outspends()` can return a reference.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to call this an "atrocity" since it's the best way to do it.

Suggested change
// This atrocity exists so that `TxGraph::outspends()` can return a reference.
// This exists so that `TxGraph::outspends()` can return a reference to an empty set of `Txid`s.

last_seen: HashMap<Txid, u64>,

// This atrocity exists so that `TxGraph::outspends()` can return a reference.
// FIXME: This can be removed once `HashSet::new` is a const fn.
empty_outspends: HashSet<Txid>,
empty_anchors: BTreeSet<A>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
empty_anchors: BTreeSet<A>,
// This exists so that `TxGraph` functions can return a reference to an empty set of `TxNode.anchors`.
empty_anchors: BTreeSet<A>,

/// ChainPosition::Unconfirmed(last_seen) => println!(
/// "tx is last seen at {}, it is unconfirmed as it is not anchored in the best chain",
/// last_seen,
/// ),
/// ChainPosition::UnconfirmedAndNotSeen => println!(
/// "tx does not conflict with anything canonical, but we never seen it in mempool",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ grammar nit

Suggested change
/// "tx does not conflict with anything canonical, but we never seen it in mempool",
/// "tx does not conflict with anything canonical, but we have never seen it in the mempool",

Previously, the field `TxGraph::anchors` existed because we assumed
there was use in having a partially-chronological order of transactions
there. Turns out it wasn't used at all.

This commit removes anchors from the `txs` field and reworks `anchors`
field to be a map of `txid -> set<anchors>`.

This is a breaking change since the signature of `all_anchors()` is
changed.
@notmandatory
Copy link
Member

Per call today @evanlinjin will open a new PR for the 1.0.0-beta milestone that only makes expected breaking changes for Wallet, likely only the ChainPosition enum. Then the algorithm improvements in this PR can be released in a future non-breaking patch version.

@notmandatory notmandatory removed this from the 1.0.0-beta milestone Nov 19, 2024
@notmandatory notmandatory added the audit Suggested as result of external code audit label Nov 19, 2024
@evanlinjin evanlinjin force-pushed the fix/1665 branch 5 times, most recently from 6abc6d0 to c34e915 Compare November 19, 2024 05:00
This is an O(n) algorithm to determine the canonical set of txids.
@evanlinjin evanlinjin force-pushed the fix/1665 branch 2 times, most recently from 8c34407 to 5278b81 Compare November 19, 2024 06:35
@notmandatory
Copy link
Member

On further discussion today at release planning call this PR was moved back into the 1.0 milestone if it can be completed and reviewed in time. If not it will have to wait for a 2.0 milestone because it required breaking changes to chain crate APIs that are exposed in the Wallet API.

Alternatively we could remove the following Wallet functions and move any error types from chain into the core module which should insulate the wallet crate from this chain crate API changes.

    /// Get a reference to the inner [`TxGraph`].
    pub fn tx_graph(&self) -> &TxGraph<ConfirmationBlockTime> {
        self.indexed_graph.graph()
    }

    /// Get a reference to the inner [`KeychainTxOutIndex`].
    pub fn spk_index(&self) -> &KeychainTxOutIndex<KeychainKind> {
        &self.indexed_graph.index
    }

    /// Get a reference to the inner [`LocalChain`].
    pub fn local_chain(&self) -> &LocalChain {
        &self.chain
    }

@notmandatory notmandatory added the api A breaking API change label Nov 21, 2024
@evanlinjin
Copy link
Member Author

@notmandatory how does this solution compare with just giving wallet a major version bump?

@notmandatory
Copy link
Member

Removing the above functions and moving required chain error type to core should allow us to do breaking chain crate api changes without having to do a major wallet crate release. I also don't see why Wallet users need to access the inner chain types directly instead of using higher level functions like transactions(), or we can add new helper functions without breaking any APIs.

But all that said if it's less risky to just keep everything as is and do a 2.0 release in 6 mo or so I'd be fine with that too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api A breaking API change audit Suggested as result of external code audit bug Something isn't working module-blockchain
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

Performance issue with get_chain_position and associated methods for unconfirmed transactions
2 participants