From fada29cc928dbe7dbf482aec443b8c36db2aa7da Mon Sep 17 00:00:00 2001 From: Dmitry Osipov Date: Mon, 4 Nov 2024 12:06:31 +0100 Subject: [PATCH] resolve comments --- ethexe/contracts/src/ScaleCodec.sol | 76 ++++--------------- ethexe/contracts/test/ScaleCodec/Bytes.t.sol | 2 +- ethexe/contracts/test/ScaleCodec/Int.t.sol | 2 +- .../contracts/test/ScaleCodec/Optional.t.sol | 6 +- ethexe/contracts/test/ScaleCodec/Result.t.sol | 20 ++--- ethexe/contracts/test/ScaleCodec/Uint.t.sol | 4 +- ethexe/contracts/test/ScaleCodec/Vec.t.sol | 4 +- 7 files changed, 31 insertions(+), 83 deletions(-) diff --git a/ethexe/contracts/src/ScaleCodec.sol b/ethexe/contracts/src/ScaleCodec.sol index 31afde3e5ff..48d9923ccec 100644 --- a/ethexe/contracts/src/ScaleCodec.sol +++ b/ethexe/contracts/src/ScaleCodec.sol @@ -16,9 +16,9 @@ function bytesToUint(bytes memory data, uint256 byteLength, uint256 offset) pure } library ScaleCodec { - struct CompactInt { + struct CompactUint256 { uint256 value; - uint256 offset; + uint8 offset; } struct DecodedString { @@ -26,14 +26,13 @@ library ScaleCodec { uint256 offset; } - struct Optional { + struct Option { bool isSome; bytes value; } struct Result { bool isOk; - bool isErr; bytes value; } @@ -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)) @@ -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 { @@ -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 { @@ -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); } } @@ -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; @@ -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; @@ -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)); } } @@ -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); } } } diff --git a/ethexe/contracts/test/ScaleCodec/Bytes.t.sol b/ethexe/contracts/test/ScaleCodec/Bytes.t.sol index f56507eba04..2ce14bdde81 100644 --- a/ethexe/contracts/test/ScaleCodec/Bytes.t.sol +++ b/ethexe/contracts/test/ScaleCodec/Bytes.t.sol @@ -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"); } } diff --git a/ethexe/contracts/test/ScaleCodec/Int.t.sol b/ethexe/contracts/test/ScaleCodec/Int.t.sol index 10c462c8f2b..03a774271eb 100644 --- a/ethexe/contracts/test/ScaleCodec/Int.t.sol +++ b/ethexe/contracts/test/ScaleCodec/Int.t.sol @@ -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)); diff --git a/ethexe/contracts/test/ScaleCodec/Optional.t.sol b/ethexe/contracts/test/ScaleCodec/Optional.t.sol index 9b45cdbb35f..8804c9612ca 100644 --- a/ethexe/contracts/test/ScaleCodec/Optional.t.sol +++ b/ethexe/contracts/test/ScaleCodec/Optional.t.sol @@ -11,8 +11,8 @@ 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) }) @@ -20,7 +20,7 @@ contract TestOptionalScaleCodec is Test { } 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, diff --git a/ethexe/contracts/test/ScaleCodec/Result.t.sol b/ethexe/contracts/test/ScaleCodec/Result.t.sol index 179d3d606e0..3cebefdf788 100644 --- a/ethexe/contracts/test/ScaleCodec/Result.t.sol +++ b/ethexe/contracts/test/ScaleCodec/Result.t.sol @@ -7,20 +7,15 @@ 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)})); } } @@ -28,15 +23,14 @@ contract TestResultScaleCodec is Test { 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"); @@ -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); } } diff --git a/ethexe/contracts/test/ScaleCodec/Uint.t.sol b/ethexe/contracts/test/ScaleCodec/Uint.t.sol index 631792ff1d8..5bbabb51ae5 100644 --- a/ethexe/contracts/test/ScaleCodec/Uint.t.sol +++ b/ethexe/contracts/test/ScaleCodec/Uint.t.sol @@ -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 @@ -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); diff --git a/ethexe/contracts/test/ScaleCodec/Vec.t.sol b/ethexe/contracts/test/ScaleCodec/Vec.t.sol index 255f71c17af..6743a0af511 100644 --- a/ethexe/contracts/test/ScaleCodec/Vec.t.sol +++ b/ethexe/contracts/test/ScaleCodec/Vec.t.sol @@ -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); @@ -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);