Smart contracts and Ethereum virtual machine: smart contracts, solidity language

Chapter Four



1. Smart Contract

The behavior of a smart contract is controlled by the contract code, and the account storage of the smart contract saves the state of the contract.
The code of the smart contract runs in the Ethereum Virtual Machine, which is the core of the Ethereum protocol.
From a storage perspective, the smart contract includes product information and all transaction records; from a functional perspective, the contract includes: replenishment function, transaction function, and withdrawal function.

1. Storage method

The storage methods of the Ethereum Virtual Machine are divided into three categories: stack, account storage, and memory. stack, storage, memory.
The stack is a common linear data structure. The length of each element in the stack is 256 bits.
Account storage is stored on the blockchain as an attribute of the account, so Yuyinpan is also a persistent storage and will not be released when the contract execution ends. The key values ​​stored in the account are all 256 bits. Assigning a value from 0 to non-0 consumes 20,000 units of Gas. Modifying a non-zero value consumes 5000 units of Gas. Assigning a value from non-zero to 0 can recover 15,000 units of gas.
Memory is a piece of linear space temporarily allocated by the Ethereum virtual machine when running code, and will be automatically released when the contract call ends. Bytes are the basic storage unit of memory. Each time it is consumed, the area in units of 32 bytes needs to be expanded, and each 32 bytes consumes 3 units of Gas.
In order to save Gas, memory is usually used during the intermediate process of contract execution, and the final result is saved in the account storage.

2. Instruction set and message call

The message call is included in the transaction and does not generate a new transaction record in the blockchain.
Delegate call: It only obtains the code from the target contract and executes it, and does not change the current context, including msg.sender and msg.value, current account, storage, memory, etc. This allows smart contracts to dynamically load code from other addresses at runtime.

3. Log

Developers can record logs generated by various events during the running of contract code. These logs can help developers call the code or serve as evidence of transactions occurring on the blockchain.
The blockchain does not save the complete log, but saves the hash value verification of the log in the transaction receipt.
Bloom Filter can be used to quickly retrieve whether an element is likely to be in a set. Can improve search efficiency.

2. solidity language

Variable types can be divided into two categories according to different parameter passing methods: value types and reference types.

  • Value type: A copy is created every time it is copied or passed as a parameter. Including: bool, int, uint suffix must be a multiple of 8 in the range of 8 to 256. Enums start from 0 by default, and the address length is 20 bytes. If address.transfer fails, an exception will be thrown and terminated. Address.send will return false and continue execution.
  • Reference type: Two storage locations: account storage and memory. State variables and some types of local variables are stored in account storage by default, while function parameters or other simple types of local variables are stored in memory. If necessary, you can also declare the variable with the memory or storage modifier to force the storage location of the variable. Including: array, push can add elements, structures, and mapping (Mapping) at the end of the array: it is a storage structure of key-value mapping relationship, mapping (KeyType=>ValueType), mapping has no concept of length, and does not store keys. , only stores the Keccak-256 hash value of the key.

1. Type conversion

Implicit conversion: uint8 can be converted to uint16 or uint32, but int8 cannot be converted to uint6. Any variable that can be converted to uint16 can be converted to the address type.

2. Operator

Solidity includes arithmetic operators, comparison operators, and bitwise operators.
delete represents the default value. But it is invalid for mapping.

3. Block and transaction attributes:

block.blockhash: Obtain the hash value of a specific block
block.coinbase: The account address of the current block "miner"
block.difficulty:
block.gaslimit
block.number: The current block number.
block.timestamp: uint, the generation time of the current block
msg.gas: uint, indicating the remaining Gas
msg.sender: address, the sender address of the current message
msg.sig: bytes4, the first 4 bytes of the calling data, function identification symbol.
msg.value: uint, the amount of Ether transferred in this message. Unit: wei
tx.gasprice: uint, the Gas price of the current transaction
tx.origin: address, the initiator of the complete call chain.
selfdestruct: Destroy the current contract.
suicide: alias of selfdistruct.

4. Control structure statements

Switch and goto are not supported.

5. Function

A function can have multiple parameters and multiple return values.
There are two types of function calls:

  • Internal call: calling a function in the same contract
  • External call: Calling an instance in another contract will create a message and send it to the called contract, such as this.a() or foo.bar().

external: external function
public: external and internal
internal: can only be accessed in the current contract or other contracts that inherit the current contract
private: internal

6. constant function and fallback function

constant or view means read-only.

7. Function modifier

modifier

8. Exception handling

  • assert: used to check variables and internal errors, and throw will identify an error and roll back the state changes caused by the current message call. The exception is that an invalid operation will be performed.
  • require: Ensure that the necessary conditions for program execution are met. A rollback operation is performed when an exception occurs.

Both Ethereum virtual machines above will undo all state changes.
However, there are exceptions for send, call, and delegatecall. These functions will return false when an exception is thrown during execution, instead of automatically throwing an exception.

9. Events and logs

event{}
or through log

10. Inheritance of smart contracts

The functions in the is
contract are all virtual functions. Unless a class name is specified, the last derived function will be called. When inheriting, they will be inherited from left to right.
Abstract contract: A contract only has function life but no specific implementation of the function. This function cannot be compiled, but can be inherited.

Summarize

提示:这里对文章进行总结:

That’s what I’m going to talk about today.
On the Ethereum platform, a smart contract is a piece of logic code stored on the blockchain and runs in the Ethereum Virtual Machine.

Guess you like

Origin blog.csdn.net/qq_53982314/article/details/124326167