以太坊 oracle,在以太坊上简单实现Oracle预言机链接

背景

以太坊中的智能合约可以为广泛的应用提供动力,但由于区块链的性质,智能合约缺乏必要的功能:互联网连接。

以太坊被设计为完全确定性,这意味着如果有人下载整个网络历史并重放它们,它们应该总是以相同的状态结束。确定性是必要的,这样节点就可以达成一致。

但是,互联网具有不是确定性,在某个时间点查询API的智能合约,不能保证以后查询相同的API会得到相同的结果。 Web上的API和数据发生了变化。因此,智能合约本质上缺乏连通性。

oracle这个名字来源于这样一个事实:从历史上讲,oracles是事实的来源。这就是我们所需要的事实。

对于智能合约来说,预言机就是每个智能合约的输入参数。所有智能合约都绕不开预言机的输入数据,输入数据决定了智能合约的运行结果。通过向区块链中添加具有所需信息的交易,智能合约可以运行并始终获取相同的信息,因为数据都是从区块中进行检索。

解决方案

我们将创建一个oracle服务,该服务可以查询JSON API并从API响应中检索单个值。 oracle将保存所有请求和答案,并将拥有一组预定义的利益相关者。

利益相关者是运行node.js服务的帐户,该服务查询API并返回对oracle的响应。 oracle还具有必须接收的最小数量的相等响应,以确认所提供的答案是有效的。v

这样竞争方依赖于oracle来支持他们的合约,但是如果其中一方(节点)试图去操纵结果,那就无法实现了。因为他们同意预定义了法定人数的等量答案结果。

oracle包含两个组件。on-chain oracle(智能合约)和off-chain oracle服务(node.js服务器)。

on-chain oracle是一个智能合约,它有一个公共函数createRequest,接收URL,查询和要检索的属性。然后启动一个事件来提醒新链接oracle的新请求。

off-chain oracle由不同方部署的几个node.js服务组成,这些服务将查询API并将响应返回给合约。

on-chain Oracle会验证是否已达到最小数量的相等响应,如果已达到,则会发出一个事件,表明其已就价值达成共识,以便查询Oracle的客户机智能合约知道其已收到响应。

156272622764177.jpg

On-chain Oracle实施

我们用约定的条款定义Oracle合同:最低法定人数和Oracle总数。对于这个例子,有三个利益相关者,为了达成共识,3个中的2个必须提供相同的答案。

pragma solidity >=0.4.21 <0.6.0;

contract Oracle {

Request[] requests; //list of requests made to the contract

uint currentId = 0; //increasing request id

uint minQuorum = 2; //minimum number of responses to receive before declaring final result

uint totalOracleCount = 3; // Hardcoded oracle count

}

然后我们添加Request Struct,它将保存请求:

// defines a general api request

struct Request {

uint id; //request id

string urlToQuery; //API url

string attributeToFetch; //json attribute (key) to retrieve in the response

string agreedValue; //value from key

mapping(uint => string) anwers; //answers provided by the oracles

mapping(address => uint) quorum; //oracles which will query the answer (1=oracle hasn't voted, 2=oracle has voted)

}

现在我们可以创建公共函数createRequest,客户端智能合约(任何想要使用oracle服务的合同)都会调用:

function createRequest (

string memory _urlToQuery,

string memory _attributeToFetch

)

public

{

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

Request storage r = requests[lenght-1];

// Hardcoded oracles address

r.quorum[address(0x6c2339b46F41a06f09CA0051ddAD54D1e582bA77)] = 1;

r.quorum[address(0xb5346CF224c02186606e5f89EACC21eC25398077)] = 1;

r.quorum[address(0xa2997F1CA363D11a0a35bB1Ac0Ff7849bc13e914)] = 1;

// launch an event to be detected by oracle outside of blockchain

emit NewRequest (

currentId,

_urlToQuery,

_attributeToFetch

);

// increase request id

currentId++;

}

该功能包含利益相关者之间协议的重要部分。 受信任参与最终解决方案的帐户的地址。并且发出被off-chain oracle监听的事件NewRequest。

//event that triggers oracle outside of the blockchain

event NewRequest (

uint id,

string urlToQuery,

string attributeToFetch

);

在监听此事件后,off-chain Oracle将调用公共函数updateRequest。

//called by the oracle to record its answer

function updateRequest (

uint _id,

string memory _valueRetrieved

) public {

Request storage currRequest = requests[_id];

//check if oracle is in the list of trusted oracles

//and if the oracle hasn't voted yet

if(currRequest.quorum[address(msg.sender)] == 1){

//marking that this address has voted

currRequest.quorum[msg.sender] = 2;

//iterate through "array" of answers until a position if free and save the retrieved value

uint tmpI = 0;

bool found = false;

while(!found) {

//find first empty slot

if(bytes(currRequest.anwers[tmpI]).length == 0){

found = true;

currRequest.anwers[tmpI] = _valueRetrieved;

}

tmpI++;

}

uint currentQuorum = 0;

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

//have voted the same answer has the current one

for(uint i = 0; i < totalOracleCount; i++){

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

bytes memory b = bytes(_valueRetrieved);

if(keccak256(a) == keccak256(b)){

currentQuorum++;

if(currentQuorum >= minQuorum){

currRequest.agreedValue = _valueRetrieved;

emit UpdatedRequest (

currRequest.id,

currRequest.urlToQuery,

currRequest.attributeToFetch,

currRequest.agreedValue

);

}

}

}

}

}

此函数将首先检查调用者是否是预定义地址之一。 然后它会检查oracle没有投票,如果是,它将保存oracle答案。 然后它将检查该答案是否至少由所需的最低法定人数提供。 如果是这样,那么我们就结果达成一致,并将发出一个事件,即UpdatedRequest,以警告客户合同结果。

//triggered when there's a consensus on the final result

event UpdatedRequest (

uint id,

string urlToQuery,

string attributeToFetch,

string agreedValue

);

Off-chain Oracle实施

这是更简单的部分,它是可以监听发出的区块链事件和查询API的任何服务。

off-chain Oracle使用web3监听链上Oracle发出的事件,并查询请求的api,解析检索到的json以获取请求的密钥,并调用公共函数updateRequest。

您可以在github上找到off-chain oracle所有利益相关方应该部署的此服务的代码。https://github.com/pedroduartecosta/blockchain-ora...

这种实现允许不依赖于一方作为唯一一个查询API的真实来源,而是让多方就一个结果达成一致。它也是一个非常灵活的服务,因为它可以查询任何公共JSONAPI,允许在大量的用例中使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值