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

feat: read fee params from diamond proxy storage #44

Merged
merged 4 commits into from
Aug 12, 2024
Merged

Conversation

0xnigir1
Copy link
Collaborator

@0xnigir1 0xnigir1 commented Aug 8, 2024

🤖 Linear

Closes ZKS-77

Description

Read feeParams from Diamond Proxy Storage

@0xnigir1 0xnigir1 requested review from 0xkenj1 and 0xyaco August 8, 2024 20:08
Copy link

linear bot commented Aug 8, 2024

ZKS-77 Fee Params

Use storage to get fee params: https://evm.storage/eth/20313953/0x32400084c286cf3e17e7b677ea9583e60a000324/s#map

AC:

  • getFeeParams successfully implemented using getStorageAt from EVMProviderService
  • Unit tests

@0xnigir1
Copy link
Collaborator Author

0xnigir1 commented Aug 8, 2024

I manually parsed the hex data from storage because I couldn't find a way to do it with Viem library tbh

0xyaco
0xyaco previously approved these changes Aug 9, 2024
Copy link
Collaborator

@0xyaco 0xyaco left a comment

Choose a reason for hiding this comment

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

Nice stuff, super minor details on the comments. Feel free to address them, it's good to go nevertheless!

@@ -7,6 +7,7 @@ import {
encodeFunctionData,
erc20Abi,
formatUnits,
Hex,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oooh didn't know viem had this type. A little bit more semantically correct for some values than Address

throw new L1MetricsServiceException("Failed to get fee params from L1.");
}

const strippedParamsData = feeParamsData.slice(2); // Remove the 0x prefix
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is nitpicking so feel free to ignore it but, what do you think about handling this with a regex replacing /^0x/ with an empty string instead of just doing slice(2)?

Assuming that this is not a performance intensive service, it might be better to specify that those two first characters we expect to slice are 0x and not something else, even if the return type is of Hex type. This way you are not "coupling" your implementation to EvmProviderService.getStorageAt as, right now, if for some reason the return type of that method is modified (or another return type is added), this method will keep doing its stuff with a potentially wrong value.

As an extra, you could remove the comment as the code would be self-explanatory.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

💯 i like the suggestion

let cursor = strippedParamsData.length;
const values: string[] = [];

for (const value of Object.values(feeParamsFieldLengths)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This one is a little bit similar to one of the comments in a previous PR about data structures + their ordering and it's totally fine to keep it as it is, although I'd strongly suggest leveraging an array for this part of the code, as the order —if I understand this loop correctly— is extremely important (ie unordered feeParamsFieldLenghts could generate wrong values).

Arrays are structures that inherently have a defined order (universally, in other programming languages too); and although newest standards force Object.values to keep the order the keys were declared with, it feels a little bit weaker to me to express the idea of "this must be ordered".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

you're totally right, i wanted to reflect the object "field"->size but it's better an array with sizes in the correct order and just clarify with a comment the reason behind

0xyaco
0xyaco previously approved these changes Aug 9, 2024
Copy link
Collaborator

@0xyaco 0xyaco left a comment

Choose a reason for hiding this comment

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

Good to go!

Comment on lines +1 to +22
//See: https://github.com/matter-labs/era-contracts/blob/8a70bbbc48125f5bde6189b4e3c6a3ee79631678/l1-contracts/contracts/state-transition/chain-deps/ZkSyncHyperchainStorage.sol#L52
export type FeeParams = {
pubdataPricingMode: number;
batchOverheadL1Gas: number;
maxPubdataPerBatch: number;
maxL2GasPerBatch: number;
priorityTxMaxPubdata: number;
minimalL2GasPrice?: bigint;
};

// Define the lengths for each field (in hex digits, each byte is 2 hex digits)
/*
{
pubdataPricingMode: uint8 -> 1 byte -> 2 hex digits
batchOverheadL1Gas: uint32 -> 4 bytes -> 8 hex digits
maxPubdataPerBatch: uint32 -> 4 bytes -> 8 hex digits
maxL2GasPerBatch: uint32 -> 4 bytes -> 8 hex digits
priorityTxMaxPubdata: uint32 -> 4 bytes -> 8 hex digits
minimalL2GasPrice: uint64 -> 8 bytes -> 16 hex digits
}
*/
export const feeParamsFieldHexDigits = [2, 8, 8, 8, 8, 16] as const;
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

@0xnigir1 0xnigir1 merged commit f59914a into dev Aug 12, 2024
6 checks passed
@0xnigir1 0xnigir1 deleted the feat/fee-params branch August 12, 2024 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants