20180716 区块链技术——摸爬滚打搭建私链

搭建自己的私链

step1:建立目录和genesis.json

 

step2:使用命令行,创造创世块 

    geth --datadri "./" init genesis.json

    geth中保存的是区块链的相关数据,keystore保存的是该条链中的用户信息

step3:创建自己的私链并打开控制台

    geth --datadir "./” --nodiscover console

step4:在自己的私链上创建用户

    eth.accounts //查看该条链上的账户

    personal.newAccount("pssword")//创建一个新的账户,该账户的密码是passowrd

step5:输出区块链的log

    geth --datadir "./" --nodiscover console 2>>geth.log

step6:开始挖矿

    miner.start()

step7:查看主账户以太币的数量

    acc0 = eth.accounts[0]

    eth.getBalance(acc0)

step8:在两个账户之间进行以太币转换

    acc0 =eth.accounts[0]

    acc1=eth.accounts[1]

    amount = web3.toWei(0.01)

    eth.sendTransaction({from :acc0 , to :acc0, value :amount})  //进行交易

step9:解锁账户 

    这个是以太坊的一个保护机制,每隔一段时间账户就会自动锁定,这个时候任何以太币在账户之间的转换都会被拒绝,除非把该账户解锁.

    personal.unlockAccount(acc0)   //需要输入密码

    在重启上面进行以太币转换

step10:部署合约

 

    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: '1000000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }

 })  //这是我们的合约

 

    a_demotypes

 a_demotypes.f.call(100)

 a_demotypes.f.call(125)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值