Skip to content

Commit

Permalink
Adding index to local cache
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenKor committed Sep 3, 2024
1 parent 994e2c0 commit de61017
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
8 changes: 4 additions & 4 deletions zp-relayer/pool/RelayPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export class RelayPool extends BasePool<Network> {
}

// cache transaction locally
await this.cacheTxLocally(outCommit, txHash, memo);
await this.cacheTxLocally(outCommit, txHash, memo, commitIndex);
// start monitoring local cache against the indexer to cleanup already indexed txs
this.startLocalCacheObserver(commitIndex);
}
Expand All @@ -278,7 +278,7 @@ export class RelayPool extends BasePool<Network> {
poolJob.data.transaction.txHash = txHash;
await poolJob.update(poolJob.data);

await this.cacheTxLocally(res.outCommit, txHash, res.memo);
await this.cacheTxLocally(res.outCommit, txHash, res.memo, res.commitIndex);
}
}
}
Expand All @@ -296,15 +296,15 @@ export class RelayPool extends BasePool<Network> {
}
}

protected async cacheTxLocally(commit: string, txHash: string, memo: string) {
protected async cacheTxLocally(commit: string, txHash: string, memo: string, index: number) {
// store or updating local tx store
// (we should keep sent transaction until the indexer grab them)
const prefixedMemo = buildPrefixedMemo(
commit,
txHash,
memo
);
await this.txStore.add(commit, prefixedMemo);
await this.txStore.add(commit, prefixedMemo, index);
logger.info(`Tx with commit ${commit} has been CACHED locally`);
}

Expand Down
28 changes: 22 additions & 6 deletions zp-relayer/state/TxStore.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
import { hexToNumber, numberToHexPadded } from '@/utils/helpers';
import type { Redis } from 'ioredis'

const INDEX_BYTES = 6;

export class TxStore {
constructor(public name: string, private redis: Redis) {}

async add(commitment: string, memo: string) {
await this.redis.hset(this.name, { [commitment]: memo })
async add(commitment: string, memo: string, index: number) {
await this.redis.hset(this.name, { [commitment]: `${numberToHexPadded(index, INDEX_BYTES)}${memo}` })
}

async remove(commitment: string) {
await this.redis.hdel(this.name, commitment)
}

async get(commitment: string) {
const memo = await this.redis.hget(this.name, commitment)
return memo
async get(commitment: string): Promise<{memo: string, index: number} | null> {
const data = await this.redis.hget(this.name, commitment);

return data ? {
memo: data.slice(INDEX_BYTES * 2),
index: hexToNumber(data.slice(0, INDEX_BYTES * 2)),
} : null;
}

async getAll() {
return await this.redis.hgetall(this.name)
return await this.redis.hgetall(this.name).then(keys => {
return Object.entries(keys)
.map(([commit, data]) =>
[commit,
{
memo: data.slice(INDEX_BYTES * 2),
index: hexToNumber(data.slice(0, INDEX_BYTES * 2)),
}] as [string, {memo: string, index: number}]
)
}
}

async removeAll() {
Expand Down
10 changes: 9 additions & 1 deletion zp-relayer/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { SnarkProof } from 'libzkbob-rs-node'
import promiseRetry from 'promise-retry'
import type Web3 from 'web3'
import type { Contract } from 'web3-eth-contract'
import { padLeft, toBN } from 'web3-utils'
import { padLeft, toBN, numberToHex } from 'web3-utils'
import { TxType } from 'zp-memo-parser'
import { isContractCallError } from './web3Errors'

Expand Down Expand Up @@ -316,3 +316,11 @@ export async function fetchJson(serverUrl: string, path: string, query: [string,

return await res.json()
}

export function numberToHexPadded(num: number, numBytes: number): string {
return padLeft(numberToHex(num).slice(2), numBytes * 2);
}

export function hexToNumber(hex: string): number {
return parseInt(hex, 16);
}

0 comments on commit de61017

Please sign in to comment.