Skip to content

Commit

Permalink
Frontier DB block mapping one-to-many (#862)
Browse files Browse the repository at this point in the history
* WIP Frontier DB block mapping one-to-many

* Fix

* fmt

* Cleanup

* Cleanup

* Cleanup

* fmt skip

* parity db migration

* Cleanup + tests

* WIP retroactively fix non-canon mapped blocks

* Update tests

* Fix more tests

* clippy

* taplo

* Test transaction metadata

* Use test runtime api

* Remove unnecessary generic
  • Loading branch information
tgmichel authored Oct 5, 2022
1 parent d2516b3 commit 5bd0dc7
Show file tree
Hide file tree
Showing 11 changed files with 896 additions and 110 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

126 changes: 74 additions & 52 deletions client/cli/src/frontier_db_cmd/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ mod tests {
type OpaqueBlock =
Block<Header<u64, BlakeTwo256>, substrate_test_runtime_client::runtime::Extrinsic>;

pub fn open_frontier_backend(
pub fn open_frontier_backend<C>(
client: Arc<C>,
path: PathBuf,
) -> Result<Arc<fc_db::Backend<OpaqueBlock>>, String> {
) -> Result<Arc<fc_db::Backend<OpaqueBlock>>, String>
where
C: sp_blockchain::HeaderBackend<OpaqueBlock>,
{
Ok(Arc::new(fc_db::Backend::<OpaqueBlock>::new(
client,
&fc_db::DatabaseSettings {
source: sc_client_db::DatabaseSource::RocksDb {
path,
Expand Down Expand Up @@ -126,11 +131,13 @@ mod tests {
// Write some data in a temp file.
let test_value_path = test_json_file(&tmp, &schema_test_value());

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
// Test client.
let (client, _) =
TestClientBuilder::new().build_with_native_executor::<RuntimeApi, _>(None);
let client = Arc::new(client);
// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");

assert_eq!(backend.meta().ethereum_schema(), Ok(None));

Expand All @@ -141,7 +148,7 @@ mod tests {
Operation::Create,
Column::Meta
)
.run(Arc::new(client), backend.clone())
.run(client, backend.clone())
.is_ok());

assert_eq!(
Expand All @@ -156,11 +163,13 @@ mod tests {
// Write some data in a temp file.
let test_value_path = test_json_file(&tmp, &schema_test_value());

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
// Test client.
let (client, _) =
TestClientBuilder::new().build_with_native_executor::<RuntimeApi, _>(None);
let client = Arc::new(client);
// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");

let data_before = vec![(EthereumStorageSchema::V2, H256::default())];

Expand All @@ -176,7 +185,7 @@ mod tests {
Operation::Create,
Column::Meta
)
.run(Arc::new(client), backend.clone())
.run(client, backend.clone())
.is_err());

let data_after = backend.meta().ethereum_schema().unwrap().unwrap();
Expand All @@ -186,12 +195,13 @@ mod tests {
#[test]
fn schema_read_works() {
let tmp = tempdir().expect("create a temporary directory");

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
// Test client.
let (client, _) =
TestClientBuilder::new().build_with_native_executor::<RuntimeApi, _>(None);
let client = Arc::new(client);
// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");

assert_eq!(backend.meta().ethereum_schema(), Ok(None));

Expand All @@ -209,7 +219,7 @@ mod tests {
Operation::Read,
Column::Meta
)
.run(Arc::new(client), backend.clone())
.run(client, backend.clone())
.is_ok());
}

Expand All @@ -218,12 +228,13 @@ mod tests {
let tmp = tempdir().expect("create a temporary directory");
// Write some data in a temp file.
let test_value_path = test_json_file(&tmp, &schema_test_value());

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
// Test client.
let (client, _) =
TestClientBuilder::new().build_with_native_executor::<RuntimeApi, _>(None);
let client = Arc::new(client);
// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");

assert_eq!(backend.meta().ethereum_schema(), Ok(None));
// Run the command
Expand All @@ -233,7 +244,7 @@ mod tests {
Operation::Update,
Column::Meta
)
.run(Arc::new(client), backend.clone())
.run(client, backend.clone())
.is_ok());

assert_eq!(
Expand All @@ -245,12 +256,13 @@ mod tests {
#[test]
fn schema_delete_works() {
let tmp = tempdir().expect("create a temporary directory");

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
// Test client.
let (client, _) =
TestClientBuilder::new().build_with_native_executor::<RuntimeApi, _>(None);
let client = Arc::new(client);
// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");

let data = vec![(EthereumStorageSchema::V2, H256::default())];

Expand All @@ -265,7 +277,7 @@ mod tests {
Operation::Delete,
Column::Meta
)
.run(Arc::new(client), backend.clone())
.run(client, backend.clone())
.is_ok());

assert_eq!(backend.meta().ethereum_schema(), Ok(Some(vec![])));
Expand All @@ -276,12 +288,13 @@ mod tests {
let tmp = tempdir().expect("create a temporary directory");
// Write some data in a temp file.
let test_value_path = test_json_file(&tmp, &tips_test_value());

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
// Test client.
let (client, _) =
TestClientBuilder::new().build_with_native_executor::<RuntimeApi, _>(None);
let client = Arc::new(client);
// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");

assert_eq!(backend.meta().current_syncing_tips(), Ok(vec![]));
// Run the command
Expand All @@ -291,7 +304,7 @@ mod tests {
Operation::Create,
Column::Meta
)
.run(Arc::new(client), backend.clone())
.run(client, backend.clone())
.is_ok());

assert_eq!(
Expand All @@ -305,12 +318,13 @@ mod tests {
let tmp = tempdir().expect("create a temporary directory");
// Write some data in a temp file.
let test_value_path = test_json_file(&tmp, &tips_test_value());

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
// Test client.
let (client, _) =
TestClientBuilder::new().build_with_native_executor::<RuntimeApi, _>(None);
let client = Arc::new(client);
// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");

let data_before = vec![H256::default()];

Expand All @@ -325,7 +339,7 @@ mod tests {
Operation::Create,
Column::Meta
)
.run(Arc::new(client), backend.clone())
.run(client, backend.clone())
.is_err());

let data_after = backend.meta().current_syncing_tips().unwrap();
Expand All @@ -335,12 +349,13 @@ mod tests {
#[test]
fn tips_read_works() {
let tmp = tempdir().expect("create a temporary directory");

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
// Test client.
let (client, _) =
TestClientBuilder::new().build_with_native_executor::<RuntimeApi, _>(None);
let client = Arc::new(client);
// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");

assert_eq!(backend.meta().current_syncing_tips(), Ok(vec![]));

Expand All @@ -357,7 +372,7 @@ mod tests {
Operation::Read,
Column::Meta
)
.run(Arc::new(client), backend.clone())
.run(client, backend.clone())
.is_ok());
}

Expand All @@ -366,12 +381,13 @@ mod tests {
let tmp = tempdir().expect("create a temporary directory");
// Write some data in a temp file.
let test_value_path = test_json_file(&tmp, &tips_test_value());

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
// Test client.
let (client, _) =
TestClientBuilder::new().build_with_native_executor::<RuntimeApi, _>(None);
let client = Arc::new(client);
// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");

assert_eq!(backend.meta().current_syncing_tips(), Ok(vec![]));
// Run the command
Expand All @@ -381,7 +397,7 @@ mod tests {
Operation::Update,
Column::Meta
)
.run(Arc::new(client), backend.clone())
.run(client, backend.clone())
.is_ok());

assert_eq!(
Expand All @@ -393,12 +409,13 @@ mod tests {
#[test]
fn tips_delete_works() {
let tmp = tempdir().expect("create a temporary directory");

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
// Test client.
let (client, _) =
TestClientBuilder::new().build_with_native_executor::<RuntimeApi, _>(None);
let client = Arc::new(client);
// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");

let data = vec![H256::default()];

Expand All @@ -413,7 +430,7 @@ mod tests {
Operation::Delete,
Column::Meta
)
.run(Arc::new(client), backend.clone())
.run(client, backend.clone())
.is_ok());

assert_eq!(backend.meta().current_syncing_tips(), Ok(vec![]));
Expand All @@ -424,13 +441,14 @@ mod tests {
let tmp = tempdir().expect("create a temporary directory");
// Write some data in a temp file.
let test_value_path = test_json_file(&tmp, &schema_test_value());

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
// Test client.
let (client, _) =
TestClientBuilder::new().build_with_native_executor::<RuntimeApi, _>(None);
let client = Arc::new(client);
// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");
let client = client;

let data = vec![(EthereumStorageSchema::V1, H256::default())];

Expand Down Expand Up @@ -498,13 +516,14 @@ mod tests {
serde_json::to_string("im_not_allowed_here").unwrap(),
)
.expect("write test value json file");

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
// Test client.
let (client, _) =
TestClientBuilder::new().build_with_native_executor::<RuntimeApi, _>(None);
let client = Arc::new(client);
// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");
let client = client;

// Run the Create command
assert!(cmd(
Expand Down Expand Up @@ -559,7 +578,8 @@ mod tests {
let test_value_path = test_json_file(&tmp, &TestValue::Commitment(block_hash));

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");

// Run the command using some ethereum block hash as key.
let ethereum_block_hash = H256::default();
Expand All @@ -575,7 +595,7 @@ mod tests {
// Expect the ethereum and substrate block hashes to be mapped.
assert_eq!(
backend.mapping().block_hash(&ethereum_block_hash),
Ok(Some(block_hash))
Ok(Some(vec![block_hash]))
);

// Expect the offchain-stored transaction metadata to match the one we stored in the runtime.
Expand Down Expand Up @@ -643,7 +663,8 @@ mod tests {
let test_value_path = test_json_file(&tmp, &TestValue::Commitment(block_a1_hash));

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");

// Run the command using some ethereum block hash as key.
let ethereum_block_hash = H256::default();
Expand All @@ -659,7 +680,7 @@ mod tests {
// Expect the ethereum and substrate block hashes to be mapped.
assert_eq!(
backend.mapping().block_hash(&ethereum_block_hash),
Ok(Some(block_a1_hash))
Ok(Some(vec![block_a1_hash]))
);

// Expect the offchain-stored transaction metadata to match the one we stored in the runtime.
Expand Down Expand Up @@ -709,7 +730,7 @@ mod tests {
// Expect the ethereum and substrate block hashes to be mapped.
assert_eq!(
backend.mapping().block_hash(&ethereum_block_hash),
Ok(Some(block_a2_hash))
Ok(Some(vec![block_a1_hash, block_a2_hash]))
);

// Expect the offchain-stored transaction metadata to have data for both blocks.
Expand Down Expand Up @@ -764,7 +785,8 @@ mod tests {
let test_value_path = test_json_file(&tmp, &TestValue::Commitment(block_hash));

// Create a temporary frontier secondary DB.
let backend = open_frontier_backend(tmp.into_path()).expect("a temporary db was created");
let backend = open_frontier_backend(client.clone(), tmp.into_path())
.expect("a temporary db was created");

// Create command using some ethereum block hash as key.
let ethereum_block_hash = H256::default();
Expand Down
Loading

0 comments on commit 5bd0dc7

Please sign in to comment.