Smart contracts are a groundbreaking innovation in blockchain technology that enable the execution of self-executing contracts with the terms of the agreement written directly into code. Bitcoin’s scripting language has paved the way for several applications of smart contracts, such as the implementation of escrowed payment transactions. However, the scripting language’s limitations, which include a small instruction set that is not Turing complete, have led to the development of new altcoins that propose adding application-specific functionality to support smart contracts.
Namecoin was the first altcoin to introduce smart contract functionality, but many others have followed suit. These cryptocurrencies, similar to Bitcoin, have been designed to support various use cases, including gambling, stock issuance, prediction markets, and more. The rise of smart contract-enabled blockchains has opened up new possibilities for creating decentralized applications, which can execute complex transactions and enforce agreements automatically without the need for intermediaries. This has the potential to revolutionize industries and create entirely new business models.
Contents
- 1 The Promise of smart contract: A Turing-Complete Cryptocurrency for Any Application
- 2 smart contract in the early days
- 3 Smart Contract Programming Model
- 4 A Simple Example: Namecoin in Ethereum
- 5 Gas, Incentives, and Security
- 6 3 powerful strategies for implementing Namecoin-style functionality using smart contracts on Ethereum
- 7 Conclusion
The Promise of smart contract: A Turing-Complete Cryptocurrency for Any Application
What if, instead of needing to launch a new system to support every application, we built a cryptocurrency that could support any application we might dream up in the future? This is what Turing completeness is all about: a Turing complete programming language lets you specify any functionality that is possible to program into a Turing machine, an abstract model of a computer that is believed to be capable of computing any function that can be computed at all.
As a consequence, every Turing-complete programming language—including familiar ones, such as Java, Python, and Lisp—is identical in the set of computations that it allows to be expressed. In a certain theoretical sense, Turing completeness is the best we can hope for in a programming language and bitcoin mechanism in terms of expressive power, ignoring practical matters, such as simplicity and performance.
smart contract in the early days
To some extent, the situation today harkens back to the early days of computers themselves in the 1940s: increasingly complicated machines were being built for various specific applications during World War II (such as brute-forcing keys used by mechanical cipher machines or determining firing trajectories for naval artillery), motivating researchers to build the first reprogrammable general-purpose computers that could be used for any conceivable applications (Figure 1.1).
Ethereum is an ambitious altcoin that aims to provide a Turing-complete programming language for writing scripts or “smart contract.” While there are other proposals to do this, Ethereum is the most notable: it introduced several novel technical ideas; held a successful crowd-funding campaign, raising $20 million over several months; and adopted aggressive choices for parameters, such as block time. In this section, we provide a brief overview of Ethereum though the system is complex enough that we could easily devote an entire second book to it!
Smart Contract Programming Model
The term smart contract was first used to describe the use of computer systems (or other automated means) to enforce contracts. As an example, you could think of a vending machine as a mechanical smart contract that enforces an agreement between you and the machine’s owner involving the purchase of a candy bar.
In Ethereum, a contract is a program that lives on the block chain. Anybody can create an Ethereum contract, for a small fee, by uploading its program code in a special transaction. This contract is written in bytecode and executed by a special Ethereum-specific virtual machine, usually just called “EVM.” Once uploaded, the contract will live on the block chain. It has its own balance of funds, other users can make procedure calls through whatever API the program exposes, and the contract can send and receive money.
A Simple Example: Namecoin in Ethereum
We claimed that Ethereum can be used to implement any application-specific altcoin’s functionality. As a simple example, we can show how to implement Namecoin-style functionality in a simple Ethereum contract.
One example implementation is shown in Figure 1.2. It is coded in Solidity, Ethereum’s high-level programming language for defining contracts. This contract implements a crude name/value store or name registry, in which names are assigned values once and for all. The contract defines a data variable, registryTable, which is a mapping from 32-byte strings to public keys. Initially, it maps every string to the null address 0x0000000000 … 000.
This contract also defines a single entry point, called claimName. This entry point accepts a single argument, name. First, the contract makes sure that the caller has sent a value of at least 10 wei, wei being the smallest currency unit in Ethereum. If insufficient funds have been sent, the contract terminates with an error (the throw statement does this), and no action is taken. If sufficient funds are sent and the name is not yet taken, then it is permanently assigned the value of whichever address invoked this function.
Other features of Namecoin
That’s all this contract can do in eight lines of code. But we could add all the other features of Namecoin with a little more work. For example, we could store more data with each mapping than just the address of the entity that claimed it. We could require name owners to re-register periodically by storing a “last updated” time and allowing other users to claim names that haven’t been updated in a long time.
We might also want to add a second function to allow the money to be withdrawn. As currently programmed, the money will just accumulate in the contract forever, essentially being removed from circulation. Of course, in the function allowing money to be withdrawn, we’d better make sure to check that the caller is the owner of the contract. Anybody can call any function on an Ethereum contract, but the calls are signed, so we can securely identify who the caller is.
Gas, Incentives, and Security
Unlike Bitcoin, Ethereum supports loops, although we didn’t need them in our first example. That should immediately raise alarm bells. If there are loops, there can be infinite loops. In general, Ethereum contracts might run forever for a variety of reasons. A famous result in computer science (the undecidability of the Halting Problem) states that there’s no algorithm that can look at a program’s source code and always correctly determine whether it will run forever or not. So how can we prevent contracts from running forever?
More generally, we need some way to limit contracts that take a long time to run, even if that time is finite. Ethereum uses a mechanism called gas to achieve this. Essentially, executing each virtual-machine instruction costs a small amount of money (gas). Different operations cost different amounts. Basic operations like addition or comparison cost 1 gas, whereas computing a SHA-3 hash (available as a built-in instruction) costs 20 gas, and writing a 256-bit word to persistent storage costs 100 gas.
Every transaction also costs 21,000 gas right off the bat. You can think of Ethereum like flying on an ultra-discount airline: you pay to get on board and you pay extra for everything you do from there. The complete list of instructions available in Ethereum and the gas cost of each is fixed; changing these would require a hard fork, just like changing the semantics of Bitcoin’s scripting language would.
3 powerful strategies for implementing Namecoin-style functionality using smart contracts on Ethereum
Here are three powerful strategies for implementing Namecoin-style functionality using smart contracts on Ethereum:
Decentralized Domain Name System (DNS):
The Domain Name System (DNS) is a critical component of the internet that enables the translation of human-readable domain names into IP addresses that computers can understand. However, the traditional DNS system is centralized, meaning that domain names are registered and managed by a single central authority, such as a domain registrar.
A decentralized DNS system, on the other hand, removes the need for a central authority and instead distributes the domain name registration and resolution process across a network of nodes. This makes it more secure and resistant to censorship, as there is no single point of failure or control.
By using smart contracts on the Ethereum blockchain, a decentralized DNS system can be created that allows for secure and censorship-resistant domain name registration and resolution. Smart contracts can be used to create a system where domain names are registered and managed on a decentralized network of nodes, with ownership of the domain name tied to a specific Ethereum address. This ensures that domain names cannot be seized or censored by a central authority, as ownership is distributed across the network.
In addition, the use of smart contracts allows for the creation of programmable rules and conditions for domain name ownership and management. For example, a smart contract could be created that automatically transfers ownership of a domain name to a new owner once certain conditions are met, such as the completion of a payment or the transfer of a specific token.
Overall, a decentralized DNS system using smart contracts on Ethereum offers numerous benefits, including greater security, privacy, and resistance to censorship, as well as the ability to create programmable rules and conditions for domain name ownership and management.
Decentralized Identity System:
A decentralized identity system using smart contracts on Ethereum involves creating a system that enables individuals to control and manage their own digital identities without relying on centralized third-party authorities such as governments, social media companies, or banks.
Traditionally, individuals rely on centralized authorities to validate their identities and manage their personal data. However, this approach has been criticized for being vulnerable to data breaches, privacy violations, and abuses of power.
A decentralized identity system using smart contracts on Ethereum, on the other hand, provides a more secure and user-centric approach to identity management. By leveraging the blockchain’s secure and decentralized architecture, individuals can create and manage their own digital identities, which are secured using public-private key encryption.
Smart contracts can be used to create a system where individuals can store and manage their personal data and identity information, with access granted only to authorized parties. For example, a smart contract could be created that enables an individual to share only a specific set of identity information with a particular service provider, without revealing any other personal information.
In addition, the use of smart contracts allows for greater transparency and auditability of identity data, as all transactions are recorded on the Ethereum blockchain. This ensures that individuals have greater control over their personal data, and can trust that it is being used only for authorized purposes.
Overall, a decentralized identity system using smart contracts on Ethereum offers significant benefits in terms of privacy, security, and user control. It has the potential to transform the way we manage and share our personal data, and enable new types of secure and decentralized applications.
Decentralized Data Storage:
Decentralized data storage using smart contracts on Ethereum involves creating a system that enables individuals and organizations to store and manage their data in a secure and decentralized manner, without relying on centralized third-party providers such as cloud storage companies.
Traditional centralized data storage systems are vulnerable to data breaches, data loss, and unauthorized access. A decentralized data storage system using smart contracts on Ethereum, on the other hand, offers greater security and resilience, as data is distributed across a network of nodes rather than being stored in a single centralized location.
Smart contracts can be used to create a system where individuals and organizations can store and manage their data securely, with access granted only to authorized parties. For example, a smart contract could be created that enables an individual to store and share their files securely with specific individuals or groups, while maintaining complete control over their data.
In addition, the use of smart contracts allows for greater transparency and auditability of data storage and access, as all transactions are recorded on the Ethereum blockchain. This ensures that data is being used only for authorized purposes, and provides a clear and transparent record of all data transactions.
Overall, a decentralized data storage system using smart contracts on Ethereum offers significant benefits in terms of security, resilience, and transparency. It has the potential to transform the way we store and manage our data, and enable new types of secure and decentralized applications.
Conclusion
Smart contracts on Ethereum offer powerful capabilities for implementing Namecoin-style functionality. By leveraging the blockchain’s secure and decentralized architecture, these smart contract strategies enable secure and censorship-resistant domain name registration and resolution, decentralized identity systems, and decentralized data storage. These strategies offer significant benefits in terms of security, privacy, and accessibility, and have the potential to transform various industries by enabling the creation of innovative decentralized applications. As blockchain technology continues to evolve, we can expect even more powerful smart contract strategies to emerge, further revolutionizing the way we interact with digital assets and services.