# TDLearning: Trusted Distributed Collaborative Learning Based on Blockchain Smart Contracts
## 3 Method
### Smart Contract Design
#### 3.3.2 Trusted Database Construction  
Trusted database construction is implemented by the DMC, DRC and DEC. The DRC creates an inverted index for on-chain data to support the keyword retrieval of data. The on-chain data retrieval returns the latest status of the data in real time, enhancing the data availability of the collaboration mechanism, which therefore eliminates the need to maintain an off-chain synchronised database. The data-assessment contract introduces a fuzzy comprehensive evaluation mechanism to evaluate the quality of the data involved in the collaboration. This evaluation process is automatically completed by the smart contract, and the relevant data-quality evaluation is updated at the end of each collaborative task. The DMC implements data registration on the chain. Its core function is to maintain the list of available data and to build a high-value trusted database by calling the DRC and DEC. The core steps are described below:  
(a) Data registration. _Tp_ calls the dataRegister() to register data information with the blockchain. \(D_{hash}\) is a unique identifier of the data in the data list, which is bound to the rest of the data information by keyword mapping.  
(b) Data indexing. The blockchain continuously listens for the data-registration event _\"event DataReg(\(D_{hash}\))\"_, and the DMC uses this to determine whether the corresponding data have been registered. When a collaborating node completes the registration of new data, the smart contract creates an inverted index for it. The detailed steps for index building are shown in Algorithm 2, which consists of two steps: data summary preprocessing and building the inverted index.  
```
1.dataRegister(0): ifmsg.sender has audited and these data are not registered then  Update database: DataSet[\(D_{hash}\)] = dataDescription (\(D_{name}\), \(D_{summary}\), \(D_{hash}\), \(D_{status}\)); else throws; endif
2.createDataIndex(0): when the blockchain has listened for data-registration event: lexicon[] = processString (\(D_{summary}\)); for all each word \(i\) in lexicon[] do if0 == bytes (lexicon[i]) or stopWords[lexicon[i]] then  continue; else  word = stem (lexicon[i]); if0 == invertedIndex[word]. termFrequency (\(D_{hash}\)) then  invertedIndex[word]. hashList.push (\(D_{hash}\));  invertedIndex[word]. termFrequency (\(D_{hash}\)) ++; else  invertedIndex[word]. termFrequency (\(D_{hash}\)) ++; endif endif endfor
```  
**Algorithm 2** Data registration and retrieval (DMC and DRC)  
Data-summary preprocessing. When the blockchain listens for a data-registration event, the DRC calls progressString() and stem() to traverse the Dsummary and complete the preprocessing. The preprocessing includes special character substitution, word separation and stemming, and the DRC builds the dictionary through these steps.
* Building the inverted index. The smart contract iterates through the dictionary and updates the inverted list _PostingList_ with the inverted index table _invertedIndex_ when the dictionary array _lexicon[i]_ is not empty and not for retrieving stop words. The inverted list stores a list of data indexes as well as word frequencies. The inverted index table is in the form of key-value pairs, where the key is the word and the value is the inverted list corresponding to the word. Through the above steps, the smart contract establishes a keyword dictionary and an inverted index table corresponding to each of these words. In addition, due to the peculiarities of the permanent storage of blockchain data, the data-retrieval contract designs a list of stop words. When you want data related to a keyword to no longer be searchable, add the keyword to the table.