Blockchain Oracle Design Patterns — Explained

Harshana Serasinghe
Coinmonks

--

Header Image for the article
Prediction vector created by vectorjuice — www.freepik.com

A Smart Contract provides the security, transparency, efficiency and trust to initiate an agreement inside a blockchain. Since it is executed inside a blockchain, it cannot access any external data such as weather data, or currency rates. Blockchain Oracles are designed to mitigate this issue.

An oracle is a program which can provide external data to a smart contract. In this article, let’s discuss different patterns that can be used to design oracles.

The Key Steps of an oracle

There are 3 key steps of an oracle that will be used by a smart contract. All the design patterns that we’re going to discuss in this article, use these steps:

  1. Collect data from an off-chain source
  2. Transfer the data with a signed message
  3. Make the data available by putting it in a smart contract’s message

Types of design patterns

The design patterns are divided into two types depending on the below factors

  1. The frequency of extracting data
  2. The flowing direction of data

Patterns originated from the Frequency of Extracting Data

1 — Immediate-Read

A smart contract which is used by a book store can use an oracle to provide the ISBN of a specific book. In this case, the oracle only stores the name and the ISBN of the book inside the contract storage since it is excessive to store attributes such as author name, year etc.
Example for Immediate-Read Design Pattern

Oracles that allow getting data which will be used to take immediate decisions. This type of oracle stores the data in the smart contract storage. The data will be updated from time to time. Since it uses contract storage, only the data that is highly relevant for the use case will be stored.

Example:

A smart contract which is used by a book store can use an oracle to provide the ISBN of a specific book. In this case, the oracle only stores the name and the ISBN of the book inside the contract storage since it is excessive to store attributes such as author name, year etc.

2 — Publish-Subscribe

Let’s imagine that there is an exchange in the decentralized world, which will allow you to buy stocks. The smart contract of this exchange should know the latest price of a specific stock or all the stocks. The smart contract can then subscribe to an oracle which will provide the prices. From the side of the smart contract, it can either poll the oracle or the oracle can push the updates to all his clients.
Example for Publish-Subscribe Design Pattern

Oracles which provide data upon subscribing to the oracle belong to this pattern. It provides a broadcast service of the data to the smart contracts. The data will be provided to the smart contract via polling from the smart contract or running a worker off-chain and watching for updates in the oracle.

Example:

Let’s imagine that there is an exchange in the decentralized world, which will allow you to buy stocks. The smart contract of this exchange should know the latest price of a specific stock or all the stocks. The smart contract can then subscribe to an oracle which will provide the prices. From the side of the smart contract, it can either poll the oracle or the oracle can push the updates to all his clients.

3 — Request-Response

Prof.Jayson wants to know the marks for Mathematics from the year 1998 to 2000 at the WONDER university. Since this university uses blockchain and smart contracts to store a part of their student’s data, the smart contract responsible to provide this query has to call the oracle and get the data back. After the smart contract calls the oracle, the oracle will pass the query to a server which has all the data records. Finally, the server will execute the query and pass the results back to the ora
Example for Request-Response Design Pattern

The oracles designed by using this pattern are much more advanced than the previous ones. This pattern is similar to the client-server architecture where a request is sent from the client and is processed by the server. The data relating to this oracle might be stored in an external infrastructure because it uses a larger dataset that cannot be stored in smart contract storage. Because of this scenario and the necessity for extra performance, these types of oracles use off-chain infrastructure such as servers.

Example:

Prof.Jayson wants to know the marks for Mathematics from the year 1998 to 2000 at the WONDER university. Since this university uses blockchain and smart contracts to store a part of their student's data, the smart contract responsible to provide this query has to call the oracle and get the data back. After the smart contract calls the oracle, the oracle will pass the query to a server which has all the data records. Finally, the server will execute the query and pass the results back to the oracle and the oracle will pass the data back to the smart contract.

Patterns originated from the flowing direction of data

1 — Inbound Oracle

An inbound oracle transmits data from the outside or off-chain world to the blockchain. There are two sub-patterns that can be identified in an inbound oracle
Example for Inbound Oracle Design Pattern

An inbound oracle transmits data from the outside (or off-chain) world to the blockchain. There are two sub-patterns that can be identified in an inbound oracle

  1. Pull-based inbound Oracle — When a smart contract needs data, it requests the data from the oracle. After the oracle receives the request, it gathers the information from the off-chain data source and provides it back to the blockchain.
  2. Push-based inbound Oracle — Oracle watches for any sort of changes in a particular off-chain data source. When there is a change in the data source, the changes will be transmitted to the blockchain.

2 — Outbound Oracle

An outbound oracle transmits data from the blockchain to the outside world. This is useful when there are data such as financial transactions that happen inside the blockchain and need to be communicated to relevant parties via an off-chain resource. There are two sub-patterns in this oracle as well.
Example for Outbound Oracle Design Pattern

An outbound oracle transmits data from the blockchain to the outside world. This is useful when there are data such as financial transactions that happen inside the blockchain and need to be communicated to relevant parties via an off-chain resource. There are two sub-patterns in this oracle as well.

  1. Pull-based outbound oracle — When an off-chain resource needs to query data from the blockchain, it requests the data from the on-chain data source. After receiving the request, it provides the data.
  2. Push-based outbound oracle — The smart contract watches for any changes in the blockchain data set. When there is a change, it will be communicated to the off-chain resource.

I hope this article has helped you to understand what are the design patterns that are used to create blockchain oracles. See you again in another article :)

References:

[1] Andreas Antonopoulos, Mastering Ethereum: Building Smart Contracts and DApps

[2] R. Mühlberger, S. Bachhofner, E. Castelló Ferrer, C. D. Ciccio, I. Weber, M. Wöhrer, U. Zdun, Foundational Oracle Patterns: Connecting Blockchain to the Off-chain World

[3] A. Beniiche, A Study of Blockchain Oracles

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read

--

--

Harshana Serasinghe
Coinmonks

In love with tech ever since I got to know what a computer is :)