1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
crate::ix!();

pub trait IsRBFOptIn {

    /**
      | Check if transaction is RBF opt in.
      |
      */
    fn is_rbf_opt_in(&mut self, tx: &Transaction) -> RBFTransactionState;
}


//-------------------------------------------[.cpp/bitcoin/src/policy/rbf.h]

/**
  | Maximum number of transactions that
  | can be replaced by BIP125 RBF (Rule #5).
  | This includes all mempool conflicts
  | and their descendants.
  |
  */
pub const MAX_BIP125_REPLACEMENT_CANDIDATES: u32 = 100;

/**
  | The rbf state of unconfirmed transactions
  |
  */
pub enum RBFTransactionState {

    /**
      | Unconfirmed tx that does not signal
      | rbf and is not in the mempool
      |
      */
    UNKNOWN,

    /**
      | Either this tx or a mempool ancestor
      | signals rbf
      |
      */
    REPLACEABLE_BIP125,

    /**
      | Neither this tx nor a mempool ancestor
      | signals rbf
      |
      */
    FINAL,
}

//-------------------------------------------[.cpp/bitcoin/src/policy/rbf.cpp]

pub fn is_rbf_opt_in_empty_mempool(tx: &Transaction) -> RBFTransactionState {
    
    todo!();
        /*
            // If we don't have a local mempool we can only check the transaction itself.
        return SignalsOptInRBF(tx) ? RBFTransactionState::REPLACEABLE_BIP125 : RBFTransactionState::UNKNOWN;
        */
}

/**
  | Enforce BIP125 Rule #3 "The replacement
  | transaction pays an absolute fee of
  | at least the sum paid by the original
  | transactions." Enforce BIP125 Rule
  | #4 "The replacement transaction must
  | also pay for its own bandwidth at or above
  | the rate set by the node's minimum relay
  | fee setting."
  | 
  | -----------
  | @param[in] original_fees
  | 
  | Total modified fees of original transaction(s).
  | ----------
  | @param[in] replacement_fees
  | 
  | Total modified fees of replacement
  | transaction(s).
  | ----------
  | @param[in] replacement_vsize
  | 
  | Total virtual size of replacement transaction(s).
  | ----------
  | @param[in] relay_fee
  | 
  | The node's minimum feerate for transaction
  | relay.
  | ----------
  | @param[in] txid
  | 
  | Transaction ID, included in the error
  | message if violation occurs.
  | 
  | -----------
  | @return
  | 
  | error string if fees are insufficient,
  | otherwise std::nullopt.
  |
  */
pub fn pays_forrbf(
        original_fees:     Amount,
        replacement_fees:  Amount,
        replacement_vsize: usize,
        relay_fee:         FeeRate,
        txid:              &u256) -> Option<String> {
    
    todo!();
        /*
            // BIP125 Rule #3: The replacement fees must be greater than or equal to fees of the
        // transactions it replaces, otherwise the bandwidth used by those conflicting transactions
        // would not be paid for.
        if (replacement_fees < original_fees) {
            return strprintf("rejecting replacement %s, less fees than conflicting txs; %s < %s",
                             txid.ToString(), FormatMoney(replacement_fees), FormatMoney(original_fees));
        }

        // BIP125 Rule #4: The new transaction must pay for its own bandwidth. Otherwise, we have a DoS
        // vector where attackers can cause a transaction to be replaced (and relayed) repeatedly by
        // increasing the fee by tiny amounts.
        CAmount additional_fees = replacement_fees - original_fees;
        if (additional_fees < relay_fee.GetFee(replacement_vsize)) {
            return strprintf("rejecting replacement %s, not enough additional fees to relay; %s < %s",
                             txid.ToString(),
                             FormatMoney(additional_fees),
                             FormatMoney(relay_fee.GetFee(replacement_vsize)));
        }
        return std::nullopt;
        */
}

//-------------------------------------------[.cpp/bitcoin/src/util/rbf.h]
//-------------------------------------------[.cpp/bitcoin/src/util/rbf.cpp]

pub const MAX_BIP125_RBF_SEQUENCE: u32 = 0xfffffffd;

/**
  | Check whether the sequence numbers
  | on this transaction are signaling opt-in
  | to replace-by-fee, according to BIP
  | 125. Allow opt-out of transaction replacement
  | by setting nSequence >
  | 
  | MAX_BIP125_RBF_SEQUENCE (SEQUENCE_FINAL-2)
  | on all inputs.
  | 
  | SEQUENCE_FINAL-1 is picked to still
  | allow use of nLockTime by non-replaceable
  | transactions. All inputs rather than
  | just one is for the sake of multi-party
  | protocols, where we don't want a single
  | party to be able to disable replacement
  | by opting out in their own input.
  |
  */
pub fn signals_opt_inrbf(tx: &Transaction) -> bool {
    
    todo!();
        /*
            for (const CTxIn &txin : tx.vin) {
            if (txin.nSequence <= MAX_BIP125_RBF_SEQUENCE) {
                return true;
            }
        }
        return false;
        */
}