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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

//! Blockchain filter

use crate::{epoch::EpochNumber, log_entry::LogEntry};
use cfx_types::{Address, Bloom, BloomInput, Space, H256};
use std::{
    error, fmt,
    ops::{Deref, DerefMut},
};

#[derive(Debug, PartialEq, Clone)]
/// Errors concerning log filtering.
pub enum FilterError {
    /// Filter has wrong epoch numbers set.
    InvalidEpochNumber {
        from_epoch: u64,
        to_epoch: u64,
    },

    /// Filter has wrong block numbers set.
    InvalidBlockNumber {
        from_block: u64,
        to_block: u64,
    },

    OutOfBoundEpochNumber {
        to_epoch: u64,
        max_epoch: u64,
    },

    EpochNumberGapTooLarge {
        from_epoch: u64,
        to_epoch: u64,
        max_gap: u64,
    },

    BlockNumberGapTooLarge {
        from_block: u64,
        to_block: u64,
        max_gap: u64,
    },

    /// Roots for verifying the requested epochs are unavailable.
    UnableToVerify {
        epoch: u64,
        latest_verifiable: u64,
    },

    /// The block requested does not exist
    UnknownBlock {
        hash: H256,
    },

    /// Epoch cannot be served as it was already pruned from db on a full node
    EpochAlreadyPruned {
        epoch: u64,
        min: u64,
    },

    /// Block cannot be served as it was already pruned from db on a full node
    // Use this when the corresponding epoch is not known.
    BlockAlreadyPruned {
        block_hash: H256,
    },

    /// Block has not been executed yet
    BlockNotExecutedYet {
        block_hash: H256,
    },

    /// There was a pivot chain reorganization during log filtering
    PivotChainReorg {
        epoch: u64,
        from: H256,
        to: H256,
    },

    /// Filter error with custom error message (e.g. timeout)
    Custom(String),
}

impl fmt::Display for FilterError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::FilterError::*;
        let msg = match *self {
            InvalidEpochNumber {
                from_epoch,
                to_epoch,
            } => format! {
                "Filter has wrong epoch numbers set (from: {}, to: {})",
                from_epoch, to_epoch
            },
            InvalidBlockNumber {
                from_block,
                to_block,
            } => format! {
                "Filter has wrong block numbers set (from: {}, to: {})",
                from_block, to_block
            },
            OutOfBoundEpochNumber {
                to_epoch,
                max_epoch,
            } => format! {
                "Filter to_epoch is larger than the current best_epoch (to: {}, max: {})",
                to_epoch, max_epoch,
            },
            EpochNumberGapTooLarge {
                from_epoch,
                to_epoch,
                max_gap,
            } => {
                format! {
                    "The gap between from_epoch and to_epoch is larger than max_gap \
                    (from: {}, to: {}, max_gap: {})",
                    from_epoch, to_epoch, max_gap
                }
            }
            BlockNumberGapTooLarge {
                from_block,
                to_block,
                max_gap,
            } => {
                format! {
                    "The gap between from_block and to_block is larger than max_gap \
                    (from: {}, to: {}, max_gap: {})",
                    from_block, to_block, max_gap
                }
            }
            UnableToVerify {
                epoch,
                latest_verifiable,
            } => format! {
                "Unable to verify epoch {} (latest verifiable epoch is {})",
                epoch, latest_verifiable
            },
            UnknownBlock { hash } => format! {
                "Unable to identify block {:?}", hash
            },
            EpochAlreadyPruned { epoch, min } => format! {
                "Epoch is smaller than the earliest epoch stored (epoch: {}, min: {})",
                epoch, min,
            },
            BlockAlreadyPruned { block_hash } => format! {
                "Block {:?} has been pruned from db", block_hash,
            },
            BlockNotExecutedYet { block_hash } => format! {
                "Block {:?} is not executed yet", block_hash,
            },
            PivotChainReorg { epoch, from, to } => format! {
                "Pivot chain at epoch {} has been reorganized during log filtering: {:?} -> {:?}. Operation terminated to avoid inconsistent results.",
                epoch, from, to,
            },
            Custom(ref s) => s.clone(),
        };

        f.write_fmt(format_args!("Filter error: {}", msg))
    }
}

impl error::Error for FilterError {
    fn description(&self) -> &str { "Filter error" }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum LogFilter {
    EpochLogFilter {
        from_epoch: EpochNumber,
        to_epoch: EpochNumber,
        params: LogFilterParams,
    },
    BlockHashLogFilter {
        block_hashes: Vec<H256>,
        params: LogFilterParams,
    },
    BlockNumberLogFilter {
        from_block: u64,
        to_block: u64,
        params: LogFilterParams,
    },
}

/// Log event Filter.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct LogFilterParams {
    /// Search addresses.
    ///
    /// If None, match all.
    /// If specified, log must be produced by one of these addresses.
    pub address: Option<Vec<Address>>,

    /// Search topics.
    ///
    /// If None, match all.
    /// If specified, log must contain one of these topics.
    pub topics: Vec<Option<Vec<H256>>>,

    /// Indicate if the log filter can be trusted, so we do not need to check
    /// other fields.
    ///
    /// It is `false` if the Filter is constructed from RPCs,
    /// and `true` if it is generated within the process with trusted logics.
    pub trusted: bool,

    /// Space: Conflux or Ethereum.
    ///
    /// Log must be produced in this space.
    pub space: Space,
}

impl Default for LogFilterParams {
    fn default() -> Self {
        LogFilterParams {
            address: None,
            topics: vec![None, None, None, None],
            trusted: false,
            space: Space::Native,
        }
    }
}

impl Default for LogFilter {
    fn default() -> Self {
        LogFilter::EpochLogFilter {
            from_epoch: EpochNumber::LatestCheckpoint,
            to_epoch: EpochNumber::LatestState,
            params: Default::default(),
        }
    }
}

impl Deref for LogFilter {
    type Target = LogFilterParams;

    fn deref(&self) -> &Self::Target {
        match &self {
            &LogFilter::EpochLogFilter { params, .. } => params,
            &LogFilter::BlockHashLogFilter { params, .. } => params,
            &LogFilter::BlockNumberLogFilter { params, .. } => params,
        }
    }
}

impl DerefMut for LogFilter {
    fn deref_mut(&mut self) -> &mut Self::Target {
        match self {
            LogFilter::EpochLogFilter { params, .. } => params,
            LogFilter::BlockHashLogFilter { params, .. } => params,
            LogFilter::BlockNumberLogFilter { params, .. } => params,
        }
    }
}

impl LogFilterParams {
    /// Returns combinations of each address and topic.
    pub fn bloom_possibilities(&self) -> Vec<Bloom> {
        let blooms = match self.address {
            Some(ref addresses) if !addresses.is_empty() => addresses
                .iter()
                .map(|ref address| {
                    Bloom::from(BloomInput::Raw(address.as_bytes()))
                })
                .collect(),
            _ => vec![Bloom::default()],
        };

        self.topics.iter().fold(blooms, |bs, topic| match *topic {
            None => bs,
            Some(ref topics) => bs
                .into_iter()
                .flat_map(|bloom| {
                    topics
                        .iter()
                        .map(|topic| {
                            let mut b = bloom.clone();
                            b.accrue(BloomInput::Raw(topic.as_bytes()));
                            b
                        })
                        .collect::<Vec<Bloom>>()
                })
                .collect(),
        })
    }

    /// Returns true if given log entry matches filter.
    pub fn matches(&self, log: &LogEntry) -> bool {
        if log.space != self.space {
            return false;
        }

        let matches = match self.address {
            Some(ref addresses) if !addresses.is_empty() => {
                addresses.iter().any(|address| &log.address == address)
            }
            _ => true,
        };

        matches
            && self
                .topics
                .iter()
                .enumerate()
                .all(|(i, topic)| match *topic {
                    Some(ref topics) if !topics.is_empty() => topics
                        .iter()
                        .any(|topic| log.topics.get(i) == Some(topic)),
                    _ => true,
                })
    }
}

impl From<String> for FilterError {
    fn from(s: String) -> Self { FilterError::Custom(s) }
}