http://www.7klian.com

在以太坊上简朴实现Oracle预言机链接

if(keccak256(a) == keccak256(b)){
//called by the oracle to record its answer
string agreedValue; //value from key
for(uint i = 0; i < totalOracleCount; i++){

}

// defines a general api request
string urlToQuery,
bytes memory b = bytes(_valueRetrieved);
mapping(address => uint) quorum; //oracles which will query the answer (1=oracle hasn't voted, 2=oracle has voted)

//iterate through "array" of answers until a position if free and save the retrieved value
if(currRequest.quorum[address(msg.sender)] == 1){
);


// increase request id
uint currentId = 0; //increasing request id
_urlToQuery,
以太坊中的智能合约可觉得遍及的应用提供动力,但由于区块链的性质,智能合约缺乏须要的成果:互联网毗连。


您可以在github上找到off-chain oracle所有好处相关方应该陈设的此处事的代码。https://github.com/pedroduartecosta/blockchain-ora...



r.quorum[address(0x6c2339b46F41a06f09CA0051ddAD54D1e582bA77)] = 1;
currRequest.quorum[msg.sender] = 2;
event NewRequest (
}
string agreedValue


string memory _urlToQuery,

currRequest.anwers[tmpI] = _valueRetrieved;

uint id,
uint id,
currRequest.attributeToFetch,
该成果包括好处相关者之间协议的重要部门。 受信任参加最终办理方案的帐户的地点。而且发出被off-chain oracle监听的事件NewRequest。
// Hardcoded oracles address
}
) public {
然后我们添加Request Struct,它将生存请求:

uint lenght = requests.push(Request(currentId, _urlToQuery, _attributeToFetch, ""));
function createRequest (

currentId,
好处相关者是运行node.js处事的帐户,该处事查询API并返回对oracle的响应。 oracle还具有必需吸收的最小数量的相等响应,以确认所提供的谜底是有效的。v



Request storage currRequest = requests[_id];
//and if the oracle hasn't voted yet


string urlToQuery; //API url



uint _id,


我们将建设一个oracle处事,该处事可以查询JSON API并从API响应中检索单个值。 oracle将生存所有请求和谜底,并将拥有一组预界说的好处相关者。

bytes memory a = bytes(currRequest.anwers[i]);
function updateRequest (


这样竞争方依赖于oracle来支持他们的合约,可是假如个中一方(节点)试图去哄骗功效,那就无法
实现了。因为他们同意预界说了法定人数的等量谜底功效。



Off-chain Oracle实施

on-chain Oracle会验证是否已到达最小数量的相等响应,假如已到达,则会发出一个事件,表白其已就代价告竣共鸣,以便查询Oracle的客户机警能合约知道其已收到响应。

我们用约定的条款界说Oracle条约:最低法定人数和Oracle总数。对付这个例子,有三个好处相关者,为了告竣共鸣,3其中的2个必需提供沟通的谜底。
string attributeToFetch; //json attribute (key) to retrieve in the response

while(!found) {
bool found = false;

off-chain Oracle利用web3监听链上Oracle发出的事件,并查询请求的api,理会检索到的json以获取请求的密钥,并挪用民众函数updateRequest。
oracle这个名字来历于这样一个事实:从汗青上讲,oracles是事实的来历。这就是我们所需要的事实。

这是更
简朴的部门,,它是可以监听发出的区块链事件和查询API的任那里事。
//have voted the same answer has the current one
}
Request[] requests; //list of requests made to the contract
uint currentQuorum = 0;
uint tmpI = 0;
//triggered when there's a consensus on the final result
if(currentQuorum >= minQuorum){



//iterate through oracle list and check if enough oracles(minimum quorum)


tmpI++;
//find first empty slot
}
此刻我们可以建设民众函数createRequest,客户端智能合约(任何想要利用oracle处事的条约)城市挪用:
);
_attributeToFetch

currRequest.agreedValue
可是,互联网具有不是确定性,在某个时间点查询API的智能合约,不能担保今后查询沟通的API会获得沟通的功效。 Web上的API和数据产生了变革。因此,智能合约本质上缺乏连通性。


currentId++;

}
if(bytes(currRequest.anwers[tmpI]).length == 0){
在监听此事件后,off-chain Oracle将挪用民众函数updateRequest。



public

r.quorum[address(0xb5346CF224c02186606e5f89EACC21eC25398077)] = 1;
mapping(uint => string) anwers; //answers provided by the oracles
Request storage r = requests[lenght-1];
对付智能合约来说,
预言机就是每个智能合约的输入参数。所有智能合约都绕不开预言机的输入数据,输入数据抉择了智能合约的运行功效。通过向区块链中添加具有所需信息的生意业务,智能合约可以运行并始终获取沟通的信息,因为数据都是从区块中举办检索。

);
uint minQuorum = 2; //minimum number of responses to receive before declaring final result
string memory _valueRetrieved
oracle包括两个组件。on-chain oracle(智能合约)和off-chain oracle处事(node.js处事器)。

uint id; //request id
currRequest.urlToQuery,

currentQuorum++;

found = true;


string attributeToFetch
event UpdatedRequest (


}
string urlToQuery,


emit NewRequest (
currRequest.id,

)

On-chain Oracle实施
string attributeToFetch,
}


struct Request {



off-chain oracle由差异方陈设的几个node.js处事构成,这些处事将查询API并将响应返回给合约。

{
emit UpdatedRequest (
此函数将首先查抄挪用者是否是预界说地点之一。 然后它会查抄oracle没有投票,假如是,它将生存oracle谜底。 然后它将查抄该谜底是否至少由所需的最低法定人数提供。 假如是这样,那么我们就功效告竣一致,并将发出一个事件,即UpdatedRequest,以告诫客户条约功效。
以太坊被设计为完全确定性,这意味着假如有人下载整个网络汗青并重放它们,它们应该老是以沟通的状态竣事。确定性是须要的,这样节点就可以告竣一致。


pragma solidity >=0.4.21 <0.6.0;

contract Oracle {
//event that triggers oracle outside of the blockchain
string memory _attributeToFetch
}



);
currRequest.agreedValue = _valueRetrieved;
uint totalOracleCount = 3; // Hardcoded oracle count
这种
实现答允不依赖于一方作为独一一个查询API的真实来历,而是让多方就一个功效告竣一致。它也是一个很是机动的处事,因为它可以查询任何民众JSONAPI,答允在大量的用例中利用。

办理方案


//check if oracle is in the list of trusted oracles
r.quorum[address(0xa2997F1CA363D11a0a35bB1Ac0Ff7849bc13e914)] = 1;
on-chain oracle是一个智能合约,它有一个民众函数createRequest,吸收URL,查询和要检索的属性。然后启动一个事件来提醒新
链接oracle的新请求。

//marking that this address has voted
// launch an event to be detected by oracle outside of blockchain
}

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

相关文章阅读