# Trusted Electronic Contract for Enabling Peer-to-Peer HPC Resource Sharing
## 3 Proposed Design
### Smart Contract Design and Communication Flow
```
1:var listOfActiveJobs=[{?jobID\": \"57714\",'status': \"RUN\", \"owner\": \"kajornsak\", \"isMonitored\": true ],... ]//job monitoring list (app.js)
2:app.post/addJobMonitoring/, (req, res) \(\infty\) {//endpoint for requesting adding new job
3:listOfActiveJobs.push({?jobID\": req.body.job_id,'status': null, owner\": req.body.username, \"isMonitored\": false })
4:res.send()jobID\":sjoID\" is being added ]; });
5:functioncreateMonitoringWorker(jobID, status){
6:let worker = new Workerf./worker.js, { workerData: { jobID: jobID, currentStatus: status } }); // create job monitoring worker
7:worker.once(\"message\", result \(\infty\) {// worker return an update in job status
8:let newStatus = result[jobID] // sample result {\"jid01234\": \"QUEUE\"}
9:if(newStatus == \"END\")[ // if the job's computing is done, send update to blockchain service and remove job from monitor list
10:axios.post(localhost:5000/updateJobStatus', result).then((response)\(\infty\)[ console.log(response.data) ))
11:listOfActiveJobs = listOfActiveJobs.filter(obj \(\infty\) obj[?jobID\"]!=jobID);
12:jelse{ // if the job's status is changed, send update to blockchain service and update the local list of job status
13:axios.post(localhost:5000/updateJobStatus', result).then((response)\(\infty\)[ console.log(response.data) )]
14:listOfActiveJobs = listOfActiveJobs.map(obj \(\infty\) obj[?jobID\"] == jobID? {...obj, status: newStatus : obj });
15: ]);
16: }
17:while(listOfActiveJobs.length == 0){ listOfActiveJobs.map({job } \(\infty\) { if (! job. isMonitored) { createMonitoringWorker(job.jobID, job.status)}}) }) }
18:app.listen(3000, () \(\infty\) { console.log('Listening at http://localhost:3000'); });
```
**Algorithm 1** Function getJobStatusUpdate()
```
1:functiongetJobStatusUpdate(){
2:exec(\"sh -/getJobStatus.sh \"+ workerData.jobID, function (err, stdout, stderr) {
3:stdout.on('data', '(data) \(\Rightarrow\) {
4:let latestStatus = data
5:if (latestStatus!== workerData.currentStatus) {
6:parentPort.postMessage([\"jobID\": workerData.jobID, \"new_status\": latestStatus)] // all jobs of the user with the states
7:jelse[ setTimeout(getJobStatusUpdate, 3600000); }
8: ]); }); }); }
9:getJobStatusUpdate()
```

**Algorithm 2** Function getJobStatusUpdate()

These bash scripts contain a sequence of Linux commands for directly interacting with a scheduler. The script needs to be customized according to the scheduler's brand--in our setting, the TORQUE scheduler was used. The output is then converted into the pre-agreed format (Figure 5b) which is understandable by the System Monitoring Service. A code snippet is displayed below. ## 5 Performance Evaluation and Security Analysis

The experiment was conducted on the blockchain network and interface module to measure the performance and perform security evaluation. ### Performance Evaluation

In our blockchain implementation, ordering service with Raft protocol was configured instead of other service types that are available in Hyperledger Fabric. To ensure that Raft presents the best performance compared to the others, we used Docker to set up a blockchain network comprising two organizations with a single peer node each. The test was set up on a machine with Ubuntu 16.04 LTS with 2 CPU cores 2.80 GHz and 12,288 MB of memory. Hyperledger Caliper [35], an open-source blockchain benchmark tool, was installed to measure network performance. Displayed in Table 2, _Raft_ exhibited lower latency in both query and invoke transactions, compared to another multi-node ordering service type (i.e., _Kafka_[36]). Latency in transaction querying in multi-node _Raft_ was close to a _Solo_, a single-node ordering service that is suggested to be used in only a development setting. As mentioned in the implementation section, the blockchain network with the same number of components and topology was deployed on two settings, which are on single-host machines and multi-host machines. For the single-host setting, Hyperledger Caliper was deployed as a container within the Docker network.