以太坊 私有链 挖矿挖矿怎么计算奖励,或者说是否可以设置奖励的算法.

区块链游戏有哪些
区块链技术让“云撸猫”成为现实,在线养宠物,招财猫、加密狗都在这里,最好玩的区块链游戏,国内最好玩的有意思的区块链游戏都在这里了,…
福德树v1.0他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)以太坊私有链创建及智能合约的部署和交互
招聘信息:
部署本机私有链区块链说白了就是一个个块链接起来的一个链表结果,所以要在本机生成一个自己的私有链首先要做的就是自己先创建一个块作为第一个将要生成的区块链的第一个区块(区块链叫做创世块),所以先生成一个json文件genesis.json,内容为创世块的内容(也是每个区块的包含的基本内容):{
&&&&&&&&&"nonce":"0x0042",
&&&&&&&&&"difficulty":"0x020000",
&&&&&&&&&"mixhash":"0x0000",
&&&&&&&&&"coinbase":"0x0000",
&&&&&&&&&"timestamp":"0x00",
&&&&&&&&&"parentHash":"0x0000",
&&&&&&&&&"extraData":"0x11bbe8db4e347b4e8c937c1ced33adb3db69cbdb7a38e1e50b1b82fa",
&&&&&&&&&"gasLimit":"0x4c4b40",
&&&&&&&&&"alloc":{}
}说明:mixhash:与nonce配合用于挖矿,由上一个区块的一部分生成的hash。nonce: nonce就是一个64位随机数,用于挖矿。difficulty: 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度alloc: 用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以我们不需要预置有币的账号,需要的时候自己创建即可以。coinbase: 矿工的账号,随便填timestamp: 设置创世块的时间戳parentHash: 上一个区块的hash值,因为是创世块,所以这个值是0extraData: 附加信息,随便填,可以填你的个性信息gasLimit: 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以可以填最大。创世块我们就这样创建好了。然后创建一个data文件夹用来存储区块数据和账号信息。本机创建私有链要确保安装以太坊的客户端https://geth.ethereum.org/downloads/。这一切准备工作完成后我们就可以开始初始化创世块及启动我们自己的私有链。初始化创世块创建脚本start_init.bat:
geth&--identity&"超神链"--datadir&"%cd%\data"&init&genesis.json
@pause说明:identity 区块标示,名称init 表示初始化区块,后面就是创世块的配置datadir 表示数据存放的位置创建私有链启动脚本startup.dat:geth&--identity&"超神链"--datadir&data&--networkid&123456&--rpc&--rpcaddr="0.0.0.0"--rpccorsdomain&"*"&--port&"30303"&--rpcapi"db,eth,net,web3"&--nodiscover&console说明:networkid 表示启动私有链的网络ID,已其他节点相连是网络IP要一致。rpc 开启rpc通信,可进行智能合约部署和调用。rpcaddr 区块链提供了对外RPC接口,默认是localhost,这个设置为可以IP访问。Rpccorsdomain 表示任何链接都可以连接到此节点。Port 该节点对外开放端口。Rpcapi 可以连接的客户端。console:启动命令行模式,可以在Geth中执行命令。其他的一些启动配置:Bootnodes:设置要连接的节点,后面跟其他节点的信息。如:--bootnodesenode://bfdb56ea443efa5b255a5bddb080ad2d34b6630deffeee60e2e6a3b9c305ae780b@10.132.97.29:30303console&所有前期准备做好以后分布启动初始化及启动脚本。启动成功后会进入ethereum的javascript控制台。如下:相关命令创建新账号personal.newAccount()或者 personal.newAccount("123456")查看节点信息admin.nodeInfo挖矿开始挖矿 miner.start(1)停止挖矿 miner.stop()查看当前矿工账号eth.coinbase默认为第一个账户修改矿工账号miner.setEtherbase(eth.accounts[1])查看账户信息eth.accounts[0]查看账户余额eth.getBalance(eth.accounts[0])或者 web3.fromWei(eth.getBalance(eth.accounts[0]),"ether")解锁账号personal.unlockAccount(eth.accounts[0])使用账户资金前都需要先解锁账号转账eth.sendTransaction({from:eth.accounts[0],to:"0x587e57afe970ff445d9",value:web3.toWei(3,"ether")})使用 txpool.status 可以看到交易状态查看区块数据&&&&&&&eth.blockNumber
&&&&&&eth.getTransaction("0x0c59fcbe9ef59bd7146edc8ff5ec37fea6710adcab825")
&&&&&&&eth.getBlock(1)通过区块号查看区块部署智能合约合约部署需要挖矿才能成功,我们新开一个窗口用与挖矿,新开一个控制台,输入命令:geth attach 连接到控制台,执行miner.start(1),开始挖矿。这里在网上找了个代币的只能合约,可以进行充值、转账和查询,issue 函数可以向充值以太到合约账户,transfer 函数可以向其他账号发送token,getBalance 函数可以获取某个账号的token余额,代码如下:pragma&solidity&^0.4.2;
contract&Token&{
&&&&&&&&&&&&&address&
&&&&&&&&&&&&&mapping&(address&=>&uint)&
&&&&&&&&&&&&&event&Issue(address&account,&uint&amount);
&&&&&&&&&&&&&event&Transfer(address&from,&address&to,uint&amount);
&&&&&&&&&&&&&function&Token()&{
&&&&&&&&&&&&&&&&&issuer&=&msg.
&&&&&&&&&&&&&}
&&&&&&&&&&&&&function&issue(address&account,&uintamount)&{
&&&&&&&&&&&&&&&&&if&(msg.sender&!=&issuer)&
&&&&&&&&&&&&&&&&&balances[account]&+=&
&&&&&&&&&&&&&}
&&&&&&&&&&&&&function&transfer(address&to,&uint&amount){
&&&&&&&&&&&&&&&&&if&(balances[msg.sender]&<&amount)
&&&&&&&&&&&&&&&&&balances[msg.sender]&-=&
&&&&&&&&&&&&&&&&&balances[to]&+=&
&&&&&&&&&&&&&&&&&Transfer(msg.sender,&to,&amount);
&&&&&&&&&&&&&}
&&&&&&&&&&&&&function&getBalance(address&account)constant&returns&(uint)&{
&&&&&&&&&&&&&&&&&return&balances[account];
&&&&&&&&&&&&&}
&&&&&&&&&}智能合约的部署需要编译,这里用在线编译: 修改编译好的gas和对象名称:varbrowser_untitled_sol_tokenContract&=web3.eth.contract([{"constant":false,"inputs":[{"name":"account","type":"address"},{"name":"amount","type":"uint256"}],"name":"issue","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"amount","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"account","type":"address"}],"name":"getBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"account","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Issue","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Transfer","type":"event"}]);
var&token&=browser_untitled_sol_tokenContract.new(
&&&&&from:&web3.eth.accounts[0],
&&&&data:&#39;0x0b000affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff5b5bf3ffffffffb063a9059cbb63f8b2cb4f5bbb803573ffffffffffffffffffffffffffffffffffffffffb005bb803573ffffffffffffffffffffffffffffffffffffffff5b005bb803573ffffffffffffffffffffffffffffffffffffffff5c565bffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373fffffffffffffffffffffffffffffffffffffffffd5b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff80fd5b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1673fffffffffffffffffffffffffffffffffffffffffddf252ad1be2c89b69c2b068fc378daa952ba7f163c4adf523b3ef808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffffa15b3ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff5baaafe007aac892e6dba9d032f540a11ff427d26ccbea2f0029&#39;,
&&&&&gas:&&#39;;
&&&},&function&(e,&contract){
&&&&console.log(e,&contract);
&&&&if&(typeof&contract.address&!==&#39;undefined&#39;)&{
&&&&&&&&&console.log(&#39;Contract&mined!&address:&#39;&+&contract.address&+&&#39;&transactionHash:&&#39;&+&contract.transactionHash);
&})将编译好的代码拷贝直接放到控制台即可。如果出现Error: account is locked undefined错误的话, 则使用personal.unlockAccount(eth.accounts[0],&#39;password&#39;)命令将用户解锁。Password为你创建账号的密码。一段时间后geth窗口就会出现Contract mined! address..., 表明合约代码发布成功。智能合约的调用合约部署成功后,在控制台可以直接调用。控制输入token,放回该合约的信息:这里重点就是address表示的是合约的地址,你会发现这个和账号的信息结果一样,其实你也可以把这个合约地址看做是一个账号地址,后面我们外部调用到的就是这个合约地址。控制台调用充值personal.unlockAccount(eth.accounts[0])
token.issue.sendTransaction(eth.accounts[0],100,&{from:&eth.accounts[0]});
miner.start(1)
miner.stop()发送 tokentoken.transfer(eth.accounts[1],&30,&{from:eth.accounts[0]})
miner.start(1)
miner.stop()查看余额token.getBalance()控制台调用就不多说,和java对象调用一样,直接调用即可。外部接口与智能合约交互以太坊对外提供的有很多接口JSON RPC接口,web3接口,这里我们用JSON RPC接口。相关API: 合约交互的原理合约的交互都是一次交易,而我们要做的就是把要调用的方法和参数按照api规定的以参数的形式向区块请求一次交易,ethereum接收到我们的请求后通过解析传递的参数来执行相关的合约代码。RPC接口给我们提供了俩个方法:eth_sendTransaction和eth_call。eth_sendTransaction
Createsnew&message&call&transaction&or&a&contract&creation,&if&the&data&field&containscode.
ParametersObject:The transaction objectfrom: DATA, 20 Bytes - The address the transaction is send from.to: DATA, 20 Bytes - (optional when creating new contract) The address the transaction is directed to.gas: QUANTITY - (optional, default: 90000) Integer of the gas provided for the transaction execution. It will return unused gas.gasPrice: QUANTITY - (optional, default: To-Be-Determined) Integer of the gasPrice used for each paid gasvalue: QUANTITY - (optional) Integer of the value send with this transactiondata: DATA - The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see nonce: QUANTITY - (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.可以看到,如果我们创建的为合约时,我们只需要from,to(文档上写的是可选的,但是实际操作中没有to为null的话合约不能正常执行,建议还是加上,这个值就是前面我们部署合约后生成的合约address),data。Data的属性的值具体可以看Contract ABI。这里大概说下:Data的值相对来说不是固定的,具体怎么生成与合约的参数类型,参数数量都有关联。这里我们以部署的token合约的三个方法为例:充值issue (address account, uintamount)这个方法有俩个参数,address充值账号,uint充值数量。根据Contract ABI,data值应该为方法名的sha3的前8个字节+参数的64字节,不够前面补充为0。这里方法名并不是issue (address account, uint amount)而是issue(address,uint256)的sha3值。我们往第一个账号充值10,这里的数据不是以太币,而是我们自己创建的代币。将10转换为16进制为000a那么data的数据为:0xfdf57ece821fdf7eb000a那么最后我们调用eth_sendTransaction方法传递参数JSON对象为:{
&&&&from:0xfdf57ece821fdf7eb,
to:0x7fe0cef1918b9abb3a3,
data:&0xfdf57ece821fdf7eb000a
}返回:此交易的hash值,此时该交易还没有执行,只是创建,还需要矿工通过挖矿才能完成。调用接口方法:JsonRpcHttpClient&client&=&newJsonRpcHttpClient(new&URL(“http://127.0.0.1:8545”));
Object&result&=&client.invoke(methodName,params,&Object.class);通过控制台查看第一个账号已有代币:执行调用接口代码。返回交易hash值:0xc3fea680faa5f9f8d24bc095此时控制台输入:说明此交易等待矿工处理。查看此时交易状态:发现这个交易中blockNumber还是null,因为此次交易还处于挂起状态,需要生成一个新的区块才能行。可以看到此次等待交易为1.现在开启挖矿。生成新的区块后我们停止挖矿,再来看看第一个账号此时的代币金额:代币增加了10个。我们再来看下此时的交易状态:来看下此次的交易详情:区块数据已经出来了,至此此次充值成功。交易和充值一样,需要注意的是代币转出账号为from属性的值,代币转入账号为data属性里的值,to对应的是合约地址。eth_call
Executes&anew&message&call&immediately&without&creating&a&transaction&on&the&block&chain.
Parameters1.Object - The transaction call objectfrom: DATA, 20 Bytes - (optional) The address the transaction is sent from.to: DATA, 20 Bytes - The address the transaction is directed to.gas: QUANTITY - (optional) Integer of the gas provided for the transaction execution. eth_call consumes zero gas, but this parameter may be needed by some executions.gasPrice: QUANTITY - (optional) Integer of the gasPrice used for each paid gasvalue: QUANTITY - (optional) Integer of the value send with this transactiondata: DATA - (optional) Hash of the method signature and encoded parameters. For details see2.QUANTITY|TAG - integer block number, or the string "latest", "earliest" or "pending", see the 这个方法返回一条信息给我们,相当于数据库的查询功能,参数也是三个,from,to,data,数据格式都是一样的。查询getBalance(address account)查询方法hash码:查询我们上一步充值的账号,那么传递的参数data为:0xf8b2cb4ffdf57ece821fdf7ebeth_call方法最后参数为:{
&&&&&&&from:&0xfdf57ece821fdf7eb,
to:0x7fe0cef1918b9abb3a3,
data:0xf8b2cb4ffdf57ece821fdf7eb
}注意,这个方法需要俩参数,处理一个JSONobject外,还有一个字符串参数,这俩可以为“”或者"latest", "earliest" or "pending"调用接口返回一个16进制字符串:0x0071就是该账号的代币数量,转换为十进制为:113,与控制查询一致。这就是一个智能合约的交互过程。是不是很简单啊。
微信扫一扫
订阅每日移动开发及APP推广热点资讯公众号:CocoaChina
您还没有登录!请或
点击量3857点击量3313点击量3254点击量3238点击量3009点击量2946点击量2725点击量2686点击量2474
&2016 Chukong Technologies,Inc.
京公网安备89

我要回帖

更多关于 以太坊挖矿奖励 的文章

 

随机推荐