# 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)
The period between the end of the answer period and the generation of X more blocks is called the **disclosure period**, during which the common key is passed to the ACC to decrypt the encrypted proof. Next to the disclosure period is the **claim period**, in which the most common answer recorded in the answer period is considered the correct answer, and storage nodes that correctly answer the quiz receive a reward in proportion to the amount of the deposit, where the reward of the quiz is paid in the original token called DBookToken (DBT), which is issued by the ACC. On the other hand, incorrect answers to the quiz will forfeit the deposit. The code for the vote Listing 4, disclosure Listing 5, and claim Listing 6 functions is as follows:
```
functionvote(bytes32 encrypted_answer)payablepublic{ require(block.number%event_length<event_length/3,\"Outofvoting \(\rightarrow\)period.\"); uintevent_id=block.number/event_length; uintgroup=_groups[_account_peer_id,map[msg.nender]]; require(group>0,\"Youaren'registered.\"); require(!encrypted_answer_exists[event_id][group][encrypted_answer],\"The sameasseralreadysubmitted.\"); encrypted_answer_exists[event_id][group][encrypted_answer]=true; _deposits[event_id][group][msg.nender]+=msg.value; }
```
Listing 5: Solidity code for the disclosure function. ```
functiondisclosure(bytes32 answer,bytes32 key)public{ require(block.number%event_length>=event_length/3&&block.number%event_length<event_length*2/3,\"Outofdisclosureperiod.\"); uintevent_id=block.number/event_length; uintgroup=_groups[_account_peer_id,map[msg.nender]]; require(encrypted_answer_exists[event_id][group][kececack256(abi.encode( \(\rightarrow\)answer,key)]); //invalidanswerandkey\"); require(_answ[event_id][group][msg.sender]==bytes32(0),\"Answer \(\rightarrow\)alreadydisclosed.\"); _answ[event_id][group][msg.sender]=answer; if(++_answer_counts[event_id][group][answer]==1)_answer_lists[ \(\rightarrow\)event_id][group].push(answer); }
```
Listing 6: Solidity code for the vote function. Figure 4: The flow of sampling quiz. ```
functionclaim()public{ require(block.number%event_length>=event_length*2/3,\"Outofdisclosureperiod.\"); uintevent_id=block.number/event_length; uintgroup=_group[_account_peer_id,map[msg.sender]]; bytes32 ans=_answere[event_id][group][msg.sender]; uintans_cnt=_answer_counts[event_id][group][ans]; for(uinti=0;i<_answer_lists[event_id][group].length;i++){ bytes32 ans=_i=_answer_lists[event_id][group][i]; require(ans:===||_answer_counts[event_id][group][ans_i]<-ans_cnt,\"Youransweriswrong\"); } uintdeposit=_deposits[event_id][group][msg.sender]; _deposits[event_id][group][msg.sender]=0; payable(msg.sender).transfer(deposit); dbt.mint(msg.sender,deposit*10);//reward }
```
Listing 6: Solidity code for the claim function. ### Autonomous Management of Node Groups

In the proposed method, the number of replicas of a shard is equal to the size of the node group corresponding to the shard, so a group with a small number of nodes is susceptible to node departures and chunk deletions. In order to overcome this problem, the d-book-repository introduces a smart contract that assigns a newly joined node to the group with the smallest number of nodes at that time, whereas before, the current implementation realizes this smart contract as part of the ACC. The specific flow for the registration of a storage node proceeds as follows: First, a new storage node identifies a group with the smallest number of nodes by referring to the next_group function of the ACC. Then, the node creates a PeerID for that group by executing registerNode and pays a registration fee to join the ACC. At the end of the registration, the number of nodes in the group is increased by one on the ACC. A similar procedure is used for the unregistration of a node. Specifically, it calls leaveNode for the registration and canceling of the registration, the number of nodes in the group recorded in the ACC is decreased by one, and the registration fee is refunded when the process of unregistration is completed.