This repository has been archived by the owner on Jul 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement memory cache and process logs (#88)
* feat(cache): upgrade the memory cache implementation * feat(log): optimize relayable log * fix(lint): syntax lint in relayer game * feat(log): add logs for listener guard * feat(redeem): optimize the process of redeem * chore(log): move shadow api to info log * feat(log): supports interval logs * perf(namespace): rename reedemAble to isRedeemAble
- Loading branch information
Showing
15 changed files
with
177 additions
and
108 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,76 @@ | ||
import { Config } from "../util"; | ||
import path from "path"; | ||
import fs from "fs"; | ||
import { IEthereumHeaderThingWithProof } from "../types"; | ||
import { IEthereumHeaderThingWithProof, ITx } from "../types"; | ||
|
||
const cache = path.resolve((new Config()).path, "../cache/blocks"); | ||
/** | ||
* Memory database for relay process | ||
*/ | ||
class Cache { | ||
public blocks: IEthereumHeaderThingWithProof[] = []; | ||
public txs: ITx[] = []; | ||
public outdateTxs: ITx[] = []; | ||
|
||
// Init Cache | ||
export function init() { | ||
if (fs.existsSync(cache)) { | ||
fs.rmdirSync(cache, { recursive: true }); | ||
/** | ||
* Get block with proof from cache | ||
*/ | ||
getBlock(n: number): IEthereumHeaderThingWithProof | null { | ||
for (const b of this.blocks) { | ||
if (b.header.number === n) { | ||
return b; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
fs.mkdirSync(cache, { recursive: true }); | ||
} | ||
/** | ||
* Set block with proof into cache | ||
*/ | ||
setBlock(headerThing: IEthereumHeaderThingWithProof) { | ||
this.blocks.push(headerThing); | ||
return; | ||
} | ||
|
||
// Get block from cache | ||
export function getBlock(block: number): IEthereumHeaderThingWithProof | null { | ||
const f = path.resolve(cache, `${block}.block`); | ||
if (fs.existsSync(f)) { | ||
return JSON.parse(fs.readFileSync(f).toString()); | ||
} else { | ||
return null; | ||
/** | ||
* Push tx into cache | ||
*/ | ||
pushTx(tx: ITx) { | ||
this.txs.push(tx); | ||
} | ||
} | ||
|
||
// Get block from cache | ||
export function setBlock(block: number, headerThing: IEthereumHeaderThingWithProof) { | ||
fs.writeFileSync(path.resolve(cache, `${block}.block`), JSON.stringify(headerThing)); | ||
/** | ||
* Get the highest tx | ||
*/ | ||
supTx(): number { | ||
const blocks = this.txs.sort( | ||
(p, q) => q.blockNumber - p.blockNumber, | ||
); | ||
|
||
if (!blocks || blocks.length === 0) { | ||
return 0; | ||
} | ||
return blocks[0].blockNumber; | ||
} | ||
|
||
/** | ||
* Slice transactions | ||
*/ | ||
trimTxs(block: number): ITx[] { | ||
const txs = this.txs.filter((t) => t.blockNumber < block); | ||
this.txs = this.txs.filter((t) => t.blockNumber >= block) | ||
return txs; | ||
} | ||
|
||
/** | ||
* Check if a tx has been redeemed | ||
*/ | ||
redeemAble(tx: ITx): boolean { | ||
for (const t of this.outdateTxs) { | ||
if (t === tx) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
const cache = new Cache(); | ||
export default cache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.