Ethereum Smart Contract

1. Smart Contracts

Smart contracts are an idea put forward by Nick Szabo, which is almost the same age as the Internet. However, due to the lack of a trusted execution environment, smart contracts have not been applied to the actual industry. Since the birth of Bitcoin, people have realized that the underlying technology of Bitcoin, blockchain, can provide a credible execution environment for smart contracts
. Ethereum first realized the complete integration of blockchain and smart contracts.

Ethereum is a blockchain with built-in Turing-complete[1] programming language. By establishing an abstract base layer, anyone can create contracts and decentralized applications, and set up their freely defined ownership rules, transactions. mode and state transition functions. The main framework for building a token can be implemented in just two
lines of code, and other protocols such as currency and reputation systems can be implemented in less than 20 lines of code. Smart contracts are like encrypted boxes that can be created on the Ethereum platform that contain value and can only be opened if certain conditions are met, and because of Turing completeness, value
-awareness, blockchain-awareness ) and the added functionality of recording multi-state is far more powerful than the smart contracts that Bitcoin script can provide.

2. Development language

Ethereum’s software development language is one of its greatest features, as programming the blockchain is a primary goal. Ethereum has 4 specialized languages: Serpent (inspired by Python), Solidity (inspired by JavaScript), Mutan (inspired by Go), and LLL (inspired by Lisp), all languages ​​designed from the ground up for contract
-oriented programming.

As Ethereum's high-level programming language, Serpent is designed to be very similar to Python. It is designed to be as concise and simple as possible, combining the efficiency advantages of low-level languages ​​with ease of use in a programming style.

Solidity, the language of choice for Ethereum, has all of Serpent's features built in, but with a syntax similar to JavaScript. Solidity takes advantage of the fact that millions of programmers already master JavaScript, lowering the learning curve and making it easy to master and use.

Another key feature of the Ethereum blockchain is its "Turing completeness," which guarantees that Ethereum can solve all computational problems. More precisely, it is "semi" Turing-complete, because it avoids the non-stop problem of fully Turing-complete languages ​​by placing an upper limit on the amount of computation.
In addition , because the language of Ethereum is specially designed for blockchain, it has the concept of account, which makes it provide real-time in visualization of transactions and query of account status. This is a welcome feature, but a challenge for Bitcoin to implement. On Bitcoin, since there is only UTXO and
no account concept, we need to import the blockchain database, parse all transactions, and query transactions in order to extract the transaction status of a user on the blockchain. With Ethereum, we can view the current account status and transaction status in real time according to an address on the real-time blockchain
.

3. Code execution

The code for an Ethereum contract is written in a low-level stack-based bytecode language called "Ethereum Virtual Machine code" or "EVM code". Code consists of a series of bytes, each byte representing an operation. In general, code execution is an infinite loop, and the program counter is incremented by one (
initially zero) and performs an operation until the code is executed or an error, STOP, or RETURN instruction is encountered. Operations can access 3 types of spaces for storing data:

1) Stack, a last-in, first-out data storage, the basic unit of stacking and popping is 32 bytes.

2) Memory, infinitely scalable byte queue.

3) The long-term storage of the contract, a key/value storage, where both the key and the value are 32 bytes in size. Unlike stack and memory, which are reset at the end of a computation, the contents of the store persist for a long time.

Code can access values, data in sent and received messages just like block header data, and code can also return a byte queue of data as output. The formal execution model for EVM code is very simple. When the Ethereum virtual machine is running, its complete computing state can be defined by a tuple (block_state
, transaction, message, code, memory, stack, pc, gas), where block_state is the global state containing all account balances and storage . How each instruction affects the tuple is defined by calling out the pc (program counter) byte of the code on each round of execution.
For example , ADD pops two elements from the stack and pushes their sum, decrements Gas by 1 and increments pc by 1; stack pops the top two elements and inserts the 2nd element from the 1st element. The defined contract storage location also reduces the Gas value by up to 200 and increases the pc by 1. While there are many ways to
optimize , a basic implementation of Ethereum can be implemented in a few hundred lines of code.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324776068&siteId=291194637