# TDLearning: Trusted Distributed Collaborative Learning Based on Blockchain Smart Contracts
## 3 Method
### Smart Contract Design
#### 3.3.4 Collaborative Task Management and Collaboration Incentive Allocation
the message publisher, _to_ is the address of the message recipient, _key_ is the public key of the message recipient and the data are stored by IPFS [30] to reduce the storage overhead of the blockchain:  
\[transmit(from,to,key,IPFS(message)) \tag{7}\]  
```
1. taskRegister(): ifmsg.sender has audited and this task is not registered then  Update database: TaskSet [\(T_{name}\)] = taskDesc (\(T_{name}\), \(T_{summary}\), \(T_{reward}\), \(Tp_{s}\), \(V_{\theta}\), \(T_{secret}\)); TaskSet [\(T_{name}\)].status = initialisation; else return false endif 2. taskPublish0: if\(T_{amount}\geq TP_{s}\).length * \(T_{reward}\) + \(Mv_{s}\).length * \(T_{reward}\) * \(k\)then TaskSet [\(T_{name}\)].status = formal publication; for each verifier \(j\) in \(Mv_{s}\)do  Transmit (\(To\), \(j\), \(key_{j}\), IPFS(validation set));  Participants use private data to train their own local model; endfor for each participant \(i\) in \(Tp_{s}\) and each verifier \(j\) in \(Mv_{s}\)do  Transmit (\(i\), \(j\), \(key_{j}\), IPFS(\(model_{i}\))); endfor endif 3. verifyConsensus0: for each verifier \(j\) in \(Mv_{s}\)do \(result_{j}\) = Hash (\(acc_{1}\), \(acc_{2}\), \(\dots\), \(acc_{m}\), \(secret_{j}\)); endfor  The random verifier uploads the plaintext result - _accuracy_ [] _consensusFlag_ = true for each verifier \(j\) in \(Mv_{s}\)do ifHash (\(accuracy\) [], \(secret_{j}\)) \(\
eq\)\(result_{j}\)then  consensusFlag = false; endif endfor if consensusFlag = true then  Go to Algorithm 5  for each verifier \(j\) in \(Mv_{s}\)do  Transmit (\(j\), \(To\), \(key_{To}\), IPFS(\(verified\)models)); endfor else  Go to (3); endif
```  
**Algorithm 4** Collaborative task management (TMC)  
(c) Model-verification consensus and collaborative-task incentives. After receiving multiple pretrained models, model verifiers perform model verification by using the standard test set and upload the cryptographic verification results through the smart contract after completing all model verifications. The verification metric is the model-classification accuracy, and the cryptographic verification result of model verifier \(j\) is shown in Equation (8). Once all verifiers have completed uploading their encrypted verification results, the smart contract randomly assigns a verifier to upload the plaintext verification results and compares them to the encrypted verification results. If the comparison results are consistent, the smart contract declares a consensus on model verification, and \(Mv\)calls transmit() to send the validated pretrained model to _To_ as an on-chain encrypted transmission. If the consensus is not reached, _Su_ steps in and restarts a new round of model-validation consensus. Once the TMC determines that consensus has been reached, it will create the TIC for the task and transfer the _To_ pledge incentive to that contract:  
\[result_{j}=Hash(acc_{1},acc_{2},...,acc_{m},secret_{j}) \tag{8}\]  
The incentive mechanism is shown in Equation (9), where the TIC incentivises task participant \(i\) by \(T_{reward}\) and the model accuracy weight, where \(\theta_{count}\) is the number of models that satisfy the predefined verification threshold of the collaborative task, and the model verifier \(j\) incentive is determined by \(T_{reward}\) and incentive factor \(k\). In addition, the TIC determines whether all collaborative nodes have been incentivised by setting incentive flag bits _fundsToParticipants_ and _fundsToVerifiers_. The remaining funds are returned to _To_ after the incentive is completed:  
\[reward_{participants}[i]=\theta_{count}\times T_{reward}\times \frac{acc_{i}}{Execarray}\] \[reward_{participants}[j]=T_{reward}\times k \tag{9}\]  
(d) End of task. Consensus on model verification marks the completion of the main collaborative-learning step, and the subsequent allocation of incentives for collaboration is automatically performed by the smart contract according to predefined rules. At the same time, the DEC updates the data-quality evaluation, and the AMC updates the model-validator reputation value.