I tried using 'Hypercore Protocol' which allows you to easily try P2P distributed storage and KVS.



P2P , which sends and receives data directly between multiple computers, has attracted attention as a technology that solves problems such as 'centralized access to servers' and 'centralization' that traditional client-server structures have. The feature of P2P is that data can be distributed and stored, and it is used in cryptocurrencies such as Bitcoin and BitTorrent . If you use the open source software ' Hypercore Protocol ', you can easily use such distributed storage by P2P and KVS for free, so I actually tried using it.

Hypercore Protocol
https://hypercore-protocol.org/

Hypercore Protocol is a group of tools based on a function called 'Hypercore' that includes functions such as 'Hyperdrive' for distributed storage and 'Hyperbee' that can be used as KVS. Hypercore, which is the foundation, is a 'log store that can only add blocks' and has the same properties as a blockchain. The blocks are distributed and stored on the nodes that participate in Hypercore.

In addition to the nature of blockchain, Hypercore has the same data validation mechanism as BitTorrent, such as building a hash tree with added blocks on its leaves.



For example, if you want to verify an orange block or hash sent by a suspicious peer ...



Using the block or hash you want to verify, the root hash that corresponds to the apex of the tree structure is calculated back, and if it matches the original root hash, it can be judged that the data is correct.



The Hypercore Protocol is provided as a Node.js module, and there is also a command line interface. First, in order to use Hypercore Protocol from the command line, execute the following command to install the Hypercore Protocol CLI package from npm. This time I'm using Ubuntu 20.04 as the OS.

[code] sudo npm install -g @ hyperspace / cli [/ code]



First, execute the following command to create 'Hyperdrive', which is one of the functions included in the Hypercore Protocol. Hyperdrive is a distributed data storage based on Hypercore and can be used in the same way as normal cloud storage.

[code] hyp create drive [/ code]



When executed, the URL of Hyperdrive will be displayed like this. In addition, Hyperdrive runs on the daemon 'Hyperspace' that provides each API for 'Corestore' that manages Hypercore of log store and local storage, and 'Hyperswarm' of distributed hash storage, and it is set to 'hyp daemon status'. You can check the operating status of the Hyperspace daemon.



Execute the following command to create a directory on Hyperdrive.

[code] hyper drive mkdir hyper: // hash value / hyperdrive [/ code]



You can check the contents of the drive with the following command.

[code] hyp drive ls hyper: // hash value [/ code]



Like this, the 'hyperdrive' directory created earlier is displayed.



Next, try saving the file in the directory. First, prepare 'text.txt' with the content 'Hello, Hyperdrive!'.



Saving a file is a bit quirky, like specifying the file path to be created on Hyperdrive and entering the contents of the file as standard.

[code] hyp drive put hyper: // hash value /hyperdrive/text.txt <text.txt [/ code]



The command to check the contents of the file is as follows.

[code] hyp cat hyper: // hash value /hyperdrive/text.txt [/ code]



Like this, I was able to save a file with the content 'Hello, Hyperdrive!'. Since it includes command names often used in Linux such as 'mkdir', 'ls', and 'cat', it can be operated intuitively.



You can also synchronize the entire contents of the directory by executing the following command.

[code] hyp drive sync sync source directory hyper: // hash value / sync destination directory
[/ code]



The execution result looks like this.



The contents of Hyperdrive can be viewed not only on the command line but also on the browser. When you execute the following command ...

[code] hyp drive http hyper: // hash value [/ code]



You can check the contents of Hyperdrive on your browser from 'http: // localhost: 8080'.



In addition to Hyperdrive, the Hypercore Protocol also has a feature called Hyperbee that can be used as a KVS. Execute the following command to create Hyperbee.

[code] hyp create bee [/ code]



The URL of Hyperbee will be displayed as in the case of Hyperdrive.



How to use Hyperbee is very simple. Specify the Key as the path in the displayed URL, then specify the Value and execute the command as shown below. In addition, the type of Value is automatically determined.

[code] hyp bee put hyper: // hash value / giga 'Hello' [/ code]



Execute the following command to retrieve Value.

[code] hyp bee get hyper: // hash value / giga [/ code]



Like this, you can save and retrieve Key and Value in KVS.



In addition, Hypyercore has a function called 'beam' that can stream data, and using beam makes it easy to perform one-to-one data streaming. As the streaming source, enter the content you want to send in 'hyp beam' passphrase '' as standard, as shown in the following command.

[code] cat beam.txt | hyp beam 'this is a secret passphrase' [/ code]



The execution result looks like this.



The standard output content can be received as the standard output of the streaming destination 'hyp beam' passphrase ''.



So far, we've tried Hypercore Protocol on the command line, but Hypercore Protocol can also be used in Node.js code. The following code is Node.js code that can save the file specified in the argument to Hyperdrive. It can run on a host running the Hyperspace daemon.

[code] import {Client as HyperspaceClient} from'hyperspace'
import Hyperdrive from'hyperdrive'
import {readFileSync} from'fs'
import {argv} from'process'

async function main () {

// Read the file specified in the argument
const data = readFileSync (argv [2], 'utf-8')

// Establish a connection with Hyperspace
const client = new HyperspaceClient ()

// Create Hyperdrive
let drive = new Hyperdrive (client.corestore (), null)
await drive.promises.ready ()
console.log ('Hyperdrive URL:')
console.log ('hyper: //' + drive.key.toString ('hex'))

// write the file
await drive.promises.writeFile (argv [2], data)

// Expand on a distributed hash table
await client.replicate (drive)
await new Promise (r => setTimeout (r, 3e3))
await client.network.configure (drive, {announce: false, lookup: false})

// Exit the client
await client.close ()
}

main () [/ code]



The execution result looks like this. I was able to confirm from the 'hyp drive cat' command that the locally saved 'hello.txt' was properly saved on Hyperdrive.



Note that the data saved by Hypercore Protocol is 'stored only locally' until you access the data on another node or someone else accesses the data on your node. Be careful about the points. Since data is not exchanged with other nodes in this review, for example, if the PC used for the node is initialized, the data saved in Hyperdrive will also be deleted.

in Review,   Software, Posted by darkhorse_log