Skip to content

Commit

Permalink
Merge 04d64c3 into f9ba8b6
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg authored Jan 18, 2023
2 parents f9ba8b6 + 04d64c3 commit 39eb5d0
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
labels: ${{ steps.docker_meta.outputs.labels }}
- name: deploy cluster
# Only deploy on master.
if: github.ref == 'refs/heads/master'
if: github.tag == 'HalleyHardFork'
shell: bash
env:
IMAGE_VERSION: ${{steps.docker_meta.outputs.version}}
Expand Down
7 changes: 7 additions & 0 deletions chain/api/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub enum ConnectBlockError {
ParentNotExist(Box<BlockHeader>),
#[error("Verify block {0} failed: {1:?}")]
VerifyBlockFailed(VerifyBlockField, Error),
#[error("Barnard hard fork block: {:?} ", .0.header())]
BarnardHardFork(Box<Block>),
}

impl ConnectBlockError {
Expand All @@ -71,11 +73,15 @@ impl ConnectBlockError {
pub const REP_VERIFY_BLOCK_FAILED: ReputationChange =
ReputationChange::new_fatal("VerifyBlockFailed");

pub const REP_BARNARD_HARD_FORK: ReputationChange =
ReputationChange::new_fatal("BarnardHardFork");

pub fn reason(&self) -> &str {
match self {
ConnectBlockError::FutureBlock(_) => "FutureBlock",
ConnectBlockError::ParentNotExist(_) => "ParentNotExist",
ConnectBlockError::VerifyBlockFailed(_, _) => "VerifyBlockFailed",
ConnectBlockError::BarnardHardFork(_) => "BarnardHardFork",
}
}

Expand All @@ -86,6 +92,7 @@ impl ConnectBlockError {
ConnectBlockError::VerifyBlockFailed(_, _) => {
ConnectBlockError::REP_VERIFY_BLOCK_FAILED
}
ConnectBlockError::BarnardHardFork(_) => ConnectBlockError::REP_BARNARD_HARD_FORK,
}
}
}
87 changes: 86 additions & 1 deletion cmd/db-exporter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::bail;
use anyhow::{bail, format_err};
use atomic_counter::AtomicCounter;
use indicatif::{ProgressBar, ProgressStyle};
use move_binary_format::errors::Location;
use starcoin_chain::{BlockChain, ChainReader};
use starcoin_config::BuiltinNetworkID;
use starcoin_config::ChainNetwork;
use starcoin_crypto::HashValue;
use starcoin_genesis::Genesis;
use starcoin_statedb::ChainStateDB;
use starcoin_storage::cache_storage::CacheStorage;
use starcoin_storage::db_storage::DBStorage;
use starcoin_storage::storage::StorageInstance;
use starcoin_storage::{Storage, StorageVersion};
use starcoin_types::block::Block;
use starcoin_types::transaction::TransactionPayload;
use starcoin_vm_types::errors::VMError;
use starcoin_vm_types::file_format::CompiledModule;
use starcoin_vm_types::on_chain_config::Version;
use starcoin_vm_types::state_view::StateReaderExt;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
Expand Down Expand Up @@ -131,3 +142,77 @@ fn verify_block_modules(block: Block) -> (usize, Vec<VerifyModuleError>) {
}
(success_modules, errors)
}

pub fn barnard_hard_fork_info(db_path: PathBuf) -> anyhow::Result<()> {
let net = ChainNetwork::new_builtin(BuiltinNetworkID::Barnard);
let db_storage = DBStorage::open_with_cfs(
db_path.join("starcoindb/db/starcoindb"),
StorageVersion::current_version()
.get_column_family_names()
.to_vec(),
true,
Default::default(),
None,
)?;
let storage = Arc::new(Storage::new(StorageInstance::new_cache_and_db_instance(
CacheStorage::new(None),
db_storage,
))?);
let (chain_info, _) = Genesis::init_and_check_storage(&net, storage.clone(), db_path.as_ref())?;
let chain = BlockChain::new(
net.time_service(),
chain_info.head().id(),
storage.clone(),
None,
)
.expect("create block chain should success.");
let mut left = 1;
let mut right = chain.status().head().number() as u64;
println!("cur_num {}", right);
let mut mid = left;
while left < right {
mid = (left + right) / 2;
let block = chain
.get_block_by_number(mid)?
.ok_or_else(|| anyhow::anyhow!("block number {} not exist", mid))?;

let root = block.header.state_root();
let statedb = ChainStateDB::new(storage.clone(), Some(root));
let stdlib_verson = statedb
.get_on_chain_config::<Version>()?
.map(|version| version.major)
.ok_or_else(|| format_err!("on chain config stdlib version can not be empty."))?;
if stdlib_verson == 12 {
right = mid;
} else {
left = mid + 1;
}
}

let block = chain
.get_block_by_number(mid + 1)?
.ok_or_else(|| anyhow::anyhow!("block number {} not exist", mid + 1))?;
let root = block.header.state_root();
let statedb = ChainStateDB::new(storage.clone(), Some(root));
let stdlib_verson = statedb
.get_on_chain_config::<Version>()?
.map(|version| version.major)
.ok_or_else(|| format_err!("on chain config stdlib version can not be empty."))?;
println!(
"stdlib_version {} number {} block hash {}",
stdlib_verson,
mid + 1,
block.header.id()
);
let block = chain
.get_block_by_number(mid)?
.ok_or_else(|| anyhow::anyhow!("block number {} not exist", mid))?;
let root = block.header.state_root();
let statedb = ChainStateDB::new(storage, Some(root));
let stdlib_verson = statedb
.get_on_chain_config::<Version>()?
.map(|version| version.major)
.ok_or_else(|| format_err!("on chain config stdlib version can not be empty."))?;
println!("parent block stdlib_version {}", stdlib_verson);
Ok(())
}
19 changes: 18 additions & 1 deletion cmd/db-exporter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bcs_ext::Sample;
use clap::IntoApp;
use clap::Parser;
use csv::Writer;
use db_exporter::verify_modules_via_export_file;
use db_exporter::{barnard_hard_fork_info, verify_modules_via_export_file};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use serde::ser::SerializeMap;
use serde::{Serialize, Serializer};
Expand Down Expand Up @@ -233,6 +233,7 @@ enum Cmd {
ApplySnapshot(ApplySnapshotOptions),
ExportResource(ExportResourceOptions),
VerifyModules(VerifyModulesOptions),
BarnardHardForkInfo(BarnardHardForkInfoOptions),
}

#[derive(Debug, Clone, Parser)]
Expand Down Expand Up @@ -425,6 +426,17 @@ pub struct VerifyModulesOptions {
pub input_path: PathBuf,
}

#[derive(Debug, Parser)]
#[clap(
name = "barnard-hard-fork_info",
about = "get barnard first stdlib v12 block number"
)]
pub struct BarnardHardForkInfoOptions {
#[clap(long, short = 'i', parse(from_os_str))]
/// starcoin node db path. like ~/.starcoin/barnard
pub db_path: PathBuf,
}

#[tokio::main(flavor = "multi_thread")]
async fn main() -> anyhow::Result<()> {
let opt = Opt::parse();
Expand Down Expand Up @@ -576,7 +588,12 @@ async fn main() -> anyhow::Result<()> {
let result = verify_modules_via_export_file(option.input_path);
return result;
}
Cmd::BarnardHardForkInfo(option) => {
let result = barnard_hard_fork_info(option.db_path);
return result;
}
}

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion kube/manifest/starcoin-barnard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ spec:
starcoin/node-pool: seed-pool
containers:
- name: starcoin
image: starcoin/starcoin:v1.12.9
image: starcoin/starcoin:barnard_rollback_block
imagePullPolicy: Always
command:
- bash
Expand Down
2 changes: 1 addition & 1 deletion kube/manifest/starcoin-halley.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ spec:
starcoin/node-pool: seed-pool
containers:
- name: starcoin
image: starcoin/starcoin:master
image: starcoin/starcoin:halley_rollback_block
imagePullPolicy: Always
command:
- bash
Expand Down
1 change: 1 addition & 0 deletions node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ impl NodeService {

let start_time = SystemTime::now();
storage_instance.check_upgrade()?;
storage_instance.barnard_hard_fork(config.clone())?;
let upgrade_time = SystemTime::now().duration_since(start_time)?;
let storage = Arc::new(Storage::new(storage_instance)?);
registry.put_shared(storage.clone()).await?;
Expand Down
18 changes: 17 additions & 1 deletion storage/src/chain_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::storage::{ColumnFamily, InnerStorage, KVStore};
use crate::{StorageVersion, CHAIN_INFO_PREFIX_NAME};
use anyhow::Result;
use starcoin_crypto::HashValue;
use starcoin_types::startup_info::{SnapshotRange, StartupInfo};
use starcoin_types::startup_info::{BarnardHardFork, SnapshotRange, StartupInfo};
use std::convert::{TryFrom, TryInto};

#[derive(Clone)]
Expand All @@ -27,6 +27,7 @@ impl ChainInfoStorage {
const GENESIS_KEY: &'static str = "genesis";
const STORAGE_VERSION_KEY: &'static str = "storage_version";
const SNAPSHOT_RANGE_KEY: &'static str = "snapshot_height";
const BARNARD_HARD_FORK: &'static str = "barnard_hard_fork";

pub fn get_startup_info(&self) -> Result<Option<StartupInfo>> {
self.get(Self::STARTUP_INFO_KEY.as_bytes())
Expand Down Expand Up @@ -95,4 +96,19 @@ impl ChainInfoStorage {
snapshot_range.try_into()?,
)
}

pub fn get_barnard_hard_fork(&self) -> Result<Option<BarnardHardFork>> {
self.get(Self::BARNARD_HARD_FORK.as_bytes())
.and_then(|bytes| match bytes {
Some(bytes) => Ok(Some(bytes.try_into()?)),
None => Ok(None),
})
}

pub fn save_barnard_hard_fork(&self, barnard_hard_fork: BarnardHardFork) -> Result<()> {
self.put_sync(
Self::BARNARD_HARD_FORK.as_bytes().to_vec(),
barnard_hard_fork.try_into()?,
)
}
}
2 changes: 2 additions & 0 deletions storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use starcoin_types::{
use std::collections::BTreeMap;
use std::fmt::{Debug, Display, Formatter};
use std::sync::Arc;
pub use upgrade::BARNARD_HARD_FORK_HASH;
pub use upgrade::BARNARD_HARD_FORK_HEIGHT;

pub mod accumulator;
pub mod batch;
Expand Down
9 changes: 9 additions & 0 deletions storage/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::db_storage::{DBStorage, SchemaIterator};
use crate::upgrade::DBUpgrade;
use anyhow::{bail, format_err, Result};
use byteorder::{BigEndian, ReadBytesExt};
use starcoin_config::NodeConfig;
use starcoin_crypto::HashValue;
use std::convert::TryInto;
use std::fmt::Debug;
Expand Down Expand Up @@ -107,6 +108,14 @@ impl StorageInstance {
pub fn check_upgrade(&mut self) -> Result<()> {
DBUpgrade::check_upgrade(self)
}

pub fn barnard_hard_fork(&mut self, config: Arc<NodeConfig>) -> Result<()> {
if config.net().id().chain_id().is_barnard() {
println!("Is Barnard in if");
return DBUpgrade::barnard_hard_fork(self);
}
Ok(())
}
}

impl InnerStore for StorageInstance {
Expand Down
35 changes: 35 additions & 0 deletions storage/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,24 @@ use crate::{
BLOCK_BODY_PREFIX_NAME, TRANSACTION_INFO_PREFIX_NAME,
};
use anyhow::{bail, ensure, format_err, Result};
use once_cell::sync::Lazy;
use starcoin_crypto::HashValue;
use starcoin_logger::prelude::{debug, info, warn};
use starcoin_types::block::BlockNumber;
use starcoin_types::startup_info::{BarnardHardFork, StartupInfo};
use starcoin_types::transaction::Transaction;
use std::cmp::Ordering;

pub struct DBUpgrade;

pub static BARNARD_HARD_FORK_HEIGHT: BlockNumber = 9716980;
pub static BARNARD_HARD_FORK_HASH: Lazy<HashValue> = Lazy::new(|| {
HashValue::from_hex_literal(
"0x8b960afd0ebd00109b54253a8d6985e8e219f38bfa7d9b9196ef858b5d09ddb8",
)
.expect("")
});
//0xab8d4b4faeb27b4e4912bf4341ff8dbe5fb467f4909e2ba333f02eb908245d38
impl DBUpgrade {
pub fn check_upgrade(instance: &mut StorageInstance) -> Result<()> {
let version_in_db = {
Expand Down Expand Up @@ -181,4 +193,27 @@ impl DBUpgrade {
}
Ok(())
}

pub fn barnard_hard_fork(instance: &mut StorageInstance) -> Result<()> {
let block_storage = BlockStorage::new(instance.clone());
let chain_info_storage = ChainInfoStorage::new(instance.clone());
let barnard_hard_fork = chain_info_storage.get_barnard_hard_fork()?;

let barnard_info = BarnardHardFork::new(BARNARD_HARD_FORK_HEIGHT, *BARNARD_HARD_FORK_HASH);
if barnard_hard_fork == Some(barnard_info.clone()) {
println!("is have forked");
return Ok(());
}

let block = block_storage.get_block_by_hash(*BARNARD_HARD_FORK_HASH)?;
if let Some(block) = block {
if block.header().number() == BARNARD_HARD_FORK_HEIGHT {
println!("barnard hard fork");
let main_hash = block.header().parent_hash();
chain_info_storage.save_barnard_hard_fork(barnard_info)?;
chain_info_storage.save_startup_info(StartupInfo::new(main_hash))?;
}
}
Ok(())
}
}
6 changes: 6 additions & 0 deletions sync/src/block_connector/write_block_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ where

fn connect_inner(&mut self, block: Block) -> Result<ConnectOk> {
let block_id = block.id();
if block_id == *starcoin_storage::BARNARD_HARD_FORK_HASH
&& block.header().number() == starcoin_storage::BARNARD_HARD_FORK_HEIGHT
{
debug!("barnard hard fork {}", block_id);
return Err(ConnectBlockError::BarnardHardFork(Box::new(block)).into());
}
if self.main.current_header().id() == block_id {
debug!("Repeat connect, current header is {} already.", block_id);
return Ok(ConnectOk::Duplicate);
Expand Down
Loading

0 comments on commit 39eb5d0

Please sign in to comment.