Basic knowledge of blockchain

JohnnyMartin 2022-04-06 09:49:56 阅读数:93

basic knowledge blockchain

Public key 、 Private key 、 And address

  • The private key is the foundation of everything , It can export the public key 、 Address , The private key cannot be derived from the public key
  • Public key encryption private key decryption , Private key signature public key verification
  • The elliptic curve algorithm used by Ethereum can calculate the address of the data sender from the signed data
  • After a series of operations, the public key can deduce the address , Ethereum is to make the public key Keccak-256 Hash , Then take the last 40 position 16 Hexadecimal characters
  • The private key must be backed up properly , Common backup methods include :
  • Mnemonics , Generally by 12 One word makes up , Getting it means getting the private key
  • keystore file , One json file , In essence, it is the data file after encrypting the private key , Decryption requires entering the password for encryption

Public chain 、 Private chain 、 League chain

The main difference : The public chain is open to all , The alliance chain is open to authorized organizations or institutions , The private chain is open to individuals who meet certain conditions ( For example, it is only open to people in our company ).

Openness Outbound node efficiency Consensus algorithm Typical representative
Public chain Completely open Any node low POW、POS、DPOS etc. The currency 、 The etheric fang 、EOS
Private chain Not open 、 Open to individuals A small number of nodes are blocked high Raft、Paxos The ant gold dress
League chain For specific institutions 、 Organizational openness Authorize the node to block out high DBFT、PBFT Super ledger (Hyperledger)、ChinaLedger

wallet 、 Hot Wallet 、 Cold Wallet

  • The essence of wallet : Generate the key 、 Management key 、 Tools for using keys .
  • Qianbao is responsible for generating a random private key , Generate a public key based on this private key , Then get the address according to the public key
  • The essential difference between hot and cold wallets lies in the way the private key is stored
  • Cold Wallet : The network cannot access the private key
  • Hot Wallet : The network can access the private key
  • Cold wallets are safe, but not suitable for frequent transactions , Hot wallet .

Hard branch 、 Soft branch

  • Essential difference : Whether the old node on the blockchain network recognizes the blocks generated by the new node .( Generally, new nodes will make some changes in the consensus rules )
  • If you don't approve , The blocks excavated by the new node will be rejected by the old node ( There is no consensus ), So the new node and the old node will start running on different chains ( dig 、 transaction 、 Validation etc. )
  • The fate of the old miners will determine the life and death of the old bifurcation
  • Hard bifurcation has a great influence , Unless agreed by the whole ecological chain , Otherwise, hasty hard bifurcation will easily cause the splitting of the chain . meanwhile , Hard bifurcation has considerable ” Medical effects “, Especially when significant bug、 When there are major defects ( At this time, it is easier to get the consent of the whole ecological chain )
  • Soft bifurcations do not produce two chains , The block generated by the new node can be recognized by the old node ( Reach a consensus ), However, the old node cannot recognize the true meaning of the data belonging to the new rule in the new block .
  • Soft bifurcation cannot add data fields , Can only be modified on existing structures , Limited upgrade space , And it complicates the code .
  • Hard bifurcations are usually extended consensus rules , Soft bifurcation is the tightening of consensus rules , So once the soft bifurcation is done , It's very difficult to go back , It is likely to cause hard bifurcation and cause capital loss


  • Anyone familiar with bitcoin should know , In order to disclose the public key as little as possible , At each transaction, the bitcoin community recommends that the balance be transferred to a new address .
    ( Although it is extremely difficult to deduce the private key from the public key , But not necessarily against quantum computers , Dual bitcoin address hash And transferring the balance to a new address during the transaction is to protect the public key , As few leaks as possible )
    So here comes the question , Each time, the balance is transferred to a new address , Then the balance in my account is gone for no reason ?
    Of course not , In fact, there is no concept of account in the bitcoin world , There is no concept of balance , It's just UTXO(Unspend Transaction Output) Transaction output not spent .“ The balance of the account ” Is related to the account UTXO And .
    Simply speaking , Bitcoin only records the transfer process , Forget the balance . A transfer consists of two parts : Input part 、 Output part . The input part consists of UTXO form , for example ,A I'm going to change 5 individual BTC to B, I know A All of the UTXO, So the wallet was picked out A Of A1,A2,A3 Three UTXO, Every 2 BTC, Put these three UTXO As the input of this transfer , This output has two parts : Some will 5 BTC Transfer to B, The other part will be the rest 1BTC Go to a new address ( It is assumed here that there is no miner's fee ), This new address is managed by the wallet . therefore , This has a BTC Of UTXO It can also be used as the input of the next transaction .
    The key question is : Each time, deposit the unspent balance in a new address , The new address means that it corresponds to a new private key , So many private keys , Do I have to back it up one by one ? Of course not. ——HD wallet (Hierarchical Deterministic Layered certainty ) Just one “ Root private key ” That is, seeds (seed), Generate any number of child private keys . We back up our wallets , Just back up “ Root private key ” The mnemonic word of . For example, we can use such an algorithm to generate sub private keys :SHA256(seed + n). as long as seed To determine the , All the following sub private keys are also determined .

The solution of the double flower problem

  • Every time a transaction is initiated , Check whether each input of the transaction is “ Not spent ”.
  • The transaction recipient has to wait 6 After this transaction is confirmed successfully .( Ethereum is 12)

The Byzantine general problem and its solution

Problem scenario :ABCD Four generals besieged the enemy in different places , No general can defeat the enemy alone —— They need to agree on a time to attack together in order to win . At most one of the four generals may be a traitor , And the honest general doesn't know who the traitor is . The traitors will try to sabotage the generals' attack plan . that , How can honest generals agree 、 Defeat the enemy ?

The computer language description of the problem is as follows : How to in a possible failure node 、 A consensus in the peer-to-peer network of evil nodes ?
Let's imitate what the generals might do first :

  • situation 1:A towards BCD The three generals gave advice : Afternoon 1 Point attack .B It is a traitor ,1 It's time ,B halt the troops and wait ,ACD Attacked and defeated the enemy as scheduled . This is a relatively simple situation .
  • situation 2:A It is a traitor ,A towards B Give advice : Afternoon 1 Point attack ; towards C Give advice : Afternoon 2 Point attack ; towards D Suggest : Afternoon 3 Point attack . therefore BCD The three sides attack separately in turn , It was all destroyed .

We can intuitively see the shortcomings of the above methods :BCD The suggestions received have not been verified .
So the simplest PBFT The algorithm appears —— For every general who receives advice , Each of them has to ask what others have received , And make a judgment .
So the above situation 2 It's like this :

  • situation 2:A It is a traitor ,A towards B Give advice : Afternoon 1 Point attack ; towards C Give advice : Afternoon 2 Point attack ; towards D Suggest : Afternoon 3 Point attack .B Received suggestions , And send b1、b2 Two messengers went to ask CD What they received ,CD Do the same thing . After asking, I found , I wipe A The suggestions given to the three of us are completely different ——A This guy must be a traitor, no doubt .

situation 2 The problem is solved , situation 1 Another new change , Please have a look at :

  • situation 1:A towards BCD The three generals gave advice : Afternoon 1 Point attack .B It is a traitor , When C Send a messenger c1 To ask B When ,B Lied to the messenger ——B The traitor tried to disrupt the timing of the attack . Yes C Come on , He received A My suggestion is 1 p.m , messenger c1 from B The suggestion brought back from the office is 2 p.m , messenger c2 from D The suggestion brought back from the office is also 1 p.m , therefore C It can be judged immediately ,B It's a traitor . alike ,D You can tell B It is a traitor , And use “ Attack at 1 p.m ” This suggestion .

PBFT The essence of the algorithm is to reach a consensus based on the number of Communications —— If there are more nodes , inefficiency .
Consensus algorithm , It's essentially a solution to the Byzantine general problem . POW、POS、DPOS Waiting is all about solving “ How to reach a consensus in an unreliable peer-to-peer network ” This problem .

  • POW Simple and crude , It's equivalent to letting the generals in 10 Throw stones at the enemy kilometers away , Killing the enemy is equivalent to mining success , Throw it all the time without killing it .
  • POS Efficiency , According to the general's seniority , Senior generals can give orders ( Block out ), Unified attack time
  • DPOS Close to the POS, Vote for several super generals , These super generals give orders 、 Unified attack time .

Isolation witness

I'll give you a reward

Uncle reward is a mechanism in Ethereum . There is no reward for the TERT block in bitcoin , Why should Ethereum reward the miners who dug out the uncle pieces ?
From the GOHST agreement .

  • What is uncle . In bitcoin , The longest chain is authority . If two miners dig out two blocks of the same height almost at the same time A1、A2, So which block is the final one ? It depends on who has a longer chain behind . So two miners will desperately broadcast their blocks , In order to allow other miners to dig more blocks on their own blocks , Make the chain where your block is located the longest chain 、 And get a reward . hypothesis A1 To win in the end , that A2 Will become a solitary block , No reward . In Ethereum , It uses “ Heaviest chain ” The concept of . Due to Ethereum 14 One piece per second , Block spacing is much smaller than BTC Of 10min, This greatly increases the probability of miners digging blocks of the same height . Ethereal GHOST The agreement states that , Solitary block , In Ethereum, it is called Uncle block , As a legal block , It's not meaningless , It can contribute to the safety of the main chain , It will also reward absenteeism in digging out isolated blocks .
  • Why reward those who dig out uncle pieces . The equivalent of this problem is : How does uncle block contribute to the safety of the main chain ? Uncle block becomes part of the main chain , So what's in the uncle's block coinbase Nature is a legal transaction , Naturally, you will receive a reward .
  • How does uncle block contribute to the safety of the main chain ? Ethereum blocks can contain 0~2 A piece of Uncle , And take this ( some ) Uncle put it on the head UncleHash Field . The following rules apply to the reference block :
  • The block references at most 2 A piece of Uncle , Can not quote
  • The referenced tertiary block must be the first... Of the current block 2~ front 7 Direct sub blocks of nodes
  • Tertiary blocks that have been referenced by other blocks cannot be referenced again
  • Reward calculation formula .
  • block ( Non uncle block ) Reward formula ( When writing, the basic reward is 2eth)= 2eth + 2eth*( Number of tertiary blocks referenced /32)+ Miner's fee
  • Uncle reward formula : The reward of a tertiary block depends on the number of layers between the block referencing the tertiary block and the tertiary block , The number of layers ranges from 1~6, Let's call the number of layers of spacing : Layer difference , Then the formula for calculating the reward of TERT block can be expressed as :(8 - Layer difference )/ 8 * 2eth.
  • It can be seen from the above formula that : Quoting uncle block can increase the reward of this block ; The higher the layer difference, the more rewards the uncle gets .
  • The heaviest chain : Unlike bitcoin, which uses the longest chain as the main chain , Ethereum uses the concept of the heaviest chain , The so-called heaviest chain , That is, the amount of calculation completed on this chain is the largest . Including uncle blocks can increase the weight of the chain . This is GHOST The main content of the agreement : Define the algorithm to determine the main chain . let me put it another way , Ethereal GHOST The agreement is a “ Main chain selection protocol ”. thus , In Ethereum , The longest chain is not necessarily the main chain !

Replay attack

This phenomenon occurs in hard bifurcation . Suppose a currency N, Yes 1000 Coins , stay 2018.11.11 It's diverging , The new chain currency is called NA, The old chain currency is still N. After the fork , Yours NA The balance in the wallet will show 1000,N The balance shown in the wallet is also 1000.
When you are in NA The wallet sends out a transaction , Want to send to B,10 NA The coin . After you sign the deal, broadcast to NA The Internet , here B Use a crooked brain , Forward the transaction you signed intact to N The Internet , If there is no replay, protect , This transaction may be recognized by two chains at the same time ——B Also received 10 NA The coin + 10 N The coin .

Consensus algorithm

  • POW(Proof of Work, Proof of workload ) The currency 、 At present, Ethereum uses this kind of consensus algorithm , Huge power consumption .
  • The currency of the POW Is to calculate a hash value , Its front N Position as 0,N Reflects the current difficulty . We can directly through the bitcoin block browser “ See ” This obvious feature of mining success .“ dig ” This is the process : Miners are constantly bringing new nonce Fill in the block , Calculate the size of the block hash value , Until the calculated hash Before the value N The values are 0, Successful mining . In the process , Miners need to assemble their own pieces , Or receive blocks from the ore pool .
  • Ethereal POW It's a little more than a bend in the bitcoin ,RAND(hashNoNonce, nonce) <= M/d; RAND Is an abstract function representing a series of operations ;hashNoNonce It's an assembled block, remove nonce Field hash value ,nonce Is the random number of each iteration attempt ;M It's a maximum ;d It's the difficulty ; In a nutshell : Use hashNoNonce + nonce Make input parameters , adopt RAND The function looks for a hash value , The size of this value is not greater than M/d; ( When the difficulty increases ,d Bigger ,M/d smaller , The difficulty of mining increases ).
  • The mining process of Ethereum only needs hashNoNonce Without the whole size , This is in the mine pool - It's very easy in miner mode , The ore pool only needs to issue hashNoNonce And corresponding nonce Just give the range to the miner , There's no need to send the whole block to miners like a bitcoin mine .
  • The currency of the POW When the algorithm was originally designed, it did not anticipate the rise of mining machines later , Ethereal Ethash At the beginning of the design, the algorithm considers the characteristic of resisting mining machine .
  • POS (Proof of Stock, Equity to prove ) Similar to property stored in a bank , This model will depend on the amount and time you hold digital currency , When the two are multiplied, we can get the coin age , The greater the currency of age , The more interest will be allocated to you 、 The greater the probability of bookkeeping . When motivated , The coin age is cleared , Re accumulation , It's all over again .Peer Coin( A little bit of money ,PPC)、 Nova coin (Nova Coin,NVC)、 Black Coin (Black Coin,BLK)、NextCoin( Future money ,NXT) wait , It's all for POS.
  • POA(Proofof Authority, Certificate of authorization ) A group of authorized nodes are responsible for the generation and verification of new blocks . Ethereum test network (Kovan) That is to adopt POA Consensus . Generally used to build alliance chain .
  • DPOS (Delegated Proof of Stake, Certificate of appointment ) BTS/EOS Consensus rules in use . Its principle is to let everyone hold BTS/EOS People vote , The resulting 101(BTS)/21(EOS) On behalf of , That is to say 101/21 Super nodes , These super nodes are responsible for packaging transactions 、 Block out . The super node will be part of the reward obtained by the block , To shareholders who support themselves .
  • PBFT(Practical Byzantine Fault Tolerance, Practical Byzantine fault tolerance algorithm ), Consensus algorithm commonly used in alliance chain .
  • Paxos, Consensus algorithm commonly used in private chain .
  • Raft, Consensus algorithm commonly used in private chain .

Detailed discussion on consensus algorithm , See another book for details .