Skip to content

Commit

Permalink
fix starcoin-genesis tests
Browse files Browse the repository at this point in the history
1. add compatible data structures for storage, such as
   CompatBlockHeader, CompatBlock, CompatGenesis
2. update get/put-related functions
3. regenrate builtin genesis-files
4. silence some warnings
  • Loading branch information
simonjiao committed Nov 23, 2023
1 parent e34a0fe commit c038713
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 98 deletions.
3 changes: 2 additions & 1 deletion chain/service/src/chain_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ impl ChainReaderServiceInner {
Ok(())
}

#[allow(dead_code)]
pub fn update_dag_accumulator(&mut self, new_block_header: BlockHeader) -> Result<()> {
async_std::task::block_on(self.flexidag_service.send(UpdateDagTips {
block_header: new_block_header,
Expand Down Expand Up @@ -526,7 +527,7 @@ impl ReadableChainService for ChainReaderServiceInner {
accumulator_root: detail.accumulator_root,
tips: detail.tips,
head_block_id: detail.accumulator_root,
k_total_difficulties: todo!(),
k_total_difficulties: detail.k_total_difficulties,
})
.collect())
}
Expand Down
11 changes: 7 additions & 4 deletions cmd/db-exporter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use starcoin_storage::{
use starcoin_transaction_builder::{
build_signed_empty_txn, create_signed_txn_with_association_account, DEFAULT_MAX_GAS_AMOUNT,
};
use starcoin_types::block::{CompatBlock, CompatBlockHeader};
use starcoin_types::{
account::{peer_to_peer_txn, Account, DEFAULT_EXPIRATION_TIME},
account_address::AccountAddress,
Expand Down Expand Up @@ -168,12 +169,14 @@ impl DbSchema {
pub fn get_value_codec(&self) -> Box<dyn Fn(Vec<u8>) -> Result<serde_json::Value>> {
Box::new(match self {
DbSchema::Block => |arg| -> Result<serde_json::Value> {
Ok(serde_json::to_value(Block::decode_value(arg.as_slice())?)?)
Ok(serde_json::to_value(Block::from(
CompatBlock::decode_value(arg.as_slice())?,
))?)
},
DbSchema::BlockHeader => |arg| -> Result<serde_json::Value> {
Ok(serde_json::to_value(BlockHeader::decode_value(
arg.as_slice(),
)?)?)
Ok(serde_json::to_value(BlockHeader::from(
CompatBlockHeader::decode_value(arg.as_slice())?,
))?)
},
DbSchema::FailedBlock => |arg| -> Result<serde_json::Value> {
Ok(serde_json::to_value(FailedBlock::decode_value(
Expand Down
1 change: 0 additions & 1 deletion consensus/src/dag/blockdag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use starcoin_accumulator::node::AccumulatorStoreType;
use starcoin_accumulator::{Accumulator, MerkleAccumulator};
use starcoin_config::{NodeConfig, RocksdbConfig};
use starcoin_crypto::{HashValue as Hash, HashValue};
use starcoin_logger::prelude::info;
use starcoin_storage::flexi_dag::SyncFlexiDagSnapshotHasher;
use starcoin_storage::storage::CodecKVStore;
use starcoin_storage::Store;
Expand Down
Binary file modified genesis/generated/barnard/genesis
Binary file not shown.
Binary file modified genesis/generated/halley/genesis
Binary file not shown.
Binary file modified genesis/generated/main/genesis
Binary file not shown.
Binary file modified genesis/generated/proxima/genesis
Binary file not shown.
37 changes: 32 additions & 5 deletions genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use std::sync::Arc;
mod errors;
pub use errors::GenesisError;
use starcoin_storage::table_info::TableInfoStore;
use starcoin_types::block::CompatBlock;
use starcoin_vm_types::state_store::table::{TableHandle, TableInfo};
use starcoin_vm_types::state_view::StateView;

Expand All @@ -49,6 +50,27 @@ pub struct Genesis {
block: Block,
}

#[derive(Debug, Serialize, Deserialize)]
struct CompatGenesis {
block: CompatBlock,
}

impl From<CompatGenesis> for Genesis {
fn from(value: CompatGenesis) -> Self {
Self {
block: value.block.into(),
}
}
}

impl From<&Genesis> for CompatGenesis {
fn from(value: &Genesis) -> Self {
Self {
block: value.block.clone().into(),
}
}
}

impl Display for Genesis {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Genesis {{")?;
Expand Down Expand Up @@ -232,8 +254,8 @@ impl Genesis {
let mut genesis_file = File::open(genesis_file_path)?;
let mut content = vec![];
genesis_file.read_to_end(&mut content)?;
let genesis = bcs_ext::from_bytes(&content)?;
Ok(Some(genesis))
let compat_genesis: CompatGenesis = bcs_ext::from_bytes(&content)?;
Ok(Some(compat_genesis.into()))
}

fn genesis_bytes(net: BuiltinNetworkID) -> Option<&'static [u8]> {
Expand All @@ -245,7 +267,10 @@ impl Genesis {

pub fn load_generated(net: BuiltinNetworkID) -> Result<Option<Self>> {
match Self::genesis_bytes(net) {
Some(bytes) => Ok(Some(bcs_ext::from_bytes::<Genesis>(bytes)?)),
Some(bytes) => Ok(Some({
let data = bcs_ext::from_bytes::<CompatGenesis>(bytes)?;
data.into()
})),
None => Ok(None),
}
}
Expand Down Expand Up @@ -280,7 +305,8 @@ impl Genesis {
}
let genesis_file = data_dir.join(Self::GENESIS_FILE_NAME);
let mut file = File::create(genesis_file)?;
let contents = bcs_ext::to_bytes(self)?;
let compat_genesis: CompatGenesis = self.into();
let contents = bcs_ext::to_bytes(&compat_genesis)?;
file.write_all(&contents)?;
Ok(())
}
Expand Down Expand Up @@ -318,7 +344,7 @@ impl Genesis {
storage: Arc<Storage>,
data_dir: &Path,
) -> Result<(ChainInfo, Genesis)> {
debug!("load startup_info.");
debug!("load startup_info {:?} from {:?}.", net.id(), data_dir);
let (chain_info, genesis) = match storage.get_chain_info() {
Ok(Some(chain_info)) => {
debug!("Get chain info {:?} from db", chain_info);
Expand All @@ -344,6 +370,7 @@ impl Genesis {
(chain_info, genesis)
}
Ok(None) => {
debug!("fresh genesis {:?} at {:?}", net.id(), data_dir);
let genesis = Self::load_and_check_genesis(net, data_dir, true)?;
let chain_info = genesis.execute_genesis_block(net, storage.clone())?;
(chain_info, genesis)
Expand Down
25 changes: 3 additions & 22 deletions node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ use futures::executor::block_on;
use futures_timer::Delay;
use network_api::{PeerProvider, PeerSelector, PeerStrategy};
use starcoin_account_service::{AccountEventService, AccountService, AccountStorage};
use starcoin_accumulator::node::AccumulatorStoreType;
use starcoin_block_relayer::BlockRelayer;
use starcoin_chain_notify::ChainNotifyHandlerService;
use starcoin_chain_service::ChainReaderService;
use starcoin_config::NodeConfig;
use starcoin_consensus::{BlockDAG, FlexiDagStorage, FlexiDagStorageConfig};
use starcoin_crypto::HashValue;
use starcoin_consensus::BlockDAG;
use starcoin_flexidag::FlexidagService;
use starcoin_genesis::{Genesis, GenesisError};
use starcoin_logger::prelude::*;
Expand Down Expand Up @@ -47,7 +45,7 @@ use starcoin_storage::db_storage::DBStorage;
use starcoin_storage::errors::StorageInitError;
use starcoin_storage::metrics::StorageMetrics;
use starcoin_storage::storage::StorageInstance;
use starcoin_storage::{BlockStore, Storage, Store};
use starcoin_storage::{BlockStore, Storage};
use starcoin_stratum::service::{StratumService, StratumServiceFactory};
use starcoin_stratum::stratum::{Stratum, StratumFactory};
use starcoin_sync::announcement::AnnouncementService;
Expand Down Expand Up @@ -179,23 +177,6 @@ impl ServiceHandler<Self, NodeRequest> for NodeService {
.service_ref::<BlockConnectorService<TxPoolService>>()?
.clone();
let network = ctx.get_shared::<NetworkServiceRef>()?;
let dag = ctx
.get_shared::<Arc<BlockDAG>>()
.expect("ghost dag object does not exits");
let parents = match dag.get_parents(block_hash) {
Ok(parents) => {
if parents.is_empty() {
None
} else {
Some(parents)
}
}
Err(error) => {
error!("Get parents error: {:?}", error);
None
}
};
// let dag_transaction_parent = storage.get_accumulator_store(AccumulatorStoreType::Block).?;
let fut = async move {
info!("Prepare to re execute block {}", block_hash);
let block = match storage.get_block(block_hash)? {
Expand Down Expand Up @@ -361,7 +342,7 @@ impl NodeService {

let (chain_info, genesis) =
Genesis::init_and_check_storage(config.net(), storage.clone(), config.data_dir())?;

registry.register::<FlexidagService>().await?;

info!(
Expand Down
28 changes: 18 additions & 10 deletions storage/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use network_p2p_types::peer_id::PeerId;
use serde::{Deserialize, Serialize};
use starcoin_crypto::HashValue;
use starcoin_logger::prelude::*;
use starcoin_types::block::{Block, BlockBody, BlockHeader};
use starcoin_types::block::{Block, BlockBody, BlockHeader, CompatBlock, CompatBlockHeader};

#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct OldFailedBlock {
Expand Down Expand Up @@ -75,11 +75,11 @@ impl Sample for FailedBlock {
}
}

define_storage!(BlockInnerStorage, HashValue, Block, BLOCK_PREFIX_NAME);
define_storage!(BlockInnerStorage, HashValue, CompatBlock, BLOCK_PREFIX_NAME);
define_storage!(
BlockHeaderStorage,
HashValue,
BlockHeader,
CompatBlockHeader,
BLOCK_HEADER_PREFIX_NAME
);

Expand Down Expand Up @@ -126,7 +126,7 @@ pub struct BlockStorage {
failed_block_storage: FailedBlockStorage,
}

impl ValueCodec for Block {
impl ValueCodec for CompatBlock {
fn encode_value(&self) -> Result<Vec<u8>> {
self.encode()
}
Expand All @@ -136,7 +136,7 @@ impl ValueCodec for Block {
}
}

impl ValueCodec for BlockHeader {
impl ValueCodec for CompatBlockHeader {
fn encode_value(&self) -> Result<Vec<u8>> {
self.encode()
}
Expand Down Expand Up @@ -204,11 +204,12 @@ impl BlockStorage {
block.header().parent_hash()
);
let block_id = block.header().id();
self.block_store.put(block_id, block)
self.block_store.put(block_id, block.into())
}

pub fn save_header(&self, header: BlockHeader) -> Result<()> {
self.header_store.put(header.id(), header)
self.header_store
.put(header.id(), CompatBlockHeader::from(header))
}

pub fn get_headers(&self) -> Result<Vec<HashValue>> {
Expand All @@ -224,11 +225,17 @@ impl BlockStorage {
}

pub fn get(&self, block_id: HashValue) -> Result<Option<Block>> {
self.block_store.get(block_id)
let compat_block = self.block_store.get(block_id)?;
Ok(compat_block.map(|c| c.into()))
}

pub fn get_blocks(&self, ids: Vec<HashValue>) -> Result<Vec<Option<Block>>> {
Ok(self.block_store.multiple_get(ids)?.into_iter().collect())
Ok(self
.block_store
.multiple_get(ids)?
.into_iter()
.map(|cb| cb.map(|cb| cb.into()))
.collect())
}

pub fn get_body(&self, block_id: HashValue) -> Result<Option<BlockBody>> {
Expand All @@ -253,7 +260,8 @@ impl BlockStorage {
}

pub fn get_block_header_by_hash(&self, block_id: HashValue) -> Result<Option<BlockHeader>> {
self.header_store.get(block_id)
let compat_header = self.header_store.get(block_id)?;
Ok(compat_header.map(|c| c.into()))
}

pub fn get_block_tips_header_by_hash(
Expand Down
46 changes: 20 additions & 26 deletions storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
state_node::StateStorage,
storage::{CodecKVStore, CodecWriteBatch, ColumnFamilyName, StorageInstance},
};
//use crate::table_info::{TableInfoStorage, TableInfoStore};
use crate::{
transaction::TransactionStorage,
transaction_info::{TransactionInfoHashStorage, TransactionInfoStorage},
Expand All @@ -26,7 +25,6 @@ use starcoin_accumulator::{
};
use starcoin_config::ChainNetworkID;
use starcoin_crypto::HashValue;
use starcoin_logger::prelude::info;
use starcoin_state_store_api::{StateNode, StateNodeStore};
use starcoin_types::block::BlockNumber;
use starcoin_types::{
Expand All @@ -38,7 +36,7 @@ use starcoin_types::{
};
use starcoin_vm_types::{
account_address::AccountAddress,
state_store::table::{TableHandle, TableInfo}, account_config::key_rotation_capability,
state_store::table::{TableHandle, TableInfo},
};
use std::{
collections::{BTreeMap, BTreeSet},
Expand Down Expand Up @@ -454,24 +452,21 @@ impl BlockStore for Storage {
let head_block_info = self.get_block_info(head_block.id())?.ok_or_else(|| {
format_err!("Startup block info {:?} should exist", startup_info.main)
})?;
let (tips, dag_accumulator_info, k_total_difficulties) = self
.get_lastest_snapshot()?
.map_or_else(|| {
info!("the dag data is none, the chain will be still a single chain");
(None, None, None)
}, |snapshot| {
info!("the dag data exists, the chain will be still a dag chain");
let (tips_hash, flexi_dag_accumulator_info, k_total_difficulties) =
if let Some(snapshot) = self.get_lastest_snapshot()? {
(
Some(snapshot.child_hashes),
Some(snapshot.accumulator_info),
Some(snapshot.k_total_difficulties),
)
});
} else {
(None, None, None)
};
let chain_info = ChainInfo::new(
head_block.chain_id(),
genesis_hash,
ChainStatus::new(head_block, head_block_info, tips),
dag_accumulator_info,
ChainStatus::new(head_block, head_block_info, tips_hash),
flexi_dag_accumulator_info,
k_total_difficulties,
);
Ok(Some(chain_info))
Expand Down Expand Up @@ -686,19 +681,18 @@ impl SyncFlexiDagStore for Storage {
}

fn get_lastest_snapshot(&self) -> Result<Option<SyncFlexiDagSnapshot>> {
let info = match self
.get_dag_accumulator_info()? {
Some(info) => info,
None => return Ok(None),
};
let merkle_tree = MerkleAccumulator::new_with_info(
info,
self.get_accumulator_store(AccumulatorStoreType::SyncDag),
);
let key = merkle_tree
.get_leaf(merkle_tree.num_leaves() - 1)?
.ok_or_else(|| anyhow!("faile to get the key since it is none"))?;
self.query_by_hash(key)
if let Some(info) = self.get_dag_accumulator_info()? {
let merkle_tree = MerkleAccumulator::new_with_info(
info,
self.get_accumulator_store(AccumulatorStoreType::SyncDag),
);
let key = merkle_tree
.get_leaf(merkle_tree.num_leaves() - 1)?
.ok_or_else(|| anyhow!("failed to get the key since it is none"))?;
self.query_by_hash(key)
} else {
Ok(None)
}
}

fn get_dag_accumulator_info(&self) -> Result<Option<AccumulatorInfo>> {
Expand Down
5 changes: 3 additions & 2 deletions storage/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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::block::{BlockHeader, BlockNumber};
use starcoin_types::startup_info::{BarnardHardFork, StartupInfo};
use starcoin_types::transaction::Transaction;
use std::cmp::Ordering;
Expand Down Expand Up @@ -231,7 +231,8 @@ impl DBUpgrade {
let mut iter = block_storage.header_store.iter()?;
iter.seek_to_first();
for item in iter {
let (id, block_header) = item?;
let (id, compat_block_header) = item?;
let block_header: BlockHeader = compat_block_header.into();
if block_header.number() >= BARNARD_HARD_FORK_HEIGHT {
block_info_storage.remove(id)?;
processed_count += 1;
Expand Down
Loading

0 comments on commit c038713

Please sign in to comment.