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
//! 
//! ```rust
//! extern crate ethbloom;
//! extern crate rustc_hex;
//! use rustc_hex::FromHex;
//! use ethbloom::{Bloom, Input};
//!
//! fn main() {
//! 	let bloom: Bloom = "00000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000008000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".into();
//! 	let address = "ef2d6d194084c2de36e0dabfce45d046b37d1106".from_hex().unwrap();
//! 	let topic = "02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc".from_hex().unwrap();
//! 	
//! 	let mut my_bloom = Bloom::default();
//! 	assert!(!my_bloom.contains(Input::Raw(&address)));
//! 	assert!(!my_bloom.contains(Input::Raw(&topic)));
//!
//! 	my_bloom.accrue(Input::Raw(&address));
//! 	assert!(my_bloom.contains(Input::Raw(&address)));
//! 	assert!(!my_bloom.contains(Input::Raw(&topic)));
//! 	
//! 	my_bloom.accrue(Input::Raw(&topic));
//! 	assert!(my_bloom.contains(Input::Raw(&address)));
//! 	assert!(my_bloom.contains(Input::Raw(&topic)));
//! 	assert_eq!(my_bloom, bloom);
//! 	}
//! ```
//!

extern crate tiny_keccak;
extern crate rustc_hex;
#[macro_use]
extern crate crunchy;

#[cfg(feature="heapsizeof")]
#[macro_use]
extern crate heapsize;

use std::{ops, fmt, mem, str};
use tiny_keccak::keccak256;
use rustc_hex::{ToHex, FromHex, FromHexError};

// 3 according to yellowpaper
const BLOOM_BITS: u32 = 3;

/// Returns log2.
fn log2(x: usize) -> u32 {
	if x <= 1 {
		return 0;
	}

	let n = x.leading_zeros();
	mem::size_of::<usize>() as u32 * 8 - n
}

pub enum Input<'a> {
	Raw(&'a [u8]),
	Hash(&'a [u8; 32]),
}

enum Hash<'a> {
	Ref(&'a [u8; 32]),
	Owned([u8; 32]),
}

impl<'a> From<Input<'a>> for Hash<'a> {
	fn from(input: Input<'a>) -> Self {
		match input {
			Input::Raw(raw) => Hash::Owned(keccak256(raw)),
			Input::Hash(hash) => Hash::Ref(hash),
		}
	}
}

impl<'a> ops::Index<usize> for Hash<'a> {
	type Output = u8;

	fn index(&self, index: usize) -> &u8 {
		match *self {
			Hash::Ref(r) => &r[index],
			Hash::Owned(ref hash) => &hash[index],
		}
	}
}

impl<'a> Hash<'a> {
	fn len(&self) -> usize {
		match *self {
			Hash::Ref(r) => r.len(),
			Hash::Owned(ref hash) => hash.len(),
		}
	}
}

pub struct Bloom {
	data: [u8; 256],
}

impl Default for Bloom {
	fn default() -> Self {
		Bloom {
			data: [0u8; 256],
		}
	}
}

impl str::FromStr for Bloom {
	type Err = FromHexError;

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		let mut result = Bloom::default();
		let hex = s.from_hex()?;
		if hex.len() != result.data.len() {
			return Err(FromHexError::InvalidHexLength);
		}
		result.data.copy_from_slice(&hex);
		Ok(result)
	}
}

impl PartialEq for Bloom {
	fn eq(&self, other: &Self) -> bool {
		let s_ref: &[u8] = &self.data;
		let o_ref: &[u8] = &other.data;
		s_ref.eq(o_ref)
	}
}

impl Eq for Bloom {}

impl<'a> PartialEq<BloomRef<'a>> for Bloom {
	fn eq(&self, other: &BloomRef<'a>) -> bool {
		let s_ref: &[u8] = &self.data;
		let o_ref: &[u8] = other.data;
		s_ref.eq(o_ref)
	}
}

impl Clone for Bloom {
	fn clone(&self) -> Self {
		let mut result = Bloom::default();
		result.data.copy_from_slice(&self.data);
		result
	}
}

impl fmt::Debug for Bloom {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		f.debug_struct("Bloom")
			.field("data", &self.data.to_hex())
			.finish()
	}
}

impl fmt::Display for Bloom {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		f.write_str(&self.data.to_hex())
	}
}

impl<'a> From<Input<'a>> for Bloom {
	fn from(input: Input<'a>) -> Bloom {
		let mut bloom = Bloom::default();
		bloom.accrue(input);
		bloom
	}
}

impl From<&'static str> for Bloom {
	fn from(s: &'static str) -> Bloom {
		s.parse().expect("&'static str to be valid Bloom")
	}
}

impl From<[u8; 256]> for Bloom {
	fn from(data: [u8; 256]) -> Bloom {
		Bloom {
			data: data
		}
	}
}

impl From<Bloom> for [u8; 256] {
	fn from(bloom: Bloom) -> [u8; 256] {
		bloom.data
	}
}

impl Bloom {
	pub fn is_empty(&self) -> bool {
		self.data.iter().all(|x| *x == 0)
	}

	pub fn contains<'a>(&self, input: Input<'a>) -> bool {
		let bloom: Bloom = input.into();
		self.contains_bloom(&bloom)
	}

	pub fn contains_bloom<'a, B>(&self, bloom: B) -> bool where BloomRef<'a>: From<B> {
		let bloom_ref: BloomRef = bloom.into();
		// workaround for https://github.com/rust-lang/rust/issues/43644
		self.contains_bloom_ref(bloom_ref)
	}

	fn contains_bloom_ref(&self, bloom: BloomRef) -> bool {
		let self_ref: BloomRef = self.into();
		self_ref.contains_bloom(bloom)
	}

	pub fn accrue<'a>(&mut self, input: Input<'a>) {
		let p = BLOOM_BITS;

		let m = self.data.len();
		let bloom_bits = m * 8;
		let mask = bloom_bits - 1;
		let bloom_bytes = (log2(bloom_bits) + 7) / 8;

		let hash: Hash = input.into();

		// must be a power of 2
		assert_eq!(m & (m - 1), 0);
		// out of range
		assert!(p * bloom_bytes <= hash.len() as u32);

		let mut ptr = 0;

		assert_eq!(BLOOM_BITS, 3);
		unroll! {
			for i in 0..3 {
				let _ = i;
				let mut index = 0 as usize;
				for _ in 0..bloom_bytes {
					index = (index << 8) | hash[ptr] as usize;
					ptr += 1;
				}
				index &= mask;
				self.data[m - 1 - index / 8] |= 1 << (index % 8);
			}
		}
	}

	pub fn accrue_bloom<'a, B>(&mut self, bloom: B) where BloomRef<'a>: From<B> {
		let bloom_ref: BloomRef = bloom.into();
		assert_eq!(self.data.len(), 256);
		assert_eq!(bloom_ref.data.len(), 256);
		for i in 0..256 {
			self.data[i] |= bloom_ref.data[i];
		}
	}

	pub fn data(&self) -> &[u8; 256] {
		&self.data
	}
}

#[derive(Clone, Copy)]
pub struct BloomRef<'a> {
	data: &'a [u8; 256],
}

impl<'a> BloomRef<'a> {
	pub fn is_empty(&self) -> bool {
		self.data.iter().all(|x| *x == 0)
	}

	pub fn contains<'b>(&self, input: Input<'b>) -> bool {
		let bloom: Bloom = input.into();
		self.contains_bloom(&bloom)
	}
	
	pub fn contains_bloom<'b, B>(&self, bloom: B) -> bool where BloomRef<'b>: From<B> {
		let bloom_ref: BloomRef = bloom.into();
		assert_eq!(self.data.len(), 256);
		assert_eq!(bloom_ref.data.len(), 256);
		for i in 0..256 {
			let a = self.data[i];
			let b = bloom_ref.data[i];
			if (a & b) != b {
				return false;
			}
		}
		true
	}

	pub fn data(&self) -> &'a [u8; 256] {
		self.data
	}
}

impl<'a> From<&'a [u8; 256]> for BloomRef<'a> {
	fn from(data: &'a [u8; 256]) -> Self {
		BloomRef {
			data: data
		}
	}
}

impl<'a> From<&'a Bloom> for BloomRef<'a> {
	fn from(bloom: &'a Bloom) -> Self {
		BloomRef {
			data: &bloom.data
		}
	}
}

#[cfg(feature="heapsizeof")]
known_heap_size!(0, Bloom);

#[cfg(test)]
mod tests {
	use rustc_hex::FromHex;
	use {Bloom, Input};

	#[test]
	fn it_works() {
		let bloom: Bloom = "00000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000008000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".into();
		let address = "ef2d6d194084c2de36e0dabfce45d046b37d1106".from_hex().unwrap();
		let topic = "02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc".from_hex().unwrap();

		let mut my_bloom = Bloom::default();
		assert!(!my_bloom.contains(Input::Raw(&address)));
		assert!(!my_bloom.contains(Input::Raw(&topic)));

		my_bloom.accrue(Input::Raw(&address));
		assert!(my_bloom.contains(Input::Raw(&address)));
		assert!(!my_bloom.contains(Input::Raw(&topic)));

		my_bloom.accrue(Input::Raw(&topic));
		assert!(my_bloom.contains(Input::Raw(&address)));
		assert!(my_bloom.contains(Input::Raw(&topic)));
		assert_eq!(my_bloom, bloom);
	}
}