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

INFO-4: Code optimization/simplification #1021

Merged
merged 1 commit into from
Nov 27, 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
7 changes: 2 additions & 5 deletions contracts/src/BeefyClient.sol
Original file line number Diff line number Diff line change
Expand Up @@ -539,13 +539,10 @@ contract BeefyClient {
}

function encodeCommitmentPayload(PayloadItem[] calldata items) internal pure returns (bytes memory) {
bytes memory payload = ScaleCodec.checkedEncodeCompactU32(uint32(items.length));
bytes memory payload = ScaleCodec.checkedEncodeCompactU32(items.length);
for (uint256 i = 0; i < items.length; i++) {
payload = bytes.concat(
payload,
items[i].payloadID,
ScaleCodec.checkedEncodeCompactU32(uint32(items[i].data.length)),
items[i].data
payload, items[i].payloadID, ScaleCodec.checkedEncodeCompactU32(items[i].data.length), items[i].data
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was easier to read this when each item was on its own line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forge fmt reverts to this form.

);
}

Expand Down
50 changes: 8 additions & 42 deletions contracts/src/Verification.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,16 @@ library Verification {
accum = bytes.concat(accum, encodeDigestItem(digestItems[i]));
}
// Encode number of digest items, followed by encoded digest items
return bytes.concat(ScaleCodec.checkedEncodeCompactU32(uint32(digestItems.length)), accum);
return bytes.concat(ScaleCodec.checkedEncodeCompactU32(digestItems.length), accum);
}

function encodeDigestItem(DigestItem calldata digestItem) internal pure returns (bytes memory) {
if (digestItem.kind == DIGEST_ITEM_PRERUNTIME) {
if (
digestItem.kind == DIGEST_ITEM_PRERUNTIME || digestItem.kind == DIGEST_ITEM_CONSENSUS
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets have each test on its line if the formatter allows it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forge fmt does not allow it.

|| digestItem.kind == DIGEST_ITEM_SEAL
) {
return bytes.concat(
bytes1(uint8(DIGEST_ITEM_PRERUNTIME)),
digestItem.consensusEngineID,
ScaleCodec.checkedEncodeCompactU32(digestItem.data.length),
digestItem.data
);
} else if (digestItem.kind == DIGEST_ITEM_CONSENSUS) {
return bytes.concat(
bytes1(uint8(DIGEST_ITEM_CONSENSUS)),
digestItem.consensusEngineID,
ScaleCodec.checkedEncodeCompactU32(digestItem.data.length),
digestItem.data
);
} else if (digestItem.kind == DIGEST_ITEM_SEAL) {
return bytes.concat(
bytes1(uint8(DIGEST_ITEM_SEAL)),
bytes1(uint8(digestItem.kind)),
digestItem.consensusEngineID,
ScaleCodec.checkedEncodeCompactU32(digestItem.data.length),
digestItem.data
Expand All @@ -193,30 +182,8 @@ library Verification {
pure
returns (bytes32)
{
// Encode Parachain header
bytes memory encodedHeader = bytes.concat(
// H256
header.parentHash,
// Compact unsigned int
ScaleCodec.checkedEncodeCompactU32(uint32(header.number)),
// H256
header.stateRoot,
// H256
header.extrinsicsRoot,
// Vec<DigestItem>
encodeDigestItems(header.digestItems)
);

// Hash of encoded parachain header merkle leaf
return keccak256(
bytes.concat(
// u32
encodedParaID,
// Vec<u8>
ScaleCodec.checkedEncodeCompactU32(encodedHeader.length),
encodedHeader
)
);
return keccak256(createParachainHeader(encodedParaID, header));
}

function createParachainHeader(bytes4 encodedParaID, ParachainHeader calldata header)
Expand All @@ -234,15 +201,14 @@ library Verification {
// H256
header.extrinsicsRoot,
// Vec<DigestItem>
ScaleCodec.checkedEncodeCompactU32(header.digestItems.length),
encodeDigestItems(header.digestItems)
);

return bytes.concat(
// u32
encodedParaID,
// length of encoded header
ScaleCodec.checkedEncodeCompactU32(uint32(encodedHeader.length)),
ScaleCodec.checkedEncodeCompactU32(encodedHeader.length),
encodedHeader
);
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/utils/Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ library Math {
*/
function saturatingSub(uint256 a, uint256 b) internal pure returns (uint256) {
unchecked {
if (b > a) {
if (b >= a) {
return 0;
}
return a - b;
Expand Down
Loading