基于以太坊智能合约平台搭建

本文是作者亲自搭建的过程,写的相对比较粗糙,实测可用:
安装操作操作步骤请参考一下博客
https://zhuanlan.zhihu.com/p/27106175
https://zhuanlan.zhihu.com/p/32911405

执行机器路径为
root@ubuntu2:/opt/eth/test#

//执行命令,制造创世区块

geth --datadir "./data0" init genesis.json

//执行一条最简单的geth命令,来创建自己的私有链条。这也是进入geth客户端的命令。
geth --datadir "./data0" --nodiscover console 2>>geth.log

//输入命令eth.accounts, 我们会发现返回值为[]
输入命令personal.newAccount("xxx"), 该命令将创造一个新的用户,该用户的密码是xxx. 当然用户也可以将xxx 改为123,或者123456,或者任意密码

//查看日志
tail -f geth.log

//启动挖
miner.setEtherbase(eth.accounts[0])
miner.start()

//查看是否在工作
eth.mining
true (true表示在工作)

//进入已经存在的JS窗口
>geth attach "/opt/eth/test/data0/geth.ipc"

//查看已经存在的节点
>eth.accounts[0]
"0xcafacf0d849e50a1bd01b32e7944661df3338aa4"

//查看区块高度
>eth.blockNumber

//查看节点中的wei数量
> eth.getBalance(eth.accounts[0])

//查看节点中的eth数量
web3.fromWei(eth.getBalance(eth.accounts[0]))

//wei和eth进行转换
Wei –> Ether: web3.fromWei
> web3.fromWei(10000000000000000)
"0.01"

//添加节点
当另外的节点完成上述步骤后就可以开始添加节点了。在geth console中输入:admin.addPeer(“”),在双引号中输入要连接的节点信息。节点信息通过在geth console中输入命令admin得到,enode项的信息就是节点的地址。

//进行转账
1.首先停止
miner.stop()
2.保证有2个以上地址,给两个地址赋值
user1=eth.accounts[0]
user2=eth.accounts[1]
3.解锁user1
personal.unlockAccount(user1)

4.将user1转给user2,转3个,单位为ether
eth.sendTransaction({from:user1,to:user2,value:web3.toWei(3,"ether")})

//智能合约1
var a_demotypesContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"f","outputs":[{"name":"b","type":"uint256"}],"payable":false,"type":"function"}]);
var a_demotypes = a_demotypesContract.new(
   {
     from: web3.eth.accounts[0],
     data: '0x6060604052341561000c57fe5b5b60ab8061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063b3de648b14603a575bfe5b3415604157fe5b60556004808035906020019091905050606b565b6040518082815260200191505060405180910390f35b600060006008830290508091505b509190505600a165627a7a7230582010decdc0b0a43b565814fe904eae2544665457d6353c7d906fc2c43c81c867e40029',
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })
//此时一直让挖矿,执行打包
miner.start()

//验证输入一个值返回为值乘以8 、

a_demotypes.f.call(100)

//智能合约2


//将以下拷贝到geth中
    var helloContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"main","outputs":[{"name":"b","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]);
    var hello = helloContract.new(
       {
         from: web3.eth.accounts[0],
         data: '0x6060604052341561000f57600080fd5b60b68061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063ab3ae255146044575b600080fd5b3415604e57600080fd5b606260048080359060200190919050506078565b6040518082815260200191505060405180910390f35b600080600883029050809150509190505600a165627a7a723058203f6d4f506de85c5287b58705788b98694a2b14b17fd6db0c376260b4a0b5a7a00029',
         gas: '4700000'
       }, function (e, contract){
        console.log(e, contract);
        if (typeof contract.address !== 'undefined') {
             console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
        }
     }) 
 //此时一直让挖矿,执行打包
miner.start()
//验证输入一个值返回为值乘以8

hello.main.call(100)

---异常信息
Error: authentication needed: password or unlock undefined
undefined
//需要注意,在部署只能合约时,出现如下错误表示账户没解锁。需要执行  personal.unlockAccount(eth.accounts[0]) 进行解锁。

显示如下日志信息表示已经正常挖矿

//geth中日志

Contract mined! address: 0x025f940e33084168a3b8f3c2d1ead767126604ac transactionHash: 0xa086fec940265a0ba1ffa07e573b12b6855840f88039ce932efc62fa060a51ec
null [object Object]
Contract mined! address: 0x0f74eeac87e2e9fc2f6cad3d3debe427ea1ec264 transactionHash: 0x8161f5011ff947d780befa3028916b7b6282b26b7bf5e06f56c1ce5fa00aab41
null [object Object]
Contract mined! address: 0x14c68ba552996c70bebb20033ad9819930578ea4 transactionHash: 0x1d78d9caa9654989cf83dbb019af008f153c7abfdc94779b1d0794aeec0e6a59
//geth.log 中日志
INFO [02-09|16:18:16] 🔨 mined potential block                  number=7 hash=1a8de5…7be496
INFO [02-09|16:18:16] Commit new mining work                   number=8 txs=0 uncles=0 elapsed=1.357ms
INFO [02-09|16:20:43] Submitted contract creation              fullhash=0xa086fec940265a0ba1ffa07e573b12b6855840f88039ce932efc62fa060a51ec contract=0x025F940e33084168a3b8f3C2D1eaD767126604AC
INFO [02-09|16:37:10] Submitted contract creation              fullhash=0x8161f5011ff947d780befa3028916b7b6282b26b7bf5e06f56c1ce5fa00aab41 contract=0x0F74eEaC87e2e9Fc2F6Cad3D3DEbE427EA1ec264
INFO [02-09|16:52:58] Submitted contract creation              fullhash=0x1d78d9caa9654989cf83dbb019af008f153c7abfdc94779b1d0794aeec0e6a59 contract=0x14C68Ba552996C70BEbb20033AD9819930578EA4
INFO [02-09|16:58:03] Regenerated local transaction journal    transactions=3 accounts=1
INFO [02-09|17:04:32] Successfully sealed new block            number=8 hash=14082a…1a5cb2


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章