-
Notifications
You must be signed in to change notification settings - Fork 458
/
txpool.ts
156 lines (136 loc) · 4.66 KB
/
txpool.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright © 2021 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
import { Chain, Transaction, Event, TransactionJSON } from '@liskhq/lisk-chain';
import { TransactionPool } from '@liskhq/lisk-transaction-pool';
import { validator } from '@liskhq/lisk-validator';
import { address as cryptoAddress } from '@liskhq/lisk-cryptography';
import { Broadcaster } from '../generator/broadcaster';
import { InvalidTransactionError } from '../generator/errors';
import {
Address,
getTransactionsFromPoolRequestSchema,
DryRunTransactionRequest,
dryRunTransactionRequestSchema,
DryRunTransactionResponse,
PostTransactionRequest,
postTransactionRequestSchema,
PostTransactionResponse,
} from '../generator/schemas';
import { RequestContext } from '../rpc/rpc_server';
import { ABI, TransactionVerifyResult } from '../../abi';
interface EndpointArgs {
abi: ABI;
pool: TransactionPool;
broadcaster: Broadcaster;
chain: Chain;
}
export class TxpoolEndpoint {
[key: string]: unknown;
private readonly _abi: ABI;
private readonly _pool: TransactionPool;
private readonly _broadcaster: Broadcaster;
private readonly _chain: Chain;
public constructor(args: EndpointArgs) {
this._abi = args.abi;
this._pool = args.pool;
this._broadcaster = args.broadcaster;
this._chain = args.chain;
}
public async postTransaction(ctx: RequestContext): Promise<PostTransactionResponse> {
validator.validate<PostTransactionRequest>(postTransactionRequestSchema, ctx.params);
const req = ctx.params;
const transaction = Transaction.fromBytes(Buffer.from(req.transaction, 'hex'));
const { result, errorMessage } = await this._abi.verifyTransaction({
contextID: Buffer.alloc(0),
transaction: transaction.toObject(),
header: this._chain.lastBlock.header.toObject(),
onlyCommand: false,
});
if (result === TransactionVerifyResult.INVALID) {
throw new InvalidTransactionError(errorMessage, transaction.id);
}
if (this._pool.contains(transaction.id)) {
return {
transactionId: transaction.id.toString('hex'),
};
}
// Broadcast transaction to network if not present in pool
this._broadcaster.enqueueTransactionId(transaction.id);
const { error } = await this._pool.add(transaction);
if (error) {
ctx.logger.error({ err: error }, 'Failed to add transaction to pool.');
throw new InvalidTransactionError(
error.message ?? 'Transaction verification failed.',
transaction.id,
);
}
ctx.logger.info(
{
id: transaction.id,
nonce: transaction.nonce.toString(),
senderPublicKey: transaction.senderPublicKey,
},
'Added transaction to pool',
);
return {
transactionId: transaction.id.toString('hex'),
};
}
// eslint-disable-next-line @typescript-eslint/require-await
public async getTransactionsFromPool(ctx: RequestContext): Promise<TransactionJSON[]> {
validator.validate<Address>(getTransactionsFromPoolRequestSchema, ctx.params);
const { address } = ctx.params;
let transactions = this._pool.getAll();
if (address) {
transactions = transactions.filter(
transaction =>
cryptoAddress.getLisk32AddressFromPublicKey(transaction.senderPublicKey) === address,
);
}
return (transactions as Transaction[]).map(transaction => transaction.toJSON());
}
public async dryRunTransaction(ctx: RequestContext): Promise<DryRunTransactionResponse> {
validator.validate<DryRunTransactionRequest>(dryRunTransactionRequestSchema, ctx.params);
const req = ctx.params;
const transaction = Transaction.fromBytes(Buffer.from(req.transaction, 'hex'));
const header = this._chain.lastBlock.header.toObject();
if (!req.skipVerify) {
const strict = req.strict ?? false;
const { result, errorMessage } = await this._abi.verifyTransaction({
contextID: Buffer.alloc(0),
transaction: transaction.toObject(),
header,
onlyCommand: !strict,
});
if (result === TransactionVerifyResult.INVALID) {
return {
result: TransactionVerifyResult.INVALID,
events: [],
errorMessage,
};
}
}
const response = await this._abi.executeTransaction({
contextID: Buffer.alloc(0),
transaction: transaction.toObject(),
assets: this._chain.lastBlock.assets.getAll(),
dryRun: true,
header,
});
return {
result: response.result,
events: response.events.map(e => new Event(e).toJSON()),
};
}
}