Blockchain Note – Concepts Interview Questions

1. What is the difference between Wei and Ether (Ethernet) in Ethereum?
A: Wei is the minimum face value of the Ethanol. It is like saying that the minimum face value of the RMB is a cent. The minimum face value of the pound is a penny. The conversion relationship is 1 Ethanol = 10^18Wei.

2. What is the average block interval between Ethereum?
A: The average block interval is 14 seconds. Of course, this is only the theoretical value, you can do it at Etherscan.
(https://etherscan.io/chart/blocktime)
The daily average block interval was found.

3. What is the average block size of Ethereum?
A: The block size is subject to availability and is approximately 2KB. Unlike Bitcoin, which uses the block size to specify the upper limit of the volume of the block, Ethereum uses the gas limit. The fuel limit determines the amount of transaction and storage/bandwidth that is processed in each block, because the execution of functions in transactions and smart contracts is based on the complexity of the instructions, so the fuel limit is used to constrain the area. The block size is feasible.

To figure out how many transactions you can hold in a block, you don’t need to know the price of the fuel clearly, just know how much fuel is used per trade and divide the entire fuel limit by it.

4. What is the node in Ethereum?
A: Essentially, a node is a computer that connects to a blockchain and can handle transactions.

5. What networks are there in Ethereum?
A: There are three types of networks in Ethereum: the Ethereum main chain (the Ethereum we usually use), the Ethereum test network (such as Ropsten and Rinkeby for developers to learn and test) and the Ethereum private chain (also called Ethereum private network, anyone can use the code to deploy their own private chain).

6. What are the methods for interacting with the Ethereum network?
A: You can use e-wallet or DApp.

7. Can you “hide” a transaction in Ethereum?
A: No. All transactions in the Ethereum blockchain are publicly visible.

8. Where is the transaction record of Ethereum stored?
A: In a publicly visible book, this book is often referred to as a blockchain.

9. The Ethereum main chain is already very strong. Why use the Ethereum private chain?
A: There are many reasons, mainly because the data involves privacy, decentralization of the database, access control and testing.

10. How can I view the details of a transaction or a block?
A: You can use a blockchain browser such as http://etherscan.io or live.ether.camp.

11. How do I view the details of a transaction or a block in the private chain?
A: Some open source blockchain browsers meet this need, such as the blockchain browser launched by ether party. (https://github.com/etherparty/explorer)

12. What is the consensus process for the blockchain?
A: Consensus is the process of verifying a transaction in accordance with a specific agreement (such as the Ethereum agreement), packaging the transaction into blocks and joining the blockchain.

13. What is the working principle of the Ethereum mining operation?
A: In principle, the mining operation in Ethereum is almost the same as Bitcoin.
To put it simply, for each block that contains transactions, the miner uses the computer to experiment with the answers to the puzzles repeatedly and very quickly until a miner guesses the puzzle.

More specifically, the miner uses the unique block header metadata (including timestamp and software version) and a nonce value of the current block as input to the hash function. The function will return a fixed length and look like A garbled random number consisting of numbers and letters is called a hash value. The characteristic of a hash function is that different inputs correspond to different hash values, so the miner only needs to change the value of the random number to get a completely different hash value.

If the calculated hash value is less than the current target value (drilling difficulty), the miner dug out a block, he will get some Ethereum rewards, and then by broadcasting the block to the entire network, other nodes can Verify the transaction in the block and add the block to the copy of the blockchain in the region after verification. That is to say, if Miner B calculates a hash value, Miner A will immediately stop the hash value calculation of the current block, add the block excavated by B to the blockchain and start a new round of hash calculation. .

It is difficult for miners to cheat in this competition. In order to get the answer to the puzzle, there is no better way to try one by one, and there is no possibility of forging these calculations. This is why the puzzle method is called “work proof”.
On the other hand, the user does not need to verify that the hash value is correct because each node has been verified.

In general, every 12 to 15 seconds, a miner will dig a new block. If the miner’s time to solve the puzzle begins to appear faster or slower, the algorithm will automatically adjust the difficulty of the problem so that the miner’s puzzle time is stable at around 14 seconds.

Miners have a chance to dig into new blocks to earn Ethereum rewards, and their ability to make money depends on luck and the computing power they put into it.
The workload proof algorithm used by Ethereum is called “ethash”, which is designed to require more memory, which makes it more difficult to mine with expensive ASIC miners, because the appearance of ASIC miners is severely squeezed using other The benefits of equipment miners are such that the only profitable form of mining in Bitcoin is the use of such customized chips. In addition, since Ethereum will gradually transition from workload proof mining to equity proof mining, the purchase of ASIC mining machines may not be a wise choice, as Ethereum will be eliminated once it turns to equity.

14. What are the two most commonly used consensus protocols in the blockchain?
A: Proof-of-Activity (PoA) is constantly emerging in the industry as proof of work (PoW) and proof of interest (PoS).

15. Please briefly describe how the proof of equity works.
A: The proof of equity is the creator of the block that is randomly selected based on the amount and timing of the currency held. It is not a computationally intensive form of mining.

16. What tools are needed to sign a transaction?
A: The user’s private key.

17. After the private key is lost, can the user still recover the Ethereum account?
A: Yes, users can use the 12-word mnemonic to recover.

18. What method can I use to connect to the Ethereum node?
A: IPC-RPC, JSON-RPC and WS-RPC (RPC refers to Remote Procedure Call, remote procedure call).

19. What is the unusually hot Geth in Ethereum?
A: Geth is a command line client of Ethereum.

20. What is the default way to connect to the Geth client?
A: By default, IPC-RPC is used to disable all other RPCs.

21. What APIs (Application Programming Interface) are available in the Geth client?
A: Admin, eth, web3, miner, net, personal, shh, debug, and txpool.

22. Which RPCs can you use to connect to the Geth client over the network?
A: You can connect to the Geth client over the network using JSON-RPC or WS-RPC. IPC-RPC can only be used to connect to the locally deployed Geth client.

23. If you enter the command “–rpc”, which RPC is enabled?
A: JSON-RPC.

24. Which RPC APIs are enabled by default?
A: eth (Ethernet), web3 and net (network).

25. How do I enable admin api for JSON-RPC?
A: Enter the command “–rpcapi”.

26. What is the function of the command “–datadir”?
A: It specifies the storage location of the blockchain.

27. What is Geth’s “fast” synchronization, and why is it faster?
A: “Fast” sync only downloads the block where the receipt transaction is located, and pulls the entire recent state database instead of downloading the entire blockchain data and replaying all the transactions that occurred, just like normal sync.

28. What is the function of the command “–testnet”?
A: It connects the client to the Ethereum Ropsten test network.

29. Starting the Geth client will print a lot of output information on the screen. What if you don’t want to be disturbed by these complicated information?
A: Use the “–verbosity” command to lower the value of the output information complexity (default is 3).

30. How to connect two Geth clients using IPC-RPC?
A: First start a Geth client, copy its pipe location, then use the same data file storage directory (data-dir) to start another Geth client and use the “–attach” command to pass the copied pipe location.

31. How do I load a custom javascript file into the Geth console?
A: Enter the path of the “–preload” command and file.

32. Where is the account stored in the Geth client?
A: Stored in the key store directory.

33. How to initiate a transaction using a given account?
A: First you need to enter the account address or index in the “–unlock” command to unlock the account. Then you need to specify a password file for this account using the “–password” command.

34. We just talked about the contents of the index. What is the index of the account?
A: It depends on the order in which you add your account.

35. Can the Geth client be used for mining?
A: Yes, enter the “–mine” command.

36. What is the “etherbase” in the mining options?
A: This is the account address that accepts the mining award. The index of this account is 0.

37. What is ABI?
A: Simply put, “ABI” is a low-level “API”.
The ABI (Application Binary Interface) application binary interface is essentially the way you call a function in a smart contract and get its return value.
The ABI defines how to call functions in a smart contract and which binary format should be used to pass information from one program component to the next.
Ethereum smart contracts are deployed in byte code form on the Ethereum blockchain, and there may be multiple functions in a smart contract. So, with ABI, you can specify which function in the smart contract to call to ensure that the return value of the function is the format you expect.

ABI is an abstraction that is not part of the Ethereum core protocol. Anyone can define a proprietary ABI for their smart contracts, and any caller of these smart contracts must follow the ABI’s rules in order to get meaningful call results. However, for all developers, using Solidity, Serpent and web3.js is much simpler, and these are also in line with ABI regulations.

38. What is a smart contract?
A: In essence, smart contracts are computer code written in multiple languages. Smart contracts exist on the blockchain network, and they perform related operations according to their own embedded rules, which can be seen as contracts between participants.

39. What are the usage scenarios for smart contracts?
A: For a simple commodity trading scenario, the buyer deploys a smart contract in Ethereum and deposits funds into it. The seller sees the deposited funds and sends the goods. After the buyer receives the goods, the buyer pays the goods through the smart contract. The smart contract here can be seen as a decentralized Alipay that protects Taobao transactions.

40. What is MetaMask?
A: Metamask is a plug-in type of Ethereum wallet that helps users interact with the Ethereum network in the browser.

41. What node does Metamask use?
A: It uses http://infura.io.

42. Compared with the traditional Ethereum wallet, what features are not supported by Metamask?
A: It does not support mining and deployment of smart contracts.

43. Is the execution of smart contracts free?
A: No, smart contracts can only be invoked by executing a trade, and the transaction requires fuel costs.

44. Is the status of the smart contract free?
A: Yes, the query status does not need to execute a transaction.

45. Who will execute the smart contract?
A: Miners.

46. ​​Why does it take money to call a function in a smart contract?
A: In addition to some functions that do not change the state of the smart contract, there is no logic other than the return value. Calling the function in the smart contract costs money.

In this cost, in addition to transferring the call to the Ethereum into the smart contract, calling the function that changes the state of the smart contract requires fuel to execute.

47. Why is it necessary to introduce fuel costs in Ethereum?
A: Because the miners use their own computers (mineral machines) to execute the smart contract code, if the miners can recover the cost of purchasing the machine and earn profits to ensure the safety of the entire system, the Ethereum design allows the miners to execute the caller’s request. Code to earn fuel costs to maintain a healthy ecology.

48. Can the fuel price determine when the transaction is processed?
A: Yes, not all. In general, the higher the fuel price you pay, the more likely the deal will be added to the blockchain. Still, fuel prices do not guarantee that transactions are processed faster.

49. What is the amount of fuel used in the transaction?
A: The amount of fuel used depends on the amount of storage, the type and quantity of instructions (opcodes). The operating code of each Ethereine virtual machine clearly defines the amount of fuel required.

50. How to calculate the transaction fee?
A: Transaction fee = quantity of fuel used * fuel price (fuel price specified by the trader).

51. If the cost of executing a smart contract is less than the fuel cost paid by the trader, will he get a refund?
A: Yes.

52. What happens if the cost of executing a smart contract exceeds the fuel cost paid by the trader?
A: Users will not receive a refund, and will stop once all fuel is exhausted, and the status of the smart contract will not change.

53. Who will pay for the call to the smart contract?
A: The user who invoked the smart contract.

54. Where does the node run the code for the smart contract?
A: The node runs the code for the smart contract in the Ethereum Virtual Machine (EVM). The Ethereum virtual machine specification is part of the Ethereum agreement. The Ethereum virtual machine is just a process that the node runs.

55. What tools does the Ethereum virtual machine need to run smart contracts?
A: It requires the byte-code of the smart contract, which is compiled and compiled by a high-level language such as Solidity.

56. What are the parts of the Ethereum virtual machine?
A: Memory area, stack and execution engine.

57. What is Remix?
A: Remix is ​​an online tool for developing, testing and deploying smart contracts. It’s great for quickly building and testing lightweight smart contracts, but not for complex smart contracts.

58. In Remix, which nodes can you connect to?
A: You can use the Metamask wallet to connect to the public node, use the Geth wallet to connect to the local node and use the Javascript virtual machine to connect to the in-memory simulated node.

59. What is a DApp, how is it different from a regular app?
A: Applications typically include a client that interacts with certain centralized resources (owned by the organization) and typically has an intermediate layer that is connected to the centralized data layer. If the data in the centralized data layer is lost, it cannot be (easily) restored.

DApp represents a decentralized application. DApps interact with blockchain networks through smart contracts, and the data they use resides in instances of smart contracts, and decentralized data is more secure than centralized data.

60. Solidity is a statically typed language (type checking is done before running, such as the compile phase) or dynamic type language (type checking is done at runtime)?
A: Solidity is a statically typed language, which means that the type is known at compile time.

61. What structure in Solidity corresponds to a class in Java?
A: Smart contract.

62. What is an example of a smart contract?
A: An example of a smart contract is a smart contract deployed on the blockchain.

63. What are the differences between Java and Solidity.
A: Compared to Java, Solidity supports multiple inheritance, but does not support method overloading.

64. What is the first parameter that needs to be specified in the Solidity file?
A: The first parameter is the version of the Solidity compiler, which needs to be specified as ^ 0.4.8. Don’t underestimate this step because it avoids incompatibility errors introduced when compiling with other versions of the compiler.

65. What does a smart contract contain?
A: Smart contracts consist primarily of storage variables, functions, and events.

66. What types of functions are there in smart contracts?
A: There are constructors, fallback functions, constant functions, and functions that modify the state of smart contracts.

67. What happens if I put multiple smart contract definitions into a single Solidity file?
A: It is entirely feasible to put multiple smart contract definitions into a single Solidity file.

68. How can two smart contracts interact?
A: A smart contract can call, create, and inherit another smart contract.

69. What happens when you try to deploy a file with multiple smart contracts?
A: The compiler will only deploy the last smart contract in the file, which means that all other smart contracts are ignored.

70. If I have a huge project, do I need to keep all relevant smart contracts in one file?
A: No, you can use the import statement to import the file as follows:
Import “./MyOtherContracts.sol”;

71. Can I only import local files?
A: No, you can also use HTTP to import files (even files on Github) as follows:
Import “http://github.com/<owner>/<repo>/<path to the file>”

72. What are the parts of the Ethereum virtual machine’s memory?
A: Storage, Memory, and Calldata.

73. Please explain the storage.
A: You can think of storage as a database. Each smart contract manages its own storage variables, resulting in a key-value pair database (256-bit keys and values). The only difference between storage and a normal database is that it costs more to read and write because of the cost of fuel.

74. Please explain the memory.
A: Memory is a temporary storage. When the function call is executed, the data in the memory will be released. You can allocate a variety of complex data types, such as arrays and structures, in memory.

75. Please explain Calldata.
A: Calldata can be understood as a function call stack (Callstack). It is temporary and unmodifiable. It stores the execution data of the Ethereum virtual machine.

76. What variables are stored in the memory area and memory area respectively?
A: State variables and local variables (usually local variables are references to state variables) are in the store, and the parameters of the function are in the memory area.

77. Read the following code, explain which part of the code corresponds to which memory area:

contract MyContract {
  // part 1
  Uint count;
  Uint[] totalPoints;

  function localVars(){
    // part 2
    Uint[] localArr;
    // part 3
    Uint[] memory memoryArr;
    // part 4
    Uint[] pointer = totalPoints;
  }
}

A: Part 1 – Storage.
Part 2 – Storage (array size points to the position of the counter)
Part 3 – Memory.
Part 4 – References to storage.

78. Can I define a function like this:
Function doSomething(uint[] storage args) internal returns(uint[] storage data) {…}
A: Yes, you can force the function’s argument to be a storage type. In this case, the compiler will complain if you don’t pass in a stored reference to the function.

79. What is the difference between EVM calls and non-EVM calls?
A: An EVM call is a function call in a smart contract that triggers function execution and requires fuel.
Non-EVM calls read publicly visible data and do not require fuel.

80. How do I set the Ethereum balance limit for smart contracts? What happens if I send excess Ethereum to a smart contract with a balance limit? answer:

Pragma solidity ^0.4.19;

contract yourContract{

  Uint256 public balanceLimit = 999;

  function () payable{
   If (this.balance + msg.value > balanceLimit) {
     Throw; / / will roll back after the excess.
   }
  }
}

81. How to set the value of msg.val in the smart contract account?
A: msg.val => msg.value = the number of weis sent with the message.

82. What is DApp?
A: A Decentralized Application (DApp) is an application that runs on a peer-to-peer network rather than a single computer. Decentralized applications have existed since the advent of peer-to-peer networks, a software tool designed to exist on the Internet in a way that is not controlled by any entity.

Decentralized applications do not necessarily need to run on a blockchain network. BitTorrent (decentralized download software), Popcorn Time (decentralized video software), BitMessage (decentralized communication protocol), Tor (onion routing) are traditional decentralized applications running on peer-to-peer networks, both of which Not running on the blockchain (blockchain is a special peer-to-peer network).
Contrary to a simple smart contract, in the classic usage scenario of Bitcoin, where user A sends money to user B, the participant in the decentralized application can be anyone in the market.

83. What is the difference between DApp and smart contracts?
A: The decentralized application is a blockchain-based website where smart contracts allow it to connect to the blockchain. To put it bluntly, in order to better understand decentralized applications and smart contracts, let’s first understand how traditional websites work.

Traditional web (web) applications use HTML, CSS, and Javascript to render the page on the front end. On the back end, it needs to use the API to get some detailed information from the database, such as the user’s username and avatar. When you enter the web version of Sina Weibo, the page will call the API to get your personal data and display it on the page. In other words, the traditional website works in the following way: Front End → API → Database.

A decentralized application is similar to a traditional web application. It uses the exact same technology to render the page on the front end. One key difference is that decentralized applications do not use APIs to connect to the database, but instead use smart contracts to connect to the blockchain. So, the way a website based on a decentralized application works is: Front End → Smart Contract → Blockchain.

In traditional centralized applications, the back-end code runs on a centralized server. In contrast to decentralized applications, its back-end code runs on a decentralized peer-to-peer network. Decentralized applications include the entire package from the back end to the front end, and smart contracts are just part of the decentralized application.

In other words, the DApp includes:
1) Front end (the page you can see)
2) Backend (application backend logic)

On the other hand, smart contracts only contain backends, usually just a small part of the entire DApp. This means that if you want to create a decentralized application on a smart contract system, you must combine multiple smart contracts and rely on third-party systems as front ends.

84. What tools are used by the front end to connect to the back end of the smart contract?
A: The Web3 API library.

85. Please list a few DApps you know.
A: The Golem project aims to create the world’s first global market for idle computer computing; Augur (word meaning: omen), the Augur project aims to combine predictive and decentralized networks to create A predictive tool to gain potential benefits; Aragon Network, which is designed to serve as a user-friendly digital management organization.

86. What tools do you need to interact with DApp’s smart contracts?
A: ABI and bytecode for smart contracts are required.

87. What is the role of ABI?
A: ABI is a description of the public interface of smart contracts. DApp uses this public interface to invoke smart contracts.

88. What is the role of bytecode?
A: The Ethereum virtual machine on each node requires the smart contract bytecode to run the smart contract.

89. Why use the BigNumber library?
A: Because Javascript can’t handle large numbers correctly.

90. Why should I always check if the web3 provider (provider) is set at the beginning of the DApp code?
A: Because if not, Metamask will overwrite it with its own web3 provider.

91. Why use the 1.x version of web3 js instead of the 0.2x.x version?
A: Mainly because the 1.x version of the asynchronous call uses the promise object (committed to execute in the future, more reasonable and more powerful than the callback object) instead of the callback object, the promise object is also the first choice in javascript.

92. How do I list all accounts in the web3 1.x version?
A: Use the command: “web3.eth.getAccounts”

93. What is the difference between “.call” and “.send”?
A: “.send” initiates a transaction and generates a fee, while “.call” only queries the status of the smart contract without incurring a fee.

94. Is it possible to send an Ethercoin with such a command “.send({value:1})”?
A: No, actually you only sent 1wei (1 Ether = 10^18Wei). The unit in the transaction is wei, not Ethereum.

95.: Does that mean that in order to send an Ethereum, I need to set the value to 10^18?
A: No, it is too much trouble. You can use the util method, ie:
web3.utils.toWei(1,’ether’).

96.: What do I need to specify when calling “.send()”?
A: You must specify the sender address “from”. Everything else is optional.

97: Is the function that sends Ethereum to a specific address only
web3.eth.sendTransaction() one?
A: No, you can also call functions in smart contracts.

98. What is the solution to the scalability problem of Ethereum?
A: The idea to solve the scalability problem is to use the chain trading mechanism. Specific solutions are state channels and plasma.

99.Name the two types of records that are present in the blockchain database?
These records are block records and transactional records. Both these records can easily be accessed, and the best thing is, it is possible to integrate them with each other without following the complex algorithms.
Blockchain is a distributed database. How does it differ from traditional databases?
Properties
Blockchain
Traditional Database
Operations
Only Insert Operations
Can perform C.R.U.D. operations
Replication
Full Replication of block on every peer
Master Slave
Multi-Master
Consensus
Majority of peers agree on the outcome of transactions
Distributed Transactions
(2 phase commit)
Invariants
Anybody can validate transactions across the network
Integrity Constraints
100. What are the properties of Blockchain?
There are four key features of blockchain:
  • Decentralized Systems
  • Distributed ledger
  • Safer & Secure Ecosystem
  • Minting
101.What are Block Identifiers?
In Blockchain, blocks can be identified by the block header hash and the block height.
102. What are Merkle trees? How important are Merkle trees in Blockchains?
Merkle Tree also known as ‘hash tree’ is a data structure in cryptography in which each leaf node is a hash of a block of data, and each non-leaf node is a hash of its child nodes.
31C21472-8B12-483D-B8D9-025FA6913E35
The benefit of using the Merkle Tree in blockchain is that instead of downloading every transaction and every block, a “light client” can only download the chain of block headers.
Also, if someone needs to verify the existence of a specific transaction in a block, then he doesn’t have to download the entire block. Downloading a set of a branch of this tree which contains this transaction is enough. We check the hashes which are just going up the branch (relevant to my transaction). If these hashes check out good, then we know that this particular transaction exist in this block.
103. Can You explain the components of Blockchain Ecosystem?
Following are the components of blockchain Ecosystem:
2BCE8C19-B2B4-4264-9A83-6B0A57EDC874
104. What is Double Spending? Is it possible to double spend in a Blockchain system?
It’s a condition when one digital token is spent multiple times because the token generally consists of a digital file that can easily be cloned. It simply leads to inflation and organizations must bear a huge loss. One of the primary aims of Blockchain technology is to eliminate this approach up to the possible extent.
Blockchain prevents double spending by confirming a transaction by multiple parties before the actual transaction is written to the ledger. It’s no exaggeration to say that the entirety of bitcoin’s system of Blockchain, mining, proof of work, difficulty etc, exist to produce this history of transactions that is computationally impractical to modify.
105. In blockchain, blocks are linked ________?
Backward to the previous block[Ans]
Forward to next block
Not linked with each other
106. Hash identifying each block in the Blockchain is generated using which of the following cryptographic algorithm?
SHA128
SHA256[Ans]
107. Blockchain forks can result in which of the following?
Multiple parent blocks
Multiple children blocks[Ans]
108. A block in the blockchain can never have more than one parent block?
True[Ans]
False
109. Can you explain what are off-chain transactions?
An off-chain transaction is the movement of value outside of the blockchain. While an on-chain transaction – usually referred to as simply ‘a transaction’ – modifies the blockchain and depends on the blockchain to determine its validity an off-chain transaction relies on other methods to record and validate the transaction.
110. What is 51% attack?
51% Attack refers to a situation where a group of miners who hold more than 50% of the Network Hash Rate could manipulate with the New transactions (Stopping the transactions to proceed or gaining conformations) or able to reverse the transactions that were recently confirmed and kind of doing Double spend. It is Highly unlikely to be able to do that today but it is possible.

Reference

  1. https://www.edureka.co/blog/interview-questions/blockchain-interview-questions/
  2. https://zhuanlan.zhihu.com/p/47771150
  3. https://medium.com/coinmonks/blockchain-interview-questions-the-collection-38de299ce44d
  4. https://zhuanlan.zhihu.com/p/48538829

Blockchain Note – EOS DApp Development

In this chapter, we are going to learn about EOS DApp development and I am highly recommend you to understand the following concepts and read through the recommended blogs and tutorials mentioned below.

Step 1: Understand the following concepts

1. What is EOS?

EOS.IO is a blockchain protocol powered by the native cryptocurrency EOS. The protocol emulates most of the attributes of a real computer including hardware (CPU(s) & GPU(s) for processing, local/RAM memory, hard-disk storage) with the computing resources distributed equally among EOS cryptocurrency holders. EOSIO operates as a smart contract platform and decentralized operating system intended for the deployment of industrial-scale decentralized applications through a decentralized autonomous corporation model.

2. What is the difference between EOS vs ETH?

ETH
EOS
Consensus
POW+POS+Casper
BFT+DPOS
Transaction Fee
Gas fee
None
TPS
20
3000

3. What is Graphene Technology?

Graphene technology is the consensus mechanism of DPOS.
The graphene technology specifically refers to the consensus mechanism that EOS adjusts the workload proof (POW) supporting BTC and ETH, and adopts the consensus mechanism of DPOS (Delegated Proof Of Stake).

In the POW mode, all nodes have the opportunity to win the billing rights. The nodes compete with each other. Whoever cracks the answer first can get the billing right. The probability of obtaining a billing right is proportional to the power of the node.

The DPOS mode is changed to a holder of all the tokens of the chain, and a certain number of nodes are elected, and then these nodes cooperate and take turns to record.

In EOS’s DPOS mode, holders of all EOS tokens can obtain votes based on the number of EOS they hold, and vote for 21 block producers (witnesses). Then, the producers of the 21 blocks cooperate with each other and perform bookkeeping in turn in a certain order. The block interval is 3 seconds for a large block. In addition, these 21 block producers not only need to keep accounts, but also need to provide the computing and network resources (including CPU, memory, storage capacity, etc.) required by the EOS chain.

If one of the blocks is paused for a reason such as a network disconnection, a block will occur and the next accounting node will keep up with the accounting. This mechanism guarantees that even if only one node is working at the end, the entire system can still work, and then continue to select new nodes to ensure stable operation.

If a fork occurs in the process, DPOS still adopts the longest chain principle, and it is agreed that each node cannot be out of the two chains at the same time (otherwise the node will be judged to be a violation and disqualified), which makes After the fork is generated, after at most half of the total number of witness nodes (11 blocks in EOS), only one chain is retained.

In addition, there is an irreversible principle under DPOS. Once a block follows a block with more than 2/3 of the total number of witnesses, the block enters an irreversible state, which is equivalent to confirming that the block is a block in the main chain. The status can no longer be rolled back to the block before the block to divide, otherwise it is considered a violation. Because each node participates in at most one chain at the same time, only one block of the same height may only have one irreversible block, which further ensures the security of the block and prevents malicious attacks and malicious forks. In general, in the 21-block generation model, 15 blocks (15/21>2/3) enter the irreversible state (ie, the transaction is 100% safe) for 45 seconds.

Therefore, the advantages of DPOS are:
1) Solved the problem of consuming a lot of power in POW mode.
2) Nodes change from competition to collaboration, and there are only 21 nodes, making it easier to quickly reach consensus and improve the performance of the main chain (TPS).
3) Hard forks are not generated because nodes follow the longest chain principle and each node cannot participate in both chains at the same time. Therefore, after the fork is generated, after at most half of the total number of witness nodes (11 blocks in EOS), only one chain is retained. Every major system upgrade will not lead to hard forks, just need to upgrade all witness nodes at the same time.
4) It is more secure, and it is necessary to control more than 2/3 of the nodes to make the wrong block irreversible.
5) Confirmation is very fast

But the problem with DPOS is that, in essence, DPOS is not decentralized, but weakly centralized. Moreover, as the time of existence of these witness nodes occurs, the degree of centralization becomes higher and higher due to the rewards of participating in the bookkeeping. This can be a potential risk to the entire community. According to previous DPOS-based projects, such as BitShares and Steemit, the final degree of centralization is indeed very high. For a time, most of the witnesses were actually BM themselves or BM-controlled nodes.

To make another derivative, EOS has also improved the basic graphene technology, not only the DPOS consensus, but the consensus mechanism for upgrading to DPOS+BFT. The concept of a cell block is proposed and the Byzantine Fault Tolerance Mechanism (BFT) and irreversible state are added.

EOS makes a big chunk every 3 seconds, and the EOS team has proposed a scheme to continue dividing the 3 second block into several small blocks with an interval of 0.5 seconds. The billing rights are not switched between these small blocks. As soon as each small piece comes out, it will be broadcast immediately. This design allows the blocks to be confirmed more quickly while minimizing the effects of forks due to network delays.

A mechanism for BFT is introduced: whenever a cell block is broadcast within a blockchain network, the remaining nodes will immediately acknowledge the block. Blocks that have been confirmed by 2/3 nodes will enter an irreversible state. Under this consensus mechanism, the security confirmation time is 0.5s (block interval) + 0.3s (block propagation delay) <1s, thus achieving the second-level confirmation performance. In addition, if the block interval of the small block is continued to be divided, it is theoretically confirmed that the required time is approximately equal to the network delay of the block propagation, and may even be less than 0.5 second.

In addition, malicious attacks, unless controlled by more than 2/3 of the nodes, can not enter the irreversible state of the malicious block. Here, it is specifically mentioned that if a malicious node controls more than 1/2 but less than 2/3 of the node, since the malicious block does not enter the irreversible state, this can leave enough time for other observer nodes and witness nodes to be malicious. The node votes out. Therefore, this mode also greatly enhances security.

4. What is assembly?

The EOS DApp developers create applications via WebAssembly (WASM) technology.
WebAssembly is a new high-performance underlying binary language that was launched in 2015 and supported by Google, Microsoft, Apple and other major browser vendors. Other high-level programming languages ​​such as C, C++ and so on can be compiled into WASM format byte code. The most mature toolchain for building applications and WASM compilation is CLang/LLVM and its C/C++ compiler.
WebAssembly is not a direct machine language, but a virtual machine language abstracted out. In the Assembly browser, there will be a piece of Javascript that will re-translate the Web Assembly into Javascript. This technique is called polyfill, a technique commonly used when HTML5 comes out. For browsers that support Web Assembly, translate directly into Native code. From WebAssembly to machine language also needs a “translation” process, but it belongs to machine language to machine language translation, so the speed is very close to machine language and very fast.
If the developer uses C++ development, first translate the C++ code into LLVM intermediate code (IR), and then compile it into a WASM grid.
The byte code is then run on the WASM virtual machine on the EOS platform using the WASM. Along with The languages ​​supported by WebAssembly increasing, the languages ​​supported by EOS can be continuously increased without any modification at all levels.

5. Why using C/C++ ?

C++ is not a panacea, C++ is suitable for the following areas:
1) C++ is suitable for constructing parts with more stable requirements in the program, and the part with large changes in requirements can use scripting language;
2) The program must try to maximize the performance of the hardware, and the performance bottleneck lies in the CPU and memory;
3) Programs must communicate frequently with the operating system or hardware;
4) Programs must use C++ frameworks/libraries, such as most game engines (such as Unreal/Source) and middleware (such as Havok/FMOD). Although some C++ libraries provide bindings for other languages, native APIs usually perform best. up to date;
5) A target platform in the project is only supported by the C++ compiler.

C++ is suitable for developing server software, desktop applications, games, real-time systems, high-performance computing, embedded systems, and more.

Why blockchain projects choose C++?

1). C++’s rich class library has inherent advantages for projects that require high development schedules.
2). C++ has high performance and will not be described here. As far as the database is concerned, most of the projects are written in C or C++.
3). The Bitcoin project is mainly written by C++. The subsequent cottage project will basically fork the bitcoin repo, which will cause the situation of the underlying project C++ to bloom everywhere.
4). In the future, you can see more projects developed in languages ​​such as Go and Java, and other tools such as smart contracts will choose new languages ​​such as Solidity.

6. How does WebAssembly and C/C++ works in EOS?

1).The example code in C
#include <math.h>
float getSqrt (float num) {
    return sqrt(num);
}
2).Compile the C into wasm(WebAssembly’s format)
22922FC8-B83B-482E-950A-FFD8643DE372
Download the program.wasm to the local file.
3).Using JavaScript to upload the .wasm in the browser.
<!doctype html>
<title>WASM Test</title>

  fetch('./program.wasm')
  .then(res => {
    if (res.ok)
      return res.arrayBuffer();
    throw new Error(`Unable to fetch WASM.`);
  })
  .then(bytes => {
    return WebAssembly.compile(bytes);
  })
  .then(module => {
    return WebAssembly.instantiate(module);
  })
  .then(instance => {
    window.wasmSqrt = instance.exports.getSqrt;
  });
4).Execue the function in the web server
python -m SimpleHTTPServer
Open Chrome and goto http://localhost:8000/test.html
6AEB0B58-2E80-4AA9-8DF2-45B773213ED6

7. What is resources of CPU/RAM/NET in EOS ?

The EOS account is an operating system. To use this operating system, it is necessary to configure system resources.
After creating an EOS account, be sure to configure the system resources first.
EOS has three major resources: CPU, NET (network bandwidth) and RAM (memory).
CPU and NET need temporary mortgage EOS acquisition, similar to the deposit, no need to redeem EOS.
RAM can only be purchased directly with EOS and will be consumed during use, but the consumption of this part is negligible compared to the miners’ fees.
The general operation of the user, including the execution of the contract code, lasts for a short period of time on the CPU and network bandwidth, so the EOS uses a take-back mechanism in exchange for the collateral as EOS Token. For example, a mortgage of 1% Token can get up to 1% of network bandwidth. Just like you pay a deposit with a shared bicycle, you can redeem it without a deposit.
The allocation of RAM is slightly different. It is stored in the state and is occupied for a long time. Simply put, the state generated according to the data on the chain is stored in RAM. For example, the information of an account, including the current state of the balance, is stored in RAM, so it is necessary to purchase RAM to store data for a long time.
Note: When the account has no resources, it is not allowed to buy RAM or mortgage for NET and CPU. Because this is a transaction itself, it needs to consume resources. The first operation needs to be done through other accounts.
Any wallet can be directly mortgaged to the CPU and NET and purchased RAM.
Basic configuration recommended: CPU: 0.5EOS, RAM: 0.1EOS, NET: 0.1EOS, this configuration can meet normal trading needs.
What if I forgot to configure the resources and the CPU or RAM is completely consumed?
Use EOS resource free temporary mortgage tools.
Trading on RAM, the trading mechanism uses Bancor algorithm, which adjusts the price of RAM through market supply and demand. If the RAM supply exceeds demand, it does not need too much EOS when buying RAM, and it will not sell when RAM is sold. Get too much EOS, and if RAM is in short supply, just the opposite.
The following operations are required to consume RAM: create a new EOS account, create a contract, transfer money to other accounts, buy and sell RAM, mortgage to get CPU or NET, redeem mortgages, and more. When the user operates this operation, a certain amount of RAM is required in the account, otherwise the operation cannot be completed.
In EOS, CPU and NET acquisition differs from RAM in that it allocates the corresponding CPU and NET bandwidth according to the proportion of mortgage EOS through collateral.
If you want to release the NET and CPU you already own, you can redeem the mortgaged EOS by redemption, but you need to wait a certain amount of time to get the account.
NET and CPU are used to ensure that users use the basic functions of the EOS network, including transfer, execution of smart contracts, etc. The more operations are performed in a unit of time, the more CPU and NET are consumed, but unlike RAM, Over time, the CPU and NET are automatically released, which means that the CPU and NET are not consumable resources.

8. EOS architecture

3D9961DA-C1EA-46AF-9B04-418B973C29FE

By comparing traditional web service modules, it is easier to understand the purpose of each module.
EOS: eosd
Web: Database

Data storage is provided, except that eosd is a state-based blockchain data storage method, and the state of the smart contract is modified through the transaction.
EOS: Query Services + GraphQL
Web: REST & Graph QL & Microservices

EOS should package user account management, transfer, etc. into micro-services; GraphQL is relatively easy to use in the React framework. GraphQL is an application-level query language proposed by Facebook. With GraphQL, you can define your backend based on the graph pattern. Then the client can request the required data set.
EOS:Client (React.js)
Web: front end

Because of the default integration of GraphQL in the EOS architecture, the front end using React.js will be the best framework choice, so developers who intend to develop applications for EOS can pay more attention to React.
EOS: IPFS File Storage
Web: File Storage

Analogy to the current Internet application architecture model is more convenient for developers to get started, but also provides an experience payment model similar to the current Internet application is currently not clear, but compared with Ethereum’s computationally paid model compared to EOS, the model currently described is very complex and Dynamic, because EOS not only needs to limit computing, but also limits bandwidth, storage, etc. Finally, it is mentioned that application developers can provide out-of-chain data interfaces through a similar architecture to achieve high scalability of applications.

The holders of EOS voted for each super-node candidate through the voting system, and selected 21 nodes as super nodes. Then the 21 super nodes negotiate a block right ownership order with their own network resource status. When each super node has a block right, after the super node A generates the first new block, A signs the block and Broadcast to other super nodes. Other super nodes will verify the block and then sign it back to the A node. When the A node receives the block from 14 different node signatures, the block becomes an irreversible block. Concatenate into the previous blockchain. The constitution of the EOS community is mainly based on the basic principles established to regulate the differences among community members. Since many blockchain projects currently have internal differences in the development process, the blockchain system cannot be effectively evolved, so it must be at the beginning of the project. The evolutionary principles of setting up a project make EOS a chain that can continue to evolve to meet the needs of market and technology development. When EOS development requires a fork and upgrade contract, the EOS code can be modified and upgraded according to constitutional rules.

At the contract level EOS integrates the virtual machine with the EOS through the Open RPC (Remote Procedure Call) interface, and the implementation of the scripting language and virtual machine will be independent of the EOS operating system technology, as long as any development language or virtual machine is appropriate. Sandboxes with sufficient performance can be integrated with EOS via RPC. And EOS can now support Wren, WASM, EVM three virtual machines, so the application on Ethereum can be directly transplanted to the EOS system through simple modification. Due to the separation of virtual machines and EOS, developers can choose their own skilled programming language for smart contract development, which makes application development on EOS more flexible, which greatly reduces the barriers to the use of blockchain technology.

Step 2: Learning EOS DApp tutorial

Learning https://battles.eos.io/ Best free EOS DApp Card Game tutorial (Code)

Reference

Blockchain Note – Concepts dictionary

51% Attack – This term describes the situation where most of the power of the blockchain network is concentrated in one place. A user or group of users that control 51% of the computing power can deliberately manipulate it or inadvertently execute conflicting transactions that could compromise the system.

Address
A cryptocurrency address is used to send or receive transactions over the network, and addresses are usually represented as numeric characters.

Airdrop – The token is distributed by the operator of the cryptocurrency network. These tokens are either distributed free of charge to holders of all cryptocurrencies or restricted by certain activities, such as promoting encryption on social networks.

Altcoin – Any cryptocurrency other than Bitcoin is called a replacement currency. Hundreds of alternative currencies are being traded around the world, including XRP, NEO, Stellar and more.

Bitcoin – the first and largest cryptocurrency (by market capitalization). Bitcoin was launched in 2009 as a decentralized currency based on blockchain technology. It is the first practical application of the blockchain. Bitcoin was created by a person named Satoshi Nakamoto or a group of people.

Blockchain – A decentralized network of contiguous chains of code (blocks). All transactions on the network are stored on the public ledger, and the public ledger exists throughout the network, which means that there is no need for the central server to authorize transactions on the network.

Block Height
The number of blocks connected to the blockchain.

Block Reward
It is an incentive form for miners who successfully calculate hashes in the block during mining. New coins are generated during the transaction verification process on the blockchain, and the miners are rewarded for some of them.

Cold storage – A security measure for storing cryptocurrencies in an offline environment. They can be storage devices (such as USB flash drives) or paper wallets.

Central Ledger (Central Book)
A ledger maintained by a central agency.

Confirmation
Decentralized a transaction and added it to the blockchain for successful confirmation.

Consensus – Since many of the data on a public blockchain network is simultaneously stored in multiple areas of the network, members want to have the same copy of these code segments (such as public ledgers) across the network.

Cryptocurrency – The first major application of the blockchain, the cryptocurrency is a currency with no central ownership, and each token and transaction is uniquely encrypted. Blockchain technology is an infrastructure that allows storage of cryptocurrencies and allows tokens on the network to change hands.

Cryptographic Hash Function
Password hashing produces a fixed size and a unique hash value from a variable size transaction. The SHA-256 calculation algorithm is an example of a cryptographic hash.

Dapp (Decentralized Application) is an open source application that runs automatically, stores its data on a blockchain, motivates it in the form of a cryptographic token, and operates on a protocol that displays valuable proof.

DAO – Abbreviation for autonomous organizations. This term describes an organization that uses blockchain practices (such as smart contracts) to manage itself without the need for central authority.

Distributed Ledger (distributed ledger)
Distributed ledger, where data is stored over a distributed node network. A distributed ledger does not have to have its own currency, it may be licensed and private.

Difficulty (easy level)
This refers to the ease with which data blocks of transaction information are successfully mined.

Digital signature – A common term used to identify a single individual or behavior on the Internet. In a blockchain, it usually refers to a unique identifier given to a user, token, or transaction.

Double Spending
A double payment occurs when a sum of money is spent more than one payment limit.

Ethereum (Ethereum)
Ethereum is a blockchain-based decentralized platform for running smart contracts designed to address issues related to censorship, fraud and third-party interference.

EVM (Ethernet virtual machine)
The Ethereum Virtual Machine (EVM) is a Turing-complete virtual machine that allows anyone to execute any EVM bytecode. Each Ethereum node runs on the EVM to maintain consistency across the blockchain.

Fork – Because the blockchain is decentralized, every change to the network must be accepted by the user in order to complete. If there are enough users to accept the upgrade or code changes, it will expand across the network. Changes that still support older versions of the network are called soft forks, and changes that make them backwards incompatible are called hard forks. Sometimes, if there is a disagreement about hard forks in the community, a new, parallel blockchain network might be created. This is the case with Bitcoin cash and the birth of the Ethereum classic.

Genesis Block
The first block of the blockchain.
Hash – The practice of using an algorithm to give a “digital fingerprint” of data. When storing information on a blockchain, the hash is used to create a uniform form to identify the code block by converting the code block into a series of fixed-size numbers and letters.

Hard Fork (hard branch)
A type of branch that makes a previously invalid transaction valid, and vice versa. This type of branch requires all nodes and users to upgrade to the latest version of the protocol software.

Hash Rate
Performance measurements for mining rigs are expressed in seconds.

Hybrid PoS/PoW (Hybrid PoS / PoW)
POW (Proof of Work) refers to how much money is obtained. Depending on the amount of work you contribute to mining, the better the performance of the mining machine, the more mines will be given to you. POS (Proof of Stake) is a system for interest distribution based on the amount and time of money you hold. In POS mode, your “mining” income is proportional to your currency age, and has nothing to do with the computer’s computing performance. Hybrid PoS / PoW can use the shared distribution algorithm on the network as a proof of sharing and proof of work. In this approach, a balance between miners and voters (holders) can be achieved, with a community-based governance system created by insiders (holders) and outsiders (miners).

KYC is the abbreviation of Know Your Customer, which means to understand your customers. In the International Anti-Money Laundering Act, organizations are required to have a comprehensive understanding of their clients to predict and discover unreasonable business practices. And potential violations.

Cross-chain technology can be understood as a bridge connecting each blockchain. Its main application is to realize atomic transactions between various blockchains, asset conversion, inter-blockchain internal information intercommunication, or solve Oracle problems.

ICO – the first issue of coins. This term describes a situation in which companies raise money by issuing cryptocurrency tokens that are sold to early investors at a fixed price.

Ledger – A digital log of all transactions that take place on a particular blockchain network. Copies of the books are stored on the network and constantly updated to match each other so that anyone on the network can verify the transaction.

Lightning Network – A “second layer” solution designed to greatly increase the time on the blockchain network for transaction processing speed. Lightning Networks creates a

P2P network to process transactions and then broadcasts these transactions to log in to the underlying blockchain public ledger.

Liquidity – The ease of use of a cryptocurrency into cash. Liquidity depends on many factors, including supply and demand relationships and transaction processing time.

Mining – Use the power of a computer to trade on the web and receive rewards. Each transaction is encrypted by an equation that requires a large amount of computational power. Miners who solve this equation first so that the trade can proceed will receive a small amount of compensation.

Mining pool – A structure created by a group of miners to handle more transactions and charge more. These funds are then distributed among the members of the pool.

Multi-Signature
Multi-signature addresses require more than one key to authorize transactions, adding a layer of security.

Node – A computer on the network that operates a blockchain ledger copy. Nodes are scattered throughout the network, helping to maintain a decentralized form.

Oracles (Prophecy Machine)
Oracle provides a bridge between real-world and blockchains by providing data to smart contracts.

Paper wallet – A cold storage solution that is considered one of the safest ways to store cryptocurrencies. The paper wallet can be printed on any printer, including the user’s unique public and private keys, which are encoded as two-dimensional codes. When users want to access their funds, all they need to do is scan their paper wallets.

Peer-to-peer (P2P) – On a particular network, two parties share information directly without passing data through the server.

Private Key – Every user on the network holds a private key. The private key is known only to the user and can be equivalent to the password.

Proof of Stake – A method of determining which users are eligible to add new blocks to the blockchain to earn mining fees. Among the users involved in the mining process, users with more tokens prefer this method than users with fewer tokens.

Proof of work – A proof of work is a similar concept before proof of interest because it is used to determine which user is eligible to create a block. However, with the proof of working methods, the pass or fail is determined by the calculation power, not by the digital wealth of the miners.

Public Address
The public address is the password hash of the public key. They act as email addresses that can be published anywhere, unlike private keys.

Public Key – If the private key mentioned above can be equated with a password, the public key is some kind of username because everyone can see it on the public ledger.

Scrypt
Is an encryption algorithm used by Litecoin. It’s faster than SHA256 because it doesn’t take up a lot of processing time.

SHA-256
It is an encryption algorithm used by Bitcoin for some columns of digital currency. However, it uses a lot of computing power and processing time, forcing miners to form mining pools to generate revenue.

SegWit – This term refers to a solution that makes the blockchain network faster. Segregated Witness can be implemented as a soft fork on a blockchain network, improving its functionality without the need to create new currencies or make the network backwards incompatible.

Smart Contract – An algorithm that automates a contract using blockchain technology. When the terms of a smart contract are met, it is executed and the participants will be rewarded according to the terms of the contract. Smart contracts are popularized by the

Ethereum network. Token – A single coin associated with a particular blockchain network, representing its currency, giving the transaction value within the network. For example, the token of the Litecoin network is called LTC.

Soft Fork (soft branch)
Soft branches differ from hard branches in that only previously valid transactions can invalidate them. Since the old node recognizes the new block as valid, the soft branch is basically backward compatible. This branch requires most miners to upgrade to execute, while hard branches require all nodes to agree on the new version.

Solidity
Solidity is the programming language that Ethereum uses to develop smart contracts.

Testnet
The test blockchain used by developers is mainly used to prevent changes to assets in the main chain.

Transaction Block
A collection of transactions aggregated into a block that can then be hashed and added to the blockchain.

Transaction Fee – Because the trading of blockchain networks requires a lot of computing power, miners on the network have to compete for the right to trade by assigning their computing power. The miner who will eventually settle it will charge a transaction fee.

Turing Complete (Turing Complete)
Turing completion refers to the ability of a machine to perform any other programmable computer capable of performing calculations. An example is the Ethereum Virtual Machine (EVM).

Wallet – An online program, or a local client program that enables users to store, transfer and view their balances. Different wallets support different cryptocurrencies, and many wallets support different cryptocurrencies on one platform.

Whitepaper – A paper that serves as a report or guide to a complex issue. In the world of cryptocurrencies, white papers are used as a means of delivering blockchain networks or encrypted structures, plans, and/or perspectives.

Reference

  1. https://zhuanlan.zhihu.com/p/51423204
  2. https://zhuanlan.zhihu.com/p/41875261
  3. https://zhuanlan.zhihu.com/p/41658584

Blockchain Note – Ethereum DApp Development

There are many awesome resources online and I suggest the following steps to become familiar with Solidity/ETH DApp development.

Step 1

Finish the courses https://cryptozombies.io/, it’s a fun and free DApp tutorial.

Step 2

Finish the Truffle pet-shop, which is also a good a free simple Truffle (an ethereum development environment) tutorial.

Step 3

Read and learning the 30 Solidity examples

Step 4

Solidity Documents

Blockchain Note – DApp Basics

DApp Basics
Before step into the world of DApp, feel free to read the white paper of

1. What is DApp?

DApp is like a blockchain compared to Android and iOS. DApp has the following five features:1). DApp decentralizes operations through network nodes. It can run on the user’s personal device, such as a mobile phone or a personal computer. It belongs to the user forever, and can be freely transferred to anyone.

2). The DApp runs on a peer-to-peer network. It does not rely on a central server, does not require a dedicated communication server to deliver messages, and does not require a central database to record data. The data is stored in the user’s personal space, either a mobile phone or a personal cloud disk.

3). The DApp data is encrypted and stored on the blockchain. It can rely on the blockchain to conduct property transactions and sales, and to carry transactions without intermediary.

4). DApp participant information is stored securely. It can protect digital assets and ensure that property rights will not be leaked or destroyed.

5). DApp must be open source and autonomous. Can be freely packaged by the user to generate, the signature mark belongs to. Its release is not subject to any institutional restrictions. Various ideas and innovations can be freely expressed and realized. Only one DApp that meets the above four conditions is considered to be an application that can run.

2. What is the difference between DAPP and APP ?

From the perspective of customer experience, APP has four major problems with DAPP. One is to intercept user data, the other is to monopolize the ecological platform, the third is to retain user rights, and the fourth is to limit product standards to stifle innovation.
From a technical point of view, there are two main differences between DAPP and APP. One is that APP is installed and running on Android or Apple system; DAPP is developed on the blockchain public chain and combined with smart contract; Second, APP information is stored in data service platform. It can be directly modified by the operator; DAPP data is stored in the blockchain after being encrypted, which is difficult to tamper with.

At the same time, the following three conditions can be called a DApp.
First, running on a distributed network;
Second, the participant information is stored securely and privacy is well protected;
Third, decentralized operations through network nodes.

ETH is based on BTC and is a super single-threaded computer.

3. What is Solidity?

Solidity is a contract-oriented, high-level language for implementing smart contracts. It was influenced by C++, Python and JavaScript and is designed to target the Ethereum Virtual Machine (EVM).

Solidity is statically typed, supports inheritance, libraries and complex user-defined types among other features

4. What is gas and why need them?

1) Ethereum’s EVM function is very powerful, known as “Turing complete.” “Turing complete” virtual machine can implement looping statements. With the loop, there will be illegal molecules or unqualified programmers to make an infinite loop. The computer will die in a loop, and it will be a big crash. It is good to restart, but Ethereum is going to the center. If the EVM is in an infinite loop, it can’t be restarted.
For the “Turing shutdown problem”, many years ago, it has been proved that there is no way to detect whether the program will die. Is there any other way to stop the infinite loop?

So Ethereum introduced the gas mechanism. The name gas is very appropriate, and the translation is the meaning of gasoline. If you compare Ethereum to a car, if the car is not oily, can you stop it? So in Ethereum, each operation needs to consume a certain amount of gas. If the gas is consumed, the program can stop. When Ethereum initiates each transaction, it will preset a certain amount of gas limit. If gas is consumed during execution, the operation will fail.
Gas The currency of the Ethereum Virtual Machine (EVM), the Ethereum virtual machine uses gas to charge for transaction packaging, smart contract execution and other operations.

2) Ethereum’s programmability makes it impossible to use Ethereum directly as a fee for Bitcoin. Since the standard for bitcoin handling fees is the size of each transaction byte, for a transaction, the bytes are computable and the resources they occupy are therefore also measurable. Ethereum is programmable. For a piece of code that can run, it is impossible to pre-calculate its resources.

For example, if you write a loop in a program, you don’t know how many times it loops. If you use ETH as a fee, the more cycles you consume, the more ETH you consume. For example, if you have a 100-cycle contract, the commission is 10 times that of 10 cycles, and no one is willing to write some logic. More intelligently speaking, gas is actually a workload unit, the gas limit represents how much work the contract does, and the gas price indicates the unit price of the workload, which is charged by ETH. Because the gas price can be adjusted, it avoids the excessive gap between the fees required for the same contract to be executed at different times due to the drastic changes in the ETH price.

When submitting a transaction to the Ethereum network, gas involves two parameters to be set (most of the time only need to set the gas limit):

Gas Price: Specify the highest price we are willing to pay for each unit of gas. The unit of gas price is the unit in Ethereum, such as Wei, GWei.

Gas Limit: Specify how many gas we are willing to pay for the execution of the transaction.

What is the meaning of gas price and gas limit? For example, the gas limit is equivalent to how much gasoline the car needs to add. The gas price is equivalent to the price per liter of gasoline. There is also a gas used by Txn, which means the actual amount of gasoline used after the end of the execution. If it is less than the gas limit, the remaining gas will be returned to the sender.

Gas cost = gas used (gas used) x gas price (gas price)

For smart contracts, different operations will correspond to different gas, such as:
Perform addition: 3 gas
Store the value of a variable: 100 gas
Call other smart contracts: 20 gas

4. What is remix?

Remix, previously known as Browser Solidity, is a web browser based IDE that allows you to write Soliditysmart contracts, then test, deploy and run the smart contract.
EB2F92AF-83DF-46E3-80FA-B881ED240772.png

Figure 1. Remix UI

5. How does contract work with Javascript?

      1. What is web3.js?
To talk to an ethereum node from inside a JavaScript application use the web3.js library, which gives a convenient interface for the RPC methods. See the JavaScript APIfor more.
  1. What is ABI?
ABI stands forapplication binary interface.
In general, an ABI is the interface between two program modules, one of which is often at the level of machine code. The interface is the de facto method for encoding/decoding data into/out of the machine code.
In Ethereum, it’s basically how you can encode Solidity contract calls for the EVM and, backwards, how to read the data out of transactions.
  1. What is Json-RPC?
JSONis a lightweight data-interchange format. It can represent numbers,
strings, ordered sequences of values, and collections of name/value pairs.
JSON-RPC is a stateless, lightweight remote procedure call (RPC) protocol. Primarily this specification defines several data structures and the rules around their processing. It is transport agnostic in that the concepts can be used within the same process, over sockets, over HTTP, or in many various message passing environments. It uses JSON (RFC 4627) as data format.
  1. ERC-20
The ERC20token standard describes the functions and events that an Ethereum token contract has to implement.
contract ERC20Interface {
    function totalSupply() public view returns (uint);
    function balanceOf(address tokenOwner) public view returns (uint balance);
    function allowance(address tokenOwner, address spender) public view returns (uint remaining);
    function transfer(address to, uint tokens) public returns (bool success);
    function approve(address spender, uint tokens) public returns (bool success);
    function transferFrom(address from, address to, uint tokens) public returns (bool success);
    event Transfer(address indexed from, address indexed to, uint tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
  1. ERC-721
ERC-721 is a more advanced token that is “non-fungible”. Think of ERC-20 as the token type for things that are money (any $5 bill is worth the same as any other $5 bill, usually) and ERC-721 as the token type for collectibles (it’s the Ethereum equivalent of baseball cards).
In ERC-721, each token is completely unique and non-interchangeable with other tokens. Another example: Pets. Many people have dogs, but THAT dog is theirs and they will not accept some other dog as a substitute. You can use ERC-721 tokens to represent those dogs and dog ownerships.
ERC-20: For money and money-like tokens.
ERC-721: For things and thing-like tokens.

6. Ethereum DApp Architecture

C64DE693-2B8E-45CF-8AB5-24D0FBB99923

Because of the special nature of DAPP, it is not possible to put everything on the ETH chain, and the efficiency overhead is not cost-effective, so it is necessary to consider this when planning. In addition to driving ETH through web3, the other is not much different from ordinary applications. It is better to have a cache of its own central database, and it is better to have high concurrent high availability.

The interface with ETH is web3, which is a set of RPC interfaces. There is nothing special about the interface and docking platforms.
Every write operation of ETH (consuming gas) is almost divided into two parts. The first step is to publish to the chain. At this time, an address will be obtained, and then the address status will be checked to determine whether the operation is completed. It may be a long time. You need to consider various exceptions, as well as server process restarts.
A765D54E-91CE-4FB6-9056-B907AF966862

7. EVM (EthereumVirtual Machine)

Smart contracts are just computer programs, and we can say that Ethereum contracts are smart contracts that run on the Ethereum Virtual Machine. The EVM is the sandboxed runtime and a completely isolated environment for smart contracts in Ethereum. This means that every smart contract running inside the EVM has no access to the network, file system, or other processes running on the computer hosting the VM

In order to reduce resource consumption and ensure system performance, the Ethereum virtual machine does not use a model that simulates a complete computer, but uses a more lightweight virtual framework.

Ethereum designed its own virtual machine (EVM) to execute transaction codes, which is the core difference between Ethereum and other systems. EVM is Turing-complete. Since the concept of gas is introduced in the Ethereum system, in principle, the total amount of calculations that can be performed in the EVM is limited by the total amount of gas. EVM uses a stack-based architecture, which is a last in, first out (LIFO) approach.

EVM has the following special design:
1) Distinguish between temporary storage (Memory, which exists in every instance of the VM and disappears after VM execution ends) and permanent storage (Storage, which exists in the blockchain state layer)
2) Using a stack-based architecture
3) Word size is 32 bytes
4) Variable, scalable memory size is used
5) Stack size is unlimited
6) 1024 call depth limit
7) No type

Highly recommend to read this two blogs:
9EEE52FE-6A1C-42B7-BCBF-874E8F73D29D
17B1C200-9DD3-4588-A59F-CCCEB340B687

8. Ethereum Storage

Let’s take a simple example of Alice giving Bob 10$.
As we can see here that we can change the state by executing a transaction on it.
Here we have to keep track of the balances and other details of different people(states) and the details of what happens between them on blockchain(transactions). Different platforms handle this differently.
Bitcoin
Bitcoin’s “state” is represented by its global collection of Unspent Transaction Outputs (UTXOs). The transfer of value in bitcoin is actioned through transactions. More specifically, a bitcoin user can spend one or more of their UTXOs by creating a transaction and adding one or more of their UTXOs as the transaction’s input.
This model of UTXO makes Bitcoin different from Ethereum. Let’s see some examples to understand the difference.
Firstly, bitcoin UTXOs can not be partially spent. If a bitcoin user spends 0.5 bitcoin (using their only UTXO which is worth 1 bitcoin) they have to deliberately self-address (send themselves) 0.5 bitcoin in return change. If they don’t send themselves change, they will loose the 0.5 bitcoin change to the bitcoin miner who mines their transaction.
UTXO transaction
Secondly, at the most fundamental level, bitcoin does not maintain user account balances. With bitcoin, a user simply holds the private keys to one or more UTXO at any given point in time.Digital wallets make it seem like the bitcoin blockchain automatically stores and organizes user account balances and so forth. This is not the case.
A visualization of how wallets work in bitcoin
The UTXO system in bitcoin works well, in part, due to the fact that digital wallets are able to facilitate most of the tasks associated with transactions. Including but not limited to:
a) handling UTXOs
b) storing keys
c) setting transaction fees
d) providing return change addresses
e) aggregating UTXOs (to show available, pending and total balances)
One analogy for the transactions in the UTXO model is paper bills (banknotes). Each account keeps track of how much money it has by adding up the amount of bills (UTXOs) in the purse (associated with this address/wallet). When we want to spend money, we use one or more bills (existing UTXOs), enough to cover the cost and maybe receive some change back (new UTXO). Each bill can only be spent once since, once spent, the UTXO is removed from the pool.
To summarize, we know that:
  • the bitcoin blockchain does not hold account balances
  • bitcoin wallets hold keys to UTXOs
  • if included in a transaction, an entire UTXO is spent (in some cases partially received back as “change” in the form of a brand new UTXO)
Ethereum
In contrast to the information above, the Ethereum world state is able to manage account balances, and more. The state of Ethereum is not an abstract concept. It is part of Ethereum’s base layer protocol. As the yellow paper mentions, Ethereum is a transaction-based “state” machine; a technology on which all transaction based state machine concepts may be built.
Let’s start at the beginning. As with all other blockchains, the Ethereum blockchain begins life at its own genesis block. From this point (genesis state at block 0) onward, activities such as transactions, contracts and mining will continually change the state of the Ethereum blockchain. In Ethereum, an example of this would be an account balance (stored in the state trie) which changes every time a transaction, in relation to that account, takes place.
Importantly, data such as account balances are not stored directly in the blocks of the Ethereum blockchain. Only the root node hashes of the transaction trie, state trie and receipts trie are stored directly in the blockchain. This is illustrated in the diagram below.
A533EFA7-EDB8-4805-A663-539E6B1EE2A5.png
There are two vastly different types of data in Ethereum; permanent data and ephemeral data. An example of permanent data would be a transaction. Once a transaction has been fully confirmed, it is recorded in the transaction trie; it is never altered. An example of ephemeral data would be the balance of a particular Ethereum account address. The balance of an account address is stored in the state trie and is altered whenever transactions against that particular account occur. It makes sense that permanent data, like mined transactions, and ephemeral data, like account balances, should be stored separately. Ethereum uses trie data structures to manage data.
A Comparison between UTXO and Account approach
The benefits of the UTXO Model:
  • Scalability — Since it is possible to process multiple UTXOs at the same time, it enables parallel transactions and encourages scalability innovation.
  • Privacy — Even Bitcoin is not a completely anonymous system, but UTXO provides a higher level of privacy, as long as the users use new addresses for each transaction. If there is a need for enhanced privacy, more complex schemes, such as ring signatures, can be considered.
The benefits of the Account/Balance Model:
  • Simplicity — Ethereum opted for a more intuitive model for the benefit of developers of complex smart contracts, especially those that require state information or involve multiple parties. An example is a smart contract that keeps track of states to perform different tasks based on them. UTXO’s stateless model would force transactions to include state information, and this unnecessarily complicates the design of the contracts.
  • Efficiency — In addition to simplicity, the Account/Balance Model is more efficient, as each transaction only needs to validate that the sending account has enough balance to pay for the transaction.
One drawback for the Account/Balance Model is the exposure to double spending attacks. An incrementing nonce can be implemented to counteract this type of attack. In Ethereum, every account has a public viewable nonce and every time a transaction is made, the nonce is increased by one. This can prevent the same transaction being submitted more than once. (Note, this nonce is different from the Ethereum proof of work nonce, which is a random value.)
A closer look at the trie structure in Ethereum
State trie — the one and only
There is one, and one only, global state trie in Ethereum.
This global state trie is constantly updated.
The state trie contains a key and value pair for every account which exists on the Ethereum network.
The “key” is a single 160 bit identifier (the address of an Ethereum account).
The “value” in the global state trie is created by encoding the following account details of an Ethereum account (using the Recursive-Length Prefix encoding (RLP) method):
– nonce
– balance
– storageRoot
– codeHash
The state trie’s root node ( a hash of the entire state trie at a given point in time) is used as a secure and unique identifier for the state trie; the state trie’s root node is cryptographically dependent on all internal state trie data.
4F4F1361-2CAD-4075-8680-711610113761
Relationship between the State Trie (leveldb implementation of a Merkle Patricia Trie) and an Ethereum block
Storage trie — where the contract data lives
A storage trie is where all of the contract data lives. Each Ethereum account has its own storage trie. A 256-bit hash of the storage trie’s root node is stored as the storageRoot value in the global state trie (which we just discussed).
896711C7-388A-48D7-B474-A20E3A289BFB
Transaction trie — one per block
Each Ethereum block has its own separate transaction trie. A block contains many transactions. The order of the transactions in a block are of course decided by the miner who assembles the block. The path to a specific transaction in the transaction trie, is via (the RLP encoding of) the index of where the transaction sits in the block. Mined blocks are never updated; the position of the transaction in a block is never changed. This means that once you locate a transaction in a block’s transaction trie, you can return to the same path over and over to retrieve the same result.
11F63715-4DE6-4D5E-BDF0-A9125B0DBBFB
Concrete examples of tries in Ethereum
The main Ethereum clients use two different database software solutions to store their tries. Ethereum’s Rust client Parity uses rocksdb. Whereas Ethereum’s Go, C++ and Python clients all use leveldb.
Ethereum and levelDB
LevelDB is an open source Google key-value storage library which provides, amongst other things, forward and backward iterations over data, ordered mapping from string keys to string values, custom comparison functions and automatic compression. The data is automatically compressed using “Snappy” an open source Google compression/decompression library. Whilst Snappy does not aim for maximum compression, it aims for very high speeds. Leveldb is an important storage and retrieval mechanism which manages the state of the Ethereum network. As such, leveldb is a dependency for the most popular Ethereum clients (nodes) such as go-ethereum, cpp-ethereum and pyethereum.
Whilst the implementation of the trie data structure can be done on disk (using database software such as leveldb) it is important to note that there is a difference between traversing a trie and simply looking at the flat key/value database.
Analysing the Ethereum database
As we mentioned previously there are many Merkle Patricia Tries (referenced ineachblock) within the Ethereum blockchain:
  • State Trie
  • Storage Trie
  • Transaction Trie
  • Receipts Trie
In Ethereum, a single Modified Merkle Patricia trie node is either:
  • an empty string (referred to as NULL)
  • an array which contains 17 items (referred to as a branch)
  • an array which contains 2 items (referred to as a leaf)
  • an array which contains 2 items (referred to as an extension)
The size complexity of a Merkle proof is logarithmic in the quantity of data stored. This means that, even if the entire state tree is a few gigabytes in size, if a node receives a state root from a trusted source that node has the ability to know with full certainty the validity of any information with the tree by only downloading a few kilobytes of data in a proof.

Reference

  1. https://theethereum.wiki/w/index.php/Remix
  2. https://ethereum.github.io/browser-solidity/

Blockchain Note – Consensus Algorithms

General Blockchain consensus algorithms:
  • PoW:Proof of Work
  • PoS:Proof of Stake
  • DPoS:Delegated Proof of Stake
  • PBFT:Practical Byzantine Fault Tolerance
  • PoSpace/PoC:Proof-of-space/Proof-of-Capacity
  • PoA:Proof of activity
  • PoB:Proof of burn
  • PoET:Proof of elapsed time
  • Casper:Ethereum new consensus algorithm
Typical-consensus-algorithms-comparison

Figure 1 Consensus Algorithms

  1. POW (Proof-of-Work)

It is an economic response to service or resource abuse or to block service attacks. Generally, users are required to perform some complicated and time-consuming complex calculations, and the answers can be quickly checked by the service provider, so that the time, equipment and energy consumed are used as guarantee costs to ensure that services and resources are used by real needs. The principle of PoW is that each node competes for the right to write in the current block by solving the problem. The fastest answer is to release the answer and other selected transaction content (that is, the current block content) to other nodes. Node verification, if other nodes verify that the block content is correct and agree that he is the earliest solver, then join the chain and store the contents of the block, and use this block content as a basis to start solving the next topic.

The advantages of POW are:
(1) For the safest public-chain consensus mechanism, Bitcoin uses this consensus algorithm
(2) The mechanism is simpler and easier to implement
(3) Relatively fair mining mechanism (that is, the generation and distribution of cryptocurrency)

The disadvantages of POW are:
(1) Consumption of a large amount of energy is calculated at the expense of energy consumption. For example, mining of mining machines now requires huge amounts of electricity.
(2) The confirmation time of the block is hard to shorten, and the bitcoin network congestion problem is getting worse.
(3) There may be a fork and you need to wait for multiple confirmations to complete the transaction.
(4) Based on (3), in theory we can say that POW has no finality, because longer chains may always appear to replace the current book, but in fact the probability of six is ​​close to 0 after confirmation.
The digital currency using the POW consensus algorithm stands for BTC, LTC, ETH, etc.

  1. POS (Proof of Stake)

First, there are no miners, only trade verifiers.
The miners in the POW have to do three things: verify the transaction, package the transaction, and count the random number, while the certifier in the POS only does the verification transaction.
Second, only the holder of the currency can verify the transaction.
One coin is one ballot. Everyone chooses the block that they think is correct, so people who don’t hold the money have no influence on the system account.

We can see two major advantages of POS:
First, the block is faster. Because the whole network does not have to work hard to calculate random numbers, only focus on verifying accounts, so POS is much faster than POW.
Second, exclude interference from unrelated stakeholders (non-shareholders) on accounting. Overall, the interests of the participating bookkeepers are the same, and everyone is maintaining the security of the entire system for their own benefit.

If I vote for a fake block made by a malicious node, I try to wind up the blocks to erase the transactions I pay to others. Once such a double payment occurs, what kind of punishment will I receive?
The answer is no punishment, this is the place where ordinary POS can’t get up, ie: Nothing at Stake.

ETH:
It can run automatically and exchange value, so naturally there is the concept of DAC Decentralized Autonomous Corporation – it is hard to imagine that in the future, a few pages of code will be a company that can perform all the tasks you can think of.

Nothing at Stake Issue -> Casper
Casper asked the verifier to charge at least 1,000 Ethereums, considering that each Ethereum is now worth $1,000, which means that the initial amount of the verification transaction has exceeded $1 million, and few people are willing to use this money as a joke. It is safer than normal POS.

Casper itself is a smart contract that doesn’t care how much margin or power is imposed by a malicious verifier. Once the evidence is malicious, it immediately strips the verifier’s chips. For the correct verifier, a normal reward will be given according to the POS algorithm.

The advantage of Casper’s POS over POW is that it reduces the scale advantage. There are a lot of calculations or a lot of coins. It used to be a very good thing, but after Casper came out, this advantage would be completely eliminated. It’s nothing to have money and resources, and it’s right. Because once you bully the system, Casper will sweep the bets on their desktops. Compared to Bitcoin, Ethereum after the implementation of POS is more decentralized.

POS is the more stencils that have more cryptocurrency (equity), the easier it is to get the right to write in the current block. It is different from POW and does not require a lot of computational power because there is no need to compete. The original version of POS produced all the currency in the early days of the creation, meaning that the new block does not give birth to new currency, so the performer verifier is not called a miner, but is still called a blacksmith, but they still get it. Transaction fee is used as a reward. Later versions of POS also had the design of block mining rewards, especially if the POS was to be applied to the public chain.
And how does POS specifically select the current block writer? If you always choose the highest equity, the member with the most currency will always have exclusive write rights, so the following two methods are usually implemented:
(1) The method of randomization
Generate random random numbers, and use random numbers to match the specific formula to select the writer. Only the high-income people still have a higher probability of being selected.
(2) Method of selection based on currency age

If you assume that everyone at DPOS is giving yourself, DPOS is actually a POS, and POS is subject to no interest. If everyone is voting for a person, then this is the centralization system.

The advantages of POS are:
(1) No competition, so low energy consumption
(2) Members of the competition write right must have money, so they prefer to guard the system to avoid evaporation of the currency compared to destruction.
(3) Compared to POW, the same size hardware budget can protect more chain assets

The disadvantages of POS are:
(1) A member who has an interest may not wish to participate in accounting
(2) If the bad guy who gets the right to write rewrites another fake chain, it only takes a small amount of computing power, which may lead to a successful double spending attack.
(3) The cost of doing bad things is very low, there is no penalty mechanism
(4) Based on (2) and (3) above, the implementation of POS needs to be combined with other mechanisms to improve this situation, and therefore more complicated than POW.
Digital currency using the POS consensus algorithm, which stands for BLK, QTUM, etc.

  1. DPOS (Delegated Proof of Stake)

The POW algorithm allows all the computing power to compete and guess the random number. The first guessed good luck person is qualified to produce the block, and the whole network node is the verifier.

In the POS algorithm, the nodes in the whole network are producers, and the holders (share holders) are responsible for verification. For example, in the future Ethereum: there is no miner, only the verifier, verify that the reward is correct, and the verification error is forfeited.

First, randomly specify the order in which the producers appear;
Second, blocks that are not produced in order are invalid;
Third, shuffle every one cycle, disrupting the original order;

Under DPOS, the work done by the producers is a bit like the bitcoin mining pool. The difference is that the producers no longer have to spend any energy to find
Random numbers, only put all the energy into the production of the correct block.
We know through the articles in the first season: the work of finding random numbers is very laborious, very slow, DPOS does not find random numbers, only
Verify the transaction, and the verification work is extremely simple. Therefore, the speed of the DPOS is like the flying power over the gap.

POW-based bitcoin processes 7 transactions per second;
Ethereum based on POW and POS handles 15 transactions per second;
Bitmap stocks (BTS) based on DPOS can handle more than 100,000 transactions per second.

Once the block is produced, it does not mean that the block will be accepted, it still needs to be verified, so the DPOS algorithm is
There are a lot of validators in the system, and production is only a prerequisite for the block. This block is valid only when everyone else agrees.

Incentive: Nothing to do, nothing to do, nothing at Stake

Without incentives, it seems that the block results are not credible, because the selected producers may not be too lazy to produce, but the holders (the holders, that is, those who have voting rights in the network) will not be too lazy to vote. Will drive those bad nodes out of the network, leaving only those honest nodes.
No network will survive because of its internal power, but only because it creates external value. Suffixed power holders will only make the entire network value lower and lower until it disappears.

Therefore, in order to lead the network to create external value, the holders will never stand by and watch any malicious, and must choose a reliable node production block. This is the only option because the interests of everyone in the network are tied together.

From the point of view of the block producers, there is no such thing as a world report (economic punishment on the spot), but it loses the most important wealth and is the only asset in the future network: credibility.

The node does evil to at least satisfy this condition:
First, hold the currency, otherwise there is no voting rights;
Second, ensure that the current gains of doing evil exceed the sum of the benefits of good deeds. But how is this possible?

One of the shortcomings of POS is that members who have rights do not necessarily want to participate in billing, and DPOS can solve this problem. Similar to the democratic representative system, it first selects the billing participant (verification node) through the equity certificate, and then through The operational mechanism allows these verification nodes to compete for block writes. At the same time, due to the significant reduction in the number of verification nodes, a consensus can be quickly reached.

In addition to the advantages of POS, DPOS has:
(1) Reducing the number of participating verification nodes and greatly increasing the speed of consensus, which is one of the key reasons why EOS can support high concurrent applications.
The disadvantages of DPOS are:
(1) Must rely on cryptocurrency, but in many cases in the current league chain there is no cryptocurrency
(2) Excluding Casper, most DPOS still have no block finality.
The digital currency using the DPOS consensus algorithm stands for BTS, EOS, etc.

  1. BFT (Byzantine Fault Tolerant)

Widely used in: Hyperleder, Stellar, Dispatch, and Ripple
Advantages: high throughput; low consumption; scalable
Disadvantages: Half trust (1⁄2 node is reliable)

This is a classic distributed computing problem, usually explained by the Byzantine failure. The problem describes the contents of several Byzantine generals and their troops, and the beleaguered cities. They must unanimously decide whether to attack. If the generals attacked separately, their siege of the city would end in failure. The generals were separated by distance and had to use messages to communicate. Some cryptocurrency protocols use different versions of BFT to reach consensus, each with its own advantages and disadvantages.

Practical Byzantine Fault Tolerant Algorithm (PBFT): One of the priority solutions is called the Practical Byzantine Fault Tolerant Algorithm. The Hyper ledger Fabric is currently in use, with very few (<20, and possibly a little more) pre-selected generals performing PBFT and showing incredibly efficient. Advantages: high transaction throughput; disadvantages: centralized / licensed.

Federal Byzantine Agreement (FBA): FBA is another solution for Byzantine generals that has been used in both Stellar and Ripple currencies. The overall idea (heh, translator’s note: author’s original heh, may want to express puns, the general’s idea), is that each general is responsible for his own chain, according to the receipt of the truth to arrange the message. In Ripple, the generals (output verification nodes) are pre-selected by the Ripple Foundation. In Stellar, anyone can be a verifier, so you can choose the verifier you want to trust.

  1. PBFT (Practical Byzantine Fault Tolerance)

“Byzantine fault tolerance” refers to the “wrong” in the distributed system, while at the same time achieving the correct consensus.
We are already familiar with the classic Byzantine fault-tolerant solution: the POW algorithm – use the workload proof to find random numbers.

PBFT just dispels the traditional scheme is not practical, so add a hypothesis, don’t underestimate this hypothesis, it will make the workload of the original shop to the horizon become almost no workload, the assumption is: bad nodes do not exceed the total number of nodes 1/3.

The whole process of the PBFT consensus algorithm is divided into five steps: the first step is “request”, which is equivalent to the client requesting read and write, and the fifth step is “reply”, that is equivalent to the system’s final response to the client.
The real consensus is the middle three steps:
Step 2: You ask the minister (pre-prepared)
The third step: the minister answers (preparation)
Step 4: Reconfirm (confirm) between bureaucratic systems

PBFT refers to you and all ministers (all network nodes) as “copy”, and you are the most important copy, the “master node”, responsible for collecting information from ministers, coordinating distribution confirmation, and finally leading the answer.

The master node broadcasts to the entire network, and this process is pre-prepared. The key to pre-preparation is distribution, where the primary node numbers each request and then sends the packet to all replicas.

The prepared message (the three arrows on the right side of Figure 1) contains the requested digital signature (poke this review), which makes it easy for other copies to verify the authenticity.

After verification, it enters the preparation phase: at this point, the copy broadcasts its own reply to the entire network. To prevent malicious attacks, the copy records both the prepared message and the prepared message on its own small book. If you look at the pre-preparation message is not pleasing to the eye, do nothing.

The most important thing in the preparation phase: Make sure all normal nodes agree.
But there are always inconsistent nodes, such as the minister who only spits out the smoke ring (copy 3), and PBFT treats this unresponsive node as invalid. Handling the failed node is what the next “confirmation phase” has to do.

In the confirmation phase, the copy first verifies the digital signature of the message. After the broadcast confirmation, the master node is responsible for statistical consensus. To put it bluntly, you only do one thing: the consensus is reached to reach 2/3 of the total number of system nodes. This consensus is the final answer.

First, at least ensure that 2/3 of the majority of normal nodes are in the same view. In other words, if the consensus does not exceed the 2/3 majority, the master node ran to reply to the client and the replica would assume that the master node failed.
Second, the request number must be within the range specified by the system. Remember that in the pre-preparation session, the master node has to assign a number to the request to broadcast? This number is designed to be mutually identifiable, otherwise the request will not be accounted for, but it also leaves a back door for the main node to disturb the system: deliberately assigning large numbers and blasting memory.

The response plan is also very simple. It is ok to set a number upper limit. This upper limit is called “waterline”. Once the copy discovery request number exceeds the waterline range, the primary node is considered to be invalid.
Third, you must define the length of the timeout, and you cannot wait for the copy to have no bottom line;

To meet this requirement, whenever a copy receives a request, it begins to time until the copy makes the final reply timing. If the response time set by the system is exceeded and the master node does not see the reply client, the copy will determine that the master node is invalid.

Therefore, the system can always deal with the failure of the master node: you are the center of work, once you are falsified or absent, it is only the next second to find someone to change, this is the core logic of the PBFT algorithm.

The PBFT algorithm is not a decentralized consensus, but a centralized control, which is why many people do not classify Ripple’s token XRP as a decentralized currency.
However, the efficiency of PBFT is the last to kill POW, so almost all large financial projects tend to use PBFT to build a coalition chain. The consensus algorithm applies to the public chain, and PBFT is a consensus mechanism that is more suitable for the alliance chain. PBFT uses the mathematical proof model to verify the speed and fault tolerance of its consensus. In the fault tolerance range, the system can not be forked. The above POW or POS can prevent the hacker from forging a large number of beneficial The node is verified, but the native PBFT does not have this capability, but if it is used in the alliance chain, this problem is naturally solved because the members and nodes of the alliance chain are originally screened and verified.

PBFT, and even all BFT can’t be applied to more than 100 nodes
The key to the reason is the message complexity of O(N^2). What does it mean? Point-to-point message transmission needs to send a message. This is O(1) message complexity. If you broadcast a message, you need to send the message to everyone in the network, which is O(N) message complexity. And if you want to reliably broadcast a message in a network with a Byzantine node (malicious node), you need at least O(N^2) message complexity, that is, if the network has 100 nodes, if you If you want to confirm that other people on the network have received your message, you must send at least 10,000 messages.

Then there was Bitcoin, which solved the reliable part with rewards, mining and the longest chain, so the larger network is just O(N) message complexity, which is scalable.

The trading output of PBFT is 1000 times per second, but it can’t be expanded. If no one has tried more than 64 nodes, it can be considered as unavailable.

The way Bitcoin reduces the communication complexity of Byzantine fault tolerance to O(N) is to give the cheater a penalty.

Thus, from the perspective of game theory, the cost and possible benefits of malicious node cheating are simplified—the only way to benefit is double payment, and the cost is more than 50% of computing power.

The study of the entire public chain consensus algorithm is actually looking for a way to ensure that block publishers will not cheat

The advantages of PBFT are:
(1) The system cannot be bifurcated within the fault tolerance range
(2) The system can tolerate any type of error within the scope of fault tolerance
(3) Verification and consensus are extremely fast
(4) No competition, so low energy consumption
(5) Based on the aforementioned point (1), the block is final

The disadvantages of PBFT are:
(1) If more than 1/3 of the verification nodes fail, the system cannot continue to operate.
(2) No ability to prevent hackers from forging a large number of verification nodes

  1. POC:Proof-of-Capacity
Most alternative agreements use certain types of paid participation models. The proof of capacity is no exception, but you need to pay for hard disk space. The more hard drives you have, the more likely you are to mine the next block and get the block reward.
This mechanism generates a large amount of plots data that will be stored in your hardware before mining in the capacity certification system. The more plots you have, the more likely you are to find the next block.
  1. POA:Proof of Activity

In order to avoid hyperinflation, Bitcoin will eventually have only 2,100 coins. This means that at some point in time, Bitcoin block rewards will stop and Bitcoin miners will only receive transaction fees.
Some people think that this will lead to serious problems, people’s behavior will be self-interest, and it will destroy the system. Therefore, Proof of activity emerged as an alternative incentive structure for Bitcoin.

Proof of activity is a hybrid approach that combines proof of effort with proof of entitlement.
In the proof of activity, mining is carried out in the form of traditional workload proofs, and miners compete with each other to solve cryptographic problems. The blocks produced by mining (which are more like templates) do not contain any transactions, so the winning block contains only the head and miner reward addresses.

At this time, the system switches to the equity certificate. Based on the header information, a random verifier is selected to sign the new block. The more tokens a system of certifiers have, the more likely it is to be selected. As long as all certifiers sign it, the template becomes a mature block.

If some of the selected certifiers do not complete the block, then the next winning block is selected, then a new set of certifiers is selected, and so on, until the block gets the correct number of signatures. The transaction fee was distributed to the miners and the verifiers of all signature blocks.

The drawback of the proof of activity is that there are drawbacks of the proof of the workload (requires a lot of energy to mine the block), and there are also the drawbacks of the proof of equity (the verifier cannot detect the double signature).
Decred is currently the only currency that uses proof of activity.

Widely used in: POA.Network, Ethereum Kovn testnet
Advantages: high throughput; scalability
Disadvantages: Centralized system
PoA is a consensus algorithm for a transaction to be verified by a user similar to the “system administrator”. These accounts are confirmed by the truth provided by other nodes. PoA has high throughput and optimizes private networks. But because it is too concentrated, you are unlikely to see this consensus in the public chain.
  1. PoB:Proof of Burn
In the proof of destruction, expensive computing equipment is not required, but they are destroyed by sending the coins to an address, and this is irreversible. By sending your currency to the imaginary address, you get the right to permanently mine in the system according to the random selection process.
Depending on the implementation of the destruction certificate, the miner may destroy the tokens of the original token or other chain, such as bitcoin. The more coins you destroy, the more chance you get to mine the next blockchain.
As time goes on, your shares in the system will decrease, so eventually you will destroy more coins to increase your chances of mining blocks. (This is a bit like Bitcoin’s mining process, you need to continually invest in more advanced computing equipment to maintain your computing power.)
  1. PoET:Proof of elapsed time
Chip manufacturer Intel released its own consensus protocol runtime proof. The system works like a workload proof system but consumes very little energy. Moreover, participants do not need to address cryptographic challenges. Instead, the mechanism uses a Trusted Execution Environment (TEE)—such as SGX—to ensure that blocks are generated in a random manner, but without the need for workload.
Intel’s approach is based on guaranteed latency provided by TEE. According to Intel, the mechanism can scale to thousands of nodes and can run efficiently in any Intel processor that supports SGX.
  1. Casper: from Ethereum
In order to solve the shortcomings of the low cost of doing the bad things in the above POS consensus, Ethereum proposed to pay the margin (cryptocurrency) to participate in the verification of the node, and if the verification node violated the rules to participate in the fraud or attack, or even just do If the system considers “invalid”, the margin will be confiscated. This consensus mechanism is called Casper. Ethereum claims that Casper will be used as a consensus algorithm at an appropriate timing in the future. Casper is not just a simple DPOS, but actually refers to the PBFT mechanism to make improvements.
Pure DPOS, like POS, has forks that can’t be finalized, and Casper guarantees the finality of the block through improved mechanisms. If the attack really occurs, both blocks of the same height are finally confirmed. If at least one-third of the verification nodes violate the rules, the margins of these nodes will be confiscated, and the value may be as high as tens of millions of dollars. As these cryptocurrencies disappear from the market, the price of the currency will rise. This may replace the previous means of enabling emergency hard forks to correct attacks.
  1. zk‑SNARK (Zero‑Knowledge Succinct Non‑Interactive Arguments of Knowledge)

1)Zero-Knowledge: You can prove your gender to the drunkard without a real guy.
2)Succinct: The evidence is short and clear, and the verification is convenient.
3)Non-Interactive means that the two sides do not talk much. The certifier only sends the information to the verifier. When it is extremely complicated, at most one round-trip can be verified.
4)Arguments of Knowledge: Others cannot be forged and have a high degree of credibility.

Zk‐proof (zero knowledge evidence)

1) Proof the number
You are the certifier, with two numbers in your pocket: 3 and 4. You want to prove that you have it, but because these two numbers are important as addresses, such as private keys, you don’t want to be seen.
I am a verifier and want to verify that you do have these two numbers, but I can’t reach out to your pocket. In the traditional sense, we only recognize it as a reality, so it is impossible to do this.

You find a glass box that is draped over the number. This box refracts the numbers so that they don’t look like 3 and 4, but 9 and 10. This magical glass box is the homomorphic hidden function E(x), which has three characteristics:
1, look at the representation number 9, can not guess the internal number 3 – although transparent, but the confidentiality is good, can not be derived by E (x).
2. Envelope 3 reveals 9 and wraps 4 out of 10 – different inputs get different outputs, ie if x≠y, then E(x)≠E(y).
3. As a verifier, I don’t know the specific number in your pocket, but the number on the surface of the glass box has the characteristics of the numbers in the glass box: I rely on E(x) and E(y), and I can get E through complex calculations. The value of x+y).
If a complex operation yields a value of E(x+y) equal to the sum of the two numbers E(x)+E(y) on the surface of the glass box, it is verified by zk‐SNARKs.

Homomorphic hiding is a type of encryption function. There are two types of subdivision: this example belongs to the additive homomorphism, that is, E(x)+E(y)= E(x+y); if E(x)Å*E is satisfied (y) = E(xy), which is the multiplicative homomorphism.

To prevent brute force: Random offset You must find a random number t so that the number transmitted by your glass box is not E(x) and E(y) itself, but E(x+t) and E(yt) ,
Otherwise your secrets are at risk of being exhausted by me. That is to say, what I see from the box at this time is no longer 9 and 10 (E(x) and E(y)), but 26 and 47 (E(x+t) and E(y-t))

2). Prove dynamic numbers
Complex scenes mean: How do you prove that you have this polynomial without showing the polynomial itself?
A glass box that relies on a homomorphic hidden function is no longer sufficient. Because in a polynomial, x may have infinite numbers. The verifier sends a random point s to the prover. The prover can complete the verification first by replying to the value of the verifier P(s). I may take the clues you gave me and brute force your polynomial, so you must find a random offset polynomial R(s ). This R(s) must be like an eye drop, so that I can’t see the P(s) at all.

You put the sum of P(s) and R(s) into the homomorphic encrypted glass box, only let me take a look at the surface number: E(P(s)+R(s)), but this is enough for me. verification.

The second question is: I sent you a random point s=2, you can find a value to fool me, because the value of P(x) may have a basket, if you don’t have this polynomial, but you can especially Guess, guess what, what should I do? The easiest way is to encrypt the random point s and send it to you.

But the trick is to do this: you can’t calculate P(s), R(s), and the cryptographic function value E(P(s)+R(s)).
So, in order for you to easily calculate E(P(s)+R(s)), I will send you all E(1), E(s), E(s2), E(s3) (s2 and s3) Represents the square and cube of s, the same below).

According to the third nature of the encryption function, you can calculate E(P(s)+R(s) based on E(1), E(s), E(s2), and E(s3).
A particularly valuable point is that the specific data of the random point s is not exposed at this time. In other words, you don’t know that I casually say that the s value is equal to 2, but you can still send valuable information to me for verification.

But if you really don’t have a polynomial, you are growing up in a lottery. How can I act as a verifier against your luck? The answer to zk‐SNARK is to continue to fill in random and dilute your good fortune.

I generate a random number k while generating the random number s, and then send you information about s and k, you just give me two numbers.
Just do:
First, the encrypted value of P(s) E(P(s))
Second, the encrypted value of kP(s) E(kP(s))

I select the random point s and the random coefficient k to send you the value of the two-slip encryption function:
E(1), E(s), E(s2), E(s3);
E(k), E(ks), E(ks2), E(ks3);
You generate a random offset polynomial R(x), which is calculated according to the value I give you:
E(P(s)+R(s))
E(kP(s)+kR(s))
You can calculate the above two values ​​and throw it to me for verification. zk‐SNARK will guarantee that we will not suffer or be fooled.

The information sent by the verifier to the prover does not change with the verification content. Therefore, it can be set in advance and reused continuously. This information that can be set in advance is called: Common Reference String: CRS Common Reference String

First, if you verify a complex polynomial, you need to spend a lot of computing resources, so it will be expensive to run on the burning platform.
The pre-common solution is to pre-compile and then run on the wind. Therefore, optimizing the calculation process is still a long and arduous road.

Second, the way to generate and store random information is still very earthy. For example, in Zcash using zk‐SNARK technology, only the encrypted value of random point s and random coefficient k is stored in CRS, while the plaintext of s and k is kept by 6 people privately.

  1. DAG (Directed Acyclic Graph)
A24724CE-F0DC-4342-8568-ABA58106E5E1

Figure 2. DAG

The transaction that is ligated together is called a tangled (Tangle)
You may be wondering why these transactions need to be wired and marked with arrows. Because each time a user initiates a transaction, they must verify the two previous transactions.

POW: The transaction initiator chooses two legal transactions and spends 2 seconds to find a random number, so that the hash value of “random number + information” meets the system requirements. The amount of work required for verification is directly proportional to the weight of the front-hand transaction. The transaction weight is equivalent to the difficulty of verification. The higher the difficulty, the longer the verification time.

In IOTA, the weight is the exponential growth of 3: 3’s 1st power, 3’s 2 power, 3’s 3 power… The more the number of times the transaction is verified, the greater the weight of the transaction

The IOTA team said that the network is not yet mature, so first find a coordinator to see the site. This coordinator is a server called Coordinator. Whether all transactions are legal or not, the boarder will make a decision for the time being, and tell the other nodes after the boarding, which transactions should be verified.

IOTA uses its own hash algorithm curl, but the hash value of the curl algorithm is very easy to collide, so it can forge digital signatures.

34% attack.
One way to prevent such attacks is to recruit miners, but since the IOTA has no fees, all of them do not respond to the mines. At the same time, IOTA is also facing the possibility of denial of service attacks, like the community that does not charge property fees, relying on owner autonomy. It is very difficult to clear the illegal elements and even sweep the fallen leaves.

DAG is a data storage structure that has been in use for more than 30 years since it was invented and has no problem in itself. But the difference between it and the blockchain is that DAG has no traditional consensus, and the credibility of each transaction depends on the number of people who believe in the deal.

So the core issue of adopting DAG technology is how to protect the consistency achieved by the whole network?

The IOTA used a centralized solution: first, the assistants took care of them, and then slowly let go. The Byte-ball, another cryptocurrency that uses DAG, is very simple, and 12 miners protect the system by collecting transaction fees.

DAG used to be a bitcoin expansion solution, but it was not adopted at the end because DAG-based distributed networks are harder to be more effective than blocks in terms of protection consensus.

  1. HashGraph

Separation of accounts is the first time in human history that distributed accounting has been done.
Honest nodes exceed 2/3 of the total number of nodes, which is the fundamental assumption of all Byzantine fault-tolerant algorithms.

The money control person and the bookkeeper are distributed on both sides of the shopkeeper, and they are mutually restrained, but they often have nothing to worry about. Later, the bank paid full-time management and handed over the statement every month. The treasurer checked his own account and continued to be safe.

Until someone suspects something inside the bank, it will silently issue coins that you don’t know. This person invented a new accounting technique.
– Blockchain: A handbook, everyone remembers, whoever is not.
Many people think that the blockchain is distributed accounting, and the distributed account does not have to be recorded with the blockchain. There is also a method: hash map.

Hash Graph is a kind of distributed accounting technology. The most dazzling feature is fast and accurate. It can record the whole network account between the electric and the flint.
Fast and quasi-Hash map’s Gossip Protocol: All nodes tell their neighbors about their trading information, and pass the transaction messages of the neighbors to other nodes. So rumors are like nuclear fission, shot to the whole network

When all the nodes receive the next moment of your event, the account is already recorded. Why?

Because you know that “others know this thing”, and others know that “you know that others know this thing”, stunned? But knowing that this chain can be extended indefinitely, eventually forming a consensus across the network. The whole process is amazing: everyone did not sit around and raise their hands and vote, and this event was recognized.

In Hashitu, this process is called Virtual Voting. “Virtual” means there is no ballot box or teller, but everyone can calculate the consensus without any mistakes.

All of this must be characterized by the hash value: concise and not counterfeit. On this basis, with the rumor agreement and virtual voting, the hash map is finally rushed to a ride.

The hash map is also a Byzantine fault-tolerant algorithm. The sub-category belongs to the completely asynchronous Byzantine Fault Tolerance (aBFT). The name is very awkward, but it is very simple to translate into vernacular: no assumptions about the speed of information dissemination, so Can deal with the impact of communication failures and denial of service attacks Of course, the shortcomings of the completely asynchronous algorithm are also obvious: waiting for less than 2/3 majority nod, all nodes can not be consensus, because everyone is waiting for each other.
The key question is: What should I do when a malicious node exceeds 1/3?
The answer is that there is no way to do it.

License Chain: Nodes need to be approved to be finalists.
The scope boundary of the license chain is the capability boundary of the hash map, which is one of the limitations.
Hashtu is a patented technology that can’t be used without paying.

Each transaction can be booked according to the time it actually takes place, instead of the blockchain technology like Bitcoin, the actual trading time is often not matched with the time recorded by the book.

This is because bitcoin miners usually have to look at the transaction broadcast first, if they find that the priority of a transaction is not high enough) or the fee is not sexy, the miner will press it under the drawer, first package other transactions , look left and right, etc., the registration time on the final account book will be later than the actual transaction time.

However, under the Hash, the trading time is consistent with the actual situation, because the moment of the transaction has spread throughout the network and is recognized. Don’t underestimate this efficiency. There are many innovative applications in the real world. The most famous one is to prevent pre-emptive transactions in the securities industry.

Hash diagram maintains network calm through pre-screening, but it can’t resist malicious flooding itself, so once it flocks to a strange node, it will poke the dead point of the hash map: security.

  1. IPFS
HTTP drawbacks

1) HTTP is inefficient, and the server is expensive. You can only download one file at a time from a single computer server using the HTTP protocol, instead of getting files from multiple computers at the same time. Video transmission over P2P can save up to 60% of bandwidth costs.

2) Historical files have been deleted for an average life of 100 days, and a large number of website files cannot be stored for a long time. Some important documents may disappear on the Internet forever due to improper operation.

3) A centralized network limits the opportunity that the Internet has always been a catalyst for human progress, but a centralized network is easily controlled and is a threat to the sound development of the Internet.

4) Network applications are too dependent on the backbone network

In order to ensure the reliability of the data, the application we developed relies too much on a large central server and guarantees data security through a large number of backups.

IPFS can essentially change the distribution mechanism of network data. How IPFS works:
1) Each file and all its blocks are given a unique fingerprint called a cryptographic hash.

2) IPFS deletes files with the same hash value through the network. It can be used to determine which files are redundant and duplicated. And keep track of the version history of each file.

3) Each network node only stores the content it is interested in, as well as some index information, which helps to figure out who is storing what.

4) When looking for a file, you can find the desired file on the network by looking up the node where the file is saved by using the hash value of the file.
Using a file called IPNS (Decentralized Naming System), each file can be collaboratively named as an easy-to-read name. Searching makes it easy to find the files you want to view.

As can be seen from the introduction of IPFS, IPFS envisages that all network terminal nodes not only serve as the role of Browser or Client. In fact, everyone can act as the operator of this network, and everyone can be a server.

Compared to HTTP, IPFS has some of these features:
1) Based on content addressing, not domain name based addressing. The file (content) has uniqueness of existence. A file is added to the IPFS network, and a uniquely encrypted hash value is given to the content based on the calculation. This will change the habit of using domain names to access the network.
2) Provide a historical version of the file controller (such as git) and let multiple nodes use to save different versions of the file.
3) The IPFS network runs a blockchain, which is a hash table for storing Internet files. Each time there is network access, the address of the content (file) is queried on the chain.
4) By using the incentives of FileCoin, each node has the power to store data. Filecoin is a storage network driven by cryptocurrency. Miners get Filecoin by providing open hard disk space for the network, while users use Filecoin to pay for storing encrypted files on a decentralized network.
Use Case: Watch a video called ABC
1) Join the IPFS network, search the network for files called ABC, (via IPNS – decentralized file naming system)
2) The IPFS network quickly indexes the hash value on the blockchain and feeds back the search results.
3) You pay a little FileCoin token, get the ABC file cache to the local, ABC file is not downloaded from the cloud or server, but contributed by the participants of this network, it may be the closest network node to you. The advantage is that not only does the intermediate server not be needed, but the network is the most efficient.
4). If the ABC file happens to be there for several people around you, the IPFS network will split the file into a small piece, saving the storage cost of these nodes and allowing you to download the video in the most efficient way.
5) This video file is cached in your own computer, not only to watch it yourself, but also to provide resources for others.
6) You can also post new content to this network, and have access to FileCoin tokens, because you also contributed to the network. In this way, the file utilization of the entire network is achieved to achieve optimal efficiency.

Traditional distributed system consensus algorithm

  1. Paxos
It is a consistent algorithm based on message passing and highly fault tolerant.
Phase 1
a) The proposer sends a prepare message to more than half of the acceptors in the network.
b) acceptor replies to the promise message under normal circumstances
Phase 2
a) proposer sends an accept message when there are enough acceptors to reply to the promise message
b) acceptor reply accepted message under normal circumstances
A typical example of Paxos used in distributed systems is Zookeeper, the first proven consensus algorithm whose principle is based on two-phase commit and extension.
The Paxos algorithm divides nodes into three types:
Presenter: Make a proposal and wait for everyone to approve the case. Often the client plays the role
Acceptor: Responsible for voting on the proposal. Often the server plays the role
Learner: was told to close the case and harmonize it, not participating in the voting process.
It is possible to make a proposal for the client or server basic process including the proposer, first to get the support of most acceptors, and when more than half of the support, send the result of the settlement to everyone for confirmation. A potential problem is that the prover has failed during this process and can be resolved by a timeout mechanism. In the worst case, every time a new round of proposal’s proposer fails, the system can never reach agreement (probability is small). Paxos guarantees that the system will reach a consensus when more than 50% of normal nodes are present.
  1. Raft
The Raft algorithm is a simple implementation of the Paxos algorithm. Raft’s core idea is easy to understand. If several databases have the same initial state, as long as the subsequent operations are consistent, the subsequent data will be consistent. From this,
Raft uses Log to synchronize, and divides the server into three roles: Leader, Follower, Candidate, which can convert each other.
The basic process is:
1) Leader Election: Each Candidate will propose an election plan after a certain period of time. The most votes in the most recent stage are selected as Leaders.
2) Synchronization Log: Leader will find the latest Log record in the system and force all Followers to refresh to this record, where Log refers to the occurrence of various events.
  1. Zab

The full name of Zab is the Zookeeper atomic broadcast protocol, which is the consistency protocol used internally by Zookeeper. Compared to Paxos, Zab’s biggest feature is to ensure strong consistency (or linear consistency consistency).
Like Raft, Zab requires a unique Leader to participate in the resolution, and Zab can be broken down into three phases: discovery, sync, and broadcast:
1) Discovery: election PL (prospective leader), PL collection Follower epoch, according to Follower’s feedback PL generates new epoch (new epoch is generated each time a new leader is elected, similar to Raft’s term)

2) Sync: PL Completion Compared to the status of the Follower majority, the follower is then replenished compared to the state of the PL missing, PL and Follower complete the state synchronization, PL becomes the official leader (established leader)

3) Broadcast: The leader handles the client’s write operation and broadcasts the status change to the Follower. After the Follower majority passes, the Leader initiates the status change (deliver/commit).

The heartbeat between the Leader and the Follower is used to determine the health status. Normally, Zab is in the broadcast phase. When an abnormality such as Leader downtime or network isolation occurs, Zab returns to the discovery phase.

Paxos, Raft, Zab, and VR are all protocols for solving consistency problems. The Paxos protocol text tends to be theoretical. Raft, Zab, VR tend to practice, and the degree of consistency guarantee also causes differences between these protocols. Compared to Raft, Zab, and VR, Paxos is purer and closer to the source of consistency problems. Although Paxos tends to be theoretical, it does not mean that Paxos cannot be applied to engineering. Paxos-based engineering practices must consider specific requirements scenarios (such as how consistent they are) and then wrap them in Paxos’ original semantics.

Reference

Blockchain Note – Common Concepts

1. Byzantine failures

There are N generals preparing to attack the city, and the information transmission can only rely on the messenger. Each general has his own idea, but only when more than half of the generals attacked the city together to win, how to finalize the siege time?

Sending only text messages brings two problems:
1) The original text is easily falsified;
2) The addressee cannot verify whether the original text has been tampered with;
One is the digital signature we are familiar with, but this only applies to one-to-one transmission scenarios.
If you want to broadcast the message to the whole army, you have to use the second method: proof of workload.

Answer 1:
Put together the summarizing intentions of all the generals on a hill:
General 1: 1 pm tomorrow;
General 2: 3 o’clock tomorrow;
General 3: 3 o’clock tomorrow;
General 4: 5 days after tomorrow;
General 5: 7 days after tomorrow;
Find a lucky number, so that the hash value of “Information + Lucky Number” is “00000000” and send 10 letters to send letters. Even if one of the communications soldiers is intercepted, the other hill general can receive 9 “messages + lucky numbers”. After the hash, there are 8 “0”s. The 8 indicates that the difficulty is already high. The probability of the enemy being cracked is One trillion. So decisively believe and use the same way
reply:
General 6: 3 o’clock tomorrow;
General 7: 3 o’clock tomorrow;
General 8: 1 o’clock tomorrow;
General 9: 7 days after tomorrow;
General 10: 3 o’clock tomorrow;
Find a lucky number, so that the hash value of “Information + Lucky Number” is “00000000” and send 10 communication soldiers to send letters.
After a few rounds of confirmation, you can reach an agreement: 3 points to attack the city tomorrow.
Increase the number of communications and increase the difficulty.

Because the enemy knows: First, it takes a long time to find lucky numbers. Secondly, nine soldiers have passed the news in the process of thousands of hashes. Even if I find lucky numbers, I can’t confuse them.

Answer 2:
As long as the vast majority of nodes do not do evil, each node tries its best to broadcast its own message, and using the Workload Proof (POW) method, the entire network will form a correct consensus.

We know that designing a system requires fault tolerance, and here the fault is diverse. Most distributed databases can tolerate downtime, network disconnection, and network delays caused by pseudo-down/pseudo-networks. Broken and other errors. This type of problem can be solved simply by resending the +fence token with timeout. In systems that require distributed consistency, you can use the quorum algorithm, such as raft, Paxos, and Zab.

The system of Byzantine fault tolerance means that in an untrusted environment, every single point knows the whole network, and the majority is honest. In this case, even if you don’t know Which point is illegal, and it can also make all the points of integrity reach a consistent system (for example, let hundreds of millions of computers agree: who owns the coin).

The blockchain system is fault-tolerant even in Byzantine error, that is, the system can maintain the correctness of the system when the Byzantine error occurs, and the general distributed database cannot tolerate Byzantine errors (so to ensure that Byzantine errors cannot occur)

2. Double Spending Attack

Double Spending refers to a potential defect in the design of a currency that allows certain currencies to pay more than once.
Take the BTG incident as an example. After the hacker temporarily controls the blockchain, he continually initiates and cancels the transaction at the exchange, and turns a certain number of BTGs between multiple wallet addresses. A “money” has been spent. The hacker’s address thus got 388,201 BTGs.
Any currency payment system will inevitably face the challenge of double payment problems. There are two solutions to prevent double payment: centralization and decentralization.
Centralized solution:
Create a trusted third-party agency to identify whether each money has been paid.
Bank is such a third party institution
The centralized solution has a problem: if the center itself is not working, the whole system is easy to misunderstand that the empty check is only one of the double payments, and the other double payment that is more powerful and spreads is the currency.
Decentralized solution:
There is an encryption technique that can be used to avoid third-party verification of transactions because each transaction is recorded in real time on a fully publicized ledger. When someone else pays you a transaction, the transaction is recognized by most people, and everyone recognizes that the transaction is legal, and there is money that cannot be shaken on your account. This technology makes double payment impossible.
Another name for this scheme is Bitcoin. No matter what kind of news you see in the future, the only use of remembering Bitcoin is to solve the double payment problem with decentralized P2P technology.
We propose a solution to solve dual payments using a peer-to-peer network. The network adds timestamps to all transactions via hashing and merges them into an ever-extended, hash-based proof-of-work chain as a transaction record. Unless the full workload proof is redone, the transaction record will not be changed once it is formed.

3. 51% Attack

51% of attacks, that is, after someone has mastered the computing power of more than 51% of the whole network, they can preempt a chain of longer, forged transactions like a race. Bitcoin only recognizes the longest chain. So the forged trade will be recognized by all the nodes, and the fake will become true. This is determined by Bitcoin’s consensus mechanism PoW (workload proof).
Double-spending attacks can still occur because Bitcoin uses a consensus mechanism for proof of workload. If A wants to launch a double-spending attack, after transaction 1 and send a broadcast, the other nodes on the network record the transaction, but after generating 6 blocks, it can be determined that the transaction has been successful. After the transaction 1 broadcast, A recorded transaction 2 in its own blockchain and reused the currency of transaction 1. If A masters more than 51% of the computing power, then A can have a large chance to dig out the next 6 blocks and then broadcast again, at which point transaction 2 will be judged successful and transaction 1 will fail. That is, a sum of money spent 2 times, but the money collector of transaction 1 did not receive the money and suffered losses. This is the so-called double spending attack. Of course, if the computing power is not enough 51%, you can also launch a double-spending attack, and there is a possibility of success. The lower the power, the less chance that the double-spending attack will succeed.

4. Sybil attack

The witch attack is in the P2P network. Because the node joins and exits at any time, in order to maintain network stability, the same data usually needs to be backed up to multiple distributed nodes. This is the data redundancy mechanism. Sybil attack is an effective means of attacking data redundancy mechanisms.

If there is a malicious node in the network, the same malicious node can have multiple identities. The data that needs to be backed up to multiple nodes is spoofed backed up to the same malicious node (the malicious node masquerades as multiple identities). This is the witch attack. .

How to solve the witch attack?
One method is the proof of work mechanism, that is, to prove that you are a node, not to say that you are not practicing, but to use the proof of computing power, which greatly increases the cost of the attack.
Another method is identity authentication (as opposed to the PoW protocol, the sybil attack is based on the BFT Byzantine fault-tolerant protocol Blockchain needs to be considered, and the corresponding identity authentication mechanism is required).
The authentication mechanism is divided into two categories:

1) Third-party based authentication
Every time a new node is added, it needs to be authenticated with a reliable third-party node.

2) Pure distributed identity authentication
Each new node needs to obtain the authentication of all reliable nodes in the current network. This method uses the public key system authentication method of random key distribution verification. It needs to obtain the authentication of most nodes in the network to join the network.

5. CAP theorem

n a distributed system, consistency, availability, and partition tolerance must not be met at the same time.

Consistency (C) refers to the complete consistency of data across the network at the same time.
Availability (A) A part of the node updates the data, the distributed system can respond to the user’s read and write requests based on the latest data.
Partition Tolerance (P) has only two optional parameters: intolerance and tolerance.
Do not tolerate partitioning is better than typing in your word, you don’t have to walk through the network, a computer can get it all; tolerance partition is a distributed system, at least two computers are online, such as the moment you save the word document, if my computer does not Interact with you, you can’t read the text you just wrote.

Distributed systems must tolerate partitioning (P), so consistency (C) and availability (A) are destined to be mutually exclusive

Bitcoin’s slowness makes sense. The reason is CAP theorem: In order to pursue the strict consistency of the entire net book in a distributed system, the availability is of course sacrificed, so it can only wait until the transaction information is deeply penetrated. On, the transaction is available

Solutions to improve availability:
First, the expansion: the original two lanes into eight lanes, so that more information flow can be carried in a unit of time, the crystallization of this scheme forks out the bitcoin cash BCH.
The second is offline trading: when the miners are busy, they will not routinely charge the small amount of transactions at the bottom of the box. This is because the incentives caused by insufficient incentives are insufficient. The high-frequency small transactions will be transferred by the “small bank”. The crystallization of this solution is the lightning network.

Impossible triangle
A9BA46B3-F7B2-4C13-AB36-A691F3BB4127

Figure 1. Impossible Triangle of blockchain

6. What is Mining?

The core principle of Bitcoin is “blockchain”. Each block corresponds to a bill. Linking all blocks is a blockchain. Any transaction information and transfer records are recorded in the blockchain.
At every other point in time, the Bitcoin system generates a random code on the system node. All computers on the Internet can look for this code. Whoever finds this code will generate a block and get a bitcoin. The process is what people often say about mining.
There is also a mechanism in the Bitcoin system, that is, Bitcoin has a limited amount, and the total amount of Bitcoin will generate 10,500,000 BTC in the first four years, halving the output every four years, and generating 5,250,000 BTC in the fourth to eighth years. In 8-12 years, there were only 2,625,000 BTC, and so on. To the end, the total number of bitcoins generated is close to 21,000,000 BTC.

7. What is hard-fork?

The hard fork is that the old node does not accept the block generated by the new node, causing the network to split into a new chain and the fork of the old chain.

DEE27994-6696-4C33-A04F-B941A72582B8

Figure 2. Hard Fork

The soft fork is the block that the old node accepts from the new node (although there may be some potential risk). If the new node is dominant, the branch game will cause the network to be finally assigned to the new chain.

E5B8B985-7228-4A9C-97A5-CF5DAAD092D2

Figure 3. Soft Fork

8. UTXO (Unspent Transaction Output)

UTXO is the basic unit in Bitcoin transactions. Once a UTXO is created, it cannot be continued to be split. It can only be spent as input to the next transaction. After spending, it will generate a new UTXO, so that the currency can be realized again and again. Value transfer.
So the account balance we see in the Bitcoin wallet is actually calculated by the wallet by scanning the blockchain and aggregating all UTXOs belonging to that user.
So, when we are saying that someone owns a bitcoin, what we are actually saying is that in the current blockchain record, the UTXO collection address for several transactions is the person’s wallet address. The sum of these UTXOs is 1 bitcoin.

For example, a wallet has a 10 dollar, a 5 dollar, a 1 dollar, a total of 16 dollar. The balance of an account in Bitcoin is also calculated based on this account UTXO.
When you spend 12 dollar to buy something, you can take 10 dollar and 5 dollar, and then get 3 dollar to change, then 10 dollar and 5 dollar before this time because it has already been spent, it is no longer UTXO, new The change of 3 dollar becomes the new UTXO, plus the untouched 1 yuan UTXO, the current balance is 4 dollar. The new transaction was recorded on a new block, but did not change the data of the historical block.
Bitcoin uses the blockchain linked back and forth to record all transaction records. When the previous UTXO appears in the input of subsequent transactions, it means that the UTXO has been spent, no longer UTXO.

From the perspective of financial system design, this approach is somewhat “transactional clearing separation”. The blockchain system only processes all transactions, but needs to clear and view the balance and other information, which is handled by the blockchain node itself.

Bitcoin’s UXTO system follows two rules:
1. Except for CoinBase, all sources of funding must come from the UXTO of one or more of the previous transactions;
2. The total input of any transaction must be equal to the total output, and the equation must be balanced on both sides.

Each bitcoin transaction is actually made up of several transaction inputs and outputs. The transaction input is the source of funds, the transaction output is the direction of the funds, and each transaction has to spend a part of the transaction input, which is the unspent transaction output (UTXO). Each transaction input can be traced back to the previous UTXO until the initial mining income.

The bitcoin transaction created by the mining result is the first transaction in each block, also known as the coinbase transaction, which is created by the miner and has no previous transaction output.

The difference between UTXO system and account system:
1). UTXO only needs to watch the last transaction, and the account system must add all the increase and decrease operations to obtain the correct balance after looking at the historical full data. The efficiency difference between the two will become larger and larger with time;

2). UTXO can cut old historical data in the future, while the account system can not discard old data. The former blockchain can control the overall size, while the latter can only continue to expand.

What problems did UTXO solve?
Digital currency cannot transfer ownership by physical transfer just like metal currency (such as gold). That is, when A gives a gold to B, A will no longer own the gold. After A transfers a digital currency to (signature) B, A can still transfer the same transaction to C, because A masters the private key, both signatures are valid signatures, which constitutes a “double spending.” Therefore, there must be a mechanism to ensure that each transaction can only be used once, that is, only the signature of the “not used” transaction can be a valid signature.

UTXO solves the double spending problem and realizes the value transfer.

9. What is the average TPS (transaction per second) of BTC? And why?

The size of one ledger per block is 1MB
Generate such a block every 10 minutes
The size of transactions per block is 250B

Number of transactions per block: 1 M / 250 = 4194
Number of transactions processed per second: 4194 / (10 * 60 ) = 6.99 = 7 TPS

Figure 4. TPS comparison

10. What is Blockchain?

The blockchain is essentially a decentralized distributed ledger database, which itself is a string of data blocks generated using cryptography, each of which contains information that is validly validated for multiple Bitcoin network transactions.
1. A blockchain is a distributed database placed in a non-secure environment.
2. The blockchain uses cryptographic methods to ensure that existing data cannot be tampered with.
3. The blockchain uses a consensus algorithm to reach consensus on new data.
A system with the above three properties is a blockchain.

11. What problem did blockchain solved?

  1. Double spending attack
  2. Byzantine failures

12. What applications should apply to blockchain?

blockchain-rev-2

Figure 5. Blockchain Applications

AC0A5AF1-01C1-4572-A02D-BE169526DBAB

Figure 6. DApp Applications

 

13. What is EDA (Emergency Difficulty Adjustment) ?

Bitcoin’s average block time is 10 minutes. This 10 minutes is adjusted by the Mining Difficulty Adjust Algorithm.
Bitcoin’s blockchain system does not know how many miners (computing power) are currently mining. It can only guess how many miners are mining by speeding out the blocks. If the block is faster than expected (the average block time is less than 10 minutes in a period of time), it means that the calculation power is much more, it is necessary to increase the difficulty of mining and reduce the miner’s speed.

14. After a few years of bitcoin mining, who will pay for the miners?

1). Financial tool
As mentioned earlier, if you simply rely on asset transfer claims to support BTC transactions, it is destined to be low-frequency. But BTC, the leader in the cryptocurrency world, will be the first currency to go online with various financial instruments. At that time, the physical settlement of futures will bring relatively frequent transfer claims, and at the same time bring about an increase in the price of the currency.

2). Strategic assets
As the amount of block awards continues to decrease, the profits of miners will become increasingly thin. Aside from the BCH factor, it is indeed slowly reaching the state of Nash equilibrium, thus adjusting the mining cost and benefit ratio to a reasonable range.
But whether or not a lot of things exist does not depend solely on whether this thing has huge profits. For example, traditional kindergartens have a profit of only 6-7% per year, but many traditional investors with low risk appetite are still particularly keen.
In the future, miners’ fees may be only 10%-15% annualized. However, if each country recognizes the value of BTC, it is an important investment target. That BTC is a very stable investment for many heavy capital. That mining cost is no longer so important.

3). Side chain
If the BTC has a lot of side chains in the future, the BTC will be transformed into a settlement layer. Although a single side chain, such as the lightning network, will reduce the number of transactions on the chain, if the BTC mounts a lot of side chains and handles a large number of settlements, the amount may be considerable. After all, BTC’s current security is still the highest. Therefore, it is not surprising to mount N side chains in the future. The side chain system represented by RSK is eager to try. Maybe ten years later, we will see a completely different BTC, and we don’t know.

4). Expansion
Although the problem of expansion has been tossed, it has ended in failure. But what happened in the past does not mean that it will happen again in the future.
If it is 2030, will BTC still remain in the 1M block?
Perhaps at a key time in the future, BTC will expand successfully. By then, more and more people will regard BTC as a “digital gold” that can be both awkward and fancy. Just as hundreds of years ago, people treated gold the same way. Perhaps more and more people in the future will use BTC to do things that do not require instant verification of payment, such as paying taxes, buying a car, buying a house, and securing assets.

15. Cross-Chain

Multi-chain is a node that can run multiple chains. You can understand that each chain has its own independent ledger data (that is, block data). The transactions on different chains are executed in parallel. You can refer to Hyperchain and RChain. The concept of namespace, not to elaborate here.

The side chain is a sub-chain derived from the main chain, and its purpose is also to solve the problem of low throughput and high delay in the current blockchain. For example, the public chain represents Ethereum, think about hundreds of DApps every day. It runs above, but it can only have 15 tps per second. For some decentralized game applications, such a low tps will only lead to a worse user experience, such as the ether cat…. The hole is wide open, these DApps that require thousands of tps are placed on their own independent blockchain (side-chain), which can have thousands of users, and the side-chains use other more efficient consensus algorithms as needed (eg Instead of the inefficient PoW algorithm, the side-chain will periodically interact with the main chain, such as the Ethereum public chain, to move anything of value to the main chain to ensure security so that the transaction can be determined more quickly. Transaction costs are lower. You can refer to the Ethereum second-tier expansion solution – Plasma.

Cross-chain, ie different blockchains, can communicate in order to achieve value interconnection in the blockchain world.

In short, whether it is multi-chain, side chain or cross-chain, its purpose is to solve one of the three major problems of the blockchain – scalability.

16. Side-Chain

The side chain refers to a block chain embedded between two main chains, which is equivalent to a bridge and connects the main chain data. The generalized side chain refers to the cross-chain technology, which can exchange the two main chains in the practical application of information and value between the chain, often one is the clearing chain, one is the application chain, and the side chain is in the middle clearing chain similar to the people. Bank, responsible for liquidation of funds. The most recognized clearing chain is bitcoin

How does the side chain connect the clearing chain and the application chain? The core logic is not complicated:
1). The key of the clearing chain exists on the side chain.
2). Establish a fast payment channel on the side chain
3). Recharge the funds on the clearing chain to the application chain.

Generalized side chain, which contains three modes: notary, narrow side chain, hash lock.

A notary public refers to a trusted intermediary by a third party or institution, and is a centralized solution, generally used in the private chain or alliance chain;
A narrow side chain refers to a chain that has all the functions of the main chain but is not known by the main chain. Specifically, the block header of the main chain is written to the side chain, and the same consensus algorithm is used for the side chain and the main chain. For example, there is a narrow side chain that connects Bitcoin and the Ethereum network, and uses Bitcoin to run smart contracts on Ethereum.
Hash lock refers to the use of hash technology to lock assets and complete transactions without the need for a trust intermediary. The most famous case of Hash Lock comes from Bitcoin Lightning Network.

The Bitcoin Lightning Network was launched by Poon and Dryja in 2015. Its idea is particularly simple: kicking the transaction outside the Bitcoin blockchain is equivalent to connecting Bitcoin to Alipay and building a fast chain. Payment channel.

Case:
There is no direct connection between A and C, but A, C is connected with B. Therefore, A can pay C to B. However, there is a trust problem at this time: If A gives B to B, and B does not give C, what should I do? At this time, A needs a random number s to help. The random number s is equivalent to a password, and A tells B the hash value H of the random number s. B can only get the money of A by H, so he can only give money to C in the channel established by himself and C, but C can’t get the money.
At this time, A tells C the random number s, and the C hash random number s. If it is equal to H, the money from B to C is taken. After B’s money is taken by C, B also sees s, and takes s to A to give B money.
Trust with the hash lock, and finally let the three parties clear.

If the lightning network can run, the most likely to become B is the major exchanges, they will evolve into transit nodes, some people will lock bitcoin, and the exchange established channels, can improve the efficiency of the entire network.
Four mainstream cross-chain technologies:
1. Notary schemes
2, side chain / relay (Side-chains/relays)
3, Hash-locking (Hash-locking)
4, distributed private key control (Distributed private key control)

17. Shading

Sharing is actually a traditional database technology that divides large databases into smaller, faster, and easier-to-manage parts called data fragmentation (Vitalik Buterin calls it Universe Universes), but applies it to the area. The blockchain can be quite complicated.
Ethereum’s shards, in simple terms, divide the blockchain network into smaller, component-based networks that can handle transactions to achieve a payment system that processes thousands of transactions per second. Set up a blockchain. There are a hundred different universes in this blockchain system. Each universe is a separate account space.
Users can have an account in a universe, and the user-initiated transaction will only affect the transaction-related universe.
If the side chain is “external grafting” to the main chain, then the fragmentation is to “internal division” of the main chain. Obviously, the latter is more difficult to implement than the former.

18. Public/Private/Consortium Blockchain

There are three major categories: Public Blockchain, Private Blockchain, and Consortium Blockchain.
The highest degree of decentralization is the public chain. This public blockchain, represented by Bitcoin and Ethereum, is not controlled by third-party agencies. All people in the world can read data records in the chain, participate in transactions, and compete for new blocks. The program developer has no right to interfere with the user, and each participant (ie, the node) can freely join and exit the network, and perform related operations as desired.

The private blockchain is completely reversed. The write permission of the network is completely controlled by an organization or organization. The data read permission is regulated by the organization, either open to the outside or has a certain degree of access restrictions. In simple terms, it can be understood as a weakly centralized or multi-centered system. Because the participating nodes are strictly limited and less; compared with the public chain, the private chain has a relatively short time to reach consensus, faster transaction speed, higher efficiency and lower cost. However, this type of blockchain is more suitable for use within specific organizations, such as the Linux Foundation.

The alliance chain is a blockchain between the public chain and the private chain, which can achieve “partial decentralization.” Each node in the chain usually has an entity or organization corresponding to it; participants participate in the operation of the blockchain by authorizing to join the network and form a stakeholder alliance. To a certain extent, the alliance chain also belongs to the category of the private chain, but the degree of privatization is different. To this end, it also has the characteristics of lower cost and higher efficiency, and is suitable for B2B transactions such as transactions and settlements between different entities.

19. What is Oracle?

When you need data that you don’t want to get yourself (or you can’t get!), you need to communicate with a third party like the oracle. there are many reasons.
On the one hand, you may not be able to trust an entity when signing a Bitcoin multi-signature transaction. Let’s say you want a fund to be transferred under certain conditions. Instead of entrusting it to yourself (without entrusting any third party) or entrusting it to a third party for execution (you won’t trust him because he may behave badly), it is better to force multiple signatures with bitcoin to dispatch the transaction approval process. To different signatories (predictive machines?).
The method of using N/M multi-signature transactions is to ensure that each oracle has only one private key, so that the predictor can It believes that the correct time to sign, but the transaction will only be valid when N of the M oracles agree on signing a transaction. This is much more powerful than trusting a single third party, because the chosen predictor may be competing, so the probability of all fraud is low.
The idea of ​​a distributed oracle network has been around for many years, but it is difficult to find consensus on a cross-prophecy communication protocol (Is Orisi one of them?). Finding parties who are willing to join the network is more difficult because of the need to build a good incentive, and we don’t know how to design interactions with this network of predictors. In addition, one of the main limitations may be the data source for which you want to get data, some of which may not be used externally without permission (we’ll discuss this later in more detail).
A predictive marketplace such as Augur or Gnosis is a good and reliable indicator of the changing mood (or knowledge!) of the crowd for the future. Forecasting markets are often referred to as oracles, but predictive markets have different meanings and coverage over the oracles discussed above.
There is another point worth discussing. Do we just want to use the oracle as a price push? In this case, the oracle is just a data source, and most of the time it won’t have any association with the blockchain. Financial institutions often refer to “Bloomberg” or “Reuters” as oracles, but the real meaning is that financial institutions simply use them as data sources. As a real prophetic opportunity to interact with the blockchain, many complex problems arise, which are unwilling to be burdened by some data sources (additional complexity and cost). However, once the appropriate data source and formula are selected, the predictor can access Bloomberg data. Calling “data sources” “prophecy machines” is a misuse of terminology.
For the convenience of description, we define these three types:
1) data source
2) Inquire
3) Prophet / Predictor Network
The data source is the source of the information you are looking for. Depending on your actual query, the data source can be anything. Some examples include “Augur” (view future events/facts), “Bloomberg” (find finance) Data), “Bitcoin blockchain” (find address balance, OP_RETURN content of a transaction or any other blockchain data), “WolframAlpha” (find response to a given Wolfram Alpha query) or .. “network” ? Getting web data sources or API data is the easiest and most common way to get data.
Queries are formulas that you can understand for your chosen data source to provide you with the data you want.
The oracle network is responsible for connecting you to the data source.
9E4C8829-C9E9-436E-88B5-51DDDFA35196

Figure 7. Oraclize structure

20. Solutions to improve TPS

1) Expansion. At present, the common expansion schemes have state channel and side-chain technology, because these two schemes seem to be relatively mature at present, the main principle of such schemes is actually very simple, that is, the non-essential calculations are not placed in the main block. On the chain, but the final result is confirmed by the main chain. This calculation is not all placed on the main chain, the equivalent of the main chain is liberated, then the speed is naturally faster. Bitcoin expansion and Genaro’s hybrid consensus mechanism are all enhanced by TPS.

2) Reduce the complexity of consensus algorithms. The lower the complexity of the algorithm, the shorter the time to complete the calculation under the same hardware conditions. Genaro is using this method to enhance TPS, and Genaro uses SPoR instead of PoW. SPoR is a proof of data retrievability and does not require a lot of computational power and does not increase over time.

3) Reduce the number of nodes per synchronization. From a technical point of view, reducing the number of nodes per synchronization has a huge impact on TPS. Tested in seven data centers distributed on five continents, the highest performance is achieved when there are only two nodes (Validator/verifier), and TPS will decrease as nodes increase.

4) Sharding. Sharding is a common method in the database, namely parallel computing. Blockchain is essentially a way to store data, so using database optimization is a good idea. So when running some basic operations, using parallel operations, then time is naturally saved.

21. TheDAO Attack

The DAO project is a crowdfunding project initiated by blockchain internet of things company Slock.it. Originally, the company only wanted to use DAO (Decentralized Autonomy) to run its own system, the Universal Sharing Network (USN). Later, I found out that this mechanism is also suitable for other projects, so I decided to create The DAO, which means “the mother of DAO”. After being attacked by a smart contract, Ethereum hardly forked out ETH.

Callback
Solidity has a thing called a callback function that has no name, no parameters, no return value, and is triggered under the following conditions:
(1) When the contract is called, if no other function can match the called function or the function that is not specified at all
(2) When the contract receives an ether transfer without a contract
Please note: If you transfer to a contract address, there must be a callback function in the contract, otherwise the transfer will be rejected.
The difference between send and call
Both addr.send(123) and addr.call.value(123)() can be used to transfer 123wei to msg.sender, but
Send: callback function can use up to 2300gas
Call: Pass all the remaining gas of this contract to the contract that accepts the transfer, which means that the contract accepting the transfer can perform more functions with gas.
Conclusion: Use transfer or send, don’t use call

22. DAO(Distributed Autonomous Organization)

DAO is sometimes referred to as DAC (Distributed Autonomous Corporation). Whether it is DAO or DAC, it refers to an organizational form that can operate autonomously without intervention and management through a series of open and fair rules. These rules often come in the form of open source software, and everyone can become a participant in the organization by buying shares in the organization or providing services.

Reference

  1. https://paper.seebug.org/544/
  2. https://theaccountant.org.mt/the-blockchain-revolution/blockchain-rev-2/

Blockchain Note – BTC Transaction Relaying

In this chapter we will implement the relaying of such transactions, that are not yet included in the blockchain. In bitcoin, these transaction are also known as “unconfirmed transactions”.
Typically, when someone wants to include a transaction to the blockchain (= send coins to some address ) he broadcasts the transaction to the network and hopefully some node will mine the transaction to the blockchain.
As a consequence, the nodes will now share two types of data when they communicate with each other:
  • the state of the blockchain ( =the blocks and transactions that are included to the blockchain)
  • unconfirmed transactions ( =the transactions that are not yet included in the blockchain)
  1. Transaction Pool

We will store our unconfirmed transactions in a new entity called “transaction pool” (also known as “mempool” in bitcoin). Transaction pool is a structure that contains all of the “unconfirmed transactions” our node know of. In this simple implementation we will just use a list.
let transactionPool:Transaction[]=[];
  1. Add Transaction to the Pool

We create the transaction just like we did in chapter4. We just add the created transaction to the pool instead of instantly trying to mine a block:
const sendTransaction=(address:string, amount:number):Transaction=>{
   const tx:Transaction=createTransaction(address, amount, getPrivateFromWallet(), getUnspentTxOuts(), getTransactionPool());
   addToTransactionPool(tx, getUnspentTxOuts());
   return tx;
};
  1. Broadcasting

The whole point of the unconfirmed transactions are that they will spread throughout the network and eventually some node will mine the transaction to the blockchain. To handle this we will introduce the following simple rules for the networking of unconfirmed transactions:
  • When a node receives an unconfirmed transaction it has not seen before, it will broadcast the full transaction pool to all peers.
  • When a node first connects to another node, it will query for the transaction pool of that node.
We will add two new MessageTypes to serve this purpose:QUERY_TRANSACTION_POOL and RESPONSE_TRANSACTION_POOL. The MessageType enum will now look now like this:
enum MessageType {
   QUERY_LATEST=0,
   QUERY_ALL=1,
   RESPONSE_BLOCKCHAIN=2,
   QUERY_TRANSACTION_POOL=3,
   RESPONSE_TRANSACTION_POOL=4
}

The transaction pool messages will be created in the following way:

const responseTransactionPoolMsg=():Message=>({
   'type':MessageType.RESPONSE_TRANSACTION_POOL,
   'data':JSON.stringify(getTransactionPool())
});

const queryTransactionPoolMsg=():Message=>({
   'type':MessageType.QUERY_TRANSACTION_POOL,
   'data':null
});
Whenever, we receive unconfirmed transactions, we try to add those to our transaction pool. If we manage to add a transaction to our pool, it means that the transaction is valid and our node has not seen the transaction before. In this case we broadcast our own transaction pool to all peers.
case MessageType.RESPONSE_TRANSACTION_POOL:
   const receivedTransactions:Transaction[]=JSONToObject<Transaction[]>(message.data);
   receivedTransactions.forEach((transaction:Transaction)=>{
       try{
           handleReceivedTransaction(transaction);
           //if no error is thrown, transaction was indeed added to the pool
           //let's broadcast transaction pool
           broadCastTransactionPool();
       }catch(e) {
           //unconfirmed transaction not valid (we probably already have it in our pool)
       }
   });
  1. Validate transaction

As the peers can send us any kind of transactions, we must validate the transactions before we can add them to the transaction pool. All of the existing transaction validation rules apply. For instance, the transaction must be correctly formatted, and the transaction inputs, outputs and signatures must match.
In addition to the existing rules, we add a new rule: a transaction cannot be added to the pool if any of the transaction inputs are already found in the existing transaction pool. This new rule is embodied in the following code:
const isValidTxForPool=(tx:Transaction, aTtransactionPool:Transaction[]):boolean=>{
   const txPoolIns:TxIn[]=getTxPoolIns(aTtransactionPool);
   const containsTxIn=(txIns:TxIn[], txIn:TxIn)=>{
       return _.find(txPoolIns, (txPoolIn=>{
           returntxIn.txOutIndex===txPoolIn.txOutIndex&&txIn.txOutId===txPoolIn.txOutId;
       }))
   };

   for (consttxIn of tx.txIns) {
       if (containsTxIn(txPoolIns, txIn)) {
           console.log('txIn already found in the txPool');
           returnfalse;
       }
   }
   return true;
};
There is no explicit way to remove a transaction from the transaction pool. The transaction pool will however be updated each time a new block is found.
  1. From transaction pool to blockchain

Let’s next implement a way for the unconfirmed transaction to find its way from the local transaction pool to a block mined by the same node. This is simple: when a node starts to mine a block, it will include the transactions from the transaction pool to the new block candidate.
const generateNextBlock=()=>{
   const coinbaseTx:Transaction=getCoinbaseTransaction(getPublicFromWallet(), getLatestBlock().index+1);
   const blockData:Transaction[]=[coinbaseTx].concat(getTransactionPool());
   return generateRawNextBlock(blockData);
};
As the transactions are already validated, before they are added to the pool, we are not doing any further validations at this points.
  1. Updating the transaction pool

As new blocks with transactions are mined to the blockchain, we must revalidate the transaction pool every time a new block is found. It is possible that the new block contains transactions that makes some of the transactions in the pool invalid. This can happen if for instance:
  • The transaction that was in the pool was mined (by the node itself or by someone else)
  • The unspent transaction output that is referred in the unconfirmed transaction is spent by some other transaction
The transaction pool will be updated with the following code:
const updateTransactionPool=(unspentTxOuts:UnspentTxOut[])=>{
   const invalidTxs=[];
   for (consttx of transactionPool) {
       for (consttxIn of tx.txIns) {
           if (!hasTxIn(txIn, unspentTxOuts)) {
               invalidTxs.push(tx);
               break;
           }
       }
   }

   if (invalidTxs.length>0) {
       console.log('removing the following transactions from txPool: %s', JSON.stringify(invalidTxs));
       transactionPool=_.without(transactionPool, ...invalidTxs)
   }
};
As it can be seen, we need to know only the current unspent transaction outputs to make the decision if a transaction should be removed from the pool.

Reference

Blockchain Note – BTC Network

BTC Network Concepts

Bitcoin Network refers to all nodes to which transaction information can be broadcast.

There are two types of nodes: ordinary wallets and miners. Ordinary wallets only report or verify transactions and belong to ordinary users. The miners are responsible for bookkeeping and proof of work, and are professional book maintainers.

If you are a miner, you can’t hide the problem with the cows. You must broadcast to the whole network in the first time, otherwise the bonus belongs to others.
After other miners in the network receive your broadcast, they only verify three things:
1. Are all transaction inputs ≥ transaction output?
2. Have all transactions never occurred?
3. Use your random number hash block. Does the hash value prefix have enough “0”?

New transactions are broadcast, no need to reach all the nodes, as long as enough nodes are reached, they will soon be integrated into one block. At the same time, block broadcasts are fault tolerant to discarded information.

If you are a miner, once you are surrounded by a malicious node, even if you have calculated the workload and can’t broadcast it, then you will lose CPU resources and power, no reward, and you can’t make ends meet.

Routing Attacks

Any block information from the starting point to the target node is in the maze, and each node in the maze is a sign. The routing attack is deliberately pointing the wrong way, let the data packet sway in the maze, and eventually disappear into the network.

BGP: Border Gateway Protocol split attack is a vulnerability in the Diamond Border Gateway Protocol
Even worse is the delayed attack: the network traffic of the Bitcoin node can be intercepted without leaving traces, and the delay block is updated for 20 minutes. Bitcoin systems generally have one block every 10 minutes. A delay of 20 minutes is equivalent to cutting the miners’ ability to profit by mining. Ordinary wallet nodes will not be able to update blocks in time, and may face double payment risks.

Bitcoin is a decentralized network. Shouldn’t it be like a stream of water? There is only one reason for such vulnerability: the nodes are not evenly distributed.

BGP hijacking is the basic way to attack the Bitcoin network. It will form a black hole, and all information will not flow in, reducing network efficiency. When there are more black holes, a boudoir will be formed. Everyone in the boudoir will bury their own coins, until one day the world is transparent. Those who only look at the coins and forget to look up the road, they find that there is actually a world outside. a longer chain, and the original mining reward disappears instantly.

BTC Network in Code

Notes from Chapter 1 of BTC demo

Communicating with other nodes

An essential part of a node is to share and sync the blockchain with other nodes. The following rules are used to keep the network in sync.
  • When a node generates a new block, it broadcasts it to the network
  • When a node connects to a new peer it queries for the latest block
  • When a node encounters a block that has an index larger than the current known block, it either adds the block the its current chain or queries for the full blockchain.
FFD7BD1C-1EF5-4CF2-8764-3A2E76758318

Figure 1. Node communication

We will use websockets for the peer-to-peer communication. The active sockets for each nodes are stored in theconst sockets: WebSocket[]variable. No automatic peer discovery is used. The locations (= Websocket URLs) of the peers must be manually added.

Controlling the node

The user must be able to control the node in some way. This is done by setting up a HTTP server.
const initHttpServer=( myHttpPort:number)=>{
   const app=express();
   app.use(bodyParser.json());
   app.get('/blocks', (req, res)=>{
       res.send(getBlockchain());
   });

   app.post('/mineBlock', (req, res)=>{
       const newBlock:Block=generateNextBlock(req.body.data);
       res.send(newBlock);
   });

   app.get('/peers', (req, res)=>{
       res.send(getSockets().map(( s:any)=>s._socket.remoteAddress+':'+s._socket.remotePort));
   });

   app.post('/addPeer', (req, res)=>{
       connectToPeers(req.body.peer);
       res.send();
   });

   app.listen(myHttpPort, ()=>{
       console.log('Listening http on port: '+myHttpPort);
   });
};
As seen, the user is able to interact with the node in the following ways:
  • List all blocks
  • Create a new block with a content given by the user
  • List or add peers

Architecture

It should be noted that the node actually exposes two web servers: One for the user to control the node (HTTP server) and one for the peer-to-peer communication between the nodes. (Websocket HTTP server)
D9DEF9EF-4B4C-4236-BFDC-A0CC5A1C5E4C

Figure 2. BTC demo network architecture

Reference

  1. https://lhartikk.github.io/jekyll/update/2017/07/14/chapter1.html

Blockchain Note – BTC Transaction

Transaction in BTC

Bitcoin or similar distributed systems will have a design that is very different from the normal design:
Each node in the layout is both a client and a server.

So in the design of distributed systems, when using classes to describe objects, it is sometimes necessary to distinguish between which classes are used as clients and which classes are used as servers to receive clients. Because the client and the server are running the same set of code, but in the actual operation process, if you follow the C/S model (in fact, you should not look at it this way, you should change your thinking into the thinking of designing p2p nodes). Will see the Tx/Block produced by C and the Tx/Block received by S

Vector<CTxIn> vin;
Vector<CTxOut> vout;

These two key member variables. These two member variables represent the “income” and “expense” of bitcoin transactions, respectively. The Bitcoin transaction is not a record of the account-shaped data changes (for example, we use the bank model to describe A to B transfer 100 yuan, then the bank will record 3 records in the process of recording this transfer, these three records are connected A Transaction process: A’s account is reduced by 100 yuan, the recorded id is tid1, B’s account is added with 100 yuan, the recorded id is tid2, and a transfer record records tid1 transferred to tid2 for 100 yuan, becoming A. The account is reduced by the “relationship connection” with the increase of the B account.), but the log shape: the Tx of the bitcoin only records the “relational connection” of the transfer from A to B. This log record only includes the transfer of A to B by 100.

And here in is to record from ”who’ (currently simple to see this, the actual is not only this, the text will slowly re-interpret), out is transferred to who, and how much money is transferred is included in In Out

In storage, bitcoin uses Tx’s hash as the key and CTxIndex as the value. So we got one
CTransaction (or its subclasses) can get the TxIndex corresponding to this Tx by obtaining the local storage of the hash index of this Tx.

CMerkleTx

This class is a subclass of Tx that is used to handle the relevant processing in the Block. CMerkleTx is a related class when the miner (server) holds the Tx
It added on the basis of the original Tx
classCMerkleTx: public CTransaction{
public:
uint256 hashBlock;
vector<uint256> vMerkleBranch;
int nIndex;
};
3 properties, hashBlock represents the hash of the current Tx block (as an index), vMerkleBranch is the hash value of the Tx paired in the merkle tree (this paired hash value will be explained in a later article), here is Used to verify Tx’s additional information in the block. Index represents the location of the Tx in the block.

vs Traditional Bank

According to the traditional process (online banking model), the two processes should be as follows: ps (about the concept of the central coinage node reference bitcoin white paper article)
• The originator of the transaction obtains the balance in the account of the central coinage node (such as a bank) (not required) -> the transaction initiator obtains the relevant information of the transaction recipient (such as the bank card number of the other party) -> the transaction initiator will transfer the amount And the other party’s information tells the central coin node
• The central coin node obtains the information of the initiator -> checks the originator’s account balance -> determines whether the transfer amount is below the balance -> destroys the amount of the initiator’s account that needs to be transferred (reducing the assets of the initiator’s account) -> casting to receiving The account accepts the amount of the transfer (increasing the same assets of the recipient’s account) -> destruction and casting constitute a “transaction”

In the overall process of bitcoin’s transfer, the two processes are different. The transaction initiator and the transaction recipient are in the same angle, but the role of the verification transaction is changed from the original “central coinage node” to “All miners and everyone who accepts the block containing this Tx”
• The transaction originator checks his own locally stored information (Wallet) to get the total assets he can use (not required) -> the transaction initiator gets the relevant information of the transaction recipient (bitcoin address) -> after local inspection ( Not necessarily) generating a transaction Tx and broadcasting

The miners (we) receive the transaction -> according to their own local storage information (all TxIndex) used to verify the historical transaction information corresponding to the TxIn received information, the most important two aspects
• Whether the Tx corresponding to TxIn is not spent (UTXO)
‐> If the verification is successful, put it into the memory pool and wait for it to be packed into the block.
‐> After packaging into the block, modify your local storage (TxIndex) ‐> Broadcast this block to more people
‐>(Other miners) If the block received is the longest chain–>Verify this block–>Verify the verification and save it (others’ history)->Remove the relevant Tx->interrupt in the memory pool Own packing block process -> repacking the Tx in the remaining memory pool

After the above analysis, the comparison between the traditional way and the bitcoin way can be concluded that the biggest difference is:
Where was the process of verifying the history being transferred?
Because in the correctness of the guarantee transaction process, the key point is that a transaction can be successful, depends on whether the previous transaction is correct, such as:
• In a traditional transaction, the user’s account balance is checked to be the transfer amount.
• In bitcoin, check to see if the TxIn used by Tx has been spent and is controlled by the originator of the transaction (pub key_script, sig_script pairing)
The traditional transaction and bitcoin are quite significant:
• Traditional transactions must rely on the central node. It is absolutely impossible to bypass the center to generate transactions, otherwise the transaction will not be recognized.
• The verification history of bitcoin transactions is based entirely on the history of personal storage, without exception, whether it is an individual or a miner, this is back to the questions raised in the analysis section:

How is a Tx in a distributed environment, even if the nodes are not trusted, they can still accept transactions generated by others. The answer to this question is simple:

Because nodes are not trusting each other, they trust themselves. Nodes only believe in their own history and only make judgments based on their own historical records. Then the question comes: Is your history record right or wrong? (The right or wrong here is a relative meaning, it should be said that it is consistent with most people)

This issue involves the highest principle of bitcoin, and only recognizes that the longest chain of work is the only recognized bitcoin chain.

BTC Script

So in my opinion, Bitcoin’s Script is actually:
scriptPubKey is a “problem” from the previous transaction (out)
And scriptSig is the money I want to use in the previous transaction, then I will provide my “answer” to the question you asked.
Because of the public-private key relationship, if the problem raised by scriptPubKey is a public key-related problem, it is obvious that only the person holding the private key can answer the question, so it is simplified to the “lock” and “key” just mentioned.

On the other hand, how to confirm the “answer” provided is to answer the “problem”? This means that the script needs to be verified, and the verification process only needs the scriptSig provided by txin and the scriptPubKey of the txout that the verifier himself finds from his own records. This verifier is the majority of miners.

Script is an extremely powerful place in the Bitcoin system. It is really a wave of smart contracts after this mode of operation has started. It extends the process of simply authenticating a transaction’s attribution problem from simple authentication to running scripts. Roughly speaking, it complicates a simple thing, and in fact greatly expands the meaning of “transaction”. Make transactions can contain “logic”, not just “state” Nakamoto has redefined the “transaction” by the pioneering evolution of the “transaction process” into a “question-answer” process.

Although the private key and wallet address of the bitcoin wallet are from ECDSA
The private key and the public key, however, the wallet address is not directly the public key, but the hash of the public key. Therefore, if you pay for a wallet, you don’t need the public key of the wallet; only when the wallet uses the money inside (to give money to others), you need to put your public key in the transaction. If a wallet is just collecting money, then it is safe — even if the Shor algorithm requires a public key to reverse the private key. Because the public key is not exposed, the Shor algorithm cannot be used.

Therefore, even if quantum computing breaks the asymmetric encryption algorithm, it can’t be cracked for those cold wallets that have not been used. For those who need multisig, it is similar.

If you have to crack the cold wallet, you need to reverse the wallet address to its public key, and this operation Shor can not be completed, only by other algorithms.

Because of this system, we can find that a transaction in Bitcoin is actually TxIn in the transaction. A transaction can be generated by different people to generate their own TxIn and then combine the transaction, which makes multi-party participation possible. Instead of a transaction, only two participants are input and output.

Priority

Priority = transaction amount × queue time ÷ transaction size
The transaction amount is the primary factor affecting the priority: the larger the amount, the higher the priority of the transaction – let the leader eat first. Trading gold

The accurate queuing time is the block age: the wallet initiates a transaction and immediately spreads to the entire network.
Broadcast, the block age when the miner received is 0. If the 100 blocks are still not recorded, then the block of the transaction

The transaction information size refers to the number of bytes in which the transaction information is recorded, and the unit of the byte is byte. For example, the transaction has only one
Pens come in and out, the size is about 250bytes, while some transactions have multiple input and output transaction information size is relatively large

First, pick out the top 2,000 transactions with the highest priority, but the total size of these transactions cannot exceed
1M.
Second, find a random number so that there is enough “0” before the hash of the “block + random number”.

If a simple transaction (250bytes) is 1 bitcoin (100 million Cong), wait 1 day (about 144 blocks)
It has not been recorded yet, and its priority is:
100,000,000 × 144 ÷ 250 = 57,600,000
With this number in mind, a transaction with a priority greater than 57,600,000 is called a “high priority transaction.” Even if no tip is given, the miner will store it in the first 50K bytes of the block – then your transaction is equivalent to Leaders can enter small rooms and enjoy special services.

Coinbase

The miners packed 2,288 transactions and found the random number (Nonce) 101544420, which was approved by the entire network miners.
So I got Transaction Fees about 8.6 coins and Block Reward.
There are 12.5 coins.
The system will put 21.1 coins into the address of the miner once, we are used to calling it Coinbase.
Coinbase is not a transaction because it has no input (see the left side of the figure: No Inputs).
Coinbase is a field structure, only those who dig out this block can fill out
Coinbase includes commissions and burrowing rewards for all transactions within the block

The attacker can take three steps:
step1: spend 3 million US dollars to buy 200 bitcoins normally, and normal packing and accounting.
Step 2: Sell these coins in the market and get $3 million in cash.
Step 3: start 51% attack, erase the second step of selling 200 coins, repackage the transaction, calculate the random number.

Transaction in Code

Transaction id

The transaction id is calculated by taking a hash from the contents of the transaction. However, the signatures of the txIds are not included in the transaction hash as the will be added later on to the transaction.
const getTransactionId=(transaction:Transaction):string=>{
   const txInContent:string=transaction.txIns
       .map((txIn:TxIn)=>txIn.txOutId+txIn.txOutIndex)
       .reduce((a, b)=>a+b,'');

   const txOutContent:string=transaction.txOuts
       .map((txOut:TxOut)=>txOut.address+txOut.amount)
       .reduce((a, b)=>a+b,'');

   returnCryptoJS.SHA256(txInContent+txOutContent).toString();
};

Transaction signatures

It is important that the contents of the transaction cannot be altered, after it has been signed. As the transactions are public, anyone can access to the transactions, even before they are included in the blockchain.
When signing the transaction inputs, only the txId will be signed. If any of the contents in the transactions is modified, the txId must change, making the transaction and signature invalid.
const signTxIn=(transaction:Transaction, txInIndex:number,
                 privateKey:string, aUnspentTxOuts:UnspentTxOut[]):string=>{
   const txIn:TxIn=transaction.txIns[txInIndex];
   const dataToSign = transaction.id;
   const referencedUnspentTxOut:UnspentTxOut=findUnspentTxOut(txIn.txOutId, txIn.txOutIndex, aUnspentTxOuts);
   const referencedAddress=referencedUnspentTxOut.address;
   const key=ec.keyFromPrivate(privateKey,'hex');
   const signature:string=toHexString(key.sign(dataToSign).toDER());
   return signature;
};

Coinbase transaction

Transaction inputs must always refer to unspent transaction outputs, but from where does the initial coins come in to the blockchain? To solve this, a special type of transaction is introduced: coinbase transaction
The coinbase transaction contains only an output, but no inputs. This means that a coinbase transaction adds new coins to circulation.
const COINBASE_AMOUNT:number=50;
The coinbase transaction is always the first transaction in the block and it is included by the miner of the block. The coinbase reward acts as an incentive for the miners: if you find the block, you are able to collect 50 coins.
We will add the block height to input of the coinbase transaction. This is to ensure that each coinbase transaction has a unique txId. Without this rule, for instance, a coinbase transaction stating “give 50 coins to address 0xabc” would always have the same txId.
const validateCoinbaseTx=(transaction:Transaction, blockIndex:number):boolean=>{
   if (getTransactionId(transaction) !== transaction.id) {
       console.log('invalid coinbase tx id: ' + transaction.id);
       return false;
   }

   if(transaction.txIns.length!==1) {
       console.log('one txIn must be specified in the coinbase transaction');
       return;

   }

   if(transaction.txIns[0].txOutIndex!==blockIndex) {
       console.log('the txIn index in coinbase tx must be the block height');
       return false;
   }

   if(transaction.txOuts.length!==1) {
       console.log('invalid number of txOuts in coinbase transaction');
       return false;
   }

   if(transaction.txOuts[0].amount!=COINBASE_AMOUNT) {
       console.log('invalid coinbase amount in coinbase transaction');
       return false;
   }

   return true;
};


Reference

  1. https://lhartikk.github.io/jekyll/update/2017/07/12/chapter3.html
  2. https://zhuanlan.zhihu.com/c_74315572
  3. https://www.zhihu.com/people/jin-xiao-94-7/activities