-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathtests.rs
210 lines (177 loc) · 6.4 KB
/
tests.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
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
use std::sync::Arc;
use graph::{
blockchain::{block_stream::BlockWithTriggers, BlockPtr},
prelude::{
web3::types::{Address, Bytes, Log, H160, H256, U64},
EthereumCall, LightEthereumBlock,
},
slog::{self, o, Logger},
};
use crate::{
chain::BlockFinality,
trigger::{EthereumBlockTriggerType, EthereumTrigger},
};
#[test]
fn test_trigger_ordering() {
let block1 = EthereumTrigger::Block(
BlockPtr::from((H256::random(), 1u64)),
EthereumBlockTriggerType::Every,
);
let block2 = EthereumTrigger::Block(
BlockPtr::from((H256::random(), 0u64)),
EthereumBlockTriggerType::WithCallTo(Address::random()),
);
let mut call1 = EthereumCall::default();
call1.transaction_index = 1;
let call1 = EthereumTrigger::Call(Arc::new(call1));
let mut call2 = EthereumCall::default();
call2.transaction_index = 2;
call2.input = Bytes(vec![0]);
let call2 = EthereumTrigger::Call(Arc::new(call2));
let mut call3 = EthereumCall::default();
call3.transaction_index = 3;
let call3 = EthereumTrigger::Call(Arc::new(call3));
// Call with the same tx index as call2
let mut call4 = EthereumCall::default();
call4.transaction_index = 2;
// different than call2 so they don't get mistaken as the same
call4.input = Bytes(vec![1]);
let call4 = EthereumTrigger::Call(Arc::new(call4));
fn create_log(tx_index: u64, log_index: u64) -> Arc<Log> {
Arc::new(Log {
address: H160::default(),
topics: vec![],
data: Bytes::default(),
block_hash: Some(H256::zero()),
block_number: Some(U64::zero()),
transaction_hash: Some(H256::zero()),
transaction_index: Some(tx_index.into()),
log_index: Some(log_index.into()),
transaction_log_index: Some(log_index.into()),
log_type: Some("".into()),
removed: Some(false),
})
}
// Event with transaction_index 1 and log_index 0;
// should be the first element after sorting
let log1 = EthereumTrigger::Log(create_log(1, 0), None);
// Event with transaction_index 1 and log_index 1;
// should be the second element after sorting
let log2 = EthereumTrigger::Log(create_log(1, 1), None);
// Event with transaction_index 2 and log_index 5;
// should come after call1 and before call2 after sorting
let log3 = EthereumTrigger::Log(create_log(2, 5), None);
let triggers = vec![
// Call triggers; these should be in the order 1, 2, 4, 3 after sorting
call3.clone(),
call1.clone(),
call2.clone(),
call4.clone(),
// Block triggers; these should appear at the end after sorting
// but with their order unchanged
block2.clone(),
block1.clone(),
// Event triggers
log3.clone(),
log2.clone(),
log1.clone(),
];
let logger = Logger::root(slog::Discard, o!());
let mut b: LightEthereumBlock = Default::default();
// This is necessary because inside of BlockWithTriggers::new
// there's a log for both fields. So just using Default above
// gives None on them.
b.number = Some(Default::default());
b.hash = Some(Default::default());
// Test that `BlockWithTriggers` sorts the triggers.
let block_with_triggers = BlockWithTriggers::<crate::Chain>::new(
BlockFinality::Final(Arc::new(b)),
triggers,
&logger,
);
assert_eq!(
block_with_triggers.trigger_data,
vec![log1, log2, call1, log3, call2, call4, call3, block2, block1]
);
}
#[test]
fn test_trigger_dedup() {
let block1 = EthereumTrigger::Block(
BlockPtr::from((H256::random(), 1u64)),
EthereumBlockTriggerType::Every,
);
let block2 = EthereumTrigger::Block(
BlockPtr::from((H256::random(), 0u64)),
EthereumBlockTriggerType::WithCallTo(Address::random()),
);
// duplicate block2
let block3 = block2.clone();
let mut call1 = EthereumCall::default();
call1.transaction_index = 1;
let call1 = EthereumTrigger::Call(Arc::new(call1));
let mut call2 = EthereumCall::default();
call2.transaction_index = 2;
let call2 = EthereumTrigger::Call(Arc::new(call2));
let mut call3 = EthereumCall::default();
call3.transaction_index = 3;
let call3 = EthereumTrigger::Call(Arc::new(call3));
// duplicate call2
let mut call4 = EthereumCall::default();
call4.transaction_index = 2;
let call4 = EthereumTrigger::Call(Arc::new(call4));
fn create_log(tx_index: u64, log_index: u64) -> Arc<Log> {
Arc::new(Log {
address: H160::default(),
topics: vec![],
data: Bytes::default(),
block_hash: Some(H256::zero()),
block_number: Some(U64::zero()),
transaction_hash: Some(H256::zero()),
transaction_index: Some(tx_index.into()),
log_index: Some(log_index.into()),
transaction_log_index: Some(log_index.into()),
log_type: Some("".into()),
removed: Some(false),
})
}
let log1 = EthereumTrigger::Log(create_log(1, 0), None);
let log2 = EthereumTrigger::Log(create_log(1, 1), None);
let log3 = EthereumTrigger::Log(create_log(2, 5), None);
// duplicate logs 2 and 3
let log4 = log2.clone();
let log5 = log3.clone();
let triggers = vec![
// Call triggers
call3.clone(),
call1.clone(),
call2.clone(),
call4.clone(),
// Block triggers
block3.clone(),
block2.clone(),
block1.clone(),
// Event triggers
log5.clone(),
log4.clone(),
log3.clone(),
log2.clone(),
log1.clone(),
];
let logger = Logger::root(slog::Discard, o!());
let mut b: LightEthereumBlock = Default::default();
// This is necessary because inside of BlockWithTriggers::new
// there's a log for both fields. So just using Default above
// gives None on them.
b.number = Some(Default::default());
b.hash = Some(Default::default());
// Test that `BlockWithTriggers` sorts the triggers.
let block_with_triggers = BlockWithTriggers::<crate::Chain>::new(
BlockFinality::Final(Arc::new(b)),
triggers,
&logger,
);
assert_eq!(
block_with_triggers.trigger_data,
vec![log1, log2, call1, log3, call2, call3, block2, block1]
);
}