Skip to content

Commit

Permalink
mapping-sync: add support when ethereum pallet is introduced mid-way (p…
Browse files Browse the repository at this point in the history
…aritytech#341)

* mapping-sync: add support when ethereum pallet is introduced mid-way

* Bump mapping sync to 1.1.0-dev

* Fix an issue in error handling
  • Loading branch information
sorpaas authored Apr 16, 2021
1 parent 15759a8 commit b17ea4e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ impl<Block: BlockT> MappingDb<Block> {
}
}

pub fn write_none(
&self,
block_hash: Block::Hash
) -> Result<(), String> {
let _lock = self.write_lock.lock();

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

transaction.set(
crate::columns::SYNCED_MAPPING,
&block_hash.encode(),
&true.encode(),
);

self.db.commit(transaction).map_err(|e| format!("{:?}", e))?;

Ok(())
}

pub fn write_hashes(
&self,
commitment: MappingCommitment<Block>,
Expand Down
4 changes: 3 additions & 1 deletion client/mapping-sync/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Changelog for `fc-mapping-sync`

## Unreleased
## Unreleased

* Added support for syncing mapping hashes mid-way.
2 changes: 1 addition & 1 deletion client/mapping-sync/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fc-mapping-sync"
version = "1.0.0"
version = "1.1.0-dev"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"
description = "Mapping sync logic for Frontier."
Expand Down
56 changes: 37 additions & 19 deletions client/mapping-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,37 @@ mod worker;
pub use worker::MappingSyncWorker;

use sp_runtime::{generic::BlockId, traits::{Block as BlockT, Header as HeaderT, Zero}};
use sp_api::ProvideRuntimeApi;
use sp_api::{ApiExt, ProvideRuntimeApi};
use sc_client_api::BlockOf;
use sp_blockchain::HeaderBackend;
use fp_rpc::EthereumRuntimeRPCApi;
use fp_consensus::FindLogError;

pub fn sync_block<Block: BlockT>(
backend: &fc_db::Backend<Block>,
header: &Block::Header,
) -> Result<(), String> {
let log = fp_consensus::find_log(header.digest()).map_err(|e| format!("{:?}", e))?;
let post_hashes = log.into_hashes();

let mapping_commitment = fc_db::MappingCommitment {
block_hash: header.hash(),
ethereum_block_hash: post_hashes.block_hash,
ethereum_transaction_hashes: post_hashes.transaction_hashes,
};
backend.mapping().write_hashes(mapping_commitment)?;
match fp_consensus::find_log(header.digest()) {
Ok(log) => {
let post_hashes = log.into_hashes();

let mapping_commitment = fc_db::MappingCommitment {
block_hash: header.hash(),
ethereum_block_hash: post_hashes.block_hash,
ethereum_transaction_hashes: post_hashes.transaction_hashes,
};
backend.mapping().write_hashes(mapping_commitment)?;

Ok(())
},
Err(FindLogError::NotFound) => {
backend.mapping().write_none(header.hash())?;

Ok(())
},
Err(FindLogError::MultipleLogs) => Err("Multiple logs found".to_string()),
}

Ok(())
}

pub fn sync_genesis_block<Block: BlockT, C>(
Expand All @@ -53,15 +64,22 @@ pub fn sync_genesis_block<Block: BlockT, C>(
{
let id = BlockId::Hash(header.hash());

let block = client.runtime_api().current_block(&id)
let has_api = client.runtime_api().has_api::<dyn EthereumRuntimeRPCApi<Block>>(&id)
.map_err(|e| format!("{:?}", e))?;
let block_hash = block.ok_or("Ethereum genesis block not found".to_string())?.header.hash();
let mapping_commitment = fc_db::MappingCommitment::<Block> {
block_hash: header.hash(),
ethereum_block_hash: block_hash,
ethereum_transaction_hashes: Vec::new(),
};
backend.mapping().write_hashes(mapping_commitment)?;

if has_api {
let block = client.runtime_api().current_block(&id)
.map_err(|e| format!("{:?}", e))?;
let block_hash = block.ok_or("Ethereum genesis block not found".to_string())?.header.hash();
let mapping_commitment = fc_db::MappingCommitment::<Block> {
block_hash: header.hash(),
ethereum_block_hash: block_hash,
ethereum_transaction_hashes: Vec::new(),
};
backend.mapping().write_hashes(mapping_commitment)?;
} else {
backend.mapping().write_none(header.hash())?;
}

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion client/rpc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

## Unreleased

- `EthPubSubApi::new` takes an additional `overrides` parameter.
* `EthPubSubApi::new` takes an additional `overrides` parameter.

0 comments on commit b17ea4e

Please sign in to comment.