From 58dc8d0ae2da89e6b82e80c2da4870c07534a2bc Mon Sep 17 00:00:00 2001 From: Pedro Lino Date: Fri, 22 Dec 2023 18:23:48 +0000 Subject: [PATCH 1/3] Rename vars in CBORDataStructures to prevent shadowing --- .../components/CBORDataStructures.sol | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/packages/solidity-cbor/contracts/components/CBORDataStructures.sol b/packages/solidity-cbor/contracts/components/CBORDataStructures.sol index d39d4fc..0ba99ed 100644 --- a/packages/solidity-cbor/contracts/components/CBORDataStructures.sol +++ b/packages/solidity-cbor/contracts/components/CBORDataStructures.sol @@ -16,22 +16,19 @@ library CBORDataStructures { /** * @dev Parses a CBOR-encoded mapping into a 2d-array of bytes. * @param encoding the dynamic bytes array to scan - * @param cursor position where mapping starts (in bytes) - * @param shortCount short data identifier included in field info + * @param mappingCursor position where mapping starts (in bytes) + * @param initialShortCount short data identifier included in field info * @return decodedMapping the mapping decoded */ function expandMapping( bytes memory encoding, - uint cursor, - uint8 shortCount + uint mappingCursor, + uint8 initialShortCount ) internal view returns ( bytes[2][] memory decodedMapping ) { - // Track our mapping start - uint256 mappingCursor = cursor; - // Count up how many keys we have, set cursor - (uint totalItems, uint dataStart, ) = getDataStructureItemLength(encoding, mappingCursor, Spec.MajorType.Map, shortCount); + (uint totalItems, uint dataStart, ) = getDataStructureItemLength(encoding, mappingCursor, Spec.MajorType.Map, initialShortCount); require(totalItems % 2 == 0, "Invalid mapping provided!"); mappingCursor = dataStart; @@ -43,13 +40,13 @@ library CBORDataStructures { // Determine the array we're modifying uint arrayIdx = item % 2; // Alternates 0,1,0,1,... - uint pair = item / 2; // 0,0,1,1,2,2.. + uint pairIdx = item / 2; // 0,0,1,1,2,2.. // See what our field looks like - (Spec.MajorType majorType, uint8 shortCount, uint start, uint end, uint next) = Utils.parseField(encoding, mappingCursor); + (Spec.MajorType fieldMajorType, uint8 fieldShortCount, uint start, uint end, uint next) = Utils.parseField(encoding, mappingCursor); // Save our data - decodedMapping[pair][arrayIdx] = Utils.extractValue(encoding, majorType, shortCount, start, end); + decodedMapping[pairIdx][arrayIdx] = Utils.extractValue(encoding, fieldMajorType, fieldShortCount, start, end); // Update our cursor mappingCursor = next; @@ -62,22 +59,19 @@ library CBORDataStructures { /** * @dev Parses a CBOR-encoded array into an array of bytes. * @param encoding the dynamic bytes array to scan - * @param cursor position where array starts (in bytes) - * @param shortCount short data identifier included in field info + * @param arrayCursor position where array starts (in bytes) + * @param initialShortCount short data identifier included in field info * @return decodedArray the array decoded */ function expandArray( bytes memory encoding, - uint cursor, - uint8 shortCount + uint arrayCursor, + uint8 initialShortCount ) internal view returns ( bytes[] memory decodedArray ) { - // Track our array start - uint arrayCursor = cursor; - // Count up how many keys we have, set cursor - (uint totalItems, uint dataStart, ) = getDataStructureItemLength(encoding, arrayCursor, Spec.MajorType.Array, shortCount); + (uint totalItems, uint dataStart, ) = getDataStructureItemLength(encoding, arrayCursor, Spec.MajorType.Array, initialShortCount); arrayCursor = dataStart; // Allocate new array @@ -87,10 +81,10 @@ library CBORDataStructures { for (uint item = 0; item < totalItems; item++) { // See what our field looks like - (Spec.MajorType majorType, uint8 shortCount, uint start, uint end, uint next) = Utils.parseField(encoding, arrayCursor); + (Spec.MajorType fieldMajorType, uint8 fieldShortCount, uint start, uint end, uint next) = Utils.parseField(encoding, arrayCursor); // Save our data - decodedArray[item] = Utils.extractValue(encoding, majorType, shortCount, start, end); + decodedArray[item] = Utils.extractValue(encoding, fieldMajorType, fieldShortCount, start, end); // Update our cursor arrayCursor = next; From c3f4a85b6f3c510ace8182403311a3c192a172b1 Mon Sep 17 00:00:00 2001 From: Pedro Lino Date: Fri, 22 Dec 2023 18:25:19 +0000 Subject: [PATCH 2/3] Changing funcs from view to pure --- packages/solidity-cbor/contracts/components/CBORByteUtils.sol | 4 ++-- .../solidity-cbor/contracts/components/CBORPrimitives.sol | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/solidity-cbor/contracts/components/CBORByteUtils.sol b/packages/solidity-cbor/contracts/components/CBORByteUtils.sol index 873caa6..dcbbd1e 100644 --- a/packages/solidity-cbor/contracts/components/CBORByteUtils.sol +++ b/packages/solidity-cbor/contracts/components/CBORByteUtils.sol @@ -19,7 +19,7 @@ library CBORByteUtils { bytes memory data, uint start, uint end - ) internal view returns ( + ) internal pure returns ( bytes memory slicedData ) { // Slice our bytes @@ -34,7 +34,7 @@ library CBORByteUtils { */ function bytesToUint256( bytes memory data - ) internal view returns ( + ) internal pure returns ( uint256 value ) { for (uint i = 0; i < data.length; i++) diff --git a/packages/solidity-cbor/contracts/components/CBORPrimitives.sol b/packages/solidity-cbor/contracts/components/CBORPrimitives.sol index 8815f54..935730f 100644 --- a/packages/solidity-cbor/contracts/components/CBORPrimitives.sol +++ b/packages/solidity-cbor/contracts/components/CBORPrimitives.sol @@ -61,7 +61,7 @@ library CBORPrimitives { bytes memory encoding, uint cursor, uint shortCount - ) internal view returns ( + ) internal pure returns ( uint dataStart, uint dataEnd ) { @@ -149,7 +149,7 @@ library CBORPrimitives { /*bytes memory encoding,*/ uint cursor, uint shortCount - ) internal view returns ( + ) internal pure returns ( uint dataStart, uint dataEnd ) { From fa5e131deffa299dbb3af69877430b50e5f62a65 Mon Sep 17 00:00:00 2001 From: Pedro Lino Date: Fri, 22 Dec 2023 18:30:28 +0000 Subject: [PATCH 3/3] Reverting one pure declaration --- packages/solidity-cbor/contracts/components/CBORPrimitives.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/solidity-cbor/contracts/components/CBORPrimitives.sol b/packages/solidity-cbor/contracts/components/CBORPrimitives.sol index 935730f..f9e5842 100644 --- a/packages/solidity-cbor/contracts/components/CBORPrimitives.sol +++ b/packages/solidity-cbor/contracts/components/CBORPrimitives.sol @@ -61,7 +61,7 @@ library CBORPrimitives { bytes memory encoding, uint cursor, uint shortCount - ) internal pure returns ( + ) internal view returns ( uint dataStart, uint dataEnd ) {