Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
);
});
}
#[test]
fn check_queued_too_many_upward_messages_fails() {
let parachains = vec![
(0u32.into(), vec![], vec![]),
];
with_externalities(&mut new_test_ext(parachains), || {
// too many messages.
Parachains::queue_upward_messages(0.into(), &vec![
UpwardMessage { origin: ParachainDispatchOrigin::Signed, data: vec![0] },
]);
let messages = vec![
UpwardMessage { origin: ParachainDispatchOrigin::Signed, data: vec![1] },
UpwardMessage { origin: ParachainDispatchOrigin::Signed, data: vec![2] },
];
assert_err!(
Parachains::check_upward_messages(0.into(), &messages, 2, 3),
"Messages added when queue full"
);
});
}
#[test]
fn check_queued_total_oversize_upward_messages_fails() {
let parachains = vec![
(0u32.into(), vec![], vec![]),
];
with_externalities(&mut new_test_ext(parachains), || {
// too much data.
Parachains::queue_upward_messages(0.into(), &vec![
UpwardMessage { origin: ParachainDispatchOrigin::Signed, data: vec![0, 1] },
]);
let messages = vec![
UpwardMessage { origin: ParachainDispatchOrigin::Signed, data: vec![2, 3] },
];
assert_err!(
Parachains::check_upward_messages(0.into(), &messages, 2, 3),
"Messages added when queue full"
);
});
}
#[test]
fn check_queued_pre_jumbo_upward_messages_fails() {
let parachains = vec![
(0u32.into(), vec![], vec![]),
];
with_externalities(&mut new_test_ext(parachains), || {
// bad - already an oversize messages queued.
Parachains::queue_upward_messages(0.into(), &vec![
UpwardMessage { origin: ParachainDispatchOrigin::Signed, data: vec![0; 4] },
]);
let messages = vec![
UpwardMessage { origin: ParachainDispatchOrigin::Signed, data: vec![0] }
];
assert_err!(
Parachains::check_upward_messages(0.into(), &messages, 2, 3),
"Messages added when queue full"
);
});
}
#[test]
fn check_queued_post_jumbo_upward_messages_fails() {
let parachains = vec![
(0u32.into(), vec![], vec![]),
];
with_externalities(&mut new_test_ext(parachains), || {
// bad - oversized and already a message queued.
Parachains::queue_upward_messages(0.into(), &vec![
UpwardMessage { origin: ParachainDispatchOrigin::Signed, data: vec![0] },
]);
let messages = vec![
UpwardMessage { origin: ParachainDispatchOrigin::Signed, data: vec![0; 4] }
];
assert_err!(
Parachains::check_upward_messages(0.into(), &messages, 2, 3),
"Messages added when queue full"
);
});
}
#[test]
fn upward_queuing_works() {
// That the list of egress queue roots is in ascending order by `ParaId`.
let parachains = vec![
(0u32.into(), vec![], vec![]),
(1u32.into(), vec![], vec![]),
];
with_externalities(&mut new_test_ext(parachains), || {
// parachain 0 is self
let mut candidates = vec![
new_candidate_with_upward_messages(0, vec![
(ParachainDispatchOrigin::Signed, vec![1]),
]),
new_candidate_with_upward_messages(1, vec![
(ParachainDispatchOrigin::Parachain, vec![2]),
])
];
candidates.iter_mut().for_each(make_attestations);
assert_ok!(Parachains::dispatch(
set_heads(candidates),
Origin::NONE,
));
assert!(<RelayDispatchQueue<Test>>::get(ParaId::from(0)).is_empty());
assert!(<RelayDispatchQueue<Test>>::get(ParaId::from(1)).is_empty());
});
}
fn active_parachains_should_work() {
let parachains = vec![
asynchronous rob
committed
(5u32.into(), vec![1,2,3], vec![1]),
(100u32.into(), vec![4,5,6], vec![2]),
];
with_externalities(&mut new_test_ext(parachains), || {
assert_eq!(Parachains::active_parachains(), vec![5u32.into(), 100u32.into()]);
assert_eq!(Parachains::parachain_code(&5u32.into()), Some(vec![1,2,3]));
assert_eq!(Parachains::parachain_code(&100u32.into()), Some(vec![4,5,6]));
});
}
#[test]
fn register_deregister() {
let parachains = vec![
asynchronous rob
committed
(5u32.into(), vec![1,2,3], vec![1]),
(100u32.into(), vec![4,5,6], vec![2,]),
];
with_externalities(&mut new_test_ext(parachains), || {
assert_eq!(Parachains::active_parachains(), vec![5u32.into(), 100u32.into()]);
assert_eq!(Parachains::parachain_code(&5u32.into()), Some(vec![1,2,3]));
assert_eq!(Parachains::parachain_code(&100u32.into()), Some(vec![4,5,6]));
assert_ok!(Parachains::register_parachain(99u32.into(), vec![7,8,9], vec![1, 1, 1]));
assert_eq!(Parachains::active_parachains(), vec![5u32.into(), 99u32.into(), 100u32.into()]);
assert_eq!(Parachains::parachain_code(&99u32.into()), Some(vec![7,8,9]));
assert_ok!(Parachains::deregister_parachain(5u32.into()));
assert_eq!(Parachains::active_parachains(), vec![99u32.into(), 100u32.into()]);
assert_eq!(Parachains::parachain_code(&5u32.into()), None);
});
}
#[test]
fn duty_roster_works() {
let parachains = vec![
asynchronous rob
committed
(0u32.into(), vec![], vec![]),
(1u32.into(), vec![], vec![]),
];
with_externalities(&mut new_test_ext(parachains), || {
let check_roster = |duty_roster: &DutyRoster| {
assert_eq!(duty_roster.validator_duty.len(), 8);
for i in (0..2).map(ParaId::from) {
assert_eq!(duty_roster.validator_duty.iter().filter(|&&j| j == Chain::Parachain(i)).count(), 3);
}
assert_eq!(duty_roster.validator_duty.iter().filter(|&&j| j == Chain::Relay).count(), 2);
};
let duty_roster_0 = Parachains::calculate_duty_roster();
check_roster(&duty_roster_0);
System::initialize(&1, &H256::from([1; 32]), &Default::default(), &Default::default());
let duty_roster_1 = Parachains::calculate_duty_roster();
check_roster(&duty_roster_1);
assert!(duty_roster_0 != duty_roster_1);
System::initialize(&2, &H256::from([2; 32]), &Default::default(), &Default::default());
let duty_roster_2 = Parachains::calculate_duty_roster();
check_roster(&duty_roster_2);
assert!(duty_roster_0 != duty_roster_2);
assert!(duty_roster_1 != duty_roster_2);
});
}
#[test]
fn unattested_candidate_is_rejected() {
let parachains = vec![
(0u32.into(), vec![], vec![]),
(1u32.into(), vec![], vec![]),
];
with_externalities(&mut new_test_ext(parachains), || {
let candidate = AttestedCandidate {
validity_votes: vec![],
candidate: CandidateReceipt {
parachain_index: 0.into(),
collator: Default::default(),
signature: Default::default(),
head_data: HeadData(vec![1, 2, 3]),
egress_queue_roots: vec![],
fees: 0,
block_data_hash: Default::default(),
assert!(Parachains::dispatch(set_heads(vec![candidate]), Origin::NONE).is_err());
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
})
}
#[test]
fn attested_candidates_accepted_in_order() {
let parachains = vec![
(0u32.into(), vec![], vec![]),
(1u32.into(), vec![], vec![]),
];
with_externalities(&mut new_test_ext(parachains), || {
let mut candidate_a = AttestedCandidate {
validity_votes: vec![],
candidate: CandidateReceipt {
parachain_index: 0.into(),
collator: Default::default(),
signature: Default::default(),
head_data: HeadData(vec![1, 2, 3]),
egress_queue_roots: vec![],
fees: 0,
block_data_hash: Default::default(),
}
};
let mut candidate_b = AttestedCandidate {
validity_votes: vec![],
candidate: CandidateReceipt {
parachain_index: 1.into(),
collator: Default::default(),
signature: Default::default(),
head_data: HeadData(vec![2, 3, 4]),
egress_queue_roots: vec![],
fees: 0,
block_data_hash: Default::default(),
}
};
make_attestations(&mut candidate_a);
make_attestations(&mut candidate_b);
assert!(Parachains::dispatch(
set_heads(vec![candidate_b.clone(), candidate_a.clone()]),
).is_err());
assert!(Parachains::dispatch(
set_heads(vec![candidate_a.clone(), candidate_b.clone()]),
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
).is_ok());
});
}
#[test]
fn duplicate_vote_is_rejected() {
let parachains = vec![
(0u32.into(), vec![], vec![]),
(1u32.into(), vec![], vec![]),
];
with_externalities(&mut new_test_ext(parachains), || {
let mut candidate = AttestedCandidate {
validity_votes: vec![],
candidate: CandidateReceipt {
parachain_index: 0.into(),
collator: Default::default(),
signature: Default::default(),
head_data: HeadData(vec![1, 2, 3]),
egress_queue_roots: vec![],
fees: 0,
block_data_hash: Default::default(),
}
};
make_attestations(&mut candidate);
let mut double_validity = candidate.clone();
double_validity.validity_votes.push(candidate.validity_votes[0].clone());
assert!(Parachains::dispatch(
set_heads(vec![double_validity]),
).is_err());
});
}
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
#[test]
fn ingress_works() {
let parachains = vec![
(0u32.into(), vec![], vec![]),
(1u32.into(), vec![], vec![]),
(99u32.into(), vec![1, 2, 3], vec![4, 5, 6]),
];
with_externalities(&mut new_test_ext(parachains), || {
let from_a = vec![(1.into(), [1; 32].into())];
let mut candidate_a = AttestedCandidate {
validity_votes: vec![],
candidate: CandidateReceipt {
parachain_index: 0.into(),
collator: Default::default(),
signature: Default::default(),
head_data: HeadData(vec![1, 2, 3]),
egress_queue_roots: from_a.clone(),
fees: 0,
block_data_hash: Default::default(),
}
};
let from_b = vec![(99.into(), [1; 32].into())];
let mut candidate_b = AttestedCandidate {
validity_votes: vec![],
candidate: CandidateReceipt {
parachain_index: 1.into(),
collator: Default::default(),
signature: Default::default(),
head_data: HeadData(vec![1, 2, 3]),
egress_queue_roots: from_b.clone(),
fees: 0,
block_data_hash: Default::default(),
}
};
make_attestations(&mut candidate_a);
make_attestations(&mut candidate_b);
assert_eq!(Parachains::ingress(ParaId::from(1)), Some(Vec::new()));
assert_eq!(Parachains::ingress(ParaId::from(99)), Some(Vec::new()));
assert!(Parachains::dispatch(
set_heads(vec![candidate_a, candidate_b]),
).is_ok());
assert_eq!(
Parachains::ingress(ParaId::from(1)),
Some(vec![(0.into(), [1; 32].into())]),
);
assert_eq!(
Parachains::ingress(ParaId::from(99)),
Some(vec![(1.into(), [1; 32].into())]),
);
assert_ok!(Parachains::deregister_parachain(1u32.into()));
// after deregistering, there is no ingress to 1 and we stop routing
// from 1.
assert_eq!(Parachains::ingress(ParaId::from(1)), None);
assert_eq!(Parachains::ingress(ParaId::from(99)), Some(Vec::new()));
});
}
#[test]
fn egress_routed_to_non_existent_parachain_is_rejected() {
// That no parachain is routed to which doesn't exist
let parachains = vec![
(0u32.into(), vec![], vec![]),
(1u32.into(), vec![], vec![]),
];
with_externalities(&mut new_test_ext(parachains), || {
// parachain 99 does not exist
let non_existent = vec![(99.into(), [1; 32].into())];
let mut candidate = new_candidate_with_egress_roots(non_existent);
make_attestations(&mut candidate);
let result = Parachains::dispatch(
set_heads(vec![candidate.clone()]),
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
);
assert_eq!(Err("Routing to non-existent parachain"), result);
});
}
#[test]
fn egress_routed_to_self_is_rejected() {
// That the parachain doesn't route to self
let parachains = vec![
(0u32.into(), vec![], vec![]),
(1u32.into(), vec![], vec![]),
];
with_externalities(&mut new_test_ext(parachains), || {
// parachain 0 is self
let to_self = vec![(0.into(), [1; 32].into())];
let mut candidate = new_candidate_with_egress_roots(to_self);
make_attestations(&mut candidate);
let result = Parachains::dispatch(
set_heads(vec![candidate.clone()]),
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
);
assert_eq!(Err("Parachain routing to self"), result);
});
}
#[test]
fn egress_queue_roots_out_of_order_rejected() {
// That the list of egress queue roots is in ascending order by `ParaId`.
let parachains = vec![
(0u32.into(), vec![], vec![]),
(1u32.into(), vec![], vec![]),
];
with_externalities(&mut new_test_ext(parachains), || {
// parachain 0 is self
let out_of_order = vec![(1.into(), [1; 32].into()), ((0.into(), [1; 32].into()))];
let mut candidate = new_candidate_with_egress_roots(out_of_order);
make_attestations(&mut candidate);
let result = Parachains::dispatch(
set_heads(vec![candidate.clone()]),
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
);
assert_eq!(Err("Egress routes out of order by ID"), result);
});
}
#[test]
fn egress_queue_roots_empty_trie_roots_rejected() {
let parachains = vec![
(0u32.into(), vec![], vec![]),
(1u32.into(), vec![], vec![]),
(2u32.into(), vec![], vec![]),
];
with_externalities(&mut new_test_ext(parachains), || {
// parachain 0 is self
let contains_empty_trie_root = vec![(1.into(), [1; 32].into()), ((2.into(), EMPTY_TRIE_ROOT.into()))];
let mut candidate = new_candidate_with_egress_roots(contains_empty_trie_root);
make_attestations(&mut candidate);
let result = Parachains::dispatch(
set_heads(vec![candidate.clone()]),
);
assert_eq!(Err("Empty trie root included"), result);
});
}
#[test]
fn empty_trie_root_const_is_blake2_hashed_null_node() {
let hashed_null_node = <NodeCodec<Blake2Hasher> as trie_db::NodeCodec<Blake2Hasher>>::hashed_null_node();
assert_eq!(hashed_null_node, EMPTY_TRIE_ROOT.into())
}