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(ethexe): introduce ScaleCodec library for Solidity #4237

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

osipov-mit
Copy link
Member

No description provided.

@osipov-mit osipov-mit changed the title feat(ethex): introduce ScaleCodec library for Solidity feat(ethexe): introduce ScaleCodec library for Solidity Sep 16, 2024
Copy link
Member

@breathx breathx left a comment

Choose a reason for hiding this comment

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

To avoid merging

@breathx breathx added the A0-pleasereview PR is ready to be reviewed by the team label Nov 4, 2024
ethexe/contracts/src/ScaleCodec.sol Outdated Show resolved Hide resolved
ethexe/contracts/src/ScaleCodec.sol Outdated Show resolved Hide resolved
ethexe/contracts/src/ScaleCodec.sol Outdated Show resolved Hide resolved
ethexe/contracts/src/ScaleCodec.sol Outdated Show resolved Hide resolved
bytes memory result = new bytes(end - start);

for (uint256 i = 0; i < end - start; i++) {
result[i] = data[i + start];
Copy link
Member

Choose a reason for hiding this comment

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

why do you need to fill each slice/bytes etc?

Copy link
Member

Choose a reason for hiding this comment

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

everywhere

return result;
}

function concatBytes(bytes[] memory value) public pure returns (bytes memory) {
Copy link
Member

Choose a reason for hiding this comment

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

gpt suggests to pre-allocate resulting bytes memory here:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract ByteConcatenation {
    function concat(bytes[] memory data) public pure returns (bytes memory) {
        // Calculate total length
        uint totalLength = 0;
        for (uint i = 0; i < data.length; i++) {
            totalLength += data[i].length;
        }

        // Allocate memory for the result
        bytes memory result = new bytes(totalLength);
        uint offset = 0;

        // Copy each bytes element into the result
        for (uint i = 0; i < data.length; i++) {
            bytes memory currentBytes = data[i];
            uint currentLength = currentBytes.length;

            // Use inline assembly to copy memory
            assembly {
                // Copy currentBytes to result at the offset
                // The `mload` is used to get the length of currentBytes
                let src := add(currentBytes, 0x20) // skip the length
                let dest := add(result, add(0x20, offset)) // skip the length of result
                mcopy(dest, src, currentLength)
            }
            offset += currentLength;
        }

        return result;
    }

    // Helper function for memory copy
    function mcopy(uint dest, uint src, uint length) internal pure {
        for (uint i = 0; i < length; i++) {
            assembly {
                mstore8(add(dest, i), mload8(add(src, i)))
            }
        }
    }
}

}

function decodeBool(bytes memory _bytes, uint256 offset) public pure returns (bool) {
return _bytes[offset] == 0x01;
Copy link
Member

Choose a reason for hiding this comment

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

what if its 0x02?

Copy link
Member

Choose a reason for hiding this comment

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

you should return result here maybe
for optimised code impl decodeBoolUnchecked

Copy link
Member

Choose a reason for hiding this comment

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

similar everywhere

Copy link
Member Author

Choose a reason for hiding this comment

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

Since there is no native Result type in Solidity I think it's not the best idea.
Maybe calling revert is better if the bytes are wrong?

return int16(decodeUint16(_bytes, offset));
}

function encodeUint32(uint32 value) public pure returns (bytes memory) {
Copy link
Member

Choose a reason for hiding this comment

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

do you always need bytes?
seems that uint32 value has zero cast to bytes4 - so it could be also useful for future usage?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually encodeUint32To function is supposed to be used most of the time (at least in contracts generated by sails). And here I belief bytes is better cuz you probably need to combine those bytes with some others and you can't do it if you have type bytes4. And even if you don't need to combine them you need to pass those bytes to the sendMessage function which takes bytes type

ethexe/contracts/src/ScaleCodec.sol Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A0-pleasereview PR is ready to be reviewed by the team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants