Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/v0.0.1 alpha.92 #84

Merged
merged 5 commits into from
Sep 26, 2024
Merged
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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.0.1-alpha.92](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.91...v0.0.1-alpha.92) (2024-09-26)


### Features

* allow override of TRUSTED_FULLNODE_PORT ([954ac95](https://github.com/DIG-Network/dig-chia-sdk/commit/954ac95dfdaeba5734e39ce4255893ad148ef132))
* allow override of TRUSTED_FULLNODE_PORT ([b2a02f5](https://github.com/DIG-Network/dig-chia-sdk/commit/b2a02f51da4f534b832e3c7eb0599aed10434ef7))

### [0.0.1-alpha.91](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.90...v0.0.1-alpha.91) (2024-09-26)


### Features

* add inactivity timeout to content server requests ([a3fee47](https://github.com/DIG-Network/dig-chia-sdk/commit/a3fee4713107f552950aaeef0fd11e60ba894247))

### [0.0.1-alpha.90](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.89...v0.0.1-alpha.90) (2024-09-26)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dignetwork/dig-sdk",
"version": "0.0.1-alpha.90",
"version": "0.0.1-alpha.92",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
67 changes: 59 additions & 8 deletions src/DigNetwork/ContentServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class ContentServer {
}

// Method to get the payment address from the peer
public async getPaymentAddress(): Promise<string> {
public async getPaymentAddress(): Promise<string | null> {
console.log(`Fetching payment address from peer ${this.ipAddress}...`);

try {
Expand All @@ -50,7 +50,7 @@ export class ContentServer {
console.error(
`Failed to fetch payment address from ${this.ipAddress}: ${error.message}`
);
throw new Error(`Failed to fetch payment address: ${error.message}`);
return null;
}
}

Expand Down Expand Up @@ -253,7 +253,7 @@ export class ContentServer {
// Helper method to fetch content with retries and redirection handling
private async fetchWithRetries(url: string): Promise<string> {
let attempt = 0;
const maxRetries = 5;
const maxRetries = 1;
const initialDelay = 2000; // 2 seconds
const maxDelay = 10000; // 10 seconds
const delayMultiplier = 1.5;
Expand Down Expand Up @@ -283,10 +283,13 @@ export class ContentServer {
);
}

// Core method to fetch content from a URL
// Core method to fetch content from a URL with a 5-second inactivity timeout
private async fetch(url: string, maxRedirects: number = 5): Promise<string> {
return new Promise((resolve, reject) => {
const urlObj = new URL(url);
const timeoutDuration = 5000; // 5 seconds

let timeout: NodeJS.Timeout | null = null; // Initialize timeout

const requestOptions = {
hostname: urlObj.hostname,
Expand All @@ -301,12 +304,54 @@ export class ContentServer {
const request = http.request(requestOptions, (response) => {
let data = "";

// Set timeout for inactivity
timeout = setTimeout(() => {
console.error(
`Request timeout: No data received for ${
timeoutDuration / 1000
} seconds.`
);
request.destroy(); // Use destroy instead of abort
reject(
new Error(
`Request timed out after ${
timeoutDuration / 1000
} seconds of inactivity`
)
);
}, timeoutDuration);

const resetTimeout = () => {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
console.error(
`Request timeout: No data received for ${
timeoutDuration / 1000
} seconds.`
);
request.destroy(); // Use destroy instead of abort
reject(
new Error(
`Request timed out after ${
timeoutDuration / 1000
} seconds of inactivity`
)
);
}, timeoutDuration);
};

if (response.statusCode === 200) {
response.on("data", (chunk) => {
data += chunk;
resetTimeout(); // Reset the timeout every time data is received
});

response.on("end", () => {
if (timeout) {
clearTimeout(timeout);
}
resolve(data);
});
} else if (
Expand All @@ -316,16 +361,19 @@ export class ContentServer {
// Handle redirects
if (maxRedirects > 0) {
const redirectUrl = new URL(response.headers.location, url); // Resolve relative URLs based on the original URL

// Recursively follow the redirect, passing the same query params
// console.log(`Redirecting to: ${redirectUrl.toString()}`);
if (timeout) {
clearTimeout(timeout);
}
this.fetch(redirectUrl.toString(), maxRedirects - 1)
.then(resolve)
.catch(reject);
} else {
reject(new Error("Too many redirects"));
}
} else {
if (timeout) {
clearTimeout(timeout);
}
reject(
new Error(
`Failed to retrieve data from ${url}. Status code: ${response.statusCode}`
Expand All @@ -334,7 +382,10 @@ export class ContentServer {
}
});

request.on("error", (error) => {
request.on("error", (error: NodeJS.ErrnoException) => {
if (timeout) {
clearTimeout(timeout);
}
console.error(`GET ${url}:`, error.message);
reject(error);
});
Expand Down
5 changes: 5 additions & 0 deletions src/DigNetwork/DigPeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ export class DigPeer {
memos: Buffer[] = []
): Promise<void> {
const paymentAddress = await this.contentServer.getPaymentAddress();

if (!paymentAddress) {
throw new Error("Payment address not found.");
}

const paymentAddressPuzzleHash = addressToPuzzleHash(paymentAddress);
const output: Output = {
puzzleHash: paymentAddressPuzzleHash,
Expand Down
10 changes: 9 additions & 1 deletion src/blockchain/FullNodePeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,17 @@ export class FullNodePeer {
const peers = await Promise.all(
peerIPs.map(async (ip) => {
if (ip) {
// Allow override of the trusted fullnode port if the override exists
let port = FULLNODE_PORT;
if (trustedNodeIp && ip === trustedNodeIp) {
const trustedFullNodePort = process.env.TRUSTED_FULLNODE_PORT;
if (trustedFullNodePort) {
port = parseInt(trustedFullNodePort);
}
}
try {
const peer = await Peer.new(
`${ip}:${FULLNODE_PORT}`,
`${ip}:${port}`,
false,
certFile,
keyFile
Expand Down
Loading