2023-01-23 17:55:12 +00:00
|
|
|
// Copyright 2022 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 internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/federationapi/storage/shared/receipt"
|
|
|
|
"github.com/matrix-org/dendrite/internal"
|
|
|
|
"github.com/matrix-org/dendrite/relayapi/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 09:55:01 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2023-01-23 17:55:12 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-01-29 19:26:16 +00:00
|
|
|
// SetRelayingEnabled implements api.RelayInternalAPI
|
|
|
|
func (r *RelayInternalAPI) SetRelayingEnabled(enabled bool) {
|
|
|
|
r.relayingEnabledMutex.Lock()
|
|
|
|
defer r.relayingEnabledMutex.Unlock()
|
|
|
|
r.relayingEnabled = enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
// RelayingEnabled implements api.RelayInternalAPI
|
|
|
|
func (r *RelayInternalAPI) RelayingEnabled() bool {
|
|
|
|
r.relayingEnabledMutex.Lock()
|
|
|
|
defer r.relayingEnabledMutex.Unlock()
|
|
|
|
return r.relayingEnabled
|
|
|
|
}
|
|
|
|
|
2023-01-23 17:55:12 +00:00
|
|
|
// PerformRelayServerSync implements api.RelayInternalAPI
|
|
|
|
func (r *RelayInternalAPI) PerformRelayServerSync(
|
|
|
|
ctx context.Context,
|
2023-04-19 15:50:33 +01:00
|
|
|
userID spec.UserID,
|
|
|
|
relayServer spec.ServerName,
|
2023-01-23 17:55:12 +00:00
|
|
|
) error {
|
|
|
|
// Providing a default RelayEntry (EntryID = 0) is done to ask the relay if there are any
|
|
|
|
// transactions available for this node.
|
2023-04-06 09:55:01 +01:00
|
|
|
prevEntry := fclient.RelayEntry{}
|
2023-01-23 17:55:12 +00:00
|
|
|
asyncResponse, err := r.fedClient.P2PGetTransactionFromRelay(ctx, userID, prevEntry, relayServer)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("P2PGetTransactionFromRelay: %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2023-01-31 19:31:57 +00:00
|
|
|
r.processTransaction(&asyncResponse.Transaction)
|
2023-01-23 17:55:12 +00:00
|
|
|
|
2023-04-06 09:55:01 +01:00
|
|
|
prevEntry = fclient.RelayEntry{EntryID: asyncResponse.EntryID}
|
2023-01-23 17:55:12 +00:00
|
|
|
for asyncResponse.EntriesQueued {
|
|
|
|
// There are still more entries available for this node from the relay.
|
|
|
|
logrus.Infof("Retrieving next entry from relay, previous: %v", prevEntry)
|
|
|
|
asyncResponse, err = r.fedClient.P2PGetTransactionFromRelay(ctx, userID, prevEntry, relayServer)
|
2023-04-06 09:55:01 +01:00
|
|
|
prevEntry = fclient.RelayEntry{EntryID: asyncResponse.EntryID}
|
2023-01-23 17:55:12 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("P2PGetTransactionFromRelay: %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2023-01-31 19:31:57 +00:00
|
|
|
r.processTransaction(&asyncResponse.Transaction)
|
2023-01-23 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PerformStoreTransaction implements api.RelayInternalAPI
|
|
|
|
func (r *RelayInternalAPI) PerformStoreTransaction(
|
|
|
|
ctx context.Context,
|
|
|
|
transaction gomatrixserverlib.Transaction,
|
2023-04-19 15:50:33 +01:00
|
|
|
userID spec.UserID,
|
2023-01-23 17:55:12 +00:00
|
|
|
) error {
|
|
|
|
logrus.Warnf("Storing transaction for %v", userID)
|
|
|
|
receipt, err := r.db.StoreTransaction(ctx, transaction)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("db.StoreTransaction: %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = r.db.AssociateTransactionWithDestinations(
|
|
|
|
ctx,
|
2023-04-19 15:50:33 +01:00
|
|
|
map[spec.UserID]struct{}{
|
2023-01-23 17:55:12 +00:00
|
|
|
userID: {},
|
|
|
|
},
|
|
|
|
transaction.TransactionID,
|
|
|
|
receipt)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryTransactions implements api.RelayInternalAPI
|
|
|
|
func (r *RelayInternalAPI) QueryTransactions(
|
|
|
|
ctx context.Context,
|
2023-04-19 15:50:33 +01:00
|
|
|
userID spec.UserID,
|
2023-04-06 09:55:01 +01:00
|
|
|
previousEntry fclient.RelayEntry,
|
2023-01-23 17:55:12 +00:00
|
|
|
) (api.QueryRelayTransactionsResponse, error) {
|
2023-04-28 20:31:21 +01:00
|
|
|
logrus.Infof("QueryTransactions for %s", userID.String())
|
2023-01-23 17:55:12 +00:00
|
|
|
if previousEntry.EntryID > 0 {
|
|
|
|
logrus.Infof("Cleaning previous entry (%v) from db for %s",
|
|
|
|
previousEntry.EntryID,
|
2023-04-28 20:31:21 +01:00
|
|
|
userID.String(),
|
2023-01-23 17:55:12 +00:00
|
|
|
)
|
|
|
|
prevReceipt := receipt.NewReceipt(previousEntry.EntryID)
|
|
|
|
err := r.db.CleanTransactions(ctx, userID, []*receipt.Receipt{&prevReceipt})
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("db.CleanTransactions: %s", err.Error())
|
|
|
|
return api.QueryRelayTransactionsResponse{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction, receipt, err := r.db.GetTransaction(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("db.GetTransaction: %s", err.Error())
|
|
|
|
return api.QueryRelayTransactionsResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := api.QueryRelayTransactionsResponse{}
|
|
|
|
if transaction != nil && receipt != nil {
|
2023-04-28 20:31:21 +01:00
|
|
|
logrus.Infof("Obtained transaction (%v) for %s", transaction.TransactionID, userID.String())
|
2023-01-23 17:55:12 +00:00
|
|
|
response.Transaction = *transaction
|
|
|
|
response.EntryID = receipt.GetNID()
|
|
|
|
response.EntriesQueued = true
|
|
|
|
} else {
|
2023-04-28 20:31:21 +01:00
|
|
|
logrus.Infof("No more entries in the queue for %s", userID.String())
|
2023-01-23 17:55:12 +00:00
|
|
|
response.EntryID = 0
|
|
|
|
response.EntriesQueued = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RelayInternalAPI) processTransaction(txn *gomatrixserverlib.Transaction) {
|
|
|
|
logrus.Warn("Processing transaction from relay server")
|
|
|
|
mu := internal.NewMutexByRoom()
|
|
|
|
t := internal.NewTxnReq(
|
|
|
|
r.rsAPI,
|
|
|
|
nil,
|
|
|
|
r.serverName,
|
|
|
|
r.keyRing,
|
|
|
|
mu,
|
|
|
|
r.producer,
|
|
|
|
r.presenceEnabledInbound,
|
|
|
|
txn.PDUs,
|
|
|
|
txn.EDUs,
|
|
|
|
txn.Origin,
|
|
|
|
txn.TransactionID,
|
|
|
|
txn.Destination)
|
|
|
|
|
|
|
|
t.ProcessTransaction(context.TODO())
|
|
|
|
}
|