# Efficient and Secure Management of Medical Data Sharing Based on Blockchain Technology
## 4 Medical Data Provenance Methods
### DAG-Based Algorithm  
In addition to the original medical data, all processed medical data correspond to a certain operational behavior. Considering the dependence between medical data and operational behaviors data, this paper uses DAG to reconstruct all medical data and operational behaviors data stored on the blockchain, thereby converting the data provenance on the chain into a DAG. In the reconstructed DAG, vertices represent medical data and edges represent operational behaviors data. The schematic diagram of the reconstruction is shown in Figure 3.  
In order to facilitate forward tracking and backward tracing, we construct two DAGs \(G_{1}\) and \(G_{2}\) to reconstruct the data on the chain. \(G_{1}\) and \(G_{2}\) have the same content except for the opposite direction of edges. In order to save storage space, we use adjacency lists to store the graph and load the adjacency list into memory for tracking or tracing. To avoid reconstructing the DAG each time a node is disconnected and reconnected, we serialize the adjacency list in memory to the local. In order to support data tracing within the blockchain interval, we design an adjacency list with block numbers to indicate which block each transaction appears in. Below, we describe the design of the adjacency list with the DAG shown in Figure 3 as an example.  
First, we number the vertices and edges in the graph using a hash table. To facilitate data access, we encode the keys of the hash table in an increasing order from 0. The  
According to the constructed forward tracking and backward tracing adjacency list, performing a breadth-first search algorithm on the DAG can quickly achieve results. The query process can be described in Algorithm 3.  
```
Input: ID of medical data: \(d_{id}\), forward or backward tracing: \(flag\); // if flag = true, backward tracing; if flag = false, forward tracking. Output: The provenance results of data \(d_{id}\): results;
1\(G_{1}\) = buildForwardGraph() ;
2\(G_{2}\) = buildBackwardGraph() ;
3\(H_{1}\) = nodesToNum(\(G_{1}\)) ;
4\(H_{2}\) = numToNodes(\(G_{1}\)) ;
5\(index_{d_{id}}\) = getIndexOfData(\(d_{id}\), \(H_{1}\)); // get the index of data \(d_{id}\) in \(G_{1},G_{2}\).
6 if flag = truethen
7 results = breadFirstSearch(\(G_{1}\), \(H_{2}\)); // return the triple list, each tuple is similar (input, output, opSign).
8 else
9 results = breadFirstSearch(\(G_{2}\), \(H_{2}\));
10 end if
11 results = DAGInterval(results); //;
12 Obtain the results that meet the constraints [f, t]; return results;
```  
**Algorithm 3**DAG-based provenance  
In the DAG-based data provenance method, since the file storing the DAG is not stored on the blockchain, in order to prevent the file from being tampered with and affecting the accuracy of the query results, we designed a verification method in the implementation process to determine whether the file storing the DAG has been tampered with. This method updates the DAG, and the file storing the DAG every \(n\) new blocks is generated and stores the updated hash value of the file storing the DAG as a special transaction on the blockchain.When the system reads and loads the DAG file every time, it needs to verify whether the hash value of the file storing the DAG is consistent with that stored on the blockchain, and if it is consistent, it will load directly, otherwise it will reconstruct the DAG file and load it again.  
Figure 6: Backward tracing adjacency list.