-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: skip
createdBy
if already created in indexer
- Loading branch information
1 parent
dfa2f1e
commit b1d694b
Showing
3 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
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
34 changes: 34 additions & 0 deletions
34
packages/neuron-wallet/src/services/indexer/tx-unique-flag.ts
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export interface TxUniqueFlag { | ||
txHash: string | ||
blockHash: string | ||
} | ||
|
||
export class TxUniqueFlagCache { | ||
private arr: TxUniqueFlag[] = [] | ||
|
||
private limit: number | ||
|
||
constructor(limit: number) { | ||
this.limit = limit | ||
} | ||
|
||
public push(value: TxUniqueFlag) { | ||
if (this.includes(value)) { | ||
return | ||
} | ||
this.arr.push(value) | ||
if (this.arr.length > this.limit) { | ||
this.arr.shift() | ||
} | ||
} | ||
|
||
public includes(value: TxUniqueFlag) { | ||
return this.arr.some(txPoint => { | ||
return txPoint.txHash == value.txHash && txPoint.blockHash == value.blockHash | ||
}) | ||
} | ||
|
||
public length() { | ||
return this.arr.length | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/neuron-wallet/tests/services/indexer/tx-unique-flag.test.ts
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { TxUniqueFlag, TxUniqueFlagCache } from '../../../src/services/indexer/tx-unique-flag' | ||
|
||
describe('TxUniqueFlagCache', () => { | ||
const txUniqueFlag: TxUniqueFlag = { | ||
txHash: '0x1742426b21175c35cac63f0fb25318e2dd1facd1c02a244caa9144a0e843e08a', | ||
blockHash: '0x42f917e4c00e39cec94696ee5820b5f79f603bf7a2b7aa7abebf4d13f97110e4', | ||
} | ||
|
||
it('unique', () => { | ||
const cache = new TxUniqueFlagCache(10) | ||
cache.push(txUniqueFlag) | ||
cache.push(txUniqueFlag) | ||
expect(cache.length()).toEqual(1) | ||
}) | ||
|
||
it('limit', () => { | ||
const cache = new TxUniqueFlagCache(10) | ||
Array.from({ length: 20 }).map((_value, index) => { | ||
cache.push({ | ||
blockHash: index.toString(), | ||
txHash: index.toString(), | ||
}) | ||
}) | ||
|
||
expect(cache.length()).toEqual(10) | ||
}) | ||
|
||
it('pop head', () => { | ||
const cache = new TxUniqueFlagCache(10) | ||
Array.from({ length: 20 }).map((_value, index) => { | ||
cache.push({ | ||
blockHash: index.toString(), | ||
txHash: index.toString(), | ||
}) | ||
}) | ||
|
||
const result = Array.from({ length: 10 }).map((_value, index) => { | ||
return { | ||
txHash: (index+10).toString(), | ||
blockHash: (index+10).toString(), | ||
} | ||
}) | ||
|
||
expect(cache.length()).toEqual(10) | ||
result.map(value => { | ||
expect(cache.includes(value)).toBe(true) | ||
}) | ||
}) | ||
}) |