Blockchain principle and technology final project experiment report

Github: https://github.com/Alva112358/BlockChain


1. Project Introduction


       The title of the project is a decentralized mobile phone trading platform, which is actually a decentralized online store. The choice of this project is actually a consideration. After all, the blockchain is characterized by a decentralized distributed system and has good data privacy. Therefore, using this feature of the blockchain, it can be realized on the network trading platform. Some good applications.
       Nowadays, online trading platforms such as Taobao and JD.com have a characteristic that they are all centralized trading systems. When trading between customers and stores, a trusted third-party user is required to intermediary to ensure the transaction. reliability. Although the current development prospects of third-party trading platforms are very good and customer satisfaction is also very high, there are still hidden risks after all, and more small platforms have greater risks, so develop a decentralized trading platform , it can increase the reliability of transactions, and it is also the direction of the development of small and medium-sized online trading platforms.
       The actual environment of this project is a mobile trading platform that uses smart contracts for programming. The contract is initiated by the store owner. When the user selects their favorite mobile phone, they can submit the order. At this time, the contract can receive the number of mobile phones and mobile phone types in the customer's order, so that the corresponding ETH will be deducted from the customer to the contract. Internally, rather than trade directly to the store. The money can be deducted from the contract and handed to the store only after the store sends the goods to the logistics company, the logistics company confirms the buyer's delivery, and the buyer finally confirms the receipt of the goods. There is no third-party intermediary involved in the whole process to achieve decentralization.


2. Test environment


       The operating system of the project's test is mac OS, which is implemented using the truffle framework and tested on the private chain using Ganache. Truffle is a popular Ethereum development framework that supports the compilation, deployment and testing of smart contracts. This project belongs to a development and learning project, so it is tested on a private chain. The versions of the corresponding accessories are as follows:

    truffle -v5.0.0
    node -v11.4.0
    npm -v6.4.1     

       Due to the file size limitation of Github, the node_modules folder is deleted when uploading, so execute the following statement in the current directory before running the project:

    npm install

       If some modules are still missing, you need to install them with npm yourself.


1. Configuration and use of Ganache-cli

       The project folder is DApp. The method of operation is to open two terminals in the DApp directory. First, enter the following command on the first terminal to create a private chain on Ganache:

    ganache-cli -e 3001 -l 99999999999999 -g 20000

       It should be noted that if Ganache is not installed, you need to run it in the terminal first:

    sudo npm install -g ganache-cli

       This experiment does not use the graphical interface of Ganache-cli, but only runs it on the terminal. After startup, the displayed interface is shown in the following figure:
1568213-20190118143919964-1517531219.jpg

)
       In order to facilitate the test, the initial ether is set to 3000. At the same time, due to some gas limit problems found during the test, the gas limit is set to a large number. The private key can be used for account deployment in plug-ins such as metamask, which is not used in this experiment. Of course, in the process of using, it may not be able to start due to the lack of some nodejs installation packages in the system, and you can install it with npm if you need it.


2. Configuration and use of truffle

       The first is the installation of truffle. The installation of truffle only needs to enter the following command in the terminal:

    sudo npm install -g truffle

       It should be noted that the installation process of truffle may fail in different systems, which can only be found on the Internet, because the analysis of errors is too complicated.
       Since it is already a complete project, the creation process of the truffle project is omitted here, and only the methods used are shown. First, you need to compile the smart contract and enter the following command:

    truffle compile --all

       After entering the command, you should get something like the following image:
1568213-20190118143952481-1502802550.jpg

       Then you need to deploy the contract to the private chain, you need to enter the following command in the terminal:

    truffle migrate --reset

       After entering the command, you should get something like the following image:
1568213-20190118144009399-96830480.jpg

       At the same time, on the terminal running Ganache-cli, the following information should be output:
1568213-20190118144018082-651842742.jpg

       Finally, run the smart contract and enter the following command:

    npm run dev

       After running the command, you will get the interface as shown below:
1568213-20190118144028362-1504617156.jpg

       It should be noted that the running port of the project in the above figure is http://localhost:8083/. The specific port number needs to access the corresponding port on Google Chrome according to the port number output at runtime, that is, it may be 8080 , 8081, 8082, etc., need special attention.
       At this time, open Google Chrome and enter localhost:8083 to access the project's web page. Note that you need to close the Metamask plug-in of Google Chrome first, otherwise there will be unknown errors. The resulting web page looks like this:
1568213-20190118144037343-819841350.jpg


2. Experimental test


1. Testing of smart contracts

       Preliminary tests of smart contracts can be used to test the functions of smart contracts on Remix. Remix has an initialized account and can perform simple tests on certain functions, which can avoid problems with certain functions when smart contracts are deployed to Ethereum. resulting in the loss of ether. The testing of smart contracts on Remix has been completed in the contract deployment stage, so it will not be repeated here. The Solidity code for the contract looks like this:

pragma solidity ^0.5.0;

contract computerShop {

    // Use to finish the transaction.
    enum States { NOTHING, ORDERING, SENDING }
    // The shop's keeper.
    address public owner;

    // The computers.
    mapping(bytes32 => uint) public computers;

    // State
    States purchase_state;
    // Constructor, open a new computer shop.
    constructor() public {
        // Initialize the computers.
        computers["iPhone"] = 500;
        computers["Sumsung"] = 350;
        computers["HUAWEI"] = 390;

        purchase_state = States.NOTHING;
    }

    // Get specific computer's price.
    function get_price_by_name(bytes32 _name, uint number) public view returns (uint) {
        return computers[_name] * number;
    }

    // Buyers order a computer.
    function ordering(uint money) public payable {
        require (purchase_state == States.NOTHING, "NOT NOTHING");
        require (msg.sender != owner, "NOT BUYER");
        require (msg.sender.balance >= money, "NOT ENOUGH MONEY");

        purchase_state = States.ORDERING;
    }

    // Owner send the computer.
    function sending() public {
        require (purchase_state == States.ORDERING, "NOT ORDERING");
        require (owner == msg.sender, "NOT SELLER");

        purchase_state = States.SENDING;
    }

    // Buyers comfirm.
    function comfirm() public {
        require (purchase_state == States.SENDING, "NOT SENDING");
        require (msg.sender != owner, "NOT BUYER");
        
        purchase_state = States.NOTHING;
    }

    function withdraw() public payable {
        require (msg.sender == owner, "NOT SELLER");
        require (purchase_state == States.NOTHING, "NOT NOTHING");
        msg.sender.transfer(address(this).balance);
    }

    function check_balances() public view returns (uint) {
        require (owner == msg.sender, "NOT OWNER");
        return address(this).balance;
    }

    function get_status() public view returns (uint) {
        if (purchase_state == States.NOTHING) return 1;
        if (purchase_state == States.ORDERING) return 2;
        if (purchase_state == States.SENDING) return 3;
    }

    function getBalance() public view returns(uint) {
        return msg.sender.balance;
    }
    
    function sellerInit(address addr) public {
        owner = addr;
    }
}

2. Test on the web side

       In the test on the web side, the first is the process for users to purchase their favorite products (this project designates the 0th account as the initiator of the contract):


Customers buy their favorite mobile phone

       Switch to customer 1 and buy the corresponding number of iPhones, the result is as follows:
1568213-20190118144116565-455548804.jpg

       At the same time, the store owner's balance did not increase immediately at this time, but first transferred to the contract to freeze, and waited for the customer's confirmation. The purchase signal of the mobile phone store was converted from NOTHING to ORDERING. At this time, the store owner can confirm the purchase information, as shown in the following figure:
1568213-20190118144126443-1699774419.jpg


The owner confirms the delivery

       At this time, only the store owner can confirm the delivery and wait for the customer to confirm the receipt, but the store owner cannot confirm the receipt. If the store owner clicks to confirm the receipt at this time, an error will be reported, as shown in the following figure:
1568213-20190118144137770-734523463.jpg

       After the correct click to confirm the delivery, the sales status of the mobile phone store will change from ORDERING to SENDING, as shown in the following figure:
1568213-20190118144147687-787802398.jpg


Customer confirms receipt

       At this point, the customer can confirm the receipt of the goods. When the customer confirms the receipt of the goods, the money can be transferred from the contract and transferred to the shop owner. The status of the shop changes from SENDING to NOTHING. At this time, the customer can purchase the goods again and confirm the receipt of the goods. , the information displayed after confirmation is as follows:
1568213-20190118144158705-801179522.jpg

       At this point, switch to the store owner's account, and you can find that the store owner's balance has increased by 1000, indicating that the transaction is successful, as shown in the following figure:
1568213-20190118144210154-1811568421.jpg

Prevent overbuying when your balance is low

       If the total price of the phone you want to buy exceeds the money you have, you will get an error, as shown below:
1568213-20190118144221495-1547525803.jpg


3. The expandable part of the project


       Due to the relatively short time of this work, the functions implemented are very simple, and the interface is simply implemented, combined with the last smart contract programming work, and because the learning process takes up a lot of time, some functions are also deleted. The improvements that can be made to this project are as follows:

1. Customer purchase information backtracking

       Customers may purchase multiple items in the same store, and the blockchain itself has the functions of backtracking and information privacy, so it can realize the function of a customer querying purchase information, and a specific customer can only query specific information.


2. The store owner purchases the goods and adds the goods

       Because the time is relatively tight, this function is not implemented. In fact, this function is relatively simple, and it only needs to simply modify the content of the smart contract.


3. Improve the login interface

       The login interface of this project is extremely simple, so it is convenient for testing. In fact, you can use a framework such as Vue to write a simple interactive interface, which may be more friendly. Adding a server and database can become a relatively complete project.


4. Multi-user concurrent purchases

       The function of this project is relatively simple. It can only realize single-user transactions. If other users want to buy mobile phones, they need to queue up. Therefore, concurrent operations can be added in subsequent supplements.


Fourth, the experimental experience


       This experiment is an actual combat of blockchain smart contract programming. Since the previous courses and course content seldom touch the programming of smart contracts, there are many problems encountered during the actual combat. I have only heard of the term blockchain before, but I have never heard of concepts such as Solidity and Ethereum, and there is very little information on the Internet. Basically, they are all introductory tutorials, so it is even more difficult.
       First of all, the most difficult thing is to set up the environment. The first time I set up the environment was the Windows operating system. I encountered many problems during the installation and initialization of truffle. Later, I gave up using Windows. Then I used a Linux virtual machine to conduct experiments, but also kept reporting errors during the truffle installation process, and the errors were difficult to find. I once thought of not using the framework, but thinking that I have learned so much for this framework, I decided to switch to Mac OS. Experimenting, luckily on mac OS the whole installation process was smooth and running smoothly.
       Then there are some programming of the web front-end and web3js. The smart contract was probably written in the last job, so I just made a simple modification to the smart contract, mainly because the process of calling the smart contract function by web3js is laborious, and I learned a lot of things. , I'm doing it and I'm still getting it out. However, the function of this project is very simple, with only simple functions of placing an order, confirming and receiving goods, but there is still a lot of effort involved, and it took 4 days to complete this project.


appendix

Project source code: Github

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324188537&siteId=291194637