solidity,将值设置为状态变量,值不变

solidity, set value to state Variables, the value not changed

我试图改变solidity的状态变量值,并在geth控制台上测试,但状态变量的值没有改变。 步骤如下:

1:通过solidity写一个简单的智能合约代码如下

pragma solidity ^0.4.0;

contract SimpleStorage {

uint public storedData=99;
mapping(string => uint)  balances;



function set(uint x) public returns (uint){

    storedData = x;
    return storedData;

}

function get() public constant returns (uint) {

    return storedData;

}

function multiply(uint a) returns(uint d) {
    return a * 7;
}

function setmapping(string key,uint value) returns(uint v)
{
     balances[key] = value;
     return balances[key];
}

function getmapping(string key) returns(uint v)
{
    return balances[key];
}

function kill()
{ 

}


}

2:通过truffle编译代码,使用command

truffle compile

3:启动geth,解锁账户,启动minner

4:部署智能合约

truffle migration --reset

然后我看到控制台输出如下

Using network 'development'.
Running migration: 1_initial_migration.js
Replacing Migrations...
... 0x8ccf9e1599c2760ff3eed993be10929403e1faa05489a247a067f4f06536c74c
Migrations: 0xec08113a9e810e527d99a1aafa8376425c4a75ed
Saving successful migration to network...
... 0xedbf12715b736759e9d9297cbaaeb3151d95f478c2f1ee71bff4819d2dbb47e5
Saving artifacts...
Running migration: 2_deploy_contracts.js
Replacing SimpleStorage...
... 0xff5b00f9b14d8ecea4828c3ad8e9dbfa9d685bc0b81530fc346759d7998b060f
SimpleStorage: 0x96cf1e076f4d99a5d0823bd76c8de6a3a209d125
Saving successful migration to network...
... 0x3452a9e76b73e250de80874ebc3fd454724ebf6a15563bee0d5ba89b7b41909f
Saving artifacts...

表示智能合约部署到geth成功

5:然后在geth控制台中,我设置abi变量如下:

abi=[{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"changeStorage","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"getAll","outputs":[{"name":"","type":"uint256"},{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"},{"name":"value","type":"uint256"}],"name":"setmapping","outputs":[{"name":"v","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"refrenceType","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"changeMemory","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"}],"name":"getmapping","outputs":[{"name":"v","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"payable":false,"type":"function"}]

6: 立即获取合约如下:

test=eth.contract(abi).at("0x2f3970e8e4e2f5ed4ccb37b0f79fe5598700e2f0")

7:运行set()

test.set.call(22);

输出是 22,我认为状态变量 storedData 已成功设置为新值 22,但是当我 运行 下面的代码读取 storedData 时,

test4.get()

return的值还是99,说明storedData的值没变,除了用uint测试,我也试了mapping,结果和uint一样,我不知道是我哪里错了还是不允许修改状态变量,有人可以帮我吗?

谢谢。

从问题来看,这是没有改变状态的调用:

test.set.call(22)

要更改状态,您必须发出交易。 call(...) 只告诉你如果你要发送交易会发生什么。有关更多背景信息,请参阅:What is the difference between a transaction and a call?

因此您可以将上面的行替换为:

test.set.sendTransaction(22)

或者,web3.js 将automatically attempt 决定是使用事务还是调用。所以在这种情况下你可以简单地使用:

test.set(22)

有关 web3.js 如何做出决定的更多背景信息,请参阅:How does web3.js decide to run a call() or sendTransaction() on a method call?