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

Add map_anchors for TxGraph #1325

Merged
merged 2 commits into from Feb 16, 2024
Merged

Conversation

yanganto
Copy link
Contributor

@yanganto yanganto commented Feb 6, 2024

Description

Fix #1295

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
Copy link
Member

Thank you for this PR! What is the purpose of commit a223c0c?

Copy link
Member

@evanlinjin evanlinjin left a comment

Choose a reason for hiding this comment

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

Thanks for this work. This will be a useful feature to have (talking from personal experience).

crates/chain/src/tx_graph.rs Outdated Show resolved Hide resolved
crates/chain/src/tx_graph.rs Outdated Show resolved Hide resolved
crates/chain/src/tx_graph.rs Outdated Show resolved Hide resolved
@yanganto
Copy link
Contributor Author

yanganto commented Feb 7, 2024

Thank you for this PR! What is the purpose of commit a223c0c?

I see the doc

/// The generic `A` should be a [`Anchor`] implementation.

And it was not required in the enum, if it is required, we may not need an additional description on the document.

This is a chore, and not about this topic, so it is separated in another commit.

@evanlinjin
Copy link
Member

@yanganto the key K for a BTreeSet in the stdlib is only useful if it impl's Ord, but it is not constrained in the struct definition (check here).

The reason for this is that generic constraints stack on top of each other and will start becoming tedious if types depend on types that have generics with constraints. So when we define anything, we avoid enforcing generic constraints wherever we can.

@yanganto
Copy link
Contributor Author

yanganto commented Feb 7, 2024

@yanganto the key K for a BTreeSet in the stdlib is only useful if it impl's Ord, but it is not constrained in the struct definition (check here).

The reason for this is that generic constraints stack on top of each other and will start becoming tedious if types depend on types that have generics with constraints. So when we define anything, we avoid enforcing generic constraints wherever we can.

I think there is no Ord constrain on BTreeSet struct and there is no doc on the struct because the real constraint is from the impl, and the get, take, replace are the real constraints with Ord. I dropped the commit here and maybe dig into this later on. It is not a chore and out of this thread.

@yanganto
Copy link
Contributor Author

yanganto commented Feb 7, 2024

@evanlinjin Does the test case look good from your end? If it is good, I will squash all the commit and resign it. 🙏

@evanlinjin
Copy link
Member

@yanganto the key K for a BTreeSet in the stdlib is only useful if it impl's Ord, but it is not constrained in the struct definition (check here).

The reason for this is that generic constraints stack on top of each other and will start becoming tedious if types depend on types that have generics with constraints. So when we define anything, we avoid enforcing generic constraints wherever we can.

I think there is no Ord constrain on BTreeSet struct and there is no doc on the struct because the real constraint is from the impl, and the get, take, replace are the real constraints with Ord. I dropped the commit here and maybe dig into this later on. It is not a chore and out of this thread.

That's exactly my point. We should only put generic constraints where it's strictly necessary.

@yanganto yanganto force-pushed the map-anchors branch 4 times, most recently from b8b98a6 to 52ef8f7 Compare February 8, 2024 12:54
@yanganto
Copy link
Contributor Author

yanganto commented Feb 8, 2024

There are more tx and blocks in the test case, and ready for review.

crates/chain/tests/test_tx_graph.rs Outdated Show resolved Hide resolved
crates/chain/src/tx_graph.rs Outdated Show resolved Hide resolved
@yanganto yanganto force-pushed the map-anchors branch 3 times, most recently from 7eeba1c to 1360847 Compare February 11, 2024 15:07
@nondiremanuel nondiremanuel added this to the 1.0.0-beta milestone Feb 13, 2024
Copy link
Member

@evanlinjin evanlinjin left a comment

Choose a reason for hiding this comment

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

ACK 5489f90

@yanganto
Copy link
Contributor Author

Thanks for the review.

Copy link
Contributor

@LLFourn LLFourn left a comment

Choose a reason for hiding this comment

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

ACK 5489f90

I guess we don't want to add this method to IndexedTxGraph?. I think this is another reason to do #1147.

F: FnMut(A) -> A2,
{
let mut new_graph = TxGraph::<A2>::default();
new_graph.apply_changeset(self.initial_changeset().map_anchors(f));
Copy link
Contributor

@LLFourn LLFourn Feb 16, 2024

Choose a reason for hiding this comment

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

⛏️ This seems weird because we allocate a giant changeset and then just apply it. I think we can just repeat the process you do in ChangeSet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are multiple anchor-related fields in TxGraph, and only one in ChangeSet. The function caller passed in possible non-determined, so it is good to map it once here to avoid issues from a non-determined function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Huh isn't there only one acnhor related field on TxGraph also?

Copy link
Member

Choose a reason for hiding this comment

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

Huh isn't there only one acnhor related field on TxGraph also?

We also have anchors in TxNode which should have 1 to 1 mapping to the anchors field. I think the implementation right now is safer and easier to read. If in the future, there is clear performance problems we can do something more efficient.

@evanlinjin
Copy link
Member

evanlinjin commented Feb 16, 2024

I guess we don't want to add this method to IndexedTxGraph?. I think this is another reason to do #1147.

My gripe with #1147 is that I'm assuming the Indexer interface is too restrictive and I would like to think about it further before making it mandatory by including it in TxGraph. I.e. with Utreexo and for optimizing updating the "transaction/utxo view" of a purely block-by-block sync. For the second point, it will be good if the indexer returned which outputs and inputs are relevant straight away. This way, we can update our view of the UTXO set without reconstructing it from the TxGraph.

Otherwise, yes, we need map_anchors on IndexedTxGraph as well (for now).

@evanlinjin evanlinjin merged commit 2c324d3 into bitcoindevkit:master Feb 16, 2024
12 checks passed
@yanganto yanganto deleted the map-anchors branch February 17, 2024 04:09
@notmandatory notmandatory modified the milestones: 1.0.0-beta, 1.0.0-alpha Feb 20, 2024
@notmandatory notmandatory mentioned this pull request Mar 26, 2024
25 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

TxGraph<A> should be able to transform anchor types.
5 participants