2019-06-03 12:28:18 +02:00
## Swarm
2015-02-05 12:34:47 -08:00
2019-06-03 12:28:18 +02:00
[https://swarm.ethereum.org ](https://swarm.ethereum.org )
2015-02-05 12:34:47 -08:00
2019-06-03 12:28:18 +02:00
Swarm is a distributed storage platform and content distribution service, a native base layer service of the ethereum web3 stack. The primary objective of Swarm is to provide a decentralized and redundant store for dapp code and data as well as block chain and state data. Swarm is also set out to provide various base layer services for web3, including node-to-node messaging, media streaming, decentralised database services and scalable state-channel infrastructure for decentralised service economies.
2015-01-24 18:48:19 +01:00
2019-06-03 13:10:20 +02:00
[](https://travis-ci.org/ethersphere/swarm)
2019-06-03 12:28:18 +02:00
[](https://gitter.im/ethersphere/orange-lounge?utm_source=badge& utm_medium=badge& utm_campaign=pr-badge)
## Table of Contents
* [Building the source ](#building-the-source )
* [Running Swarm ](#running-swarm )
* [Documentation ](#documentation )
* [Developers Guide ](#developers-guide )
* [Go Environment ](#development-environment )
* [Vendored Dependencies ](#vendored-dependencies )
* [Testing ](#testing )
* [Profiling Swarm ](#profiling-swarm )
* [Metrics and Instrumentation in Swarm ](#metrics-and-instrumentation-in-swarm )
* [Public Gateways ](#public-gateways )
* [Swarm Dapps ](#swarm-dapps )
* [Contributing ](#contributing )
* [License ](#license )
2014-10-27 17:22:29 +01:00
2015-08-18 22:46:58 +02:00
## Building the source
2015-04-28 12:13:42 +02:00
2019-06-03 14:08:40 +02:00
Building Swarm requires Go (version 1.11 or later).
2015-04-28 12:13:42 +02:00
2019-06-03 20:59:48 +02:00
To simply compile the `swarm` binary without a `GOPATH` :
$ git clone https://github.com/ethersphere/swarm
$ cd swarm
$ make swarm
You will find the binary under `./build/bin/swarm` .
To build a vendored `swarm` using `go get` you must have `GOPATH` set. Then run:
2019-06-03 13:10:20 +02:00
go get -d github.com/ethersphere/swarm
2015-04-28 12:13:42 +02:00
2019-06-03 13:10:20 +02:00
go install github.com/ethersphere/swarm/cmd/swarm
2015-04-28 12:13:42 +02:00
2019-06-03 12:28:18 +02:00
## Running Swarm
2016-05-23 14:19:17 +01:00
2019-06-03 12:28:18 +02:00
Going through all the possible command line flags is out of scope here, but we've enumerated a few common parameter combos to get you up to speed quickly on how you can run your own Swarm node.
2016-05-23 14:19:17 +01:00
2019-06-03 13:10:20 +02:00
To run Swarm you need an Ethereum account. Download and install [Geth ](https://geth.ethereum.org ) if you don't have it on your system. You can create a new Ethereum account by running the following command:
2014-02-15 00:04:46 +01:00
2019-06-03 12:28:18 +02:00
geth account new
2014-11-18 20:23:17 +01:00
2019-06-03 12:28:18 +02:00
You will be prompted for a password:
2014-02-15 13:27:23 +01:00
2019-06-03 12:28:18 +02:00
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase:
Repeat passphrase:
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
Once you have specified the password, the output will be the Ethereum address representing that account. For example:
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
Address: {2f1cd699b0bf461dcfbf0098ad8f5587b038f0f1}
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
Using this account, connect to Swarm with
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
swarm --bzzaccount < your-account-here >
# in our example
swarm --bzzaccount 2f1cd699b0bf461dcfbf0098ad8f5587b038f0f1
### Verifying that your local Swarm node is running
When running, Swarm is accessible through an HTTP API on port 8500.
Confirm that it is up and running by pointing your browser to http://localhost:8500
### Ethereum Name Service resolution
The Ethereum Name Service is the Ethereum equivalent of DNS in the classic web. In order to use ENS to resolve names to Swarm content hashes (e.g. `bzz://theswarm.eth` ), `swarm` has to connect to a `geth` instance, which is synced with the Ethereum mainnet. This is done using the `--ens-api` flag.
swarm --bzzaccount < your-account-here > \
--ens-api '$HOME/.ethereum/geth.ipc'
# in our example
swarm --bzzaccount 2f1cd699b0bf461dcfbf0098ad8f5587b038f0f1 \
--ens-api '$HOME/.ethereum/geth.ipc'
For more information on usage, features or command line flags, please consult the Documentation.
## Documentation
Swarm documentation can be found at [https://swarm-guide.readthedocs.io ](https://swarm-guide.readthedocs.io ).
## Developers Guide
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
### Go Environment
2016-07-12 12:51:31 +03:00
2019-06-03 13:10:20 +02:00
We assume that you have Go v1.11 installed, and `GOPATH` is set.
2016-07-12 12:51:31 +03:00
2019-06-03 13:10:20 +02:00
You must have your working copy under `$GOPATH/src/github.com/ethersphere/swarm` .
2016-07-12 12:51:31 +03:00
2019-06-03 13:10:20 +02:00
Most likely you will be working from your fork of `swarm` , let's say from `github.com/nirname/swarm` . Clone or move your fork into the right place:
2016-07-12 12:51:31 +03:00
```
2019-06-03 13:10:20 +02:00
git clone git@github .com:nirname/swarm.git $GOPATH/src/github.com/ethersphere/swarm
2016-07-12 12:51:31 +03:00
```
2019-06-03 12:28:18 +02:00
### Vendored Dependencies
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
All dependencies are tracked in the `vendor` directory. We use `govendor` to manage them.
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
If you want to add a new dependency, run `govendor fetch <import-path>` , then commit the result.
2018-02-10 04:50:14 -06:00
2019-06-03 12:28:18 +02:00
If you want to update all dependencies to their latest upstream version, run `govendor fetch +v` .
### Testing
This section explains how to run unit, integration, and end-to-end tests in your development sandbox.
Testing one library:
2018-02-10 04:50:14 -06:00
```
2019-06-03 13:10:20 +02:00
go test -v -cpu 4 ./api
2018-02-10 04:50:14 -06:00
```
2019-06-03 12:28:18 +02:00
Note: Using options -cpu (number of cores allowed) and -v (logging even if no error) is recommended.
2017-06-21 14:53:08 +02:00
2019-06-03 12:28:18 +02:00
Testing only some methods:
2017-06-21 14:53:08 +02:00
```
2019-06-03 13:10:20 +02:00
go test -v -cpu 4 ./api -run TestMethod
2017-06-21 14:53:08 +02:00
```
2019-06-03 12:28:18 +02:00
Note: here all tests with prefix TestMethod will be run, so if you got TestMethod, TestMethod1, then both!
Running benchmarks:
2017-06-21 14:53:08 +02:00
```
2019-06-03 12:28:18 +02:00
go test -v -cpu 4 -bench . -run BenchmarkJoin
2017-06-21 14:53:08 +02:00
```
2019-06-03 12:28:18 +02:00
### Profiling Swarm
2016-08-18 12:43:38 -04:00
2019-06-03 12:28:18 +02:00
This section explains how to add Go `pprof` profiler to Swarm
2016-08-18 12:43:38 -04:00
2019-06-03 12:28:18 +02:00
If `swarm` is started with the `--pprof` option, a debugging HTTP server is made available on port 6060.
2016-08-18 12:43:38 -04:00
2019-06-03 12:28:18 +02:00
You can bring up http://localhost:6060/debug/pprof to see the heap, running routines etc.
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
By clicking full goroutine stack dump (clicking http://localhost:6060/debug/pprof/goroutine?debug=2) you can generate trace that is useful for debugging.
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
### Metrics and Instrumentation in Swarm
This section explains how to visualize and use existing Swarm metrics and how to instrument Swarm with a new metric.
Swarm metrics system is based on the `go-metrics` library.
The most common types of measurements we use in Swarm are `counters` and `resetting timers` . Consult the `go-metrics` documentation for full reference of available types.
2016-07-12 12:51:31 +03:00
```
2019-06-03 12:28:18 +02:00
# incrementing a counter
metrics.GetOrRegisterCounter("network.stream.received_chunks", nil).Inc(1)
# measuring latency with a resetting timer
start := time.Now()
t := metrics.GetOrRegisterResettingTimer("http.request.GET.time"), nil)
...
t := UpdateSince(start)
2016-07-12 12:51:31 +03:00
```
2019-06-03 12:28:18 +02:00
#### Visualizing metrics
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
Swarm supports an InfluxDB exporter. Consult the help section to learn about the command line arguments used to configure it:
2016-07-12 12:51:31 +03:00
```
2019-06-03 12:28:18 +02:00
swarm --help | grep metrics
2016-07-12 12:51:31 +03:00
```
2019-06-03 14:08:40 +02:00
We use Grafana and InfluxDB to visualise metrics reported by Swarm. We keep our Grafana dashboards under version control at https://github.com/ethersphere/grafana-dashboards. You could use them or design your own.
2016-07-12 12:51:31 +03:00
2019-06-03 14:08:40 +02:00
We have built a tool to help with automatic start of Grafana and InfluxDB and provisioning of dashboards at https://github.com/nonsense/stateth, which requires that you have Docker installed.
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
Once you have `stateth` installed, and you have Docker running locally, you have to:
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
1. Run `stateth` and keep it running in the background
```
2019-06-03 14:08:40 +02:00
stateth --rm --grafana-dashboards-folder $GOPATH/src/github.com/ethersphere/grafana-dashboards --influxdb-database metrics
2019-06-03 12:28:18 +02:00
```
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
2. Run `swarm` with at least the following params:
2016-07-12 12:51:31 +03:00
```
2019-06-03 12:28:18 +02:00
--metrics \
--metrics.influxdb.export \
--metrics.influxdb.endpoint "http://localhost:8086" \
--metrics.influxdb.username "admin" \
--metrics.influxdb.password "admin" \
--metrics.influxdb.database "metrics"
2016-07-12 12:51:31 +03:00
```
2019-06-03 12:28:18 +02:00
3. Open Grafana at http://localhost:3000 and view the dashboards to gain insight into Swarm.
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
## Public Gateways
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
Swarm offers a local HTTP proxy API that Dapps can use to interact with Swarm. The Ethereum Foundation is hosting a public gateway, which allows free access so that people can try Swarm without running their own node.
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
The Swarm public gateways are temporary and users should not rely on their existence for production services.
The Swarm public gateway can be found at https://swarm-gateways.net and is always running the latest `stable` Swarm release.
## Swarm Dapps
You can find a few reference Swarm decentralised applications at: https://swarm-gateways.net/bzz:/swarmapps.eth
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
Their source code can be found at: https://github.com/ethersphere/swarm-dapps
2016-07-12 12:51:31 +03:00
2019-06-03 12:28:18 +02:00
## Contributing
2013-12-26 13:29:45 +01:00
2016-04-29 17:40:19 +03:00
Thank you for considering to help out with the source code! We welcome contributions from
anyone on the internet, and are grateful for even the smallest of fixes!
2015-03-03 01:44:29 +07:00
2019-06-03 12:28:18 +02:00
If you'd like to contribute to Swarm, please fork, fix, commit and send a pull request
2016-04-29 17:40:19 +03:00
for the maintainers to review and merge into the main code base. If you wish to submit more
2019-06-03 12:28:18 +02:00
complex changes though, please check up with the core devs first on [our Swarm gitter channel ](https://gitter.im/ethersphere/orange-lounge )
2016-05-12 22:40:47 +09:00
to ensure those changes are in line with the general philosophy of the project and/or get some
2016-04-29 17:40:19 +03:00
early feedback which can make both your efforts much lighter as well as our review and merge
procedures quick and simple.
2015-03-03 01:44:29 +07:00
2016-05-12 22:40:47 +09:00
Please make sure your contributions adhere to our coding guidelines:
2014-02-08 22:16:11 +01:00
2016-04-29 17:40:19 +03:00
* Code must adhere to the official Go [formatting ](https://golang.org/doc/effective_go.html#formatting ) guidelines (i.e. uses [gofmt ](https://golang.org/cmd/gofmt/ )).
2016-05-12 22:40:47 +09:00
* Code must be documented adhering to the official Go [commentary ](https://golang.org/doc/effective_go.html#commentary ) guidelines.
2016-11-15 15:47:04 +01:00
* Pull requests need to be based on and opened against the `master` branch.
2019-06-03 13:10:20 +02:00
* [Code review guidelines ](https://github.com/ethersphere/swarm/blob/master/docs/Code-Review-Guidelines.md ).
2016-04-29 17:40:19 +03:00
* Commit messages should be prefixed with the package(s) they modify.
2019-06-03 13:10:20 +02:00
* E.g. "fuse: ignore default manifest entry"
2015-03-03 01:44:29 +07:00
2016-04-29 17:40:19 +03:00
## License
2014-01-11 15:27:08 +01:00
2016-04-29 17:40:19 +03:00
The go-ethereum library (i.e. all code outside of the `cmd` directory) is licensed under the
2016-09-27 08:48:55 +02:00
[GNU Lesser General Public License v3.0 ](https://www.gnu.org/licenses/lgpl-3.0.en.html ), also
2016-04-29 17:40:19 +03:00
included in our repository in the `COPYING.LESSER` file.
2014-02-15 11:49:29 +01:00
2016-04-29 17:40:19 +03:00
The go-ethereum binaries (i.e. all code inside of the `cmd` directory) is licensed under the
2016-09-27 08:48:55 +02:00
[GNU General Public License v3.0 ](https://www.gnu.org/licenses/gpl-3.0.en.html ), also included
2016-04-29 17:40:19 +03:00
in our repository in the `COPYING` file.