Skip to content

Commit c8f629a

Browse files
committed
graph: Add deduplicate triggers test
1 parent 9967168 commit c8f629a

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

chain/ethereum/src/tests.rs

+96
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,99 @@ fn test_trigger_ordering() {
112112
vec![log1, log2, call1, log3, call2, call4, call3, block2, block1]
113113
);
114114
}
115+
116+
#[test]
117+
fn test_trigger_dedup() {
118+
let block1 = EthereumTrigger::Block(
119+
BlockPtr::from((H256::random(), 1u64)),
120+
EthereumBlockTriggerType::Every,
121+
);
122+
123+
let block2 = EthereumTrigger::Block(
124+
BlockPtr::from((H256::random(), 0u64)),
125+
EthereumBlockTriggerType::WithCallTo(Address::random()),
126+
);
127+
128+
// duplicate block2
129+
let block3 = block2.clone();
130+
131+
let mut call1 = EthereumCall::default();
132+
call1.transaction_index = 1;
133+
let call1 = EthereumTrigger::Call(Arc::new(call1));
134+
135+
let mut call2 = EthereumCall::default();
136+
call2.transaction_index = 2;
137+
let call2 = EthereumTrigger::Call(Arc::new(call2));
138+
139+
let mut call3 = EthereumCall::default();
140+
call3.transaction_index = 3;
141+
let call3 = EthereumTrigger::Call(Arc::new(call3));
142+
143+
// duplicate call2
144+
let mut call4 = EthereumCall::default();
145+
call4.transaction_index = 2;
146+
let call4 = EthereumTrigger::Call(Arc::new(call4));
147+
148+
fn create_log(tx_index: u64, log_index: u64) -> Arc<Log> {
149+
Arc::new(Log {
150+
address: H160::default(),
151+
topics: vec![],
152+
data: Bytes::default(),
153+
block_hash: Some(H256::zero()),
154+
block_number: Some(U64::zero()),
155+
transaction_hash: Some(H256::zero()),
156+
transaction_index: Some(tx_index.into()),
157+
log_index: Some(log_index.into()),
158+
transaction_log_index: Some(log_index.into()),
159+
log_type: Some("".into()),
160+
removed: Some(false),
161+
})
162+
}
163+
164+
let log1 = EthereumTrigger::Log(create_log(1, 0), None);
165+
let log2 = EthereumTrigger::Log(create_log(1, 1), None);
166+
let log3 = EthereumTrigger::Log(create_log(2, 5), None);
167+
// duplicate logs 2 and 3
168+
let log4 = log2.clone();
169+
let log5 = log3.clone();
170+
171+
let triggers = vec![
172+
// Call triggers
173+
call3.clone(),
174+
call1.clone(),
175+
call2.clone(),
176+
call4.clone(),
177+
// Block triggers
178+
block3.clone(),
179+
block2.clone(),
180+
block1.clone(),
181+
// Event triggers
182+
log5.clone(),
183+
log4.clone(),
184+
log3.clone(),
185+
log2.clone(),
186+
log1.clone(),
187+
];
188+
189+
let logger = Logger::root(slog::Discard, o!());
190+
191+
let mut b: LightEthereumBlock = Default::default();
192+
193+
// This is necessary because inside of BlockWithTriggers::new
194+
// there's a log for both fields. So just using Default above
195+
// gives None on them.
196+
b.number = Some(Default::default());
197+
b.hash = Some(Default::default());
198+
199+
// Test that `BlockWithTriggers` sorts the triggers.
200+
let block_with_triggers = BlockWithTriggers::<crate::Chain>::new(
201+
BlockFinality::Final(Arc::new(b)),
202+
triggers,
203+
&logger,
204+
);
205+
206+
assert_eq!(
207+
block_with_triggers.trigger_data,
208+
vec![log1, log2, call1, log3, call2, call3, block2, block1]
209+
);
210+
}

0 commit comments

Comments
 (0)