Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Set error code sign correctly (#175)
Browse files Browse the repository at this point in the history
* fix: set error code sign correctly
* chore: add comment with link to source code
  • Loading branch information
emmanuelm41 authored Jan 26, 2023
1 parent 42400f0 commit 6fe083c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 54 deletions.
8 changes: 4 additions & 4 deletions contracts/v0.8/utils/Actor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
56 changes: 6 additions & 50 deletions contracts/v0.8/utils/Misc.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 6fe083c

Please sign in to comment.