Skip to content

Commit

Permalink
fix: fix vm version select
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Nov 2, 2023
1 parent d0a434a commit e775b74
Show file tree
Hide file tree
Showing 20 changed files with 183 additions and 73 deletions.
3 changes: 2 additions & 1 deletion ckb-bin/src/subcommand/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ pub fn export(args: ExportArgs, async_handle: Handle) -> Result<(), ExitCode> {
&args.config.db,
None,
async_handle,
args.consensus,
)?;
let (shared, _) = builder.consensus(args.consensus).build()?;
let (shared, _) = builder.build()?;
Export::new(shared, args.target).execute().map_err(|err| {
eprintln!("Export error: {err:?}");
ExitCode::Failure
Expand Down
3 changes: 2 additions & 1 deletion ckb-bin/src/subcommand/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ pub fn import(args: ImportArgs, async_handle: Handle) -> Result<(), ExitCode> {
&args.config.db,
None,
async_handle,
args.consensus,
)?;
let (shared, mut pack) = builder.consensus(args.consensus).build()?;
let (shared, mut pack) = builder.build()?;

let chain_service = ChainService::new(shared, pack.take_proposal_table());
let chain_controller = chain_service.start::<&str>(Some("ImportChainService"));
Expand Down
2 changes: 1 addition & 1 deletion ckb-bin/src/subcommand/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::cmp::Ordering;
use crate::helper::prompt;

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

{
let read_only_db = migrate.open_read_only_db().map_err(|e| {
Expand Down
8 changes: 3 additions & 5 deletions ckb-bin/src/subcommand/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub fn replay(args: ReplayArgs, async_handle: Handle) -> Result<(), ExitCode> {
&args.config.db,
None,
async_handle.clone(),
args.consensus.clone(),
)?;
let (shared, _) = shared_builder
.consensus(args.consensus.clone())
.tx_pool_config(args.config.tx_pool.clone())
.build()?;

Expand All @@ -45,11 +45,9 @@ pub fn replay(args: ReplayArgs, async_handle: Handle) -> Result<(), ExitCode> {
&tmp_db_config,
None,
async_handle,
args.consensus,
)?;
let (tmp_shared, mut pack) = shared_builder
.consensus(args.consensus)
.tx_pool_config(args.config.tx_pool)
.build()?;
let (tmp_shared, mut pack) = shared_builder.tx_pool_config(args.config.tx_pool).build()?;
let chain = ChainService::new(tmp_shared, pack.take_proposal_table());

if let Some((from, to)) = args.profile {
Expand Down
3 changes: 2 additions & 1 deletion ckb-bin/src/subcommand/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ impl Statics {
&args.config.db,
None,
async_handle,
args.consensus,
)?;
let (shared, _) = shared_builder.consensus(args.consensus).build()?;
let (shared, _) = shared_builder.build()?;

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

Expand Down
12 changes: 6 additions & 6 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1659,12 +1659,12 @@ Response
"genesis_hash": "0x7978ec7ce5b507cfb52e149e36b1a23f6062ed150503c85bbf825da3599095ed",
"hardfork_features": [
{ "rfc": "0028", "epoch_number": "0x1526" },
{ "rfc": "0029", "epoch_number": "0x0" },
{ "rfc": "0030", "epoch_number": "0x0" },
{ "rfc": "0031", "epoch_number": "0x0" },
{ "rfc": "0032", "epoch_number": "0x0" },
{ "rfc": "0036", "epoch_number": "0x0" },
{ "rfc": "0038", "epoch_number": "0x0" },
{ "rfc": "0029", "epoch_number": "0x1526" },
{ "rfc": "0030", "epoch_number": "0x1526" },
{ "rfc": "0031", "epoch_number": "0x1526" },
{ "rfc": "0032", "epoch_number": "0x1526" },
{ "rfc": "0036", "epoch_number": "0x1526" },
{ "rfc": "0038", "epoch_number": "0x1526" },
{ "rfc": "0048", "epoch_number": null },
{ "rfc": "0049", "epoch_number": null }
],
Expand Down
12 changes: 6 additions & 6 deletions rpc/src/module/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,12 +1341,12 @@ pub trait ChainRpc {
/// "genesis_hash": "0x7978ec7ce5b507cfb52e149e36b1a23f6062ed150503c85bbf825da3599095ed",
/// "hardfork_features": [
/// { "rfc": "0028", "epoch_number": "0x1526" },
/// { "rfc": "0029", "epoch_number": "0x0" },
/// { "rfc": "0030", "epoch_number": "0x0" },
/// { "rfc": "0031", "epoch_number": "0x0" },
/// { "rfc": "0032", "epoch_number": "0x0" },
/// { "rfc": "0036", "epoch_number": "0x0" },
/// { "rfc": "0038", "epoch_number": "0x0" },
/// { "rfc": "0029", "epoch_number": "0x1526" },
/// { "rfc": "0030", "epoch_number": "0x1526" },
/// { "rfc": "0031", "epoch_number": "0x1526" },
/// { "rfc": "0032", "epoch_number": "0x1526" },
/// { "rfc": "0036", "epoch_number": "0x1526" },
/// { "rfc": "0038", "epoch_number": "0x1526" },
/// { "rfc": "0048", "epoch_number": null },
/// { "rfc": "0049", "epoch_number": null }
/// ],
Expand Down
17 changes: 16 additions & 1 deletion script/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,18 @@ impl<DL: CellDataProvider + HeaderProvider + ExtensionProvider + Send + Sync + C
}
}

fn is_vm_version_1_and_syscalls_2_enabled(&self) -> bool {
// If the proposal window is allowed to prejudge on the vm version,
// it will cause proposal tx to start a new vm in the blocks before hardfork,
// destroying the assumption that the transaction execution only uses the old vm
// before hardfork, leading to unexpected network splits.
let epoch_number = self.tx_env.epoch_number_without_proposal_window();
let hardfork_switch = self.consensus.hardfork_switch();
hardfork_switch
.ckb2021
.is_vm_version_1_and_syscalls_2_enabled(epoch_number)
}

fn is_vm_version_2_and_syscalls_3_enabled(&self) -> bool {
// If the proposal window is allowed to prejudge on the vm version,
// it will cause proposal tx to start a new vm in the blocks before hardfork,
Expand All @@ -551,6 +563,7 @@ impl<DL: CellDataProvider + HeaderProvider + ExtensionProvider + Send + Sync + C
/// Returns the version of the machine based on the script and the consensus rules.
pub fn select_version(&self, script: &Script) -> Result<ScriptVersion, ScriptError> {
let is_vm_version_2_and_syscalls_3_enabled = self.is_vm_version_2_and_syscalls_3_enabled();
let is_vm_version_1_and_syscalls_2_enabled = self.is_vm_version_1_and_syscalls_2_enabled();
let script_hash_type = ScriptHashType::try_from(script.hash_type())
.map_err(|err| ScriptError::InvalidScriptHashType(err.to_string()))?;
match script_hash_type {
Expand All @@ -566,8 +579,10 @@ impl<DL: CellDataProvider + HeaderProvider + ExtensionProvider + Send + Sync + C
ScriptHashType::Type => {
if is_vm_version_2_and_syscalls_3_enabled {
Ok(ScriptVersion::V2)
} else {
} else if is_vm_version_1_and_syscalls_2_enabled {
Ok(ScriptVersion::V1)
} else {
Ok(ScriptVersion::V0)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions script/src/verify/tests/ckb_latest/features_since_v2019.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ fn check_signature_referenced_via_type_hash() {
#[test]
fn check_signature_referenced_via_type_hash_ok_with_multiple_matches() {
let script_version = SCRIPT_VERSION;
if script_version < ScriptVersion::V1 {
// This transaction is not supported in the 2019 version
return;
}

let mut file = open_cell_always_success();
let mut buffer = Vec::new();
Expand Down
11 changes: 9 additions & 2 deletions script/src/verify/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub(crate) struct TransactionScriptsVerifierWithEnv {
// Ref: https://doc.rust-lang.org/reference/destructors.html
store: Arc<ChainDB>,
consensus: Arc<Consensus>,
version_1_enabled_at: EpochNumber,
version_2_enabled_at: EpochNumber,
_tmp_dir: TempDir,
}
Expand All @@ -135,10 +136,15 @@ impl TransactionScriptsVerifierWithEnv {
let tmp_dir = TempDir::new().unwrap();
let db = RocksDB::open_in(&tmp_dir, COLUMNS);
let store = Arc::new(ChainDB::new(db, Default::default()));
let version_1_enabled_at = 10;
let version_2_enabled_at = 10;

let hardfork_switch = HardForks {
ckb2021: CKB2021::new_mirana(),
ckb2021: CKB2021::new_mirana()
.as_builder()
.rfc_0032(version_1_enabled_at)
.build()
.unwrap(),
ckb2023: CKB2023::new_mirana()
.as_builder()
.rfc_0049(version_2_enabled_at)
Expand All @@ -152,6 +158,7 @@ impl TransactionScriptsVerifierWithEnv {
);
Self {
store,
version_1_enabled_at,
version_2_enabled_at,
consensus,
_tmp_dir: tmp_dir,
Expand Down Expand Up @@ -237,7 +244,7 @@ impl TransactionScriptsVerifierWithEnv {
let data_loader = self.store.as_data_loader();
let epoch = match version {
ScriptVersion::V0 => EpochNumberWithFraction::new(0, 0, 1),
ScriptVersion::V1 => EpochNumberWithFraction::new(0, 0, 1),
ScriptVersion::V1 => EpochNumberWithFraction::new(self.version_1_enabled_at, 0, 1),
ScriptVersion::V2 => EpochNumberWithFraction::new(self.version_2_enabled_at, 0, 1),
};
let header = HeaderView::new_advanced_builder()
Expand Down
27 changes: 5 additions & 22 deletions spec/src/hardfork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ impl HardForkConfig {
/// sets all `None` to default values, otherwise, return an `Err`.
pub fn complete_mainnet(&self) -> Result<HardForks, String> {
let mut ckb2021 = CKB2021::new_builder();
ckb2021 = self.update_2021(
ckb2021,
mainnet::CKB2021_START_EPOCH,
mainnet::RFC0028_START_EPOCH,
)?;
ckb2021 = self.update_2021(ckb2021, mainnet::CKB2021_START_EPOCH)?;

Ok(HardForks {
ckb2021: ckb2021.build()?,
Expand All @@ -36,11 +32,7 @@ impl HardForkConfig {
/// sets all `None` to default values, otherwise, return an `Err`.
pub fn complete_testnet(&self) -> Result<HardForks, String> {
let mut ckb2021 = CKB2021::new_builder();
ckb2021 = self.update_2021(
ckb2021,
testnet::CKB2021_START_EPOCH,
testnet::RFC0028_START_EPOCH,
)?;
ckb2021 = self.update_2021(ckb2021, testnet::CKB2021_START_EPOCH)?;
let mut ckb2023 = CKB2023::new_builder();
ckb2023 = self.update_2023(ckb2023, testnet::CKB2023_START_EPOCH)?;

Expand All @@ -54,10 +46,9 @@ impl HardForkConfig {
&self,
builder: CKB2021Builder,
ckb2021: EpochNumber,
rfc_0028_start: EpochNumber,
) -> Result<CKB2021Builder, String> {
let builder = builder
.rfc_0028(rfc_0028_start)
.rfc_0028(ckb2021)
.rfc_0029(ckb2021)
.rfc_0030(ckb2021)
.rfc_0031(ckb2021)
Expand All @@ -80,22 +71,14 @@ impl HardForkConfig {
///
/// Enable features which are set to `None` at the dev default config.
pub fn complete_with_dev_default(&self) -> Result<HardForks, String> {
let mut ckb2021 = CKB2021::new_builder();
ckb2021 = self.update_2021(
ckb2021,
testnet::CKB2021_START_EPOCH,
testnet::RFC0028_START_EPOCH,
)?;
let ckb2021 = CKB2021::new_dev_default();

let ckb2023 = if let Some(epoch) = self.ckb2023 {
CKB2023::new_with_specified(epoch)
} else {
CKB2023::new_dev_default()
};

Ok(HardForks {
ckb2021: ckb2021.build()?,
ckb2023,
})
Ok(HardForks { ckb2021, ckb2023 })
}
}
6 changes: 2 additions & 4 deletions util/constant/src/hardfork/mainnet.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/// The Chain Specification name.
pub const CHAIN_SPEC_NAME: &str = "ckb";

/// hardcode rfc0028 epoch
pub const RFC0028_START_EPOCH: u64 = 5414;
/// First epoch number for CKB v2021, at about 2022/05/10 1:00 UTC
// pub const CKB2021_START_EPOCH: u64 = 5414;
pub const CKB2021_START_EPOCH: u64 = 0;
pub const CKB2021_START_EPOCH: u64 = 5414;
// pub const CKB2021_START_EPOCH: u64 = 0;

/// hardcode ckb2023 epoch
pub const CKB2023_START_EPOCH: u64 = u64::MAX;
6 changes: 2 additions & 4 deletions util/constant/src/hardfork/testnet.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/// The Chain Specification name.
pub const CHAIN_SPEC_NAME: &str = "ckb_testnet";

/// hardcode rfc0028 epoch
pub const RFC0028_START_EPOCH: u64 = 3113;
/// First epoch number for CKB v2021, at about 2021/10/24 3:15 UTC.
// pub const CKB2021_START_EPOCH: u64 = 3113;
pub const CKB2021_START_EPOCH: u64 = 0;
pub const CKB2021_START_EPOCH: u64 = 3113;
// pub const CKB2021_START_EPOCH: u64 = 0;

/// hardcode ckb2023 epoch
pub const CKB2023_START_EPOCH: u64 = u64::MAX;
2 changes: 1 addition & 1 deletion util/launcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ impl Launcher {
&self.args.config.db,
Some(self.args.config.ancient.clone()),
self.async_handle.clone(),
self.args.consensus.clone(),
)?;

let (shared, pack) = shared_builder
.consensus(self.args.consensus.clone())
.tx_pool_config(self.args.config.tx_pool.clone())
.notify_config(self.args.config.notify.clone())
.store_config(self.args.config.store)
Expand Down
4 changes: 3 additions & 1 deletion util/launcher/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ckb_db::{ReadOnlyDB, RocksDB};
use ckb_db_migration::{DefaultMigration, Migrations};
use ckb_db_schema::{COLUMNS, COLUMN_META};
use ckb_error::Error;
use ckb_types::core::hardfork::HardForks;
use std::cmp::Ordering;
use std::path::PathBuf;

Expand All @@ -18,7 +19,7 @@ pub struct Migrate {

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

Migrate {
migrations,
Expand Down
2 changes: 2 additions & 0 deletions util/launcher/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod add_chain_root_mmr;
mod add_extra_data_hash;
mod add_number_hash_mapping;
mod cell;
mod set_2019_block_cycle_zero;
mod table_to_struct;

pub use add_block_extension_cf::AddBlockExtensionColumnFamily;
Expand All @@ -14,4 +15,5 @@ pub use add_chain_root_mmr::AddChainRootMMR;
pub use add_extra_data_hash::AddExtraDataHash;
pub use add_number_hash_mapping::AddNumberHashMapping;
pub use cell::CellMigration;
pub use set_2019_block_cycle_zero::BlockExt2019ToZero;
pub use table_to_struct::ChangeMoleculeTableToStruct;
Loading

0 comments on commit e775b74

Please sign in to comment.