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

Document & bolster calculate_fees #14

Open
DanGould opened this issue Nov 17, 2022 · 6 comments
Open

Document & bolster calculate_fees #14

DanGould opened this issue Nov 17, 2022 · 6 comments
Labels
bug Something isn't working

Comments

@DanGould
Copy link

calculate fees seems to work for a subset of possible transaction creations. What do the magic numbers mean? I'd like to add the ability to batch another arbitrary output. Nolooking also failed to meet min relayfee in an off by one error when sending from sparrow (payjoin/nolooking#34)

loptos/src/main.rs

Lines 187 to 195 in 4390bb0

fn calculate_fees(channel_count: u64, fee_rate: u64, has_additional_output: bool) -> bitcoin::Amount {
let additional_vsize = if has_additional_output {
channel_count * (8 + 1 + 1 + 32)
} else {
(channel_count - 1) * (8 + 1 + 1 + 32) + 12
};
bitcoin::Amount::from_sat(fee_rate * additional_vsize)
}

@DanGould
Copy link
Author

channel_count * (8 + 1 + 1 + 32) // <8 invariant bytes = 4 version + 4 locktime> OP_0 OP_PUSHBYTES_32 <32 byte script>

@DanGould
Copy link
Author

This check_fees fn in the bip78 crate is relevant. I'm still not sure what those +12 bytes are for when the output has no additional output 🤔

https://github.com/Kixunil/payjoin/blob/150dd33b9c60a4e1c219955b9af2e5aca0a43bec/bip78/src/sender/mod.rs#L196-L225

@DanGould
Copy link
Author

The public key hash in P2WPKH is 20 bytes; The script hash in P2WSH is 32 bytes. 32-20 is 12 when the static reserve output is substituted with a channel funding tx

@DanGould
Copy link
Author

DanGould commented Nov 17, 2022

I think this fee calculation assumes only + 1 additional input and + 1 additional output while the args support more than one channel to be queued.

@DanGould
Copy link
Author

DanGould commented Nov 17, 2022

Could 8 + 1 + 1 + 32 be wrong? I think OP_0 OP_PUSHBYTES_32 <32 byte script> is actually 34 bytes

@Kixunil
Copy link
Owner

Kixunil commented Nov 17, 2022

Oh, yeah, it seems to be wrong. 8 is for amount, 1 should be for the length of the script (compact size), 1 for OP_0, 1 for OP_PUSHBYTES32 and 32 for the hash. So off-by one.

But this still assumes there are less than 253 channels which is fine for now but this needs to be implemented correctly long term. It should go to payjoin crate anyway.

@Kixunil Kixunil added the bug Something isn't working label Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants