Skip to content

Commit

Permalink
Merge pull request #27 from labscommunity/perf/improve-warp-caching
Browse files Browse the repository at this point in the history
perf: Improve warp caching
  • Loading branch information
pawanpaudel93 authored Mar 11, 2024
2 parents f14cc6e + b822ffd commit ad1a0a5
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,11 @@ git remote add origin proland://YOUR_PROTOCOL_LAND_REPO_ID
```

Replace `YOUR_PROTOCOL_LAND_REPO_ID` with the specific ID of the Protocol Land repository you wish to associate with your project. This establishes a connection to the remote repository, allowing you to fetch, pull, and push changes seamlessly.

## Fix Remote Helper Issues

If you're facing problems with the remote helper that is related to corrupted warp cache, try deleting the Warp cache directory shown while running Git commands. After deleting it, run your Git commands again to check if the issues are resolved.

For example, this is the directory where the warp cache is stored. It varies for every user.

![image](https://github.com/labscommunity/protocol-land-remote-helper/assets/11836100/640669bf-f196-4302-a5c2-3d1a95387b90)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@amplitude/analytics-node": "^1.3.4",
"arbundles": "^0.10.0",
"arweave": "^1.14.4",
"env-paths": "^3.0.0",
"jszip": "^3.10.1",
"node-machine-id": "^1.1.12",
"redstone-api": "^0.4.11",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

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

8 changes: 4 additions & 4 deletions src/lib/protocolLandSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const downloadProtocolLandRepo = async (
// find repo in Protocol Land's warp contract
let repo: Repo | undefined;
try {
repo = await getRepo(repoId, destPath);
repo = await getRepo(repoId);
} catch (err) {
log(err);
}
Expand All @@ -52,7 +52,7 @@ export const downloadProtocolLandRepo = async (
}

// cache is dirty, clear cache and continue
clearCache(destPath, { keepFolders: ['cache'] });
clearCache(destPath, { keepFolders: [] });
}

// if not, download repo data from arweave
Expand Down Expand Up @@ -119,7 +119,7 @@ export const downloadProtocolLandRepo = async (

// rm -rf everything but the bare repo and warp cache (discard stdout)
try {
clearCache(destPath, { keepFolders: ['cache', repo.dataTxId] });
clearCache(destPath, { keepFolders: [repo.dataTxId] });
} catch {}

return repo;
Expand Down Expand Up @@ -176,7 +176,7 @@ export const uploadProtocolLandRepo = async (

// update repo info in warp
log('Updating in warp ...');
const updated = await updateWarpRepo(repo, dataTxId, destPath);
const updated = await updateWarpRepo(repo, dataTxId);

// check for warp update success
return { success: updated.id === repo.id, pushCancelled };
Expand Down
2 changes: 1 addition & 1 deletion src/lib/remoteHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ const spawnPipedGitCommand = async (
// We clear the cached remote:
// If upload succeeded, there's a new txId for the repo
// If upload failed, the cached remote has an inconsistent state
clearCache(tmpPath, { keepFolders: ['cache'] });
clearCache(tmpPath, { keepFolders: [] });

// remove inconsistent cache mark
unsetCacheDirty(tmpPath, repo.dataTxId);
Expand Down
41 changes: 34 additions & 7 deletions src/lib/warpHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,40 @@ import {
defaultCacheOptions,
type LogLevel,
} from 'warp-contracts/mjs';
import { getWallet, getWarpContractTxId, isValidUuid, waitFor } from './common';
import {
PL_TMP_PATH,
getWallet,
getWarpContractTxId,
isValidUuid,
log,
waitFor,
} from './common';
import type { Repo, User } from '../types';
import envPaths from 'env-paths';
import path from 'path';
import fs from 'fs';

async function getWarpContract(signer?: any) {
const contractTxId = getWarpContractTxId();
const cacheDirectory = envPaths(PL_TMP_PATH, { suffix: '' }).cache;
const cacheDirectoryExists = fs.existsSync(cacheDirectory);

log(`Warp cache stored at: ${cacheDirectory}`);

const warp = getWarp(cacheDirectory);
const contract = warp.contract(contractTxId);

if (!cacheDirectoryExists) {
fs.mkdirSync(cacheDirectory, { recursive: true });
await contract
.syncState('https://pl-cache.saikranthi.dev/contract', {
validity: true,
})
.catch(() => {});
}

return signer ? contract.connect(signer) : contract;
}

export const getWarpCacheOptions = (cachePath: string) => {
return {
Expand All @@ -25,7 +56,7 @@ const getWarp = (destPath?: string, logLevel?: LogLevel) => {
};

export async function getRepo(id: string, destpath?: string) {
let pl = getWarp(destpath).contract(getWarpContractTxId());
let pl = await getWarpContract();
if (isValidUuid(id)) {
// let warp throw error if it can't retrieve the repository
const response = await pl.viewState({
Expand Down Expand Up @@ -58,11 +89,7 @@ export async function getRepo(id: string, destpath?: string) {
}
}

export async function updateWarpRepo(
repo: Repo,
newDataTxId: string,
destPath?: string
) {
export async function updateWarpRepo(repo: Repo, newDataTxId: string) {
if (!repo.id || !repo.name || !newDataTxId)
throw '[ warp ] No id, title or dataTxId to update repo ';

Expand Down

0 comments on commit ad1a0a5

Please sign in to comment.