# Decentralized Storage with Access Control and Data Persistence for e-Book Stores
## 4 Proposed System
### Decentralized Access Control to the Stored Data
#### 4.2.1 Access Control Contract (ACC)
Note that the PeerID of the caller can be calculated from msg.sender. The code for the registerNode Listing 7, next_group Listing 8, and leaveNode Listing 9 functions is as follows:
```
functionregisterNode(stringmemorypeer_id)payablepublic{ require(_groups[peer_id]==0,\"Thispeeridisalreadyused.\"); require(msg.value==registration_fee,\"Registrationfeeisrequired.\"); uintgroup=next_group();-groupUpdateCounter[group]++;-groups[peer_id]=group;-account_peer_id_map[msg.sender]=peer_id; }
```
Listing 7: Solidity code for the registerNode function. ```
functionnext_group()publicviewreturns(uint){ uintgroup=1; uintm=_groupNodeCounter[1]; for(uinti=2;i<=node_number;i++){ if(mi>-groupNodeCounter[i]){ mi=_groupNodeCounter[i]; group=i; } } returngroup; }
```
Listing 8: Solidity code for the next_group function. ```
functionleaveNode()public{ stringmemorypeer_id=account_peer_id_map[msg.sender]; uintgroup=_groups[peer_id]; require(group!=0,\"Thispeerisnotregistered.\"); __groups[peer_id]=0; __groupNodeCounter[group]--; __account_peer_id_map[msg.sender]=\"\"; __payable(msg.sender).transfer(registration_fee); }
```

**Listing 9**. Solidity code for the leaveNode function. Note that charging a participation fee for node registration is intended to prevent Sybil attacks. In addition, the fact that a node cannot specify the group it will join increases the difficulty of a 50% attack on the incentive system and makes it difficult to create multiple accounts to join the system as storage nodes to restore arbitrary content. ### Lightweight Tamper Detection Using Merkle Tree

In order to provide tamper resistance to stored data, the d-book-repository provides a verification function using a Merkle tree. The specific procedure is as follows: First, the author constructs a Merkle tree with the hash values of the shards as the leaves and submits the Merkle root to the ACC when the e-book is registered. See Figure 5 for an illustration. The Merkle proof is then attached when the shard is uploaded to the d-book-repository. In this way, a client receiving a shard from a storage node can verify whether the shard has been tampered with or not by referring to the Merkle proof attached to the shard and the Merkle root stored in the ACC. The main reason for using the Merkle tree is to save on gas costs. In addition, this method can significantly reduce the storage cost since only the root, not the hash value of each shard, needs to be stored. ## 5 Evaluation

In this section, we assess the performance of the proposed system by combining empirical data obtained through experiments conducted with a cloud-based prototype system and predictive values derived from event-driven simulations. We begin by describing the implementation of the core functionalities of the prototype system in Section 5.1, followed by a presentation of experimental outcomes obtained from the prototype in Section 5.2. The primary objective of these experiments is to verify the practical speed of the proposed system's operation. Subsequently, in Section 5.3 and subsequent sections, we proceed to evaluate various aspects, including persistence, fault tolerance, and the cost of the functionalities. Figure 5: Overview of Merkle tree built from content shards. ### Prototype System

As described in the previous section, the proposed system comprises two key components: the d-book-repository and the ACC. In the present implementation, the ACC is written in Solidity, a JavaScript-like programming language for specifying smart contracts, while we selected Rust for writing the d-book-repository because it is a memory-safe and fast language. The implementation of d-book-repository leverages various pre-existing technologies. The management of PeerIDs, the propagation of e-book data through the gossipsub protocol, and message exchange between peers are all realized through the utilization of the libp2p library [26], a technology previously employed in the development of the IPFS.