Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
BlockId removal: refactor: Backend::begin_state_operation (#12541)
Browse files Browse the repository at this point in the history
It changes the arguments of `Backend::begin_state_operation`
from: block: `BlockId<Block>` to: hash: `&Block::Hash`

This PR is part of BlockId::Number refactoring analysis (paritytech/substrate#11292)
  • Loading branch information
michalkucharczyk authored Oct 21, 2022
1 parent e458835 commit 1802a11
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 51 deletions.
2 changes: 1 addition & 1 deletion client/api/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ pub trait Backend<Block: BlockT>: AuxStore + Send + Sync {
fn begin_state_operation(
&self,
operation: &mut Self::BlockImportOperation,
block: BlockId<Block>,
block: &Block::Hash,
) -> sp_blockchain::Result<()>;

/// Commit block insertion.
Expand Down
5 changes: 2 additions & 3 deletions client/api/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,10 +695,9 @@ where
fn begin_state_operation(
&self,
operation: &mut Self::BlockImportOperation,
block: BlockId<Block>,
block: &Block::Hash,
) -> sp_blockchain::Result<()> {
let hash = self.blockchain.expect_block_hash_from_id(&block)?;
operation.old_state = self.state_at(&hash)?;
operation.old_state = self.state_at(block)?;
Ok(())
}

Expand Down
3 changes: 1 addition & 2 deletions client/db/benches/state_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use sc_client_api::{Backend as _, BlockImportOperation, NewBlockState, StateBack
use sc_client_db::{Backend, BlocksPruning, DatabaseSettings, DatabaseSource, PruningMode};
use sp_core::H256;
use sp_runtime::{
generic::BlockId,
testing::{Block as RawBlock, ExtrinsicWrapper, Header},
StateVersion, Storage,
};
Expand Down Expand Up @@ -67,7 +66,7 @@ fn insert_blocks(db: &Backend<Block>, storage: Vec<(Vec<u8>, Vec<u8>)>) -> H256
for i in 0..10 {
let mut op = db.begin_operation().unwrap();

db.begin_state_operation(&mut op, BlockId::Hash(parent_hash)).unwrap();
db.begin_state_operation(&mut op, &parent_hash).unwrap();

let mut header = Header {
number,
Expand Down
73 changes: 30 additions & 43 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1961,13 +1961,12 @@ impl<Block: BlockT> sc_client_api::backend::Backend<Block> for Backend<Block> {
fn begin_state_operation(
&self,
operation: &mut Self::BlockImportOperation,
block: BlockId<Block>,
block: &Block::Hash,
) -> ClientResult<()> {
let hash = self.blockchain.expect_block_hash_from_id(&block)?;
if block.is_pre_genesis() {
if *block == Default::default() {
operation.old_state = self.empty_state()?;
} else {
operation.old_state = self.state_at(&hash)?;
operation.old_state = self.state_at(block)?;
}

operation.commit_state = true;
Expand Down Expand Up @@ -2442,13 +2441,9 @@ pub(crate) mod tests {
};
let header_hash = header.hash();

let block_id = if number == 0 {
BlockId::Hash(Default::default())
} else {
BlockId::Number(number - 1)
};
let block_hash = if number == 0 { Default::default() } else { parent_hash };
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, block_id).unwrap();
backend.begin_state_operation(&mut op, &block_hash).unwrap();
op.set_block_data(header, Some(body), None, None, NewBlockState::Best).unwrap();
if let Some(index) = transaction_index {
op.update_transaction_index(index).unwrap();
Expand Down Expand Up @@ -2489,21 +2484,17 @@ pub(crate) mod tests {
assert!(db.blockchain().hash(i).unwrap().is_none());

{
let id = if i == 0 {
BlockId::Hash(Default::default())
let hash = if i == 0 {
Default::default()
} else {
BlockId::Number(i - 1)
db.blockchain.hash(i - 1).unwrap().unwrap()
};

let mut op = db.begin_operation().unwrap();
db.begin_state_operation(&mut op, id).unwrap();
db.begin_state_operation(&mut op, &hash).unwrap();
let header = Header {
number: i,
parent_hash: if i == 0 {
Default::default()
} else {
db.blockchain.hash(i - 1).unwrap().unwrap()
},
parent_hash: hash,
state_root: Default::default(),
digest: Default::default(),
extrinsics_root: Default::default(),
Expand Down Expand Up @@ -2585,7 +2576,7 @@ pub(crate) mod tests {

{
let mut op = db.begin_operation().unwrap();
db.begin_state_operation(&mut op, BlockId::Number(0)).unwrap();
db.begin_state_operation(&mut op, &hash).unwrap();
let mut header = Header {
number: 1,
parent_hash: hash,
Expand Down Expand Up @@ -2626,9 +2617,7 @@ pub(crate) mod tests {

let hash = {
let mut op = backend.begin_operation().unwrap();
backend
.begin_state_operation(&mut op, BlockId::Hash(Default::default()))
.unwrap();
backend.begin_state_operation(&mut op, &Default::default()).unwrap();
let mut header = Header {
number: 0,
parent_hash: Default::default(),
Expand Down Expand Up @@ -2665,7 +2654,7 @@ pub(crate) mod tests {

let hashof1 = {
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Number(0)).unwrap();
backend.begin_state_operation(&mut op, &hash).unwrap();
let mut header = Header {
number: 1,
parent_hash: hash,
Expand Down Expand Up @@ -2702,7 +2691,7 @@ pub(crate) mod tests {

let hashof2 = {
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Number(1)).unwrap();
backend.begin_state_operation(&mut op, &hashof1).unwrap();
let mut header = Header {
number: 2,
parent_hash: hashof1,
Expand Down Expand Up @@ -2736,7 +2725,7 @@ pub(crate) mod tests {

let hashof3 = {
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Number(2)).unwrap();
backend.begin_state_operation(&mut op, &hashof2).unwrap();
let mut header = Header {
number: 3,
parent_hash: hashof2,
Expand Down Expand Up @@ -3070,14 +3059,14 @@ pub(crate) mod tests {
let block4 = insert_header(&backend, 4, block3, None, Default::default());
{
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Hash(block0)).unwrap();
backend.begin_state_operation(&mut op, &block0).unwrap();
op.mark_finalized(&block1, None).unwrap();
op.mark_finalized(&block2, None).unwrap();
backend.commit_operation(op).unwrap();
}
{
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Hash(block2)).unwrap();
backend.begin_state_operation(&mut op, &block2).unwrap();
op.mark_finalized(&block3, None).unwrap();
op.mark_finalized(&block4, None).unwrap();
backend.commit_operation(op).unwrap();
Expand All @@ -3091,9 +3080,7 @@ pub(crate) mod tests {

let hash0 = {
let mut op = backend.begin_operation().unwrap();
backend
.begin_state_operation(&mut op, BlockId::Hash(Default::default()))
.unwrap();
backend.begin_state_operation(&mut op, &Default::default()).unwrap();
let mut header = Header {
number: 0,
parent_hash: Default::default(),
Expand Down Expand Up @@ -3131,7 +3118,7 @@ pub(crate) mod tests {

let hash1 = {
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Number(0)).unwrap();
backend.begin_state_operation(&mut op, &hash0).unwrap();
let mut header = Header {
number: 1,
parent_hash: hash0,
Expand Down Expand Up @@ -3180,7 +3167,7 @@ pub(crate) mod tests {
let block2 = insert_header(&backend, 2, block1, None, Default::default());
{
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Hash(block0)).unwrap();
backend.begin_state_operation(&mut op, &block0).unwrap();
op.mark_finalized(&block2, None).unwrap();
backend.commit_operation(op).unwrap_err();
}
Expand Down Expand Up @@ -3208,7 +3195,7 @@ pub(crate) mod tests {

{
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Hash(blocks[4])).unwrap();
backend.begin_state_operation(&mut op, &blocks[4]).unwrap();
for i in 1..5 {
op.mark_finalized(&blocks[i], None).unwrap();
}
Expand Down Expand Up @@ -3243,7 +3230,7 @@ pub(crate) mod tests {
}

let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Hash(blocks[4])).unwrap();
backend.begin_state_operation(&mut op, &blocks[4]).unwrap();
for i in 1..3 {
op.mark_finalized(&blocks[i], None).unwrap();
}
Expand Down Expand Up @@ -3300,7 +3287,7 @@ pub(crate) mod tests {
.unwrap();

let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Hash(blocks[4])).unwrap();
backend.begin_state_operation(&mut op, &blocks[4]).unwrap();
op.mark_head(&blocks[4]).unwrap();
backend.commit_operation(op).unwrap();

Expand All @@ -3309,7 +3296,7 @@ pub(crate) mod tests {

for i in 1..5 {
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Hash(blocks[i])).unwrap();
backend.begin_state_operation(&mut op, &blocks[i]).unwrap();
op.mark_finalized(&blocks[i], None).unwrap();
backend.commit_operation(op).unwrap();
}
Expand Down Expand Up @@ -3369,13 +3356,13 @@ pub(crate) mod tests {
)
.unwrap();
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Hash(blocks[4])).unwrap();
backend.begin_state_operation(&mut op, &blocks[4]).unwrap();
op.mark_head(&blocks[4]).unwrap();
backend.commit_operation(op).unwrap();

for i in 1..5 {
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Hash(blocks[4])).unwrap();
backend.begin_state_operation(&mut op, &blocks[4]).unwrap();
op.mark_finalized(&blocks[i], None).unwrap();
backend.commit_operation(op).unwrap();
}
Expand Down Expand Up @@ -3501,7 +3488,7 @@ pub(crate) mod tests {

for i in 1..10 {
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Hash(blocks[4])).unwrap();
backend.begin_state_operation(&mut op, &blocks[4]).unwrap();
op.mark_finalized(&blocks[i], None).unwrap();
backend.commit_operation(op).unwrap();
let bc = backend.blockchain();
Expand Down Expand Up @@ -3701,7 +3688,7 @@ pub(crate) mod tests {

let block3 = {
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Number(1)).unwrap();
backend.begin_state_operation(&mut op, &block1).unwrap();
let header = Header {
number: 3,
parent_hash: block2,
Expand All @@ -3720,7 +3707,7 @@ pub(crate) mod tests {

let block4 = {
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Hash(block2)).unwrap();
backend.begin_state_operation(&mut op, &block2).unwrap();
let header = Header {
number: 4,
parent_hash: block3,
Expand All @@ -3739,7 +3726,7 @@ pub(crate) mod tests {

let block3_fork = {
let mut op = backend.begin_operation().unwrap();
backend.begin_state_operation(&mut op, BlockId::Hash(block2)).unwrap();
backend.begin_state_operation(&mut op, &block2).unwrap();
let header = Header {
number: 3,
parent_hash: block2,
Expand Down
3 changes: 1 addition & 2 deletions client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,7 @@ where
Some(storage_changes) => {
let storage_changes = match storage_changes {
sc_consensus::StorageChanges::Changes(storage_changes) => {
self.backend
.begin_state_operation(&mut operation.op, BlockId::Hash(parent_hash))?;
self.backend.begin_state_operation(&mut operation.op, &parent_hash)?;
let (main_sc, child_sc, offchain_sc, tx, _, tx_index) =
storage_changes.into_inner();

Expand Down

0 comments on commit 1802a11

Please sign in to comment.