Skip to content

Commit b1ab9cb

Browse files
authored
Merge pull request #646 from cryptape/release-0.24.1
Release 0.24.1
2 parents 1f10bc8 + 8044d67 commit b1ab9cb

File tree

2 files changed

+142
-35
lines changed

2 files changed

+142
-35
lines changed

cita-executor/src/backlogs.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use super::core::libexecutor::block::{ClosedBlock, OpenBlock};
1919
use cita_db::Itertools;
2020
use cita_types::Address;
2121
use libproto::{ExecutedResult, Proof};
22+
use std::cmp::min;
2223
use std::collections::BTreeMap;
2324

2425
#[derive(Debug, Copy, Clone)]
@@ -392,10 +393,11 @@ impl Backlogs {
392393

393394
pub fn prune(&mut self, height: u64) {
394395
// Importance guard: we must keep the executed result of the recent
395-
// 2 height(current_height - 1, current_height - 2), which used when
396-
// postman check arrived proof via `Postman::check_proof`
397-
if height + 2 < self.get_current_height() {
398-
self.completed = self.completed.split_off(&height);
396+
// 3 height(current_height, current_height - 1, current_height - 2),
397+
// which used when postman check arrived proof via `Postman::check_proof`
398+
if self.get_current_height() > 2 {
399+
let split_height = min(height, self.get_current_height() - 2);
400+
self.completed = self.completed.split_off(&split_height);
399401
}
400402
}
401403
}

cita-executor/src/postman.rs

Lines changed: 136 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,48 +1114,160 @@ mod tests {
11141114
}
11151115

11161116
#[test]
1117-
fn test_update_rich_status_with_prune() {
1118-
let mut postman = helpers::generate_postman(6, Default::default());
1117+
fn test_update_rich_status_0() {
1118+
let mut postman = helpers::generate_postman(5, Default::default());
11191119

1120-
let execute_result_1 = generate_executed_result(1);
1121-
let execute_result_2 = generate_executed_result(2);
1122-
let execute_result_3 = generate_executed_result(3);
1123-
let execute_result_4 = generate_executed_result(4);
1120+
backlogs_prepare(&mut postman);
11241121

1125-
postman
1126-
.backlogs
1127-
.insert_completed_result(1, execute_result_1);
1128-
postman
1129-
.backlogs
1130-
.insert_completed_result(2, execute_result_2);
1131-
postman
1132-
.backlogs
1133-
.insert_completed_result(3, execute_result_3);
1134-
postman
1135-
.backlogs
1136-
.insert_completed_result(4, execute_result_4);
1122+
let mut rich_status = RichStatus::new();
1123+
1124+
rich_status.set_height(0);
1125+
1126+
postman.update_by_rich_status(&rich_status);
1127+
1128+
// block height in rich_status is from Chain, that means database's block height.
1129+
// block height in postman, that means executed block height which cache in Executor.
1130+
// Testcase 0: block_height[chain] = 0, block_height[executor] = 5.
1131+
// Expected: remove the block 0 from cache, but not other block.
1132+
assert!(postman.backlogs.get_completed_result(0).is_none());
1133+
assert!(postman.backlogs.get_completed_result(1).is_some());
1134+
assert!(postman.backlogs.get_completed_result(2).is_some());
1135+
assert!(postman.backlogs.get_completed_result(3).is_some());
1136+
assert!(postman.backlogs.get_completed_result(4).is_some());
1137+
assert!(postman.backlogs.get_completed_result(5).is_some());
1138+
}
1139+
1140+
#[test]
1141+
fn test_update_rich_status_1() {
1142+
let mut postman = helpers::generate_postman(5, Default::default());
1143+
1144+
backlogs_prepare(&mut postman);
11371145

11381146
let mut rich_status = RichStatus::new();
1147+
1148+
rich_status.set_height(1);
1149+
1150+
postman.update_by_rich_status(&rich_status);
1151+
1152+
// block height in rich_status is from Chain, that means database's block height.
1153+
// block height in postman, that means executed block height which cache in Executor.
1154+
// Testcase 0: block_height[chain] = 1, block_height[executor] = 5.
1155+
// Expected: remove the block 0, 1 from cache, but not other block.
1156+
assert!(postman.backlogs.get_completed_result(0).is_none());
1157+
assert!(postman.backlogs.get_completed_result(1).is_none());
1158+
assert!(postman.backlogs.get_completed_result(2).is_some());
1159+
assert!(postman.backlogs.get_completed_result(3).is_some());
1160+
assert!(postman.backlogs.get_completed_result(4).is_some());
1161+
assert!(postman.backlogs.get_completed_result(5).is_some());
1162+
}
1163+
1164+
#[test]
1165+
fn test_update_rich_status_2() {
1166+
let mut postman = helpers::generate_postman(5, Default::default());
1167+
1168+
backlogs_prepare(&mut postman);
1169+
1170+
let mut rich_status = RichStatus::new();
1171+
11391172
rich_status.set_height(2);
1140-
// chain height = 2, executor height = 6
1141-
// 3 + 2 < 6, executor backlogs will prune executed result which height <= 2
1173+
1174+
postman.update_by_rich_status(&rich_status);
1175+
1176+
// block height in rich_status is from Chain, that means database's block height.
1177+
// block height in postman, that means executed block height which cache in Executor.
1178+
// Testcase 0: block_height[chain] = 2, block_height[executor] = 5.
1179+
// Expected: remove the block 0, 1, 2 from cache, but not other block.
1180+
assert!(postman.backlogs.get_completed_result(0).is_none());
1181+
assert!(postman.backlogs.get_completed_result(1).is_none());
1182+
assert!(postman.backlogs.get_completed_result(2).is_none());
1183+
assert!(postman.backlogs.get_completed_result(3).is_some());
1184+
assert!(postman.backlogs.get_completed_result(4).is_some());
1185+
assert!(postman.backlogs.get_completed_result(5).is_some());
1186+
}
1187+
1188+
#[test]
1189+
fn test_update_rich_status_3() {
1190+
let mut postman = helpers::generate_postman(5, Default::default());
1191+
1192+
backlogs_prepare(&mut postman);
1193+
1194+
let mut rich_status = RichStatus::new();
1195+
1196+
rich_status.set_height(3);
1197+
1198+
postman.update_by_rich_status(&rich_status);
1199+
1200+
// block height in rich_status is from Chain, that means database's block height.
1201+
// block height in postman, that means executed block height which cache in Executor.
1202+
// Testcase 0: block_height[chain] = 3, block_height[executor] = 5.
1203+
// Expected: postman needs to keep at least 3 block in cache, so remove the block 0, 1, 2 from cache, but not other block.
1204+
assert!(postman.backlogs.get_completed_result(0).is_none());
1205+
assert!(postman.backlogs.get_completed_result(1).is_none());
1206+
assert!(postman.backlogs.get_completed_result(2).is_none());
1207+
assert!(postman.backlogs.get_completed_result(3).is_some());
1208+
assert!(postman.backlogs.get_completed_result(4).is_some());
1209+
assert!(postman.backlogs.get_completed_result(5).is_some());
1210+
}
1211+
1212+
#[test]
1213+
fn test_update_rich_status_4() {
1214+
let mut postman = helpers::generate_postman(5, Default::default());
1215+
1216+
backlogs_prepare(&mut postman);
1217+
1218+
let mut rich_status = RichStatus::new();
1219+
1220+
rich_status.set_height(4);
1221+
11421222
postman.update_by_rich_status(&rich_status);
11431223

1224+
// block height in rich_status is from Chain, that means database's block height.
1225+
// block height in postman, that means executed block height which cache in Executor.
1226+
// Testcase 0: block_height[chain] = 3, block_height[executor] = 5.
1227+
// Expected: postman needs to keep at least 3 block in cache, so remove the block 0, 1, 2 from cache, but not other block.
1228+
assert!(postman.backlogs.get_completed_result(0).is_none());
11441229
assert!(postman.backlogs.get_completed_result(1).is_none());
11451230
assert!(postman.backlogs.get_completed_result(2).is_none());
11461231
assert!(postman.backlogs.get_completed_result(3).is_some());
11471232
assert!(postman.backlogs.get_completed_result(4).is_some());
1233+
assert!(postman.backlogs.get_completed_result(5).is_some());
11481234
}
11491235

11501236
#[test]
1151-
fn test_update_rich_status_without_prune() {
1237+
fn test_update_rich_status_5() {
11521238
let mut postman = helpers::generate_postman(5, Default::default());
11531239

1240+
backlogs_prepare(&mut postman);
1241+
1242+
let mut rich_status = RichStatus::new();
1243+
1244+
rich_status.set_height(5);
1245+
1246+
postman.update_by_rich_status(&rich_status);
1247+
1248+
// block height in rich_status is from Chain, that means database's block height.
1249+
// block height in postman, that means executed block height which cache in Executor.
1250+
// Testcase 0: block_height[chain] = 3, block_height[executor] = 5.
1251+
// Expected: postman needs to keep at least 3 block in cache, so remove the block 0, 1, 2 from cache, but not other block.
1252+
assert!(postman.backlogs.get_completed_result(0).is_none());
1253+
assert!(postman.backlogs.get_completed_result(1).is_none());
1254+
assert!(postman.backlogs.get_completed_result(2).is_none());
1255+
assert!(postman.backlogs.get_completed_result(3).is_some());
1256+
assert!(postman.backlogs.get_completed_result(4).is_some());
1257+
assert!(postman.backlogs.get_completed_result(5).is_some());
1258+
}
1259+
1260+
fn backlogs_prepare(postman: &mut Postman) {
1261+
let execute_result_0 = generate_executed_result(0);
11541262
let execute_result_1 = generate_executed_result(1);
11551263
let execute_result_2 = generate_executed_result(2);
11561264
let execute_result_3 = generate_executed_result(3);
11571265
let execute_result_4 = generate_executed_result(4);
1266+
let execute_result_5 = generate_executed_result(5);
11581267

1268+
postman
1269+
.backlogs
1270+
.insert_completed_result(0, execute_result_0);
11591271
postman
11601272
.backlogs
11611273
.insert_completed_result(1, execute_result_1);
@@ -1168,15 +1280,8 @@ mod tests {
11681280
postman
11691281
.backlogs
11701282
.insert_completed_result(4, execute_result_4);
1171-
1172-
let mut rich_status = RichStatus::new();
1173-
rich_status.set_height(2);
1174-
// chain height = 2, executor height = 5
1175-
// 3 + 2 = 5, not < 5, so executor backlogs will not prune
1176-
postman.update_by_rich_status(&rich_status);
1177-
1178-
assert!(postman.backlogs.get_completed_result(1).is_some());
1179-
assert!(postman.backlogs.get_completed_result(2).is_some());
1180-
assert!(postman.backlogs.get_completed_result(3).is_some());
1283+
postman
1284+
.backlogs
1285+
.insert_completed_result(5, execute_result_5);
11811286
}
11821287
}

0 commit comments

Comments
 (0)