Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
osipov-mit committed Nov 4, 2024
1 parent d7f0fe6 commit fada29c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 83 deletions.
76 changes: 15 additions & 61 deletions ethexe/contracts/src/ScaleCodec.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,23 @@ function bytesToUint(bytes memory data, uint256 byteLength, uint256 offset) pure
}

library ScaleCodec {
struct CompactInt {
struct CompactUint256 {
uint256 value;
uint256 offset;
uint8 offset;
}

struct DecodedString {
string value;
uint256 offset;
}

struct Optional {
struct Option {
bool isSome;
bytes value;
}

struct Result {
bool isOk;
bool isErr;
bytes value;
}

Expand Down Expand Up @@ -61,51 +60,6 @@ library ScaleCodec {
return res;
}

function bytes1Tobytes(bytes1 value) public pure returns (bytes memory) {
bytes memory result = new bytes(1);
result[0] = value;
return result;
}

function bytes2Tobytes(bytes2 value) public pure returns (bytes memory) {
bytes memory result = new bytes(2);
result[0] = value[0];
result[1] = value[1];
return result;
}

function bytes4Tobytes(bytes4 value) public pure returns (bytes memory) {
bytes memory result = new bytes(4);
for (uint256 i = 0; i < 4; i++) {
result[i] = value[i];
}
return result;
}

function bytes8Tobytes(bytes8 value) public pure returns (bytes memory) {
bytes memory result = new bytes(8);
for (uint256 i = 0; i < 8; i++) {
result[i] = value[i];
}
return result;
}

function bytes16Tobytes(bytes16 value) public pure returns (bytes memory) {
bytes memory result = new bytes(16);
for (uint256 i = 0; i < 16; i++) {
result[i] = value[i];
}
return result;
}

function bytes32Tobytes(bytes32 value) public pure returns (bytes memory) {
bytes memory result = new bytes(32);
for (uint256 i = 0; i < 32; i++) {
result[i] = value[i];
}
return result;
}

function bytesToBytes32(bytes memory value, uint256 offset) public pure returns (bytes32 result) {
assembly {
result := mload(add(add(value, 0x20), offset))
Expand Down Expand Up @@ -378,11 +332,11 @@ library ScaleCodec {
}
}

function decodeCompactInt(bytes memory _bytes, uint256 offset) public pure returns (CompactInt memory) {
function decodeCompactInt(bytes memory _bytes, uint256 offset) public pure returns (CompactUint256 memory) {
uint8 mode = uint8(_bytes[offset]) & 0x03;

if (mode == 0x00) {
return CompactInt(uint8(_bytes[offset]) >> 2, 1);
return CompactUint256(uint8(_bytes[offset]) >> 2, 1);
} else if (mode == 0x01) {
uint16 value;
assembly {
Expand All @@ -392,7 +346,7 @@ library ScaleCodec {
v := byte(0, mload(src_ptr))
value := shr(2, or(value, v))
}
return CompactInt(value, 2);
return CompactUint256(value, 2);
} else if (mode == 0x02) {
uint32 value;
assembly {
Expand All @@ -404,13 +358,13 @@ library ScaleCodec {
let v := byte(0, mload(src_ptr))
value := shr(2, or(value, v))
}
return CompactInt(value, 4);
return CompactUint256(value, 4);
} else {
uint8 bytesLen = (uint8(_bytes[offset]) >> 2) + 4;

uint256 value = bytesToUint(_bytes, bytesLen, offset + 1);

return CompactInt(value, bytesLen + 1);
return CompactUint256(value, bytesLen + 1);
}
}

Expand Down Expand Up @@ -442,7 +396,7 @@ library ScaleCodec {
}

function decodeString(bytes memory _bytes, uint256 offset) public pure returns (DecodedString memory) {
CompactInt memory len = decodeCompactInt(_bytes, offset);
CompactUint256 memory len = decodeCompactInt(_bytes, offset);

offset += len.offset;

Expand All @@ -463,7 +417,7 @@ library ScaleCodec {
return DecodedString(string(result), offset);
}

function encodeOptional(Optional memory value) public pure returns (bytes memory) {
function encodeOption(Option memory value) public pure returns (bytes memory) {
if (value.isSome) {
bytes memory result = new bytes(value.value.length + 1);
result[0] = 0x01;
Expand All @@ -481,11 +435,11 @@ library ScaleCodec {
}
}

function decodeOptional(bytes memory _bytes, uint256 offset) public pure returns (Optional memory) {
function decodeOption(bytes memory _bytes, uint256 offset) public pure returns (Option memory) {
if (_bytes[offset] == 0x00) {
return Optional(false, new bytes(0));
return Option(false, new bytes(0));
} else {
return Optional(true, sliceBytes(_bytes, 1 + offset, _bytes.length));
return Option(true, sliceBytes(_bytes, 1 + offset, _bytes.length));
}
}

Expand All @@ -500,9 +454,9 @@ library ScaleCodec {
function decodeResult(bytes memory _bytes, uint256 offset) public pure returns (Result memory) {
bytes memory value = sliceBytes(_bytes, 1 + offset, _bytes.length);
if (_bytes[offset] == 0x00) {
return Result(true, false, value);
return Result(true, value);
} else {
return Result(false, true, value);
return Result(false, value);
}
}
}
2 changes: 1 addition & 1 deletion ethexe/contracts/test/ScaleCodec/Bytes.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ contract TestBytesScaleCodec is Test {
function test_insertBytes20() public pure {
bytes memory _bytes = new bytes(21);
_bytes[0] = 0x05;
ScaleCodec.insertBytes32To(hex"0102030401020304010203040102030401020304", _bytes, 1);
ScaleCodec.insertBytes20To(hex"0102030401020304010203040102030401020304", _bytes, 1);
assertEq(_bytes, hex"050102030401020304010203040102030401020304");
}
}
2 changes: 1 addition & 1 deletion ethexe/contracts/test/ScaleCodec/Int.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract TestIntScaleCodec is Test {
assertEq(ScaleCodec.decodeInt8(hex"01bb", 1), int8(-69));
}

function test_int16EncodeDecode() public {
function test_int16EncodeDecode() public pure {
assertEq(ScaleCodec.encodeInt16(int16(42)), hex"2a00");
assertEq(ScaleCodec.decodeInt16(hex"2a00", 0), int16(42));

Expand Down
6 changes: 3 additions & 3 deletions ethexe/contracts/test/ScaleCodec/Optional.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ contract TestOptionalScaleCodec is Test {
}

function encodeOptionalString(OptionalString memory _value) internal pure returns (bytes memory) {
return ScaleCodec.encodeOptional(
ScaleCodec.Optional({
return ScaleCodec.encodeOption(
ScaleCodec.Option({
isSome: _value.isSome,
value: _value.isSome ? ScaleCodec.encodeString(_value.value) : new bytes(0)
})
);
}

function decodeOptionalString(bytes memory _bytes) internal pure returns (OptionalString memory) {
ScaleCodec.Optional memory decoded = ScaleCodec.decodeOptional(_bytes, 0);
ScaleCodec.Option memory decoded = ScaleCodec.decodeOption(_bytes, 0);

return OptionalString({
isSome: decoded.isSome,
Expand Down
20 changes: 7 additions & 13 deletions ethexe/contracts/test/ScaleCodec/Result.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,30 @@ import "forge-std/Test.sol";
contract TestResultScaleCodec is Test {
struct ResultStringU8 {
bool isOk;
bool isErr;
string ok;
uint8 err;
}

function encodeResultStringU8(ResultStringU8 memory _value) internal pure returns (bytes memory) {
if (_value.isOk) {
return ScaleCodec.encodeResult(
ScaleCodec.Result({isOk: true, isErr: false, value: ScaleCodec.encodeString(_value.ok)})
);
return ScaleCodec.encodeResult(ScaleCodec.Result({isOk: true, value: ScaleCodec.encodeString(_value.ok)}));
} else {
return ScaleCodec.encodeResult(
ScaleCodec.Result({isOk: false, isErr: true, value: ScaleCodec.encodeUint8(_value.err)})
);
return ScaleCodec.encodeResult(ScaleCodec.Result({isOk: false, value: ScaleCodec.encodeUint8(_value.err)}));
}
}

function decodeResultStringU8(bytes memory _value) public pure returns (ResultStringU8 memory) {
ScaleCodec.Result memory decoded = ScaleCodec.decodeResult(_value, 0);

if (decoded.isOk) {
return
ResultStringU8({isOk: true, isErr: false, ok: ScaleCodec.decodeString(decoded.value, 0).value, err: 0});
return ResultStringU8({isOk: true, ok: ScaleCodec.decodeString(decoded.value, 0).value, err: 0});
} else {
return ResultStringU8({isOk: false, isErr: true, ok: "", err: ScaleCodec.decodeUint8(decoded.value, 0)});
return ResultStringU8({isOk: false, ok: "", err: ScaleCodec.decodeUint8(decoded.value, 0)});
}
}

function test_ResultOkEncodeDecode() public pure {
ResultStringU8 memory _result = ResultStringU8({isOk: true, isErr: false, ok: "Gear", err: 0});
ResultStringU8 memory _result = ResultStringU8({isOk: true, ok: "Gear", err: 0});

assertEq(encodeResultStringU8(_result), hex"001047656172");

Expand All @@ -47,13 +41,13 @@ contract TestResultScaleCodec is Test {
}

function test_ResultErrEncodeDecode() public pure {
ResultStringU8 memory _result = ResultStringU8({isOk: false, isErr: true, ok: "", err: 1});
ResultStringU8 memory _result = ResultStringU8({isOk: false, ok: "", err: 1});

assertEq(encodeResultStringU8(_result), hex"0101");

ResultStringU8 memory _decoded = decodeResultStringU8(hex"0101");

assertEq(_decoded.isErr, true);
assertEq(_decoded.isOk, false);
assertEq(_decoded.err, 1);
}
}
4 changes: 2 additions & 2 deletions ethexe/contracts/test/ScaleCodec/Uint.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract TestUintScaleCodec is Test {
assertEq(ScaleCodec.decodeUint8(hex"0145", 1), uint8(69));
}

function test_uint16Encode() public {
function test_uint16Encode() public pure {
assertEq(ScaleCodec.encodeUint16(uint16(42)), hex"2a00");

// Encode to
Expand Down Expand Up @@ -137,7 +137,7 @@ contract TestUintScaleCodec is Test {
assertEq(ScaleCodec.decodeCompactInt(hex"feff0300", 0).value, 65535);
assertEq(ScaleCodec.decodeCompactInt(hex"0b00407a10f35a", 0).value, 100000000000000);

ScaleCodec.CompactInt memory value = ScaleCodec.decodeCompactInt(hex"010b00407a10f35a", 1);
ScaleCodec.CompactUint256 memory value = ScaleCodec.decodeCompactInt(hex"010b00407a10f35a", 1);
assertEq(value.value, 100000000000000);
assertEq(value.offset, 7);

Expand Down
4 changes: 2 additions & 2 deletions ethexe/contracts/test/ScaleCodec/Vec.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ contract TestVecScaleCodec is Test {
function test_decodeVecUint8() public pure {
bytes memory data = hex"0c010203";

ScaleCodec.CompactInt memory vecLen = ScaleCodec.decodeCompactInt(data, 0);
ScaleCodec.CompactUint256 memory vecLen = ScaleCodec.decodeCompactInt(data, 0);
uint256 offset = vecLen.offset;

uint8[] memory vec = new uint8[](vecLen.value);
Expand All @@ -85,7 +85,7 @@ contract TestVecScaleCodec is Test {
function test_decodeVecString() public pure {
bytes memory data = hex"081468656c6c6f14776f726c64";

ScaleCodec.CompactInt memory vecLen = ScaleCodec.decodeCompactInt(data, 0);
ScaleCodec.CompactUint256 memory vecLen = ScaleCodec.decodeCompactInt(data, 0);
uint256 offset = vecLen.offset;

string[] memory vec = new string[](vecLen.value);
Expand Down

0 comments on commit fada29c

Please sign in to comment.