Skip to content

Commit

Permalink
Arc::try_unwrap -> Arc::into_inner
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimenez committed Jan 16, 2025
1 parent cbcb20e commit b531bf7
Show file tree
Hide file tree
Showing 27 changed files with 82 additions and 85 deletions.
7 changes: 3 additions & 4 deletions crates/cli/commands/src/init_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> InitStateC

let hash = init_from_state_dump(reader, provider_rw.clone(), config.stages.etl)?;

match Arc::try_unwrap(provider_rw) {
Ok(provider) => provider.commit()?,
Err(_) => eyre::bail!("could not unwrap provider"),
};
let provider =
Arc::into_inner(provider_rw).ok_or_else(|| eyre::eyre!("could not unwrap provider"))?;
provider.commit()?;

info!(target: "reth::cli", hash = ?hash, "Genesis block written");
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions crates/cli/commands/src/recover/storage_tries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C>
);
}

match Arc::try_unwrap(provider) {
Ok(provider) => provider.commit()?,
Err(_) => eyre::bail!("could not unwrap provider"),
};
let provider =
Arc::into_inner(provider).ok_or_else(|| eyre::eyre!("could not unwrap provider"))?;
provider.commit()?;

info!(target: "reth::cli", deleted = deleted_tries, "Finished recovery");

Ok(())
Expand Down
6 changes: 4 additions & 2 deletions crates/cli/commands/src/stage/dump/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@ fn unwind_and_copy<N: ProviderNodeTypes>(
},
)?;

let unwind_inner_tx = Arc::try_unwrap(provider)
.unwrap_or(db_tool.provider_factory.database_provider_rw()?)
let unwind_inner_tx = Arc::into_inner(provider)
.unwrap_or_else(|| {
db_tool.provider_factory.database_provider_rw().expect("failed to create new provider")
})
.into_tx();

output_db
Expand Down
6 changes: 4 additions & 2 deletions crates/cli/commands/src/stage/dump/hashing_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ fn unwind_and_copy<N: ProviderNodeTypes>(
bad_block: None,
},
)?;
let unwind_inner_tx = Arc::try_unwrap(provider)
.unwrap_or(db_tool.provider_factory.database_provider_rw()?)
let unwind_inner_tx = Arc::into_inner(provider)
.unwrap_or_else(|| {
db_tool.provider_factory.database_provider_rw().expect("failed to create new provider")
})
.into_tx();

output_db.update(|tx| tx.import_table::<tables::PlainAccountState, _>(&unwind_inner_tx))??;
Expand Down
6 changes: 4 additions & 2 deletions crates/cli/commands/src/stage/dump/hashing_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ fn unwind_and_copy<N: ProviderNodeTypes>(
bad_block: None,
},
)?;
let unwind_inner_tx = Arc::try_unwrap(provider)
.unwrap_or(db_tool.provider_factory.database_provider_rw()?)
let unwind_inner_tx = Arc::into_inner(provider)
.unwrap_or_else(|| {
db_tool.provider_factory.database_provider_rw().expect("failed to create new provider")
})
.into_tx();

// TODO optimize we can actually just get the entries we need for both these tables
Expand Down
6 changes: 4 additions & 2 deletions crates/cli/commands/src/stage/dump/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ fn unwind_and_copy<N: ProviderNodeTypes>(
.execute(provider.clone(), execute_input)
.unwrap();

let unwind_inner_tx = Arc::try_unwrap(provider)
.unwrap_or(db_tool.provider_factory.database_provider_rw()?)
let unwind_inner_tx = Arc::into_inner(provider)
.unwrap_or_else(|| {
db_tool.provider_factory.database_provider_rw().expect("failed to create new provider")
})
.into_tx();

// TODO optimize we can actually just get the entries we need
Expand Down
7 changes: 3 additions & 4 deletions crates/cli/commands/src/stage/unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C>
provider.save_finalized_block_number(target)?;
}

match Arc::try_unwrap(provider) {
Ok(provider) => provider.commit()?,
Err(_) => eyre::bail!("could not unwrap provider"),
};
let provider = Arc::into_inner(provider)
.ok_or_else(|| eyre::eyre!("could not unwrap provider"))?;
provider.commit()?;
}

info!(target: "reth::cli", ?target, "Unwound blocks");
Expand Down
7 changes: 3 additions & 4 deletions crates/optimism/cli/src/commands/init_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ impl<C: ChainSpecParser<ChainSpec = OpChainSpec>> InitStateCommandOp<C> {
let reader = BufReader::new(reth_fs_util::open(self.init_state.state)?);
let hash = init_from_state_dump(reader, provider_rw.clone(), config.stages.etl)?;

match Arc::try_unwrap(provider_rw) {
Ok(provider) => provider.commit()?,
Err(_) => eyre::bail!("could not unwrap provider"),
};
let provider =
Arc::into_inner(provider_rw).ok_or_else(|| eyre::eyre!("could not unwrap provider"))?;
provider.commit()?;

info!(target: "reth::cli", hash = ?hash, "Genesis block written");
Ok(())
Expand Down
9 changes: 3 additions & 6 deletions crates/prune/prune/src/pruner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,9 @@ where
pub fn run(&mut self, tip_block_number: BlockNumber) -> PrunerResult {
let provider = Arc::new(self.provider_factory.database_provider_rw()?);
let result = self.run_with_provider(provider.clone(), tip_block_number);
match Arc::try_unwrap(provider) {
Ok(provider) => provider.commit()?,
Err(_) => {
return Err(PrunerError::Provider(reth_errors::ProviderError::Commit));
}
};
let provider = Arc::into_inner(provider)
.ok_or(PrunerError::Provider(reth_errors::ProviderError::Commit))?;
provider.commit()?;
result
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/prune/prune/src/segments/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ mod tests {
result.checkpoint.unwrap().as_prune_checkpoint(prune_mode),
)
.unwrap();
Arc::try_unwrap(provider).unwrap().commit().expect("commit");
Arc::into_inner(provider).unwrap().commit().expect("commit");

let last_pruned_block_number = blocks
.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/prune/prune/src/segments/static_file/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ mod tests {
result.checkpoint.unwrap().as_prune_checkpoint(prune_mode),
)
.unwrap();
Arc::try_unwrap(provider).unwrap().commit().expect("commit");
Arc::into_inner(provider).unwrap().commit().expect("commit");

let last_pruned_block_number = to_block.min(
next_block_number_to_prune +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ mod tests {
result.checkpoint.unwrap().as_prune_checkpoint(prune_mode),
)
.unwrap();
Arc::try_unwrap(provider).unwrap().commit().expect("commit");
Arc::into_inner(provider).unwrap().commit().expect("commit");

let last_pruned_tx_number = blocks
.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/prune/prune/src/segments/user/account_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod tests {
result.checkpoint.unwrap().as_prune_checkpoint(prune_mode),
)
.unwrap();
Arc::try_unwrap(provider).unwrap().commit().expect("commit");
Arc::into_inner(provider).unwrap().commit().expect("commit");

let changesets = changesets
.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/prune/prune/src/segments/user/receipts_by_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ mod tests {
limiter,
},
);
Arc::try_unwrap(provider).unwrap().commit().expect("commit");
Arc::into_inner(provider).unwrap().commit().expect("commit");

assert_matches!(result, Ok(_));
let output = result.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/prune/prune/src/segments/user/sender_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ mod tests {
result.checkpoint.unwrap().as_prune_checkpoint(prune_mode),
)
.unwrap();
Arc::try_unwrap(provider).unwrap().commit().expect("commit");
Arc::into_inner(provider).unwrap().commit().expect("commit");

let last_pruned_block_number = last_pruned_block_number
.checked_sub(if result.progress.is_finished() { 0 } else { 1 });
Expand Down
2 changes: 1 addition & 1 deletion crates/prune/prune/src/segments/user/storage_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ mod tests {
result.checkpoint.unwrap().as_prune_checkpoint(prune_mode),
)
.unwrap();
Arc::try_unwrap(provider).unwrap().commit().expect("commit");
Arc::into_inner(provider).unwrap().commit().expect("commit");

let changesets = changesets
.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/prune/prune/src/segments/user/transaction_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ mod tests {
result.checkpoint.unwrap().as_prune_checkpoint(prune_mode),
)
.unwrap();
Arc::try_unwrap(provider).unwrap().commit().expect("commit");
Arc::into_inner(provider).unwrap().commit().expect("commit");

let last_pruned_block_number = last_pruned_block_number
.checked_sub(if result.progress.is_finished() { 0 } else { 1 });
Expand Down
2 changes: 1 addition & 1 deletion crates/stages/stages/benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn measure_stage<F, S>(
.await
.and_then(|_| stage.execute(provider.clone(), input))
.unwrap();
Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();
},
)
});
Expand Down
6 changes: 3 additions & 3 deletions crates/stages/stages/benches/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(crate) fn stage_unwind<
})
.unwrap();

Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();
})
});
}
Expand All @@ -85,7 +85,7 @@ where
AccountHashingStage::default().execute(provider.clone(), input).unwrap();
StorageHashingStage::default().execute(provider.clone(), input).unwrap();

Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();
}

// Helper for generating testdata for the benchmarks.
Expand Down Expand Up @@ -184,7 +184,7 @@ pub(crate) fn txs_testdata(num_blocks: u64) -> TestStageDB {
let root = {
let provider = Arc::new(db.factory.provider_rw().unwrap());
let root = StateRoot::from_provider(provider.clone()).root().unwrap();
Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();
root
};

Expand Down
12 changes: 6 additions & 6 deletions crates/stages/stages/src/stages/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ mod tests {
receipts_writer.increment_block(0).unwrap();
receipts_writer.commit().unwrap();
}
Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();

// insert pre state
let provider = factory.provider_rw().unwrap();
Expand Down Expand Up @@ -902,7 +902,7 @@ mod tests {

let provider = Arc::new(provider);
let output = execution_stage.execute(provider.clone(), input).unwrap();
Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();

assert_matches!(output, ExecOutput {
checkpoint: StageCheckpoint {
Expand Down Expand Up @@ -969,7 +969,7 @@ mod tests {
UnwindInput { checkpoint: output.checkpoint, unwind_to: 0, bad_block: None },
)
.unwrap();
Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();
}
}

Expand Down Expand Up @@ -1038,7 +1038,7 @@ mod tests {

let provider = Arc::new(provider);
let result = execution_stage.execute(provider.clone(), input).unwrap();
Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();

// Test Unwind
let mut provider = factory.database_provider_rw().unwrap();
Expand All @@ -1054,7 +1054,7 @@ mod tests {
)
.unwrap();

Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();

assert_matches!(result, UnwindOutput {
checkpoint: StageCheckpoint {
Expand Down Expand Up @@ -1158,7 +1158,7 @@ mod tests {
let provider = Arc::new(test_db.factory.database_provider_rw().unwrap());
let mut execution_stage = stage();
let _ = execution_stage.execute(provider.clone(), input).unwrap();
Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();

// assert unwind stage
let provider = test_db.factory.database_provider_rw().unwrap();
Expand Down
6 changes: 3 additions & 3 deletions crates/stages/stages/src/stages/index_account_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod tests {
let provider = Arc::new(db.factory.database_provider_rw().unwrap());
let out = stage.execute(provider.clone(), input).unwrap();
assert_eq!(out, ExecOutput { checkpoint: StageCheckpoint::new(run_to), done: true });
Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();
}

fn unwind(db: &TestStageDB, unwind_from: u64, unwind_to: u64) {
Expand All @@ -238,7 +238,7 @@ mod tests {
let provider = Arc::new(db.factory.database_provider_rw().unwrap());
let out = stage.unwind(provider.clone(), input).unwrap();
assert_eq!(out, UnwindOutput { checkpoint: StageCheckpoint::new(unwind_to) });
Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -485,7 +485,7 @@ mod tests {
let provider = Arc::new(db.factory.database_provider_rw().unwrap());
let out = stage.execute(provider.clone(), input).unwrap();
assert_eq!(out, ExecOutput { checkpoint: StageCheckpoint::new(20000), done: true });
Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();

// verify
let table = cast(db.table::<tables::AccountsHistory>().unwrap());
Expand Down
6 changes: 3 additions & 3 deletions crates/stages/stages/src/stages/index_storage_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ mod tests {
let provider = Arc::new(db.factory.database_provider_rw().unwrap());
let out = stage.execute(provider.clone(), input).unwrap();
assert_eq!(out, ExecOutput { checkpoint: StageCheckpoint::new(run_to), done: true });
Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();
}

fn unwind(db: &TestStageDB, unwind_from: u64, unwind_to: u64) {
Expand All @@ -257,7 +257,7 @@ mod tests {
let provider = Arc::new(db.factory.database_provider_rw().unwrap());
let out = stage.unwind(provider.clone(), input).unwrap();
assert_eq!(out, UnwindOutput { checkpoint: StageCheckpoint::new(unwind_to) });
Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -507,7 +507,7 @@ mod tests {
let provider = Arc::new(db.factory.database_provider_rw().unwrap());
let out = stage.execute(provider.clone(), input).unwrap();
assert_eq!(out, ExecOutput { checkpoint: StageCheckpoint::new(20000), done: true });
Arc::try_unwrap(provider).unwrap().commit().unwrap();
Arc::into_inner(provider).unwrap().commit().unwrap();

// verify
let table = cast(db.table::<tables::StoragesHistory>().unwrap());
Expand Down
4 changes: 2 additions & 2 deletions crates/stages/stages/src/test_utils/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(crate) trait ExecuteStageTestRunner: StageTestRunner {
let result = stage.execute_ready(input).await.and_then(|_| {
let provider_rw = Arc::new(db.database_provider_rw().unwrap());
let result = stage.execute(provider_rw.clone(), input);
Arc::try_unwrap(provider_rw).unwrap().commit().expect("failed to commit");
Arc::into_inner(provider_rw).unwrap().commit().expect("failed to commit");
result
});
tx.send(result).expect("failed to send message")
Expand All @@ -80,7 +80,7 @@ pub(crate) trait UnwindStageTestRunner: StageTestRunner {
tokio::spawn(async move {
let provider = Arc::new(db.database_provider_rw().unwrap());
let result = stage.unwind(provider.clone(), input);
Arc::try_unwrap(provider).unwrap().commit().expect("failed to commit");
Arc::into_inner(provider).unwrap().commit().expect("failed to commit");
tx.send(result).expect("failed to send result");
});
rx.await.unwrap()
Expand Down
15 changes: 5 additions & 10 deletions crates/storage/provider/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,9 @@ pub fn insert_genesis<N: ProviderNodeTypes<ChainSpec = ChainSpec>>(

// get the inner provider by consuming the Arc
// this is safe because we know we have the only reference at this point
match Arc::try_unwrap(provider) {
Ok(provider) => {
provider.commit()?;
Ok(root)
}
Err(_) => {
// this should never happen in practice since we have the only Arc,
Err(reth_db::DatabaseError::Other("Failed to unwrap Arc<Provider>".into()).into())
}
}
let provider = Arc::into_inner(provider)
// this should never happen in practice since we have the only Arc,
.ok_or_else(|| reth_db::DatabaseError::Other("Failed to unwrap Arc<Provider>".into()))?;
provider.commit()?;
Ok(root)
}
Loading

0 comments on commit b531bf7

Please sign in to comment.