-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathethereum.rs
More file actions
234 lines (208 loc) · 7.46 KB
/
ethereum.rs
File metadata and controls
234 lines (208 loc) · 7.46 KB
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
use std::marker::PhantomData;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use super::{
CommonChainConfig, MutexBlockStreamBuilder, NoopAdapterSelector, NoopRuntimeAdapterBuilder,
StaticBlockRefetcher, StaticStreamBuilder, Stores, TestChain, test_ptr,
};
use graph::abi;
use graph::blockchain::block_stream::BlockWithTriggers;
use graph::blockchain::block_stream::{EntityOperationKind, EntitySourceOperation};
use graph::blockchain::client::ChainClient;
use graph::blockchain::{BlockPtr, Trigger, TriggersAdapterSelector};
use graph::cheap_clone::CheapClone;
use graph::data_source::subgraph;
use graph::prelude::alloy::primitives::{Address, B256, U256, keccak256};
use graph::prelude::alloy::rpc::types::BlockTransactions;
use graph::prelude::{
DeploymentHash, ENV_VARS, Entity, LightEthereumBlock, create_dummy_transaction,
create_minimal_block_for_test,
};
use graph::schema::EntityType;
use graph_chain_ethereum::network::EthereumNetworkAdapters;
use graph_chain_ethereum::trigger::LogRef;
use graph_chain_ethereum::{Chain, chain::ChainSettings};
use graph_chain_ethereum::{
chain::BlockFinality,
trigger::{EthereumBlockTriggerType, EthereumTrigger},
};
pub async fn chain(
test_name: &str,
blocks: Vec<BlockWithTriggers<Chain>>,
stores: &Stores,
triggers_adapter: Option<Arc<dyn TriggersAdapterSelector<Chain>>>,
) -> TestChain<Chain> {
let triggers_adapter = triggers_adapter.unwrap_or(Arc::new(NoopAdapterSelector {
triggers_in_block_sleep: Duration::ZERO,
x: PhantomData,
}));
let CommonChainConfig {
logger_factory,
mock_registry,
chain_store,
firehose_endpoints,
} = CommonChainConfig::new(test_name, stores).await;
let client = Arc::new(ChainClient::<Chain>::new_firehose(firehose_endpoints));
let static_block_stream = Arc::new(StaticStreamBuilder { chain: blocks });
let block_stream_builder = Arc::new(MutexBlockStreamBuilder(Mutex::new(static_block_stream)));
let eth_adapters = Arc::new(EthereumNetworkAdapters::empty_for_testing());
let chain = Chain::new(
logger_factory,
stores.network_name.clone(),
mock_registry,
chain_store.cheap_clone(),
chain_store,
client,
stores.chain_head_listener.cheap_clone(),
block_stream_builder.clone(),
Arc::new(StaticBlockRefetcher { x: PhantomData }),
triggers_adapter,
Arc::new(NoopRuntimeAdapterBuilder {}),
eth_adapters,
ENV_VARS.reorg_threshold(),
// We assume the tested chain is always ingestible for now
true,
Arc::new(ChainSettings::from_env_defaults()),
);
TestChain {
chain: Arc::new(chain),
block_stream_builder,
}
}
pub fn genesis() -> BlockWithTriggers<graph_chain_ethereum::Chain> {
let ptr = test_ptr(0);
let block = create_minimal_block_for_test(ptr.number as u64, ptr.hash.as_b256());
BlockWithTriggers::<graph_chain_ethereum::Chain> {
block: BlockFinality::Final(Arc::new(LightEthereumBlock::new(block))),
trigger_data: vec![Trigger::Chain(EthereumTrigger::Block(
ptr,
EthereumBlockTriggerType::End,
))],
}
}
pub fn generate_empty_blocks_for_range(
parent_ptr: BlockPtr,
start: i32,
end: i32,
add_to_hash: u64, // Use to differentiate forks
) -> Vec<BlockWithTriggers<Chain>> {
let mut blocks: Vec<BlockWithTriggers<Chain>> = vec![];
for i in start..(end + 1) {
let parent_ptr = blocks.last().map(|b| b.ptr()).unwrap_or(parent_ptr.clone());
let ptr = BlockPtr {
number: i,
hash: B256::from(U256::from(i as u64 + add_to_hash)).into(),
};
blocks.push(empty_block(parent_ptr, ptr));
}
blocks
}
pub fn empty_block(parent_ptr: BlockPtr, ptr: BlockPtr) -> BlockWithTriggers<Chain> {
assert!(ptr != parent_ptr);
assert!(ptr.number > parent_ptr.number);
let dummy_txn =
create_dummy_transaction(ptr.number as u64, ptr.hash.as_b256(), Some(0), B256::ZERO);
let transactions = BlockTransactions::Full(vec![dummy_txn]);
let alloy_block = create_minimal_block_for_test(ptr.number as u64, ptr.hash.as_b256())
.map_header(|mut header| {
// Ensure the parent hash matches the given parent_ptr so that parent_ptr() lookups succeed
header.inner.parent_hash = parent_ptr.hash.as_b256();
header
})
.with_transactions(transactions);
BlockWithTriggers::<graph_chain_ethereum::Chain> {
block: BlockFinality::Final(Arc::new(LightEthereumBlock::new(alloy_block))),
trigger_data: vec![Trigger::Chain(EthereumTrigger::Block(
ptr,
EthereumBlockTriggerType::End,
))],
}
}
pub fn push_test_log(block: &mut BlockWithTriggers<Chain>, payload: impl Into<String>) {
use graph::prelude::alloy::{self, primitives::LogData, rpc::types::Log};
let log = Arc::new(Log {
inner: alloy::primitives::Log {
address: Address::ZERO,
data: LogData::new_unchecked(
vec![keccak256(b"TestEvent(string)")],
abi::DynSolValue::String(payload.into()).abi_encode().into(),
),
},
block_hash: Some(B256::from_slice(block.ptr().hash.as_slice())),
block_number: Some(block.ptr().number as u64),
transaction_hash: Some(B256::from(U256::from(0))),
transaction_index: Some(0),
log_index: Some(0),
block_timestamp: None,
removed: false,
});
block
.trigger_data
.push(Trigger::Chain(EthereumTrigger::Log(LogRef::FullLog(
log, None,
))))
}
pub fn push_test_subgraph_trigger(
block: &mut BlockWithTriggers<Chain>,
source: DeploymentHash,
entity: Entity,
entity_type: EntityType,
entity_op: EntityOperationKind,
vid: i64,
source_idx: u32,
) {
let entity = EntitySourceOperation {
entity,
entity_type,
entity_op,
vid,
};
block
.trigger_data
.push(Trigger::Subgraph(subgraph::TriggerData {
source,
entity,
source_idx,
}));
}
pub fn push_test_command(
block: &mut BlockWithTriggers<Chain>,
test_command: impl Into<String>,
data: impl Into<String>,
) {
use graph::prelude::alloy::{self, primitives::LogData, rpc::types::Log};
let log = Arc::new(Log {
inner: alloy::primitives::Log {
address: Address::ZERO,
data: LogData::new_unchecked(
vec![keccak256(b"TestEvent(string,string)")],
abi::DynSolValue::Tuple(vec![
abi::DynSolValue::String(test_command.into()),
abi::DynSolValue::String(data.into()),
])
.abi_encode_params()
.into(),
),
},
block_hash: Some(block.ptr().hash.as_b256()),
block_number: Some(block.ptr().number as u64),
transaction_hash: Some(B256::from(U256::from(0))),
transaction_index: Some(0),
log_index: Some(0),
block_timestamp: None,
removed: false,
});
block
.trigger_data
.push(Trigger::Chain(EthereumTrigger::Log(LogRef::FullLog(
log, None,
))))
}
pub fn push_test_polling_trigger(block: &mut BlockWithTriggers<Chain>) {
block
.trigger_data
.push(Trigger::Chain(EthereumTrigger::Block(
block.ptr(),
EthereumBlockTriggerType::End,
)))
}