Skip to content

Commit e775b74

Browse files
committed
fix: fix vm version select
1 parent d0a434a commit e775b74

File tree

20 files changed

+183
-73
lines changed

20 files changed

+183
-73
lines changed

ckb-bin/src/subcommand/export.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ pub fn export(args: ExportArgs, async_handle: Handle) -> Result<(), ExitCode> {
1010
&args.config.db,
1111
None,
1212
async_handle,
13+
args.consensus,
1314
)?;
14-
let (shared, _) = builder.consensus(args.consensus).build()?;
15+
let (shared, _) = builder.build()?;
1516
Export::new(shared, args.target).execute().map_err(|err| {
1617
eprintln!("Export error: {err:?}");
1718
ExitCode::Failure

ckb-bin/src/subcommand/import.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ pub fn import(args: ImportArgs, async_handle: Handle) -> Result<(), ExitCode> {
1111
&args.config.db,
1212
None,
1313
async_handle,
14+
args.consensus,
1415
)?;
15-
let (shared, mut pack) = builder.consensus(args.consensus).build()?;
16+
let (shared, mut pack) = builder.build()?;
1617

1718
let chain_service = ChainService::new(shared, pack.take_proposal_table());
1819
let chain_controller = chain_service.start::<&str>(Some("ImportChainService"));

ckb-bin/src/subcommand/migrate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::cmp::Ordering;
66
use crate::helper::prompt;
77

88
pub fn migrate(args: MigrateArgs) -> Result<(), ExitCode> {
9-
let migrate = Migrate::new(&args.config.db.path);
9+
let migrate = Migrate::new(&args.config.db.path, args.consensus.hardfork_switch);
1010

1111
{
1212
let read_only_db = migrate.open_read_only_db().map_err(|e| {

ckb-bin/src/subcommand/replay.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ pub fn replay(args: ReplayArgs, async_handle: Handle) -> Result<(), ExitCode> {
1818
&args.config.db,
1919
None,
2020
async_handle.clone(),
21+
args.consensus.clone(),
2122
)?;
2223
let (shared, _) = shared_builder
23-
.consensus(args.consensus.clone())
2424
.tx_pool_config(args.config.tx_pool.clone())
2525
.build()?;
2626

@@ -45,11 +45,9 @@ pub fn replay(args: ReplayArgs, async_handle: Handle) -> Result<(), ExitCode> {
4545
&tmp_db_config,
4646
None,
4747
async_handle,
48+
args.consensus,
4849
)?;
49-
let (tmp_shared, mut pack) = shared_builder
50-
.consensus(args.consensus)
51-
.tx_pool_config(args.config.tx_pool)
52-
.build()?;
50+
let (tmp_shared, mut pack) = shared_builder.tx_pool_config(args.config.tx_pool).build()?;
5351
let chain = ChainService::new(tmp_shared, pack.take_proposal_table());
5452

5553
if let Some((from, to)) = args.profile {

ckb-bin/src/subcommand/stats.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ impl Statics {
3333
&args.config.db,
3434
None,
3535
async_handle,
36+
args.consensus,
3637
)?;
37-
let (shared, _) = shared_builder.consensus(args.consensus).build()?;
38+
let (shared, _) = shared_builder.build()?;
3839

3940
let tip_number = shared.snapshot().tip_number();
4041

rpc/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,12 +1659,12 @@ Response
16591659
"genesis_hash": "0x7978ec7ce5b507cfb52e149e36b1a23f6062ed150503c85bbf825da3599095ed",
16601660
"hardfork_features": [
16611661
{ "rfc": "0028", "epoch_number": "0x1526" },
1662-
{ "rfc": "0029", "epoch_number": "0x0" },
1663-
{ "rfc": "0030", "epoch_number": "0x0" },
1664-
{ "rfc": "0031", "epoch_number": "0x0" },
1665-
{ "rfc": "0032", "epoch_number": "0x0" },
1666-
{ "rfc": "0036", "epoch_number": "0x0" },
1667-
{ "rfc": "0038", "epoch_number": "0x0" },
1662+
{ "rfc": "0029", "epoch_number": "0x1526" },
1663+
{ "rfc": "0030", "epoch_number": "0x1526" },
1664+
{ "rfc": "0031", "epoch_number": "0x1526" },
1665+
{ "rfc": "0032", "epoch_number": "0x1526" },
1666+
{ "rfc": "0036", "epoch_number": "0x1526" },
1667+
{ "rfc": "0038", "epoch_number": "0x1526" },
16681668
{ "rfc": "0048", "epoch_number": null },
16691669
{ "rfc": "0049", "epoch_number": null }
16701670
],

rpc/src/module/chain.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,12 +1341,12 @@ pub trait ChainRpc {
13411341
/// "genesis_hash": "0x7978ec7ce5b507cfb52e149e36b1a23f6062ed150503c85bbf825da3599095ed",
13421342
/// "hardfork_features": [
13431343
/// { "rfc": "0028", "epoch_number": "0x1526" },
1344-
/// { "rfc": "0029", "epoch_number": "0x0" },
1345-
/// { "rfc": "0030", "epoch_number": "0x0" },
1346-
/// { "rfc": "0031", "epoch_number": "0x0" },
1347-
/// { "rfc": "0032", "epoch_number": "0x0" },
1348-
/// { "rfc": "0036", "epoch_number": "0x0" },
1349-
/// { "rfc": "0038", "epoch_number": "0x0" },
1344+
/// { "rfc": "0029", "epoch_number": "0x1526" },
1345+
/// { "rfc": "0030", "epoch_number": "0x1526" },
1346+
/// { "rfc": "0031", "epoch_number": "0x1526" },
1347+
/// { "rfc": "0032", "epoch_number": "0x1526" },
1348+
/// { "rfc": "0036", "epoch_number": "0x1526" },
1349+
/// { "rfc": "0038", "epoch_number": "0x1526" },
13501350
/// { "rfc": "0048", "epoch_number": null },
13511351
/// { "rfc": "0049", "epoch_number": null }
13521352
/// ],

script/src/verify.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,18 @@ impl<DL: CellDataProvider + HeaderProvider + ExtensionProvider + Send + Sync + C
536536
}
537537
}
538538

539+
fn is_vm_version_1_and_syscalls_2_enabled(&self) -> bool {
540+
// If the proposal window is allowed to prejudge on the vm version,
541+
// it will cause proposal tx to start a new vm in the blocks before hardfork,
542+
// destroying the assumption that the transaction execution only uses the old vm
543+
// before hardfork, leading to unexpected network splits.
544+
let epoch_number = self.tx_env.epoch_number_without_proposal_window();
545+
let hardfork_switch = self.consensus.hardfork_switch();
546+
hardfork_switch
547+
.ckb2021
548+
.is_vm_version_1_and_syscalls_2_enabled(epoch_number)
549+
}
550+
539551
fn is_vm_version_2_and_syscalls_3_enabled(&self) -> bool {
540552
// If the proposal window is allowed to prejudge on the vm version,
541553
// it will cause proposal tx to start a new vm in the blocks before hardfork,
@@ -551,6 +563,7 @@ impl<DL: CellDataProvider + HeaderProvider + ExtensionProvider + Send + Sync + C
551563
/// Returns the version of the machine based on the script and the consensus rules.
552564
pub fn select_version(&self, script: &Script) -> Result<ScriptVersion, ScriptError> {
553565
let is_vm_version_2_and_syscalls_3_enabled = self.is_vm_version_2_and_syscalls_3_enabled();
566+
let is_vm_version_1_and_syscalls_2_enabled = self.is_vm_version_1_and_syscalls_2_enabled();
554567
let script_hash_type = ScriptHashType::try_from(script.hash_type())
555568
.map_err(|err| ScriptError::InvalidScriptHashType(err.to_string()))?;
556569
match script_hash_type {
@@ -566,8 +579,10 @@ impl<DL: CellDataProvider + HeaderProvider + ExtensionProvider + Send + Sync + C
566579
ScriptHashType::Type => {
567580
if is_vm_version_2_and_syscalls_3_enabled {
568581
Ok(ScriptVersion::V2)
569-
} else {
582+
} else if is_vm_version_1_and_syscalls_2_enabled {
570583
Ok(ScriptVersion::V1)
584+
} else {
585+
Ok(ScriptVersion::V0)
571586
}
572587
}
573588
}

script/src/verify/tests/ckb_latest/features_since_v2019.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ fn check_signature_referenced_via_type_hash() {
193193
#[test]
194194
fn check_signature_referenced_via_type_hash_ok_with_multiple_matches() {
195195
let script_version = SCRIPT_VERSION;
196+
if script_version < ScriptVersion::V1 {
197+
// This transaction is not supported in the 2019 version
198+
return;
199+
}
196200

197201
let mut file = open_cell_always_success();
198202
let mut buffer = Vec::new();

script/src/verify/tests/utils.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ pub(crate) struct TransactionScriptsVerifierWithEnv {
126126
// Ref: https://doc.rust-lang.org/reference/destructors.html
127127
store: Arc<ChainDB>,
128128
consensus: Arc<Consensus>,
129+
version_1_enabled_at: EpochNumber,
129130
version_2_enabled_at: EpochNumber,
130131
_tmp_dir: TempDir,
131132
}
@@ -135,10 +136,15 @@ impl TransactionScriptsVerifierWithEnv {
135136
let tmp_dir = TempDir::new().unwrap();
136137
let db = RocksDB::open_in(&tmp_dir, COLUMNS);
137138
let store = Arc::new(ChainDB::new(db, Default::default()));
139+
let version_1_enabled_at = 10;
138140
let version_2_enabled_at = 10;
139141

140142
let hardfork_switch = HardForks {
141-
ckb2021: CKB2021::new_mirana(),
143+
ckb2021: CKB2021::new_mirana()
144+
.as_builder()
145+
.rfc_0032(version_1_enabled_at)
146+
.build()
147+
.unwrap(),
142148
ckb2023: CKB2023::new_mirana()
143149
.as_builder()
144150
.rfc_0049(version_2_enabled_at)
@@ -152,6 +158,7 @@ impl TransactionScriptsVerifierWithEnv {
152158
);
153159
Self {
154160
store,
161+
version_1_enabled_at,
155162
version_2_enabled_at,
156163
consensus,
157164
_tmp_dir: tmp_dir,
@@ -237,7 +244,7 @@ impl TransactionScriptsVerifierWithEnv {
237244
let data_loader = self.store.as_data_loader();
238245
let epoch = match version {
239246
ScriptVersion::V0 => EpochNumberWithFraction::new(0, 0, 1),
240-
ScriptVersion::V1 => EpochNumberWithFraction::new(0, 0, 1),
247+
ScriptVersion::V1 => EpochNumberWithFraction::new(self.version_1_enabled_at, 0, 1),
241248
ScriptVersion::V2 => EpochNumberWithFraction::new(self.version_2_enabled_at, 0, 1),
242249
};
243250
let header = HeaderView::new_advanced_builder()

spec/src/hardfork.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ impl HardForkConfig {
2020
/// sets all `None` to default values, otherwise, return an `Err`.
2121
pub fn complete_mainnet(&self) -> Result<HardForks, String> {
2222
let mut ckb2021 = CKB2021::new_builder();
23-
ckb2021 = self.update_2021(
24-
ckb2021,
25-
mainnet::CKB2021_START_EPOCH,
26-
mainnet::RFC0028_START_EPOCH,
27-
)?;
23+
ckb2021 = self.update_2021(ckb2021, mainnet::CKB2021_START_EPOCH)?;
2824

2925
Ok(HardForks {
3026
ckb2021: ckb2021.build()?,
@@ -36,11 +32,7 @@ impl HardForkConfig {
3632
/// sets all `None` to default values, otherwise, return an `Err`.
3733
pub fn complete_testnet(&self) -> Result<HardForks, String> {
3834
let mut ckb2021 = CKB2021::new_builder();
39-
ckb2021 = self.update_2021(
40-
ckb2021,
41-
testnet::CKB2021_START_EPOCH,
42-
testnet::RFC0028_START_EPOCH,
43-
)?;
35+
ckb2021 = self.update_2021(ckb2021, testnet::CKB2021_START_EPOCH)?;
4436
let mut ckb2023 = CKB2023::new_builder();
4537
ckb2023 = self.update_2023(ckb2023, testnet::CKB2023_START_EPOCH)?;
4638

@@ -54,10 +46,9 @@ impl HardForkConfig {
5446
&self,
5547
builder: CKB2021Builder,
5648
ckb2021: EpochNumber,
57-
rfc_0028_start: EpochNumber,
5849
) -> Result<CKB2021Builder, String> {
5950
let builder = builder
60-
.rfc_0028(rfc_0028_start)
51+
.rfc_0028(ckb2021)
6152
.rfc_0029(ckb2021)
6253
.rfc_0030(ckb2021)
6354
.rfc_0031(ckb2021)
@@ -80,22 +71,14 @@ impl HardForkConfig {
8071
///
8172
/// Enable features which are set to `None` at the dev default config.
8273
pub fn complete_with_dev_default(&self) -> Result<HardForks, String> {
83-
let mut ckb2021 = CKB2021::new_builder();
84-
ckb2021 = self.update_2021(
85-
ckb2021,
86-
testnet::CKB2021_START_EPOCH,
87-
testnet::RFC0028_START_EPOCH,
88-
)?;
74+
let ckb2021 = CKB2021::new_dev_default();
8975

9076
let ckb2023 = if let Some(epoch) = self.ckb2023 {
9177
CKB2023::new_with_specified(epoch)
9278
} else {
9379
CKB2023::new_dev_default()
9480
};
9581

96-
Ok(HardForks {
97-
ckb2021: ckb2021.build()?,
98-
ckb2023,
99-
})
82+
Ok(HardForks { ckb2021, ckb2023 })
10083
}
10184
}

util/constant/src/hardfork/mainnet.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/// The Chain Specification name.
22
pub const CHAIN_SPEC_NAME: &str = "ckb";
33

4-
/// hardcode rfc0028 epoch
5-
pub const RFC0028_START_EPOCH: u64 = 5414;
64
/// First epoch number for CKB v2021, at about 2022/05/10 1:00 UTC
7-
// pub const CKB2021_START_EPOCH: u64 = 5414;
8-
pub const CKB2021_START_EPOCH: u64 = 0;
5+
pub const CKB2021_START_EPOCH: u64 = 5414;
6+
// pub const CKB2021_START_EPOCH: u64 = 0;
97

108
/// hardcode ckb2023 epoch
119
pub const CKB2023_START_EPOCH: u64 = u64::MAX;

util/constant/src/hardfork/testnet.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/// The Chain Specification name.
22
pub const CHAIN_SPEC_NAME: &str = "ckb_testnet";
33

4-
/// hardcode rfc0028 epoch
5-
pub const RFC0028_START_EPOCH: u64 = 3113;
64
/// First epoch number for CKB v2021, at about 2021/10/24 3:15 UTC.
7-
// pub const CKB2021_START_EPOCH: u64 = 3113;
8-
pub const CKB2021_START_EPOCH: u64 = 0;
5+
pub const CKB2021_START_EPOCH: u64 = 3113;
6+
// pub const CKB2021_START_EPOCH: u64 = 0;
97

108
/// hardcode ckb2023 epoch
119
pub const CKB2023_START_EPOCH: u64 = u64::MAX;

util/launcher/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ impl Launcher {
204204
&self.args.config.db,
205205
Some(self.args.config.ancient.clone()),
206206
self.async_handle.clone(),
207+
self.args.consensus.clone(),
207208
)?;
208209

209210
let (shared, pack) = shared_builder
210-
.consensus(self.args.consensus.clone())
211211
.tx_pool_config(self.args.config.tx_pool.clone())
212212
.notify_config(self.args.config.notify.clone())
213213
.store_config(self.args.config.store)

util/launcher/src/migrate.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use ckb_db::{ReadOnlyDB, RocksDB};
55
use ckb_db_migration::{DefaultMigration, Migrations};
66
use ckb_db_schema::{COLUMNS, COLUMN_META};
77
use ckb_error::Error;
8+
use ckb_types::core::hardfork::HardForks;
89
use std::cmp::Ordering;
910
use std::path::PathBuf;
1011

@@ -18,7 +19,7 @@ pub struct Migrate {
1819

1920
impl Migrate {
2021
/// Construct new migrate
21-
pub fn new<P: Into<PathBuf>>(path: P) -> Self {
22+
pub fn new<P: Into<PathBuf>>(path: P, hardforks: HardForks) -> Self {
2223
let mut migrations = Migrations::default();
2324
migrations.add_migration(Box::new(DefaultMigration::new(INIT_DB_VERSION)));
2425
migrations.add_migration(Box::new(migrations::ChangeMoleculeTableToStruct)); // since v0.35.0
@@ -29,6 +30,7 @@ impl Migrate {
2930
migrations.add_migration(Box::new(migrations::AddChainRootMMR)); // TODO(light-client) update the comment: which version?
3031
migrations.add_migration(Box::new(migrations::AddBlockFilterColumnFamily)); // since v0.105.0
3132
migrations.add_migration(Box::new(migrations::AddBlockFilterHash)); // since v0.108.0
33+
migrations.add_migration(Box::new(migrations::BlockExt2019ToZero::new(hardforks))); // since v0.111.0
3234

3335
Migrate {
3436
migrations,

util/launcher/src/migrations/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod add_chain_root_mmr;
55
mod add_extra_data_hash;
66
mod add_number_hash_mapping;
77
mod cell;
8+
mod set_2019_block_cycle_zero;
89
mod table_to_struct;
910

1011
pub use add_block_extension_cf::AddBlockExtensionColumnFamily;
@@ -14,4 +15,5 @@ pub use add_chain_root_mmr::AddChainRootMMR;
1415
pub use add_extra_data_hash::AddExtraDataHash;
1516
pub use add_number_hash_mapping::AddNumberHashMapping;
1617
pub use cell::CellMigration;
18+
pub use set_2019_block_cycle_zero::BlockExt2019ToZero;
1719
pub use table_to_struct::ChangeMoleculeTableToStruct;

0 commit comments

Comments
 (0)