Skip to content

Commit

Permalink
feat: fix head store
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Sep 21, 2024
1 parent 7bab5e9 commit 6b76a34
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DIG_USERNAME=1234
DIG_PASSWORD=1234
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"build": "rm -rf dist && tsc && npm run postbuild",
"postbuild": "copyfiles -u 1 \"src/**/*.crt\" \"src/**/*.key\" dist",
"start": "ts-node src/index.ts",
"dev": "set DIG_USERNAME=1234 && set DIG_PASSWORD=1234 && ts-node src/index.ts",
"server": "ts-node src/server.ts",
"test": "mocha -r ts-node/register tests.ts",
"prepare-release": "bash ./scripts/release.sh"
Expand Down
85 changes: 35 additions & 50 deletions src/controllers/merkleTreeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,20 @@ export const headStore = async (req: Request, res: Response): Promise<void> => {
try {
console.log("Received request in headStore controller.");

const publicKey = req.headers["x-public-key"] as string;
console.log("Public Key:", publicKey);

if (!publicKey) {
console.log("Missing x-public-key header.");
throw new HttpError(400, "Missing x-public-key header");
// Extract the publicKey from query params if present (optional)
const publicKey = req.query.publicKey as string | undefined;

let userNonce: string | null = null;
if (publicKey) {
// Generate a nonce if a valid public key is provided
userNonce = await generateNonce(publicKey);
console.log("Generated User Nonce for publicKey:", userNonce);
}

const userNonce = await generateNonce(publicKey);
console.log("Generated User Nonce:", userNonce);

const { storeId } = req.params;
console.log("Store ID:", storeId);

const hasRootHash = req.query.hasRootHash as string;
const hasRootHash = req.query.hasRootHash as string | undefined;
console.log("Has Root Hash Query:", hasRootHash);

if (!storeId) {
Expand All @@ -77,52 +76,39 @@ export const headStore = async (req: Request, res: Response): Promise<void> => {
const dataStore = DataStore.from(storeId);
console.log("Data Store initialized for Store ID:", storeId);

if (hasRootHash) {
const rootHistory = await dataStore.getRootHistory();
console.log("Root History:", rootHistory);

const hasRootHashInHistory = rootHistory?.some(
// @ts-ignore
(history) => history.root_hash === hasRootHash && history.synced
);
console.log(`Root hash ${hasRootHash} in history and synced:`, hasRootHashInHistory);

res.setHeader(
"X-Has-RootHash",
hasRootHashInHistory ? "true" : "false"
);
}

// Check if the store exists on this machine
const storeList = getStoresList();
const hasStore = storeList.includes(storeId);
console.log("Store list:", storeList);
console.log("Has Store:", hasStore);

if (!hasStore) {
console.log("Store not found, setting headers and responding.");
res
.set({
"x-store-id": storeId,
"x-upload-type": "direct",
...(userNonce && { "x-nonce": userNonce }),
})
.status(200)
.end();
return;
}
const storeExists = storeList.includes(storeId);
console.log(`Store exists on this machine: ${storeExists}`);

// Fetch root history and calculate the latest sync status
const rootHistory = await dataStore.getRootHistory();
const hashes = rootHistory.map((history) => history.root_hash);
const lastHash = hashes.length > 0 ? hashes[hashes.length - 1] : null;
console.log("Root Hashes:", hashes);
console.log("Last Hash:", lastHash);
console.log("Root History:", rootHistory);

const latestRootHash = rootHistory.length > 0 ? rootHistory[rootHistory.length - 1].root_hash : null;
let isSynced = false;

if (latestRootHash) {
// If hasRootHash is provided, compare it to the latest root hash
if (hasRootHash) {
const hasRootHashInHistory = rootHistory.some(
(history) => history.root_hash === hasRootHash && history.synced
);
console.log(`Root hash ${hasRootHash} in history and synced:`, hasRootHashInHistory);
res.setHeader("X-Has-RootHash", hasRootHashInHistory ? "true" : "false");
}

// Always check if the store is synced with the latest root hash
isSynced = rootHistory.some((history) => history.root_hash === latestRootHash && history.synced);
console.log(`Store is synced with latest root hash: ${isSynced}`);
}

// Response headers
res
.set({
"x-store-id": storeId,
"x-generation-hash": lastHash || "",
"x-generation-index": hashes.length - 1,
"x-upload-type": "direct",
"x-store-exists": storeExists ? "true" : "false",
"x-synced": isSynced ? "true" : "false",
...(userNonce && { "x-nonce": userNonce }),
})
.status(200)
Expand All @@ -137,7 +123,6 @@ export const headStore = async (req: Request, res: Response): Promise<void> => {
}
};


// Controller to handle GET requests for /stores/:storeId
export const getStore = async (req: Request, res: Response) => {
try {
Expand Down
1 change: 1 addition & 0 deletions src/middleware/verifyMnemonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const verifyCredentials = async (

next(); // Proceed to the next middleware or route handler
} catch (error) {
console.log(error)
return res
.status(500)
.send("An error occurred while verifying the mnemonic.");
Expand Down

0 comments on commit 6b76a34

Please sign in to comment.