go-ethereum/swarm
Ferenc Szabo 50b872bf05 p2p, swarm: fix node up races by granular locking (#18976)
* swarm/network: DRY out repeated giga comment

I not necessarily agree with the way we wait for event propagation.
But I truly disagree with having duplicated giga comments.

* p2p/simulations: encapsulate Node.Up field so we avoid data races

The Node.Up field was accessed concurrently without "proper" locking.
There was a lock on Network and that was used sometimes to access
the  field. Other times the locking was missed and we had
a data race.

For example: https://github.com/ethereum/go-ethereum/pull/18464
The case above was solved, but there were still intermittent/hard to
reproduce races. So let's solve the issue permanently.

resolves: ethersphere/go-ethereum#1146

* p2p/simulations: fix unmarshal of simulations.Node

Making Node.Up field private in 13292ee897e345045fbfab3bda23a77589a271c1
broke TestHTTPNetwork and TestHTTPSnapshot. Because the default
UnmarshalJSON does not handle unexported fields.

Important: The fix is partial and not proper to my taste. But I cut
scope as I think the fix may require a change to the current
serialization format. New ticket:
https://github.com/ethersphere/go-ethereum/issues/1177

* p2p/simulations: Add a sanity test case for Node.Config UnmarshalJSON

* p2p/simulations: revert back to defer Unlock() pattern for Network

It's a good patten to call `defer Unlock()` right after `Lock()` so
(new) error cases won't miss to unlock. Let's get back to that pattern.

The patten was abandoned in 85a79b3ad3c5863f8612d25c246bcfad339f36b7,
while fixing a data race. That data race does not exist anymore,
since the Node.Up field got hidden behind its own lock.

* p2p/simulations: consistent naming for test providers Node.UnmarshalJSON

* p2p/simulations: remove JSON annotation from private fields of Node

As unexported fields are not serialized.

* p2p/simulations: fix deadlock in Network.GetRandomDownNode()

Problem: GetRandomDownNode() locks -> getDownNodeIDs() ->
GetNodes() tries to lock -> deadlock

On Network type, unexported functions must assume that `net.lock`
is already acquired and should not call exported functions which
might try to lock again.

* p2p/simulations: ensure method conformity for Network

Connect* methods were moved to p2p/simulations.Network from
swarm/network/simulation. However these new methods did not follow
the pattern of Network methods, i.e., all exported method locks
the whole Network either for read or write.

* p2p/simulations: fix deadlock during network shutdown

`TestDiscoveryPersistenceSimulationSimAdapter` often got into deadlock.
The execution was stuck on two locks, i.e, `Kademlia.lock` and
`p2p/simulations.Network.lock`. Usually the test got stuck once in each
20 executions with high confidence.

`Kademlia` was stuck in `Kademlia.EachAddr()` and `Network` in
`Network.Stop()`.

Solution: in `Network.Stop()` `net.lock` must be released before
calling `node.Stop()` as stopping a node (somehow - I did not find
the exact code path) causes `Network.InitConn()` to be called from
`Kademlia.SuggestPeer()` and that blocks on `net.lock`.

Related ticket: https://github.com/ethersphere/go-ethereum/issues/1223

* swarm/state: simplify if statement in DBStore.Put()

* p2p/simulations: remove faulty godoc from private function

The comment started with the wrong method name.

The method is simple and self explanatory. Also, it's private.
=> Let's just remove the comment.
2019-02-18 07:38:14 +01:00
..
api swarm: Debug API and HasChunks() API endpoint (#18980) 2019-02-07 15:49:19 +01:00
bmt vendor, crypto, swarm: switch over to upstream sha3 package 2019-01-04 09:26:07 +02:00
chunk swarm/network, swarm/storage: validate chunk size (#17397) 2018-08-14 16:03:56 +02:00
dev swarm: bzz-list, bzz-raw and bzz-immutable schemes (#15667) 2017-12-19 10:49:30 +02:00
docker swarm/docker: add global-store and split docker images (#19038) 2019-02-12 08:34:08 +01:00
fuse cmd/swarm, swarm/api/http, swarm/bmt, swarm/fuse, swarm/network/stream, swarm/storage, swarm/storage/encryption, swarm/testutil: use pseudo-random instead of crypto-random for test files content generation (#18083) 2018-11-14 09:21:14 +01:00
log swarm: network rewrite merge 2018-06-21 21:10:31 +02:00
metrics cmd/utils: allow for multiple influxdb tags (#18520) 2019-01-29 09:14:24 +01:00
network p2p, swarm: fix node up races by granular locking (#18976) 2019-02-18 07:38:14 +01:00
pot swarm/pot: each() functions refactored (#18452) 2019-01-15 11:51:33 +01:00
pss swarm/pss: refactoring (#19110) 2019-02-17 06:29:41 +01:00
sctx swarm: modify context key (#17925) 2018-11-06 09:54:19 +01:00
services/swap swarm: network rewrite merge 2018-06-21 21:10:31 +02:00
shed swarm: Shed Index and Uint64Field additions (#18398) 2019-01-07 13:20:11 +01:00
spancontext swarm: integrate OpenTracing; propagate ctx to internal APIs (#17169) 2018-07-13 17:40:28 +02:00
state p2p, swarm: fix node up races by granular locking (#18976) 2019-02-18 07:38:14 +01:00
storage swarm/storage: fix influxdb gc metrics report (#19102) 2019-02-15 07:41:42 +01:00
swap Accounting metrics reporter (#18136) 2018-11-26 17:05:18 +01:00
testutil cmd/swarm, swarm/api/http, swarm/bmt, swarm/fuse, swarm/network/stream, swarm/storage, swarm/storage/encryption, swarm/testutil: use pseudo-random instead of crypto-random for test files content generation (#18083) 2018-11-14 09:21:14 +01:00
tracing swarm/tracing: disable stdout logging for opentracing (#17931) 2018-10-17 14:46:59 +02:00
version params, swarm/version: Geth v1.9.0 unstable, Swarm v0.3.11-unstable 2019-01-29 17:43:13 +01:00
AUTHORS swarm: network rewrite merge 2018-06-21 21:10:31 +02:00
network_test.go swarm/network: Rename minproxbinsize, add as member of simulation (#18408) 2019-01-10 12:33:51 +01:00
OWNERS Remove multihash from Swarm bzz:// for Feeds (#18175) 2018-11-26 16:10:22 +01:00
README.md swarm/README: add more sections to easily onboard developers (#17333) 2018-08-07 12:56:01 +02:00
swarm_test.go swarm, swarm/storage: lower constants for faster tests (#17876) 2018-10-09 11:45:42 +02:00
swarm.go swarm: fix uptime gauge update goroutine leak by introducing cleanup functions (#19040) 2019-02-13 08:15:03 +01:00

Swarm

https://swarm.ethereum.org

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.

Travis Gitter

Table of Contents

Building the source

Building Swarm requires Go (version 1.10 or later).

go get -d github.com/ethereum/go-ethereum

go install github.com/ethereum/go-ethereum/cmd/swarm

Running Swarm

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.

To run Swarm you need an Ethereum account. You can create a new account by running the following command:

geth account new

You will be prompted for a password:

Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase:
Repeat passphrase:

Once you have specified the password, the output will be the Ethereum address representing that account. For example:

Address: {2f1cd699b0bf461dcfbf0098ad8f5587b038f0f1}

Using this account, connect to Swarm with

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.

Developers Guide

Go Environment

We assume that you have Go v1.10 installed, and GOPATH is set.

You must have your working copy under $GOPATH/src/github.com/ethereum/go-ethereum.

Most likely you will be working from your fork of go-ethereum, let's say from github.com/nirname/go-ethereum. Clone or move your fork into the right place:

git clone git@github.com:nirname/go-ethereum.git $GOPATH/src/github.com/ethereum/go-ethereum

Vendored Dependencies

All dependencies are tracked in the vendor directory. We use govendor to manage them.

If you want to add a new dependency, run govendor fetch <import-path>, then commit the result.

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:

go test -v -cpu 4 ./swarm/api

Note: Using options -cpu (number of cores allowed) and -v (logging even if no error) is recommended.

Testing only some methods:

go test -v -cpu 4 ./eth -run TestMethod

Note: here all tests with prefix TestMethod will be run, so if you got TestMethod, TestMethod1, then both!

Running benchmarks:

go test -v -cpu 4 -bench . -run BenchmarkJoin

Profiling Swarm

This section explains how to add Go pprof profiler to Swarm

If swarm is started with the --pprof option, a debugging HTTP server is made available on port 6060.

You can bring up http://localhost:6060/debug/pprof to see the heap, running routines etc.

By clicking full goroutine stack dump (clicking http://localhost:6060/debug/pprof/goroutine?debug=2) you can generate trace that is useful for debugging.

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.

# 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)

Visualizing metrics

Swarm supports an InfluxDB exporter. Consult the help section to learn about the command line arguments used to configure it:

swarm --help | grep metrics

We use Grafana and InfluxDB to visualise metrics reported by Swarm. We keep our Grafana dashboards under version control at ./swarm/grafana_dashboards. You could use them or design your own.

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.

Once you have stateth installed, and you have Docker running locally, you have to:

  1. Run stateth and keep it running in the background
stateth --rm --grafana-dashboards-folder $GOPATH/src/github.com/ethereum/go-ethereum/swarm/grafana_dashboards --influxdb-database metrics
  1. Run swarm with at least the following params:
--metrics \
--metrics.influxdb.export \
--metrics.influxdb.endpoint "http://localhost:8086" \
--metrics.influxdb.username "admin" \
--metrics.influxdb.password "admin" \
--metrics.influxdb.database "metrics"
  1. Open Grafana at http://localhost:3000 and view the dashboards to gain insight into Swarm.

Public Gateways

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.

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

Their source code can be found at: https://github.com/ethersphere/swarm-dapps

Contributing

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!

If you'd like to contribute to Swarm, please fork, fix, commit and send a pull request for the maintainers to review and merge into the main code base. If you wish to submit more complex changes though, please check up with the core devs first on our Swarm gitter channel to ensure those changes are in line with the general philosophy of the project and/or get some early feedback which can make both your efforts much lighter as well as our review and merge procedures quick and simple.

Please make sure your contributions adhere to our coding guidelines:

  • Code must adhere to the official Go formatting guidelines (i.e. uses gofmt).
  • Code must be documented adhering to the official Go commentary guidelines.
  • Pull requests need to be based on and opened against the master branch.
  • Code review guidelines.
  • Commit messages should be prefixed with the package(s) they modify.
    • E.g. "swarm/fuse: ignore default manifest entry"

License

The go-ethereum library (i.e. all code outside of the cmd directory) is licensed under the GNU Lesser General Public License v3.0, also included in our repository in the COPYING.LESSER file.

The go-ethereum binaries (i.e. all code inside of the cmd directory) is licensed under the GNU General Public License v3.0, also included in our repository in the COPYING file.