Skip to content

Commit

Permalink
Added consensus parameters version and state transition version to th…
Browse files Browse the repository at this point in the history
…e `ApplicationHeader` (#1767)

It is a preparation of the public types for the next changes:

#1754
#1753
  • Loading branch information
xgreenx committed Mar 19, 2024
1 parent ac93cff commit 837ca27
Show file tree
Hide file tree
Showing 22 changed files with 589 additions and 229 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Description of the upcoming release here.

### Added

- [#1767](https://github.com/FuelLabs/fuel-core/pull/1767): Added consensus parameters version and state transition version to the `ApplicationHeader` to describe what was used to produce this block.
- [#1760](https://github.com/FuelLabs/fuel-core/pull/1760): Added tests to verify that the network operates with a custom chain id and base asset id.
- [#1752](https://github.com/FuelLabs/fuel-core/pull/1752): Add `ProducerGasPrice` trait that the `Producer` depends on to get the gas price for the block.
- [#1747](https://github.com/FuelLabs/fuel-core/pull/1747): The DA block height is now included in the genesis state.
Expand Down
8 changes: 8 additions & 0 deletions crates/client/assets/schema.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,14 @@ type Header {
"""
daHeight: U64!
"""
The version of the consensus parameters used to create this block.
"""
consensusParametersVersion: U32!
"""
The version of the state transition bytecode used to create this block.
"""
stateTransitionBytecodeVersion: U32!
"""
Number of transactions in this block.
"""
transactionsCount: U64!
Expand Down
2 changes: 2 additions & 0 deletions crates/client/src/client/schema/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ pub struct BlockMutation {
pub struct Header {
pub id: BlockId,
pub da_height: U64,
pub consensus_parameters_version: U32,
pub state_transition_bytecode_version: U32,
pub transactions_count: U64,
pub message_receipt_count: U64,
pub transactions_root: Bytes32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ query($height: U32) {
header {
id
daHeight
consensusParametersVersion
stateTransitionBytecodeVersion
transactionsCount
messageReceiptCount
transactionsRoot
Expand All @@ -34,5 +36,3 @@ query($height: U32) {
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ query($id: BlockId) {
header {
id
daHeight
consensusParametersVersion
stateTransitionBytecodeVersion
transactionsCount
messageReceiptCount
transactionsRoot
Expand All @@ -34,5 +36,3 @@ query($id: BlockId) {
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ query($after: String, $before: String, $first: Int, $last: Int) {
header {
id
daHeight
consensusParametersVersion
stateTransitionBytecodeVersion
transactionsCount
messageReceiptCount
transactionsRoot
Expand Down Expand Up @@ -45,5 +47,3 @@ query($after: String, $before: String, $first: Int, $last: Int) {
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ query {
header {
id
daHeight
consensusParametersVersion
stateTransitionBytecodeVersion
transactionsCount
messageReceiptCount
transactionsRoot
Expand Down
6 changes: 6 additions & 0 deletions crates/client/src/client/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ impl Block {
pub struct Header {
pub id: BlockId,
pub da_height: u64,
pub consensus_parameters_version: u32,
pub state_transition_bytecode_version: u32,
pub transactions_count: u64,
pub message_receipt_count: u64,
pub transactions_root: MerkleRoot,
Expand Down Expand Up @@ -68,6 +70,10 @@ impl From<schema::block::Header> for Header {
Self {
id: value.id.into(),
da_height: value.da_height.into(),
consensus_parameters_version: value.consensus_parameters_version.into(),
state_transition_bytecode_version: value
.state_transition_bytecode_version
.into(),
transactions_count: value.transactions_count.into(),
message_receipt_count: value.message_receipt_count.into(),
transactions_root: value.transactions_root.into(),
Expand Down
4 changes: 4 additions & 0 deletions crates/fuel-core/src/query/message/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ async fn can_build_message_proof() {
let commit_block_header = PartialBlockHeader {
application: ApplicationHeader {
da_height: 0u64.into(),
consensus_parameters_version: Default::default(),
state_transition_bytecode_version: Default::default(),
generated: Default::default(),
},
consensus: ConsensusHeader {
Expand All @@ -144,6 +146,8 @@ async fn can_build_message_proof() {
let message_block_header = PartialBlockHeader {
application: ApplicationHeader {
da_height: 0u64.into(),
consensus_parameters_version: Default::default(),
state_transition_bytecode_version: Default::default(),
generated: Default::default(),
},
consensus: ConsensusHeader {
Expand Down
10 changes: 10 additions & 0 deletions crates/fuel-core/src/schema/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ impl Header {
self.0.da_height.0.into()
}

/// The version of the consensus parameters used to create this block.
async fn consensus_parameters_version(&self) -> U32 {
self.0.consensus_parameters_version.into()
}

/// The version of the state transition bytecode used to create this block.
async fn state_transition_bytecode_version(&self) -> U32 {
self.0.state_transition_bytecode_version.into()
}

/// Number of transactions in this block.
async fn transactions_count(&self) -> U64 {
self.0.transactions_count.into()
Expand Down
36 changes: 35 additions & 1 deletion crates/fuel-core/src/service/adapters/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,27 @@ use fuel_core_producer::{
ports::TxPool,
};
use fuel_core_storage::{
iter::{
IterDirection,
IteratorOverTable,
},
not_found,
tables::FuelBlocks,
tables::{
ConsensusParametersVersions,
FuelBlocks,
StateTransitionBytecodeVersions,
},
transactional::Changes,
Result as StorageResult,
StorageAsRef,
};
use fuel_core_types::{
blockchain::{
block::CompressedBlock,
header::{
ConsensusParametersVersion,
StateTransitionBytecodeVersion,
},
primitives,
},
fuel_tx,
Expand Down Expand Up @@ -146,6 +158,28 @@ impl fuel_core_producer::ports::BlockProducerDatabase for Database {
fn block_header_merkle_root(&self, height: &BlockHeight) -> StorageResult<Bytes32> {
self.storage::<FuelBlocks>().root(height).map(Into::into)
}

fn latest_consensus_parameters_version(
&self,
) -> StorageResult<ConsensusParametersVersion> {
let (version, _) = self
.iter_all::<ConsensusParametersVersions>(Some(IterDirection::Reverse))
.next()
.ok_or(not_found!(ConsensusParametersVersions))??;

Ok(version)
}

fn latest_state_transition_bytecode_version(
&self,
) -> StorageResult<StateTransitionBytecodeVersion> {
let (version, _) = self
.iter_all::<StateTransitionBytecodeVersions>(Some(IterDirection::Reverse))
.next()
.ok_or(not_found!(StateTransitionBytecodeVersions))??;

Ok(version)
}
}

impl GasPriceProvider for StaticGasPrice {
Expand Down
26 changes: 26 additions & 0 deletions crates/fuel-core/src/service/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ use fuel_core_chain_config::{
use fuel_core_storage::{
tables::{
Coins,
ConsensusParametersVersions,
ContractsInfo,
ContractsLatestUtxo,
ContractsRawCode,
Messages,
StateTransitionBytecodeVersions,
},
transactional::{
Changes,
Expand All @@ -38,7 +40,9 @@ use fuel_core_types::{
header::{
ApplicationHeader,
ConsensusHeader,
ConsensusParametersVersion,
PartialBlockHeader,
StateTransitionBytecodeVersion,
},
primitives::{
DaBlockHeight,
Expand Down Expand Up @@ -87,6 +91,8 @@ pub async fn execute_genesis_block(
import_chain_state(workers).await?;

let genesis = Genesis {
// TODO: We can get the serialized consensus parameters from the database.
// https://github.com/FuelLabs/fuel-core/issues/1570
chain_config_hash: config.chain_config.root()?.into(),
coins_root: original_database.genesis_coins_root()?.into(),
messages_root: original_database.genesis_messages_root()?.into(),
Expand All @@ -101,6 +107,20 @@ pub async fn execute_genesis_block(
};

let mut database_transaction = original_database.read_transaction();
// TODO: The chain config should be part of the snapshot state.
// https://github.com/FuelLabs/fuel-core/issues/1570
database_transaction
.storage_as_mut::<ConsensusParametersVersions>()
.insert(
&ConsensusParametersVersion::MIN,
&config.chain_config.consensus_parameters,
)?;
// TODO: The bytecode of the state transition function should be part of the snapshot state.
// https://github.com/FuelLabs/fuel-core/issues/1570
database_transaction
.storage_as_mut::<StateTransitionBytecodeVersions>()
.insert(&ConsensusParametersVersion::MIN, &[])?;

cleanup_genesis_progress(&mut database_transaction)?;

let result = UncommittedImportResult::new(
Expand Down Expand Up @@ -146,6 +166,12 @@ pub fn create_genesis_block(config: &Config) -> Block {
PartialBlockHeader {
application: ApplicationHeader::<Empty> {
da_height: da_block_height,
// After regenesis, we don't need to support old consensus parameters,
// so we can start the versioning from the beginning.
consensus_parameters_version: ConsensusParametersVersion::MIN,
// After regenesis, we don't need to support old state transition functions,
// so we can start the versioning from the beginning.
state_transition_bytecode_version: StateTransitionBytecodeVersion::MIN,
generated: Empty,
},
consensus: ConsensusHeader::<Empty> {
Expand Down
10 changes: 8 additions & 2 deletions crates/services/producer/src/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,17 @@ where
height: BlockHeight,
block_time: Tai64,
) -> anyhow::Result<PartialBlockHeader> {
let previous_block_info = self.previous_block_info(height)?;
let view = self.view_provider.latest_view();
let previous_block_info = self.previous_block_info(height, &view)?;
let consensus_parameters_version = view.latest_consensus_parameters_version()?;
let state_transition_bytecode_version =
view.latest_state_transition_bytecode_version()?;

Ok(PartialBlockHeader {
application: ApplicationHeader {
da_height: previous_block_info.da_height,
consensus_parameters_version,
state_transition_bytecode_version,
generated: Default::default(),
},
consensus: ConsensusHeader {
Expand All @@ -319,6 +325,7 @@ where
fn previous_block_info(
&self,
height: BlockHeight,
view: &ViewProvider::View,
) -> anyhow::Result<PreviousBlockInfo> {
let latest_height = self
.view_provider
Expand All @@ -332,7 +339,6 @@ where
}
.into())
} else {
let view = self.view_provider.latest_view();
// get info from previous block height
let prev_height = height.pred().expect("We checked the height above");
let previous_block = view.get_block(&prev_height)?;
Expand Down
Loading

0 comments on commit 837ca27

Please sign in to comment.