I need to understand a couple of things:
- What is the maximum value an int and uint can store?
- Are the maximum values stored in int and uint the same?
evmremixsolidity
I need to understand a couple of things:
To convert one type to another, use uint(x)
or int(x)
. The rules are basically the same as in C, where the first bit of the uint becomes the sign.
For example:
if (int(a - b) < c) doSomething();
is likely what you want, as the numbers will be subtracted, then converted.
The type(x)
syntax is used for all kinds of casting, such as truncating a number (uint8(someUint)
), converting to and from odd types (bytes32(someUint)
), or even casting a given address to a specific contract type (someContract(someAddress)
).
This somehow seems to be a problem with Browser Solidity interacting with geth
. When I deploy the contract and execute the transactions directly in geth
, the contract works as expected.
@PietroR91
, please submit your code as an issue in the Browser Solidity github. If you are unable to do so, I can do it later.
The error in Browser Solidity has been verified with the pragma and compiler being ^0.4.0
, ^0.4.4
and ^0.4.7
.
The geth
environment I am using is version 1.5.5-stable on OS/X.
Here is your source code, modified by the addition of getUser(...)
to check the values of the data in the mapping, and some minor formatting changes:
pragma solidity ^0.4.0;
contract Test {
struct User {
string name;
string surname;
uint active;
}
mapping (bytes32 => User) public userRegister;
function add_user(string _name, string _surname) {
bytes32 _hc = sha3(_name, _surname);
User p = userRegister[_hc];
p.name = _name;
p.surname = _surname;
p.active = 1;
}
function remove(string _name, string _surname) {
bytes32 _hc = sha3(_name, _surname);
userRegister[_hc].active = 0;
}
function active(string _name, string _surname) {
bytes32 _hc=sha3(_name, _surname);
userRegister[_hc].active = 1;
}
function getUser(string _name, string _surname) constant
returns (string name, string surname, uint256 active) {
bytes32 _hc = sha3(_name, _surname);
name = userRegister[_hc].name;
surname = userRegister[_hc].surname;
active = userRegister[_hc].active;
}
}
add_user(...)
remove(...)
When attempting to execute remove(...)
in Browser Solidity, the message callback contain no result Error: Intrinsic gas too low
is displayed.
Following is the Web3 Deploy code from Browser Solidity:
var testContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"userRegister","outputs":[{"name":"name","type":"string"},{"name":"surname","type":"string"},{"name":"active","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"},{"name":"_surname","type":"string"}],"name":"add_user","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"},{"name":"_surname","type":"string"}],"name":"remove","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"string"},{"name":"_surname","type":"string"}],"name":"getUser","outputs":[{"name":"name","type":"string"},{"name":"surname","type":"string"},{"name":"active","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"},{"name":"_surname","type":"string"}],"name":"active","outputs":[],"payable":false,"type":"function"}]);
var test = testContract.new(
{
from: web3.eth.accounts[0],
data: '0x606060405234610000575b610aae806100196000396000f300606060405263ffffffff60e060020a60003504166312f8ed308114610050578063400462fa1461016b57806344590a7e146101fd57806383e6858f1461028f57806398f5be8714610418575b610000565b34610000576100606004356104aa565b604080519081018290526060808252845460026000196101006001841615020190911604908201819052819060208201906080830190879080156100e55780601f106100ba576101008083540402835291602001916100e5565b820191906000526020600020905b8154815290600101906020018083116100c857829003601f168201915b50508381038252855460026000196101006001841615020190911604808252602090910190869080156101595780601f1061012e57610100808354040283529160200191610159565b820191906000526020600020905b81548152906001019060200180831161013c57829003601f168201915b50509550505050505060405180910390f35b34610000576101fb600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284375050604080516020601f89358b018035918201839004830284018301909452808352979998810197919650918201945092508291508401838280828437509496506104c595505050505050565b005b34610000576101fb600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284375050604080516020601f89358b018035918201839004830284018301909452808352979998810197919650918201945092508291508401838280828437509496506106d995505050505050565b005b346100005761031f600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284375050604080516020601f89358b0180359182018390048302840183019094528083529799988101979196509182019450925082915084018382808284375094965061079895505050505050565b6040805190810182905260608082528451908201528351819060208083019160808401918801908083838215610370575b80518252602083111561037057601f199092019160209182019101610350565b505050905090810190601f16801561039c5780820380516001836020036101000a031916815260200191505b50838103825285518152855160209182019187019080838382156103db575b8051825260208311156103db57601f1990920191602091820191016103bb565b505050905090810190601f1680156104075780820380516001836020036101000a031916815260200191505b509550505050505060405180910390f35b34610000576101fb600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284375050604080516020601f89358b018035918201839004830284018301909452808352979998810197919650918201945092508291508401838280828437509496506109bf95505050505050565b005b60006020819052908152604090206002810154600182019083565b6000600083836040518083805190602001908083835b602083106104fa5780518252601f1990920191602091820191016104db565b51815160209384036101000a600019018019909216911617905285519190930192850191508083835b602083106105425780518252601f199092019160209182019101610523565b6001836020036101000a0380198251168184511680821785525050505050509050019250505060405180910390209150600060008360001916600019168152602001908152602001600020905083816000019080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106105dd57805160ff191683800117855561060a565b8280016001018555821561060a579182015b8281111561060a5782518255916020019190600101906105ef565b5b5061062b9291505b808211156106275760008155600101610613565b5090565b505082816001019080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061067b57805160ff19168380011785556106a8565b828001600101855582156106a8579182015b828111156106a857825182559160200191906001019061068d565b5b506106c99291505b808211156106275760008155600101610613565b5090565b5050600160028201555b50505050565b600082826040518083805190602001908083835b6020831061070c5780518252601f1990920191602091820191016106ed565b51815160209384036101000a600019018019909216911617905285519190930192850191508083835b602083106107545780518252601f199092019160209182019101610735565b51815160209384036101000a60001901801990921691161790526040805192909401829003909120600081815291829052928120600201555093505050505b505050565b602060405190810160405280600081525060206040519081016040528060008152506000600085856040518083805190602001908083835b602083106107ef5780518252601f1990920191602091820191016107d0565b51815160209384036101000a600019018019909216911617905285519190930192850191508083835b602083106108375780518252601f199092019160209182019101610818565b518151600019602094850361010090810a8201928316921993909316919091179092526040805196909401869003862060008181528085528590208054601f600260018316159095029095011692909204928301849004840287018401909452818652929850939650909450919250508301828280156108f85780601f106108cd576101008083540402835291602001916108f8565b820191906000526020600020905b8154815290600101906020018083116108db57829003601f168201915b50505060008481526020818152604091829020600190810180548451600260001994831615610100029490940190911692909204601f81018490048402830184019094528382529599509493509091508301828280156109995780601f1061096e57610100808354040283529160200191610999565b820191906000526020600020905b81548152906001019060200180831161097c57829003601f168201915b50505060008481526020819052604090206002015492955091935050505b509250925092565b600082826040518083805190602001908083835b602083106109f25780518252601f1990920191602091820191016109d3565b51815160209384036101000a600019018019909216911617905285519190930192850191508083835b60208310610a3a5780518252601f199092019160209182019101610a1b565b51815160209384036101000a6000190180199092169116179052604080519290940182900390912060008181529182905292902060016002909101555093505050505b5050505600a165627a7a72305820a6692301b30e1302f9b8c77813503eb5f8e127531e5903cffcf70070af2e9a7a0029',
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
geth
I paste the Web3 Deploy code from the previous section into my Dev geth
console:
I0106 08:17:04.067480 internal/ethapi/api.go:1045] Tx(0x2ce868c7e3377f4330ef847747b471c5ff27384b67e973fff2c86f3547316db6) created: 0x22718021ba1f0ce28931c3d0653f624c6b409763
null [object Object]
undefined
...
null [object Object]
Contract mined! address: 0x22718021ba1f0ce28931c3d0653f624c6b409763 transactionHash: 0x2ce868c7e3377f4330ef847747b471c5ff27384b67e973fff2c86f3547316db6
geth
I then executed the following functions in geth
:
> test.add_user("test_name", "test_surname", {from: eth.accounts[0], gas: 400000});
I0106 08:19:07.999559 internal/ethapi/api.go:1047] Tx(0xda430dd968ce3537ed29b4e57ff6f10a4feb6fb674ac1f50ac5684368b9415c1) to: 0x22718021ba1f0ce28931c3d0653f624c6b409763
"0xda430dd968ce3537ed29b4e57ff6f10a4feb6fb674ac1f50ac5684368b9415c1"
...
> eth.getTransactionReceipt("0xda430dd968ce3537ed29b4e57ff6f10a4feb6fb674ac1f50ac5684368b9415c1")
{
blockHash: "0x02e8f2b1385bafb4336a8057640967763e05ad430c47a169ce0512f5e3bc9859",
blockNumber: 5878,
contractAddress: null,
cumulativeGasUsed: 85340,
from: "0x000d1009bd8f0b1301cc5edc28ed1222a3ce671e",
gasUsed: 85340,
logs: [],
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
root: "0x28b34cf11c24cc9dc72ea47539b556ce8266fa5d4bed8910da24783019e6f9cb",
to: "0x22718021ba1f0ce28931c3d0653f624c6b409763",
transactionHash: "0xda430dd968ce3537ed29b4e57ff6f10a4feb6fb674ac1f50ac5684368b9415c1",
transactionIndex: 0
}
Checking the function got executed correctly:
> test.getUser("test_name", "test_surname");
["test_name", "test_surname", 1]
Let's now execute the remove(...)
function with the problem:
> test.remove("test_name", "test_surname", {from: eth.accounts[0], gas: 400000});
I0106 08:22:30.226885 internal/ethapi/api.go:1047] Tx(0x684c27aac5ff70c97c374646de9ec376ad2a2b67c7e0205445ed208b1b95b30b) to: 0x22718021ba1f0ce28931c3d0653f624c6b409763
"0x684c27aac5ff70c97c374646de9ec376ad2a2b67c7e0205445ed208b1b95b30b"
...
> eth.getTransactionReceipt("0x684c27aac5ff70c97c374646de9ec376ad2a2b67c7e0205445ed208b1b95b30b")
{
blockHash: "0x895f68c8d0aa3adf2ec86e22ddc4f7ca9b185252258374430ff96988646ae7a0",
blockNumber: 5899,
contractAddress: null,
cumulativeGasUsed: 14828,
from: "0x000d1009bd8f0b1301cc5edc28ed1222a3ce671e",
gasUsed: 14828,
logs: [],
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
root: "0x4f21b21ed39fc110bc8c2e9e5ac3be3576c1733c59660f728bd180273dd16210",
to: "0x22718021ba1f0ce28931c3d0653f624c6b409763",
transactionHash: "0x684c27aac5ff70c97c374646de9ec376ad2a2b67c7e0205445ed208b1b95b30b",
transactionIndex: 0
}
Checking the function got executed correctly:
> test.getUser("test_name", "test_surname");
["test_name", "test_surname", 0]
If I modify remove(...)
to add the following:
function remove(string _name, string _surname) {
bytes32 _hc = sha3(_name, _surname);
userRegister[_hc].name = _name;
userRegister[_hc].surname = _surname;
userRegister[_hc].active = 0;
}
I am able to execute the function in Browser Solidity, but the returned from getUser(...)
shows that the value of active is not modified from 1 to 0.
remove(...)
function in Browser SolidityThe following screen shows the remove(...)
function being successfully executed in Browser Solidity:
Following is the transaction receipt in geth
:
> eth.getTransactionReceipt("0x59734b415f6e1094f57b07579cfaba53325321f1cee9512c84c4b257ed222d3c")
{
blockHash: "0x91949c01a0bf15a8d8cf558b9e9afcda686855669960c5aaf46069d9d75d5c82",
blockNumber: 5949,
contractAddress: null,
cumulativeGasUsed: 85468,
from: "0x000d1009bd8f0b1301cc5edc28ed1222a3ce671e",
gasUsed: 85468,
logs: [],
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
root: "0x2bf9db658636a8de8af1d4f171c8b8ea97d11bf55592d0fce67b69f3947719a6",
to: "0x49472fa49f6e3e84c0c038416f000eaeddb5c891",
transactionHash: "0x59734b415f6e1094f57b07579cfaba53325321f1cee9512c84c4b257ed222d3c",
transactionIndex: 0
}
The following screen shows the second invocation of getUser(...)
where the results are unchanged. active
is still 1
instead of the expected 0
:
Best Answer
uint
is short foruint256
, so it can store 2^256 values - because it's unsigned the maximum value is 2^256-1 (zero needs one space). What is the maximum input value for function uint256 parameter?int
is short forint256
and it can store the same amount of values - because it's signed the maximum (positive) value is 2^256 / 2 - 1.So the maximum values of
uint
andint
are not the same. They have the same amount of values butint
needs to store values also for negative numbers.You can find more details at: Max/min values of standard data types