-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathevent.rs
86 lines (75 loc) · 2.29 KB
/
event.rs
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
// Copyright 2020 Kodebox, Inc.
// This file is part of CodeChain.
//
// This program 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.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::db::Key;
use coordinator::types::Event;
use ctypes::{BlockHash, TxHash};
use primitives::H256;
use rlp::{Decodable, Encodable, Rlp, RlpStream};
use std::hash::Hash;
use std::ops::Deref;
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum EventSource {
Block(BlockHash),
Transaction(TxHash),
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Events(pub Vec<Event>);
impl Encodable for Events {
fn rlp_append(&self, s: &mut RlpStream) {
s.append_list(&self.0);
}
}
impl Decodable for Events {
fn decode(rlp: &Rlp) -> Result<Self, rlp::DecoderError> {
Ok(Events(rlp.as_list()?))
}
}
impl Key<Events> for EventSource {
type Target = H256;
fn key(&self) -> H256 {
match self {
EventSource::Block(hash) => *hash.deref(),
EventSource::Transaction(hash) => *hash.deref(),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct EventsWithSource {
pub source: EventSource,
pub events: Vec<Event>,
}
#[cfg(test)]
mod tests {
use rlp::rlp_encode_and_decode_test;
use super::*;
#[test]
fn encode_and_decode_events() {
let event1 = Event {
key: "key1".to_string(),
value: vec![1, 2, 3, 4, 5],
};
let event2 = Event {
key: "key2".to_string(),
value: vec![2, 3, 4, 5, 6],
};
let event3 = Event {
key: "key3".to_string(),
value: vec![3, 4, 5, 6, 7],
};
let events = Events(vec![event1, event2, event3]);
rlp_encode_and_decode_test!(events);
}
}