-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathchain.rs
More file actions
396 lines (345 loc) · 12 KB
/
chain.rs
File metadata and controls
396 lines (345 loc) · 12 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
use std::sync::Arc;
use diesel::sql_query;
use diesel::Connection;
use diesel::PgConnection;
use diesel::RunQueryDsl;
use graph::blockchain::BlockHash;
use graph::blockchain::BlockPtr;
use graph::blockchain::ChainIdentifier;
use graph::cheap_clone::CheapClone;
use graph::components::network_provider::ChainName;
use graph::components::store::ChainIdStore;
use graph::components::store::StoreError;
use graph::prelude::BlockNumber;
use graph::prelude::ChainStore as _;
use graph::prelude::LightEthereumBlockExt;
use graph::prelude::{anyhow, anyhow::bail};
use graph::slog::Logger;
use graph::{
components::store::BlockStore as _, components::store::ChainHeadStore as _,
prelude::anyhow::Error,
};
use graph_chain_ethereum::chain::BlockFinality;
use graph_chain_ethereum::EthereumAdapter;
use graph_chain_ethereum::EthereumAdapterTrait as _;
use graph_store_postgres::add_chain;
use graph_store_postgres::block_store::primary::Chain as PrimaryChain;
use graph_store_postgres::find_chain;
use graph_store_postgres::update_chain_name;
use graph_store_postgres::BlockStore;
use graph_store_postgres::ChainStatus;
use graph_store_postgres::ChainStore;
use graph_store_postgres::PoolCoordinator;
use graph_store_postgres::Shard;
use graph_store_postgres::{command_support::catalog::block_store, ConnectionPool};
use crate::network_setup::Networks;
pub async fn list(primary: ConnectionPool, store: Arc<BlockStore>) -> Result<(), Error> {
let mut chains = {
let mut conn = primary.get()?;
block_store::load_chains(&mut conn)?
};
chains.sort_by_key(|chain| chain.name.clone());
if !chains.is_empty() {
println!(
"{:^20} | {:^10} | {:^10} | {:^7} | {:^10}",
"name", "shard", "namespace", "version", "head block"
);
println!(
"{:-^20}-+-{:-^10}-+-{:-^10}-+-{:-^7}-+-{:-^10}",
"", "", "", "", ""
);
}
for chain in chains {
let head_block = match store.chain_store(&chain.name) {
None => "no chain".to_string(),
Some(chain_store) => chain_store
.chain_head_ptr()
.await?
.map(|ptr| ptr.number.to_string())
.unwrap_or("none".to_string()),
};
println!(
"{:<20} | {:<10} | {:<10} | {:>7} | {:>10}",
chain.name, chain.shard, chain.storage, chain.net_version, head_block
);
}
Ok(())
}
pub async fn clear_call_cache(
chain_store: Arc<ChainStore>,
from: i32,
to: i32,
) -> Result<(), Error> {
println!(
"Removing entries for blocks from {from} to {to} from the call cache for `{}`",
chain_store.chain
);
chain_store.clear_call_cache(from, to).await?;
Ok(())
}
pub async fn clear_stale_call_cache(
chain_store: Arc<ChainStore>,
ttl_days: i32,
ttl_max_contracts: Option<i64>,
) -> Result<(), Error> {
println!(
"Removing stale entries from the call cache for `{}`",
chain_store.chain
);
chain_store
.clear_stale_call_cache(ttl_days, ttl_max_contracts)
.await?;
Ok(())
}
pub async fn info(
primary: ConnectionPool,
store: Arc<BlockStore>,
name: String,
offset: BlockNumber,
hashes: bool,
) -> Result<(), Error> {
fn row(label: &str, value: impl std::fmt::Display) {
println!("{:<16} | {}", label, value);
}
fn print_ptr(label: &str, ptr: Option<BlockPtr>, hashes: bool) {
match ptr {
None => {
row(label, "ø");
}
Some(ptr) => {
row(label, ptr.number);
if hashes {
row("", ptr.hash);
}
}
}
}
let mut conn = primary.get()?;
let chain = block_store::find_chain(&mut conn, &name)?
.ok_or_else(|| anyhow!("unknown chain: {}", name))?;
let chain_store = store
.chain_store(&chain.name)
.ok_or_else(|| anyhow!("unknown chain: {}", name))?;
let head_block = chain_store.cheap_clone().chain_head_ptr().await?;
let ancestor = match &head_block {
None => None,
Some(head_block) => chain_store
.ancestor_block(head_block.clone(), offset, None)
.await?
.map(|x| x.1),
};
row("name", chain.name);
row("shard", chain.shard);
row("namespace", chain.storage);
row("net_version", chain.net_version);
if hashes {
row("genesis", chain.genesis_block);
}
print_ptr("head block", head_block, hashes);
row("reorg threshold", offset);
print_ptr("reorg ancestor", ancestor, hashes);
Ok(())
}
pub fn remove(primary: ConnectionPool, store: Arc<BlockStore>, name: String) -> Result<(), Error> {
let sites = {
let mut conn =
graph_store_postgres::command_support::catalog::Connection::new(primary.get()?);
conn.find_sites_for_network(&name)?
};
if !sites.is_empty() {
println!(
"there are {} deployments using chain {}:",
sites.len(),
name
);
for site in sites {
println!("{:<8} | {} ", site.namespace, site.deployment);
}
bail!("remove all deployments using chain {} first", name);
}
store.drop_chain(&name)?;
Ok(())
}
pub async fn update_chain_genesis(
networks: &Networks,
coord: Arc<PoolCoordinator>,
store: Arc<dyn ChainIdStore>,
logger: &Logger,
chain_id: ChainName,
genesis_hash: BlockHash,
force: bool,
) -> Result<(), Error> {
let ident = networks.chain_identifier(logger, &chain_id).await?;
if !genesis_hash.eq(&ident.genesis_block_hash) {
println!(
"Expected adapter for chain {} to return genesis hash {} but got {}",
chain_id, genesis_hash, ident.genesis_block_hash
);
if !force {
println!("Not performing update");
return Ok(());
} else {
println!("--force used, updating anyway");
}
}
println!("Updating shard...");
// Update the local shard's genesis, whether or not it is the primary.
// The chains table is replicated from the primary and keeps another genesis hash.
// To keep those in sync we need to update the primary and then refresh the shard tables.
store.set_chain_identifier(
&chain_id,
&ChainIdentifier {
net_version: ident.net_version.clone(),
genesis_block_hash: genesis_hash,
},
)?;
// Refresh the new values
println!("Refresh mappings");
crate::manager::commands::database::remap(&coord, None, None, false).await?;
Ok(())
}
struct ChainSwapOutcome {
latest_backup_name: String,
previous_backup_final_name: Option<String>,
reused_previous_backup: bool,
allocated_chain: Option<PrimaryChain>,
}
fn next_backup_name(conn: &mut PgConnection, chain: &str, base: &str) -> Result<String, StoreError> {
let mut suffix = 0usize;
loop {
let candidate = if suffix == 0 {
format!("{chain}-{base}")
} else {
format!("{chain}-{base}-{suffix}")
};
if find_chain(conn, &candidate)?.is_none() {
return Ok(candidate);
}
suffix += 1;
}
}
pub fn change_block_cache_shard(
primary_store: ConnectionPool,
store: Arc<BlockStore>,
chain_name: String,
shard: String,
) -> Result<(), Error> {
println!("Changing block cache shard for {} to {}", chain_name, shard);
let mut conn = primary_store.get()?;
let chain = find_chain(&mut conn, &chain_name)?
.ok_or_else(|| anyhow!("unknown chain: {}", chain_name))?;
let old_shard = chain.shard;
let canonical_backup_name = format!("{chain_name}-old");
let existing_backup = find_chain(&mut conn, &canonical_backup_name)?;
let had_existing_backup = existing_backup.is_some();
let existing_backup_store = store.chain_store(&canonical_backup_name);
println!("Current shard: {}", old_shard);
let chain_store = store
.chain_store(&chain_name)
.ok_or_else(|| anyhow!("unknown chain: {}", &chain_name))?;
let ident = chain_store.chain_identifier()?;
let target_shard = Shard::new(shard.clone())?;
let reuse_existing_backup = existing_backup
.as_ref()
.map(|backup| backup.shard.as_str() == target_shard.as_str())
.unwrap_or(false);
let outcome = conn.transaction(|conn| -> Result<ChainSwapOutcome, StoreError> {
sql_query(
"alter table deployment_schemas drop constraint deployment_schemas_network_fkey;",
)
.execute(conn)?;
let mut previous_backup_final_name: Option<String> = None;
let temp_backup_name = if let Some(backup) = existing_backup.as_ref() {
let temp_name = next_backup_name(conn, &chain_name, "old")?;
update_chain_name(conn, &backup.name, &temp_name)?;
previous_backup_final_name = Some(temp_name.clone());
Some(temp_name)
} else {
None
};
let latest_backup_name = next_backup_name(conn, &chain_name, "old")?;
update_chain_name(conn, &chain_name, &latest_backup_name)?;
let mut allocated_chain = None;
if reuse_existing_backup {
if let Some(temp_name) = temp_backup_name.as_ref() {
update_chain_name(conn, temp_name, &chain_name)?;
previous_backup_final_name = Some(chain_name.clone());
}
} else {
let created_chain = add_chain(conn, &chain_name, &target_shard, ident.clone())?;
allocated_chain = Some(created_chain);
}
sql_query(
"alter table deployment_schemas add constraint deployment_schemas_network_fkey foreign key (network) references chains(name);",
)
.execute(conn)?;
Ok(ChainSwapOutcome {
latest_backup_name,
previous_backup_final_name,
reused_previous_backup: reuse_existing_backup,
allocated_chain,
})
})?;
let ChainSwapOutcome {
latest_backup_name,
previous_backup_final_name,
reused_previous_backup,
allocated_chain,
} = outcome;
chain_store.update_name(&latest_backup_name)?;
if let (Some(backup_store), Some(final_name)) = (
existing_backup_store.as_ref(),
previous_backup_final_name.as_ref(),
) {
backup_store.update_name(final_name)?;
}
if let Some(ref chain) = allocated_chain {
store.add_chain_store(chain, ChainStatus::Ingestible, true)?;
}
println!(
"Changed block cache shard for {} from {} to {}",
chain_name, old_shard, shard
);
println!("Latest backup recorded as `{}`", latest_backup_name);
if reused_previous_backup {
println!(
"Reused existing backup `{}` as the active `{}` chain",
canonical_backup_name, chain_name
);
} else if let Some(ref preserved) = previous_backup_final_name {
if had_existing_backup {
println!("Preserved earlier backup as `{}`", preserved);
}
}
if allocated_chain.is_some() {
println!(
"Allocated new chain state for `{}` on shard {}",
chain_name, shard
);
}
Ok(())
}
pub async fn ingest(
logger: &Logger,
chain_store: Arc<ChainStore>,
ethereum_adapter: Arc<EthereumAdapter>,
number: BlockNumber,
) -> Result<(), Error> {
let Some(block) = ethereum_adapter
.block_by_number(logger, number)
.await
.map_err(|e| anyhow!("error getting block number {number}: {}", e))?
else {
bail!("block number {number} not found");
};
let ptr = block.block_ptr();
// For inserting the block, it doesn't matter whether the block is final or not.
let block = Arc::new(BlockFinality::Final(Arc::new(block)));
chain_store.upsert_block(block).await?;
let rows = chain_store.confirm_block_hash(ptr.number, &ptr.hash)?;
println!("Inserted block {}", ptr);
if rows > 0 {
println!(" (also deleted {rows} duplicate row(s) with that number)");
}
Ok(())
}