Skip to content

Commit f306b75

Browse files
committed
store: Asyncify remaining methods in block_store
Also, remove unused update_chain_genesis_hash
1 parent 4a6f8a0 commit f306b75

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

node/src/manager/commands/chain.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::network_setup::Networks;
3838
pub async fn list(primary: ConnectionPool, store: BlockStore) -> Result<(), Error> {
3939
let mut chains = {
4040
let mut conn = primary.get_async().await?;
41-
block_store::load_chains(&mut conn)?
41+
block_store::load_chains(&mut conn).await?
4242
};
4343
chains.sort_by_key(|chain| chain.name.clone());
4444

@@ -124,7 +124,8 @@ pub async fn info(
124124

125125
let mut conn = primary.get_async().await?;
126126

127-
let chain = block_store::find_chain(&mut conn, &name)?
127+
let chain = block_store::find_chain(&mut conn, &name)
128+
.await?
128129
.ok_or_else(|| anyhow!("unknown chain: {}", name))?;
129130

130131
let chain_store = store
@@ -233,7 +234,8 @@ pub async fn change_block_cache_shard(
233234

234235
let mut conn = primary_store.get_async().await?;
235236

236-
let chain = find_chain(&mut conn, &chain_name)?
237+
let chain = find_chain(&mut conn, &chain_name)
238+
.await?
237239
.ok_or_else(|| anyhow!("unknown chain: {}", chain_name))?;
238240
let old_shard = chain.shard;
239241

@@ -250,7 +252,7 @@ pub async fn change_block_cache_shard(
250252
async {
251253
let shard = Shard::new(shard.to_string())?;
252254

253-
let chain = BlockStore::allocate_chain(conn, &chain_name, &shard, &ident)?;
255+
let chain = BlockStore::allocate_chain(conn, &chain_name, &shard, &ident).await?;
254256

255257
graph::block_on(store.add_chain_store(&chain,ChainStatus::Ingestible, true))?;
256258

@@ -261,10 +263,10 @@ pub async fn change_block_cache_shard(
261263
.execute(conn)?;
262264

263265
// Update the current chain name to chain-old
264-
update_chain_name(conn, &chain_name, &new_name)?;
266+
update_chain_name(conn, &chain_name, &new_name).await?;
265267

266268
// Create a new chain with the name in the destination shard
267-
let _ = add_chain(conn, &chain_name, &shard, ident)?;
269+
let _ = add_chain(conn, &chain_name, &shard, ident).await?;
268270

269271
// Re-add the foreign key constraint
270272
sql_query(

store/postgres/src/block_store.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ pub mod primary {
5454
use std::convert::TryFrom;
5555

5656
use diesel::{
57-
delete, insert_into,
58-
r2d2::{ConnectionManager, PooledConnection},
59-
update, ExpressionMethods, OptionalExtension, PgConnection, QueryDsl, RunQueryDsl,
57+
delete, insert_into, update, ExpressionMethods, OptionalExtension, PgConnection, QueryDsl,
58+
RunQueryDsl,
6059
};
6160
use graph::{
6261
blockchain::{BlockHash, ChainIdentifier},
@@ -109,18 +108,21 @@ pub mod primary {
109108
}
110109
}
111110

112-
pub fn load_chains(conn: &mut PgConnection) -> Result<Vec<Chain>, StoreError> {
111+
pub async fn load_chains(conn: &mut PgConnection) -> Result<Vec<Chain>, StoreError> {
113112
Ok(chains::table.load(conn)?)
114113
}
115114

116-
pub fn find_chain(conn: &mut PgConnection, name: &str) -> Result<Option<Chain>, StoreError> {
115+
pub async fn find_chain(
116+
conn: &mut PgConnection,
117+
name: &str,
118+
) -> Result<Option<Chain>, StoreError> {
117119
Ok(chains::table
118120
.filter(chains::name.eq(name))
119121
.first(conn)
120122
.optional()?)
121123
}
122124

123-
pub fn add_chain(
125+
pub async fn add_chain(
124126
conn: &mut PgConnection,
125127
name: &str,
126128
shard: &Shard,
@@ -165,7 +167,7 @@ pub mod primary {
165167
}
166168

167169
// update chain name where chain name is 'name'
168-
pub fn update_chain_name(
170+
pub async fn update_chain_name(
169171
conn: &mut PgConnection,
170172
name: &str,
171173
new_name: &str,
@@ -175,17 +177,6 @@ pub mod primary {
175177
.execute(conn)?;
176178
Ok(())
177179
}
178-
179-
pub fn update_chain_genesis_hash(
180-
conn: &mut PooledConnection<ConnectionManager<PgConnection>>,
181-
name: &str,
182-
hash: BlockHash,
183-
) -> Result<(), StoreError> {
184-
update(chains::table.filter(chains::name.eq(name)))
185-
.set(chains::genesis_block_hash.eq(hash.hash_hex()))
186-
.execute(conn)?;
187-
Ok(())
188-
}
189180
}
190181

191182
/// The store that chains use to maintain their state and cache often used
@@ -265,7 +256,7 @@ impl BlockStore {
265256

266257
let mirror = PrimaryMirror::new(&pools);
267258
let existing_chains = mirror
268-
.read_async(|conn| async { primary::load_chains(conn) }.scope_boxed())
259+
.read_async(|conn| primary::load_chains(conn).scope_boxed())
269260
.await?;
270261
let chain_head_cache = TimedCache::new(CHAIN_HEAD_CACHE_TTL);
271262
let chains = shards.clone();
@@ -345,7 +336,7 @@ impl BlockStore {
345336
self.mirror.primary().query_permit().await
346337
}
347338

348-
pub fn allocate_chain(
339+
pub async fn allocate_chain(
349340
conn: &mut PgConnection,
350341
name: &String,
351342
shard: &Shard,
@@ -463,7 +454,7 @@ impl BlockStore {
463454
self.mirror
464455
.read_async(|conn| {
465456
async {
466-
match primary::find_chain(conn, &chain)? {
457+
match primary::find_chain(conn, &chain).await? {
467458
Some(chain) => {
468459
let chain_store = this
469460
.add_chain_store(&chain, ChainStatus::ReadOnly, false)
@@ -617,7 +608,7 @@ impl BlockStore {
617608
}
618609
})
619610
.ok_or_else(|| anyhow!("unable to find shard for network {}", network))?;
620-
let chain = primary::add_chain(&mut conn, &network, &shard, ident)?;
611+
let chain = primary::add_chain(&mut conn, &network, &shard, ident).await?;
621612
self.add_chain_store(&chain, ChainStatus::Ingestible, true)
622613
.await
623614
.map_err(anyhow::Error::from)

0 commit comments

Comments
 (0)