Skip to content

Commit

Permalink
Prune unsupported feature (#4124)
Browse files Browse the repository at this point in the history
* remove LegacyCompactBlock

* remove legacy block verifing

* ignore test_db_upgrade
  • Loading branch information
simonjiao authored May 30, 2024
1 parent 22bdfb9 commit 561bf86
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 176 deletions.
10 changes: 2 additions & 8 deletions chain/src/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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(),
Expand Down
49 changes: 5 additions & 44 deletions network/api/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,43 +50,13 @@ 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<LegacyCompactBlockMessage> for CompactBlockMessage {
fn from(value: LegacyCompactBlockMessage) -> Self {
Self {
compact_block: value.compact_block.into(),
block_info: value.block_info,
}
}
}

impl From<CompactBlockMessage> 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 {
compact_block,
block_info,
}
}

pub fn is_legacy(&self) -> bool {
self.compact_block.header.is_legacy()
}
}

impl Sample for CompactBlockMessage {
Expand Down Expand Up @@ -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)?)
}
Expand All @@ -179,15 +148,7 @@ impl NotificationMessage {
pub fn encode_notification(&self) -> Result<(Cow<'static, str>, Vec<u8>)> {
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::<LegacyCompactBlockMessage>::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()?)
}
Expand Down
1 change: 1 addition & 0 deletions storage/src/tests/test_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ fn generate_old_db(path: &Path) -> Result<(Vec<HashValue>, Vec<HashValue>, Vec<H
}

#[stest::test]
#[ignore]
pub fn test_db_upgrade() -> Result<()> {
let tmpdir = starcoin_config::temp_dir();
let (txn_info_ids, block_ids, failed_block_ids) = generate_old_db(tmpdir.path())?;
Expand Down
45 changes: 14 additions & 31 deletions types/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
94 changes: 1 addition & 93 deletions types/src/compact_block.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -12,41 +12,6 @@ pub struct CompactBlock {
pub uncles: Option<Vec<BlockHeader>>,
}

#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename = "CompactBlock")]
pub struct LegacyCompactBlock {
pub header: LegacyBlockHeader,
pub short_ids: Vec<ShortId>,
pub prefilled_txn: Vec<PrefilledTxn>,
pub uncles: Option<Vec<LegacyBlockHeader>>,
}

impl From<LegacyCompactBlock> 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<CompactBlock> 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,
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 561bf86

Please sign in to comment.