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 TxBuilder APIs #611

Merged

Conversation

thunderbiscuit
Copy link
Member

@thunderbiscuit thunderbiscuit commented Oct 22, 2024

This PR brings in some of the missing APIs on the TxBuilder outlined in #597.

  • TxBuilder::add_data
  • TxBuilder::current_height
  • TxBuilder::nlocktime
  • TxBuilder::allow_dust
  • TxBuilder::version

Changelog notice

Added:
  - TxBuilder::add_data [#611]
  - TxBuilder::current_height [#611]
  - TxBuilder::nlocktime [#611]
  - TxBuilder::allow_dust [#611]
  - TxBuilder::allow_dust [#611]

[#611]: /~https://github.com/bitcoindevkit/bdk-ffi/pull/611

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

Copy link
Collaborator

@reez reez left a comment

Choose a reason for hiding this comment

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

Watching Steve's tabconf talk last week he used this functionality and I actually jotted down a note about it wanting to try it out in bdk-ffi/iOS

ACK e06f2a9

Side note: the two checkboxes I've added tests for the new feature + I've added docs for the new feature are checked, are you adding a test as a follow up and/or adding docs as a follow up to whatever PR we do adding docstrings, or were those just checked accidentally?

@thunderbiscuit thunderbiscuit force-pushed the feature/add-txbuilder-apis branch from 0ac8dce to 337d676 Compare November 15, 2024 16:36
@rustaceanrob
Copy link
Contributor

With the discussion on bitcoindevkit/bdk#1676 I think we should add a set_version or similar while we are at it. Transaction version one is a fingerprint, and users might be clever enough to use transaction version three

@thunderbiscuit thunderbiscuit force-pushed the feature/add-txbuilder-apis branch 2 times, most recently from e9714e6 to 1d8b95d Compare November 21, 2024 18:25
@thunderbiscuit thunderbiscuit force-pushed the feature/add-txbuilder-apis branch from 0741588 to 99adbf7 Compare November 21, 2024 19:29
@thunderbiscuit
Copy link
Member Author

This is finally ready for review. I recommend reviewing 1 commit at a time, as they are individual independent pieces. There are 3 methods left on the TxBuilder identified in #597, but all 3 require a bit more work/thought behind them, and would not be creating breaking changes when we add them.

I think merging the 5 methods proposed here makes sense for the 1.0 release.

@thunderbiscuit thunderbiscuit self-assigned this Nov 22, 2024
@thunderbiscuit thunderbiscuit added this to the 1.0.0-beta milestone Nov 22, 2024
Comment on lines +251 to +254
if !&self.data.is_empty() {
let push_bytes = PushBytesBuf::try_from(self.data.clone())?;
tx_builder.add_data(&push_bytes);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm reviewing these commit by commit as recommended, and for this add_data I was wondering if we needed to manually validate the size of the data here based on Bitcoin's standard transaction relay policy. I'm wondering this because that thought popped into my head so I started writing some tests around it like:

  • test_add_data
  • test_add_data_within_limit
  • test_add_data_exceeding_limit
  • test_add_empty_data

and I couldn't get fn test_add_data_exceeding_limit test to work without adding something like

            if self.data.len() > 80 {
                return Err(CreateTxError::PushBytesError);
            }

in the finish method.

Thoughts? I could totally be missing something though.

Also let me know if adding the specific implementation of my tests is helpful too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Personally, I don't think add_data should land for 1.0.0. PushBytesBuf doesn't have a clear API and is_standard_op_return isn't even a method yet in rust-bitcoin (I am patching the method right now). Ideally everything about OP_RETURN is left to the Rust-side and FFI has no logic for handling this

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ideally everything about OP_RETURN is left to the Rust-side and FFI has no logic for handling this

Def agree with this 100%. I just happened to be running into an issue with it when I was coming up with test to test what happened when I gave it data exceeding that limit. But maybe I wrote my test incorrectly, will double back and look at it again

Copy link
Contributor

Choose a reason for hiding this comment

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

There is no check on the Rust side and currently it will allow for invalid OP_RETURN data

Copy link
Collaborator

Choose a reason for hiding this comment

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

Looking back at my test the other thing that played into the way I wrote it was how I approached non-standard transactions.

Copy link
Collaborator

Choose a reason for hiding this comment

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

There is no check on the Rust side and currently it will allow for invalid OP_RETURN data

Thanks for this! I didn't see it before I wrote:

Looking back at my test the other thing that played into the way I wrote it was how I approached non-standard transactions.

I'm actually really interested in where we land on this conversation even if for the time being overall we end up at this place you mentioned rob:

I don't think add_data should land for 1.0.0

Copy link
Member Author

Choose a reason for hiding this comment

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

I think enforcing standardness will come with bitcoindevkit/bdk#1726. I agree we should not have logic handling standardness here, and leave it to bdk_wallet (or rust-bitcoin when your patch is merged @rob, however that shakes out).

This was already a (somewhat) faulty API in the 0.32.0 release, and IMO people using it will just have to know how it works if they want to build relayable transactions (just like it was in bdk_wallet <= beta.5). The fix in beta 6 or the final 1.0 release will enforce standardness.

Comment on lines +209 to +215
pub(crate) fn current_height(&self, height: u32) -> Arc<Self> {
Arc::new(TxBuilder {
current_height: Some(height),
..self.clone()
})
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

reviewing commit by commit, this looks good, I wrote a test for it and it passed ✅

Copy link
Member Author

Choose a reason for hiding this comment

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

You sir are building good tests. Do you want to maybe add them as a commit to this PR? I'd merge that for sure.

Comment on lines +219 to +225
pub(crate) fn nlocktime(&self, locktime: LockTime) -> Arc<Self> {
Arc::new(TxBuilder {
locktime: Some(locktime),
..self.clone()
})
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

looks great, wrote a test and passed ✅

Comment on lines +228 to +234
pub(crate) fn allow_dust(&self, allow_dust: bool) -> Arc<Self> {
Arc::new(TxBuilder {
allow_dust,
..self.clone()
})
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

looks great as well, wrote a test and passed ✅

Comment on lines +237 to +243
pub(crate) fn version(&self, version: i32) -> Arc<Self> {
Arc::new(TxBuilder {
version: Some(version),
..self.clone()
})
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Was able to write two tests (one for version 1 and one for version 2) and both passed, looks good!

Copy link
Collaborator

@reez reez left a comment

Choose a reason for hiding this comment

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

did a review, left comments for each function I tested. the only one I have outstanding questions on is add_data, so just requesting changes atm but I could be wrong on my side

good job with these tho!

@reez
Copy link
Collaborator

reez commented Nov 25, 2024

ACK d0bec77

Wouldn't mind seeing docstrings added for these, but not a blocker-

@thunderbiscuit
Copy link
Member Author

@reez docstrings are absolutely required at this point thanks for reminding me and feel free put your foot down a little harder when I forget and use the full might of the full stop character just kidding that would hurt my feelings.

But jokes aside I do expect PRs going forward to have docstrings so please hold me to it. We've gotten out of the habit but it's not an excuse.

@reez
Copy link
Collaborator

reez commented Nov 25, 2024

  • I've added docs for the new feature

Haha, no problem, sounds good, my plan is to just look for this checkbox to be ✅ on PR's with new methods from now on

@thunderbiscuit thunderbiscuit merged commit 4f702eb into bitcoindevkit:master Nov 26, 2024
27 checks passed
@thunderbiscuit thunderbiscuit deleted the feature/add-txbuilder-apis branch November 26, 2024 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants