SimpleStorage.sol:
pragma solidity ^0.4.4;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public constant returns (uint) {
return storedData;
}
}
绑定/ SimpleStorage.go:
// Set is a paid mutator transaction binding the contract method 0x60fe47b1.
//
// Solidity: function set(x uint256) returns()
func (_SimpleStorage *SimpleStorageSession) Set(x *big.Int) (*types.Transaction, error) {
return _SimpleStorage.Contract.Set(&_SimpleStorage.TransactOpts, x)
}
// Solidity: function set(x uint256) returns()
func (_SimpleStorage *SimpleStorageTransactor) Set(opts *bind.TransactOpts, x *big.Int) (*types.Transaction, error) {
return _SimpleStorage.contract.Transact(opts, "set", x)
}
// Solidity: function get() constant returns(uint256)
func (_SimpleStorage *SimpleStorageSession) Get() (*big.Int, error) {
return _SimpleStorage.Contract.Get(&_SimpleStorage.CallOpts)
}
// Get is a free data retrieval call binding the contract method 0x6d4ce63c.
//
// Solidity: function get() constant returns(uint256)
func (_SimpleStorage *SimpleStorageCaller) Get(opts *bind.CallOpts) (*big.Int, error) {
var (
ret0 = new(*big.Int)
)
out := ret0
err := _SimpleStorage.contract.Call(opts, out, "get")
return *ret0, err
}
测试/ SimpleStorage.go:
// TestGet checks if Get return a value that has been set
func (s *SimpleStorageSuite) TestGet(c *check.C) {
session := contract.Session("SimpleStorage")
c.Assert(session, check.NotNil)
storageSession, ok := session.(*bindings.SimpleStorageSession)
c.Assert(ok, check.Equals, true)
c.Assert(storageSession, check.NotNil)
fmt.Printf("session: %+v\n", storageSession)
tx, err := storageSession.Set(big.NewInt(2302))
c.Assert(err, check.IsNil)
c.Assert(tx, check.NotNil)
fmt.Printf("tx: %+v\n", tx)
fmt.Printf("tx.To: %v\n", tx.To())
storedData, err := storageSession.Get()
fmt.Printf("%d\n", storedData)
c.Assert(err, check.IsNil)
c.Assert(storedData, check.Equals, 2302)
}
geth
使用this脚本启动:
geth --datadir $DIR --nodiscover --maxpeers 0 init $DIR/genesis.json
geth --datadir $DIR --nodiscover --maxpeers 0 --mine --minerthreads 1 --rpc --rpcapi "eth,web3,personal,net" console
问题:正在运行perigord test
给了我结果:
----------------------------------------------------------------------
FAIL: tests/SimpleStorage.go:37: SimpleStorageSuite.TestGet
tests/SimpleStorage.go:55:
c.Assert(storedData, check.Equals, 2302)
... obtained *big.Int = 0 (%!q(big.Int=0))
... expected int = 2302
OOPS: 0 passed, 1 FAILED
为什么get()
返回0而不是2302?
通过打印交易,我看到了:
TX(555ca4c3edfa0190ce41bb6bfced502d903a721b02113634bbab5ff48902503a)
Contract: false
From: 945cd603a6754cb13c3d61d8fe240990f86f9f8a
To: c4b69e1de27994a2a769302c4b9e7f56fa65d8d9
Nonce: 83
GasPrice: 0x430e23400
GasLimit 0xa2be
Value: 0x0
Data: 0x60fe47b100000000000000000000000000000000000000000000000000000000000008fe
V: 0x1b
R: 0x43b542cd09feb08db234a7693ed756cead678e0cf7b11b749594e9559333f7bb
S: 0x143f01822afe5d8ea65f97078b2df95df80128842a42d1f40edf031691df6bf8
Hex: f88853850430e2340082a2be94c4b69e1de27994a2a769302c4b9e7f56fa65d8d980a460fe47b100000000000000000000000000000000000000000000000000000000000008fe1ba043b542cd09feb08db234a7693ed756cead678e0cf7b11b749594e9559333f7bba0143f01822afe5d8ea65f97078b2df95df80128842a42d1f40edf031691df6bf8
如果我试图获取To
地址的值:
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.3-stable/darwin-amd64/go1.10.1
coinbase: 0x945cd603a6754cb13c3d61d8fe240990f86f9f8a
at block: 7189 (Thu, 26 Apr 2018 20:07:44 +07)
datadir: /tmp/geth_private_testnet
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
> eth.getStorageAt("0xc4b69e1de27994a2a769302c4b9e7f56fa65d8d9", 0)
"0x00000000000000000000000000000000000000000000000000000000000008fe"
和
Python 2.7.14 (default, Mar 22 2018, 14:43:05)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> int("0x00000000000000000000000000000000000000000000000000000000000008fe", 16)
2302
答案 0 :(得分:0)
正如@smarx指出的那样,虽然difficulty设置为0x1
,但交易仍需要一段时间才能开采。
我添加了以下代码来等待它:
ethClient, err := ethclient.Dial(s.network.Url())
txPending := true
for txPending {
_, txPending, err = ethClient.TransactionByHash(context.Background(), tx.Hash())
c.Assert(err, check.IsNil)
if txPending {
time.Sleep(3 * time.Second)
} else {
break
}
}
现在通过测试:
Running migration 1
Running migration 2
Setting last migration to: 2
s.network: &{name:dev rpc_client:0xc4202773b0 client:0xc4203780c0 keystore:0xc4204642d0}
session: &{Contract:0xc42027e400 CallOpts:{Pending:true From:[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] Context:<nil>} TransactOpts:{From:[148 92 214 3 166 117 76 177 60 61 97 216 254 36 9 144 248 111 159 138] Nonce:<nil> Signer:0x448c760 Value:<nil> GasPrice:<nil> GasLimit:0 Context:<nil>}}
tx:
TX(11afba591622660236c2726ec67616c26f2f4a65ea5c3b4333bc6cf9523d2d2b)
Contract: false
From: 945cd603a6754cb13c3d61d8fe240990f86f9f8a
To: 37ad882be0e122a82da9a93addbc22f6b0330ac1
Nonce: 135
GasPrice: 0x430e23400
GasLimit 0xa2be
Value: 0x0
Data: 0x60fe47b100000000000000000000000000000000000000000000000000000000000008fe
V: 0x1b
R: 0x2f66cf07f390714d835c2d539ab03d26e274854903c535b751475d270f68c17c
S: 0x31d41b82a5614162cf4ec518c67fb5dfb0b6f16c6ce8329d43860483b996a470
Hex: f8898187850430e2340082a2be9437ad882be0e122a82da9a93addbc22f6b0330ac180a460fe47b100000000000000000000000000000000000000000000000000000000000008fe1ba02f66cf07f390714d835c2d539ab03d26e274854903c535b751475d270f68c17ca031d41b82a5614162cf4ec518c67fb5dfb0b6f16c6ce8329d43860483b996a470
tx.Hash: [17 175 186 89 22 34 102 2 54 194 114 110 198 118 22 194 111 47 74 101 234 92 59 67 51 188 108 249 82 61 45 43]
ret0: 842351256648
out: 842351256648
2302
OK: 1 passed
PASS
ok github.com/quantonganh/simplestorage 82.625s