Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test/support block version test #1852

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/client/src/client/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ impl TryFrom<schema::block::Block> for Block {
block_producer,
})
}
2 => Ok(Self {
id: BlockId::from([123u8; 32]),
header: value.header.try_into()?,
consensus: Consensus::Unknown,
transactions: vec![],
block_producer: None,
}),
_ => Err(ConversionError::UnknownVariant("BlockVersion")),
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/fuel-core/src/schema/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ impl Block {
async fn version(&self) -> Version {
match self.0 {
CompressedBlock::V1(_) => Version(1),
CompressedBlock::V2(_) => Version(2),
_ => Version(255),
}
}

Expand Down
82 changes: 82 additions & 0 deletions crates/types/src/blockchain/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ use crate::{
pub enum Block<TransactionRepresentation = Transaction> {
/// V1 Block
V1(BlockV1<TransactionRepresentation>),
/// V2 Block
V2((BlockHeader, Vec<TransactionRepresentation>)),
/// VUnknown
VUnknown(BlockV1<TransactionRepresentation>),
}

#[cfg(any(test, feature = "test-helpers"))]
Expand Down Expand Up @@ -125,6 +129,22 @@ impl Block<Transaction> {
};
Block::V1(new_inner)
}
Block::V2((header, txs)) => {
let txs = txs.iter().map(|tx| tx.id(chain_id)).collect();
Block::V2((header.clone(), txs))
}
Block::VUnknown(inner) => {
let transactions = inner
.transactions
.iter()
.map(|tx| tx.id(chain_id))
.collect();
let new_inner = BlockV1 {
header: inner.header.clone(),
transactions,
};
Block::VUnknown(new_inner)
}
}
}
}
Expand All @@ -137,6 +157,11 @@ impl<T> Block<T> {
header,
transactions,
}) => (header, transactions),
Block::V2((header, txs)) => (header, txs),
Block::VUnknown(BlockV1 {
header,
transactions,
}) => (header, transactions),
}
}
}
Expand All @@ -151,6 +176,11 @@ impl CompressedBlock {
header: inner.header,
transactions,
}),
Block::V2((header, _txs)) => Block::V2((header, transactions)),
Block::VUnknown(inner) => Block::VUnknown(BlockV1 {
header: inner.header,
transactions,
}),
}
}
}
Expand All @@ -171,13 +201,17 @@ impl<TransactionRepresentation> Block<TransactionRepresentation> {
pub fn transactions(&self) -> &[TransactionRepresentation] {
match self {
Block::V1(inner) => &inner.transactions,
Block::V2(_) => &[],
Block::VUnknown(inner) => &inner.transactions,
}
}

/// Get the complete header.
pub fn header(&self) -> &BlockHeader {
match self {
Block::V1(inner) => &inner.header,
Block::V2((header, _)) => header,
Block::VUnknown(inner) => &inner.header,
}
}

Expand All @@ -191,6 +225,8 @@ impl<TransactionRepresentation> Block<TransactionRepresentation> {
pub fn transactions_mut(&mut self) -> &mut Vec<TransactionRepresentation> {
match self {
Block::V1(inner) => &mut inner.transactions,
Block::V2((_, txs)) => txs,
Block::VUnknown(inner) => &mut inner.transactions,
}
}

Expand All @@ -199,6 +235,8 @@ impl<TransactionRepresentation> Block<TransactionRepresentation> {
pub fn header_mut(&mut self) -> &mut BlockHeader {
match self {
Block::V1(inner) => &mut inner.header,
Block::V2((header, _)) => header,
Block::VUnknown(inner) => &mut inner.header,
}
}
}
Expand Down Expand Up @@ -273,6 +311,50 @@ impl From<Block> for PartialFuelBlock {
},
transactions,
},
Block::V2((_header, txs)) => Self {
header: PartialBlockHeader {
application: Default::default(),
consensus: Default::default(),
},
transactions: txs,
},
Block::VUnknown(BlockV1 {
header:
BlockHeader::V1(BlockHeaderV1 {
application:
ApplicationHeader {
da_height,
consensus_parameters_version,
state_transition_bytecode_version,
..
},
consensus:
ConsensusHeader {
prev_root,
height,
time,
..
},
..
}),
transactions,
}) => Self {
header: PartialBlockHeader {
application: ApplicationHeader {
da_height,
consensus_parameters_version,
state_transition_bytecode_version,
generated: Empty {},
},
consensus: ConsensusHeader {
prev_root,
height,
time,
generated: Empty {},
},
},
transactions,
},
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions crates/types/src/blockchain/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ pub type StateTransitionBytecodeVersion = u32;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(any(test, feature = "test-helpers"), derive(Default))]
// #[cfg_attr(any(test, feature = "test-helpers"), derive(Default))]
#[derive(Default)]
/// The fuel block application header.
/// Contains everything except consensus related data.
pub struct ApplicationHeader<Generated> {
Expand All @@ -185,7 +186,8 @@ pub struct ApplicationHeader<Generated> {

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(any(test, feature = "test-helpers"), derive(Default))]
// #[cfg_attr(any(test, feature = "test-helpers"), derive(Default))]
#[derive(Default)]
/// Concrete generated application header fields.
/// These are generated once the full block has been run.
pub struct GeneratedApplicationFields {
Expand Down Expand Up @@ -219,7 +221,8 @@ pub struct ConsensusHeader<Generated> {

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(any(test, feature = "test-helpers"), derive(Default))]
// #[cfg_attr(any(test, feature = "test-helpers"), derive(Default))]
#[derive(Default)]
/// Concrete generated consensus header fields.
/// These are generated once the full block has been run.
pub struct GeneratedConsensusFields {
Expand All @@ -235,7 +238,7 @@ pub struct BlockHeaderMetadata {
id: BlockId,
}

#[cfg(any(test, feature = "test-helpers"))]
// #[cfg(any(test, feature = "test-helpers"))]
impl Default for BlockHeader {
fn default() -> Self {
let mut default: BlockHeader = BlockHeaderV1 {
Expand Down Expand Up @@ -459,7 +462,7 @@ impl ConsensusHeader<GeneratedConsensusFields> {
}
}

#[cfg(any(test, feature = "test-helpers"))]
// #[cfg(any(test, feature = "test-helpers"))]
impl<T> Default for ConsensusHeader<T>
where
T: Default,
Expand Down
69 changes: 67 additions & 2 deletions tests/tests/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use fuel_core_client::client::{
PageDirection,
PaginationRequest,
},
types::TransactionStatus,
types::{
primitives::BlockId,
TransactionStatus,
},
FuelClient,
};
use fuel_core_poa::Trigger;
Expand All @@ -29,8 +32,12 @@ use fuel_core_storage::{
};
use fuel_core_types::{
blockchain::{
block::CompressedBlock,
block::{
BlockV1,
CompressedBlock,
},
consensus::Consensus,
header::BlockHeader,
},
fuel_tx::*,
secrecy::ExposeSecret,
Expand Down Expand Up @@ -109,6 +116,64 @@ async fn block_by_height_returns_genesis_block() {
));
}

#[tokio::test]
async fn block_by_height_returns_block_with_expected_values() {
let mut block = CompressedBlock::V2((BlockHeader::default(), vec![]));
let height = 1.into();
block.header_mut().set_block_height(height);
let mut db = Database::default();
let srv = FuelService::from_database(db.clone(), Config::local_node())
.await
.unwrap();
let client = FuelClient::from(srv.bound_address);

let mut transaction = db.write_transaction();
transaction
.storage::<FuelBlocks>()
.insert(&height, &block)
.unwrap();
transaction
.storage::<SealedBlockConsensus>()
.insert(&height, &Consensus::PoA(Default::default()))
.unwrap();
transaction.commit().unwrap();

let b = client
.block_by_height(height)
.await
.expect("Unable to get block")
.expect("Expected block");
assert_eq!(b.id, BlockId::from([123u8; 32]));
}

#[tokio::test]
async fn block_by_height_returns_err_unknown_version() {
let mut block = CompressedBlock::VUnknown(BlockV1::default());
let height = 1.into();
block.header_mut().set_block_height(height);
let mut db = Database::default();
let srv = FuelService::from_database(db.clone(), Config::local_node())
.await
.unwrap();
let client = FuelClient::from(srv.bound_address);

let mut transaction = db.write_transaction();
transaction
.storage::<FuelBlocks>()
.insert(&height, &block)
.unwrap();
transaction
.storage::<SealedBlockConsensus>()
.insert(&height, &Consensus::PoA(Default::default()))
.unwrap();
transaction.commit().unwrap();

client
.block_by_height(height)
.await
.expect_err("Block version expected to be invalid");
}

#[tokio::test]
async fn produce_block() {
let config = Config::local_node();
Expand Down
Loading