2020-02-13 17:27:33 +00:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
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 https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
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.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
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)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
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`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
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",
});
}
```
* Linting
2020-03-06 10:23:55 +00:00
|
|
|
"encoding/json"
|
2020-02-13 17:27:33 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-05-21 14:40:13 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-02-13 17:27:33 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
const eventsSchema = `
|
|
|
|
CREATE TABLE IF NOT EXISTS roomserver_events (
|
|
|
|
event_nid INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
room_nid INTEGER NOT NULL,
|
|
|
|
event_type_nid INTEGER NOT NULL,
|
|
|
|
event_state_key_nid INTEGER NOT NULL,
|
|
|
|
sent_to_output BOOLEAN NOT NULL DEFAULT FALSE,
|
|
|
|
state_snapshot_nid INTEGER NOT NULL DEFAULT 0,
|
|
|
|
depth INTEGER NOT NULL,
|
|
|
|
event_id TEXT NOT NULL UNIQUE,
|
|
|
|
reference_sha256 BLOB NOT NULL,
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
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 https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
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.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
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)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
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`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
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",
});
}
```
* Linting
2020-03-06 10:23:55 +00:00
|
|
|
auth_event_nids TEXT NOT NULL DEFAULT '[]'
|
2020-02-13 17:27:33 +00:00
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
const insertEventSQL = `
|
|
|
|
INSERT INTO roomserver_events (room_nid, event_type_nid, event_state_key_nid, event_id, reference_sha256, auth_event_nids, depth)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
`
|
|
|
|
|
|
|
|
const selectEventSQL = "" +
|
|
|
|
"SELECT event_nid, state_snapshot_nid FROM roomserver_events WHERE event_id = $1"
|
|
|
|
|
|
|
|
// Bulk lookup of events by string ID.
|
|
|
|
// Sort by the numeric IDs for event type and state key.
|
|
|
|
// This means we can use binary search to lookup entries by type and state key.
|
|
|
|
const bulkSelectStateEventByIDSQL = "" +
|
|
|
|
"SELECT event_type_nid, event_state_key_nid, event_nid FROM roomserver_events" +
|
|
|
|
" WHERE event_id IN ($1)" +
|
|
|
|
" ORDER BY event_type_nid, event_state_key_nid ASC"
|
|
|
|
|
|
|
|
const bulkSelectStateAtEventByIDSQL = "" +
|
|
|
|
"SELECT event_type_nid, event_state_key_nid, event_nid, state_snapshot_nid FROM roomserver_events" +
|
|
|
|
" WHERE event_id IN ($1)"
|
|
|
|
|
|
|
|
const updateEventStateSQL = "" +
|
|
|
|
"UPDATE roomserver_events SET state_snapshot_nid = $1 WHERE event_nid = $2"
|
|
|
|
|
|
|
|
const selectEventSentToOutputSQL = "" +
|
|
|
|
"SELECT sent_to_output FROM roomserver_events WHERE event_nid = $1"
|
|
|
|
|
|
|
|
const updateEventSentToOutputSQL = "" +
|
|
|
|
"UPDATE roomserver_events SET sent_to_output = TRUE WHERE event_nid = $1"
|
|
|
|
|
|
|
|
const selectEventIDSQL = "" +
|
|
|
|
"SELECT event_id FROM roomserver_events WHERE event_nid = $1"
|
|
|
|
|
|
|
|
const bulkSelectStateAtEventAndReferenceSQL = "" +
|
|
|
|
"SELECT event_type_nid, event_state_key_nid, event_nid, state_snapshot_nid, event_id, reference_sha256" +
|
|
|
|
" FROM roomserver_events WHERE event_nid IN ($1)"
|
|
|
|
|
|
|
|
const bulkSelectEventReferenceSQL = "" +
|
|
|
|
"SELECT event_id, reference_sha256 FROM roomserver_events WHERE event_nid IN ($1)"
|
|
|
|
|
|
|
|
const bulkSelectEventIDSQL = "" +
|
|
|
|
"SELECT event_nid, event_id FROM roomserver_events WHERE event_nid IN ($1)"
|
|
|
|
|
|
|
|
const bulkSelectEventNIDSQL = "" +
|
|
|
|
"SELECT event_id, event_nid FROM roomserver_events WHERE event_id IN ($1)"
|
|
|
|
|
|
|
|
const selectMaxEventDepthSQL = "" +
|
|
|
|
"SELECT COALESCE(MAX(depth) + 1, 0) FROM roomserver_events WHERE event_nid IN ($1)"
|
|
|
|
|
2020-03-27 16:28:22 +00:00
|
|
|
const selectRoomNIDForEventNIDSQL = "" +
|
|
|
|
"SELECT room_nid FROM roomserver_events WHERE event_nid = $1"
|
|
|
|
|
2020-02-13 17:27:33 +00:00
|
|
|
type eventStatements struct {
|
|
|
|
db *sql.DB
|
|
|
|
insertEventStmt *sql.Stmt
|
|
|
|
selectEventStmt *sql.Stmt
|
|
|
|
bulkSelectStateEventByIDStmt *sql.Stmt
|
|
|
|
bulkSelectStateAtEventByIDStmt *sql.Stmt
|
|
|
|
updateEventStateStmt *sql.Stmt
|
|
|
|
selectEventSentToOutputStmt *sql.Stmt
|
|
|
|
updateEventSentToOutputStmt *sql.Stmt
|
|
|
|
selectEventIDStmt *sql.Stmt
|
|
|
|
bulkSelectStateAtEventAndReferenceStmt *sql.Stmt
|
|
|
|
bulkSelectEventReferenceStmt *sql.Stmt
|
|
|
|
bulkSelectEventIDStmt *sql.Stmt
|
|
|
|
bulkSelectEventNIDStmt *sql.Stmt
|
2020-03-27 16:28:22 +00:00
|
|
|
selectRoomNIDForEventNIDStmt *sql.Stmt
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *eventStatements) prepare(db *sql.DB) (err error) {
|
|
|
|
s.db = db
|
|
|
|
_, err = db.Exec(eventsSchema)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return statementList{
|
|
|
|
{&s.insertEventStmt, insertEventSQL},
|
|
|
|
{&s.selectEventStmt, selectEventSQL},
|
|
|
|
{&s.bulkSelectStateEventByIDStmt, bulkSelectStateEventByIDSQL},
|
|
|
|
{&s.bulkSelectStateAtEventByIDStmt, bulkSelectStateAtEventByIDSQL},
|
|
|
|
{&s.updateEventStateStmt, updateEventStateSQL},
|
|
|
|
{&s.updateEventSentToOutputStmt, updateEventSentToOutputSQL},
|
|
|
|
{&s.selectEventSentToOutputStmt, selectEventSentToOutputSQL},
|
|
|
|
{&s.selectEventIDStmt, selectEventIDSQL},
|
|
|
|
{&s.bulkSelectStateAtEventAndReferenceStmt, bulkSelectStateAtEventAndReferenceSQL},
|
|
|
|
{&s.bulkSelectEventReferenceStmt, bulkSelectEventReferenceSQL},
|
|
|
|
{&s.bulkSelectEventIDStmt, bulkSelectEventIDSQL},
|
|
|
|
{&s.bulkSelectEventNIDStmt, bulkSelectEventNIDSQL},
|
2020-03-27 16:28:22 +00:00
|
|
|
{&s.selectRoomNIDForEventNIDStmt, selectRoomNIDForEventNIDSQL},
|
2020-02-13 17:27:33 +00:00
|
|
|
}.prepare(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *eventStatements) insertEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
eventTypeNID types.EventTypeNID,
|
|
|
|
eventStateKeyNID types.EventStateKeyNID,
|
|
|
|
eventID string,
|
|
|
|
referenceSHA256 []byte,
|
|
|
|
authEventNIDs []types.EventNID,
|
|
|
|
depth int64,
|
2020-04-29 18:41:45 +01:00
|
|
|
) (types.EventNID, error) {
|
|
|
|
// attempt to insert: the last_row_id is the event NID
|
2020-05-21 14:40:13 +01:00
|
|
|
insertStmt := internal.TxStmt(txn, s.insertEventStmt)
|
2020-04-29 18:41:45 +01:00
|
|
|
result, err := insertStmt.ExecContext(
|
2020-02-13 17:27:33 +00:00
|
|
|
ctx, int64(roomNID), int64(eventTypeNID), int64(eventStateKeyNID),
|
|
|
|
eventID, referenceSHA256, eventNIDsAsArray(authEventNIDs), depth,
|
2020-04-29 18:41:45 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
modified, err := result.RowsAffected()
|
|
|
|
if modified == 0 && err == nil {
|
|
|
|
return 0, sql.ErrNoRows
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
2020-04-29 18:41:45 +01:00
|
|
|
eventNID, err := result.LastInsertId()
|
|
|
|
return types.EventNID(eventNID), err
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *eventStatements) selectEvent(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventID string,
|
|
|
|
) (types.EventNID, types.StateSnapshotNID, error) {
|
|
|
|
var eventNID int64
|
|
|
|
var stateNID int64
|
2020-05-21 14:40:13 +01:00
|
|
|
selectStmt := internal.TxStmt(txn, s.selectEventStmt)
|
2020-02-13 17:27:33 +00:00
|
|
|
err := selectStmt.QueryRowContext(ctx, eventID).Scan(&eventNID, &stateNID)
|
|
|
|
return types.EventNID(eventNID), types.StateSnapshotNID(stateNID), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// bulkSelectStateEventByID lookups a list of state events by event ID.
|
|
|
|
// If any of the requested events are missing from the database it returns a types.MissingEventError
|
|
|
|
func (s *eventStatements) bulkSelectStateEventByID(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventIDs []string,
|
|
|
|
) ([]types.StateEntry, error) {
|
|
|
|
///////////////
|
|
|
|
iEventIDs := make([]interface{}, len(eventIDs))
|
|
|
|
for k, v := range eventIDs {
|
|
|
|
iEventIDs[k] = v
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
selectOrig := strings.Replace(bulkSelectStateEventByIDSQL, "($1)", internal.QueryVariadic(len(iEventIDs)), 1)
|
2020-02-13 17:27:33 +00:00
|
|
|
selectPrep, err := txn.Prepare(selectOrig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
///////////////
|
|
|
|
|
2020-05-21 14:40:13 +01:00
|
|
|
selectStmt := internal.TxStmt(txn, selectPrep)
|
2020-02-13 17:27:33 +00:00
|
|
|
rows, err := selectStmt.QueryContext(ctx, iEventIDs...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectStateEventByID: rows.close() failed")
|
2020-02-13 17:27:33 +00:00
|
|
|
// We know that we will only get as many results as event IDs
|
|
|
|
// because of the unique constraint on event IDs.
|
|
|
|
// So we can allocate an array of the correct size now.
|
|
|
|
// We might get fewer results than IDs so we adjust the length of the slice before returning it.
|
|
|
|
results := make([]types.StateEntry, len(eventIDs))
|
|
|
|
i := 0
|
|
|
|
for ; rows.Next(); i++ {
|
|
|
|
result := &results[i]
|
|
|
|
if err = rows.Scan(
|
|
|
|
&result.EventTypeNID,
|
|
|
|
&result.EventStateKeyNID,
|
|
|
|
&result.EventNID,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i != len(eventIDs) {
|
|
|
|
// If there are fewer rows returned than IDs then we were asked to lookup event IDs we don't have.
|
|
|
|
// We don't know which ones were missing because we don't return the string IDs in the query.
|
|
|
|
// However it should be possible debug this by replaying queries or entries from the input kafka logs.
|
|
|
|
// If this turns out to be impossible and we do need the debug information here, it would be better
|
2020-05-21 14:40:13 +01:00
|
|
|
// to do it as a separate query rather than slowing down/complicating the internal case.
|
2020-02-13 17:27:33 +00:00
|
|
|
return nil, types.MissingEventError(
|
|
|
|
fmt.Sprintf("storage: state event IDs missing from the database (%d != %d)", i, len(eventIDs)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// bulkSelectStateAtEventByID lookups the state at a list of events by event ID.
|
|
|
|
// If any of the requested events are missing from the database it returns a types.MissingEventError.
|
|
|
|
// If we do not have the state for any of the requested events it returns a types.MissingEventError.
|
|
|
|
func (s *eventStatements) bulkSelectStateAtEventByID(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventIDs []string,
|
|
|
|
) ([]types.StateAtEvent, error) {
|
|
|
|
///////////////
|
|
|
|
iEventIDs := make([]interface{}, len(eventIDs))
|
|
|
|
for k, v := range eventIDs {
|
|
|
|
iEventIDs[k] = v
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
selectOrig := strings.Replace(bulkSelectStateAtEventByIDSQL, "($1)", internal.QueryVariadic(len(iEventIDs)), 1)
|
2020-02-13 17:27:33 +00:00
|
|
|
selectPrep, err := txn.Prepare(selectOrig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
///////////////
|
|
|
|
|
2020-05-21 14:40:13 +01:00
|
|
|
selectStmt := internal.TxStmt(txn, selectPrep)
|
2020-02-13 17:27:33 +00:00
|
|
|
rows, err := selectStmt.QueryContext(ctx, iEventIDs...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectStateAtEventByID: rows.close() failed")
|
2020-02-13 17:27:33 +00:00
|
|
|
results := make([]types.StateAtEvent, len(eventIDs))
|
|
|
|
i := 0
|
|
|
|
for ; rows.Next(); i++ {
|
|
|
|
result := &results[i]
|
|
|
|
if err = rows.Scan(
|
|
|
|
&result.EventTypeNID,
|
|
|
|
&result.EventStateKeyNID,
|
|
|
|
&result.EventNID,
|
|
|
|
&result.BeforeStateSnapshotNID,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if result.BeforeStateSnapshotNID == 0 {
|
|
|
|
return nil, types.MissingEventError(
|
|
|
|
fmt.Sprintf("storage: missing state for event NID %d", result.EventNID),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i != len(eventIDs) {
|
|
|
|
return nil, types.MissingEventError(
|
|
|
|
fmt.Sprintf("storage: event IDs missing from the database (%d != %d)", i, len(eventIDs)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *eventStatements) updateEventState(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventNID types.EventNID, stateNID types.StateSnapshotNID,
|
|
|
|
) error {
|
2020-05-21 14:40:13 +01:00
|
|
|
updateStmt := internal.TxStmt(txn, s.updateEventStateStmt)
|
2020-02-13 17:27:33 +00:00
|
|
|
_, err := updateStmt.ExecContext(ctx, int64(stateNID), int64(eventNID))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *eventStatements) selectEventSentToOutput(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventNID types.EventNID,
|
|
|
|
) (sentToOutput bool, err error) {
|
2020-05-21 14:40:13 +01:00
|
|
|
selectStmt := internal.TxStmt(txn, s.selectEventSentToOutputStmt)
|
2020-02-13 17:27:33 +00:00
|
|
|
err = selectStmt.QueryRowContext(ctx, int64(eventNID)).Scan(&sentToOutput)
|
|
|
|
//err = s.selectEventSentToOutputStmt.QueryRowContext(ctx, int64(eventNID)).Scan(&sentToOutput)
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *eventStatements) updateEventSentToOutput(ctx context.Context, txn *sql.Tx, eventNID types.EventNID) error {
|
2020-05-21 14:40:13 +01:00
|
|
|
updateStmt := internal.TxStmt(txn, s.updateEventSentToOutputStmt)
|
2020-02-13 17:27:33 +00:00
|
|
|
_, err := updateStmt.ExecContext(ctx, int64(eventNID))
|
|
|
|
//_, err := s.updateEventSentToOutputStmt.ExecContext(ctx, int64(eventNID))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *eventStatements) selectEventID(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventNID types.EventNID,
|
|
|
|
) (eventID string, err error) {
|
2020-05-21 14:40:13 +01:00
|
|
|
selectStmt := internal.TxStmt(txn, s.selectEventIDStmt)
|
2020-02-13 17:27:33 +00:00
|
|
|
err = selectStmt.QueryRowContext(ctx, int64(eventNID)).Scan(&eventID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *eventStatements) bulkSelectStateAtEventAndReference(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID,
|
|
|
|
) ([]types.StateAtEventAndReference, error) {
|
|
|
|
///////////////
|
|
|
|
iEventNIDs := make([]interface{}, len(eventNIDs))
|
|
|
|
for k, v := range eventNIDs {
|
|
|
|
iEventNIDs[k] = v
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
selectOrig := strings.Replace(bulkSelectStateAtEventAndReferenceSQL, "($1)", internal.QueryVariadic(len(iEventNIDs)), 1)
|
2020-02-13 17:27:33 +00:00
|
|
|
//////////////
|
|
|
|
|
|
|
|
rows, err := txn.QueryContext(ctx, selectOrig, iEventNIDs...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectStateAtEventAndReference: rows.close() failed")
|
2020-02-13 17:27:33 +00:00
|
|
|
results := make([]types.StateAtEventAndReference, len(eventNIDs))
|
|
|
|
i := 0
|
|
|
|
for ; rows.Next(); i++ {
|
|
|
|
var (
|
|
|
|
eventTypeNID int64
|
|
|
|
eventStateKeyNID int64
|
|
|
|
eventNID int64
|
|
|
|
stateSnapshotNID int64
|
|
|
|
eventID string
|
|
|
|
eventSHA256 []byte
|
|
|
|
)
|
|
|
|
if err = rows.Scan(
|
|
|
|
&eventTypeNID, &eventStateKeyNID, &eventNID, &stateSnapshotNID, &eventID, &eventSHA256,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := &results[i]
|
|
|
|
result.EventTypeNID = types.EventTypeNID(eventTypeNID)
|
|
|
|
result.EventStateKeyNID = types.EventStateKeyNID(eventStateKeyNID)
|
|
|
|
result.EventNID = types.EventNID(eventNID)
|
|
|
|
result.BeforeStateSnapshotNID = types.StateSnapshotNID(stateSnapshotNID)
|
|
|
|
result.EventID = eventID
|
|
|
|
result.EventSHA256 = eventSHA256
|
|
|
|
}
|
|
|
|
if i != len(eventNIDs) {
|
|
|
|
return nil, fmt.Errorf("storage: event NIDs missing from the database (%d != %d)", i, len(eventNIDs))
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *eventStatements) bulkSelectEventReference(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID,
|
|
|
|
) ([]gomatrixserverlib.EventReference, error) {
|
|
|
|
///////////////
|
|
|
|
iEventNIDs := make([]interface{}, len(eventNIDs))
|
|
|
|
for k, v := range eventNIDs {
|
|
|
|
iEventNIDs[k] = v
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
selectOrig := strings.Replace(bulkSelectEventReferenceSQL, "($1)", internal.QueryVariadic(len(iEventNIDs)), 1)
|
2020-02-13 17:27:33 +00:00
|
|
|
selectPrep, err := txn.Prepare(selectOrig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
///////////////
|
|
|
|
|
2020-05-21 14:40:13 +01:00
|
|
|
selectStmt := internal.TxStmt(txn, selectPrep)
|
2020-02-13 17:27:33 +00:00
|
|
|
rows, err := selectStmt.QueryContext(ctx, iEventNIDs...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventReference: rows.close() failed")
|
2020-02-13 17:27:33 +00:00
|
|
|
results := make([]gomatrixserverlib.EventReference, len(eventNIDs))
|
|
|
|
i := 0
|
|
|
|
for ; rows.Next(); i++ {
|
|
|
|
result := &results[i]
|
|
|
|
if err = rows.Scan(&result.EventID, &result.EventSHA256); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i != len(eventNIDs) {
|
|
|
|
return nil, fmt.Errorf("storage: event NIDs missing from the database (%d != %d)", i, len(eventNIDs))
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bulkSelectEventID returns a map from numeric event ID to string event ID.
|
|
|
|
func (s *eventStatements) bulkSelectEventID(ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID) (map[types.EventNID]string, error) {
|
|
|
|
///////////////
|
|
|
|
iEventNIDs := make([]interface{}, len(eventNIDs))
|
|
|
|
for k, v := range eventNIDs {
|
|
|
|
iEventNIDs[k] = v
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
selectOrig := strings.Replace(bulkSelectEventIDSQL, "($1)", internal.QueryVariadic(len(iEventNIDs)), 1)
|
2020-02-13 17:27:33 +00:00
|
|
|
selectPrep, err := txn.Prepare(selectOrig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
///////////////
|
|
|
|
|
2020-05-21 14:40:13 +01:00
|
|
|
selectStmt := internal.TxStmt(txn, selectPrep)
|
2020-02-13 17:27:33 +00:00
|
|
|
rows, err := selectStmt.QueryContext(ctx, iEventNIDs...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventID: rows.close() failed")
|
2020-02-13 17:27:33 +00:00
|
|
|
results := make(map[types.EventNID]string, len(eventNIDs))
|
|
|
|
i := 0
|
|
|
|
for ; rows.Next(); i++ {
|
|
|
|
var eventNID int64
|
|
|
|
var eventID string
|
|
|
|
if err = rows.Scan(&eventNID, &eventID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results[types.EventNID(eventNID)] = eventID
|
|
|
|
}
|
|
|
|
if i != len(eventNIDs) {
|
|
|
|
return nil, fmt.Errorf("storage: event NIDs missing from the database (%d != %d)", i, len(eventNIDs))
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bulkSelectEventNIDs returns a map from string event ID to numeric event ID.
|
|
|
|
// If an event ID is not in the database then it is omitted from the map.
|
|
|
|
func (s *eventStatements) bulkSelectEventNID(ctx context.Context, txn *sql.Tx, eventIDs []string) (map[string]types.EventNID, error) {
|
|
|
|
///////////////
|
|
|
|
iEventIDs := make([]interface{}, len(eventIDs))
|
|
|
|
for k, v := range eventIDs {
|
|
|
|
iEventIDs[k] = v
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
selectOrig := strings.Replace(bulkSelectEventNIDSQL, "($1)", internal.QueryVariadic(len(iEventIDs)), 1)
|
2020-02-13 17:27:33 +00:00
|
|
|
selectPrep, err := txn.Prepare(selectOrig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
///////////////
|
|
|
|
|
2020-05-21 14:40:13 +01:00
|
|
|
selectStmt := internal.TxStmt(txn, selectPrep)
|
2020-02-13 17:27:33 +00:00
|
|
|
rows, err := selectStmt.QueryContext(ctx, iEventIDs...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectEventNID: rows.close() failed")
|
2020-02-13 17:27:33 +00:00
|
|
|
results := make(map[string]types.EventNID, len(eventIDs))
|
|
|
|
for rows.Next() {
|
|
|
|
var eventID string
|
|
|
|
var eventNID int64
|
|
|
|
if err = rows.Scan(&eventID, &eventNID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results[eventID] = types.EventNID(eventNID)
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *eventStatements) selectMaxEventDepth(ctx context.Context, txn *sql.Tx, eventNIDs []types.EventNID) (int64, error) {
|
|
|
|
var result int64
|
2020-03-06 14:31:12 +00:00
|
|
|
iEventIDs := make([]interface{}, len(eventNIDs))
|
|
|
|
for i, v := range eventNIDs {
|
|
|
|
iEventIDs[i] = v
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
sqlStr := strings.Replace(selectMaxEventDepthSQL, "($1)", internal.QueryVariadic(len(iEventIDs)), 1)
|
2020-03-06 14:31:12 +00:00
|
|
|
err := txn.QueryRowContext(ctx, sqlStr, iEventIDs...).Scan(&result)
|
2020-02-13 17:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-03-27 16:28:22 +00:00
|
|
|
func (s *eventStatements) selectRoomNIDForEventNID(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventNID types.EventNID,
|
|
|
|
) (roomNID types.RoomNID, err error) {
|
2020-05-21 14:40:13 +01:00
|
|
|
selectStmt := internal.TxStmt(txn, s.selectRoomNIDForEventNIDStmt)
|
2020-03-27 16:28:22 +00:00
|
|
|
err = selectStmt.QueryRowContext(ctx, int64(eventNID)).Scan(&roomNID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
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 https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
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.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
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)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
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`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
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",
});
}
```
* Linting
2020-03-06 10:23:55 +00:00
|
|
|
func eventNIDsAsArray(eventNIDs []types.EventNID) string {
|
|
|
|
b, _ := json.Marshal(eventNIDs)
|
|
|
|
return string(b)
|
2020-02-13 17:27:33 +00:00
|
|
|
}
|