当前位置:网站首页>Blockchain technology and Application

Blockchain technology and Application

2020-12-07 19:20:18 Jingyi

The next class is very long , Jingyi , To stick to !!!!!! Try to finish watching in the afternoon !!! come on. !!!fighting!!!

ETH- Intelligent contract

The code structure of smart contracts .

contract Be similar to c++ Class in class, Many state variables are defined .

uint Namely unsigned int, Unsigned integer .

address yes solidity Language specific , The back can speak .

mapping It's a hash table , It's an address to unsigned int Mapping .solidity Hash table does not support traversal in language , If you want to traverse the elements in the hash table , Then you have to find a way to record the elements in the hash table . use bidders Array records .

bidders It's an array that dynamically changes the length . Add a bidder ,bidders.push(bidder). Want to know the length of the array ,bidders.length. A fixed length array ,address[1024] bidders.

constructor Define a constructor , There can only be one .

None of the three member functions here have parameters , But some member functions can have parameters .

How external accounts call smart contracts ?

Sender address The address of the account that initiated the call To contract address Address of the contract being called

The lower left corner TX DATA Give the function to call , If the function has parameters , The value of the parameter should also be in the DATA Field indicates .

value It's how much money is transferred when the call is initiated . Here is 0, The purpose of this call is only to call the function, not to transfer money .

Gas used How much gas did the deal cost .

Gas price The price per unit of gasoline .

Gas limit It's the deal, the most I'm willing to pay for gas .

How one contract calls a function in another contract ?

1. Call directly .

use emit This operation calls the event .

Ethereum stipulates that only external accounts can initiate a transaction , A contract account cannot initiate a transaction on its own initiative .

So there's actually an external account calling B Of callAFooDirectly function , And then this function calls A Of foo function .

2. Use address.call().

Call failure returns false, The calling function can continue to execute .

3. Proxy call delegatecall().

payable It is stipulated in Ethereum that , If your contract account needs to be able to accept external transfers , It must be marked as payable.

Online Auction Contracts ,bid Function is used to bid . Participate in the auction , Say it 100 Bitcoin , So called bid function , To send the auction bid to the smart contract , Lock it in .bid The function should have the ability to support external transfer .

withdraw function , The auction is over , The highest bidder won the auction . You can put the original in the bid In the function, the Ethernet locked into the smart contract is taken out .

fallback() function , Anonymous functions , No parameters, no return value .

A call B This contract , To be in a transfer transaction data Field to call B Which function of the contract ? If A Transfer to B A sum of money , It doesn't say which function ,data Domain is empty , Default call fallback function . If the called function does not exist , Also call fallback function .

Be careful : The transfer amount is to the payee , The gas charge is for the miners who are digging .

The creation and operation of smart contracts .

EVM 256 Bit addressing space . similar JVM. For platform portability .

Gas .

Ethereum should provide Turing complete programmable model .

There's a dead cycle , Can't . Downtime problem (Halting problem) It's not solvable at all . The problem is not NPC problem ,NPC It's solvable , It's just that there's no polynomial time solution , It is solvable without considering the complexity .

Initiate a call to a smart contract , It costs gas .

Account nonce Transaction number , The prevention mentioned above replay attack.

Price The price per unit of gasoline .

GasLimit It's the deal that I'm willing to pay the most for .

Recipient Transfer address .

Amount Transfer amount .

Payload As I said before data Domain , Which function in the contract is used to store the call , What are the parameters of the function .

When a full node receives a call to a smart contract , According to the contract GasLimit Figure out the maximum gas cost that could be spent . Then, the maximum gas fee will be deducted from the account that initiated the call at one time , Then, according to the actual implementation situation, the redundant gas fee Go back to .

Error handling in Ethereum . Error handling , Or do it all , Or not at all , Atomicity . There's not enough gas , Roll back . An error occurred , There is no refund for the gas that has been spent .

Nested calls . Cascading rollback ? not always , Depends on how the smart contract is called . Call directly , Will be cascaded back . If call , Will not be .

Block header Data structure of .

GasUsed All the gas costs in this block add up .

GasLimit The upper limit on the amount of gasoline that can be consumed by all transactions in this block . Restrictions on block size . The smart contract byte in Ethereum looks very small , But it's very resource intensive . Cannot be determined by byte size . Bitcoin stipulates that each transaction size cannot exceed 1MB, It's written in the agreement . Prevent publishing blocks that are too large , Consumption of resources .

The etheric fang , The miner releases a block that can be up or down GasLimit Of 1/1024, Finally, it tends to be the average of the opinions of all the miners .

Be careful , Post a deal , All nodes have to execute , To be consistent , To update UTXO.

The whole node should execute these smart contracts before mining , Or mining first to get accounting rights and then execute smart contracts ?

Execute smart contracts first and then mine ! The root hash values of the three trees can only be obtained after execution , To try nonce, Only then can we carry out mining .

Consumed resources, executed the transaction , In the end , No bookkeeping right , What do I do ? There's no gas charge . There is no compensation in Ethereum . More Than This , It also needs to locally execute and verify the transactions of blocks published by others , Update the contents of the three trees , Calculate the root hash , Whether the comparison and the release are the same . The slow miners suffer a lot . There is no way to skip the verification step .

Will some miners not verify the correctness of the block ? Endangering the security of blockchain . If you skip the validation process , There is no way to update the local three trees , So there's no way to continue mining .

In the mine pool , The whole node is responsible for unified verification , Miners don't verify .

Are all transactions published to the blockchain successfully executed , If there is an error in the execution of the smart contract , Do you want to publish it to the blockchain ? It's also going to be posted , Otherwise the gas charge will not be deducted . Transactions posted to blocklines are not necessarily executed successfully .

How to know whether a transaction is successful or not ? A receipt is formed after each transaction is executed ,Status, The execution of the transaction .

Does smart contract support multithreading ? Multi core parallel processing ?solidity Multithreading not supported , Ethereum is a transaction driven state machine , The transition of state must be deterministic , If the state is uncertain , The root hash of the three trees may not be correct . Multithreading may cause uncertain execution results . You can't use real random numbers in Ethereum , Only pseudo-random numbers can be used .

The block information that smart contracts can get .

The call information that the smart contract can get .

Yes f_2 function ,msg.sender yes C1, however tx.origin yes A.

Address types in smart contracts .

The first is the member variable , The rest is the member function .(unit256) It's not a parameter type , It's the type of the variable .

<address>.balance How much money is transferred from the current contract to this address .address It's a transfer to address .

C Contract this account to addr Up and in 12345Wei The money .

Three ways to transfer money .

transfer Will cause cascading rollbacks .send return false Does not cause cascading rollbacks .

call Send all the remaining gasoline , Does not cause cascading rollback .

An example , Simple auction .

Auction rules : Before the auction ends , Everyone can bid , To bid , In order to ensure integrity when bidding , Send the bitcoin used in the auction to the smart contract , Lock up . At the end of the auction , The highest bidder's money will go to the auction beneficiary , Other unsuccessful people can get the money back . Bids can be made multiple times , Just make up the difference . A valid bid indicates a price higher than the current highest price .

Imagine a hacker bidding like this , Not written fallback function . It may have been deliberate mischief , It may be that I forgot to write fallback.

There is a problem with the refund at the end of the auction , Money back to the contract account . No function is called during the transfer , Should be called fallback function , And this contract has no definition fallback function , There will be anomalies , Cause cascading rollbacks , Operation failed , No one can get money . No matter in front of hackers, behind hackers , It's all about local data structures , Only if all of them are successful , Release , Only when the verification is successful can we reach a consensus . Everyone's money is locked up in smart trading , wait for death ......

Codeis law, It's like an irreversible trust (irrevocable trust).

Smart contract lock operation .pre mining Locked up for three years .

Before releasing the smart contract , Be sure to test, test, and test again , Make sure it's all right before you release it .

Smart contracts leave a back door , Used to repair bug? Give contract creator super administrator rights . Contrary to the idea of decentralization , User does not accept , derecognition .

The second edition .

problem .Hack_bid Same as the last one , Auction end call withdraw. The problem is fallback function , I took the money again . After the transfer, the hacker account will be cleared . And it's already recursive , It can't be reset at all . There is still a problem , Reenter attack . recursive , Hackers keep taking money from this account , The first time I took my money , And then get all other people's money .

Stop conditions : The balance of the auction contract is insufficient, or the gas charge is insufficient, or the call stack overflows .

The third edition .

Clear first, then transfer .

Clear first, then transfer . use sender and transfer Common ground , When transferring money, the past gas charge is only 2300 A unit of , This is not enough for the accepted contract to initiate a new call .

Original statement , This article is authorized by the author + Community publication , Unauthorized , Shall not be reproduced .

If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

版权声明
本文为[Jingyi]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/11/20201113185659751e.html