Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public applyTransaction(transaction: Interfaces.ITransaction): void {
const { data } = transaction;
const { recipientId, senderPublicKey } = data;
const transactionHandler: Handlers.TransactionHandler = Handlers.Registry.get(transaction.type);
const sender: State.IWallet = this.findByPublicKey(senderPublicKey);
const recipient: State.IWallet = this.findByAddress(recipientId);
// handle exceptions / verify that we can apply the transaction to the sender
if (Utils.isException(data)) {
this.logger.warn(`Transaction ${data.id} forcibly applied because it has been added as an exception.`);
} else {
try {
transactionHandler.canBeApplied(transaction, sender, this);
} catch (error) {
this.logger.error(
`Can't apply transaction id:${data.id} from sender:${sender.address} due to ${error.message}`,
);
this.logger.debug(`Audit: ${JSON.stringify(sender.auditApply(data), undefined, 2)}`);
throw new Error(`Can't apply transaction ${data.id}`);
}
}
transactionHandler.apply(transaction, this);
this.updateVoteBalances(sender, recipient, data);
}
const blocks: Interfaces.IBlockData[] = await blockchain.p2p
.getMonitor()
.syncWithNetwork(lastDownloadedBlock.height);
if (blockchain.isStopped) {
return;
}
// Could have changed since entering this function, e.g. due to a rollback.
if (lastDownloadedBlock.id !== stateStorage.lastDownloadedBlock.id) {
return;
}
const empty: boolean = !blocks || blocks.length === 0;
const chained: boolean =
!empty && (isBlockChained(lastDownloadedBlock, blocks[0]) || Utils.isException(blocks[0]));
if (chained) {
logger.info(
`Downloaded ${blocks.length} new ${pluralize(
"block",
blocks.length,
)} accounting for a total of ${pluralize(
"transaction",
blocks.reduce((sum, b) => sum + b.numberOfTransactions, 0),
true,
)}`,
);
stateStorage.noBlockCounter = 0;
stateStorage.p2pUpdateCounter = 0;
// Edge case if sender is unknown and has no balance.
// NOTE: Check is performed against the database wallet manager.
const { senderPublicKey } = transaction.data;
if (!this.databaseService.walletManager.has(senderPublicKey)) {
const senderAddress: string = Identities.Address.fromPublicKey(senderPublicKey);
if (this.databaseService.walletManager.findByAddress(senderAddress).balance.isZero()) {
const message: string = "Cold wallet is not allowed to send until receiving transaction is confirmed.";
this.logger.error(message);
throw new Error(JSON.stringify([message]));
}
}
if (Utils.isException(transaction.data)) {
this.logger.warn(
`Transaction forcibly applied because it has been added as an exception: ${transaction.id}`,
);
} else {
const sender: State.IWallet = this.findByPublicKey(senderPublicKey);
try {
Handlers.Registry.get(transaction.type).canBeApplied(
transaction,
sender,
this.databaseService.walletManager,
);
} catch (error) {
this.logger.error(
`[PoolWalletManager] Can't apply transaction ${transaction.id} from ${
sender.address
public async applyToSender(
transaction: Interfaces.ITransaction,
walletManager: State.IWalletManager,
): Promise {
const sender: State.IWallet = walletManager.findByPublicKey(transaction.data.senderPublicKey);
const data: Interfaces.ITransactionData = transaction.data;
if (Utils.isException(data)) {
walletManager.logger.warn(`Transaction forcibly applied as an exception: ${transaction.id}.`);
}
await this.throwIfCannotBeApplied(transaction, sender, walletManager);
sender.verifyTransactionNonceApply(transaction);
sender.nonce = data.nonce;
const lockId: string = data.asset.claim.lockTransactionId;
const lockWallet: State.IWallet = walletManager.findByIndex(State.WalletIndexes.Locks, lockId);
assert(lockWallet && lockWallet.getAttribute("htlc.locks")[lockId]);
const locks: Interfaces.IHtlcLocks = lockWallet.getAttribute("htlc.locks");
const recipientWallet: State.IWallet = walletManager.findByAddress(locks[lockId].recipientId);
public async getHandler(block: Interfaces.IBlock): Promise {
if (Utils.isException(block.data)) {
return new ExceptionHandler(this.blockchain, block);
}
if (!this.verifyBlock(block)) {
return new VerificationFailedHandler(this.blockchain, block);
}
const isValidGenerator: boolean = await validateGenerator(block);
const isChained: boolean = isBlockChained(this.blockchain.getLastBlock().data, block.data);
if (!isChained) {
return new UnchainedHandler(this.blockchain, block, isValidGenerator);
}
if (!isValidGenerator) {
return new InvalidGeneratorHandler(this.blockchain, block);
}
public async throwIfCannotBeApplied(
transaction: Interfaces.ITransaction,
wallet: State.IWallet,
walletManager: State.IWalletManager,
): Promise {
if (Utils.isException(transaction.data)) {
return;
}
if (!wallet.hasAttribute("business")) {
throw new WalletIsNotBusinessError();
}
if (wallet.getAttribute("business.resigned") === true) {
throw new BusinessIsResignedError();
}
const { data }: Interfaces.ITransaction = transaction;
const bridgechains: Record = wallet.getAttribute("business.bridgechains");
if (
bridgechains &&
public async applyToSender(
transaction: Interfaces.ITransaction,
walletManager: State.IWalletManager,
): Promise {
const sender: State.IWallet = walletManager.findByPublicKey(transaction.data.senderPublicKey);
const data: Interfaces.ITransactionData = transaction.data;
if (Utils.isException(data)) {
walletManager.logger.warn(`Transaction forcibly applied as an exception: ${transaction.id}.`);
}
await this.throwIfCannotBeApplied(transaction, sender, walletManager);
sender.verifyTransactionNonceApply(transaction);
sender.nonce = data.nonce;
const lockId: string = data.asset.refund.lockTransactionId;
const lockWallet: State.IWallet = walletManager.findByIndex(State.WalletIndexes.Locks, lockId);
assert(lockWallet && lockWallet.getAttribute("htlc.locks")[lockId]);
const locks: Interfaces.IHtlcLocks = lockWallet.getAttribute("htlc.locks");
const newBalance: Utils.BigNumber = lockWallet.balance.plus(locks[lockId].amount).minus(data.fee);
assert(!newBalance.isNegative());