Skip to content

Commit

Permalink
Add explicit RBF setting / checking according to BIP125
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrtj committed Oct 7, 2023
1 parent 431ec12 commit eab8740
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/Bitcoin/Crypto/Transaction.pm
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,40 @@ sub fee_rate
return $fee->as_float / $size;
}

signature_for set_rbf => (
method => Object,
positional => [PositiveOrZeroInt, {default => 0}],
);

sub set_rbf
{
my ($self, $input_index) = @_;

# rules according to BIP125
# https://github.com/bitcoin/bips/blob/master/bip-0125.mediawiki
if (!$self->has_rbf) {
$self->inputs->[$input_index]->set_sequence_no(0xffffffff - 2);
}

return $self;
}

signature_for has_rbf => (
method => Object,
positional => [],
);

sub has_rbf
{
my ($self) = @_;

foreach my $input (@{$self->inputs}) {
return !!1 if $input->sequence_no < 0xffffffff - 1;
}

return !!0;
}

signature_for virtual_size => (
method => Object,
positional => [],
Expand Down
8 changes: 8 additions & 0 deletions t/40-transaction/01-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,13 @@ subtest 'should update UTXOs' => sub {
} 'Bitcoin::Crypto::Exception::UTXO';
};

# run this test last, as changing sequence changes transaction id (collides with UTXO test)
subtest 'should handle RBF' => sub {
is $tx->has_rbf, !!0, 'no RBF ok';
$tx->set_rbf;
is $tx->has_rbf, !!1, 'RBF ok';
is $tx->inputs->[0]->sequence_no, 0xffffffff - 2, 'RBF set ok';
};

done_testing;

0 comments on commit eab8740

Please sign in to comment.