fix(amm): reload changed sequencer configuration

This commit is contained in:
Ricardo Guilherme Schmidt 2026-07-17 19:48:50 -03:00
parent ba23429fc4
commit 1d1db9ba4e
No known key found for this signature in database
GPG Key ID: 1396EA17DE132FFE
4 changed files with 67 additions and 31 deletions

View File

@ -217,10 +217,7 @@ void AmmUiBackend::syncWalletState()
setSequencerAddr(state.sequencerAddress);
setSequencerReachable(state.sequencerReachable);
if (m_sequencerConfigPath != state.configPath || !m_sequencer->isConfigured()) {
m_sequencerConfigPath = state.configPath;
m_sequencer->configure(state.configPath);
}
m_sequencer->configure(state.configPath);
const bool addressChanged = previousAddress != state.sequencerAddress;
if (addressChanged) {

View File

@ -78,7 +78,6 @@ private:
ActiveNetwork m_network;
QVariantMap m_newPositionHints;
QString m_sequencerConfigPath;
bool m_identityProbeInFlight = false;
quint64 m_contextGeneration = 0;
struct PendingTransaction {

View File

@ -82,36 +82,40 @@ SequencerClient::SequencerClient(AmmClient* client, QObject* parent)
bool SequencerClient::configure(const QString& configPath)
{
QUrl endpoint;
QByteArray authorization;
QFile file(configPath);
bool valid = file.open(QIODevice::ReadOnly);
if (valid) {
const QJsonDocument document = QJsonDocument::fromJson(file.readAll());
const QJsonObject config = document.object();
endpoint = QUrl(config.value(QStringLiteral("sequencer_addr")).toString());
const QString scheme = endpoint.scheme().toLower();
valid = document.isObject() && endpoint.isValid() && !endpoint.host().isEmpty()
&& (scheme == QStringLiteral("http") || scheme == QStringLiteral("https"));
if (valid) {
const QJsonObject basicAuth = config.value(QStringLiteral("basic_auth")).toObject();
const QString username = basicAuth.value(QStringLiteral("username")).toString();
const QString password = basicAuth.value(QStringLiteral("password")).toString();
if (!username.isEmpty()) {
authorization = QByteArrayLiteral("Basic ")
+ (username + QLatin1Char(':') + password).toUtf8().toBase64();
}
}
}
if (!valid) {
endpoint = QUrl();
authorization.clear();
}
if (endpoint == m_endpoint && authorization == m_authorization)
return valid;
++m_generation;
cancelPendingReads();
clear();
QFile file(configPath);
if (!file.open(QIODevice::ReadOnly)) {
m_endpoint = QUrl();
m_authorization.clear();
return false;
}
const QJsonDocument document = QJsonDocument::fromJson(file.readAll());
const QJsonObject config = document.object();
const QUrl endpoint(config.value(QStringLiteral("sequencer_addr")).toString());
const QString scheme = endpoint.scheme().toLower();
if (!document.isObject() || !endpoint.isValid() || endpoint.host().isEmpty()
|| (scheme != QStringLiteral("http") && scheme != QStringLiteral("https"))) {
m_endpoint = QUrl();
m_authorization.clear();
return false;
}
m_endpoint = endpoint;
m_authorization.clear();
const QJsonObject basicAuth = config.value(QStringLiteral("basic_auth")).toObject();
const QString username = basicAuth.value(QStringLiteral("username")).toString();
const QString password = basicAuth.value(QStringLiteral("password")).toString();
if (!username.isEmpty()) {
m_authorization = QByteArrayLiteral("Basic ")
+ (username + QLatin1Char(':') + password).toUtf8().toBase64();
}
return true;
m_authorization = authorization;
return valid;
}
void SequencerClient::readAccounts(const QStringList& accountIds,

View File

@ -445,5 +445,41 @@ int main(int argc, char** argv)
"holding read should recover from the sequencer"))
return 1;
const int requestsBeforeUnchangedConfig = server.requestCount();
if (!expect(sequencer.configure(sequencerConfig.fileName()),
"unchanged sequencer config should remain valid"))
return 1;
QVector<WalletAccountRead> unchangedConfigRead;
if (!expect(waitForAccounts(sequencer, { holding.address }, false,
&unchangedConfigRead),
"cached read after unchanged config should complete"))
return 1;
if (!expect(server.requestCount() == requestsBeforeUnchangedConfig,
"unchanged config should preserve the account cache"))
return 1;
LocalRpcServer replacementServer;
if (!expect(replacementServer.listen(),
"replacement sequencer should listen"))
return 1;
if (!expect(sequencerConfig.resize(0) && sequencerConfig.seek(0),
"sequencer config should rewind"))
return 1;
sequencerConfig.write(QJsonDocument(QJsonObject {
{ QStringLiteral("sequencer_addr"), replacementServer.endpoint() },
}).toJson(QJsonDocument::Compact));
sequencerConfig.flush();
if (!expect(sequencer.configure(sequencerConfig.fileName()),
"same-path endpoint update should configure"))
return 1;
QVector<WalletAccountRead> replacementRead;
if (!expect(waitForAccounts(sequencer, { holding.address }, false,
&replacementRead),
"same-path endpoint read should complete"))
return 1;
if (!expect(replacementServer.requestCount() == 1,
"same-path endpoint update should clear cache and use new sequencer"))
return 1;
return 0;
}