/
block_processing_env.ts
395 lines (373 loc) · 12.8 KB
/
block_processing_env.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* eslint-disable dot-notation */
/*
* 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 * as fs from 'fs-extra';
import * as path from 'path';
import * as os from 'os';
import {
Block,
Chain,
DataAccess,
BlockHeader,
Transaction,
StateStore,
standardEventDataSchema,
} from '@liskhq/lisk-chain';
import { Database, StateDB } from '@liskhq/lisk-db';
import { objects } from '@liskhq/lisk-utils';
import { codec, Schema } from '@liskhq/lisk-codec';
import { BaseModule } from '../modules';
import { channelMock } from './mocks';
import {
defaultConfig,
getGeneratorPrivateKeyFromDefaultConfig,
getKeysFromDefaultConfig,
Keys,
} from './fixtures';
import { removeDB } from './utils';
import { ApplicationConfig, EndpointHandler, EventJSON, GenesisConfig } from '../types';
import { Consensus } from '../engine/consensus';
import { MethodContext, StateMachine } from '../state_machine';
import { Engine } from '../engine';
import {
createImmutableMethodContext,
createNewMethodContext,
} from '../state_machine/method_context';
import { blockAssetsJSON } from './fixtures/genesis-asset';
import { ValidatorsModule } from '../modules/validators';
import { TokenModule } from '../modules/token';
import { AuthModule } from '../modules/auth';
import { FeeModule } from '../modules/fee';
import { RandomModule } from '../modules/random';
import { PoSModule } from '../modules/pos';
import { Generator } from '../engine/generator';
import { ABIHandler } from '../abi_handler/abi_handler';
import { generateGenesisBlock } from '../genesis_block';
import { systemDirs } from '../system_dirs';
import { PrefixedStateReadWriter } from '../state_machine/prefixed_state_read_writer';
import { createLogger } from '../logger';
import { MainchainInteroperabilityModule } from '../modules/interoperability';
import { DynamicRewardModule } from '../modules/dynamic_rewards';
type Options = {
genesis?: GenesisConfig;
modules?: Record<string, Record<string, unknown>>;
databasePath?: string;
passphrase?: string;
};
interface BlockProcessingParams {
modules?: BaseModule[];
options?: Options;
initValidators?: Buffer[];
logLevel?: string;
}
type DecodedEventJSON = EventJSON & { decodedData: Record<string, unknown> };
export interface BlockProcessingEnv {
createBlock: (transactions?: Transaction[], timestamp?: number) => Promise<Block>;
getConsensus: () => Consensus;
getConsensusStore: () => StateStore;
getGenerator: () => Generator;
getGenesisBlock: () => Block;
getChain: () => Chain;
getMethodContext: () => MethodContext;
getBlockchainDB: () => Database;
process: (block: Block) => Promise<void>;
processUntilHeight: (height: number) => Promise<void>;
getLastBlock: () => Block;
getNextValidatorKeys: (blockHeader: BlockHeader, distance?: number) => Promise<Keys>;
getDataAccess: () => DataAccess;
getEvents: (height: number, module?: string, name?: string) => Promise<DecodedEventJSON[]>;
getChainID: () => Buffer;
invoke: <T = void>(path: string, params?: Record<string, unknown>) => Promise<T>;
cleanup: (config: Options) => void;
}
const getAppConfig = (
genesisConfig?: Partial<GenesisConfig>,
moduleConfig?: Record<string, Record<string, unknown>>,
): ApplicationConfig => {
const mergedConfig = objects.mergeDeep(
{},
{
...defaultConfig,
system: {
...defaultConfig.system,
dataPath: path.join(os.tmpdir(), `lisk-framework-test-${Date.now().toString()}`),
},
genesis: {
...defaultConfig.genesis,
...(genesisConfig ?? {}),
},
modules: {
...(moduleConfig ?? {}),
},
},
) as ApplicationConfig;
return mergedConfig;
};
const getNextTimestamp = (engine: Engine, previousBlock: BlockHeader, distance = 1) => {
const previousSlotNumber = engine['_consensus']['_bft'].method.getSlotNumber(
previousBlock.timestamp,
);
return engine['_consensus']['_bft'].method.getSlotTime(previousSlotNumber + distance);
};
const createProcessableBlock = async (
engine: Engine,
transactions: Transaction[],
timestamp?: number,
): Promise<Block> => {
engine['_logger'].info({ numberOfTransactions: transactions.length }, 'Start generating block');
// Get previous block and generate valid timestamp, seed reveal, maxHeightPrevoted, reward and maxHeightPreviouslyForged
const stateStore = new StateStore(engine['_blockchainDB']);
const previousBlockHeader = engine['_chain'].lastBlock.header;
const nextTimestamp = timestamp ?? getNextTimestamp(engine, previousBlockHeader);
const generator = await engine['_consensus']['_bft'].method.getGeneratorAtTimestamp(
stateStore,
previousBlockHeader.height + 1,
nextTimestamp,
);
const generatorPrivateKey = getGeneratorPrivateKeyFromDefaultConfig(generator.address);
for (const tx of transactions) {
await engine['_generator']['_pool'].add(tx);
}
const block = await engine.generateBlock({
generatorAddress: generator.address,
height: previousBlockHeader.height + 1,
privateKey: generatorPrivateKey,
timestamp: nextTimestamp,
transactions,
});
engine['_logger'].info(
{ height: block.header.height, numberOfTransactions: block.transactions.length },
'Generated block',
);
return block;
};
export const getBlockProcessingEnv = async (
params: BlockProcessingParams,
): Promise<BlockProcessingEnv> => {
const chainID = Buffer.from('00000000', 'hex');
const appConfig = getAppConfig(
params.options?.genesis
? {
...params.options.genesis,
chainID: chainID.toString('hex'),
}
: { chainID: chainID.toString('hex') },
params.options?.modules ?? {},
);
const systemDir = systemDirs(appConfig.system.dataPath);
removeDB(systemDir.data);
const moduleDB = new Database(path.join(systemDir.data, 'module.db'));
const stateDB = new StateDB(path.join(systemDir.data, 'state.db'));
const validatorsModule = new ValidatorsModule();
const authModule = new AuthModule();
const tokenModule = new TokenModule();
const feeModule = new FeeModule();
const rewardModule = new DynamicRewardModule();
const randomModule = new RandomModule();
const posModule = new PoSModule();
const interopModule = new MainchainInteroperabilityModule();
const modules = [
validatorsModule,
authModule,
tokenModule,
feeModule,
rewardModule,
randomModule,
posModule,
];
const stateMachine = new StateMachine();
const logger = createLogger({ name: 'blockProcessingEnv', logLevel: params.logLevel ?? 'none' });
// resolve dependencies
tokenModule.addDependencies(interopModule.method, feeModule.method);
feeModule.addDependencies(tokenModule.method, interopModule.method);
rewardModule.addDependencies(
tokenModule.method,
randomModule.method,
validatorsModule.method,
posModule.method,
);
posModule.addDependencies(
randomModule.method,
validatorsModule.method,
tokenModule.method,
feeModule.method,
);
// register modules
stateMachine.registerModule(feeModule);
stateMachine.registerModule(authModule);
stateMachine.registerModule(validatorsModule);
stateMachine.registerModule(tokenModule);
stateMachine.registerModule(rewardModule);
stateMachine.registerModule(randomModule);
stateMachine.registerModule(posModule);
const blockAssets = blockAssetsJSON.map(asset => ({
...asset,
data: codec.fromJSON<Record<string, unknown>>(asset.schema, asset.data),
}));
await stateMachine.init(logger, appConfig.genesis, appConfig.modules);
const genesisBlock = await generateGenesisBlock(stateMachine, logger, {
timestamp: Math.floor(Date.now() / 1000) - 60 * 60 * 12,
assets: blockAssets,
chainID,
});
const abiHandler = new ABIHandler({
channel: channelMock,
config: appConfig,
logger,
stateDB,
moduleDB,
modules,
stateMachine,
chainID,
});
appConfig.genesis.block.blob = genesisBlock.getBytes().toString('hex');
appConfig.generator.keys.fromFile = path.join(__dirname, './fixtures/keys_fixture.json');
const engine = new Engine(abiHandler, appConfig);
await engine['_init']();
engine['_logger'] = logger;
engine['_consensus']['_logger'] = logger;
await abiHandler.init({
chainID,
lastBlockHeight: engine['_chain'].lastBlock.header.height,
lastStateRoot: engine['_chain'].lastBlock.header.stateRoot as Buffer,
});
return {
createBlock: async (transactions: Transaction[] = [], timestamp?: number): Promise<Block> =>
createProcessableBlock(engine, transactions, timestamp),
getGenesisBlock: () => genesisBlock,
getChain: () => engine['_chain'],
getConsensus: () => engine['_consensus'],
getConsensusStore: () => new StateStore(engine['_blockchainDB']),
getGenerator: () => engine['_generator'],
getMethodContext: () => createNewMethodContext(stateDB.newReadWriter()),
getBlockchainDB: () => engine['_blockchainDB'],
process: async (block): Promise<void> => {
logger.debug(
{ height: block.header.height, numberOfTransactions: block.transactions.length },
'Start executing block',
);
await engine['_consensus']['_execute'](block, 'peer-id');
logger.debug(
{ height: block.header.height, numberOfTransactions: block.transactions.length },
'Executed block',
);
},
processUntilHeight: async (height): Promise<void> => {
while (engine['_chain'].lastBlock.header.height < height) {
const nextBlock = await createProcessableBlock(engine, []);
await engine['_consensus'].execute(nextBlock);
}
},
getLastBlock: () => engine['_chain'].lastBlock,
getNextValidatorKeys: async (previousBlockHeader: BlockHeader, distance = 1): Promise<Keys> => {
const stateStore = new StateStore(engine['_blockchainDB']);
const nextTimestamp = getNextTimestamp(engine, previousBlockHeader, distance);
const validator = await engine['_consensus']['_bft'].method.getGeneratorAtTimestamp(
stateStore,
previousBlockHeader.height + distance,
nextTimestamp,
);
const keys = getKeysFromDefaultConfig(validator.address);
return keys;
},
getEvents: async (
height: number,
module?: string,
name?: string,
): Promise<DecodedEventJSON[]> => {
const handler = engine['_rpcServer']['_getHandler']('chain', 'getEvents');
if (!handler) {
throw new Error('invalid');
}
const resp = (await handler({
logger,
chainID: engine['_chain'].chainID,
params: { height },
})) as EventJSON[];
const eventMetadata = stateMachine['_modules']
.map(m => ({ module: m.name, ...m.metadata().events } as Record<string, unknown>))
.flat();
const results: DecodedEventJSON[] = [];
for (const event of resp) {
if (module && event.module !== module) {
continue;
}
if (name && event.name !== name) {
continue;
}
if (event.name === 'commandExecutionResult') {
results.push({
...event,
decodedData: codec.decodeJSON(standardEventDataSchema, Buffer.from(event.data, 'hex')),
});
continue;
}
const metadata = eventMetadata.find(
e => e.module === event.module && e.name === event.name,
);
results.push({
...event,
decodedData: metadata
? codec.decodeJSON(metadata.schema as Schema, Buffer.from(event.data, 'hex'))
: {},
});
}
return results;
},
async invoke<T = void>(func: string, input: Record<string, unknown> = {}): Promise<T> {
const [namespace, method] = func.split('_');
const handler = engine['_rpcServer']['_getHandler'](namespace, method);
if (handler) {
const resp = (await handler({
logger,
chainID: engine['_chain'].chainID,
params: input,
})) as T;
return resp;
}
const moduleIndex = modules.findIndex(mod => mod.name === namespace);
if (moduleIndex < 0) {
throw new Error(`namespace ${namespace} is not registered`);
}
const moduleHandler = modules[moduleIndex].endpoint[method] as EndpointHandler | undefined;
if (!moduleHandler) {
throw new Error(`Method ${method} in namespace ${namespace} is not registered`);
}
const bindedHandler = moduleHandler.bind(modules[moduleIndex].endpoint);
const stateStore = new PrefixedStateReadWriter(stateDB.newReadWriter());
try {
const result = await bindedHandler({
getStore: (moduleID: Buffer, storePrefix: Buffer) =>
stateStore.getStore(moduleID, storePrefix),
getImmutableMethodContext: () => createImmutableMethodContext(stateStore),
logger: engine['_logger'],
chainID: engine['_chain'].chainID,
params: input,
});
return result as T;
} finally {
stateStore.inner.close();
}
},
getChainID: () => chainID,
getDataAccess: () => engine['_chain'].dataAccess,
cleanup: (_val): void => {
engine['_closeDB']();
moduleDB.close();
stateDB.close();
fs.removeSync(systemDir.data);
},
};
};