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-transactions): make handler functions asynchronous #2865

Merged
merged 3 commits into from
Aug 6, 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
36 changes: 5 additions & 31 deletions __tests__/integration/core-blockchain/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ const addBlocks = async untilHeight => {
}
};

const indexWalletWithSufficientBalance = (transaction: Interfaces.ITransaction): void => {
const walletManager = blockchain.database.walletManager;

const wallet = walletManager.findByPublicKey(transaction.data.senderPublicKey);
wallet.balance = wallet.balance.abs().plus(transaction.data.amount.plus(transaction.data.fee));
walletManager.reindex(wallet);
};

describe("Blockchain", () => {
beforeAll(async () => {
container = await setUp({
Expand Down Expand Up @@ -104,34 +96,16 @@ describe("Blockchain", () => {
await tearDown();
});

afterEach(async () => {
beforeEach(async () => {
await resetToHeight1();
await addBlocks(5);
await resetBlocksInCurrentRound();
});

describe("postTransactions", () => {
it("should be ok", async () => {
blockchain.transactionPool.flush();

jest.spyOn(blockchain.transactionPool as any, "removeForgedTransactions").mockReturnValue([]);

for (const transaction of genesisBlock.transactions) {
indexWalletWithSufficientBalance(transaction);
}

const transferTransactions = genesisBlock.transactions.filter(tx => tx.type === 0);

await blockchain.postTransactions(transferTransactions);
const transactions = await blockchain.transactionPool.getTransactions(0, 200);

expect(transactions).toHaveLength(transferTransactions.length);

expect(transactions).toIncludeAllMembers(transferTransactions.map(transaction => transaction.serialized));

blockchain.transactionPool.flush();
jest.restoreAllMocks();
});
afterEach(async () => {
await resetToHeight1();
await addBlocks(5);
await resetBlocksInCurrentRound();
});

describe("removeBlocks", () => {
Expand Down
20 changes: 10 additions & 10 deletions __tests__/integration/core-transaction-pool/wallet-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@ afterAll(async () => {
});

describe("throwIfCannotBeApplied", () => {
it("should add an error for delegate registration when username is already taken", () => {
it("should add an error for delegate registration when username is already taken", async () => {
const delegateReg = TransactionFactory.delegateRegistration("genesis_11")
.withNetwork("unitnet")
.withPassphrase(wallets[11].passphrase)
.build()[0];

const username: string = delegateReg.data.asset.delegate.username;

expect(() => poolWalletManager.throwIfCannotBeApplied(delegateReg)).toThrow(
`Failed to apply transaction, because the username '${username}' is already registered.`
await expect(poolWalletManager.throwIfCannotBeApplied(delegateReg)).rejects.toThrow(
`Failed to apply transaction, because the username '${username}' is already registered.`,
);
});

it("should add an error when voting for a delegate that doesn't exist", () => {
it("should add an error when voting for a delegate that doesn't exist", async () => {
const vote = TransactionFactory.vote(wallets[12].keys.publicKey)
.withNetwork("unitnet")
.withPassphrase(wallets[11].passphrase)
.build()[0];

expect(() => poolWalletManager.throwIfCannotBeApplied(vote)).toThrow(
`Failed to apply transaction, because only delegates can be voted.`
await expect(poolWalletManager.throwIfCannotBeApplied(vote)).rejects.toThrow(
`Failed to apply transaction, because only delegates can be voted.`,
);
});
});
Expand Down Expand Up @@ -73,7 +73,7 @@ describe("applyPoolTransactionToSender", () => {
poolWalletManager.reindex(newWallet);

const transactionHandler = Handlers.Registry.get(transfer.type);
transactionHandler.applyToSender(transfer, poolWalletManager);
await transactionHandler.applyToSender(transfer, poolWalletManager);

expect(+delegateWallet.balance).toBe(+delegate0.balance - amount1 - 0.1 * 10 ** 8);
expect(newWallet.balance.isZero()).toBeTrue();
Expand Down Expand Up @@ -102,7 +102,7 @@ describe("applyPoolTransactionToSender", () => {
poolWalletManager.reindex(newWallet);

const transactionHandler = Handlers.Registry.get(transfer.type);
transactionHandler.applyToSender(transfer, poolWalletManager);
await transactionHandler.applyToSender(transfer, poolWalletManager);

expect(+delegateWallet.balance).toBe(+delegate0.balance - amount1 - fee);
expect(newWallet.balance.isZero()).toBeTrue();
Expand Down Expand Up @@ -149,8 +149,8 @@ describe("applyPoolTransactionToSender", () => {
.walletManager.findByPublicKey(transfer.data.senderPublicKey);

try {
poolWalletManager.throwIfCannotBeApplied(transfer);
transactionHandler.applyToSender(transfer, poolWalletManager);
await poolWalletManager.throwIfCannotBeApplied(transfer);
await transactionHandler.applyToSender(transfer, poolWalletManager);
expect(t.from).toBe(delegate);
} catch (error) {
expect(t.from).toBe(walletsGen[0]);
Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/core-database/database-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ describe("Database Service", () => {
);

block.data.generatorPublicKey = keys.publicKey;
walletManager.applyBlock(block);
await walletManager.applyBlock(block);

blocksInRound.push(block);
}
Expand Down
18 changes: 9 additions & 9 deletions __tests__/unit/core-state/wallets/wallet-manager-htlc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe("Wallet Manager", () => {
expect(delegate.getAttribute("delegate.voteBalance")).toEqual(initialDelegateWalletBalance);

// apply vote
walletManager.applyTransaction(voteTransaction);
await walletManager.applyTransaction(voteTransaction);

const expectBeforeLockTx = () => {
expect(lockWallet.balance).toEqual(initialLockWalletBalance.minus(voteTransaction.data.fee));
Expand All @@ -113,7 +113,7 @@ describe("Wallet Manager", () => {
};
expectBeforeLockTx();

walletManager.applyTransaction(lockTransaction);
await walletManager.applyTransaction(lockTransaction);

const expectAfterLockTx = () => {
expect(lockWallet.balance).toEqual(
Expand All @@ -135,7 +135,7 @@ describe("Wallet Manager", () => {
};
expectAfterLockTx();

walletManager.applyTransaction(claimTransaction);
await walletManager.applyTransaction(claimTransaction);

expect(lockWallet.balance).toEqual(
initialLockWalletBalance
Expand Down Expand Up @@ -220,7 +220,7 @@ describe("Wallet Manager", () => {
expect(delegate.balance).toEqual(initialDelegateWalletBalance);
expect(delegate.getAttribute("delegate.voteBalance")).toEqual(initialDelegateWalletBalance);

walletManager.applyTransaction(voteTransaction);
await walletManager.applyTransaction(voteTransaction);

const expectBeforeLockTx = () => {
expect(lockWallet.balance).toEqual(initialLockWalletBalance);
Expand All @@ -235,7 +235,7 @@ describe("Wallet Manager", () => {
};
expectBeforeLockTx();

walletManager.applyTransaction(lockTransaction);
await walletManager.applyTransaction(lockTransaction);

const expectAfterLockTx = () => {
expect(lockWallet.balance).toEqual(
Expand All @@ -252,7 +252,7 @@ describe("Wallet Manager", () => {
};
expectAfterLockTx();

walletManager.applyTransaction(claimTransaction);
await walletManager.applyTransaction(claimTransaction);

expect(lockWallet.balance).toEqual(
initialLockWalletBalance.minus(amount).minus(lockTransaction.data.fee),
Expand Down Expand Up @@ -363,7 +363,7 @@ describe("Wallet Manager", () => {
expect(delegate.balance).toEqual(initialDelegateWalletBalance);
expect(delegate.getAttribute("delegate.voteBalance")).toEqual(initialDelegateWalletBalance);

walletManager.applyTransaction(voteTransaction);
await walletManager.applyTransaction(voteTransaction);

const expectBeforeLockTx = () => {
expect(lockWallet.balance).toEqual(initialLockWalletBalance.minus(voteTransaction.data.fee));
Expand All @@ -377,7 +377,7 @@ describe("Wallet Manager", () => {
};
expectBeforeLockTx();

walletManager.applyTransaction(lockTransaction);
await walletManager.applyTransaction(lockTransaction);

const expectAfterLockTx = () => {
expect(lockWallet.balance).toEqual(
Expand All @@ -398,7 +398,7 @@ describe("Wallet Manager", () => {
};
expectAfterLockTx();

walletManager.applyTransaction(refundTransaction);
await walletManager.applyTransaction(refundTransaction);

expect(lockWallet.balance).toEqual(
initialLockWalletBalance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe("Wallet Manager", () => {
expect(delegate.getAttribute("delegate.voteBalance")).toEqual(initialDelegateWalletBalance);

// apply vote
walletManager.applyTransaction(voteTransaction);
await walletManager.applyTransaction(voteTransaction);

const expectBeforeMultiPayment = () => {
expect(senderWallet.balance).toEqual(initialSenderWalletBalance.minus(voteTransaction.data.fee));
Expand All @@ -88,7 +88,7 @@ describe("Wallet Manager", () => {
}
const multipaymentTransaction = multipaymentBuilder.sign(senderPassphrase).build();

walletManager.applyTransaction(multipaymentTransaction);
await walletManager.applyTransaction(multipaymentTransaction);

expect(senderWallet.balance).toEqual(
initialSenderWalletBalance
Expand Down Expand Up @@ -126,14 +126,15 @@ describe("Wallet Manager", () => {

// apply vote
const voteTransactionFee = "125";
recipientsPassphrases.map(p => {
for (const passphrase of recipientsPassphrases) {
const voteTransaction = Transactions.BuilderFactory.vote()
.votesAsset([`+${delegateKeys.publicKey}`])
.fee(voteTransactionFee)
.sign(p)
.sign(passphrase)
.build();
walletManager.applyTransaction(voteTransaction);
});

await walletManager.applyTransaction(voteTransaction);
}

const expectBeforeMultiPayment = () => {
expect(senderWallet.balance).toEqual(initialSenderWalletBalance);
Expand All @@ -159,7 +160,7 @@ describe("Wallet Manager", () => {
}
const multipaymentTransaction = multipaymentBuilder.sign(senderPassphrase).build();

walletManager.applyTransaction(multipaymentTransaction);
await walletManager.applyTransaction(multipaymentTransaction);

expect(senderWallet.balance).toEqual(
initialSenderWalletBalance
Expand Down
18 changes: 10 additions & 8 deletions __tests__/unit/core-state/wallets/wallet-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ describe("Wallet Manager", () => {
it("should return the current block", async () => {
expect(walletManager.getCurrentBlock()).toBeUndefined();

const applyTransaction = jest.spyOn(walletManager, "applyTransaction").mockImplementationOnce(() => {
expect(walletManager.getCurrentBlock()).toBe(block2);
});
const applyTransaction = jest
.spyOn(walletManager, "applyTransaction")
.mockImplementationOnce(async () => {
expect(walletManager.getCurrentBlock()).toBe(block2);
});

await walletManager.applyBlock(block2);
expect(applyTransaction).toHaveBeenCalled();
Expand Down Expand Up @@ -244,7 +246,7 @@ describe("Wallet Manager", () => {
recipient.setAttribute("delegate", {});
}

walletManager.applyTransaction(transaction);
await walletManager.applyTransaction(transaction);

expect(sender.balance).toEqual(balanceSuccess.minus(amount).minus(transaction.data.fee));

Expand All @@ -260,7 +262,7 @@ describe("Wallet Manager", () => {
expect(+recipient.balance.toFixed()).toBe(0);

try {
expect(walletManager.applyTransaction(transaction)).rejects.toThrow(InsufficientBalanceError);
await expect(walletManager.applyTransaction(transaction)).rejects.toThrow(InsufficientBalanceError);
expect(undefined).toBe("this should fail if no error is thrown");
} catch (error) {
expect(+sender.balance.toFixed()).toBe(+balanceFail);
Expand Down Expand Up @@ -321,7 +323,7 @@ describe("Wallet Manager", () => {
);
expect(voter.balance).toEqual(Utils.BigNumber.make(100_000));

walletManager.applyTransaction(voteTransaction);
await walletManager.applyTransaction(voteTransaction);

expect(voter.balance).toEqual(Utils.BigNumber.make(100_000).minus(voteTransaction.data.fee));
expect(delegate.getAttribute<Utils.BigNumber>("delegate.voteBalance")).toEqual(
Expand Down Expand Up @@ -364,7 +366,7 @@ describe("Wallet Manager", () => {
);
expect(voter.balance).toEqual(Utils.BigNumber.make(100_000));

walletManager.applyTransaction(voteTransaction);
await walletManager.applyTransaction(voteTransaction);

expect(voter.balance).toEqual(Utils.BigNumber.make(100_000).minus(voteTransaction.data.fee));
expect(delegate.getAttribute<Utils.BigNumber>("delegate.voteBalance")).toEqual(
Expand All @@ -378,7 +380,7 @@ describe("Wallet Manager", () => {
.sign("secret")
.build();

walletManager.applyTransaction(unvoteTransaction);
await walletManager.applyTransaction(unvoteTransaction);

expect(voter.balance).toEqual(
Utils.BigNumber.make(100_000)
Expand Down
17 changes: 10 additions & 7 deletions __tests__/unit/core-transaction-pool/__stubs__/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ export class Connection implements TransactionPool.IConnection {
return;
}

public getPoolSize(): number {
public async getPoolSize(): Promise<number> {
return 0;
}

public getSenderSize(senderPublicKey: string): number {
public async getSenderSize(senderPublicKey: string): Promise<number> {
return 0;
}

public addTransactions(transactions: Interfaces.ITransaction[]): ITransactionsProcessed {
public async addTransactions(transactions: Interfaces.ITransaction[]): Promise<ITransactionsProcessed> {
return { added: [], notAdded: [] };
}

Expand All @@ -73,7 +73,7 @@ export class Connection implements TransactionPool.IConnection {
return [];
}

public getTransaction(id: string): Interfaces.ITransaction {
public async getTransaction(id: string): Promise<Interfaces.ITransaction> {
return undefined;
}

Expand All @@ -93,7 +93,7 @@ export class Connection implements TransactionPool.IConnection {
return;
}

public hasExceededMaxTransactions(senderPublicKey: string): boolean {
public async hasExceededMaxTransactions(senderPublicKey: string): Promise<boolean> {
return true;
}

Expand All @@ -109,7 +109,7 @@ export class Connection implements TransactionPool.IConnection {
return;
}

public acceptChainedBlock(block: Blocks.Block): void {
public async acceptChainedBlock(block: Blocks.Block): Promise<void> {
return;
}

Expand All @@ -121,7 +121,10 @@ export class Connection implements TransactionPool.IConnection {
return;
}

public senderHasTransactionsOfType(senderPublicKey: string, transactionType: Enums.TransactionType): boolean {
public async senderHasTransactionsOfType(
senderPublicKey: string,
transactionType: Enums.TransactionType,
): Promise<boolean> {
return true;
}
}
Loading