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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//! SPV event mapper.
#![allow(clippy::manual_range_contains, clippy::new_without_default)]

pub mod utxos;

#[cfg(test)]
mod tests;

use std::collections::HashSet;
use std::{fmt, net};

use nakamoto_common::bitcoin::{Block, Txid};
use nakamoto_common::block::{BlockHash, Height};
use nakamoto_net::event::Emitter;
use nakamoto_p2p as p2p;
use p2p::protocol;

use crate::client::Event;

/// Transaction status of a given transaction.
#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq)]
pub enum TxStatus {
    /// This is the initial state of a transaction after it has been announced by the
    /// client.
    Unconfirmed,
    /// Transaction was acknowledged by a peer.
    ///
    /// This is the case when a peer requests the transaction data from us after an inventory
    /// announcement. It does not mean the transaction is considered valid by the peer.
    Acknowledged {
        /// Peer acknowledging the transaction.
        peer: net::SocketAddr,
    },
    /// Transaction was included in a block. This event is fired after
    /// a block from the main chain is scanned.
    Confirmed {
        /// Height at which it was included.
        height: Height,
        /// Hash of the block in which it was included.
        block: BlockHash,
    },
    /// A transaction that was previously confirmed, and is now reverted due to a
    /// re-org. Note that this event can only fire if the originally confirmed tx
    /// is still in memory.
    Reverted,
    /// Transaction was replaced by another transaction, and will probably never
    /// be included in a block. This can happen if an RBF transaction is replaced by one with
    /// a higher fee, or if a transaction is reverted and a conflicting transaction replaces
    /// it. In this case it would be preceded by a [`TxStatus::Reverted`] status.
    Stale {
        /// Transaction replacing the given transaction and causing it to be stale.
        replaced_by: Txid,
        /// Block of the included transaction.
        block: BlockHash,
    },
}

impl fmt::Display for TxStatus {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Unconfirmed => write!(fmt, "transaction is unconfirmed"),
            Self::Acknowledged { peer } => {
                write!(fmt, "transaction was acknowledged by peer {}", peer)
            }
            Self::Confirmed { height, block } => write!(
                fmt,
                "transaction was included in block {} at height {}",
                block, height
            ),
            Self::Reverted => write!(fmt, "transaction has been reverted"),
            Self::Stale { replaced_by, block } => write!(
                fmt,
                "transaction was replaced by {} in block {}",
                replaced_by, block
            ),
        }
    }
}

/// Event mapper for SPV and client events.
/// Consumes protocol events and emits [`Event`].
pub struct Mapper {
    /// Best height known.
    tip: Height,
    /// The height up to which we've processed filters and matching blocks.
    sync_height: Height,
    /// The height up to which we've processed filters.
    /// This is usually going to be greater than `sync_height`.
    filter_height: Height,
    /// The height up to which we've processed matching blocks.
    /// This is always going to be lesser or equal to `filter_height`.
    block_height: Height,
    /// Filter heights that have been matched, and for which we are awaiting a block to process.
    pending: HashSet<Height>,
}

impl Mapper {
    /// Create a new SPV event mapper.
    pub fn new() -> Self {
        let tip = 0;
        let sync_height = 0;
        let filter_height = 0;
        let block_height = 0;
        let pending = HashSet::new();

        Self {
            tip,
            sync_height,
            filter_height,
            block_height,
            pending,
        }
    }

    /// Process protocol event and map it to client event(s).
    pub fn process(&mut self, event: protocol::Event, emitter: &Emitter<Event>) {
        match event {
            protocol::Event::Ready {
                height,
                filter_height,
                ..
            } => {
                emitter.emit(Event::Ready {
                    tip: height,
                    filter_tip: filter_height,
                });
            }
            protocol::Event::Peer(protocol::PeerEvent::Connected(addr, link)) => {
                emitter.emit(Event::PeerConnected { addr, link });
            }
            protocol::Event::Peer(protocol::PeerEvent::ConnectionFailed(addr, error)) => {
                emitter.emit(Event::PeerConnectionFailed { addr, error });
            }
            protocol::Event::Peer(protocol::PeerEvent::Negotiated {
                addr,
                link,
                services,
                user_agent,
                height,
                version,
            }) => {
                emitter.emit(Event::PeerNegotiated {
                    addr,
                    link,
                    services,
                    user_agent,
                    height,
                    version,
                });
            }
            protocol::Event::Peer(protocol::PeerEvent::Disconnected(addr, reason)) => {
                emitter.emit(Event::PeerDisconnected { addr, reason });
            }
            protocol::Event::Chain(protocol::ChainEvent::PeerHeightUpdated { height }) => {
                emitter.emit(Event::PeerHeightUpdated { height });
            }
            protocol::Event::Chain(protocol::ChainEvent::Synced(_, height)) => {
                self.tip = height;
            }
            protocol::Event::Chain(protocol::ChainEvent::BlockConnected { header, height }) => {
                emitter.emit(Event::BlockConnected {
                    header,
                    hash: header.block_hash(),
                    height,
                });
            }
            protocol::Event::Chain(protocol::ChainEvent::BlockDisconnected { header, height }) => {
                emitter.emit(Event::BlockDisconnected {
                    header,
                    hash: header.block_hash(),
                    height,
                });
            }
            protocol::Event::Inventory(protocol::InventoryEvent::BlockProcessed {
                block,
                height,
                fees,
            }) => {
                let hash = self.process_block(block, height, emitter);

                if let Some(fees) = fees {
                    emitter.emit(Event::FeeEstimated {
                        block: hash,
                        height,
                        fees,
                    });
                }
            }
            protocol::Event::Inventory(protocol::InventoryEvent::Confirmed {
                transaction,
                height,
                block,
            }) => {
                emitter.emit(Event::TxStatusChanged {
                    txid: transaction.txid(),
                    status: TxStatus::Confirmed { height, block },
                });
            }
            protocol::Event::Inventory(protocol::InventoryEvent::Acknowledged { txid, peer }) => {
                emitter.emit(Event::TxStatusChanged {
                    txid,
                    status: TxStatus::Acknowledged { peer },
                });
            }
            protocol::Event::Filter(protocol::FilterEvent::RescanStarted { start, .. }) => {
                self.pending.clear();

                self.filter_height = start;
                self.sync_height = start;
                self.block_height = start;
            }
            protocol::Event::Filter(protocol::FilterEvent::FilterProcessed {
                block,
                height,
                matched,
                valid,
                ..
            }) => {
                self.process_filter(block, height, matched, valid, emitter);
            }
            _ => {}
        }
        assert!(
            self.block_height <= self.filter_height,
            "Filters are processed before blocks"
        );
        assert!(
            self.sync_height <= self.filter_height,
            "Filters are processed before we are done"
        );

        // If we have no blocks left to process, we are synced to the height of the last
        // processed filter. Otherwise, we're synced up to the last processed block.
        let height = if self.pending.is_empty() {
            self.filter_height
        } else {
            self.block_height
        };

        // Ensure we only broadcast sync events when the sync height has changed.
        if height > self.sync_height {
            self.sync_height = height;

            emitter.emit(Event::Synced {
                height,
                tip: self.tip,
            });
        }
    }

    // PRIVATE METHODS /////////////////////////////////////////////////////////

    // TODO: Instead of receiving the block, fetch it if matched.
    fn process_block(
        &mut self,
        block: Block,
        height: Height,
        emitter: &Emitter<Event>,
    ) -> BlockHash {
        let hash = block.block_hash();

        if !self.pending.remove(&height) {
            // Received unexpected block.
            return hash;
        }

        log::debug!("Received block {} at height {}", hash, height);
        debug_assert!(height >= self.block_height);

        self.block_height = height;

        emitter.emit(Event::BlockMatched {
            height,
            hash,
            header: block.header,
            transactions: block.txdata,
        });

        hash
    }

    fn process_filter(
        &mut self,
        block: BlockHash,
        height: Height,
        matched: bool,
        valid: bool,
        emitter: &Emitter<Event>,
    ) {
        debug_assert!(height >= self.filter_height);

        if matched {
            log::debug!("Filter matched for block #{}", height);
            self.pending.insert(height);
        }
        self.filter_height = height;

        emitter.emit(Event::FilterProcessed {
            height,
            matched,
            valid,
            block,
        });
    }
}