Skip to content

Commit 2f2218f

Browse files
authored
refactor: include Starknet version in BlockEnv (#161)
1 parent c74cd72 commit 2f2218f

File tree

16 files changed

+65
-55
lines changed

16 files changed

+65
-55
lines changed

crates/chain-spec/src/dev.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl ChainSpec {
4848
pub fn block(&self) -> Block {
4949
let header = Header {
5050
state_diff_length: 0,
51-
protocol_version: CURRENT_STARKNET_VERSION,
51+
starknet_version: CURRENT_STARKNET_VERSION,
5252
number: self.genesis.number,
5353
timestamp: self.genesis.timestamp,
5454
events_count: 0,
@@ -369,7 +369,7 @@ mod tests {
369369
l1_gas_prices: chain_spec.genesis.gas_prices.clone(),
370370
l1_data_gas_prices: chain_spec.genesis.gas_prices.clone(),
371371
l1_da_mode: L1DataAvailabilityMode::Calldata,
372-
protocol_version: CURRENT_STARKNET_VERSION,
372+
starknet_version: CURRENT_STARKNET_VERSION,
373373
transaction_count: 0,
374374
events_count: 0,
375375
},

crates/chain-spec/src/rollup/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub struct ChainSpec {
3838
impl ChainSpec {
3939
pub fn block(&self) -> ExecutableBlock {
4040
let header = PartialHeader {
41-
protocol_version: CURRENT_STARKNET_VERSION,
41+
starknet_version: CURRENT_STARKNET_VERSION,
4242
number: self.genesis.number,
4343
timestamp: self.genesis.timestamp,
4444
parent_hash: self.genesis.parent_hash,

crates/core/src/backend/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<EF: ExecutorFactory> Backend<EF> {
106106
let partial_header = PartialHeader {
107107
number: block_env.number,
108108
timestamp: block_env.timestamp,
109-
protocol_version: CURRENT_STARKNET_VERSION,
109+
starknet_version: CURRENT_STARKNET_VERSION,
110110
l1_da_mode: L1DataAvailabilityMode::Calldata,
111111
sequencer_address: block_env.sequencer_address,
112112
l2_gas_prices: block_env.l2_gas_prices.clone(),
@@ -359,7 +359,7 @@ impl<'a, P: TrieWriter> UncommittedBlock<'a, P> {
359359
l1_gas_prices: self.header.l1_gas_prices,
360360
l1_data_gas_prices: self.header.l1_data_gas_prices,
361361
sequencer_address: self.header.sequencer_address,
362-
protocol_version: self.header.protocol_version,
362+
starknet_version: self.header.starknet_version,
363363
};
364364

365365
let hash = header.compute_hash();
@@ -405,7 +405,7 @@ impl<'a, P: TrieWriter> UncommittedBlock<'a, P> {
405405
l1_gas_prices: self.header.l1_gas_prices,
406406
l1_data_gas_prices: self.header.l1_data_gas_prices,
407407
sequencer_address: self.header.sequencer_address,
408-
protocol_version: self.header.protocol_version,
408+
starknet_version: self.header.starknet_version,
409409
};
410410

411411
let hash = header.compute_hash();

crates/core/src/service/block_producer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ impl<EF: ExecutorFactory> InstantBlockProducer<EF> {
625625
parent_hash,
626626
number: block_env.number,
627627
timestamp: block_env.timestamp,
628-
protocol_version: CURRENT_STARKNET_VERSION,
628+
starknet_version: CURRENT_STARKNET_VERSION,
629629
sequencer_address: block_env.sequencer_address,
630630
l1_da_mode: L1DataAvailabilityMode::Calldata,
631631
l2_gas_prices: block_env.l2_gas_prices.clone(),

crates/executor/src/implementation/blockifier/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use cache::ClassCache;
1515
use katana_primitives::block::{ExecutableBlock, GasPrices as KatanaGasPrices, PartialHeader};
1616
use katana_primitives::env::{BlockEnv, CfgEnv};
1717
use katana_primitives::transaction::{ExecutableTx, ExecutableTxWithHash, TxWithHash};
18+
use katana_primitives::version::StarknetVersion;
1819
use katana_provider::traits::state::StateProvider;
1920
use starknet_api::block::{
2021
BlockInfo, BlockNumber, BlockTimestamp, GasPriceVector, GasPrices, NonzeroGasPrice,
@@ -97,6 +98,7 @@ pub struct StarknetVMProcessor<'a> {
9798
simulation_flags: ExecutionFlags,
9899
stats: ExecutionStats,
99100
bouncer: Bouncer,
101+
starknet_version: StarknetVersion,
100102
}
101103

102104
impl<'a> StarknetVMProcessor<'a> {
@@ -138,6 +140,7 @@ impl<'a> StarknetVMProcessor<'a> {
138140
simulation_flags,
139141
stats: Default::default(),
140142
bouncer,
143+
starknet_version: block_env.starknet_version,
141144
}
142145
}
143146

@@ -186,6 +189,7 @@ impl<'a> StarknetVMProcessor<'a> {
186189
use_kzg_da: false,
187190
};
188191

192+
self.starknet_version = header.starknet_version;
189193
self.block_context = Arc::new(BlockContext::new(
190194
block_info,
191195
chain_info,
@@ -313,6 +317,7 @@ impl<'a> BlockExecutor<'a> for StarknetVMProcessor<'a> {
313317
l2_gas_prices,
314318
l1_gas_prices,
315319
l1_data_gas_prices,
320+
starknet_version: self.starknet_version,
316321
number: self.block_context.block_info().block_number.0,
317322
timestamp: self.block_context.block_info().block_timestamp.0,
318323
sequencer_address: utils::to_address(self.block_context.block_info().sequencer_address),

crates/executor/tests/fixtures/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub fn state_provider(chain: &ChainSpec) -> Box<dyn StateProvider> {
9191
/// [state_provider].
9292
#[rstest::fixture]
9393
pub fn valid_blocks() -> [ExecutableBlock; 3] {
94-
let protocol_version = CURRENT_STARKNET_VERSION;
94+
let starknet_version = CURRENT_STARKNET_VERSION;
9595
let chain_id = ChainId::parse("KATANA").unwrap();
9696
let sequencer_address = ContractAddress(1u64.into());
9797

@@ -107,7 +107,7 @@ pub fn valid_blocks() -> [ExecutableBlock; 3] {
107107
[
108108
ExecutableBlock {
109109
header: PartialHeader {
110-
protocol_version: protocol_version.clone(),
110+
starknet_version,
111111
number: 1,
112112
timestamp: 100,
113113
sequencer_address,
@@ -157,7 +157,7 @@ pub fn valid_blocks() -> [ExecutableBlock; 3] {
157157
},
158158
ExecutableBlock {
159159
header: PartialHeader {
160-
protocol_version: protocol_version.clone(),
160+
starknet_version,
161161
number: 2,
162162
timestamp: 200,
163163
sequencer_address,
@@ -191,7 +191,7 @@ pub fn valid_blocks() -> [ExecutableBlock; 3] {
191191
},
192192
ExecutableBlock {
193193
header: PartialHeader {
194-
protocol_version,
194+
starknet_version,
195195
number: 3,
196196
timestamp: 300,
197197
sequencer_address,

crates/feeder-gateway/src/types/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use katana_primitives::class::{
77
};
88
use katana_primitives::contract::{Nonce, StorageKey, StorageValue};
99
use katana_primitives::da::L1DataAvailabilityMode;
10-
use katana_primitives::version::ProtocolVersion;
10+
use katana_primitives::version::StarknetVersion;
1111
use katana_primitives::{ContractAddress, Felt};
1212
use katana_rpc_types::class::ConversionError;
1313
pub use katana_rpc_types::class::RpcSierraContractClass;
@@ -102,7 +102,7 @@ pub struct Block {
102102
pub transactions: Vec<ConfirmedTransaction>,
103103
pub transaction_receipts: Vec<ConfirmedReceipt>,
104104
#[serde(default)]
105-
pub starknet_version: Option<ProtocolVersion>,
105+
pub starknet_version: Option<StarknetVersion>,
106106
}
107107

108108
// -- Conversion to Katana primitive types.

crates/primitives/src/block.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use starknet::macros::short_string;
88
use crate::contract::ContractAddress;
99
use crate::da::L1DataAvailabilityMode;
1010
use crate::transaction::{ExecutableTxWithHash, TxHash, TxWithHash};
11-
use crate::version::ProtocolVersion;
11+
use crate::version::StarknetVersion;
1212
use crate::Felt;
1313

1414
pub type BlockIdOrTag = starknet::core::types::BlockId;
@@ -57,7 +57,7 @@ pub struct PartialHeader {
5757
pub l1_data_gas_prices: GasPrices,
5858
pub l2_gas_prices: GasPrices,
5959
pub l1_da_mode: L1DataAvailabilityMode,
60-
pub protocol_version: ProtocolVersion,
60+
pub starknet_version: StarknetVersion,
6161
}
6262

6363
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
@@ -194,7 +194,7 @@ pub struct Header {
194194
pub l1_data_gas_prices: GasPrices,
195195
pub l2_gas_prices: GasPrices,
196196
pub l1_da_mode: L1DataAvailabilityMode,
197-
pub protocol_version: ProtocolVersion,
197+
pub starknet_version: StarknetVersion,
198198
}
199199

200200
impl Header {
@@ -250,7 +250,7 @@ impl Header {
250250
self.l1_gas_prices.strk.get().into(),
251251
self.l1_data_gas_prices.eth.get().into(),
252252
self.l1_data_gas_prices.strk.get().into(),
253-
cairo_short_string_to_felt(&self.protocol_version.to_string()).unwrap(),
253+
cairo_short_string_to_felt(&self.starknet_version.to_string()).unwrap(),
254254
Felt::ZERO,
255255
self.parent_hash,
256256
])
@@ -316,7 +316,7 @@ impl Default for Header {
316316
l1_data_gas_prices: GasPrices::default(),
317317
sequencer_address: ContractAddress::default(),
318318
l1_da_mode: L1DataAvailabilityMode::Calldata,
319-
protocol_version: ProtocolVersion::default(),
319+
starknet_version: StarknetVersion::default(),
320320
}
321321
}
322322
}

crates/primitives/src/env.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::block::{BlockNumber, GasPrices};
22
use crate::chain::ChainId;
33
use crate::contract::ContractAddress;
4+
use crate::version::StarknetVersion;
45

56
/// Block environment values.
67
#[derive(Debug, Clone, Default, PartialEq, Eq)]
@@ -17,6 +18,8 @@ pub struct BlockEnv {
1718
pub l1_data_gas_prices: GasPrices,
1819
/// The contract address of the sequencer.
1920
pub sequencer_address: ContractAddress,
21+
/// The version of the Starknet protocol.
22+
pub starknet_version: StarknetVersion,
2023
}
2124

2225
/// The chain configuration values.

crates/primitives/src/version.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/// The currently supported version of the Starknet protocol.
2-
pub const CURRENT_STARKNET_VERSION: ProtocolVersion = ProtocolVersion::new([0, 13, 1, 1]); // version 0.13.1.1
2+
pub const CURRENT_STARKNET_VERSION: StarknetVersion = StarknetVersion::new([0, 13, 1, 1]); // version 0.13.1.1
33

4-
// TODO: figure out the exact format of the version string.
54
/// Starknet protocol version.
6-
#[derive(Debug, Clone, PartialEq, Eq)]
5+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
76
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
8-
pub struct ProtocolVersion {
7+
pub struct StarknetVersion {
98
/// Each segments represents a part of the version number.
109
segments: [u8; 4],
1110
}
@@ -18,7 +17,7 @@ pub enum ParseVersionError {
1817
ParseSegment(#[from] std::num::ParseIntError),
1918
}
2019

21-
impl ProtocolVersion {
20+
impl StarknetVersion {
2221
pub const fn new(segments: [u8; 4]) -> Self {
2322
Self { segments }
2423
}
@@ -45,9 +44,9 @@ impl ProtocolVersion {
4544
}
4645
}
4746

48-
impl core::default::Default for ProtocolVersion {
47+
impl core::default::Default for StarknetVersion {
4948
fn default() -> Self {
50-
ProtocolVersion::new([0, 1, 0, 0])
49+
StarknetVersion::new([0, 1, 0, 0])
5150
}
5251
}
5352

@@ -58,7 +57,7 @@ impl core::default::Default for ProtocolVersion {
5857
// - Version::new([1, 2, 3, 4]) will be displayed as "1.2.3.4"
5958
// - Version::new([1, 2, 3, 0]) will be displayed as "1.2.3"
6059
// - Version::new([0, 2, 3, 0]) will be displayed as "0.2.3"
61-
impl core::fmt::Display for ProtocolVersion {
60+
impl core::fmt::Display for StarknetVersion {
6261
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
6362
for (idx, segment) in self.segments.iter().enumerate() {
6463
// If it's the last segment, don't print it if it's zero.
@@ -77,10 +76,10 @@ impl core::fmt::Display for ProtocolVersion {
7776
}
7877
}
7978

80-
impl TryFrom<String> for ProtocolVersion {
79+
impl TryFrom<String> for StarknetVersion {
8180
type Error = ParseVersionError;
8281
fn try_from(value: String) -> Result<Self, Self::Error> {
83-
ProtocolVersion::parse(&value)
82+
StarknetVersion::parse(&value)
8483
}
8584
}
8685

@@ -91,16 +90,16 @@ mod serde {
9190
// We de/serialize the version from/into a human-readable string format to prevent breaking the
9291
// database encoding format if ever decide to change its memory representation.
9392

94-
impl ::serde::Serialize for ProtocolVersion {
93+
impl ::serde::Serialize for StarknetVersion {
9594
fn serialize<S: ::serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
9695
serializer.serialize_str(&self.to_string())
9796
}
9897
}
9998

100-
impl<'de> ::serde::Deserialize<'de> for ProtocolVersion {
99+
impl<'de> ::serde::Deserialize<'de> for StarknetVersion {
101100
fn deserialize<D: ::serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
102101
let s = String::deserialize(deserializer)?;
103-
ProtocolVersion::parse(&s).map_err(::serde::de::Error::custom)
102+
StarknetVersion::parse(&s).map_err(::serde::de::Error::custom)
104103
}
105104
}
106105
}
@@ -112,47 +111,47 @@ mod tests {
112111
#[test]
113112
fn parse_version_valid() {
114113
let version = "1.9.0.0";
115-
let parsed = ProtocolVersion::parse(version).unwrap();
114+
let parsed = StarknetVersion::parse(version).unwrap();
116115
assert_eq!(parsed.segments, [1, 9, 0, 0]);
117116
assert_eq!(String::from("1.9.0"), parsed.to_string());
118117
}
119118

120119
#[test]
121120
fn parse_version_missing_parts() {
122121
let version = "1.9.0";
123-
let parsed = ProtocolVersion::parse(version).unwrap();
122+
let parsed = StarknetVersion::parse(version).unwrap();
124123
assert_eq!(parsed.segments, [1, 9, 0, 0]);
125124
assert_eq!("1.9.0", parsed.to_string());
126125
}
127126

128127
#[test]
129128
fn parse_version_invalid_digit_should_fail() {
130129
let version = "0.fv.1.0";
131-
assert!(ProtocolVersion::parse(version).is_err());
130+
assert!(StarknetVersion::parse(version).is_err());
132131
}
133132

134133
#[test]
135134
fn parse_version_missing_digit_default_zero() {
136135
let version = "1...";
137-
let parsed = ProtocolVersion::parse(version).unwrap();
136+
let parsed = StarknetVersion::parse(version).unwrap();
138137
assert_eq!(parsed.segments, [1, 0, 0, 0]);
139138
assert_eq!("1.0.0", parsed.to_string());
140139
}
141140

142141
#[test]
143142
fn parse_version_many_parts_should_succeed() {
144143
let version = "1.2.3.4";
145-
let parsed = ProtocolVersion::parse(version).unwrap();
144+
let parsed = StarknetVersion::parse(version).unwrap();
146145
assert_eq!(parsed.segments, [1, 2, 3, 4]);
147146
assert_eq!("1.2.3.4", parsed.to_string());
148147
}
149148

150149
#[test]
151150
fn parse_invalid_formats() {
152151
let version = "";
153-
assert!(ProtocolVersion::parse(version).is_err());
152+
assert!(StarknetVersion::parse(version).is_err());
154153
let version = "1.2.3.4.5";
155-
assert!(ProtocolVersion::parse(version).is_err());
154+
assert!(StarknetVersion::parse(version).is_err());
156155
}
157156

158157
#[cfg(feature = "serde")]
@@ -161,17 +160,17 @@ mod tests {
161160

162161
#[test]
163162
fn rt_human_readable() {
164-
let version = ProtocolVersion::new([1, 2, 3, 4]);
163+
let version = StarknetVersion::new([1, 2, 3, 4]);
165164
let serialized = serde_json::to_string(&version).unwrap();
166-
let deserialized: ProtocolVersion = serde_json::from_str(&serialized).unwrap();
165+
let deserialized: StarknetVersion = serde_json::from_str(&serialized).unwrap();
167166
assert_eq!(version, deserialized);
168167
}
169168

170169
#[test]
171170
fn rt_non_human_readable() {
172-
let version = ProtocolVersion::new([1, 2, 3, 4]);
171+
let version = StarknetVersion::new([1, 2, 3, 4]);
173172
let serialized = postcard::to_stdvec(&version).unwrap();
174-
let deserialized: ProtocolVersion = postcard::from_bytes(&serialized).unwrap();
173+
let deserialized: StarknetVersion = postcard::from_bytes(&serialized).unwrap();
175174
assert_eq!(version, deserialized);
176175
}
177176
}

0 commit comments

Comments
 (0)