Skip to content

Commit

Permalink
Switch to one-to-one block mapping in db (paritytech#343)
Browse files Browse the repository at this point in the history
* Switch to one-to-one block mapping in db

* Remove old comment
  • Loading branch information
tgmichel authored Apr 7, 2021
1 parent 63c4a77 commit b6c59c4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
12 changes: 5 additions & 7 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ impl<Block: BlockT> MappingDb<Block> {
}
}

pub fn block_hashes(
pub fn block_hash(
&self,
ethereum_block_hash: &H256,
) -> Result<Vec<Block::Hash>, String> {
) -> Result<Option<Block::Hash>, String> {
match self.db.get(crate::columns::BLOCK_MAPPING, &ethereum_block_hash.encode()) {
Some(raw) => Ok(Vec::<Block::Hash>::decode(&mut &raw[..]).map_err(|e| format!("{:?}", e))?),
None => Ok(Vec::new()),
Some(raw) => Ok(Some(Block::Hash::decode(&mut &raw[..]).map_err(|e| format!("{:?}", e))?)),
None => Ok(None),
}
}

Expand All @@ -187,12 +187,10 @@ impl<Block: BlockT> MappingDb<Block> {

let mut transaction = sp_database::Transaction::new();

let mut block_hashes = self.block_hashes(&commitment.ethereum_block_hash)?;
block_hashes.push(commitment.block_hash);
transaction.set(
crate::columns::BLOCK_MAPPING,
&commitment.ethereum_block_hash.encode(),
&block_hashes.encode()
&commitment.block_hash.encode()
);

for (i, ethereum_transaction_hash) in commitment.ethereum_transaction_hashes.into_iter().enumerate() {
Expand Down
15 changes: 3 additions & 12 deletions client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,18 @@ pub mod frontier_backend_client {
})
}

// Asumes there is only one mapped canonical block in the AuxStore, otherwise something is wrong
pub fn load_hash<B: BlockT, C>(client: &C, backend: &fc_db::Backend<B>, hash: H256) -> RpcResult<Option<BlockId<B>>> where
B: BlockT,
C: HeaderBackend<B> + 'static,
B: BlockT<Hash=H256> + Send + Sync + 'static,
C: Send + Sync + 'static,
{
let hashes = backend.mapping().block_hashes(&hash)
let substrate_hash = backend.mapping().block_hash(&hash)
.map_err(|err| internal_err(format!("fetch aux store failed: {:?}", err)))?;
let out: Vec<H256> = hashes.into_iter()
.filter_map(|h| {
if is_canon::<B, C>(client, h) {
Some(h)
} else {
None
}
}).collect();

if out.len() == 1 {
if let Some(substrate_hash) = substrate_hash {
return Ok(Some(
BlockId::Hash(out[0])
BlockId::Hash(substrate_hash)
));
}
Ok(None)
Expand Down

0 comments on commit b6c59c4

Please sign in to comment.