Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core-transaction-pool): clear cached transaction ids after accepting block #2916

Merged
merged 3 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/integration/core-p2p/mocks/core-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ jest.mock("@arkecosystem/core-container", () => {
getLastBlocks: () => [genesisBlock],
getLastHeight: () => genesisBlock.data.height,
cacheTransactions: jest.fn().mockImplementation(txs => ({ notAdded: txs, added: [] })),
removeCachedTransactionIds: jest.fn().mockReturnValue(undefined),
clearCachedTransactionIds: jest.fn().mockReturnValue(undefined),
}),
};
}
Expand Down
8 changes: 4 additions & 4 deletions __tests__/unit/core-blockchain/stubs/state-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export class StateStoreStub implements State.IStateStore {
return undefined;
}

public clear(): void {}
public clear(): void { }

public clearWakeUpTimeout(): void {}
public clearWakeUpTimeout(): void { }

public getCachedTransactionIds(): string[] {
return [];
Expand Down Expand Up @@ -76,9 +76,9 @@ export class StateStoreStub implements State.IStateStore {
};
}

public removeCachedTransactionIds(transactionIds: string[]): void {}
public clearCachedTransactionIds(): void { }

public reset(): void {}
public reset(): void { }

public setLastBlock(block: Blocks.Block): void {
this.lastDownloadedBlock = block.data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export class StateStoreStub implements State.IStateStore {
return undefined;
}

public clear(): void {}
public clear(): void { }

public clearWakeUpTimeout(): void {}
public clearWakeUpTimeout(): void { }

public getCachedTransactionIds(): string[] {
return [];
Expand Down Expand Up @@ -65,13 +65,13 @@ export class StateStoreStub implements State.IStateStore {
return false;
}

public pushPingBlock(block: Interfaces.IBlockData): void {}
public pushPingBlock(block: Interfaces.IBlockData): void { }

public removeCachedTransactionIds(transactionIds: string[]): void {}
public clearCachedTransactionIds(): void { }

public reset(): void {}
public reset(): void { }

public setLastBlock(block: Blocks.Block): void {}
public setLastBlock(block: Blocks.Block): void { }
}

export const stateStorageStub = new StateStoreStub();
12 changes: 6 additions & 6 deletions __tests__/unit/core-state/__fixtures__/state-storage-stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export class StateStorageStub implements State.IStateStorage {
return undefined;
}

public clear(): void {}
public clear(): void { }

public clearWakeUpTimeout(): void {}
public clearWakeUpTimeout(): void { }

public getCachedTransactionIds(): string[] {
return [];
Expand Down Expand Up @@ -56,13 +56,13 @@ export class StateStorageStub implements State.IStateStorage {
return false;
}

public pushPingBlock(block: Interfaces.IBlockData): void {}
public pushPingBlock(block: Interfaces.IBlockData): void { }

public removeCachedTransactionIds(transactionIds: string[]): void {}
public clearCachedTransactionIds(): void { }

public reset(): void {}
public reset(): void { }

public setLastBlock(block: Blocks.Block): void {}
public setLastBlock(block: Blocks.Block): void { }
}

export const stateStorageStub = new StateStorageStub();
4 changes: 2 additions & 2 deletions __tests__/unit/core-state/stores/state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ describe("State Storage", () => {
});
});

describe("removeCachedTransactionIds", () => {
describe("clearCachedTransactionIds", () => {
it("should remove cached transaction ids", () => {
const transactions = [];
for (let i = 0; i < 10; i++) {
Expand All @@ -262,7 +262,7 @@ describe("State Storage", () => {
});

expect(stateStorage.getCachedTransactionIds()).toHaveLength(10);
stateStorage.removeCachedTransactionIds(transactions.map(tx => tx.id));
stateStorage.clearCachedTransactionIds();
expect(stateStorage.getCachedTransactionIds()).toHaveLength(0);
});
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/core-transaction-pool/mocks/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ export const state = {
cacheTransactions: () => undefined,
getLastBlock: () => ({ data: { height: 0 } }),
getLastHeight: () => 1,
removeCachedTransactionIds: () => undefined,
clearCachedTransactionIds: () => undefined,
}),
};
4 changes: 2 additions & 2 deletions packages/core-interfaces/src/core-state/state-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ export interface IStateStore {
): { [key in "added" | "notAdded"]: Interfaces.ITransactionData[] };

/**
* Remove the given transaction ids from the cache.
* Drop all cached transaction ids.
*/
removeCachedTransactionIds(transactionIds: string[]): void;
clearCachedTransactionIds(): void;

/**
* Get cached transaction ids.
Expand Down
6 changes: 3 additions & 3 deletions packages/core-state/src/stores/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ export class StateStore implements State.IStateStore {
}

/**
* Remove the given transaction ids from the cache.
* Drop all cached transaction ids.
*/
public removeCachedTransactionIds(transactionIds: string[]): void {
this.cachedTransactionIds = this.cachedTransactionIds.subtract(transactionIds);
public clearCachedTransactionIds(): void {
this.cachedTransactionIds = this.cachedTransactionIds.clear();
}

/**
Expand Down
11 changes: 6 additions & 5 deletions packages/core-transaction-pool/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class Connection implements TransactionPool.IConnection {
if (!this.loggedAllowedSenders.includes(senderPublicKey)) {
this.logger.debug(
`Transaction pool: allowing sender public key ${senderPublicKey} ` +
`(listed in options.allowedSenders), thus skipping throttling.`,
`(listed in options.allowedSenders), thus skipping throttling.`,
);

this.loggedAllowedSenders.push(senderPublicKey);
Expand Down Expand Up @@ -246,7 +246,7 @@ export class Connection implements TransactionPool.IConnection {

this.logger.error(
`Cannot apply transaction ${transaction.id} when trying to accept ` +
`block ${block.data.id}: ${error.message}`,
`block ${block.data.id}: ${error.message}`,
);

continue;
Expand All @@ -269,9 +269,10 @@ export class Connection implements TransactionPool.IConnection {
delegateWallet.balance = delegateWallet.balance.plus(block.data.reward.plus(block.data.totalFee));
}


app.resolvePlugin<State.IStateService>("state")
.getStore()
.removeCachedTransactionIds(block.transactions.map(tx => tx.id));
.clearCachedTransactionIds();
}

public async buildWallets(): Promise<void> {
Expand All @@ -281,7 +282,7 @@ export class Connection implements TransactionPool.IConnection {

app.resolvePlugin<State.IStateService>("state")
.getStore()
.removeCachedTransactionIds(transactionIds);
.clearCachedTransactionIds();

for (const transactionId of transactionIds) {
const transaction: Interfaces.ITransaction = await this.getTransaction(transactionId);
Expand Down Expand Up @@ -409,7 +410,7 @@ export class Connection implements TransactionPool.IConnection {
if (await this.has(transaction.id)) {
this.logger.debug(
"Transaction pool: ignoring attempt to add a transaction that is already " +
`in the pool, id: ${transaction.id}`,
`in the pool, id: ${transaction.id}`,
);

return { transaction, type: "ERR_ALREADY_IN_POOL", message: "Already in pool" };
Expand Down
6 changes: 1 addition & 5 deletions packages/core-transaction-pool/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Processor implements TransactionPool.IProcessor {
private readonly invalid: Map<string, Interfaces.ITransactionData> = new Map();
private readonly errors: { [key: string]: TransactionPool.ITransactionErrorResponse[] } = {};

constructor(private readonly pool: TransactionPool.IConnection, private readonly walletManager: WalletManager) {}
constructor(private readonly pool: TransactionPool.IConnection, private readonly walletManager: WalletManager) { }

public async validate(transactions: Interfaces.ITransactionData[]): Promise<TransactionPool.IProcessorResult> {
this.cacheTransactions(transactions);
Expand All @@ -31,10 +31,6 @@ export class Processor implements TransactionPool.IProcessor {

await this.addTransactionsToPool();

app.resolvePlugin<State.IStateService>("state")
.getStore()
.removeCachedTransactionIds([...new Set([...this.accept.keys(), ...Object.keys(this.errors)])]);

this.printStats();
}

Expand Down