Skip to content

Commit

Permalink
refactor: undo gas_limit renaming (#9558)
Browse files Browse the repository at this point in the history
Chunk header `gas_limit` was renamed to `prev_gas_limit` in
#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.
  • Loading branch information
pugachAG authored and nikurt committed Sep 26, 2023
1 parent a4a1e34 commit aef043b
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 46 deletions.
4 changes: 2 additions & 2 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 4 additions & 5 deletions chain/chain/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions chain/chunks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidatorStake>,
transactions: Vec<SignedTransaction>,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion chain/client/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2345,7 +2345,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(),
Expand Down
2 changes: 1 addition & 1 deletion chain/client/src/tests/process_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions core/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
28 changes: 14 additions & 14 deletions core/primitives/src/sharding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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<ValidatorStake>,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions core/primitives/src/sharding/shard_chunk_header_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ impl From<ShardChunkHeader> 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(),
Expand All @@ -1080,7 +1080,7 @@ impl From<ChunkHeaderView> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
2 changes: 1 addition & 1 deletion tools/state-viewer/src/apply_chain_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tools/state-viewer/src/apply_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ 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,
is_first_block_with_chunk_of_version,
Default::default(),
use_flat_storage,
)?,
chunk_header.prev_gas_limit(),
chunk_header.gas_limit(),
))
}

Expand Down
4 changes: 2 additions & 2 deletions tools/state-viewer/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit aef043b

Please sign in to comment.