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

refactor: rename fields that correspond to the previous chunk #9500

Merged
merged 3 commits into from
Sep 8, 2023
Merged
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
24 changes: 14 additions & 10 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1567,8 +1567,8 @@ impl Chain {
.chunks()
.iter()
.filter(|chunk| block_height == chunk.height_included())
.flat_map(|chunk| chunk.validator_proposals())
.zip_longest(block.header().validator_proposals())
.flat_map(|chunk| chunk.prev_validator_proposals())
.zip_longest(block.header().prev_validator_proposals())
{
match pair {
itertools::EitherOrBoth::Both(cp, hp) => {
Expand Down Expand Up @@ -2944,7 +2944,7 @@ impl Chain {
&block
.chunks()
.iter()
.map(|chunk| chunk.outgoing_receipts_root())
.map(|chunk| chunk.prev_outgoing_receipts_root())
.collect::<Vec<CryptoHash>>(),
);

Expand All @@ -2956,12 +2956,12 @@ impl Chain {
let receipts_hash = CryptoHash::hash_borsh(ReceiptList(shard_id, receipts));
let from_shard_id = *from_shard_id as usize;

let root_proof = block.chunks()[from_shard_id].outgoing_receipts_root();
let root_proof = block.chunks()[from_shard_id].prev_outgoing_receipts_root();
root_proofs_cur
.push(RootProof(root_proof, block_receipts_proofs[from_shard_id].clone()));

// Make sure we send something reasonable.
assert_eq!(block_header.chunk_receipts_root(), &block_receipts_root);
assert_eq!(block_header.prev_chunk_outgoing_receipts_root(), &block_receipts_root);
assert!(verify_path(root_proof, proof, &receipts_hash));
assert!(verify_path(
block_receipts_root,
Expand Down Expand Up @@ -3233,7 +3233,11 @@ impl Chain {
return Err(Error::Other("set_shard_state failed: invalid proofs".into()));
}
// 4f. Proving the outgoing_receipts_root matches that in the block
if !verify_path(*block_header.chunk_receipts_root(), block_proof, root) {
if !verify_path(
*block_header.prev_chunk_outgoing_receipts_root(),
block_proof,
root,
) {
byzantine_assert!(false);
return Err(Error::Other("set_shard_state failed: invalid proofs".into()));
}
Expand Down Expand Up @@ -4031,7 +4035,7 @@ impl Chain {
};

let chunk_inner = chunk.cloned_header().take_inner();
let gas_limit = chunk_inner.gas_limit();
let gas_limit = chunk_inner.prev_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 @@ -4069,7 +4073,7 @@ impl Chain {
&block_hash,
&receipts,
chunk.transactions(),
chunk_inner.validator_proposals(),
chunk_inner.prev_validator_proposals(),
gas_price,
gas_limit,
&challenges_result,
Expand Down Expand Up @@ -5610,7 +5614,7 @@ impl<'a> ChainUpdate<'a> {
};

let chunk_header = chunk.cloned_header();
let gas_limit = chunk_header.gas_limit();
let gas_limit = chunk_header.prev_gas_limit();
// This is set to false because the value is only relevant
// during protocol version RestoreReceiptsAfterFixApplyChunks.
// TODO(nikurt): Determine the value correctly.
Expand All @@ -5625,7 +5629,7 @@ impl<'a> ChainUpdate<'a> {
block_header.hash(),
&receipts,
chunk.transactions(),
chunk_header.validator_proposals(),
chunk_header.prev_validator_proposals(),
gas_price,
gas_limit,
block_header.challenges_result(),
Expand Down
8 changes: 4 additions & 4 deletions chain/chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1970,7 +1970,7 @@ impl<'a> ChainStoreUpdate<'a> {
.transactions
.insert(transaction.get_hash(), Arc::new(transaction.clone()));
}
for receipt in chunk.receipts() {
for receipt in chunk.prev_outgoing_receipts() {
self.chain_store_cache_update
.receipts
.insert(receipt.receipt_id, Arc::new(receipt.clone()));
Expand Down Expand Up @@ -2224,7 +2224,7 @@ impl<'a> ChainStoreUpdate<'a> {
for transaction in chunk.transactions() {
self.gc_col(DBCol::Transactions, transaction.get_hash().as_bytes());
}
for receipt in chunk.receipts() {
for receipt in chunk.prev_outgoing_receipts() {
self.gc_col(DBCol::Receipts, receipt.get_hash().as_bytes());
}

Expand Down Expand Up @@ -2550,7 +2550,7 @@ impl<'a> ChainStoreUpdate<'a> {
for transaction in chunk.transactions() {
self.gc_col(DBCol::Transactions, transaction.get_hash().as_bytes());
}
for receipt in chunk.receipts() {
for receipt in chunk.prev_outgoing_receipts() {
self.gc_col(DBCol::Receipts, receipt.get_hash().as_bytes());
}

Expand Down Expand Up @@ -3041,7 +3041,7 @@ impl<'a> ChainStoreUpdate<'a> {
}

// Increase receipt refcounts for all included receipts
for receipt in chunk.receipts().iter() {
for receipt in chunk.prev_outgoing_receipts().iter() {
let bytes = receipt.try_to_vec().expect("Borsh cannot fail");
store_update.increment_refcount(
DBCol::Receipts,
Expand Down
2 changes: 1 addition & 1 deletion chain/chain/src/store_validator/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ pub(crate) fn chunk_tx_exists(
let tx_hash = tx.get_hash();
sv.inner.tx_refcount.entry(tx_hash).and_modify(|x| *x += 1).or_insert(1);
}
for receipt in shard_chunk.receipts().iter() {
for receipt in shard_chunk.prev_outgoing_receipts().iter() {
sv.inner.receipt_refcount.entry(receipt.get_hash()).and_modify(|x| *x += 1).or_insert(1);
}
for tx in shard_chunk.transactions().iter() {
Expand Down
2 changes: 1 addition & 1 deletion chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub fn display_chain(me: &Option<AccountId>, chain: &mut Chain, tail: bool) {
chunk_header.shard_id(),
chunk_producer,
chunk.transactions().len(),
chunk.receipts().len()
chunk.prev_outgoing_receipts().len()
);
} else if let Ok(partial_chunk) =
chain_store.get_partial_chunk(&chunk_header.chunk_hash())
Expand Down
21 changes: 11 additions & 10 deletions chain/chain/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ pub fn validate_chunk_proofs(
return Ok(false);
}
let height_created = chunk.height_created();
let outgoing_receipts_root = chunk.outgoing_receipts_root();
let (transactions, receipts) = (chunk.transactions(), chunk.receipts());
let outgoing_receipts_root = chunk.prev_outgoing_receipts_root();
let (transactions, receipts) = (chunk.transactions(), chunk.prev_outgoing_receipts());

// 2b. Checking that chunk transactions are valid
let (tx_root, _) = merklize(transactions);
Expand Down Expand Up @@ -132,27 +132,27 @@ pub fn validate_chunk_with_chunk_extra(
return Err(Error::InvalidStateRoot);
}

if *prev_chunk_extra.outcome_root() != chunk_header.outcome_root() {
if *prev_chunk_extra.outcome_root() != chunk_header.prev_outcome_root() {
return Err(Error::InvalidOutcomesProof);
}

let chunk_extra_proposals = prev_chunk_extra.validator_proposals();
let chunk_header_proposals = chunk_header.validator_proposals();
let chunk_header_proposals = chunk_header.prev_validator_proposals();
if chunk_header_proposals.len() != chunk_extra_proposals.len()
|| !chunk_extra_proposals.eq(chunk_header_proposals)
{
return Err(Error::InvalidValidatorProposals);
}

if prev_chunk_extra.gas_limit() != chunk_header.gas_limit() {
if prev_chunk_extra.gas_limit() != chunk_header.prev_gas_limit() {
return Err(Error::InvalidGasLimit);
}

if prev_chunk_extra.gas_used() != chunk_header.gas_used() {
if prev_chunk_extra.gas_used() != chunk_header.prev_gas_used() {
return Err(Error::InvalidGasUsed);
}

if prev_chunk_extra.balance_burnt() != chunk_header.balance_burnt() {
if prev_chunk_extra.balance_burnt() != chunk_header.prev_balance_burnt() {
return Err(Error::InvalidBalanceBurnt);
}

Expand All @@ -168,13 +168,14 @@ pub fn validate_chunk_with_chunk_extra(
};
let (outgoing_receipts_root, _) = merklize(&outgoing_receipts_hashes);

if outgoing_receipts_root != chunk_header.outgoing_receipts_root() {
if outgoing_receipts_root != chunk_header.prev_outgoing_receipts_root() {
return Err(Error::InvalidReceiptsProof);
}

let prev_gas_limit = prev_chunk_extra.gas_limit();
if chunk_header.gas_limit() < prev_gas_limit - prev_gas_limit / GAS_LIMIT_ADJUSTMENT_FACTOR
|| chunk_header.gas_limit() > prev_gas_limit + prev_gas_limit / GAS_LIMIT_ADJUSTMENT_FACTOR
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
{
return Err(Error::InvalidGasLimit);
}
Expand Down
36 changes: 20 additions & 16 deletions chain/chunks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ impl ShardsManager {

// Get outgoing receipts for the chunk and construct vector of their
// proofs.
let outgoing_receipts = chunk.receipts();
let outgoing_receipts = chunk.prev_outgoing_receipts();
let present_receipts: HashMap<ShardId, _> = match make_outgoing_receipts_proofs(
&header,
&outgoing_receipts,
Expand Down Expand Up @@ -1429,7 +1429,11 @@ impl ShardsManager {
let shard_id = proof.1.to_shard_id;
let ReceiptProof(shard_receipts, receipt_proof) = proof;
let receipt_hash = CryptoHash::hash_borsh(ReceiptList(shard_id, shard_receipts));
if !verify_path(header.outgoing_receipts_root(), &receipt_proof.proof, &receipt_hash) {
if !verify_path(
header.prev_outgoing_receipts_root(),
&receipt_proof.proof,
&receipt_hash,
) {
byzantine_assert!(false);
return Err(Error::ChainError(near_chain::Error::InvalidReceiptsProof));
}
Expand Down Expand Up @@ -1803,16 +1807,16 @@ impl ShardsManager {
pub fn create_encoded_shard_chunk(
prev_block_hash: CryptoHash,
prev_state_root: StateRoot,
outcome_root: CryptoHash,
prev_outcome_root: CryptoHash,
height: u64,
shard_id: ShardId,
gas_used: Gas,
gas_limit: Gas,
balance_burnt: Balance,
validator_proposals: Vec<ValidatorStake>,
prev_gas_used: Gas,
prev_gas_limit: Gas,
prev_balance_burnt: Balance,
prev_validator_proposals: Vec<ValidatorStake>,
transactions: Vec<SignedTransaction>,
outgoing_receipts: &[Receipt],
outgoing_receipts_root: CryptoHash,
prev_outgoing_receipts: &[Receipt],
prev_outgoing_receipts_root: CryptoHash,
tx_root: CryptoHash,
signer: &dyn ValidatorSigner,
rs: &mut ReedSolomonWrapper,
Expand All @@ -1821,18 +1825,18 @@ impl ShardsManager {
EncodedShardChunk::new(
prev_block_hash,
prev_state_root,
outcome_root,
prev_outcome_root,
height,
shard_id,
rs,
gas_used,
gas_limit,
balance_burnt,
prev_gas_used,
prev_gas_limit,
prev_balance_burnt,
tx_root,
validator_proposals,
prev_validator_proposals,
transactions,
outgoing_receipts,
outgoing_receipts_root,
prev_outgoing_receipts,
prev_outgoing_receipts_root,
signer,
protocol_version,
)
Expand Down
4 changes: 2 additions & 2 deletions chain/chunks/src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn make_outgoing_receipts_proofs(

let hashes = Chain::build_receipts_hashes(&outgoing_receipts, &shard_layout);
let (root, proofs) = merklize(&hashes);
assert_eq!(chunk_header.outgoing_receipts_root(), root);
assert_eq!(chunk_header.prev_outgoing_receipts_root(), root);

let mut receipts_by_shard =
Chain::group_receipts_by_shard(outgoing_receipts.to_vec(), &shard_layout);
Expand Down Expand Up @@ -158,7 +158,7 @@ pub fn decode_encoded_chunk(
let partial_chunk = create_partial_chunk(
encoded_chunk,
merkle_paths,
shard_chunk.receipts().to_vec(),
shard_chunk.prev_outgoing_receipts().to_vec(),
me,
epoch_manager,
shard_tracker,
Expand Down
4 changes: 2 additions & 2 deletions chain/client/src/client_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,8 +1283,8 @@ impl ClientActor {
if included {
self.info_helper.chunk_processed(
chunk.shard_id(),
chunk.gas_used(),
chunk.balance_burnt(),
chunk.prev_gas_used(),
chunk.prev_balance_burnt(),
);
} else {
self.info_helper.chunk_skipped(chunk.shard_id());
Expand Down
2 changes: 1 addition & 1 deletion chain/client/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ impl ClientActor {
chunk.shard_id(),
)
.ok(),
gas_used: chunk.gas_used(),
gas_used: chunk.prev_gas_used(),
processing_time_ms: CryptoHashTimer::get_timer_value(
chunk.chunk_hash().0,
)
Expand Down
14 changes: 7 additions & 7 deletions chain/client/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2346,18 +2346,18 @@ pub fn create_chunk(
let (mut encoded_chunk, mut new_merkle_paths) = EncodedShardChunk::new(
*header.prev_block_hash(),
header.prev_state_root(),
header.outcome_root(),
header.prev_outcome_root(),
header.height_created(),
header.shard_id(),
&mut rs,
header.gas_used(),
header.gas_limit(),
header.balance_burnt(),
header.prev_gas_used(),
header.prev_gas_limit(),
header.prev_balance_burnt(),
tx_root,
header.validator_proposals().collect(),
header.prev_validator_proposals().collect(),
transactions,
decoded_chunk.receipts(),
header.outgoing_receipts_root(),
decoded_chunk.prev_outgoing_receipts(),
header.prev_outgoing_receipts_root(),
&*signer,
PROTOCOL_VERSION,
)
Expand Down
18 changes: 9 additions & 9 deletions chain/client/src/tests/process_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn test_not_process_height_twice() {

let proposals =
vec![ValidatorStake::new("test1".parse().unwrap(), PublicKey::empty(KeyType::ED25519), 0)];
duplicate_block.mut_header().get_mut().inner_rest.validator_proposals = proposals;
duplicate_block.mut_header().get_mut().inner_rest.prev_validator_proposals = proposals;
duplicate_block.mut_header().resign(&validator_signer);
let dup_block_hash = *duplicate_block.hash();
// we should have dropped the block before we even tried to process it, so the result should be ok
Expand Down Expand Up @@ -55,29 +55,29 @@ fn test_bad_shard_id() {
let mut chunks: Vec<_> = block.chunks().iter().cloned().collect();
// modify chunk 0 to have shard_id 1
let chunk = chunks.get(0).unwrap();
let outgoing_receipts_root = chunks.get(1).unwrap().outgoing_receipts_root();
let outgoing_receipts_root = chunks.get(1).unwrap().prev_outgoing_receipts_root();
let mut modified_chunk = ShardChunkHeaderV3::new(
*chunk.prev_block_hash(),
chunk.prev_state_root(),
chunk.outcome_root(),
chunk.prev_outcome_root(),
chunk.encoded_merkle_root(),
chunk.encoded_length(),
2,
1,
chunk.gas_used(),
chunk.gas_limit(),
chunk.balance_burnt(),
chunk.prev_gas_used(),
chunk.prev_gas_limit(),
chunk.prev_balance_burnt(),
outgoing_receipts_root,
chunk.tx_root(),
chunk.validator_proposals().collect(),
chunk.prev_validator_proposals().collect(),
&validator_signer,
);
modified_chunk.height_included = 2;
chunks[0] = ShardChunkHeader::V3(modified_chunk);
block.mut_header().get_mut().inner_rest.chunk_headers_root =
Block::compute_chunk_headers_root(&chunks).0;
block.mut_header().get_mut().inner_rest.chunk_receipts_root =
Block::compute_chunk_receipts_root(&chunks);
block.mut_header().get_mut().inner_rest.prev_chunk_outgoing_receipts_root =
Block::compute_chunk_prev_outgoing_receipts_root(&chunks);
block.set_chunks(chunks);
block.mut_header().get_mut().inner_rest.block_body_hash =
block.compute_block_body_hash().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion chain/client/src/view_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ impl Handler<WithSpanContext<GetExecutionOutcome>> for ViewClientActor {
.get_block(&h)?
.chunks()
.iter()
.map(|header| header.outcome_root())
.map(|header| header.prev_outcome_root())
.collect::<Vec<_>>();
if target_shard_id >= (outcome_roots.len() as u64) {
return Err(GetExecutionOutcomeError::InconsistentState {
Expand Down
Loading