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

feat: known peers integration #528

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ARG NGX_BROTLI_COMMIT=6e975bcb015f62e1f303054897783355e2a877dc
# https://nodejs.org/en
ARG NODEJS_MAJOR_VERSION="20"
# https://github.com/filecoin-project/lassie/releases
ARG LASSIE_VERSION="v0.21.0"
ARG LASSIE_VERSION="v0.22.0"
# https://github.com/max-lt/nginx-jwt-module
ARG NGINX_JWT_VERSION="v3.2.2"
ARG LIBJWT_VERSION=1.15.3
Expand Down
188 changes: 188 additions & 0 deletions container/shim/package-lock.json

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

1 change: 1 addition & 0 deletions container/shim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"express": "^4.18.2",
"express-async-handler": "^1.2.0",
"fast-glob": "^3.3.2",
"jsonwebtoken": "^8.5.1",
"logfmt": "^1.4.0",
"lru-cache": "^10.1.0",
"mime-types": "^2.1.35",
Expand Down
32 changes: 32 additions & 0 deletions container/shim/src/fetchers/lassie.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fetch from "node-fetch";
import { LASSIE_ORIGIN, LASSIE_SP_ELIGIBLE_PORTION, hasNodeToken } from "../config.js";
import { submitLassieLogs } from "../modules/log_ingestor.js";
import { proxyAllResponseHeaders, proxyCARResponseHeaders, toUtf8 } from "../utils/http.js";
import { getKnownPeers } from "../utils/jwt.js";
import { debug as Debug } from "../utils/logging.js";

const debug = Debug.extend("lassie");
Expand Down Expand Up @@ -171,6 +172,7 @@ export async function respondFromLassie(req, res, { cidObj, format }) {

function createLassieURL(req, isRawFormat) {
const lassieUrl = new URL(LASSIE_ORIGIN + toUtf8(req.path));

for (const [key, val] of Object.entries(req.query)) {
if (key === "filename") {
continue;
Expand Down Expand Up @@ -223,6 +225,14 @@ function createLassieURL(req, isRawFormat) {
lassieUrl.searchParams.set("protocols", "bitswap,http");
}
}

const knownPeers = getKnownPeers(req);
const knownPeersLassieUrl = generatePeersLassieUrl(knownPeers);

if (knownPeersLassieUrl) {
lassieUrl.searchParams.set("providers", knownPeersLassieUrl);
}

return lassieUrl;
}

Expand Down Expand Up @@ -297,6 +307,28 @@ async function getRequestedBlockFromCar(streamIn, streamOut, cidObj, filename) {
}
}

function generatePeersLassieUrl(knownPeers) {
const peerUrls = [];
if (knownPeers) {
const knownPeerList = Object.values(knownPeers).flat()

knownPeerList.forEach((peer, idx) => {
const { peerID, multiaddr, protocol } = peer;

if (multiaddr && multiaddr.startsWith("http")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be checking multiaddr or protocol for http?

The peer structure i'm working with is

"knownPeers": {
    "web3.storage": [
      {
        "peerID": "QmUA9D3H7HeCYsirB3KmPSvZh3dNXMZas6Lwgr4fv1HTTp",
        "protocol": "http",
        "multiaddr": "/dns4/dag.w3s.link/tcp/443/https"
      }
    ]
  },

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final url with the current logic will result in "/dns4/dag.w3s.link/tcp/443/https/p2p/QmUA9D3H7HeCYsirB3KmPSvZh3dNXMZas6Lwgr4fv1HTTp+http" which seems off.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to @hannahhoward's description, we pass the raw multiaddr only if the actual multiaddr starts with http

peerUrls.push(multiaddr);
return;
}
if (multiaddr && peerID && protocol) {
const peerUrl = `${multiaddr}/p2p/${peerID}+${protocol}`;
peerUrls.push(peerUrl);
}
});
}
const urlString = peerUrls.join(",");
return urlString;
}

async function queueMetricsReport(newMetric) {
metrics.push(newMetric);

Expand Down
23 changes: 23 additions & 0 deletions container/shim/src/utils/jwt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import jwt from "jsonwebtoken";

function findJWT(req) {
const jwtQuery = req.query.jwt;

let jwtHeader = "";
const authHeader = req.headers.authorization;
if (authHeader && authHeader.startsWith("Bearer ")) {
jwtHeader = authHeader.replace("Bearer ", "");
}

return jwtQuery || jwtHeader;
}

export function getKnownPeers(req) {
const reqJwt = findJWT(req);
if (reqJwt) {
const jwtObject = jwt.decode(reqJwt);
const knownPeers = jwtObject.knownPeers;
return knownPeers;
}
return null;
}
Loading