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

Include size lookup of contract for computing LDC opcode cost #598

Merged
merged 35 commits into from
Oct 11, 2023

Conversation

MitchTurner
Copy link
Member

@MitchTurner MitchTurner marked this pull request as ready for review October 5, 2023 01:14
fuel-vm/src/interpreter/blockchain.rs Outdated Show resolved Hide resolved
fuel-vm/src/interpreter/executors/instruction.rs Outdated Show resolved Hide resolved
fuel-vm/src/tests/blockchain.rs Show resolved Hide resolved
@MitchTurner
Copy link
Member Author

It looks like the storage allows getting the contract bytes for a specified buffer, so we only copy what is specified with $rC now, not the entire contract :).

I've added a test that copies a contract with a bunch of noops and checks the costs based on that.

Still might clean up these tests a bit. There is some DRYing I can do before merging this.

fuel-vm/src/interpreter/blockchain.rs Outdated Show resolved Hide resolved
fuel-vm/src/interpreter/executors/instruction.rs Outdated Show resolved Hide resolved
fuel-vm/src/interpreter/contract.rs Outdated Show resolved Hide resolved
fuel-vm/src/interpreter/contract.rs Outdated Show resolved Hide resolved
fuel-vm/src/tests/blockchain.rs Outdated Show resolved Hide resolved
fuel-vm/src/tests/blockchain.rs Outdated Show resolved Hide resolved
@MitchTurner MitchTurner requested a review from a team October 6, 2023 17:54
Comment on lines 586 to 588
op::move_(reg_a, RegId::HP), // r[a] := $hp
op::ldc(0x10, RegId::ZERO, reg_a), /* Load first two words from the
* contract */
Copy link
Member

Choose a reason for hiding this comment

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

The comment is incorrect here. This loads first $hp bytes from the contract. The code should likely read like this:

Suggested change
op::move_(reg_a, RegId::HP), // r[a] := $hp
op::ldc(0x10, RegId::ZERO, reg_a), /* Load first two words from the
* contract */
op::ldc(0x10, RegId::ZERO, RegId::HP),

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. I think a lot of these are wrong, but I don't really understand them enough to fix them. I'm just copying what was already there.

Might be worth fixing all of them, maybe in another PR.

Comment on lines 568 to 569
op::ldc(0x10, RegId::ZERO, RegId::ZERO), /* Load first two words from *
* the contract */
Copy link
Member

Choose a reason for hiding this comment

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

This loads zero words from the contract, so the comment is incorrect. I suggest loading a single byte anyway, since this test shouldn't be hitting the zero-length-load corner case.

Suggested change
op::ldc(0x10, RegId::ZERO, RegId::ZERO), /* Load first two words from *
* the contract */
op::ldc(0x10, RegId::ZERO, RegId::ONE),

Comment on lines 429 to 432
op::move_(reg_a, RegId::SSP), // r[a] := $ssp
op::subi(reg_a, reg_a, 16), // r[a] -= 16 (start of the loaded code)
op::xor(reg_b, reg_b, reg_b), // r[b] := 0
op::addi(reg_b, reg_b, 16), // r[b] += 16 (length of the loaded code)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
op::move_(reg_a, RegId::SSP), // r[a] := $ssp
op::subi(reg_a, reg_a, 16), // r[a] -= 16 (start of the loaded code)
op::xor(reg_b, reg_b, reg_b), // r[b] := 0
op::addi(reg_b, reg_b, 16), // r[b] += 16 (length of the loaded code)
op::subi(reg_a, RegId::SSP, 16), // r[a] := $ssp - 16 (start of the loaded code)
op::movi(reg_b, 16), // r[b] = 16 (length of the loaded code)

@Dentosal Dentosal added breaking A breaking api change fuel-vm Related to the `fuel-vm` crate. labels Oct 7, 2023
.into_owned()
.into();
let contract_len = contract_bytes.len();
let contract_sub_bytes =
Copy link
Collaborator

Choose a reason for hiding this comment

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

I thought it was already handled by copy_from_slice_zero_fill_noownerchecks.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. And reverting to using this fixes the test bug.

super::contract::contract(self.storage, &contract_id)?
.into_owned()
.into();
let contract_len = contract_bytes.len();
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be nice to charge the user here before we do the main work.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll look into it. This would be quite the architectural change, I think, but if it's how it needs to work, then I'll make it work.

Copy link
Member Author

Choose a reason for hiding this comment

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

K. I split the function in half to return the bytes and then copy them separately. I don't love the solution becuase the functions are doing too much, but it's probably better :)

fuel-vm/src/tests/blockchain.rs Outdated Show resolved Hide resolved
fuel-vm/src/tests/blockchain.rs Outdated Show resolved Hide resolved
fuel-vm/src/tests/blockchain.rs Show resolved Hide resolved
@MitchTurner MitchTurner requested a review from a team October 9, 2023 19:21
xgreenx
xgreenx previously approved these changes Oct 10, 2023
Copy link
Collaborator

@xgreenx xgreenx left a comment

Choose a reason for hiding this comment

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

@MitchTurner I moved charging for the contract size into the load_contract_code. It works in the same way as for code_size.

If you are okay with it, then you can merge the PR=)

Comment on lines 510 to 514
op::ldc(reg_a, reg_b, reg_c), // Load first two words from the contract
op::subi(reg_a, RegId::SSP, 16), /* r[a] := $ssp - 16 (start of the loaded
* code) */
op::movi(reg_c, 16), // r[b] = 16 (length of the loaded code)
op::logd(RegId::ZERO, RegId::ZERO, reg_a, reg_c), /* Log digest of the
Copy link
Contributor

@bvrooman bvrooman Oct 10, 2023

Choose a reason for hiding this comment

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

It looks like you're loading len bytes from the contract, given that len is stored in reg_c. In the tests, you pass 0 for len, but the following opcodes hardcode 16. If I understand correctly, ldc still loads the whole contract regardless of length/$rC, but I think the subi, movi, and logd opcodes here become incorrect. Are they even necessary for these tests? Maybe we can remove them.

Copy link
Member Author

Choose a reason for hiding this comment

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

It fails the ldc__load_external_contract_code test without it.

But you're completely right. I think it just needs to be parameterized for the len. It just so happens that the padded length for that test is 16 🤡

xgreenx
xgreenx previously approved these changes Oct 11, 2023
Comment on lines 522 to 525
op::subi(reg_a, RegId::SSP, padded_len), /* r[a] := $ssp - 16 (start of
* the loaded
* code) */
op::movi(reg_c, padded_len as u32), // r[b] = 16 (length of the loaded code)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe you want to update a comment regarding 16

@MitchTurner MitchTurner added this pull request to the merge queue Oct 11, 2023
Merged via the queue into master with commit 36bf42e Oct 11, 2023
36 checks passed
@MitchTurner MitchTurner deleted the fix-ldc-cost branch October 11, 2023 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking A breaking api change fuel-vm Related to the `fuel-vm` crate.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants