Skip to content

Commit

Permalink
Get rid of internal call in EncodedShardChunk (#4424)
Browse files Browse the repository at this point in the history
Get rid of internal `Self::from_parts_and_metadata` passing almost the same set of arguments, which should improve readability
  • Loading branch information
Looogarithm authored Jul 1, 2021
1 parent 949d4e2 commit afac695
Showing 1 changed file with 32 additions and 65 deletions.
97 changes: 32 additions & 65 deletions core/primitives/src/sharding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ impl EncodedShardChunkV1 {
}

pub fn decode_chunk(&self, data_parts: usize) -> Result<ShardChunkV1, std::io::Error> {
let transaction_receipts = EncodedShardChunk::create_transaction_receipts(
let transaction_receipts = EncodedShardChunk::decode_transaction_receipts(
&self.content.parts[0..data_parts],
self.header.inner.encoded_length,
)?;
Expand Down Expand Up @@ -1035,25 +1035,25 @@ impl EncodedShardChunk {
}
}

pub fn new(
prev_block_hash: CryptoHash,
prev_state_root: StateRoot,
outcome_root: CryptoHash,
height: BlockHeight,
shard_id: ShardId,
rs: &mut ReedSolomonWrapper,
gas_used: Gas,
gas_limit: Gas,
balance_burnt: Balance,
fn decode_transaction_receipts(
parts: &[Option<Box<[u8]>>],
encoded_length: u64,
) -> Result<TransactionReceipt, std::io::Error> {
let encoded_data = parts
.iter()
.flat_map(|option| option.as_ref().expect("Missing shard").iter())
.cloned()
.take(encoded_length as usize)
.collect::<Vec<u8>>();

tx_root: CryptoHash,
validator_proposals: Vec<ValidatorStake>,
TransactionReceipt::try_from_slice(&encoded_data)
}

fn encode_transaction_receipts(
rs: &mut ReedSolomonWrapper,
transactions: Vec<SignedTransaction>,
outgoing_receipts: &Vec<Receipt>,
outgoing_receipts_root: CryptoHash,
signer: &dyn ValidatorSigner,
protocol_version: ProtocolVersion,
) -> Result<(Self, Vec<MerklePath>), std::io::Error> {
) -> Result<(Vec<Option<Box<[u8]>>>, u64), std::io::Error> {
let mut bytes = TransactionReceipt(transactions, outgoing_receipts.clone()).try_to_vec()?;

let mut parts = vec![];
Expand All @@ -1076,50 +1076,31 @@ impl EncodedShardChunk {
for _ in data_parts..total_parts {
parts.push(None);
}

let (new_chunk, merkle_paths) = Self::from_parts_and_metadata(
prev_block_hash,
prev_state_root,
outcome_root,
height,
shard_id,
gas_used,
gas_limit,
balance_burnt,
outgoing_receipts_root,
tx_root,
validator_proposals,
encoded_length as u64,
parts,
rs,
signer,
protocol_version,
);
Ok((new_chunk, merkle_paths))
Ok((parts, encoded_length as u64))
}

pub fn from_parts_and_metadata(
pub fn new(
prev_block_hash: CryptoHash,
prev_state_root: StateRoot,
outcome_root: CryptoHash,
height: BlockHeight,
shard_id: ShardId,
rs: &mut ReedSolomonWrapper,
gas_used: Gas,
gas_limit: Gas,
balance_burnt: Balance,
outgoing_receipts_root: CryptoHash,
tx_root: CryptoHash,
validator_proposals: Vec<ValidatorStake>,

encoded_length: u64,
parts: Vec<Option<Box<[u8]>>>,

rs: &mut ReedSolomonWrapper,

transactions: Vec<SignedTransaction>,
outgoing_receipts: &Vec<Receipt>,
outgoing_receipts_root: CryptoHash,
signer: &dyn ValidatorSigner,
protocol_version: ProtocolVersion,
) -> (Self, Vec<MerklePath>) {
let mut content = EncodedShardChunkBody { parts };
) -> Result<(Self, Vec<MerklePath>), std::io::Error> {
let (transaction_receipts_parts, encoded_length) =
Self::encode_transaction_receipts(rs, transactions, outgoing_receipts)?;

let mut content = EncodedShardChunkBody { parts: transaction_receipts_parts };
content.reconstruct(rs).unwrap();
let (encoded_merkle_root, merkle_paths) = content.get_merkle_hash_and_paths();

Expand Down Expand Up @@ -1149,7 +1130,7 @@ impl EncodedShardChunk {
signer,
);
let chunk = EncodedShardChunkV1 { header, content };
(Self::V1(chunk), merkle_paths)
Ok((Self::V1(chunk), merkle_paths))
} else if block_header_v3_version.is_none()
|| protocol_version < block_header_v3_version.unwrap()
{
Expand All @@ -1173,7 +1154,7 @@ impl EncodedShardChunk {
signer,
);
let chunk = EncodedShardChunkV2 { header: ShardChunkHeader::V2(header), content };
(Self::V2(chunk), merkle_paths)
Ok((Self::V2(chunk), merkle_paths))
} else {
#[cfg(not(feature = "protocol_feature_block_header_v3"))]
unreachable!();
Expand All @@ -1196,7 +1177,7 @@ impl EncodedShardChunk {
signer,
);
let chunk = EncodedShardChunkV2 { header: ShardChunkHeader::V3(header), content };
(Self::V2(chunk), merkle_paths)
Ok((Self::V2(chunk), merkle_paths))
}
}
}
Expand Down Expand Up @@ -1260,20 +1241,6 @@ impl EncodedShardChunk {
PartialEncodedChunkWithArcReceipts { header, parts, receipts }
}

fn create_transaction_receipts(
parts: &[Option<Box<[u8]>>],
encoded_length: u64,
) -> Result<TransactionReceipt, std::io::Error> {
let encoded_data = parts
.iter()
.flat_map(|option| option.as_ref().expect("Missing shard").iter())
.cloned()
.take(encoded_length as usize)
.collect::<Vec<u8>>();

TransactionReceipt::try_from_slice(&encoded_data)
}

pub fn decode_chunk(&self, data_parts: usize) -> Result<ShardChunk, std::io::Error> {
let parts = match self {
Self::V1(chunk) => &chunk.content.parts[0..data_parts],
Expand All @@ -1284,7 +1251,7 @@ impl EncodedShardChunk {
Self::V2(chunk) => chunk.header.encoded_length(),
};

let transaction_receipts = Self::create_transaction_receipts(parts, encoded_length)?;
let transaction_receipts = Self::decode_transaction_receipts(parts, encoded_length)?;

match self {
Self::V1(chunk) => Ok(ShardChunk::V1(ShardChunkV1 {
Expand Down

0 comments on commit afac695

Please sign in to comment.