# Blockchain Technology for Intelligent Environments
## 2 Blockchains
### Smart Contracts  
_Smart Contracts_ bring another groundbreaking innovation to the blockchain world. Rather than using blockchains' decentralized trust model for offering just an immutable decentralized append-only data store, they exploit the mechanisms to provide a tamper-proof decentralized _world computer_. Through smart contracts, blockchains are promoted from special-purpose tools serving a single application (e.g., Bitcoin) to general-purpose platforms, allowing developers to deploy and execute custom code that may implement arbitrary application logic. A smart contract is essentially a program running on a ledger, maintaining some internal state, and updating this state through transactions. To allow transactions to be made, a smart contract exports an API consisting of one or more functions. Read-only functions, i.e., functions that only read the smart contract's program state without changing it, are executed locally on the node that calls them. Calling such functions does not create new transactions to the underlying ledger. Read-write functions, i.e., functions that update the smart contract's state, are executed on all nodes running the ledger, and require new transactions to be entered into the ledger. More specifically, all nodes that validate a block now, or any time into the future, have to execute all smart contract function calls included in that block, in order to check whether the resulting overall state matches the state recorded into the block's hash. Even more specifically, smart contracts gain their power through the following fundamental features:  
* **State storage:** It is possible for a smart contract to store its own state into a ledger. This makes it possible for a smart contract to store arbitrary data, not just data specific to Bitcoin or some other application. * **Turing-complete computation:** Many smart contracts enjoy a Turing-complete computation environment. This allows them to implement arbitrary logic for any type of custom ledger applications. * **Interaction with other contracts:** A smart contract can call functions of other contracts to interact with them. * **Input from the external world:** A smart contract can get input from external sources, such as what is the current cryptocurrency value in fiat money (euro, dollar, etc.). This can be done indirectly through interaction with other contracts, which, in turn, are updated by (trusted) external organizations pushing frequent updates to them. These, so-called, _oracles_ are discussed further in Section 4. * **Input from the ledger itself:** A smart contract is generally allowed to read and use any value in the ledger, such as the last block's hash value. For example, the block hash value can serve as a deterministic pseudo-random number generator. A platform supporting smart contracts essentially allows arbitrary applications to be developed, and to benefit from the same level of trust and transaction irreversibility enforced by a large community of participating nodes. The possibilities opening up with smart contracts are unlimited. Some examples can help to better understand possible scenarios:  
* **Multisignature wallets:** A group of two or more people may set up a shared wallet, configuring it in such a way that only the _combination_ of all signatures allows the withdrawal of arbitrary amounts, but a single signature _alone_ may spend e.g., up to 1% of the stored amount on a daily basis. * **Insurance:** A company could issue insurance through smart contracts, e.g., letting a farmer insure his crop against bad weather conditions. The smart contract would require the farmer to pay his insurance fees up to a given deadline, and would cover that farmer's potential loss based on input from a trusted weather-logging smart contract, which, in turn, would get its input from an externally trusted IoT system (an _oracle_).