欢迎来到天天文库
浏览记录
ID:34081457
大小:34.38 KB
页数:9页
时间:2019-03-03
《兄弟连go语言培训以太坊源码分析报告(55)以太坊随机数生成方式》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实用标准兄弟连Go语言+区块链技术培训以太坊源码分析(55)以太坊随机数生成方式最近考虑一个基于以太坊的去中心化赌场的实现,赌场如果需要实现,那么随机数是必须的。然后研究了一下以太坊里面的随机数生成,发现并不容易。以太坊里面生成随机数的几种方式。#oraclizeOraclize定位为去中心化应用的数据搬运工,他作为WebAPIs和DApp的可靠链接。有了Oraclize,就不需要建立额外的信任链,因为我们的行为已经被强制加密验证。Oraclize是一个可证明的诚实的预言机服务,可以让智能合约可以访问互
2、联网。Oraclize是平台无关的,为所有主流的智能合约能力平台提供一种虚拟的接口。可以想像,通过这个投入成千上万的有意义的数据到区块链中,可以使得智能合约产业更繁荣和更多有价值的应用呈现更大的生命力。Oraclize的使用方式可以参考下面的[代码](https://github.com/oraclize/ethereum-examples/blob/master/solidity/random-datasource/randomExample.sol)在update方法里面调用oraclize_new
3、RandomDSQuery方法来调用Oracle的智能合约的代码,Oracle根据请求来生成对应的数据,然后把结果通过回调__callback来传入。 /* Oraclizerandom-datasourceexample Thiscontractusestherandom-datasourcetosecurelygenerateoff-chainNrandombytes */ pragmasolidity^0.4.11; import"git
4、hub.com/oraclize/ethereum-api/oraclizeAPI.sol"; contractRandomExampleisusingOraclize{ eventnewRandomNumber_bytes(bytes); eventnewRandomNumber_uint(uint);文案大全实用标准 functionRandomExample(){ oraclize_setProof(proofType_Ledger);//se
5、tstheLedgerauthenticityproofintheconstructor update();//let'saskforNrandombytesimmediatelywhenthecontractiscreated! } //thecallbackfunctioniscalledbyOraclizewhentheresultisready //theoraclize_randomDS_proofVerifymodifierpreventsaninvalidp
6、rooftoexecutethisfunctioncode: //theproofvalidityisfullyverifiedon-chain function__callback(bytes32_queryId,string_result,bytes_proof) { //ifwereachthispointsuccessfully,itmeansthattheattachedauthenticityproofhaspassed! if(msg.sender!=orac
7、lize_cbAddress())throw; if(oraclize_randomDS_proofVerify__returnCode(_queryId,_result,_proof)!=0){ //theproofverificationhasfailed,doweneedtotakeanyactionhere?(dependsontheusecase) }else{ //theproofverificationhaspassed //nowthatweknow
8、thattherandomnumberwassafelygenerated,let'suseit.. newRandomNumber_bytes(bytes(_result));//thisistheresultingrandomnumber(bytes) //forsimplicityofuse,let'salsoconverttherandombytestouintifweneed uintmaxRan
此文档下载收益归作者所有