Smart Contracts - Security Considerations

Related concepts

That is, when we write code, we consider this kind of issue related to contract security: the following aspects are the key considerations when writing contracts.

Create a message to send ether :

1. To create a message to send Ether, you need to construct a valid transaction and send it to the Ethereum network .

2. After the transaction is sent to the Ethereum network, it will be verified and packaged by the miners , and added to the blockchain. Once the transaction is confirmed, the specified amount of ether will be transferred from the sender's address to the receiver's address .

ps: The bottom layer of message execution is the smart contract or not, the smart contract is a kind of verification of the transaction.

safe constant

1. Re-entry attack : To prevent re-entry, the easiest way is to set the account in the contract to 0, and then send ether to the account address.

2. Gas limitation and circulation: The gas consumed by a block is not directly proportional to the gas owned.

  1. Contract Creation: When you deploy a new contract, a certain amount of Gas will be consumed. The amount of gas depends on the bytecode size of the contract and the complexity of the constructor.

  2. Data storage and reading: Writing (i.e. storing) or reading (i.e. obtaining) state variables will consume Gas. A write operation is usually more expensive than a read operation because it involves updating state.

  3. Function call: When you call a function in the contract, Gas will be consumed. The consumption depends on the complexity of the function, the number and size of the parameters, and the calculation operations inside the function.

  4. Control structures: including conditional statements (such as if, else, switch), loop statements (such as for, while) and exception handling (such as try/catch). Each execution path in these structures consumes Gas.

  5. Array and map operations: operations such as adding, deleting, modifying, and accessing arrays and maps will consume Gas. The exact consumption depends on the number of elements and complexity involved in the operation.

  6. Cryptographic operations: Encryption and decryption operations, such as hash functions, signature functions, and encryption functions, usually consume a large amount of Gas.

  7. External calls: Interacting with other contracts, including operations such as message sending, contract calling, and delegate calling, will consume Gas.

  8. Contract self-destruction: When you call the function in the contract selfdestructto destroy the contract, a certain amount of Gas will also be consumed.

3. Send and accept ether:

4.  Call stack depth : no more than 1024.

5. Authorized agent

6. The mapping in solidit cannot be deleted , only through

1. Set elements as default 

2. It is to delete one map mark after another while setting the default value of the element.

mapping(uint => bool) public isDeleted; 
mapping(uint => uint) public myMapping; 
function deleteFromMapping(uint _key) public {
     delete myMapping[_key];    // 删除键值对, 就是将mapping[key] 的值设置为默认值
     isDeleted[_key] = true;    // 标记已删除
 }

7. Restrict access : permission modifiers and the like

Guess you like

Origin blog.csdn.net/Qhx20040819/article/details/131723908