Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add peer-to-peer support into Dendrite via libp2p and fetch #880

Merged
merged 33 commits into from
Mar 6, 2020

Conversation

kegsay
Copy link
Member

@kegsay kegsay commented Mar 5, 2020

This PR adds in support for running peer-to-peer Dendrite instances.

Overview

  • Dendrite can now compile to WebAssembly. $ GOOS=js GOARCH=wasm go build -o dendrite.wasm ./cmd/dendritejs
  • Dendrite will listen for function calls from JS-land for incoming traffic (both local and federation). These function calls are called by accompanying JS-Go bridge code located at https://github.com/matrix-org/go-http-js-libp2p
  • Dendrite will use a new SQL driver when running in JS: https://github.com/matrix-org/go-sqlite3-js
  • There is now an in-memory LRU cache around the KeyDB. This is required for p2p (though all implementations benefit) in order to keep federation traffic below the threshold which would cause us to be blacklisted (denied) on the relay server.
  • There's a new document p2p.md which contains instructions on how to set up a complete peer (including a Riot instance).

Breaking changes

Previously, tracing defaulted to on. Now it is off by default. Set tracing.enabled = true in the config YAML to re-enable it.

Notes

  • We now conditionally compile the storage.go entry point for all microservices. These files only contain a single NewWhateverDatabase function, so the code duplication is minor. For WebAssembly, this means running storage_wasm.go. This is required in order to remove a hard dependency on lib/pq which does not compile under WASM. Around 700 lines of this diff is doing this refactoring.
  • We now explicitly JSON serialise "arrays" when working under SQLite. Previously, we relied on pq.StringArray to do this serialisation, which as the above point makes, we cannot do anymore.

Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.

See tidwall/gjson#157

When it's resolved, let's go back to mainline.
Caused when sending events over federation.
Partly so I don't forget myself!
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.

Also, clean up main.go to read a bit better.
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.

Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:

```
    global.process = {
        pid: 1,
    };
    global.fs.stat = function(path, cb) {
        cb({
            code: "EINVAL",
        });
    }
```
@kegsay kegsay changed the title WIP: Add peer-to-peer support into Dendrite via libp2p and fetch Add peer-to-peer support into Dendrite via libp2p and fetch Mar 5, 2020
Copy link
Contributor

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after the lint errors are resolved!

@kegsay
Copy link
Member Author

kegsay commented Mar 6, 2020

The failing tests are SQLite only - which is a known issue I'm going to look into today!

@kegsay kegsay merged commit a97b8ea into master Mar 6, 2020
@kegsay kegsay deleted the kegan/wasm branch March 6, 2020 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants