以太坊实验
复制代码
1
2
3在自己的电脑上安装配置以太坊开发环境,搭建以太坊私有链,编写一个简单的智能合约,通过以太坊JSON RPC 和JavaScript API编程接口将其部署到所创建的以太坊私有链,并可调用合约获得正确的合约执行结果。 本次作业的最终提交物是一个描述上述过程的完整文档,请把整个过程用文字和截图描述清楚。
- 本机已经安装go
- 安装以太坊Ethereum
复制代码
1
2
3
4
5brew update brew upgrade brew tap ethereum/ethereum brew install ethereum
- 安装solc编译器
复制代码
1
2npm install solc
- 创建账户
复制代码
1
2geth account new
- 编写创始块文件
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25{ "config": { "chainId": 10, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0 }, "nonce": "0x0000000000000042", "difficulty": "0x1", "alloc": { "14b1d82b1c851ea9a6623a20d9865677ecdac70c":{ "balance": "20000009800000000000000000000" }, "aa25a7b683fe0564fe6b2a2574e10dc886ecb3ce":{ "balance": "20000009800000000000000000000" } }, "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase": "0x0000000000000000000000000000000000000000", "timestamp": "0x00", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa", "gasLimit": "0xb2d05e00" }
-
配置自动解锁账户的脚本
复制代码1
2在~/Library/Ethereum下创建password文件内容为创建账户的密码
-
编写启动脚本
复制代码
1
2
3#!/bin/bash geth --rpc --rpcaddr="0.0.0.0" --rpcport "8545" --rpccorsdomain="*" --unlock '0,1' --password ~/Library/Ethereum/password --nodiscover --maxpeers '5' --n etworkid '123' --datadir '~/Library/Ethereum' console --rpcapi "db,eth,net,web3,personal,admin,miner"
- 启动网络
复制代码
1
2./start.sh
- 通过JRPC与网络交互(均编写在脚本中,方便下次使用)
复制代码
1
2
3curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x14b1d82b1c851ea9a6623a20d9865677ecdac7 0c","latest"],"id":1}' http://0.0.0.0:8545
- 返回的结果
复制代码
1
2
3localhost:eth liu$ ./curlexample.sh {"jsonrpc":"2.0","id":1,"result":"0x409f9fadbc2695e2f02d4a82"}
- 测试合约,后续需需要通过event的返回log的方式查看调用结果信息
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16localhost:eth liu$ cat Hello.sol pragma solidity ^0.4.17; contract Hello { event Print(uint); function multiply(uint input)public returns (uint) { emit Print(input * 7); return input * 7; } //print name function print(string name) public pure returns (string) { return name; } }
- solc编译合约,出现问题,但不影响后续的操作
复制代码
1
2
3
4
5
6
7
8
9localhost:eth liu$ solc Hello.sol (node:1915) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated. (node:1915) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated. localhost:eth liu$ cat contracts/Hello.sol:Hello.bin 608060405234801561001057600080fd5b506101f1806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806311114af114610051578063c6888fa114610133575b600080fd5b34801561005d57600080fd5b506100b8600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290505050610174565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100f85780820151818401526020810190506100dd565b50505050905090810190601f1680156101255780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561013f57600080fd5b5061015e6004803603810190808035906020019092919050505061017e565b6040518082815260200191505060405180910390f35b6060819050919050565b60007f24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da600783026040518082815260200191505060405180910390a16007820290509190505600a165627a7a72305820b7dcf3ffc23d38b70328fb068693613965fa00a36d684661ae2f53df694f1e570029 localhost:eth liu$ localhost:eth liu$ cat contracts/Hello.sol:Hello.abi [{"constant":true,"inputs":[{"name":"name","type":"string"}],"name":"print","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":false,"inputs":[{"name":"input","type":"uint256"}],"name":"multiply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"","type":"uint256"}],"name":"Print","type":"event"}]localhost:eth liu$
- 部署调用合约
复制代码
1
2
3
4
5
6
7
8
9localhost:eth liu$ cat curlsend.sh #!/bin/bash curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{ "from": "0x14b1d82b1c851ea9a6623a20d9865677ecdac70c", "gas": "0x76c000", "data": "0x608060405234801561001057600080fd5b506101f1806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806311114af114610051578063c6888fa114610133575b600080fd5b34801561005d57600080fd5b506100b8600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290505050610174565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100f85780820151818401526020810190506100dd565b50505050905090810190601f1680156101255780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561013f57600080fd5b5061015e6004803603810190808035906020019092919050505061017e565b6040518082815260200191505060405180910390f35b6060819050919050565b60007f24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da600783026040518082815260200191505060405180910390a16007820290509190505600a165627a7a72305820b7dcf3ffc23d38b70328fb068693613965fa00a36d684661ae2f53df694f1e570029" }],"id":1}' http://0.0.0.0:8545 localhost:eth liu$
- 调用
复制代码
1
2
3localhost:eth liu$ ./curlsend.sh {"jsonrpc":"2.0","id":1,"result":"0x649cddcacd03f12d860a37b21b47af0ec90375e2eed81c132dc8d451bb09c8e2"}
-
geth客户端,产生提交的合约地址,后续调用合约要用
复制代码1
2合约地址0x234C5DB1913e7a046387222eF3960576D22A90c3
复制代码
1
2
3
4
5
6
7
8> INFO [10-27|21:15:22.779] Submitted contract creation fullhash=0x994e4af5dee931bdec20c422c06b9c58e11a2aa22377b704ea4cb669b230e0ce contract=0x234C5DB1913e7a046387222eF3960576D22A90c3 > txpool.status { pending: 1, queued: 0 } >
- 启动挖矿,确认交易
复制代码
1
2
3
4
5
6
7> miner.start() > txpool.status { pending: 0, queued: 0 }
- 调用合约,脚本中to对应的value为合约地址,获得交易的hash值,需要挖矿确认交易
合约地址:0x234C5DB1913e7a046387222eF3960576D22A90c3
-
调用脚本中的data,为调用合约的签名信息(hash值),
复制代码1
2
3
4
5web3.sha3("multiply(uint256)").substring(0, 8)确定前4字节的数据:0xc6888f 接着需要输入4字节的输入参数数据,假设传入6对应uint256数据为 0000000000000000000000000000000000000000000000000000000000000006 两组hash值拼接即为data数据
复制代码
1
2
3
4
5
6
7
8
9
10
11localhost:eth liu$ cat curlcall.sh #!/bin/bash curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{ "from": "0x14b1d82b1c851ea9a6623a20d9865677ecdac70c", "to": "0x234C5DB1913e7a046387222eF3960576D22A90c3", "data": "0xc6888fa10000000000000000000000000000000000000000000000000000000000000006" }],"id":1}' http://0.0.0.0:8545 localhost:eth liu$ ./curlcall.sh {"jsonrpc":"2.0","id":1,"result":"0x85dae51f6a405c049c63354f210fd718a043612a2695a65145feaeedac7dec22"}
-
通过交易的hash值获取交易信息
复制代码1
2
3交易的hash值: 0x85dae51f6a405c049c63354f210fd718a043612a2695a65145feaeedac7dec22
复制代码
1
2
3
4
5
6
7localhost:eth liu$ cat curlget.sh #!/bin/bash curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":[ "0x85dae51f6a405c049c63354f210fd718a043612a2695a65145feaeedac7dec22" ],"id":1}' http://0.0.0.0:8545 localhost:eth liu$
-
查看交易,从logs.data中可以看到结果,该log是通过event的方式返回42(6*7)
复制代码1
20x000000000000000000000000000000000000000000000000000000000000002a
复制代码
1
2
3
4localhost:eth liu$ ./curlget.sh {"jsonrpc":"2.0","id":1,"result":{"blockHash":"0x40b0928fee368e94adfecb3b33d0e29b12a36e5a525e203fd1b4ae2da58ed19e","blockNumber":"0x3ab","contractAddress":null,"cumulativeGasUsed":"0x592a","from":"0x14b1d82b1c851ea9a6623a20d9865677ecdac70c","gasUsed":"0x592a","logs":[{"address":"0x234c5db1913e7a046387222ef3960576d22a90c3","topics":["0x24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da"],"data":"0x000000000000000000000000000000000000000000000000000000000000002a","blockNumber":"0x3ab","transactionHash":"0x85dae51f6a405c049c63354f210fd718a043612a2695a65145feaeedac7dec22","transactionIndex":"0x0","blockHash":"0x40b0928fee368e94adfecb3b33d0e29b12a36e5a525e203fd1b4ae2da58ed19e","logIndex":"0x0","removed":false}],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000200000000000000000000000002000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","root":"0x00ad10c4db648a69a37fdf87d3cdc29228807e00d9cd7d5d30b957f6303f2e27","to":"0x234c5db1913e7a046387222ef3960576d22a90c3","transactionHash":"0x85dae51f6a405c049c63354f210fd718a043612a2695a65145feaeedac7dec22","transactionIndex":"0x0"}} localhost:eth liu$
- 在实验中发现,一些因版本更新造成API发生变化的问题,以及solidity部分语法变化。
最后
以上就是开朗跳跳糖最近收集整理的关于以太坊部署调用合约的全部内容,更多相关以太坊部署调用合约内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复