How Ethereum contract can communicate with external data source

You will face into the problem when your product logic is implemented in a solidity and deployed to Ethereum network should start communicating with outside world. Ethereum virtual state machine cannot communicate with the outside world in the simple and an easy way. Why?

Because to approve each block-chain BLOCK validity, all mining nodes should get the same results from the outside. And it is not an easy task to deal with that.

After some time of research I will try to summarize my findings.

There are two ways how this can happen: Pull way or Push way.

Pull way:

To use this method you should trust 3rd party solution like Oraclize. This service is not free and you should pay them for all magic they do. Usage is a really simple example:

pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract DieselPrice is usingOraclize {

event newOraclizeQuery(string description);
event newDieselPrice(string price);
  function DieselPrice() {
update();
}
  function __callback(bytes32 myid, string result) {
if (msg.sender != oraclize_cbAddress()) throw;
newDieselPrice(result);
}
  function update() payable {        
newOraclizeQuery("Oraclize query was sent,
standing by for the answer..");

oraclize_query("URL","xml(https://www.fueleconomy.gov/
ws/rest/fuelprices).fuelPrices.diesel
");
}
}

Pros:
- Easy and fast to use, no need to think about implementation.
- Fixed cost. Gas spending will always be the same because they will call your contract __callback after they form the response (if the call is successful).

Cons:
- Oracle query can reach only this public API, that means that no data security available here. All data provided for contracts or published to Ethereum block chain instantly is public. It is a problem if you are working with sensitive data and don`t want to give the public access to your API.
- Full trust of Oracle. If Oracle gets hacked, your __callback calling can happen unexpectedly.
- Only the success case is covered. Not a lot of space to handle failures.
- Unclear how and who will pay for __callback execution.

Push way:

Pros:
- Open up data only needed for your calculations logic.
- You can control all process, no 3rd party components.
- More easily to track various communication issues and response statuses.

Cons:
- Complicated to implement and test it.
- Need Custom development and hosting for “Custom Executor”.
- Your front end application “PINGS” contract, which is not a very nice way to check result availability.

In both ways, your contract will trust an external wallet address, but in the second you can decide how to manage the security and balance of this address.

Overall, it is needed to remember one thing about Etherium VM, that this is a state machine, not a normal computer. Nowadays we think that everything can compute anything. We believe that in the future this global computer could do everything.

We will choose the push way for our implementation if we cannot find a better or smarter way. What do you think guys about such solution?

Sources: https://ethereum.stackexchange.com/questions/2/how-can-an-ethereum-contract-get-data-from-a-website

Check out our testing ground — Aigang!

More about Aigang — http://bit.ly/AigangWebsite
Aigang Latest Version of Whitepaper — http://bit.ly/AigangWhitepaper
Join the Aigang Slack here — http://bit.ly/AigangSlack
Aigang on Telegram — http://bit.ly/AigangTelegram
Get Aigang Latest Updates — http://bit.ly/AigangUpdates