mirror of
https://github.com/logos-blockchain/lez-programs.git
synced 2026-07-21 06:19:32 +00:00
fix(amm): align reads with adopted wallet endpoint
This commit is contained in:
parent
cfaaba3bce
commit
8023195ec8
@ -210,7 +210,7 @@ void AmmUiBackend::syncWalletState()
|
|||||||
setSequencerAddr(state.sequencerAddress);
|
setSequencerAddr(state.sequencerAddress);
|
||||||
setSequencerReachable(state.sequencerReachable);
|
setSequencerReachable(state.sequencerReachable);
|
||||||
|
|
||||||
m_sequencer->configure(state.configPath);
|
m_sequencer->configure(state.configPath, state.sequencerAddress);
|
||||||
|
|
||||||
const bool addressChanged = previousAddress != state.sequencerAddress;
|
const bool addressChanged = previousAddress != state.sequencerAddress;
|
||||||
if ((walletCouldSubmit && !state.canSubmit()) || addressChanged)
|
if ((walletCouldSubmit && !state.canSubmit()) || addressChanged)
|
||||||
|
|||||||
@ -80,20 +80,24 @@ SequencerClient::SequencerClient(AmmClient* client, QObject* parent)
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SequencerClient::configure(const QString& configPath)
|
bool SequencerClient::configure(const QString& configPath,
|
||||||
|
const QString& effectiveEndpoint)
|
||||||
{
|
{
|
||||||
QUrl endpoint;
|
QUrl configuredEndpoint;
|
||||||
QByteArray authorization;
|
QByteArray authorization;
|
||||||
QFile file(configPath);
|
QFile file(configPath);
|
||||||
bool valid = file.open(QIODevice::ReadOnly);
|
bool configValid = file.open(QIODevice::ReadOnly);
|
||||||
if (valid) {
|
if (configValid) {
|
||||||
const QJsonDocument document = QJsonDocument::fromJson(file.readAll());
|
const QJsonDocument document = QJsonDocument::fromJson(file.readAll());
|
||||||
const QJsonObject config = document.object();
|
const QJsonObject config = document.object();
|
||||||
endpoint = QUrl(config.value(QStringLiteral("sequencer_addr")).toString());
|
configuredEndpoint = QUrl(
|
||||||
const QString scheme = endpoint.scheme().toLower();
|
config.value(QStringLiteral("sequencer_addr")).toString());
|
||||||
valid = document.isObject() && endpoint.isValid() && !endpoint.host().isEmpty()
|
const QString scheme = configuredEndpoint.scheme().toLower();
|
||||||
|
configValid = document.isObject()
|
||||||
|
&& configuredEndpoint.isValid()
|
||||||
|
&& !configuredEndpoint.host().isEmpty()
|
||||||
&& (scheme == QStringLiteral("http") || scheme == QStringLiteral("https"));
|
&& (scheme == QStringLiteral("http") || scheme == QStringLiteral("https"));
|
||||||
if (valid) {
|
if (configValid) {
|
||||||
const QJsonObject basicAuth = config.value(QStringLiteral("basic_auth")).toObject();
|
const QJsonObject basicAuth = config.value(QStringLiteral("basic_auth")).toObject();
|
||||||
const QString username = basicAuth.value(QStringLiteral("username")).toString();
|
const QString username = basicAuth.value(QStringLiteral("username")).toString();
|
||||||
const QString password = basicAuth.value(QStringLiteral("password")).toString();
|
const QString password = basicAuth.value(QStringLiteral("password")).toString();
|
||||||
@ -103,10 +107,18 @@ bool SequencerClient::configure(const QString& configPath)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!valid) {
|
|
||||||
|
QUrl endpoint = effectiveEndpoint.isEmpty()
|
||||||
|
? configuredEndpoint : QUrl(effectiveEndpoint);
|
||||||
|
const QString scheme = endpoint.scheme().toLower();
|
||||||
|
const bool valid = endpoint.isValid()
|
||||||
|
&& !endpoint.host().isEmpty()
|
||||||
|
&& (scheme == QStringLiteral("http") || scheme == QStringLiteral("https"))
|
||||||
|
&& (configValid || !effectiveEndpoint.isEmpty());
|
||||||
|
if (!valid)
|
||||||
endpoint = QUrl();
|
endpoint = QUrl();
|
||||||
|
if (!configValid || configuredEndpoint != endpoint)
|
||||||
authorization.clear();
|
authorization.clear();
|
||||||
}
|
|
||||||
if (endpoint == m_endpoint && authorization == m_authorization)
|
if (endpoint == m_endpoint && authorization == m_authorization)
|
||||||
return valid;
|
return valid;
|
||||||
|
|
||||||
|
|||||||
@ -25,7 +25,8 @@ public:
|
|||||||
|
|
||||||
explicit SequencerClient(AmmClient* client, QObject* parent = nullptr);
|
explicit SequencerClient(AmmClient* client, QObject* parent = nullptr);
|
||||||
|
|
||||||
bool configure(const QString& configPath);
|
bool configure(const QString& configPath,
|
||||||
|
const QString& effectiveEndpoint = {});
|
||||||
QString endpoint() const { return m_endpoint.toString(); }
|
QString endpoint() const { return m_endpoint.toString(); }
|
||||||
bool isConfigured() const { return m_endpoint.isValid() && !m_endpoint.isEmpty(); }
|
bool isConfigured() const { return m_endpoint.isValid() && !m_endpoint.isEmpty(); }
|
||||||
void applyAuthorization(QNetworkRequest& request) const;
|
void applyAuthorization(QNetworkRequest& request) const;
|
||||||
|
|||||||
@ -53,6 +53,10 @@ namespace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int requestCount() const { return m_requestCount; }
|
int requestCount() const { return m_requestCount; }
|
||||||
|
QByteArray lastRequest() const
|
||||||
|
{
|
||||||
|
return m_lastRequest;
|
||||||
|
}
|
||||||
void failNextRequest() { ++m_failuresRemaining; }
|
void failNextRequest() { ++m_failuresRemaining; }
|
||||||
void holdResponses() { m_holdResponses = true; }
|
void holdResponses() { m_holdResponses = true; }
|
||||||
void releaseNextResponse()
|
void releaseNextResponse()
|
||||||
@ -108,6 +112,7 @@ namespace {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
++m_requestCount;
|
++m_requestCount;
|
||||||
|
m_lastRequest = request;
|
||||||
const bool fail = m_failuresRemaining > 0;
|
const bool fail = m_failuresRemaining > 0;
|
||||||
if (fail)
|
if (fail)
|
||||||
--m_failuresRemaining;
|
--m_failuresRemaining;
|
||||||
@ -121,6 +126,7 @@ namespace {
|
|||||||
|
|
||||||
QTcpServer m_server;
|
QTcpServer m_server;
|
||||||
QHash<QTcpSocket*, QByteArray> m_requests;
|
QHash<QTcpSocket*, QByteArray> m_requests;
|
||||||
|
QByteArray m_lastRequest;
|
||||||
int m_requestCount = 0;
|
int m_requestCount = 0;
|
||||||
int m_failuresRemaining = 0;
|
int m_failuresRemaining = 0;
|
||||||
bool m_holdResponses = false;
|
bool m_holdResponses = false;
|
||||||
@ -480,6 +486,64 @@ int main(int argc, char** argv)
|
|||||||
"stale quote should have no wallet side effects"))
|
"stale quote should have no wallet side effects"))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
LocalRpcServer configuredEndpointServer;
|
||||||
|
LocalRpcServer adoptedEndpointServer;
|
||||||
|
if (!expect(configuredEndpointServer.listen()
|
||||||
|
&& adoptedEndpointServer.listen(),
|
||||||
|
"endpoint-alignment sequencers should listen"))
|
||||||
|
return 1;
|
||||||
|
QTemporaryFile endpointConfig;
|
||||||
|
if (!expect(endpointConfig.open(), "endpoint-alignment config should open"))
|
||||||
|
return 1;
|
||||||
|
endpointConfig.write(QJsonDocument(QJsonObject {
|
||||||
|
{ QStringLiteral("sequencer_addr"), configuredEndpointServer.endpoint() },
|
||||||
|
{ QStringLiteral("basic_auth"), QJsonObject {
|
||||||
|
{ QStringLiteral("username"), QStringLiteral("user") },
|
||||||
|
{ QStringLiteral("password"), QStringLiteral("pass") },
|
||||||
|
} },
|
||||||
|
}).toJson(QJsonDocument::Compact));
|
||||||
|
endpointConfig.flush();
|
||||||
|
FakeAmmClient endpointClient;
|
||||||
|
SequencerClient alignedSequencer(&endpointClient);
|
||||||
|
if (!expect(alignedSequencer.configure(
|
||||||
|
endpointConfig.fileName(), adoptedEndpointServer.endpoint()),
|
||||||
|
"adopted wallet endpoint should configure"))
|
||||||
|
return 1;
|
||||||
|
if (!expect(alignedSequencer.endpoint() == adoptedEndpointServer.endpoint(),
|
||||||
|
"adopted wallet endpoint should override stale config"))
|
||||||
|
return 1;
|
||||||
|
QVector<WalletAccountRead> adoptedEndpointReads;
|
||||||
|
const QString endpointAccount(64, QLatin1Char('3'));
|
||||||
|
if (!expect(waitForAccounts(
|
||||||
|
alignedSequencer, { endpointAccount }, false,
|
||||||
|
&adoptedEndpointReads),
|
||||||
|
"adopted endpoint read should complete"))
|
||||||
|
return 1;
|
||||||
|
if (!expect(configuredEndpointServer.requestCount() == 0
|
||||||
|
&& adoptedEndpointServer.requestCount() == 1,
|
||||||
|
"direct reads should follow adopted wallet endpoint"))
|
||||||
|
return 1;
|
||||||
|
if (!expect(!adoptedEndpointServer.lastRequest().toLower().contains(
|
||||||
|
"authorization:"),
|
||||||
|
"stale config credentials must not reach adopted endpoint"))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (!expect(alignedSequencer.configure(
|
||||||
|
endpointConfig.fileName(), configuredEndpointServer.endpoint()),
|
||||||
|
"matching wallet endpoint should configure"))
|
||||||
|
return 1;
|
||||||
|
QVector<WalletAccountRead> configuredEndpointReads;
|
||||||
|
if (!expect(waitForAccounts(
|
||||||
|
alignedSequencer, { endpointAccount }, false,
|
||||||
|
&configuredEndpointReads),
|
||||||
|
"matching endpoint read should complete"))
|
||||||
|
return 1;
|
||||||
|
if (!expect(configuredEndpointServer.requestCount() == 1
|
||||||
|
&& configuredEndpointServer.lastRequest().toLower().contains(
|
||||||
|
"authorization: basic dxnlcjpwyxnz"),
|
||||||
|
"matching endpoint should retain configured credentials"))
|
||||||
|
return 1;
|
||||||
|
|
||||||
LocalRpcServer server;
|
LocalRpcServer server;
|
||||||
if (!expect(server.listen(), "local sequencer should listen"))
|
if (!expect(server.listen(), "local sequencer should listen"))
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user