Creating a private testnet with Swarm in Ubuntu

in #crypto7 years ago

So if you've looked at ethereum-testrpc, its methods are suitable for most of the functions that you'd want to implement, however they do not include connecting to Swarm, ethereum's persistent file storage protocol (It is important to note that both of these are in development so use Swarm at your own risk).

Therefore if you would like to use these protocols in your app, it is necessary to use a geth private network, and I will show you how to set it up. This is assuming that you have a working instance of geth that you can access from the command line so if you don't follow the instructions here to install geth.

Create a file titled CustomGenesis.json with the code below. This is called the genesis block which supplies the configuration for the private net:
{
"coinbase" : "0x0000000000000000000000000000000000000001",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc": {
"91669ebE17D588CD24d5d4a37d8191f8E8E411Ad": {
"balance": "1337000000000000000000"
}
},
"config": {
"chainId": 34,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
}
}

Move your CustomGenesis.json to a directory you would like your private net to be stored and initialize the private net with the command below.

geth init --datadir=/path/to/private/net /path/to/private/net/CustomGenesis.json

You can run geth with the command:

$ geth --datadir=/path/to/private/net --networkid=34 --rpc

Note that networkid corresponds to the chainID we set in the genesis block.

If you would like to run commands on the geth Javascript console, make sure you have geth running in a separate terminal and enter:

$ geth attach /path/to/private/net/geth.ipc

Important Note about Swarm: If you're running Ubuntu 16.04 there is a very good chance that the swarm package installed points to a package about molecular clustering. A run of apt-cache policy swarm shows that the instance of swarm with higher priority is the one with a higher version number:

$ apt-cache policy swarm
swarm:
Installed: 2.1.6-1
Candidate: 1.6.6+build10111+xenial
Version table:
*** 2.1.6-1 500
500 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 Packages
100 /var/lib/dpkg/status
1.6.6+build10111+xenial 500
500 http://ppa.launchpad.net/ethereum/ethereum/ubuntu xenial/main amd64 Packages

To fix that, edit /etc/apt/preferences with sudo rights like so:

Package: swarm
Pin: version 1.6.6+build10111+xenial
Pin-Priority: 1001

In pin, make sure that you insert the number of the newest version of swarm.
Running apt-cache policy swarm will return something like this:

apt-cache policy swarm
swarm:
Installed: 2.1.6-1
Candidate: 1.6.6+build10111+xenial
Version table:
*** 2.1.6-1 500
500 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 Packages
100 /var/lib/dpkg/status
1.6.6+build10111+xenial 1001
500 http://ppa.launchpad.net/ethereum/ethereum/ubuntu xenial/main amd64 Packages

This heightened priority to the different swarm package will trigger a package downgrade to the lower version number.
Run these commands to install the correct version of swarm:

$ sudo apt-get clean all
$ sudo apt-get update
$ sudo apt-get install swarm

Now that you have swarm installed, here's how to use it. For this next section, it is easier to save the path to your geth instance in an environment variable like $DATADIR similar to how $PATH is stored on linux. We can do that by typing sudo nano ~/.bashrc and adding the lines:

DATADIR="/path/to/private/net/"

Then run source ~/.bashrc to apply the changes.

To set up swarm, set up an account by using geth attach $DATADIR/geth.ipc as shown above to attach to your running geth instance and run the command:

personal.newAccount("passphrase")

Copy the hex code and add it to BZZKEY in /etc/environment exactly like what we did for DATADIR above.

Then to run swarm, make sure geth is running then run this command:

swarm --bzzaccount $BZZKEY
--swap
--datadir $DATADIR
--ethapi $DATADIR/geth.ipc
--maxpeers 0
--bzznetworkid 34 \

You will be prompted for a password so enter what you entered for the passphrase for your account and then the swarm instance will have been created.

Sort:  

Congratulations @inflatibleyoshi! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

SteemitBoard Ranking update - A better rich list comparator
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Congratulations @inflatibleyoshi! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.17
TRX 0.24
JST 0.034
BTC 95807.92
ETH 2778.70
SBD 0.68