Design and Implementation of Traceability System Based on Blockchain

: The fifth Plenary Session of the 19th CPC Central Committee included poverty alleviation and rural revitalization as the main goals of economic and social development during the 14th Five-Year Plan period. Moreover, it has created a new situation of digital sales of poverty alleviation and helps agricultural products such as "agriculture + e-commerce" and "agriculture + live streaming", and the traceability of agricultural products has attracted much attention. Com14bining the current social environment background and the investigation and understanding of the existing agricultural product sales mode of poverty alleviation, we found that the existing sales model chain and the traditional traceability system model have many shortcomings, such as unclear information sources and easy tampering. Based on this design, the idea of constructing a traceability system based on blockchain is proposed, and the required technical and economic feasibility is analyzed to confirm the realization of the idea. It is intended to meet the needs of agricultural product information management, poverty alleviation farmer information management, product traceability, etc. And through the web page and mobile terminal information interaction, the realization of agricultural products at any time, mobile, portable query. The final system test can ensure the normal operation of functions and the security, credibility, and transparency of information, provide new ideas and solutions for the field of targeted poverty alleviation, and help rural revitalization.


Foreword
With the steady development of society, people's awareness of the safety of agricultural products is also increasing. However, the complex procedures involved in the agricultural supply chain of agricultural products make it difficult for consumers to intuitively judge whether there are safety problems in the poverty alleviation of agricultural products, let alone determine which link of the problems appears. In order to protect people's health, and improve public confidence in agricultural products, using blockchain technology to establish safe and credible agricultural products traceability system for poverty alleviation, which improves the information transparency of agricultural products in the supply chain, effectively solves the current poverty alleviation agricultural traceability problems, and meet agricultural information management, farmers information management, product traceability, and other needs. The technical characteristics of blockchain give it a natural advantage in the field of traceability. From the perspective of decentralization: participants can automatically execute transactions through smart contracts and need to reach a consensus from multiple parties, transaction autonomy, and a High degree of decentralization; In terms of tamper-proof information: all transaction information is stored in the block, Multiple participating nodes need to confirm together and each block has a copy of the transaction information. In terms of data security: the data is processed using asymmetric encryption technology, and only with the corresponding private key can you decrypt, High data security; In terms of regulatory accountability: all the information on the blockchain can be found, When a product has a quality problem, The regulatory department can locate the responsible person in time, Precise accountability. Therefore, the application of blockchain to the traceability of agricultural products is helpful to establish a traceability system of agricultural products that can be traced, so as to build a solid bottom line for agricultural products safety.

Blockchain-related overview
As an emerging distributed database technology, the transactions occurring on the chain will produce a time stamp. It records the time at which the transaction took place, and the hash values generated by a certain hash algorithm are concatenated with the previous and subsequent blocks so that the transaction can be traced.
Blockchain technology is a new paradigm that is different from traditional network architectures. The unique decentralized mode of blockchain can avoid the shortcomings brought by the centralized network, and build a trusted autonomous operation network mode [1][2]. By type, blockchains can be divided into public, federated (industrial), and private blockchains. Public blockchain (public blockchains) refers to the consensus process in which any user or group in the world can send a transaction, and the transaction can be confirmed by the blockchain, and anyone can participate in the transaction. Joint blockchain (consortium blockchains) refers to the internal designation of multiple nodes as bookkeeping nodes, the generation of each block is determined by all the preset nodes, other nodes can participate in the transaction, but have no bookkeeping authority, anyone can query through the blockchain [3]. Private blockchain (private blockchains) refers to using the bookkeeping technology of the blockchain only, either as an enterprise, a company, or an individual, with the written permission of the private blockchain.
The characteristics of blockchain also include decentralization, openness, independence, security, and anonymity. (1) Decentralization of power. Blockchain does not rely on a third manager or hardware, nor does it have a central server. Each node implements self-verification of data, transmission, and management through distributed computing and storage. (2) Open-minded. Blockchain technology is open source. In addition to the private information kept confidential, the data within the blockchain is open to everyone, and everyone can enter the blockchain through an open interface for query and development [4]. (3) Security. As long as 51% of all the nodes are not mastered, the data can not be casually tampered with, which makes the blockchain avoid artificial malicious tampering [4]. (4) Anonymous nature. The identity information of any node does not need to be disclosed and verified, and the data transfer can be sent anonymously [5]. (5) Traceability. The information is uploaded to each block in the blockchain. Each block has the hash value of the previous block. Only by identifying the correct hash block can you get the chain connected ( Figure 1), which ensures the traceability of the informationuse.

Blockchain Development Platform
Currently, blockchain technology has been applied in various fields and many decentralized applications and blockchain platforms have emerged, but at present, there are mainly three mature blockchain platforms: Bitcoin, Ethereum, and Hyperledger Fabric. Scale, activity, stability, and difficulty of node deployment should be considered as much as possible. In order to avoid the occurrence of platform software vulnerabilities, and also to avoid technical problems in the development process of the blockchain platform, you can communicate with other developers to solve the problems. Ethereum is a blockchain development platform with programming capabilities that enable any business logic through smart contracts. It currently supports a variety of programming languages, but its official promotion is the Turing complete Solidity programming language, which can be better run in EVM (Ethereum Virtual Machine). At the same time, the Gas mechanism is used to charge, to effectively reduce the generation of useless transactions, or to prevent some malicious and continuous transfer of extremely minor accounts resulting in network paralysis. In addition, Ethereum has a large number of users and developers and has open source code and rich platform interfaces.

Traceability of traditional agricultural products
References The agricultural products traceability system refers to the system that tracks agricultural products (including food, means of production, etc.) into each stage of the market (from the whole process of production to circulation), and involves multiple links such as the origin, processing, transportation, wholesale and sales of agricultural products, which is conducive to quality control and product recall when necessary. The adoption of the agricultural products traceability system can realize the traceability of the product source to the processing and circulation process, ensure that the end users can buy the assured products, and prevent fake and shoddy agricultural products from entering the market [4] .
Throughout the agricultural supply chain, each level of participation can be classified as producer, processor, carrier, regional seller, and retailer. At present, the mainstream agricultural product traceability system is mainly doublelayer traceability combining internal traceability and external traceability ( Figure 2). This traditional way of traceability has exposed a variety of problems, such as (1) information is easy to be tampered with, lost, regional agricultural information sharing is difficult, low data transmission efficiency, the first level of agricultural products (crops) traceability difficulty; (2) complex system, regulatory information overlap or contradiction, regulatory pressure; (3) users at all levels are difficult to trust the authenticity of the data [6].

The Origin of agricultural product tide source system
The earliest agricultural product traceability system originated in Europe in the 1970s, when a severe mad cow disease began in Europe, and European countries began to trace the sources of livestock agricultural products such as cattle and mutton [7]. After the re-emergence of BSE in 1997, the EU set up a traceability system for cattle and beef products, with ears tagged and animal passports issued, allowing consumers to trace the entire process from breeding to slaughter to processing and marketing. At present, the EU has established a traceability system for livestock and poultry animals and their meat products, transgenic products, and feed [8].
Japan began to promote the "food traceability system" in 2001. Most supermarkets have installed product traceability terminals, which basically realize the traceability of the food retail business. In 2003, Japan began tracking beef products; in 2005, Japan Agricultural Cooperation (CA) required that the agricultural products listed through the CA must be traced [9][10]; in 2008, Japan announced plans for a traceability system for rice [11].
In the early 2000s, the United States began to build a traceability system from the level of national strategy and established the American National Animal Identification System (NAIS). Australia has also built a National Livestock Certification System (NLIS), which issues a unique "ID card" to each cow through RFID technology to realize the whole process of data collection of breeding. india has established GrapeNet, a traceability platform, to ensure the safety and quality of exported grapes.
China has been late to the party on the traceability of agricultural products, but it has also been making progress. In 2004, the former Ministry of Agriculture took the lead in starting the pilot traceability system in Beijing and Hebei and implementing the "pilot project of vegetable product quality traceability system in Beijing" [12]. In 2008, the state proposed establishing and improving the labeling of agricultural products and establishing and implementing traceability of agricultural product safety systems. In addition, the national vegetable quality and safety traceability system and other traceability systems are also under construction and piloted. In 2017, the National Agricultural product quality and safety traceability management information platform was officially launched, first launched in Sichuan, Guangdong, Shandong, and other places to pilot [13].

The Application of Blockchain in Agricultural Product Traceability
The quality and safety of agricultural products is the fundamental guarantee of people's healthy life. The traceability system of the agricultural products supply chain is an important means to guarantee the quality of agricultural products. The traditional agricultural supply chain traceability system is highly dependent on the centralized server. Once the server operation fails, the stored data will be lost. In addition, in the process of agricultural product industry chain circulation, data sharing is not timely, which easily leads to the updating speed of traceability information can not keep up with the speed of data query. As a result, traditional agricultural supply chain traceability systems need to seek new technological solutions to address the aforementioned issues.
Because of its own characteristics, blockchain has great advantages over traditional traceability methods (such as Table 1). Compared with the traditional traceability method, the data collection work at the front end of the traceability method based on blockchain technology is not very different from the traditional method, and the huge difference lies in the back end. Blockchain technology can provide new traceability tools that make it easy for consumers to query agricultural product safety information. In addition, the blockchain has the characteristics of decentralization, so that each node can share data in real-time through the chain, and all nodes must also fulfill the obligation of jointly maintaining data reliability [21][22][23][24]. Table 1. Differences between blockchain and traditional ways Therefore, in view of the problems existing in the current poverty alleviation system, such as excessive power, lack of mutual trust, and insufficient sales of agricultural products, this paper proposes a traceability system for poverty alleviation agricultural products based on blockchain technology. In order to solve the storage problem of large text data, this paper proposes a way to combine blockchain with the interstellar Archival System (IPFS) to realize the retrieval and storage of large text data.
Through the construction of a traceability system for poverty alleviation agricultural products based on blockchain technology, the poverty problem in the current povertystricken areas is analyzed and the overall plan is implemented. In this article, we address the need for a blockchain-based agri-product traceability system for poverty alleviation from the point of view of system account management, poverty alleviation management and agri-product traceability. Based on this approach, a traceability system for poverty alleviation agricultural products based on blockchain technology ensures decentralized storage, anti-counterfeiting, and traceability of data, thus effectively improving the security of system data and providing a new idea and approach for the implementation of targeted poverty alleviation efforts.

System design ideas
The system adopts the construction mode of front-rear-end separation and back-end three-layer architecture. The Web main interface sets multiple functional modules such as agricultural products management, poverty alleviation farmer management, file management, and traceability coding query to realize accurate data query and management, and realizes the portable query of small programs through data interaction with the client. The back end realizes on-chain data management, file upload management, data interaction, and other functions through the three-tier architecture mode of blockchain technology, IPFS, and MySQL, as shown in Figure 3.

System technical characteristics
The system is mainly supported by Ethereum, smart contracts, database technology, and other technologies.

Ethereum Technology
As a mature and stable blockchain platform with active updates and development and rich SDK interfaces, Ethereum is one of the most important components of the system.
By creating genesis. The JSON file, the blockchain on which the system depends, is set as a private chain, so as to distinguish it from the main chain on the public network and enhance the stability and maintainability of the system. Using the official Ethereum Lightweight SDKweb3.js, you can call the exposed RPC interface to communicate with the local Ethereum nodes and add other nodes to the supply chain. After the successful addition, the nodes will participate in the consensus mechanism of the blockchain system. The transaction information and mining information of each node on the blockchain will be synchronized to all other nodes, and the traceability query can be carried out at any node, which is also the basis for the system to realize multi-node synchronous traceability.
The system realizes the interaction with the Ethereum blockchain platform through the SDK interface, as shown in Figure 4.

Smart contract writing
A Smart contract is the source code written in the Solidity language. A contract means a set of code (its functions) and data (its status), located on a specific address of the Ethereum blockchain. With smart contracts, we can do the functions on Ethereum without third-party involvement. For example, transfers or contracts between users are automatically executed when certain conditions are not satisfied by the actions of the users.
Using Solidity's online compiler Remix, pre-written smart contracts can be compiled with ABI, files, and bin files. ABI files expose all the construction parameters of the contract and require Java classes based on ABI files.
web3j provides the packaging of Solidity smart contracts, which can directly interact with all smart contracts through the resulting packaging objects. After executing commands from the command-line tool provided on GitHub via web3j, the tool is under the specified file path.

Database technology
Use the relational database MySql to provide the storage of basic information, maintain various kinds of data generated during the management and operation of the system, and provide data support for traceability query.

System function design
According to the functional requirements of the system, the main modules of the system are the agricultural product management module, document management module, poverty alleviation farmers management module, traceability code data tracking, query module, etc. The functions of the main modules are introduced below.

Agricultural Products Management Module
Agricultural product management is an important module of the system. According to the database design, agricultural products are dependent on most of the data tables of the system, and the data on the blockchain is the relevant information from the agricultural products. Therefore, agricultural product management is the key and difficult point of the agricultural products traceability system for poverty alleviation. The poverty alleviation agricultural products traceability system uses Web3J to operate Ethereum smart contracts. Web3J enables Java to operate our deployed Ethereum private chain and call the smart contract deployed above in the form of RPC communication. After the administrator links the information related to agricultural products, he returns the block hash corresponding to the agricultural product information, and then the system will generate the corresponding traceability QR code and traceability code of the agricultural product according to the hash (see Figure 6).

File management module
The File management module provides the upload files, modify file information and delete file information function, upload files and add agricultural products generated traceability QR code will be saved in IPFS. IPFS is a peerdistributed file system and has a permanent, decentralized save and sharing files, point-to-point super media, and content addressing features, every time upload a file, it will generate a unique hash. The corresponding files can be viewed and downloaded on the IPFS through the hash. The traceable twodimensional code is stored on the IPFS, ensuring that the poverty-alleviation agricultural products are "one thing, one code, anti-counterfeiting, and true authentication" (as shown in Figure 7).

Management module for poverty alleviation farmers
The peasant household module registers the information of the poor farmers so that relevant information about the farmers can be obtained when tracing the source of the agricultural products. Whether the farmers meet the standard of poverty alleviation needs the administrator to evaluate according to their materials and documents and upload the peasant household information. Administrators can also distribute funds to poor households, which will be distributed to the farmers' Ethereum accounts. Figure 8 fund distribution transaction, Figure 9 Ethereum account balance for poor households.

Traceability module
The traceability module mainly includes two aspects, namely the generation of traceability code and traceability QR code. Traceability coding adopts the idea of 8-bit UUID, which refers to the generation method of a microblog short domain name. However, the repetition probability is too high, and four codes are generated each time, and one code needs to be randomly selected. The method uses 62 printable characters to randomly produce a 32-bit UUID. The UUID is a 16-decimal number, which is divided into 8 groups of 4 each, and the number of repetitions is greatly reduced by using module 62 to remove characters as indices.
The trace QR codes were generated using the ZXing API, the implementation principle of this QR code is relatively simple, according to the height and width of the QR code, and then according to the algorithm in the ZXing, divide it into one pixel, and fill them with black and white (see Figure 10). The traceability coding query module obtains the associated block Hash through the traceability coding, and through the block Hash obtains the agricultural products information stored on the blockchain, so as to achieve the function of the traceability of agricultural products (as shown in Figure 11). The page queries the corresponding hash by entering a traceability code and then queries the information stored in the blockchain by entering the hash. According to the information displayed, the goods can be traced and anticounterfeiting. Enter the incorrect traceability code to give the incorrect response prompt pass through

Test of agricultural product management module
This module contains a list of poverty alleviation products and new poverty alleviation products. This page can view detailed information on poverty alleviation products, such as a breeding address, processing plants, etc. The block hash, traceability QR code, and traceability code of each poverty alleviation product are displayed on this page. When the administrator adds a poverty alleviation product, the system will chain the information of the poverty alleviation product, and generate the traceability QR code and traceability code.

Test of the management module of farmers and processing plants
The farmer management module includes the management of poverty alleviation farmers, the display of detailed information, and the uploading of related poverty alleviation information of farmers. Farmers' information is processed on the chain when added, and farmers' information is stored in the blockchain. Relevant poverty alleviation materials are uploaded and stored in the IPFS. The processing factory module is the management module of the processing factory in the traceability information of poverty alleviation products.

Document management module test
This module is the centralized management of the documents required by the whole system, such as the QR code of poverty alleviation products, block hash, farmer poverty alleviation materials, etc. The module also records the system's operation log and related files uploaded to the IPFS.

Test results
According to the test requirements, the relevant test cases are designed, and the problems in the system are analyzed and tested. First, from the perspective of function, the functions of each module are tested in detail, and specific test cases are given. Next, test the user interface. Through testing, after many attempts to modify, and finally, to make the system achieve the best efficiency.
The test results confirm that the system basically meets the original design requirements, and the software has full functionality and a good user interface. The interface basically realizes multiple functional modules, including traceability coding and query module, poverty alleviation product management module, file management module, peasant household management module, and so on. The system realizes the registration and management of the name, type, planting (breeding) address, and other information about agricultural products, and the function of adding and deleting the types of agricultural products. It has realized the information registration management of poverty alleviation farmers and the flow direction management of poverty alleviation funds.

System test
This project by analyzing the current poverty alleviation agricultural products sales chain, fund distribution mechanism, reference to the traditional product traceability system and information management system, found that the existing product traceability system and based on traditional software development technology "centralized" information management system has some personnel can tamper with poverty alleviation data, agricultural information is difficult to verify the root cause, poor fraud, poverty alleviation funds is not transparent, in order to solve the above problems, puts forward the chain based on blocks. Based on blockchain and interstellar file system (IPFS) technology of agricultural products traceability system for demand analysis, mainly including agricultural products management, poverty alleviation, poor information management, system file management four modules, and the overall design of the whole system, user interface design, smart contract design, and the design of the core class. Based on the demand analysis, the development environment required for the poverty alleviation product traceability system, including the operating environment and Ethereum platform, a complete poverty alleviation system has been constructed, tested, and analyzed to ensure the complete operation of various functions. I believe that the future and prospects of this project are also worth looking forward to.