Blockchain and blockchain applications in decentralized financial systems

Author: Zen and the Art of Computer Programming

1 Introduction

In "The "Ice and Fire" Effect of A-share Investment - The "Unpopular Business" of Internet Big Guys", we mentioned that before the end of 2017, the market's participation in private equity funds, futures companies, precious metal exchanges and other markets The rate is not high. But by the beginning of 2018, with the emergence of various investment platforms and the influx of hot money, the capital market began to compete, and even more fierce competition was set off. This has to make people wonder, is our brain bought by money or is our thinking controlled by money? If it is the latter, then Blockchain Technology will become an important leader in the future, because it is one of the only remaining technologies that can truly solve this problem.
Blockchain technology has gradually entered the industry and has broad and far-reaching influence. Blockchain technology based on Distributed Ledger Technology (DLT) has been proven to effectively manage complex business data transfer processes, thereby ensuring the security of user information, non-tampering of data, and achieving trust and hygiene, among other features. As an independent distributed system, blockchain has strong security and privacy protection capabilities in terms of fault tolerance, high performance, low latency, and automation. Therefore, in many scenarios, blockchain technology will undoubtedly become an important means of future financial services.
The emergence of virtual digital currencies such as Bitcoin has also brought new opportunities to it. It is both a payment tool and a value storage medium, with huge commercial value. In addition, it is also the first substantial blockchain application case and is evolving towards a truly decentralized financial system. As long as the underlying protocol and infrastructure of Bitcoin are copied to other decentralized financial systems, these systems will also become more secure, transparent, fast and easy to use.

2. Explanation of basic concepts and terms

2.1 DLT (Distributed Ledger Technology)

DLT (Distributed Ledger Technology) refers to a database built using distributed network technology, which can record and verify the transaction behavior of all participants, ensure reliable dissemination and comprehensive consistency of information, and prevent double payments. , counterfeit transactions, counterfeiting, etc. Currently, mainstream distributed ledger technologies include Bitcoin, Ethereum, Hyperledger Fabric, etc.

2.2 Consensus Algorithm

Consensus Algorithm, or consensus algorithm, is an algorithm used in distributed systems to maintain data synchronization among multiple nodes and reach consensus. Commonly used consensus algorithms include Paxos, Raft, Zab, PBFT, etc. In the blockchain, we usually use the Proof of Work (PoW) algorithm to build consensus, that is, using computing resources (CPU, GPU, etc.) to complete the workload proof (Work). This algorithm requires miners to perform a large number of repeated calculations, making the entire blockchain network difficult to attack.

2.3 Distributed ledger

Distributed Ledger Technology (DLT), also known as distributed database or block chain technology, is a technology that records and verifies transaction activities through the network. It is characterized by a database built using distributed network technology that can record The transaction behavior of all participants can ensure reliable dissemination and comprehensive consistency of information, and can prevent double payment, forged transactions, counterfeiting, etc. Currently, mainstream distributed ledger technologies include Bitcoin, Ethereum, Hyperledger Fabric, etc.

2.4 Blockchain

Blockchain refers to a distributed database established through encryption technology. It is used to record and verify the global transmission of a series of data. It is a non-custodial, decentralized distributed accounting technology. Through the non-tampering, non-forgery, traceability and other characteristics of the transaction data encryption, chaining, storage, verification and other processes, any participant can verify and confirm the transaction history data, making the entire transaction process transparent, efficient, safe and reliable. . Blockchain has the characteristics of non-evil, consensus mechanism, fast transactions, anonymity, and high concurrency processing.

2.5 Blockchain application cases

2.5.1 Bitcoin

Bitcoins, a cryptocurrency, were first created by Satoshi Nakamoto in 2009. It is a peer-to-peer electronic currency that does not rely on any country or legal entity and is completely controlled and managed by individuals. It has the characteristics of high efficiency, high transparency, and complete openness, and can also be used for payment, storage, instant consumption, etc. Bitcoin mainly uses the Proof of Work (PoW) algorithm to establish its decentralized public ledger. A large-scale distributed network jointly maintains this ledger and maintains that each block can only have one unique Proof of work is required to join the chain. Its advantage is that it can achieve instant transactions without worrying about currency supply, and it is highly secure.

2.5.2 Ethereum

Ethereum is an open source, lightweight blockchain that supports the smart contract programming language Solidity. It is characterized by its sharding architecture, which can support ultra-large-scale distributed transaction processing. At the same time, the low transaction fees and flexible smart contract functions provided by Ethereum have also attracted more and more people to invest in its research and applications. Ethereum will become the next generation distributed application development platform and drive the prosperity of blockchain.

2.5.3 Ripple

Ripple is a digital currency issued based on XRP, a global cryptocurrency. Its unique sharding architecture design can support extremely high transaction processing speeds. Another feature is instant confirmation of transactions, ensuring fast and accurate transactions, but it currently does not have Solidity, a programming language that implements smart contracts like Ethereum. However, Ripple’s current community support and acceptance are relatively good, and its market value is slowly rising.

2.5.4 Cardano

Cardano is an open source blockchain project that uses a POS+POW hybrid consensus mechanism to provide efficient and low-cost transaction confirmation while supporting smart contracts. Thanks to its commissioned production approach, Cardano can also split its network into multiple sub-networks through sharding technology. This project will change the current landscape of financial services.

3. Core algorithm principles and specific operation steps

3.1 Proof of Work algorithm

The PoW algorithm, the proof-of-work algorithm, is a type of blockchain consensus algorithm that requires miners to perform a large number of repeated calculations to verify whether the work has been verified through certain conditions. Under normal circumstances, the PoW algorithm will limit the size of the generated blocks, the time interval between block generation, etc., to ensure the timeliness of block generation. In the blockchain system, the PoW algorithm can ensure the consistency, security and integrity of each node's data. The specific operation steps are as follows:

  1. The miner selects a nonce value and performs a hash operation on the previous block hash, current transaction record, random number nonce and other information together to obtain the result.
  2. If the result ends with a certain number, that number represents the target value, that is, the transactions in the block comply with the rule and need to continue adding proof of work.
  3. This result is compared with the hash value in the previous block. If it is different, the generated block will be packaged into the blockchain.
  4. When a block is generated, all transactions on it will be packaged on the chain.
  5. The new block can accept new transactions, and the new block will contain all the original transaction information, and there will be no duplicate records.
  6. Ordinary users can query all transaction information on the chain and conduct self-verification based on the transaction information.

3.2 Proof of Stake algorithm

The PoS algorithm, or Proof of Stake algorithm, is another blockchain consensus algorithm. Compared with the PoW algorithm, the PoS algorithm allocates the rights and interests in the blockchain network to validators instead of rewarding their computing power. This approach eliminates the centralization and controllability of the PoW algorithm, improves the decentralization level of the blockchain, and enhances its flexibility, autonomy, and scalability. Under the PoS algorithm, validators only need to hold a certain amount of their cryptocurrency to participate in the maintenance and operation of the blockchain network. Under this model, the network will always remain online as long as enough validators participate in the system. Under the PoS algorithm, the frequency of block generation is determined by the time interval rather than changes in the target value. Therefore, there will be no blocking during the block generation process. The specific steps are as follows:

  1. Users submit transaction requests to the blockchain network, using the amount of cryptocurrency as equity.
  2. The network selects a certain number of validators and distributes their rights and interests according to a certain proportion (for example, 1/3).
  3. The user sends the transaction he wants to make to the blockchain network, and the network verifies the transaction record. If the verification passes, the transaction record is packaged into a block.
  4. Each validator will be assigned a fixed period (for example, 10 seconds) and collect blocks during this period to confirm the transaction information in the block. If the confirmed block is longer than the previous block, it is upgraded to an on-chain block.
  5. Users can query all transaction information on the chain and conduct self-verification based on the transaction information.

3.3 Merkle Tree algorithm

Merkle Tree algorithm, also known as Merkle tree algorithm, is an important blockchain data structure. Merkle Tree is a binary tree used to represent a set of hash values ​​of a set of elements. Each node corresponds to a member of the set. The value at the root node represents the overall hash value of the collection. In the blockchain system, Merkle Tree can be used to ensure data integrity, verify data correlation, and is fast. The specific operation steps are as follows:

  1. The hash function of a data set converts a collection of all data items into a single value that represents the hash value of the set of data.
  2. According to the order of hash values ​​of data items, a hash tree is formed.
  3. Traverse the hash tree downwards starting from the root node. Whenever a leaf node is encountered, the value of the leaf node is taken and the hash value of the parent node is obtained, until the root node.
  4. The final result is the overall hash value of this set of data.
  5. Through these two steps, the integrity of the data and the correlation of the data can be verified.

3.4 Programmable smart contracts

Smart Contracts are an important part of distributed accounting technology. Their function is to deploy a series of contract codes in the blockchain system, compile them into bytecodes, and put them into the blockchain network for execution. . Smart contracts can realize various application scenarios such as asset exchange, lending, and game gambling. In a blockchain system, smart contracts can help achieve the following functions:

  1. Issuing digital currency
  2. Execute various business logic
  3. Manage digital assets
  4. Provide signature verification
  5. Support multiple programming languages
  6. Build complex financial derivatives trading systems

3.5 Tokens and Tokens

Token is a standardized term used to identify digital assets on the blockchain. Tokens and tokens are interchangeable digital tokens, and their main purpose is to facilitate the exchange of digital assets between different blockchains. Tokens and tokens play an important role in distributed accounting technology. The specific operation steps are as follows:

  1. Issue Token
  2. Convert Token to Token
  3. View token information
  4. The issuance, conversion, viewing, and trading of trading
    tokens and tokens are all a series of operations performed on the blockchain, and their purpose is to promote the transaction, flow, and circulation of digital assets.

4. Code examples and explanations

4.1 Python example: writing a simple blockchain

This section shows how to write a simple blockchain using Python and the Flask framework. First, you need to install the relevant libraries:

pip install flask
pip install Flask-Cors
pip install requests

Create app.pya file and write the following code:

from flask import Flask, jsonify, request
from hashlib import sha256
class Block:
  def __init__(self, index, timestamp, data, previous_hash):
      self.index = index
      self.timestamp = timestamp
      self.data = data
      self.previous_hash = previous_hash
      self.hash = self._calc_hash()
      
  def _calc_hash(self):
      block_str = str(self.index) + \
                  str(self.timestamp) + \
                  str(self.data) + \
                  str(self.previous_hash)
      return sha256(block_str.encode()).hexdigest()
      
class BlockChain:
  def __init__(self):
      self.unconfirmed_transactions = []
      self.chain = []
      self.create_genesis_block()
  
  def create_genesis_block(self):
      genesis_block = Block(0, '01/01/2018', "Genesis Block", "0")
      self.chain.append(genesis_block)
      
  def get_last_block(self):
      return self.chain[-1]
      
  def add_new_transaction(self, transaction):
      self.unconfirmed_transactions.append(transaction)
      
  def mine_pending_transactions(self, miner_address):
      last_block = self.get_last_block()
      new_block = Block(len(self.chain),
                        len(self.chain),
                        {
    
    'tx': [t.to_json() for t in
                               self.unconfirmed_transactions]},
                        last_block.hash)
      proof = self.proof_of_work(new_block)
      new_block.hash = new_block._calc_hash()
      reward_transaction = Transaction("Miner Reward", miner_address, None,
                                        len(self.chain)+1)
      self.add_new_transaction(reward_transaction)
      self.chain.append(new_block)
      self.unconfirmed_transactions = []
      return (new_block, proof)
  
  @staticmethod
  def proof_of_work(block):
      nonce = 0
      while True:
          if BlockChain.valid_proof(block, nonce):
              print('Successfully found the proof')
              break
          else:
              nonce += 1
      return nonce
  
  @staticmethod
  def valid_proof(block, nonce):
      guess_str = str(block.index) + \
                  str(block.timestamp) + \
                  str({
    
    'tx': [t.to_json() for t in
                              block.data['tx']]}).replace("'", '"') + \
                  str(block.previous_hash) + \
                  str(nonce)
      guess_hash = sha256(guess_str.encode()).hexdigest()
      return guess_hash[:4] == '0'*4
class Transaction:
  def __init__(self, sender, recipient, amount, time_stamp):
      self.sender = sender
      self.recipient = recipient
      self.amount = amount
      self.time_stamp = time_stamp
  
  def to_json(self):
      return {
    
    
         'sender': self.sender,
         'recipient': self.recipient,
          'amount': self.amount,
          'time_stamp': self.time_stamp
      }
  
app = Flask(__name__)
CORS(app)
blockchain = BlockChain()
@app.route('/mine', methods=['POST'])
def mine():
  miner_address = request.get_json()['miner_address']
  new_block, proof = blockchain.mine_pending_transactions(miner_address)
  response = {
    
    
     'message': "New Block Forged",
      'index': new_block.index,
      'transactions': [{
    
    'sender': tx['sender'],'recipient': tx['recipient'], 'amount': tx['amount']} for tx in
                       new_block.data['tx']],
      'proof': proof,
      'previous_hash': new_block.previous_hash
  }
  return jsonify(response), 200
  
@app.route('/transactions/new', methods=['POST'])
def new_transaction():
  values = request.get_json()
  required_fields = ['sender','recipient', 'amount']
  if not all(k in values for k in required_fields):
      return 'Missing values', 400
  transaction = Transaction(values['sender'], values['recipient'], float(values['amount']),
                             int(datetime.now().strftime('%s')))
  blockchain.add_new_transaction(transaction)
  return "Transaction will be added to Block {}".format(blockchain.get_last_block().index+1), 201
@app.route('/chain', methods=['GET'])
def full_chain():
  chain_data = [b.__dict__ for b in blockchain.chain]
  return jsonify({
    
    'length': len(chain_data)-1, 'chain': chain_data})
if __name__ == '__main__':
  app.run(host='0.0.0.0', port=5000)

The above code defines a Block class to create blocks, a BlockChain class to maintain the blockchain data structure, and a Transaction class to create the transaction data structure. Among them, the BlockChain class implements various blockchain operations, including block generation, mining, adding transaction records, etc.
In the app.py file, three routes are also defined:

  1. /mine, used for mining to generate new blocks;
  2. /transactions/new, used to create new transaction records;
  3. /chain, used to obtain complete information of the blockchain.
    In if __name__ == '__main__':the code snippet, start the Flask server. Run the command:
    flask run --host=0.0.0.0 --port=5000
    At this point, you can open the browser and enter http://localhost:5000. If you see the following output, it means that the blockchain is running normally:
Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: XXX-XXX-XXX

Visit http://localhost:5000/chain with your browser to view the complete information of the blockchain.
The following figure is an example of successfully generating a new block:

4.2 C++ Example: Writing a Simple Blockchain

This section shows how to write a simple blockchain using C++, OpenSSL, and Boost libraries. First, you need to install the relevant libraries:

sudo apt update && sudo apt upgrade -y
sudo apt install build-essential cmake libboost-all-dev openssl libssl-dev wget curl git unzip -y

Download source code:

mkdir ~/src
cd src
git clone https://github.com/DaveGamble/c-blosc
git clone https://github.com/openssl/openssl

Install OpenSSL:

cd openssl
./config no-shared no-tests -fPIC
make depend
make all
sudo make install

Configure and compile Blosc:

cd../c-blosc
mkdir build
cd build
cmake.. -DCMAKE_INSTALL_PREFIX=/usr/local
make
sudo make install

Configure and compile the blockchain:

cd ~
mkdir mychain
cd mychain
touch main.cpp
nano main.cpp // 编辑文件

Write the main.cpp file:

#include <iostream>
#include <sstream>
#include <vector>
#include <ctime>
#include <chrono>
#include <iomanip>
using namespace std;
string SHA256(string input){
  unsigned char hash[SHA256_DIGEST_LENGTH];
   SHA256_CTX ctx;
  SHA256_Init(&ctx);
  SHA256_Update(&ctx,input.c_str(),input.size());
  SHA256_Final(hash,&ctx);
   stringstream ss;
  for(int i = 0; i<SHA256_DIGEST_LENGTH ; ++i)
      ss << hex << setw(2) << setfill('0') << static_cast<unsigned int>(hash[i]);
   return ss.str();
}
struct Block{
  int index;
  chrono::system_clock::time_point timestamp;
  vector<pair<string,string>> transactions;
  string prevHash;
  string hash;
};
class BlockChain{
public:
  void init(){
      genesisBlock = generateGenesisBlock();
      blocks.push_back(genesisBlock);
  }
   void addBlock(const Block& block){
      if(!verifyBlock(block)){
          throw invalid_argument("Invalid block");
      }
       blocks.push_back(block);
  }
   bool verifyBlock(const Block& block){
      if(block.index!= this->getLastIndex()+1 || block.prevHash!= getLastHash()){
          cout<<"Invalid block"<<endl;
          return false;
      }
       if(block.hash!=calculateHash(block)){
          cout<<"Invalid block's hash"<<endl;
          return false;
      }
       return true;
  }
   Block getLastBlock(){
      return blocks.back();
  }
   int getLastIndex(){
      return blocks.empty()?0:blocks.back().index;
  }
   string getLastHash(){
      return blocks.empty()?"":blocks.back().hash;
  }
   string calculateHash(const Block& block){
      ostringstream oss;
      oss<<block.index<<block.timestamp.time_since_epoch().count()
         <<block.transactions<<block.prevHash;
       return SHA256(oss.str());
  }
   Block generateGenesisBlock(){
      Block block;
      block.index = 0;
      block.timestamp = chrono::system_clock::now();
      block.transactions.emplace_back({"Dave","Alice",10});
      block.prevHash = "";
      block.hash = calculateHash(block);
      return block;
  }
   const Block& getGenesisBlock(){
      return genesisBlock;
  }
private:
  vector<Block> blocks;
  Block genesisBlock;
};
void mineBlock(BlockChain& bc, string minerAddress){
  auto now = chrono::system_clock::now();
  auto duration = chrono::duration_cast<chrono::seconds>(now.time_since_epoch()).count();
  srand((unsigned int)time(nullptr));
  int randNum = rand()%100;
   string dummyData="dummy";
   Block block;
  block.index = bc.getLastIndex()+1;
  block.timestamp = now;
  block.transactions.emplace_back({minerAddress,"Reward",randNum*pow(10,-1)});
  block.transactions.emplace_back({"Dave","Alice",10});
  block.prevHash = bc.getLastHash();
  block.hash = bc.calculateHash(block);
   bc.addBlock(block);
   cout<<"Mined a block: "<<bc.getLastIndex()<<" | Hash:"<<block.hash<<endl;
}
int main() {
  BlockChain bc;
  bc.init();
   mineBlock(bc,"alice");
  mineBlock(bc,"bob");
  mineBlock(bc,"charlie");
   for(auto block : bc.blocks){
      cout<<"------------------------------------------"<<endl;
      cout<<"index:        "<<block.index<<endl;
      cout<<"timestamp:    "<<chrono::system_clock::to_time_t(block.timestamp)<<endl;
      for(auto pair : block.transactions){
          cout<<"    from: "<<pair.first<<"    to: "<<pair.second<<"    amount: "<<block.hash<<endl;
      }
      cout<<"prevHash:    "<<block.prevHash<<endl;
      cout<<"hash:        "<<block.hash<<endl;
  }
   return 0;
}

In the main function, first initialize the blockchain object, then call the mineBlock function to generate three blocks, and finally print all the information of the blockchain.
In the mineBlock function, first obtain the current timestamp, then generate a random number and generate a blank string. After that, create a block, set the block index, timestamp, transaction record, previous block hash, block hash, and then add the block to the blockchain.
In the calculateHash function, ostringstream is used to serialize the block information into a string and return its hash value.
Run the program:

g++ -std=c++11 -lcrypto -lboost_system -o main main.cpp
./main

As you can see, the program outputs the generated block information.

Guess you like

Origin blog.csdn.net/universsky2015/article/details/132114643