From 6fe083c93f745f5e81074b939223796e86656f4d Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Thu, 26 Jan 2023 17:10:29 -0300 Subject: [PATCH] Set error code sign correctly (#175) * fix: set error code sign correctly * chore: add comment with link to source code --- contracts/v0.8/utils/Actor.sol | 8 ++--- contracts/v0.8/utils/Misc.sol | 56 ++++------------------------------ 2 files changed, 10 insertions(+), 54 deletions(-) diff --git a/contracts/v0.8/utils/Actor.sol b/contracts/v0.8/utils/Actor.sol index bd95114e..f931db06 100644 --- a/contracts/v0.8/utils/Actor.sol +++ b/contracts/v0.8/utils/Actor.sol @@ -72,9 +72,9 @@ library Actor { } function getErrorCodeMsg(int256 exit_code) internal pure returns (string memory) { - if (exit_code < 0) { - return string.concat("actor error code -", Strings.toString(uint256(exit_code))); - } - return string.concat("actor error code -", Strings.toString(uint256(exit_code))); + return + exit_code >= 0 + ? string.concat("actor error code ", Strings.toString(uint256(exit_code))) + : string.concat("actor error code -", Strings.toString(Misc.abs(exit_code))); } } diff --git a/contracts/v0.8/utils/Misc.sol b/contracts/v0.8/utils/Misc.sol index 971ab15c..4cf06068 100644 --- a/contracts/v0.8/utils/Misc.sol +++ b/contracts/v0.8/utils/Misc.sol @@ -22,56 +22,12 @@ library Misc { uint64 constant CBOR_CODEC = 0x71; uint64 constant NONE_CODEC = 0x00; - function toUint256(bytes32 _bytes) internal pure returns (uint256 value) { - return uint256(_bytes); - } - - function toInt256(bytes32 _bytes) internal pure returns (int256 value) { - return int256(uint256(_bytes)); - } - - function toUint256(bytes memory _bytes, uint offset) internal pure returns (uint256 value) { - assembly { - value := mload(add(_bytes, offset)) - } - } - - function toInt256(bytes memory _bytes, uint offset) internal pure returns (int256 value) { - return int256(toUint256(_bytes, offset)); - } - - function toBytes(uint256 x) internal pure returns (bytes memory b) { - b = new bytes(32); - assembly { - mstore(add(b, 32), x) - } - } - - function toBytes(int256 x) internal pure returns (bytes memory b) { - b = new bytes(32); - assembly { - mstore(add(b, 32), x) - } - } - - function copy(uint src, uint dest, uint len) internal pure { - // Copy word-length chunks while possible - for (; len >= 32; len -= 32) { - assembly { - mstore(dest, mload(src)) - } - dest += 32; - src += 32; - } - - if (len == 0) return; - - // Copy remaining bytes - uint mask = 256 ** (32 - len) - 1; - assembly { - let srcpart := and(mload(src), not(mask)) - let destpart := and(mload(dest), mask) - mstore(dest, or(destpart, srcpart)) + // Code taken from Openzeppelin repo + // Link: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/0320a718e8e07b1d932f5acb8ad9cec9d9eed99b/contracts/utils/math/SignedMath.sol#L37-L42 + function abs(int256 n) internal pure returns (uint256) { + unchecked { + // must be unchecked in order to support `n = type(int256).min` + return uint256(n >= 0 ? n : -n); } } }