From 561bf8674937c2a547f9ee1ade1b21302b0db770 Mon Sep 17 00:00:00 2001 From: simonjiao Date: Thu, 30 May 2024 18:38:19 +0800 Subject: [PATCH] Prune unsupported feature (#4124) * remove LegacyCompactBlock * remove legacy block verifing * ignore test_db_upgrade --- chain/src/verifier/mod.rs | 10 +--- network/api/src/messages.rs | 49 ++-------------- storage/src/tests/test_storage.rs | 1 + types/src/block/mod.rs | 45 +++++---------- types/src/compact_block.rs | 94 +------------------------------ 5 files changed, 23 insertions(+), 176 deletions(-) diff --git a/chain/src/verifier/mod.rs b/chain/src/verifier/mod.rs index 24fb893930..7f6ec744cb 100644 --- a/chain/src/verifier/mod.rs +++ b/chain/src/verifier/mod.rs @@ -9,9 +9,7 @@ use starcoin_chain_api::{ use starcoin_consensus::{Consensus, ConsensusVerifyError}; use starcoin_logger::prelude::debug; use starcoin_open_block::AddressFilter; -use starcoin_types::block::{ - Block, BlockHeader, DagHeaderType, LegacyBlockBody, ALLOWED_FUTURE_BLOCKTIME, -}; +use starcoin_types::block::{Block, BlockHeader, DagHeaderType, ALLOWED_FUTURE_BLOCKTIME}; use std::{collections::HashSet, str::FromStr}; #[derive(Debug, Clone)] @@ -47,11 +45,7 @@ pub struct StaticVerifier; impl StaticVerifier { pub fn verify_body_hash(block: &Block) -> Result<()> { // verify body - let body_hash = if block.header().is_legacy() { - LegacyBlockBody::from(block.body.clone()).hash() - } else { - block.body.hash() - }; + let body_hash = block.body.hash(); verify_block!( VerifyBlockField::Body, body_hash == block.header().body_hash(), diff --git a/network/api/src/messages.rs b/network/api/src/messages.rs index 8f3cded0ba..046fb58e77 100644 --- a/network/api/src/messages.rs +++ b/network/api/src/messages.rs @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize}; use starcoin_crypto::HashValue; use starcoin_service_registry::ServiceRequest; use starcoin_types::block::BlockInfo; -use starcoin_types::compact_block::{CompactBlock, LegacyCompactBlock}; +use starcoin_types::compact_block::CompactBlock; use starcoin_types::startup_info::ChainInfo; use starcoin_types::transaction::SignedUserTransaction; use std::borrow::Cow; @@ -50,32 +50,6 @@ pub struct CompactBlockMessage { pub block_info: BlockInfo, } -/// The legacy Message of block notification exchanged over network -#[derive(Serialize, Deserialize)] -#[serde(rename = "CompactBlockMessage")] -pub struct LegacyCompactBlockMessage { - pub compact_block: LegacyCompactBlock, - pub block_info: BlockInfo, -} - -impl From for CompactBlockMessage { - fn from(value: LegacyCompactBlockMessage) -> Self { - Self { - compact_block: value.compact_block.into(), - block_info: value.block_info, - } - } -} - -impl From for LegacyCompactBlockMessage { - fn from(value: CompactBlockMessage) -> Self { - Self { - compact_block: value.compact_block.into(), - block_info: value.block_info, - } - } -} - impl CompactBlockMessage { pub fn new(compact_block: CompactBlock, block_info: BlockInfo) -> Self { Self { @@ -83,10 +57,6 @@ impl CompactBlockMessage { block_info, } } - - pub fn is_legacy(&self) -> bool { - self.compact_block.header.is_legacy() - } } impl Sample for CompactBlockMessage { @@ -161,10 +131,9 @@ impl NotificationMessage { TXN_PROTOCOL_NAME => { NotificationMessage::Transactions(TransactionsMessage::decode(bytes)?) } - BLOCK_PROTOCOL_NAME => NotificationMessage::CompactBlock(Box::new( - CompactBlockMessage::decode(bytes) - .or_else(|_| LegacyCompactBlockMessage::decode(bytes).map(Into::into))?, - )), + BLOCK_PROTOCOL_NAME => { + NotificationMessage::CompactBlock(Box::new(CompactBlockMessage::decode(bytes)?)) + } ANNOUNCEMENT_PROTOCOL_NAME => { NotificationMessage::Announcement(Announcement::decode(bytes)?) } @@ -179,15 +148,7 @@ impl NotificationMessage { pub fn encode_notification(&self) -> Result<(Cow<'static, str>, Vec)> { Ok(match self { NotificationMessage::Transactions(msg) => (TXN_PROTOCOL_NAME.into(), msg.encode()?), - NotificationMessage::CompactBlock(msg) => ( - BLOCK_PROTOCOL_NAME.into(), - if msg.is_legacy() { - let legacy = Into::::into(*msg.clone()); - legacy.encode() - } else { - msg.encode() - }?, - ), + NotificationMessage::CompactBlock(msg) => (BLOCK_PROTOCOL_NAME.into(), msg.encode()?), NotificationMessage::Announcement(msg) => { (ANNOUNCEMENT_PROTOCOL_NAME.into(), msg.encode()?) } diff --git a/storage/src/tests/test_storage.rs b/storage/src/tests/test_storage.rs index b8a19eecec..216d85d61b 100644 --- a/storage/src/tests/test_storage.rs +++ b/storage/src/tests/test_storage.rs @@ -395,6 +395,7 @@ fn generate_old_db(path: &Path) -> Result<(Vec, Vec, Vec Result<()> { let tmpdir = starcoin_config::temp_dir(); let (txn_info_ids, block_ids, failed_block_ids) = generate_old_db(tmpdir.path())?; diff --git a/types/src/block/mod.rs b/types/src/block/mod.rs index 66dc2f4660..1c3be3c234 100644 --- a/types/src/block/mod.rs +++ b/types/src/block/mod.rs @@ -358,10 +358,6 @@ impl BlockHeader { self.number == 0 } - pub fn is_legacy(&self) -> bool { - self.parents_hash.is_none() - } - pub fn is_single(&self) -> bool { self.parents_hash .as_ref() @@ -855,33 +851,20 @@ impl Block { .map(|uncles| uncles.len() as u64) .unwrap_or(0); - if !self.header.is_legacy() { - BlockMetadata::new_with_parents( - self.header.parent_hash(), - self.header.timestamp, - self.header.author, - self.header.author_auth_key, - uncles, - self.header.number, - self.header.chain_id, - parent_gas_used, - self.header - .parents_hash - .clone() - .expect("Parents must exist"), - ) - } else { - BlockMetadata::new( - self.header.parent_hash(), - self.header.timestamp, - self.header.author, - self.header.author_auth_key, - uncles, - self.header.number, - self.header.chain_id, - parent_gas_used, - ) - } + BlockMetadata::new_with_parents( + self.header.parent_hash(), + self.header.timestamp, + self.header.author, + self.header.author_auth_key, + uncles, + self.header.number, + self.header.chain_id, + parent_gas_used, + self.header + .parents_hash + .clone() + .expect("Parents must exist"), + ) } pub fn random() -> Self { diff --git a/types/src/compact_block.rs b/types/src/compact_block.rs index f5b01533d2..826b02aa5f 100644 --- a/types/src/compact_block.rs +++ b/types/src/compact_block.rs @@ -1,4 +1,4 @@ -use crate::block::{Block, BlockHeader, LegacyBlockHeader}; +use crate::block::{Block, BlockHeader}; use crate::transaction::SignedUserTransaction; use bcs_ext::Sample; use serde::{Deserialize, Serialize}; @@ -12,41 +12,6 @@ pub struct CompactBlock { pub uncles: Option>, } -#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)] -#[serde(rename = "CompactBlock")] -pub struct LegacyCompactBlock { - pub header: LegacyBlockHeader, - pub short_ids: Vec, - pub prefilled_txn: Vec, - pub uncles: Option>, -} - -impl From for CompactBlock { - fn from(value: LegacyCompactBlock) -> Self { - Self { - header: value.header.into(), - short_ids: value.short_ids, - prefilled_txn: value.prefilled_txn, - uncles: value - .uncles - .map(|u| u.into_iter().map(Into::into).collect()), - } - } -} - -impl From for LegacyCompactBlock { - fn from(value: CompactBlock) -> Self { - Self { - header: value.header.into(), - short_ids: value.short_ids, - prefilled_txn: value.prefilled_txn, - uncles: value - .uncles - .map(|u| u.into_iter().map(Into::into).collect()), - } - } -} - #[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)] pub struct PrefilledTxn { pub index: u64, @@ -92,60 +57,3 @@ impl Sample for CompactBlock { Block::sample().into() } } - -#[cfg(test)] -mod tests { - use super::{CompactBlock, LegacyCompactBlock, ShortId}; - use crate::block::BlockHeader; - use bcs_ext::BCSCodec; - use starcoin_crypto::HashValue; - - fn setup_data() -> (LegacyCompactBlock, CompactBlock) { - let header = BlockHeader::random(); - let uncles = vec![BlockHeader::random(), BlockHeader::random()]; - let short_ids = vec![ShortId(HashValue::random()), ShortId(HashValue::random())]; - let legacy = LegacyCompactBlock { - header: header.clone().into(), - short_ids: short_ids.clone(), - prefilled_txn: vec![], - uncles: Some(uncles.iter().cloned().map(Into::into).collect()), - }; - - let block = CompactBlock { - header, - short_ids, - prefilled_txn: vec![], - uncles: Some(uncles), - }; - (legacy, block) - } - - #[test] - fn test_compact_block_converting() { - let (legacy, block) = setup_data(); - - let converted_block: CompactBlock = legacy.clone().into(); - assert_eq!(block, converted_block); - - let converted_legacy: LegacyCompactBlock = block.into(); - assert_eq!(legacy, converted_legacy); - } - - #[test] - fn test_compact_block_encode_decode() { - let (legacy, block) = setup_data(); - - // legacy format -> upgraded format - let legacy_raw = legacy.encode().unwrap(); - let de_legacy = LegacyCompactBlock::decode(&legacy_raw).unwrap(); - assert_eq!(legacy, de_legacy); - assert!(CompactBlock::decode(&legacy_raw).is_err()); - let converted_block: CompactBlock = de_legacy.into(); - assert_eq!(block, converted_block); - - // upgraded format -> legacy format - let converted_legacy: LegacyCompactBlock = block.into(); - let converted_legacy_raw = converted_legacy.encode().unwrap(); - assert_eq!(legacy_raw, converted_legacy_raw); - } -}