Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Simplify predicate and script #19

Conversation

pixelcircuits
Copy link
Collaborator

No description provided.

@pixelcircuits pixelcircuits self-assigned this Mar 4, 2023
@pixelcircuits pixelcircuits linked an issue Mar 4, 2023 that may be closed by this pull request
Copy link
Contributor

@Braqzen Braqzen left a comment

Choose a reason for hiding this comment

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

I don't really know what I'm looking at when it comes to predicates.

contract-message-predicate/Forc.toml Show resolved Hide resolved
contract-message-predicate/tests/harness.rs Outdated Show resolved Hide resolved
@pixelcircuits
Copy link
Collaborator Author

I don't really know what I'm looking at when it comes to predicates.

I'll definitely be scheduling a meeting to go over this. A LOT is changed in this repo now

Copy link

@nfurfaro nfurfaro left a comment

Choose a reason for hiding this comment

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

Just a few comments, looking good overall though.

README.md Outdated Show resolved Hide resolved
contract-message-predicate/src/contract_message_test.sw Outdated Show resolved Hide resolved
}

// Get the data of a message input
pub fn input_message_data<T>(index: u64, offset: u64) -> T {
Copy link

Choose a reason for hiding this comment

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

Same with this one, though it now returns a Bytes type. I refactored the Bridge-fungible-token contract to handle the new return type already here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How can I convert the Bytes to a u64?

Copy link

Choose a reason for hiding this comment

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

Hmm, we'd have to build a local conversion function based on the into() method for Bytes that converts to b256.
Ideally we would just impl TryInto () for Bytes, but we don't have that trait yet (blocked).

Copy link

@nfurfaro nfurfaro Mar 10, 2023

Choose a reason for hiding this comment

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

@pixelcircuits this is what I was refering to here:

// NOTE: this cas be lossy! Added here as the From trait currently requires it,
    // but the conversion from `Bytes` ->`b256` should be implemented as
    // `impl TryFrom<Bytes> for b256` when the `TryFrom` trait lands:
    // https://github.com/FuelLabs/sway/pull/3881
    fn into(self) -> b256 {
        let mut value = 0x0000000000000000000000000000000000000000000000000000000000000000;
        let ptr = __addr_of(value);
        self.buf.ptr().copy_to::<b256>(ptr, 1);

        value
    }

I think implementing something similar as a free function, i.e: fn into_u64(b: Bytes) -> u64; is the way to go for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Bytes to b256 is already supported for into see here. Do you mean an into for Bytes to u64? I added a helper function to do just that, but were should it eventually get included in the sway std lib? The conversion to b256 is in the b256.sw file

Choose a reason for hiding this comment

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

Eventually it should be added via implementing TryInto for u64, but this can't be done yet. The workaround is to impl From for Bytes and use the into()method, but this is also blocked atm.

@pixelcircuits pixelcircuits marked this pull request as ready for review March 11, 2023 03:32
.github/workflows/ci.yml Show resolved Hide resolved
contract-message-predicate/src/contract_message_test.sw Outdated Show resolved Hide resolved
contract-message-predicate/src/lib.rs Outdated Show resolved Hide resolved
contract-message-predicate/src/main.rs Outdated Show resolved Hide resolved
use std::collections::HashMap;

use fuel_core_interfaces::common::prelude::Word;
use fuels::prelude::*;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would import explicitly i.e. comment out, build, see what it complains about and then add it back in manually.

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 tend to agree, but doesn't that defeat the whole reason we have the prelude?

contract-message-predicate/tests/utils/builder.rs Outdated Show resolved Hide resolved
Braqzen
Braqzen previously approved these changes Mar 13, 2023
Copy link
Contributor

@Braqzen Braqzen left a comment

Choose a reason for hiding this comment

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

Uncertain about the new codes but looks fine otherwise

contract-message-predicate/src/predicate_asm.rs Outdated Show resolved Hide resolved
//referenced data (expected script hash)
//00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
]
.into_iter()
Copy link

Choose a reason for hiding this comment

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

I'll put some comments here below so not to pollute the script itself.

  • In general it'd be great to have those jump destination 'addresses' (18, 16, 11) as constants as well, with names which match the comment before them. And if possible, to do a check after collecting them that each offset points the the instruction you expect it to. That might be overkill for a script which doesn't change often, but a thought. The 19 could be verified very easily too.
  • The addi(EXPECTED_INPUT_TYPE, ZERO, INPUT_MESSAGE_TYPE) could just be a movi. You might've copied that from the compiler output. We had ADDI before MOVI and some parts of the compiler haven't been updated.
  • Similarly the op::jnei(INPUT_MSG_DATA_LEN, ZERO, 18) could be jnzi.
  • You're assuming that INPUT_INDEX starts at at least 2, although I guess if it's 1 then it'll be checking the first input which will never have zero length? And if it's 0 then the subi will underflow.
  • I've been trying to think of a way to shorten it by optimising the control flow but I don't think there's a way to remove any more instructions. 👍 Although I did just notice that you could use the heap instead of the stack for the script hash buffer, which, once the new ALOC semantics are merged, would mean you could just use $hp instead of SCRIPT_HASH_PTR and that would remove one move instruction. 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great thinking with using the heap vs the stack!

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 spoke too soon... there is no immediate version of ALOC like there is CFEI so it would require at least 2 instructions to increase the heap.

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 moved the jump instruction values to constants since some of them are referenced more than once. We could add a check for things like the value for the end of the program code before adding referenced data bits, but we effectively already are indirectly through the tests (particularly the test where everything has to work correctly).

Copy link

@nfurfaro nfurfaro Mar 14, 2023

Choose a reason for hiding this comment

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

once the new ALOC semantics are merged

Using the heap may be an optimization that has to wait, not sure when it will land.

Using jnzi sounds like an easy win though.
I don't have anything to add; @otrho was too thorough :)

Copy link

Choose a reason for hiding this comment

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

I spoke too soon... there is no immediate version of ALOC like there is CFEI so it would require at least 2 instructions to increase the heap.

Ahh, yeah. Bugger. OK, I think it has been golfed all the way.

contract-message-predicate/src/script_asm.rs Show resolved Hide resolved
otrho
otrho previously approved these changes Mar 14, 2023
Copy link

@otrho otrho left a comment

Choose a reason for hiding this comment

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

Happy with the ASM blocks.

These tests ensure the bytecode won't change, and also provides an easy
way to reference the serialized bytecode.
@pixelcircuits pixelcircuits merged commit 1c95e83 into master Mar 16, 2023
2 checks passed
@pixelcircuits pixelcircuits deleted the 18-update-message-to-contract-predicate-with-new-message-retry-spec branch March 16, 2023 02:57
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update message to contract predicate with new message retry spec
5 participants