Skip to content

Commit

Permalink
Fix not suit block since parent is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
sanlee42 committed May 10, 2024
1 parent 1fcd227 commit 1359516
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 39 deletions.
42 changes: 16 additions & 26 deletions chain/open-block/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,17 @@ impl OpenedBlock {
let chain_state =
ChainStateDB::new(storage.into_super_arc(), Some(previous_header.state_root()));
let chain_id = previous_header.chain_id();
let block_meta = if chain_id.is_proxima() {
BlockMetadata::new(
previous_block_id,
block_timestamp,
author,
None,
uncles.len() as u64,
previous_header.number() + 1,
chain_id,
previous_header.gas_used(),
)
} else {
BlockMetadata::new_with_parents(
previous_block_id,
block_timestamp,
author,
None,
uncles.len() as u64,
previous_header.number() + 1,
chain_id,
previous_header.gas_used(),
tips_hash.unwrap_or_default(),
)
};
let block_meta = BlockMetadata::new_with_parents(
previous_block_id,
block_timestamp,
author,
None,
uncles.len() as u64,
previous_header.number() + 1,
chain_id,
previous_header.gas_used(),
tips_hash.unwrap_or_default(),
);
let mut opened_block = Self {
previous_block_info: block_info,
block_meta,
Expand Down Expand Up @@ -222,10 +209,13 @@ impl OpenedBlock {
untouched_txns: untouched_user_txns,
})
}

/// Run blockmeta first
fn initialize(&mut self) -> Result<()> {
let block_metadata_txn = Transaction::BlockMetadata(self.block_meta.clone());
let block_metadata_txn = if self.chain_id.is_proxima() {
Transaction::BlockMetadata(self.block_meta.to_legacy())
} else {
Transaction::BlockMetadata(self.block_meta.clone())
};
let block_meta_txn_hash = block_metadata_txn.id();
let mut results = execute_transactions(
&self.state,
Expand Down
20 changes: 7 additions & 13 deletions vm/types/src/block_metadata/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::genesis_config::ChainId;
use crate::transaction::authenticator::AuthenticationKey;
use anyhow::anyhow;
use move_core_types::account_address::AccountAddress;
use serde::{Deserialize, Deserializer, Serialize};
use starcoin_crypto::hash::{CryptoHash, CryptoHasher, PlainCryptoHash};
Expand Down Expand Up @@ -78,17 +77,10 @@ impl From<BlockMetadata> for super::BlockMetadata {
}
}

impl TryFrom<super::BlockMetadata> for BlockMetadata {
type Error = anyhow::Error;

fn try_from(value: super::BlockMetadata) -> Result<Self, Self::Error> {
if value.parents_hash.is_some() {
return Err(anyhow!(
"Can't convert a new BlockMetaData txn with parents_hash to an old one"
));
}
Ok(Self {
id: value.id,
impl From<super::BlockMetadata> for BlockMetadata {
fn from(value: super::BlockMetadata) -> Self {
let mut meta = Self {
id: None,
parent_hash: value.parent_hash,
timestamp: value.timestamp,
author: value.author,
Expand All @@ -97,6 +89,8 @@ impl TryFrom<super::BlockMetadata> for BlockMetadata {
number: value.number,
chain_id: value.chain_id,
parent_gas_used: value.parent_gas_used,
})
};
meta.id = Some(meta.crypto_hash());
meta
}
}
4 changes: 4 additions & 0 deletions vm/types/src/block_metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ impl BlockMetadata {
pub fn author(&self) -> AccountAddress {
self.author
}
pub fn to_legacy(&self) -> Self {
let meta: legacy::BlockMetadata = self.clone().into();
meta.into()
}
}

impl<'de> Deserialize<'de> for BlockMetadata {
Expand Down

0 comments on commit 1359516

Please sign in to comment.