From ce230e15f355e543e2f8c827d889cf946ac09e5b Mon Sep 17 00:00:00 2001 From: Anton Puhach Date: Fri, 22 Sep 2023 15:40:17 +0200 Subject: [PATCH] refactor: undo gas_limit renaming (#9558) Chunk header `gas_limit` was renamed to `prev_gas_limit` in https://github.com/near/nearcore/pull/9500. After discussion with @jakmeier I've realised while it is determined in the previous chunk, it actually corresponds to gas limit for the current chunk. --- chain/chain/src/chain.rs | 4 +-- chain/chain/src/validate.rs | 9 +++--- chain/chunks/src/lib.rs | 4 +-- chain/client/src/test_utils.rs | 2 +- chain/client/src/tests/process_blocks.rs | 2 +- core/primitives/src/block.rs | 4 +-- core/primitives/src/sharding.rs | 28 +++++++++---------- .../src/sharding/shard_chunk_header_inner.rs | 20 ++++++------- core/primitives/src/views.rs | 4 +-- .../client/features/cap_max_gas_price.rs | 2 +- .../features/increase_storage_compute_cost.rs | 2 +- tools/state-viewer/src/apply_chain_range.rs | 2 +- tools/state-viewer/src/apply_chunk.rs | 4 +-- tools/state-viewer/src/commands.rs | 4 +-- 14 files changed, 45 insertions(+), 46 deletions(-) diff --git a/chain/chain/src/chain.rs b/chain/chain/src/chain.rs index 0024eca1381..84b79eaa1f1 100644 --- a/chain/chain/src/chain.rs +++ b/chain/chain/src/chain.rs @@ -4032,7 +4032,7 @@ impl Chain { }; let chunk_inner = chunk.cloned_header().take_inner(); - let gas_limit = chunk_inner.prev_gas_limit(); + let gas_limit = chunk_inner.gas_limit(); // This variable is responsible for checking to which block we can apply receipts previously lost in apply_chunks // (see https://github.com/near/nearcore/pull/4248/) @@ -5611,7 +5611,7 @@ impl<'a> ChainUpdate<'a> { }; let chunk_header = chunk.cloned_header(); - let gas_limit = chunk_header.prev_gas_limit(); + let gas_limit = chunk_header.gas_limit(); // This is set to false because the value is only relevant // during protocol version RestoreReceiptsAfterFixApplyChunks. // TODO(nikurt): Determine the value correctly. diff --git a/chain/chain/src/validate.rs b/chain/chain/src/validate.rs index 9ae4b587cd3..b570d16636c 100644 --- a/chain/chain/src/validate.rs +++ b/chain/chain/src/validate.rs @@ -128,7 +128,7 @@ pub fn validate_chunk_with_chunk_extra( return Err(Error::InvalidValidatorProposals); } - if prev_chunk_extra.gas_limit() != chunk_header.prev_gas_limit() { + if prev_chunk_extra.gas_limit() != chunk_header.gas_limit() { return Err(Error::InvalidGasLimit); } @@ -156,10 +156,9 @@ pub fn validate_chunk_with_chunk_extra( return Err(Error::InvalidReceiptsProof); } - let prev_gas_limit = prev_chunk_extra.gas_limit(); - if chunk_header.prev_gas_limit() < prev_gas_limit - prev_gas_limit / GAS_LIMIT_ADJUSTMENT_FACTOR - || chunk_header.prev_gas_limit() - > prev_gas_limit + prev_gas_limit / GAS_LIMIT_ADJUSTMENT_FACTOR + let gas_limit = prev_chunk_extra.gas_limit(); + if chunk_header.gas_limit() < gas_limit - gas_limit / GAS_LIMIT_ADJUSTMENT_FACTOR + || chunk_header.gas_limit() > gas_limit + gas_limit / GAS_LIMIT_ADJUSTMENT_FACTOR { return Err(Error::InvalidGasLimit); } diff --git a/chain/chunks/src/lib.rs b/chain/chunks/src/lib.rs index ec21234901e..b423c0f93e2 100644 --- a/chain/chunks/src/lib.rs +++ b/chain/chunks/src/lib.rs @@ -1811,7 +1811,7 @@ impl ShardsManager { height: u64, shard_id: ShardId, prev_gas_used: Gas, - prev_gas_limit: Gas, + gas_limit: Gas, prev_balance_burnt: Balance, prev_validator_proposals: Vec, transactions: Vec, @@ -1830,7 +1830,7 @@ impl ShardsManager { shard_id, rs, prev_gas_used, - prev_gas_limit, + gas_limit, prev_balance_burnt, tx_root, prev_validator_proposals, diff --git a/chain/client/src/test_utils.rs b/chain/client/src/test_utils.rs index 2edf27aeda3..998992a505a 100644 --- a/chain/client/src/test_utils.rs +++ b/chain/client/src/test_utils.rs @@ -2351,7 +2351,7 @@ pub fn create_chunk( header.shard_id(), &mut rs, header.prev_gas_used(), - header.prev_gas_limit(), + header.gas_limit(), header.prev_balance_burnt(), tx_root, header.prev_validator_proposals().collect(), diff --git a/chain/client/src/tests/process_blocks.rs b/chain/client/src/tests/process_blocks.rs index b510339e27d..9762653c005 100644 --- a/chain/client/src/tests/process_blocks.rs +++ b/chain/client/src/tests/process_blocks.rs @@ -65,7 +65,7 @@ fn test_bad_shard_id() { 2, 1, chunk.prev_gas_used(), - chunk.prev_gas_limit(), + chunk.gas_limit(), chunk.prev_balance_burnt(), outgoing_receipts_root, chunk.tx_root(), diff --git a/core/primitives/src/block.rs b/core/primitives/src/block.rs index 5c81c29b635..841ba9ab48d 100644 --- a/core/primitives/src/block.rs +++ b/core/primitives/src/block.rs @@ -247,7 +247,7 @@ impl Block { if chunk.height_included() == height { prev_validator_proposals.extend(chunk.prev_validator_proposals()); gas_used += chunk.prev_gas_used(); - gas_limit += chunk.prev_gas_limit(); + gas_limit += chunk.gas_limit(); balance_burnt += chunk.prev_balance_burnt(); chunk_mask.push(true); } else { @@ -482,7 +482,7 @@ impl Block { ) -> Gas { chunks.into_iter().fold(0, |acc, chunk| { if chunk.height_included() == height { - acc + chunk.prev_gas_limit() + acc + chunk.gas_limit() } else { acc } diff --git a/core/primitives/src/sharding.rs b/core/primitives/src/sharding.rs index 28c5485831f..43f1c5ff84c 100644 --- a/core/primitives/src/sharding.rs +++ b/core/primitives/src/sharding.rs @@ -120,7 +120,7 @@ impl ShardChunkHeaderV2 { height: BlockHeight, shard_id: ShardId, prev_gas_used: Gas, - prev_gas_limit: Gas, + gas_limit: Gas, prev_balance_burnt: Balance, prev_outgoing_receipts_root: CryptoHash, tx_root: CryptoHash, @@ -136,7 +136,7 @@ impl ShardChunkHeaderV2 { height_created: height, shard_id, prev_gas_used, - prev_gas_limit, + gas_limit, prev_balance_burnt, prev_outgoing_receipts_root, tx_root, @@ -184,7 +184,7 @@ impl ShardChunkHeaderV3 { height: BlockHeight, shard_id: ShardId, prev_gas_used: Gas, - prev_gas_limit: Gas, + gas_limit: Gas, prev_balance_burnt: Balance, prev_outgoing_receipts_root: CryptoHash, tx_root: CryptoHash, @@ -200,7 +200,7 @@ impl ShardChunkHeaderV3 { height_created: height, shard_id, prev_gas_used, - prev_gas_limit, + gas_limit, prev_balance_burnt, prev_outgoing_receipts_root, tx_root, @@ -338,11 +338,11 @@ impl ShardChunkHeader { } #[inline] - pub fn prev_gas_limit(&self) -> Gas { + pub fn gas_limit(&self) -> Gas { match &self { - ShardChunkHeader::V1(header) => header.inner.prev_gas_limit, - ShardChunkHeader::V2(header) => header.inner.prev_gas_limit, - ShardChunkHeader::V3(header) => header.inner.prev_gas_limit(), + ShardChunkHeader::V1(header) => header.inner.gas_limit, + ShardChunkHeader::V2(header) => header.inner.gas_limit, + ShardChunkHeader::V3(header) => header.inner.gas_limit(), } } @@ -441,7 +441,7 @@ impl ShardChunkHeaderV1 { height: BlockHeight, shard_id: ShardId, prev_gas_used: Gas, - prev_gas_limit: Gas, + gas_limit: Gas, prev_balance_burnt: Balance, prev_outgoing_receipts_root: CryptoHash, tx_root: CryptoHash, @@ -457,7 +457,7 @@ impl ShardChunkHeaderV1 { height_created: height, shard_id, prev_gas_used, - prev_gas_limit, + gas_limit, prev_balance_burnt, prev_outgoing_receipts_root, tx_root, @@ -1048,7 +1048,7 @@ impl EncodedShardChunk { shard_id: ShardId, rs: &mut ReedSolomonWrapper, prev_gas_used: Gas, - prev_gas_limit: Gas, + gas_limit: Gas, prev_balance_burnt: Balance, tx_root: CryptoHash, prev_validator_proposals: Vec, @@ -1079,7 +1079,7 @@ impl EncodedShardChunk { height, shard_id, prev_gas_used, - prev_gas_limit, + gas_limit, prev_balance_burnt, prev_outgoing_receipts_root, tx_root, @@ -1102,7 +1102,7 @@ impl EncodedShardChunk { height, shard_id, prev_gas_used, - prev_gas_limit, + gas_limit, prev_balance_burnt, prev_outgoing_receipts_root, tx_root, @@ -1121,7 +1121,7 @@ impl EncodedShardChunk { height, shard_id, prev_gas_used, - prev_gas_limit, + gas_limit, prev_balance_burnt, prev_outgoing_receipts_root, tx_root, diff --git a/core/primitives/src/sharding/shard_chunk_header_inner.rs b/core/primitives/src/sharding/shard_chunk_header_inner.rs index e1e5cf7251e..537727f231b 100644 --- a/core/primitives/src/sharding/shard_chunk_header_inner.rs +++ b/core/primitives/src/sharding/shard_chunk_header_inner.rs @@ -31,11 +31,11 @@ impl ShardChunkHeaderInner { } #[inline] - pub fn prev_gas_limit(&self) -> Gas { + pub fn gas_limit(&self) -> Gas { match self { - Self::V1(inner) => inner.prev_gas_limit, - Self::V2(inner) => inner.prev_gas_limit, - Self::V3(inner) => inner.prev_gas_limit, + Self::V1(inner) => inner.gas_limit, + Self::V2(inner) => inner.gas_limit, + Self::V3(inner) => inner.gas_limit, } } @@ -144,8 +144,8 @@ pub struct ShardChunkHeaderInnerV1 { pub shard_id: ShardId, /// Gas used in the previous chunk. pub prev_gas_used: Gas, - /// Gas limit voted by validators in the previous chunk. - pub prev_gas_limit: Gas, + /// Gas limit voted by validators. + pub gas_limit: Gas, /// Total balance burnt in the previous chunk. pub prev_balance_burnt: Balance, /// Previous chunk's outgoing receipts merkle root. @@ -171,8 +171,8 @@ pub struct ShardChunkHeaderInnerV2 { pub shard_id: ShardId, /// Gas used in the previous chunk. pub prev_gas_used: Gas, - /// Gas limit voted by validators in the previous chunk. - pub prev_gas_limit: Gas, + /// Gas limit voted by validators. + pub gas_limit: Gas, /// Total balance burnt in the previous chunk. pub prev_balance_burnt: Balance, /// Previous chunk's outgoing receipts merkle root. @@ -198,8 +198,8 @@ pub struct ShardChunkHeaderInnerV3 { pub shard_id: ShardId, /// Gas used in the previous chunk. pub prev_gas_used: Gas, - /// Gas limit voted by validators in the previous chunk. - pub prev_gas_limit: Gas, + /// Gas limit voted by validators. + pub gas_limit: Gas, /// Total balance burnt in the previous chunk. pub prev_balance_burnt: Balance, /// Previous chunk's outgoing receipts merkle root. diff --git a/core/primitives/src/views.rs b/core/primitives/src/views.rs index f53a0a1a03a..c64612bed58 100644 --- a/core/primitives/src/views.rs +++ b/core/primitives/src/views.rs @@ -1056,7 +1056,7 @@ impl From for ChunkHeaderView { height_included, shard_id: inner.shard_id(), gas_used: inner.prev_gas_used(), - gas_limit: inner.prev_gas_limit(), + gas_limit: inner.gas_limit(), rent_paid: 0, validator_reward: 0, balance_burnt: inner.prev_balance_burnt(), @@ -1080,7 +1080,7 @@ impl From for ShardChunkHeader { height_created: view.height_created, shard_id: view.shard_id, prev_gas_used: view.gas_used, - prev_gas_limit: view.gas_limit, + gas_limit: view.gas_limit, prev_balance_burnt: view.balance_burnt, prev_outgoing_receipts_root: view.outgoing_receipts_root, tx_root: view.tx_root, diff --git a/integration-tests/src/tests/client/features/cap_max_gas_price.rs b/integration-tests/src/tests/client/features/cap_max_gas_price.rs index f24593bed1e..4f039676f10 100644 --- a/integration-tests/src/tests/client/features/cap_max_gas_price.rs +++ b/integration-tests/src/tests/client/features/cap_max_gas_price.rs @@ -17,7 +17,7 @@ fn does_gas_price_exceed_limit(protocol_version: ProtocolVersion) -> bool { .unwrap(); let min_gas_price = env.clients[0].chain.block_economics_config.min_gas_price(protocol_version); - was_congested |= block.chunks()[0].prev_gas_used() >= block.chunks()[0].prev_gas_limit(); + was_congested |= block.chunks()[0].prev_gas_used() >= block.chunks()[0].gas_limit(); price_exceeded_limit |= block.header().gas_price() > 20 * min_gas_price; } diff --git a/integration-tests/src/tests/client/features/increase_storage_compute_cost.rs b/integration-tests/src/tests/client/features/increase_storage_compute_cost.rs index ab66a1657da..922dbcbc757 100644 --- a/integration-tests/src/tests/client/features/increase_storage_compute_cost.rs +++ b/integration-tests/src/tests/client/features/increase_storage_compute_cost.rs @@ -237,7 +237,7 @@ fn assert_compute_limit_reached( ); let chunk_header = old_chunk.cloned_header(); let gas_burnt = chunk_header.prev_gas_used(); - let gas_limit: u64 = chunk_header.prev_gas_limit(); + let gas_limit: u64 = chunk_header.gas_limit(); assert!( gas_burnt >= gas_limit, "should saturate gas limit, only burnt {gas_burnt} when limit was {gas_limit}" diff --git a/tools/state-viewer/src/apply_chain_range.rs b/tools/state-viewer/src/apply_chain_range.rs index 0f117d49b06..b9dc3d2407d 100644 --- a/tools/state-viewer/src/apply_chain_range.rs +++ b/tools/state-viewer/src/apply_chain_range.rs @@ -239,7 +239,7 @@ fn apply_block_from_range( chunk.transactions(), chunk_inner.prev_validator_proposals(), prev_block.header().gas_price(), - chunk_inner.prev_gas_limit(), + chunk_inner.gas_limit(), block.header().challenges_result(), *block.header().random_value(), true, diff --git a/tools/state-viewer/src/apply_chunk.rs b/tools/state-viewer/src/apply_chunk.rs index 1bebd3ac532..16dbedb36a5 100644 --- a/tools/state-viewer/src/apply_chunk.rs +++ b/tools/state-viewer/src/apply_chunk.rs @@ -142,7 +142,7 @@ pub(crate) fn apply_chunk( transactions, chunk_header.prev_validator_proposals(), gas_price, - chunk_header.prev_gas_limit(), + chunk_header.gas_limit(), &vec![], hash("random seed".as_ref()), true, @@ -150,7 +150,7 @@ pub(crate) fn apply_chunk( Default::default(), use_flat_storage, )?, - chunk_header.prev_gas_limit(), + chunk_header.gas_limit(), )) } diff --git a/tools/state-viewer/src/commands.rs b/tools/state-viewer/src/commands.rs index 5fded22d2ec..4060c0043c1 100644 --- a/tools/state-viewer/src/commands.rs +++ b/tools/state-viewer/src/commands.rs @@ -87,7 +87,7 @@ pub(crate) fn apply_block( chunk.transactions(), chunk_inner.prev_validator_proposals(), prev_block.header().gas_price(), - chunk_inner.prev_gas_limit(), + chunk_inner.gas_limit(), block.header().challenges_result(), *block.header().random_value(), true, @@ -524,7 +524,7 @@ pub(crate) fn check_apply_block_result( let height = block.header().height(); let block_hash = block.header().hash(); let new_chunk_extra = - resulting_chunk_extra(apply_result, block.chunks()[shard_id as usize].prev_gas_limit()); + resulting_chunk_extra(apply_result, block.chunks()[shard_id as usize].gas_limit()); println!( "apply chunk for shard {} at height {}, resulting chunk extra {:?}", shard_id, height, &new_chunk_extra,