-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathdelegate-registration.ts
138 lines (110 loc) · 5.43 KB
/
delegate-registration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { Database, EventEmitter, State, TransactionPool } from "@arkecosystem/core-interfaces";
import { Enums, Interfaces, Transactions } from "@arkecosystem/crypto";
import {
NotSupportedForMultiSignatureWalletError,
WalletUsernameAlreadyRegisteredError,
WalletUsernameEmptyError,
WalletUsernameNotEmptyError,
} from "../errors";
import { TransactionHandler } from "./transaction";
const { TransactionTypes } = Enums;
export class DelegateRegistrationTransactionHandler extends TransactionHandler {
public getConstructor(): Transactions.TransactionConstructor {
return Transactions.DelegateRegistrationTransaction;
}
public async bootstrap(connection: Database.IConnection, walletManager: State.IWalletManager): Promise<void> {
const transactions = await connection.transactionsRepository.getAssetsByType(this.getConstructor().type);
const forgedBlocks = await connection.blocksRepository.getDelegatesForgedBlocks();
const lastForgedBlocks = await connection.blocksRepository.getLastForgedBlocks();
for (const transaction of transactions) {
const wallet = walletManager.findByPublicKey(transaction.senderPublicKey);
wallet.username = transaction.asset.delegate.username;
walletManager.reindex(wallet);
}
for (const block of forgedBlocks) {
const wallet = walletManager.findByPublicKey(block.generatorPublicKey);
wallet.forgedFees = wallet.forgedFees.plus(block.totalFees);
wallet.forgedRewards = wallet.forgedRewards.plus(block.totalRewards);
wallet.producedBlocks = +block.totalProduced;
}
for (const block of lastForgedBlocks) {
const wallet = walletManager.findByPublicKey(block.generatorPublicKey);
wallet.lastBlock = block;
}
walletManager.buildDelegateRanking();
}
public canBeApplied(
transaction: Interfaces.ITransaction,
wallet: State.IWallet,
databaseWalletManager: State.IWalletManager,
): boolean {
const { data }: Interfaces.ITransaction = transaction;
if (databaseWalletManager.findByPublicKey(data.senderPublicKey).multisignature) {
throw new NotSupportedForMultiSignatureWalletError();
}
const { username }: { username: string } = data.asset.delegate;
if (!username) {
throw new WalletUsernameEmptyError();
}
if (wallet.username) {
throw new WalletUsernameNotEmptyError();
}
if (databaseWalletManager.findByUsername(username)) {
throw new WalletUsernameAlreadyRegisteredError(username);
}
return super.canBeApplied(transaction, wallet, databaseWalletManager);
}
public emitEvents(transaction: Interfaces.ITransaction, emitter: EventEmitter.EventEmitter): void {
emitter.emit("delegate.registered", transaction.data);
}
public canEnterTransactionPool(
data: Interfaces.ITransactionData,
pool: TransactionPool.IConnection,
processor: TransactionPool.IProcessor,
): boolean {
if (this.typeFromSenderAlreadyInPool(data, pool, processor)) {
return false;
}
const { username }: { username: string } = data.asset.delegate;
const delegateRegistrationsSameNameInPayload = processor
.getTransactions()
.filter(tx => tx.type === TransactionTypes.DelegateRegistration && tx.asset.delegate.username === username);
if (delegateRegistrationsSameNameInPayload.length > 1) {
processor.pushError(
data,
"ERR_CONFLICT",
`Multiple delegate registrations for "${username}" in transaction payload`,
);
return false;
}
const delegateRegistrationsInPool: Interfaces.ITransactionData[] = Array.from(
pool.getTransactionsByType(TransactionTypes.DelegateRegistration),
).map((memTx: Interfaces.ITransaction) => memTx.data);
const containsDelegateRegistrationForSameNameInPool: boolean = delegateRegistrationsInPool.some(
transaction => transaction.asset.delegate.username === username,
);
if (containsDelegateRegistrationForSameNameInPool) {
processor.pushError(data, "ERR_PENDING", `Delegate registration for "${username}" already in the pool`);
return false;
}
return true;
}
protected applyToSender(transaction: Interfaces.ITransaction, walletManager: State.IWalletManager): void {
super.applyToSender(transaction, walletManager);
const sender: State.IWallet = walletManager.findByPublicKey(transaction.data.senderPublicKey);
sender.username = transaction.data.asset.delegate.username;
walletManager.reindex(sender);
}
protected revertForSender(transaction: Interfaces.ITransaction, walletManager: State.IWalletManager): void {
super.revertForSender(transaction, walletManager);
const sender: State.IWallet = walletManager.findByPublicKey(transaction.data.senderPublicKey);
walletManager.forgetByUsername(sender.username);
sender.username = undefined;
}
protected applyToRecipient(transaction: Interfaces.ITransaction, walletManager: State.IWalletManager): void {
return;
}
protected revertForRecipient(transaction: Interfaces.ITransaction, walletManager: State.IWalletManager): void {
return;
}
}