How to use machine learning algorithms as Oracles in Smart Contracts?

Smart Contracts enable a diversity of new blockchain-based applications. However, due to the consensus strategy of the blockchain, Smart Contracts cannot query data from the outside world, thereby requiring the use of Oracles. Thus, some proposals have been presented in the last years with the objective of collecting and storing external information into blockchain to be used by smart contracts. For example, Provable provides an excellent API to handle information available online and Augur (and Gnosis) provides reliable indicators regarding the sentiment of future facts.

We present an alternative way to build Oracles for Smart Contracts by using machine learning algorithms from event knowledge datasets. An event is defined as “a particular thing which happens at a specific time and place” and is represented by components, such as place of occurrence (where), date of publication (when), textual information (what), and related persons and organizations (who). Thus, given an event of interest, machine learning algorithms are used to verify the future occurrence of this event. While most existing Oracles solutions collect pre-structured information, machine learning allows the construction of more semantic indicators by analyzing event content. For example, in the field of agribusiness, it is possible to construct a machine learning classifier to verify the occurrence of pests and diseases attacking a given agricultural product. Smart contracts can then be used as a decentralized insurance contract for farmers without a third party.

Can we trust that a classifier has been trained and executed correctly?

We need to define some rules among those involved in a Smart Contract (i.e. Stakeholders) to use classifiers as a Oracles:

  1. Stakeholders should agree on the classifier training set. The training set contains a (representative) sample of events.
  2. Stakeholders should agree on a stream source for new events. These events are presented to the classifier to find out if they are similar (given a confidence level) to the training set.
  3. Stakeholders should agree on the choice of a classification algorithm and its parameters.
  4. The output of a classifier will be published into Blockchain and the Smart Contract will make decisions based on this output.

After training a machine learning classifier, events of interest are classified whenever a user requests the Oracle status update. However, we need to ensure that a malicious party does not manipulate the classifier output and the execution of the smart contract!

To give reliability in the execution of a classifier, we use the iExec — Blockchain-Based Decentralized Cloud Computing.

iExec is the first blockchain-based decentralized marketplace for cloud resources, powering applications in the fields of artificial intelligence, fintech, scientific research or 3D rendering.

In particular, our interest is in the PoCo Protocol (Proof-of-Contribution), which allows consensus of off-chain computations directly on the blockchain. In short, our classifier runs on different nodes until it reaches consensus. In addition, iExec also offers distributed computing as a commodity, which allows us to run the classifiers at a low cost.

Let’s see an example!

Let’s consider a hypothetical scenario involving a Smart Contract that will make some decision depending on the occurrence of earthquake events.

First, we define a sample of events to define the classifier training set and the classifier prediction stream. In this example, we will use news-extracted events about earthquakes to demonstrate the potentialities of an artificial intelligence-based Oracle. There are several platforms for event analysis that can be used as an event data source. We use a platform called Websensors, as shown in the figures below.

Example of selecting a sample of events considering a geographic region.
Example of selected events for the classifier training set, the stream source of new events, and the classifier parameters.

Next, we need to choose a classifier and its training parameters. For this example, we use a variation of the One-Class-SVM classifier. All parameters and information required for the classifier can be serialized into a JSON configuration file. In particular, it is preferable that this JSON file is stored with guarantees of immutability, for example, by using IPFS.

We write and publish a DApp on the iExec platform to run the classifier. The output of the classifier after the PoCo consensus is stored in the Ethereum Blockchain (Kovan Testnet). Thus, any Smart Contract can use this output for decision making. The figure below illustrates the execution of the classifier in the iExec Marketplace.

iExec Marketplace

The execution log of the classifier on the iExec is also stored for external verification and validation. Each classifier run receives a unique TASK ID, which is publicly available. In our example, we trained the classifier to return the integer 2 if new similar events from stream source were identified (given 75% of confidence). The classifier output was stored in this Smart Contract that will act as Oracle.

Example of an Oracle that stores the output of a machine learning classifier.

The example presented here can be accessed at https://play.websensors.net.br/sensor/10000008. Feel free to run this example again.

Conclusions

We believe that the integration between Artificial Intelligence and Blockchain will enable a new generation of Smart Contracts. We have developed Websensors-Dapp for iExec as a solution for Artificial Intelligence-based Oracles and the results are promising! Moreover, any other event analysis platform or machine learning framework can (and should) be evaluated for this task.

You can train your own Artificial Intelligence-based Oracle. We have developed some tools that can help you with this task:

Websensors Builder: Interface to define the URL with a sample of training events, event stream source for prediction and configuration of the classifier parameters. You will receive a unique classifier ID (which we call sensor) that is used as a parameter for the Dapp Websensors on the iExec MarketPlace platform.

Websensors Explorer: Optionally, if you want to use our platform as an event source, feel free to use this tool. Here you can search for events and build URLs (endpoints) with events for both training and prediction.

In the next articles, we will present more detailed examples of applications that can be executed using Artificial Intelligence-based Oracles, especially decentralized insurance contracts written with the Solidity language.

This is an academic experiment, developed at the Federal University of Mato Grosso do Sul, Brazil. The classifier source code and Websensors Dapp for iExec are available for free to promote the development of other related solutions that integrate Artificial Intelligence and Blockchains technologies.

--

--