我正在尝试更改solidity的状态变量值,并在geth控制台上进行测试,但状态变量的值不会更改。 步骤如下:
1:写一个简单的智能合约代码,如下所示
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 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...
这意味着智能合约已成功部署
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()
返回值仍然是99,这意味着storedData的值没有改变,除了使用uint做测试之外,我也试过了映射,但答案和uint一样,我不知道是否我在某个地方错了,或者状态变量不允许被修改,任何人都可以帮助我吗?
感谢。
答案 0 :(得分:1)
从问题来看,这是不改变状态的呼叫:
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?