Newer
Older
let a2 = client.new_block(Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a2.clone()).unwrap();
let v: Vec<H256> = Vec::new();
assert_eq!(v, client.uncles(a2.hash(), 3).unwrap());
}
#[test]
fn uncles_with_multiple_forks() {
// block tree:
// G -> A1 -> A2 -> A3 -> A4 -> A5
// A1 -> B2 -> B3 -> B4
// B2 -> C3
// A1 -> D2
let client = test_client::new();
// G -> A1
let a1 = client.new_block(Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a1.clone()).unwrap();
// A1 -> A2
let a2 = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a2.clone()).unwrap();
// A2 -> A3
let a3 = client.new_block_at(&BlockId::Hash(a2.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a3.clone()).unwrap();
// A3 -> A4
let a4 = client.new_block_at(&BlockId::Hash(a3.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a4.clone()).unwrap();
// A4 -> A5
let a5 = client.new_block_at(&BlockId::Hash(a4.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a5.clone()).unwrap();
// A1 -> B2
let mut builder = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap();
// this push is required as otherwise B2 has the same hash as A2 and won't get imported
builder.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 41,
nonce: 0,
}).unwrap();
let b2 = builder.bake().unwrap();
client.import(BlockOrigin::Own, b2.clone()).unwrap();
// B2 -> B3
let b3 = client.new_block_at(&BlockId::Hash(b2.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, b3.clone()).unwrap();
// B3 -> B4
let b4 = client.new_block_at(&BlockId::Hash(b3.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, b4.clone()).unwrap();
// // B2 -> C3
let mut builder = client.new_block_at(&BlockId::Hash(b2.hash()), Default::default()).unwrap();
// this push is required as otherwise C3 has the same hash as B3 and won't get imported
builder.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 1,
nonce: 1,
}).unwrap();
let c3 = builder.bake().unwrap();
client.import(BlockOrigin::Own, c3.clone()).unwrap();
// A1 -> D2
let mut builder = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap();
// this push is required as otherwise D2 has the same hash as B2 and won't get imported
builder.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 1,
nonce: 0,
}).unwrap();
let d2 = builder.bake().unwrap();
client.import(BlockOrigin::Own, d2.clone()).unwrap();
let genesis_hash = client.info().chain.genesis_hash;
let uncles1 = client.uncles(a4.hash(), 10).unwrap();
assert_eq!(vec![b2.hash(), d2.hash()], uncles1);
let uncles2 = client.uncles(a4.hash(), 0).unwrap();
assert_eq!(0, uncles2.len());
let uncles3 = client.uncles(a1.hash(), 10).unwrap();
assert_eq!(0, uncles3.len());
let uncles4 = client.uncles(genesis_hash, 10).unwrap();
assert_eq!(0, uncles4.len());
let uncles5 = client.uncles(d2.hash(), 10).unwrap();
assert_eq!(vec![a2.hash(), b2.hash()], uncles5);
let uncles6 = client.uncles(b3.hash(), 1).unwrap();
assert_eq!(vec![c3.hash()], uncles6);
}
fn best_containing_on_longest_chain_with_single_chain_3_blocks() {
// block tree:
// G -> A1 -> A2
let (client, longest_chain_select) = TestClientBuilder::new().build_with_longest_chain();
let a1 = client.new_block(Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a1.clone()).unwrap();
let a2 = client.new_block(Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a2.clone()).unwrap();
let genesis_hash = client.info().chain.genesis_hash;
assert_eq!(a2.hash(), longest_chain_select.finality_target(genesis_hash, None).unwrap().unwrap());
assert_eq!(a2.hash(), longest_chain_select.finality_target(a1.hash(), None).unwrap().unwrap());
assert_eq!(a2.hash(), longest_chain_select.finality_target(a2.hash(), None).unwrap().unwrap());
fn best_containing_on_longest_chain_with_multiple_forks() {
// block tree:
// G -> A1 -> A2 -> A3 -> A4 -> A5
// A1 -> B2 -> B3 -> B4
// B2 -> C3
// A1 -> D2
let (client, longest_chain_select) = TestClientBuilder::new().build_with_longest_chain();
let a1 = client.new_block(Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a1.clone()).unwrap();
let a2 = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a2.clone()).unwrap();
let a3 = client.new_block_at(&BlockId::Hash(a2.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a3.clone()).unwrap();
let a4 = client.new_block_at(&BlockId::Hash(a3.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a4.clone()).unwrap();
let a5 = client.new_block_at(&BlockId::Hash(a4.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a5.clone()).unwrap();
let mut builder = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap();
// this push is required as otherwise B2 has the same hash as A2 and won't get imported
builder.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 41,
nonce: 0,
}).unwrap();
let b2 = builder.bake().unwrap();
client.import(BlockOrigin::Own, b2.clone()).unwrap();
let b3 = client.new_block_at(&BlockId::Hash(b2.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, b3.clone()).unwrap();
let b4 = client.new_block_at(&BlockId::Hash(b3.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, b4.clone()).unwrap();
let mut builder = client.new_block_at(&BlockId::Hash(b2.hash()), Default::default()).unwrap();
// this push is required as otherwise C3 has the same hash as B3 and won't get imported
builder.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 1,
nonce: 1,
}).unwrap();
let c3 = builder.bake().unwrap();
client.import(BlockOrigin::Own, c3.clone()).unwrap();
let mut builder = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap();
// this push is required as otherwise D2 has the same hash as B2 and won't get imported
builder.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 1,
nonce: 0,
}).unwrap();
let d2 = builder.bake().unwrap();
client.import(BlockOrigin::Own, d2.clone()).unwrap();
assert_eq!(client.info().chain.best_hash, a5.hash());
let genesis_hash = client.info().chain.genesis_hash;
let leaves = longest_chain_select.leaves().unwrap();
assert!(leaves.contains(&a5.hash()));
assert!(leaves.contains(&b4.hash()));
assert!(leaves.contains(&c3.hash()));
assert!(leaves.contains(&d2.hash()));
assert_eq!(leaves.len(), 4);
// search without restriction
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
assert_eq!(a5.hash(), longest_chain_select.finality_target(
genesis_hash, None).unwrap().unwrap());
assert_eq!(a5.hash(), longest_chain_select.finality_target(
a1.hash(), None).unwrap().unwrap());
assert_eq!(a5.hash(), longest_chain_select.finality_target(
a2.hash(), None).unwrap().unwrap());
assert_eq!(a5.hash(), longest_chain_select.finality_target(
a3.hash(), None).unwrap().unwrap());
assert_eq!(a5.hash(), longest_chain_select.finality_target(
a4.hash(), None).unwrap().unwrap());
assert_eq!(a5.hash(), longest_chain_select.finality_target(
a5.hash(), None).unwrap().unwrap());
assert_eq!(b4.hash(), longest_chain_select.finality_target(
b2.hash(), None).unwrap().unwrap());
assert_eq!(b4.hash(), longest_chain_select.finality_target(
b3.hash(), None).unwrap().unwrap());
assert_eq!(b4.hash(), longest_chain_select.finality_target(
b4.hash(), None).unwrap().unwrap());
assert_eq!(c3.hash(), longest_chain_select.finality_target(
c3.hash(), None).unwrap().unwrap());
assert_eq!(d2.hash(), longest_chain_select.finality_target(
d2.hash(), None).unwrap().unwrap());
// search only blocks with number <= 5. equivalent to without restriction for this scenario
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
assert_eq!(a5.hash(), longest_chain_select.finality_target(
genesis_hash, Some(5)).unwrap().unwrap());
assert_eq!(a5.hash(), longest_chain_select.finality_target(
a1.hash(), Some(5)).unwrap().unwrap());
assert_eq!(a5.hash(), longest_chain_select.finality_target(
a2.hash(), Some(5)).unwrap().unwrap());
assert_eq!(a5.hash(), longest_chain_select.finality_target(
a3.hash(), Some(5)).unwrap().unwrap());
assert_eq!(a5.hash(), longest_chain_select.finality_target(
a4.hash(), Some(5)).unwrap().unwrap());
assert_eq!(a5.hash(), longest_chain_select.finality_target(
a5.hash(), Some(5)).unwrap().unwrap());
assert_eq!(b4.hash(), longest_chain_select.finality_target(
b2.hash(), Some(5)).unwrap().unwrap());
assert_eq!(b4.hash(), longest_chain_select.finality_target(
b3.hash(), Some(5)).unwrap().unwrap());
assert_eq!(b4.hash(), longest_chain_select.finality_target(
b4.hash(), Some(5)).unwrap().unwrap());
assert_eq!(c3.hash(), longest_chain_select.finality_target(
c3.hash(), Some(5)).unwrap().unwrap());
assert_eq!(d2.hash(), longest_chain_select.finality_target(
d2.hash(), Some(5)).unwrap().unwrap());
// search only blocks with number <= 4
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
assert_eq!(a4.hash(), longest_chain_select.finality_target(
genesis_hash, Some(4)).unwrap().unwrap());
assert_eq!(a4.hash(), longest_chain_select.finality_target(
a1.hash(), Some(4)).unwrap().unwrap());
assert_eq!(a4.hash(), longest_chain_select.finality_target(
a2.hash(), Some(4)).unwrap().unwrap());
assert_eq!(a4.hash(), longest_chain_select.finality_target(
a3.hash(), Some(4)).unwrap().unwrap());
assert_eq!(a4.hash(), longest_chain_select.finality_target(
a4.hash(), Some(4)).unwrap().unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a5.hash(), Some(4)).unwrap());
assert_eq!(b4.hash(), longest_chain_select.finality_target(
b2.hash(), Some(4)).unwrap().unwrap());
assert_eq!(b4.hash(), longest_chain_select.finality_target(
b3.hash(), Some(4)).unwrap().unwrap());
assert_eq!(b4.hash(), longest_chain_select.finality_target(
b4.hash(), Some(4)).unwrap().unwrap());
assert_eq!(c3.hash(), longest_chain_select.finality_target(
c3.hash(), Some(4)).unwrap().unwrap());
assert_eq!(d2.hash(), longest_chain_select.finality_target(
d2.hash(), Some(4)).unwrap().unwrap());
// search only blocks with number <= 3
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
assert_eq!(a3.hash(), longest_chain_select.finality_target(
genesis_hash, Some(3)).unwrap().unwrap());
assert_eq!(a3.hash(), longest_chain_select.finality_target(
a1.hash(), Some(3)).unwrap().unwrap());
assert_eq!(a3.hash(), longest_chain_select.finality_target(
a2.hash(), Some(3)).unwrap().unwrap());
assert_eq!(a3.hash(), longest_chain_select.finality_target(
a3.hash(), Some(3)).unwrap().unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a4.hash(), Some(3)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a5.hash(), Some(3)).unwrap());
assert_eq!(b3.hash(), longest_chain_select.finality_target(
b2.hash(), Some(3)).unwrap().unwrap());
assert_eq!(b3.hash(), longest_chain_select.finality_target(
b3.hash(), Some(3)).unwrap().unwrap());
assert_eq!(None, longest_chain_select.finality_target(
b4.hash(), Some(3)).unwrap());
assert_eq!(c3.hash(), longest_chain_select.finality_target(
c3.hash(), Some(3)).unwrap().unwrap());
assert_eq!(d2.hash(), longest_chain_select.finality_target(
d2.hash(), Some(3)).unwrap().unwrap());
// search only blocks with number <= 2
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
assert_eq!(a2.hash(), longest_chain_select.finality_target(
genesis_hash, Some(2)).unwrap().unwrap());
assert_eq!(a2.hash(), longest_chain_select.finality_target(
a1.hash(), Some(2)).unwrap().unwrap());
assert_eq!(a2.hash(), longest_chain_select.finality_target(
a2.hash(), Some(2)).unwrap().unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a3.hash(), Some(2)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a4.hash(), Some(2)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a5.hash(), Some(2)).unwrap());
assert_eq!(b2.hash(), longest_chain_select.finality_target(
b2.hash(), Some(2)).unwrap().unwrap());
assert_eq!(None, longest_chain_select.finality_target(
b3.hash(), Some(2)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
b4.hash(), Some(2)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
c3.hash(), Some(2)).unwrap());
assert_eq!(d2.hash(), longest_chain_select.finality_target(
d2.hash(), Some(2)).unwrap().unwrap());
// search only blocks with number <= 1
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
assert_eq!(a1.hash(), longest_chain_select.finality_target(
genesis_hash, Some(1)).unwrap().unwrap());
assert_eq!(a1.hash(), longest_chain_select.finality_target(
a1.hash(), Some(1)).unwrap().unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a2.hash(), Some(1)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a3.hash(), Some(1)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a4.hash(), Some(1)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a5.hash(), Some(1)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
b2.hash(), Some(1)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
b3.hash(), Some(1)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
b4.hash(), Some(1)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
c3.hash(), Some(1)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
d2.hash(), Some(1)).unwrap());
// search only blocks with number <= 0
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
assert_eq!(genesis_hash, longest_chain_select.finality_target(
genesis_hash, Some(0)).unwrap().unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a1.hash(), Some(0)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a2.hash(), Some(0)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a3.hash(), Some(0)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a4.hash(), Some(0)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
a5.hash(), Some(0)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
b2.hash(), Some(0)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
b3.hash(), Some(0)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
b4.hash(), Some(0)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
c3.hash().clone(), Some(0)).unwrap());
assert_eq!(None, longest_chain_select.finality_target(
d2.hash().clone(), Some(0)).unwrap());
fn best_containing_on_longest_chain_with_max_depth_higher_than_best() {
// block tree:
// G -> A1 -> A2
let (client, longest_chain_select) = TestClientBuilder::new().build_with_longest_chain();
let a1 = client.new_block(Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a1.clone()).unwrap();
let a2 = client.new_block(Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a2.clone()).unwrap();
let genesis_hash = client.info().chain.genesis_hash;
assert_eq!(a2.hash(), longest_chain_select.finality_target(genesis_hash, Some(10)).unwrap().unwrap());
#[test]
fn key_changes_works() {
let (client, _, test_cases) = prepare_client_with_key_changes();
for (index, (begin, end, key, expected_result)) in test_cases.into_iter().enumerate() {
let end = client.block_hash(end).unwrap().unwrap();
let actual_result = client.key_changes(
begin,
BlockId::Hash(end),
None,
&StorageKey(key),
).unwrap();
match actual_result == expected_result {
true => (),
false => panic!(format!("Failed test {}: actual = {:?}, expected = {:?}",
index, actual_result, expected_result)),
}
}
}
#[test]
fn import_with_justification() {
let client = test_client::new();
// G -> A1
let a1 = client.new_block(Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a1.clone()).unwrap();
// A1 -> A2
let a2 = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a2.clone()).unwrap();
// A2 -> A3
let justification = vec![1, 2, 3];
let a3 = client.new_block_at(&BlockId::Hash(a2.hash()), Default::default()).unwrap().bake().unwrap();
client.import_justified(BlockOrigin::Own, a3.clone(), justification.clone()).unwrap();
assert_eq!(
client.info().chain.finalized_hash,
a3.hash(),
);
assert_eq!(
client.justification(&BlockId::Hash(a3.hash())).unwrap(),
Some(justification),
);
assert_eq!(
client.justification(&BlockId::Hash(a1.hash())).unwrap(),
None,
);
assert_eq!(
client.justification(&BlockId::Hash(a2.hash())).unwrap(),
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
#[test]
fn importing_diverged_finalized_block_should_trigger_reorg() {
let client = test_client::new();
// G -> A1 -> A2
// \
// -> B1
let a1 = client.new_block_at(&BlockId::Number(0), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a1.clone()).unwrap();
let a2 = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a2.clone()).unwrap();
let mut b1 = client.new_block_at(&BlockId::Number(0), Default::default()).unwrap();
// needed to make sure B1 gets a different hash from A1
b1.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 1,
nonce: 0,
}).unwrap();
// create but don't import B1 just yet
let b1 = b1.bake().unwrap();
// A2 is the current best since it's the longest chain
assert_eq!(
a2.hash(),
);
// importing B1 as finalized should trigger a re-org and set it as new best
let justification = vec![1, 2, 3];
client.import_justified(BlockOrigin::Own, b1.clone(), justification).unwrap();
assert_eq!(
b1.hash(),
);
assert_eq!(
client.info().chain.finalized_hash,
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
b1.hash(),
);
}
#[test]
fn finalizing_diverged_block_should_trigger_reorg() {
let (client, select_chain) = TestClientBuilder::new().build_with_longest_chain();
// G -> A1 -> A2
// \
// -> B1 -> B2
let a1 = client.new_block_at(&BlockId::Number(0), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a1.clone()).unwrap();
let a2 = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a2.clone()).unwrap();
let mut b1 = client.new_block_at(&BlockId::Number(0), Default::default()).unwrap();
// needed to make sure B1 gets a different hash from A1
b1.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 1,
nonce: 0,
}).unwrap();
let b1 = b1.bake().unwrap();
client.import(BlockOrigin::Own, b1.clone()).unwrap();
let b2 = client.new_block_at(&BlockId::Hash(b1.hash()), Default::default()).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, b2.clone()).unwrap();
// A2 is the current best since it's the longest chain
assert_eq!(
a2.hash(),
);
// we finalize block B1 which is on a different branch from current best
// which should trigger a re-org.
ClientExt::finalize_block(&client, BlockId::Hash(b1.hash()), None).unwrap();
// B1 should now be the latest finalized
assert_eq!(
client.info().chain.finalized_hash,
b1.hash(),
);
// and B1 should be the new best block (`finalize_block` as no way of
// knowing about B2)
assert_eq!(
b1.hash(),
);
// `SelectChain` should report B2 as best block though
assert_eq!(
select_chain.best_chain().unwrap().hash(),
b2.hash(),
);
// after we build B3 on top of B2 and import it
// it should be the new best block,
let b3 = client.new_block_at(
&BlockId::Hash(b2.hash()),
Default::default(),
).unwrap().bake().unwrap();
client.import(BlockOrigin::Own, b3.clone()).unwrap();
assert_eq!(
b3.hash(),
);
}
#[test]
fn get_header_by_block_number_doesnt_panic() {
let client = test_client::new();
// backend uses u32 for block numbers, make sure we don't panic when
// trying to convert
let id = BlockId::<Block>::Number(72340207214430721);
client.header(&id).expect_err("invalid block number overflows u32");
}
#[test]
fn state_reverted_on_reorg() {
let _ = env_logger::try_init();
let client = test_client::new();
let current_balance = ||
client.runtime_api().balance_of(
&BlockId::number(client.info().chain.best_number), AccountKeyring::Alice.into()
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
).unwrap();
// G -> A1 -> A2
// \
// -> B1
let mut a1 = client.new_block_at(&BlockId::Number(0), Default::default()).unwrap();
a1.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Bob.into(),
amount: 10,
nonce: 0,
}).unwrap();
let a1 = a1.bake().unwrap();
client.import(BlockOrigin::Own, a1.clone()).unwrap();
let mut b1 = client.new_block_at(&BlockId::Number(0), Default::default()).unwrap();
b1.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 50,
nonce: 0,
}).unwrap();
let b1 = b1.bake().unwrap();
// Reorg to B1
client.import_as_best(BlockOrigin::Own, b1.clone()).unwrap();
assert_eq!(950, current_balance());
let mut a2 = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap();
a2.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Charlie.into(),
amount: 10,
nonce: 1,
}).unwrap();
// Re-org to A2
client.import_as_best(BlockOrigin::Own, a2.bake().unwrap()).unwrap();
assert_eq!(980, current_balance());
}
#[test]
fn doesnt_import_blocks_that_revert_finality() {
let _ = env_logger::try_init();
let tmp = tempfile::tempdir().unwrap();
// we need to run with archive pruning to avoid pruning non-canonical
// states
let backend = Arc::new(Backend::new(
DatabaseSettings {
state_cache_size: 1 << 20,
state_cache_child_ratio: None,
pruning: PruningMode::ArchiveAll,
source: DatabaseSettingsSrc::Path {
path: tmp.path().into(),
cache_size: None,
}
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
},
u64::max_value(),
).unwrap());
let client = TestClientBuilder::with_backend(backend).build();
// -> C1
// /
// G -> A1 -> A2
// \
// -> B1 -> B2 -> B3
let a1 = client.new_block_at(&BlockId::Number(0), Default::default())
.unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a1.clone()).unwrap();
let a2 = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default())
.unwrap().bake().unwrap();
client.import(BlockOrigin::Own, a2.clone()).unwrap();
let mut b1 = client.new_block_at(&BlockId::Number(0), Default::default()).unwrap();
// needed to make sure B1 gets a different hash from A1
b1.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 1,
nonce: 0,
}).unwrap();
let b1 = b1.bake().unwrap();
client.import(BlockOrigin::Own, b1.clone()).unwrap();
let b2 = client.new_block_at(&BlockId::Hash(b1.hash()), Default::default())
.unwrap().bake().unwrap();
client.import(BlockOrigin::Own, b2.clone()).unwrap();
// we will finalize A2 which should make it impossible to import a new
// B3 at the same height but that doesnt't include it
ClientExt::finalize_block(&client, BlockId::Hash(a2.hash()), None).unwrap();
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
let b3 = client.new_block_at(&BlockId::Hash(b2.hash()), Default::default())
.unwrap().bake().unwrap();
let import_err = client.import(BlockOrigin::Own, b3).err().unwrap();
let expected_err = ConsensusError::ClientImport(
error::Error::NotInFinalizedChain.to_string()
);
assert_eq!(
import_err.to_string(),
expected_err.to_string(),
);
// adding a C1 block which is lower than the last finalized should also
// fail (with a cheaper check that doesn't require checking ancestry).
let mut c1 = client.new_block_at(&BlockId::Number(0), Default::default()).unwrap();
// needed to make sure C1 gets a different hash from A1 and B1
c1.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 2,
nonce: 0,
}).unwrap();
let c1 = c1.bake().unwrap();
let import_err = client.import(BlockOrigin::Own, c1).err().unwrap();
let expected_err = ConsensusError::ClientImport(
error::Error::NotInFinalizedChain.to_string()
);
assert_eq!(
import_err.to_string(),
expected_err.to_string(),
);
}
Arkadiy Paronyan
committed
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
#[test]
fn returns_status_for_pruned_blocks() {
let _ = env_logger::try_init();
let tmp = tempfile::tempdir().unwrap();
// set to prune after 1 block
// states
let backend = Arc::new(Backend::new(
DatabaseSettings {
state_cache_size: 1 << 20,
state_cache_child_ratio: None,
pruning: PruningMode::keep_blocks(1),
source: DatabaseSettingsSrc::Path {
path: tmp.path().into(),
cache_size: None,
}
},
u64::max_value(),
).unwrap());
let mut client = TestClientBuilder::with_backend(backend).build();
let a1 = client.new_block_at(&BlockId::Number(0), Default::default())
.unwrap().bake().unwrap();
let mut b1 = client.new_block_at(&BlockId::Number(0), Default::default()).unwrap();
// b1 is created, but not imported
b1.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 1,
nonce: 0,
}).unwrap();
let b1 = b1.bake().unwrap();
let check_block_a1 = BlockCheckParams {
hash: a1.hash().clone(),
number: 0,
parent_hash: a1.header().parent_hash().clone(),
allow_missing_state: false
};
assert_eq!(client.check_block(check_block_a1.clone()).unwrap(), ImportResult::imported(false));
assert_eq!(client.block_status(&BlockId::hash(check_block_a1.hash)).unwrap(), BlockStatus::Unknown);
client.import_as_final(BlockOrigin::Own, a1.clone()).unwrap();
assert_eq!(client.check_block(check_block_a1.clone()).unwrap(), ImportResult::AlreadyInChain);
assert_eq!(client.block_status(&BlockId::hash(check_block_a1.hash)).unwrap(), BlockStatus::InChainWithState);
let a2 = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default())
.unwrap().bake().unwrap();
client.import_as_final(BlockOrigin::Own, a2.clone()).unwrap();
let check_block_a2 = BlockCheckParams {
hash: a2.hash().clone(),
number: 1,
parent_hash: a1.header().parent_hash().clone(),
allow_missing_state: false
};
assert_eq!(client.check_block(check_block_a1.clone()).unwrap(), ImportResult::AlreadyInChain);
assert_eq!(client.block_status(&BlockId::hash(check_block_a1.hash)).unwrap(), BlockStatus::InChainPruned);
assert_eq!(client.check_block(check_block_a2.clone()).unwrap(), ImportResult::AlreadyInChain);
assert_eq!(client.block_status(&BlockId::hash(check_block_a2.hash)).unwrap(), BlockStatus::InChainWithState);
let a3 = client.new_block_at(&BlockId::Hash(a2.hash()), Default::default())
.unwrap().bake().unwrap();
client.import_as_final(BlockOrigin::Own, a3.clone()).unwrap();
let check_block_a3 = BlockCheckParams {
hash: a3.hash().clone(),
number: 2,
parent_hash: a2.header().parent_hash().clone(),
allow_missing_state: false
};
// a1 and a2 are both pruned at this point
assert_eq!(client.check_block(check_block_a1.clone()).unwrap(), ImportResult::AlreadyInChain);
assert_eq!(client.block_status(&BlockId::hash(check_block_a1.hash)).unwrap(), BlockStatus::InChainPruned);
assert_eq!(client.check_block(check_block_a2.clone()).unwrap(), ImportResult::AlreadyInChain);
assert_eq!(client.block_status(&BlockId::hash(check_block_a2.hash)).unwrap(), BlockStatus::InChainPruned);
assert_eq!(client.check_block(check_block_a3.clone()).unwrap(), ImportResult::AlreadyInChain);
assert_eq!(client.block_status(&BlockId::hash(check_block_a3.hash)).unwrap(), BlockStatus::InChainWithState);
let mut check_block_b1 = BlockCheckParams {
hash: b1.hash().clone(),
number: 0,
parent_hash: b1.header().parent_hash().clone(),
allow_missing_state: false
};
assert_eq!(client.check_block(check_block_b1.clone()).unwrap(), ImportResult::MissingState);
check_block_b1.allow_missing_state = true;
assert_eq!(client.check_block(check_block_b1.clone()).unwrap(), ImportResult::imported(false));
check_block_b1.parent_hash = H256::random();
assert_eq!(client.check_block(check_block_b1.clone()).unwrap(), ImportResult::UnknownParent);
}