-
Notifications
You must be signed in to change notification settings - Fork 0
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
Fix Segwit Fee Calculation #127
base: main
Are you sure you want to change the base?
Changes from 4 commits
1623b4c
dfcac37
30719b8
93ff42c
8a86a6f
50e9371
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,11 +29,22 @@ sealed class Input { | |
_prevScriptPubKey = prevScriptPubKey, | ||
_wittnessScript = wittnessScript; | ||
|
||
BigInt get weight { | ||
if (_scriptSig == null || _prevScriptPubKey == null) return -1.toBI; | ||
return calculateWeight(_prevScriptPubKey!, _scriptSig!); | ||
BigInt get witnessSize { | ||
if (_wittnessScript == null || _wittnessScript!.isEmpty) return 0.toBI; | ||
|
||
BigInt size = getVarIntSize(_wittnessScript!.length) | ||
.toBI; // Count of witness elements | ||
|
||
for (final element in witness!) { | ||
size += getVarIntSize(element.length).toBI; // Size of this element | ||
size += element.length.toBI; // The element itself | ||
} | ||
|
||
return size; | ||
} | ||
|
||
BigInt get weight; | ||
|
||
int get intValue => value != null ? value!.toInt() : 0; | ||
|
||
String? get scriptSigHex => _scriptSig != null ? _scriptSig!.toHex : null; | ||
|
@@ -225,7 +236,7 @@ class BTCInput extends Input { | |
txid.length + | ||
output_index_length + | ||
scriptSig.length + | ||
1 + | ||
getVarIntSize(scriptSig.length) + | ||
sequence_length, | ||
); | ||
|
||
|
@@ -244,6 +255,24 @@ class BTCInput extends Input { | |
|
||
return buffer; | ||
} | ||
|
||
@override | ||
BigInt get weight { | ||
BigInt weight = (txid.length + output_index_length + sequence_length).toBI * | ||
4.toBI; // (32 + 4 + 4) * 4 | ||
|
||
weight += | ||
(scriptSig.length + getVarIntSize(scriptSig.length)).toBI * 4.toBI; | ||
|
||
if (_wittnessScript != null) { | ||
weight += | ||
(_wittnessScript!.length + getVarIntSize(_wittnessScript!.length)) | ||
.toBI * | ||
4.toBI; | ||
} | ||
|
||
return weight; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Weight calculation for witness data needs correction Based on the code review and search results, the current implementation incorrectly applies a weight multiplier of 4 to witness data (_wittnessScript). According to the Segwit specification (BIP141):
The code needs to be modified to multiply _wittnessScript length by 1 instead of 4. Locations needing attention:
🔗 Analysis chainVerify correctness of Segwit weighting 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Searching for weight calculations that multiply witness data by 4
rg -A 3 "weight.*=.*(_wittnessScript|witnessSize).*4"
Length of output: 55 Script: #!/bin/bash
# Let's search for all weight-related calculations in the codebase
rg -A 5 "weight.*=.*\*.*4" lib/
Length of output: 868 Script: #!/bin/bash
# Let's also check for any BIP documentation or comments about Segwit weight calculation
rg -i "segwit.*weight|weight.*segwit" lib/
Length of output: 285 Script: #!/bin/bash
# Let's look at the entire input class implementation to understand the context better
ast-grep --pattern 'class Input {
$$$
}'
Length of output: 44 Script: #!/bin/bash
# Let's also check if there are any test files validating the weight calculation
fd -e dart test_weight -x cat {}
fd -e dart weight_test -x cat {}
Length of output: 73 Script: #!/bin/bash
# Let's try a different pattern to find the Input class implementation
ast-grep --pattern 'class Input {
BigInt get weight {
$$$
}
}'
Length of output: 72 Script: #!/bin/bash
# Let's also check for any test files related to transaction weight or segwit
rg -l "test.*weight|test.*segwit" --type dart
Length of output: 47 Script: #!/bin/bash
# Let's look for any documentation about weight calculation in comments
rg -B 2 -A 2 "weight.*=|weight.*{" --type dart
Length of output: 150 |
||
} | ||
|
||
const value_length = 8; | ||
|
@@ -259,6 +288,12 @@ class EC8Input extends Input { | |
super.wittnessScript, | ||
}); | ||
|
||
@override | ||
BigInt get weight { | ||
if (_scriptSig == null || _prevScriptPubKey == null) return -1.toBI; | ||
return calculateWeight(_prevScriptPubKey!, _scriptSig!); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid returning negative weight - if (_scriptSig == null || _prevScriptPubKey == null) return -1.toBI;
+ if (_scriptSig == null || _prevScriptPubKey == null) {
+ // Consider returning 0 or throwing an informative exception
+ return 0.toBI;
+ }
|
||
EC8Input addScript({ | ||
Uint8List? scriptSig, | ||
Uint8List? wittnessScript, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential missing 'witness' property reference and spelling concern
It looks like line 38 refers to "for (final element in witness!)", but this property or getter named "witness" is not defined within the class. This might be an unintentional reference, a leftover from a refactor, or a missing getter. Also note that the internal field is spelled "_wittnessScript" instead of "_witnessScript." Consider renaming and ensuring code references match.