Before learning how to program ethereum smart contracts, it is important to understand the basic concepts related to Ethereum.

account

Unlike Bitcoin’s UTXO model, Ethereum uses an account model. The account itself is divided into two types, one is controlled by private key, the other is smart contract. Smart contracts can be created using private key accounts or other smart contracts.

An account is defined as follows:

// Account is the Ethereum consensus representation of accounts.  
// These objects are stored in the main account trie.  
type Account struct {  
   Nonce    uint64  
   Balance  *big.Int  
   Root     common.Hash // merkle root of the storage trie  
   CodeHash []byte  
}
Copy the code

The meanings of each field are as follows: Nonce: Prevents double flowers. After a transaction is successfully sent and packaged, the value is automatically increased by 1. Balance: indicates the account Balance, in the unit of wei. Root: Merkle Root of the status variable of the contract account. The essence of a smart contract is to maintain a set of state variables. CodeHash: Hash of the contract code

trading

Another important concept of Ethereum is called transaction. Transferring money is a transaction, creating a smart contract is a transaction, and invoking a smart contract is a transaction. Here are the parameters to initiate a transaction using RPC:

{
  "jsonrpc": "2.0"."method": "eth_sendTransaction"."params": [{"from": "0xf4bc2b33e080991222858a592775193a7ca932c6"."to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567"."gas": "0x76c0"."gasPrice": "0x9184e72a000"."value": "0x9184e72a"."data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}]."id": 1
}
Copy the code

A transaction is defined by the following fields:

From: transaction initiator to: transaction receiver. If the value is empty, a contract is created. Gas: The maximum allowable gas gasPrice, measured in ether value, and transaction data

What are gas and gasPrice? After compilation, the smart contract consists of some basic atomic operation OP, and each OP corresponds to a certain gas. The product of the sum of gas required by all executed op and gasPrice is the cost that the caller needs to pay to the miners. Gas is designed to protect against attacks. Imagine a smart contract with an infinite loop, in which nodes run out of resources, miners can’t pack up, and the network is blocked. With the concept of GAS and gasPrice, evM charges for each step, and the program is forced to terminate when the cost exceeds the limit. It is important to note that gas is not refunded once it is collected, regardless of whether the contract call succeeds or fails. There have been DDOS attacks on Ethereum in the past because failed transactions were not charged.

What about the congestion we often talk about on the Ethereum network? When miners receive the transaction sent by us, they will preferentially select the transaction with high gasPrice for packaging according to the transaction price, so the transaction with low gasPrice needs to queue up. Richer also increases the gasPrice in order for transactions to be executed as quickly as possible, resulting in transactions with lower gasprices being excluded and waiting.

If to in the transaction parameter is empty, data is the compiled code of the smart contract. If TO is a contract address, data describes which function to call the smart contract and what the arguments are.

In addition to calling smart contracts through transactions, there is another way to call smart contract functions without spending GAS. This invocation does not cause the smart contract’s state variables to change, so nodes do not need to broadcast any information to other nodes.

The storage space of the Ethereum smart contract consists of 2^256 slots, each with a size of 32bytes. The state variables of the contract are mapped to this huge storage space through the SHA3 function.

+--------+--------+--------+--------+-------+-----+------
| slot1  | slot2  | slot3  | slot4  | slot5 | ... | slot2^256 
+--------+--------+--------+--------+-------+-----+------
Copy the code

Ethereum uses a Merkle tree to persist state variables, whose Root is the Root in Account.

Smart contract is also called smart contract, which is different from the concept of intelligence in AI. Smart contract emphasizes that Code is Law. Once the Code is released, it will be executed according to the pre-designed logic, and theoretically no one can manipulate its results and his calculation is always credible, which is the charm of smart contract.

block

Several transactions are packaged together by miners to form a block, with the following partial fields in a block header:

difficulty              3393930978663113
extraData               0x737061726b706f6f6c2d636e2d6e6f64652d3031
gasLimit                8000029
gasUsed                 7980102
hash                    0xe2813542fb7829957b9f535982e126e54893f53dd28bf3eae38facae536e980d
miner                   0x5a0b54d5dc17e0aadc383d2db43b0a0d3e029c4c
mixHash                 0x543f2ad3ce64aafcae5d133e7883d272061c2f90508b11c0d274f9790fd009ca
nonce                   11187430928983643115
number                  5903045
parentHash              0xfcb74cff4034c44410feccf6766c2f358c3f23db092dd5e8eae8de903c685b39
receiptsRoot            0xc4ac8d3a6ee03dfa85287ddc419aa75c8a294d1b820e123668982aae3c59eeb1
size                    43634
stateRoot               0x43374ec343c8aa4fb99c78acedb015128ebf80838709fc15fca2ed53c4d0f7ff
timestamp               1530691577
totalDifficulty         0x1171d0869f13c025207
transactionsRoot        0x0955dc7ee31a25f449272a1d2360a581af5a1f3d628dd8bb154034e194be5fd6
Copy the code

All the blocks are linked together by parentHash called blockchain. The stateRoot is the Merkle root of the logging account, through which the account information under the current block can be retrieved. TransactionsRoot The Merkle root of the list of transactions that the current block contains. ReceiptsRoot is the Merkle root of transaction receipts, which stores transaction events, gas costs, etc.