Skip to content

Commit a24d813

Browse files
authored
refactor: move bytesToUint to StdUtils for shared use (#222)
* move bytesToUint to StdUtils for shared use * handle edge case over 32 bytes * fmt * make virtual
1 parent 14b6b45 commit a24d813

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

src/StdCheats.sol

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,10 @@ abstract contract StdCheatsSafe {
315315
txDetail.data = rawDetail.data;
316316
txDetail.from = rawDetail.from;
317317
txDetail.to = rawDetail.to;
318-
txDetail.nonce = bytesToUint(rawDetail.nonce);
319-
txDetail.txType = bytesToUint(rawDetail.txType);
320-
txDetail.value = bytesToUint(rawDetail.value);
321-
txDetail.gas = bytesToUint(rawDetail.gas);
318+
txDetail.nonce = _bytesToUint(rawDetail.nonce);
319+
txDetail.txType = _bytesToUint(rawDetail.txType);
320+
txDetail.value = _bytesToUint(rawDetail.value);
321+
txDetail.gas = _bytesToUint(rawDetail.gas);
322322
txDetail.accessList = rawDetail.accessList;
323323
return txDetail;
324324
}
@@ -368,12 +368,12 @@ abstract contract StdCheatsSafe {
368368
receipt.to = rawReceipt.to;
369369
receipt.from = rawReceipt.from;
370370
receipt.contractAddress = rawReceipt.contractAddress;
371-
receipt.effectiveGasPrice = bytesToUint(rawReceipt.effectiveGasPrice);
372-
receipt.cumulativeGasUsed = bytesToUint(rawReceipt.cumulativeGasUsed);
373-
receipt.gasUsed = bytesToUint(rawReceipt.gasUsed);
374-
receipt.status = bytesToUint(rawReceipt.status);
375-
receipt.transactionIndex = bytesToUint(rawReceipt.transactionIndex);
376-
receipt.blockNumber = bytesToUint(rawReceipt.blockNumber);
371+
receipt.effectiveGasPrice = _bytesToUint(rawReceipt.effectiveGasPrice);
372+
receipt.cumulativeGasUsed = _bytesToUint(rawReceipt.cumulativeGasUsed);
373+
receipt.gasUsed = _bytesToUint(rawReceipt.gasUsed);
374+
receipt.status = _bytesToUint(rawReceipt.status);
375+
receipt.transactionIndex = _bytesToUint(rawReceipt.transactionIndex);
376+
receipt.blockNumber = _bytesToUint(rawReceipt.blockNumber);
377377
receipt.logs = rawToConvertedReceiptLogs(rawReceipt.logs);
378378
receipt.logsBloom = rawReceipt.logsBloom;
379379
receipt.transactionHash = rawReceipt.transactionHash;
@@ -390,12 +390,12 @@ abstract contract StdCheatsSafe {
390390
for (uint256 i; i < rawLogs.length; i++) {
391391
logs[i].logAddress = rawLogs[i].logAddress;
392392
logs[i].blockHash = rawLogs[i].blockHash;
393-
logs[i].blockNumber = bytesToUint(rawLogs[i].blockNumber);
393+
logs[i].blockNumber = _bytesToUint(rawLogs[i].blockNumber);
394394
logs[i].data = rawLogs[i].data;
395-
logs[i].logIndex = bytesToUint(rawLogs[i].logIndex);
395+
logs[i].logIndex = _bytesToUint(rawLogs[i].logIndex);
396396
logs[i].topics = rawLogs[i].topics;
397-
logs[i].transactionIndex = bytesToUint(rawLogs[i].transactionIndex);
398-
logs[i].transactionLogIndex = bytesToUint(rawLogs[i].transactionLogIndex);
397+
logs[i].transactionIndex = _bytesToUint(rawLogs[i].transactionIndex);
398+
logs[i].transactionLogIndex = _bytesToUint(rawLogs[i].transactionLogIndex);
399399
logs[i].removed = rawLogs[i].removed;
400400
}
401401
return logs;
@@ -466,12 +466,9 @@ abstract contract StdCheatsSafe {
466466
who = vm.rememberKey(privateKey);
467467
}
468468

469-
function bytesToUint(bytes memory b) private pure returns (uint256) {
470-
uint256 number;
471-
for (uint256 i = 0; i < b.length; i++) {
472-
number = number + uint256(uint8(b[i])) * (2 ** (8 * (b.length - (i + 1))));
473-
}
474-
return number;
469+
function _bytesToUint(bytes memory b) private pure returns (uint256) {
470+
require(b.length <= 32, "StdCheats _bytesToUint(bytes): Bytes length exceeds 32.");
471+
return abi.decode(abi.encodePacked(new bytes(32 - b.length), b), (uint256));
475472
}
476473
}
477474

src/StdUtils.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ abstract contract StdUtils {
7474
return addressFromLast20Bytes(keccak256(abi.encodePacked(bytes1(0xff), deployer, salt, initcodeHash)));
7575
}
7676

77+
function bytesToUint(bytes memory b) internal pure virtual returns (uint256) {
78+
require(b.length <= 32, "StdUtils bytesToUint(bytes): Bytes length exceeds 32.");
79+
return abi.decode(abi.encodePacked(new bytes(32 - b.length), b), (uint256));
80+
}
81+
7782
function addressFromLast20Bytes(bytes32 bytesValue) private pure returns (address) {
7883
return address(uint160(uint256(bytesValue)));
7984
}

test/StdUtils.t.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,20 @@ contract StdUtilsTest is Test {
8888
address create2Address = computeCreate2Address(salt, initcodeHash, deployer);
8989
assertEq(create2Address, 0xB147a5d25748fda14b463EB04B111027C290f4d3);
9090
}
91+
92+
function testBytesToUint() external {
93+
bytes memory maxUint = hex"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
94+
bytes memory two = hex"02";
95+
bytes memory millionEther = hex"d3c21bcecceda1000000";
96+
97+
assertEq(bytesToUint(maxUint), type(uint256).max);
98+
assertEq(bytesToUint(two), 2);
99+
assertEq(bytesToUint(millionEther), 1_000_000 ether);
100+
}
101+
102+
function testCannotConvertGT32Bytes() external {
103+
bytes memory thirty3Bytes = hex"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
104+
vm.expectRevert("StdUtils bytesToUint(bytes): Bytes length exceeds 32.");
105+
bytesToUint(thirty3Bytes);
106+
}
91107
}

0 commit comments

Comments
 (0)