Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions harvest-finance/backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ PAYMENT_AUTO_RELEASE=true
STELLAR_NETWORK=testnet
STELLAR_PLATFORM_PUBLIC_KEY=
STELLAR_PLATFORM_SECRET_KEY=
STELLAR_CIRCUIT_FAILURE_THRESHOLD=5
STELLAR_CIRCUIT_RESET_TIMEOUT_MS=30000

# Stellar Authentication (SEP-10)
STELLAR_SERVER_SECRET=SBX7SARQOFS6IM2HS2N5TVK54AEF55E3FHOXBTWA6IPEEJJ4W5WJWE6W
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import * as StellarSdk from 'stellar-sdk';
import {
BadRequestException,
InternalServerErrorException,
ServiceUnavailableException,
} from '@nestjs/common';
import { CircuitBreaker } from '../utils/circuit-breaker';
import { isRetryableStellarError } from '../utils/stellar-retry';

describe('StellarService - Escrow Creation', () => {
let service: StellarService;
Expand Down Expand Up @@ -39,26 +42,17 @@ describe('StellarService - Escrow Creation', () => {
get: jest.fn().mockImplementation((key, defaultValue) => {
const config: Record<string, any> = {
STELLAR_NETWORK: 'testnet',
STELLAR_PLATFORM_PUBLIC_KEY:
'GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
STELLAR_PLATFORM_PUBLIC_KEY: platformKeypair.publicKey(),
};
return config[key] ?? defaultValue;
}),
getOrThrow: jest
.fn()
.mockReturnValue(
'GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
),
getOrThrow: jest.fn().mockReturnValue(platformKeypair.publicKey()),
},
},
{
provide: SecretsService,
useValue: {
getSecret: jest
.fn()
.mockResolvedValue(
'SDXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
),
getSecret: jest.fn().mockResolvedValue(platformKeypair.secret()),
},
},
{
Expand Down Expand Up @@ -247,14 +241,16 @@ describe('StellarService - Escrow Creation', () => {
});

it('should throw error after max retries', async () => {
mockServer.submitTransaction.mockRejectedValue(
new Error('Persistent failure'),
);
const persistentTimeout = Object.assign(new Error('Persistent timeout'), {
code: 'ETIMEDOUT',
});

mockServer.submitTransaction.mockRejectedValue(persistentTimeout);

const mockTx = {} as any;

await expect(service['submitWithRetry'](mockTx, 'test')).rejects.toThrow(
'Persistent failure',
'Persistent timeout',
);
expect(mockServer.submitTransaction).toHaveBeenCalledTimes(3);
});
Expand Down Expand Up @@ -282,6 +278,38 @@ describe('StellarService - Escrow Creation', () => {
});
});

describe('Horizon Circuit Breaker', () => {
beforeEach(() => {
(service as any).horizonCircuitBreaker = new CircuitBreaker({
name: 'stellar-horizon-test',
failureThreshold: 2,
resetTimeoutMs: 30_000,
shouldTrip: isRetryableStellarError,
});
});

it('opens after repeated transient Horizon failures and blocks the next call', async () => {
const transientFailure = {
response: { status: 503 },
message: 'Horizon unavailable',
};

mockServer.loadAccount.mockRejectedValue(transientFailure);

await expect(
service.getAccountInfo(farmerKeypair.publicKey()),
).rejects.toThrow(InternalServerErrorException);
await expect(
service.getAccountInfo(farmerKeypair.publicKey()),
).rejects.toThrow(InternalServerErrorException);
await expect(
service.getAccountInfo(farmerKeypair.publicKey()),
).rejects.toThrow(ServiceUnavailableException);

expect(mockServer.loadAccount).toHaveBeenCalledTimes(2);
});
});

describe('Fee Bump Transactions', () => {
it('should submit fee bump with priority fee', async () => {
const submitFeeBumpSpy = jest
Expand Down
Loading