Sitelet https://github.com/r55-eth/r55/pull/19
Skip to content

Crude gas metering - #19

Merged
leonardoalt merged 5 commits into
r55-eth:mainfrom
0xRampey:gas-meter
Dec 2, 2024
Merged

leonardoalt merged 5 commits into
r55-eth:mainfrom
0xRampey:gas-meter

Conversation

@0xRampey

Copy link
Copy Markdown
Contributor

explores #17

Some notes:

  • ABI decoding tx calldata from bytes to Rust types is the most expensive operation, running into millions of RISC-V opcodes executed.
  • An ERC20 transfer takes 19K gas with the current gas model (incl sload, sstore)
  • mint => 11.4K, balance => 6K, approve => 11.2K

Comment thread r55/src/exec.rs Outdated
Comment thread r55/src/exec.rs Outdated
Comment thread r55/src/exec.rs
Comment on lines +189 to +201
println!(
"evm gas: {}, r55 gas: {}, total cost: {}",
evm_gas, r55_gas, total_cost
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just curious; which formatter are you using?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good ol cargo fmt

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

maybe log::info or debug instead of println?

Comment thread r55/src/exec.rs Outdated
Comment thread r55/src/exec.rs Outdated

@leonardoalt leonardoalt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice! I'd say we can almost merge as is. We should probably merge the changes into rvemu first?

Comment thread r55/src/exec.rs Outdated

// This is the minimum "gas used" to ABI decode 'empty' calldata into Rust type arguments. Real calldata will take more gas.
// Internalising this would focus gas metering more on the function logic
let base_cost = 9_175_538;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Whats base cost?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the minimum cost to ABI decode calldata to Rust types, from here
Perhaps base cost is not the best naming and it could be abi_decode_cost. I've subtracted it because it overshadows function execution cost and happens before it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

But do we need to count it separately here with a constant? Doesn't the decoding code also run in RISCV?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I counted it separately mainly because the decoding logic consumes significantly more cycles compared to the user function logic.
So every contract call incurs a minimum decoding cost of ~9M cycles + the actual cost of contract execution- which is in the thousands and negligible by comparison due to the difference in magnitude.

The decoding logic does run inside RISCV tho and if we want to factor it in, we probably have to come up with a reduction like 0.001 * r55_cost, to be able to properly add it to EVM gas costs.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Right, that's kinda crazy. Let's merge this like this and we can investigate later why ABI decoding takes so many cycles

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah! I think we're g2g with this PR then

@0xRampey

Copy link
Copy Markdown
Contributor Author

We should probably merge the changes into rvemu first?

Yup, waiting on that here: r55-eth/rvemu#1 or do you mean to upstream it?

@leonardoalt

Copy link
Copy Markdown
Collaborator

Yup, waiting on that here: lvella/rvemu#1 or do you mean to upstream it?

Ah right, I actually don't have write permissions there, I just asked Lucas to transfer the repo to r55-eth org.

@0xRampey

Copy link
Copy Markdown
Contributor Author

I just asked Lucas to transfer the repo to r55-eth org.

Sweet, thanks! I've pushed in the change for that.

@leonardoalt
leonardoalt merged commit 7e92cb6 into r55-eth:main Dec 2, 2024
@0xRampey
0xRampey deleted the gas-meter branch December 3, 2024 03:35
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