-
Notifications
You must be signed in to change notification settings - Fork 105
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid merging
bytes memory result = new bytes(end - start); | ||
|
||
for (uint256 i = 0; i < end - start; i++) { | ||
result[i] = data[i + start]; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if its 0x02?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similar everywhere
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
No description provided.