Skip to content

Commit

Permalink
Merge remote-tracking branch 'ArkEcosystem/core/develop' into nonces
Browse files Browse the repository at this point in the history
* ArkEcosystem/core/develop:
  fix(core-blockchain): use resolveOptions("blockchain") to get databaseRollback options (#2572)
  refactor: change hasExceededMaxTransactions()'s argument type (#2568)
  • Loading branch information
vasild committed May 13, 2019
2 parents 82261ae + e1111a5 commit 5b15270
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ describe("Transaction Guard", () => {
// .withPassphrase(wallets[10].passphrase)
// .create(3);

// jest.spyOn(processor.pool, "hasExceededMaxTransactions").mockImplementationOnce(tx => true);
// jest.spyOn(processor.pool, "hasExceededMaxTransactions").mockImplementationOnce(senderPublicKey => true);

// processor.__filterAndTransformTransactions(transactions);

Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/core-blockchain/mocks/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const container = {

return undefined;
},
resolveOptions: () => ({}),
resolveOptions: plugin => ({}),
has: () => true,
forceExit: () => undefined,
},
Expand Down
29 changes: 15 additions & 14 deletions __tests__/unit/core-blockchain/state-machine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,24 +388,31 @@ describe("State Machine", () => {
await expect(() => actionMap.startForkRecovery()).toDispatch(blockchain, "SUCCESS");

expect(loggerInfo).toHaveBeenCalledWith("Starting fork recovery");
methodsCalled.forEach(method => {
for (const method of methodsCalled) {
expect(method).toHaveBeenCalled();
});
}
});
});

describe("rollbackDatabase", () => {
afterEach(() => jest.restoreAllMocks());

beforeEach(() => {
jest.spyOn(container.app, "resolveOptions").mockImplementation(plugin =>
plugin === "blockchain"
? {
databaseRollback: {
maxBlockRewind: 14,
steps: 3,
},
}
: {},
);
});

it("should try to remove X blocks based on databaseRollback config until database.verifyBlockchain() passes - and dispatch SUCCESS", async () => {
const loggerInfo = jest.spyOn(logger, "info");

jest.spyOn(container.app, "resolveOptions").mockReturnValue({
databaseRollback: {
maxBlockRewind: 14,
steps: 3,
},
});
// @ts-ignore
const removeTopBlocks = jest.spyOn(blockchain, "removeTopBlocks").mockReturnValue(true);
jest.spyOn(blockchain.database, "verifyBlockchain")
Expand All @@ -428,12 +435,6 @@ describe("State Machine", () => {

it(`should try to remove X blocks based on databaseRollback config until database.verifyBlockchain() passes
and dispatch FAILURE as verifyBlockchain never passed`, async () => {
jest.spyOn(container.app, "resolveOptions").mockReturnValue({
databaseRollback: {
maxBlockRewind: 14,
steps: 3,
},
});
// @ts-ignore
const removeTopBlocks = jest.spyOn(blockchain, "removeTopBlocks").mockReturnValue(true);
// @ts-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class Connection implements TransactionPool.IConnection {
return;
}

public hasExceededMaxTransactions(transaction: Interfaces.ITransactionData): boolean {
public hasExceededMaxTransactions(senderPublicKey: string): boolean {
return true;
}

Expand Down
14 changes: 6 additions & 8 deletions __tests__/unit/core-transaction-pool/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ describe("Connection", () => {
]);

expect(connection.getPoolSize()).toBe(7);
const exceeded = connection.hasExceededMaxTransactions(mockData.dummy3.data);
const exceeded = connection.hasExceededMaxTransactions(mockData.dummy3.data.senderPublicKey);
expect(exceeded).toBeTrue();
});

Expand All @@ -358,7 +358,7 @@ describe("Connection", () => {
addTransactions([mockData.dummy4, mockData.dummy5, mockData.dummy6]);

expect(connection.getPoolSize()).toBe(3);
const exceeded = connection.hasExceededMaxTransactions(mockData.dummy3.data);
const exceeded = connection.hasExceededMaxTransactions(mockData.dummy3.data.senderPublicKey);
expect(exceeded).toBeFalse();
});

Expand All @@ -377,7 +377,7 @@ describe("Connection", () => {
]);

expect(connection.getPoolSize()).toBe(7);
const exceeded = connection.hasExceededMaxTransactions(mockData.dummy3.data);
const exceeded = connection.hasExceededMaxTransactions(mockData.dummy3.data.senderPublicKey);
expect(exceeded).toBeFalse();
});
});
Expand Down Expand Up @@ -830,11 +830,9 @@ describe("Connection", () => {

for (let i = 0; i < testSize * 2; i++) {
connection.getPoolSize();
for (const sender of ["nonexistent", mockData.dummy1.data.senderPublicKey]) {
connection.getSenderSize(sender);
// @FIXME: Uhm excuse me, what the?
// @ts-ignore
connection.hasExceededMaxTransactions(sender);
for (const senderPublicKey of ["nonexistent", mockData.dummy1.data.senderPublicKey]) {
connection.getSenderSize(senderPublicKey);
connection.hasExceededMaxTransactions(senderPublicKey);
}
connection.getTransaction(fakeTransactionId(i));
connection.getTransactions(0, i);
Expand Down
2 changes: 1 addition & 1 deletion packages/core-blockchain/src/state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ blockchainMachine.actionMap = (blockchain: Blockchain) => ({
async rollbackDatabase() {
logger.info("Trying to restore database integrity");

const { maxBlockRewind, steps } = app.resolveOptions("p2p").databaseRollback;
const { maxBlockRewind, steps } = app.resolveOptions("blockchain").databaseRollback;

for (let i = maxBlockRewind; i >= 0; i -= steps) {
await blockchain.removeTopBlocks(steps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface IConnection {
getTransactionsData<T>(start: number, size: number, property: string, maxBytes?: number): T[];
getTransactionsForForging(blockSize: number): string[];
has(transactionId: string): any;
hasExceededMaxTransactions(transaction: Interfaces.ITransactionData): boolean;
hasExceededMaxTransactions(senderPublicKey: string): boolean;
isSenderBlocked(senderPublicKey: string): boolean;
purgeByBlock(block: Interfaces.IBlock): void;
purgeByPublicKey(senderPublicKey: string): void;
Expand Down
12 changes: 6 additions & 6 deletions packages/core-transaction-pool/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,24 +197,24 @@ export class Connection implements TransactionPool.IConnection {
}

// @TODO: move this to a more appropriate place
public hasExceededMaxTransactions(transaction: Interfaces.ITransactionData): boolean {
public hasExceededMaxTransactions(senderPublicKey: string): boolean {
this.purgeExpired();

if (this.options.allowedSenders.includes(transaction.senderPublicKey)) {
if (!this.loggedAllowedSenders.includes(transaction.senderPublicKey)) {
if (this.options.allowedSenders.includes(senderPublicKey)) {
if (!this.loggedAllowedSenders.includes(senderPublicKey)) {
this.logger.debug(
`Transaction pool: allowing sender public key: ${
transaction.senderPublicKey
senderPublicKey
} (listed in options.allowedSenders), thus skipping throttling.`,
);

this.loggedAllowedSenders.push(transaction.senderPublicKey);
this.loggedAllowedSenders.push(senderPublicKey);
}

return false;
}

return this.memory.getBySender(transaction.senderPublicKey).size >= this.options.maxTransactionsPerSender;
return this.memory.getBySender(senderPublicKey).size >= this.options.maxTransactionsPerSender;
}

public flush(): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-transaction-pool/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class Processor implements TransactionPool.IProcessor {
"ERR_TOO_LARGE",
`Transaction ${transaction.id} is larger than ${maxTransactionBytes} bytes.`,
);
} else if (this.pool.hasExceededMaxTransactions(transaction)) {
} else if (this.pool.hasExceededMaxTransactions(transaction.senderPublicKey)) {
this.excess.push(transaction.id);
} else if (this.validateTransaction(transaction)) {
try {
Expand Down

0 comments on commit 5b15270

Please sign in to comment.