Skip to content

Commit 5d7d3d6

Browse files
committed
refactor: replace proxy services with container bindings
1 parent 3c12f5a commit 5d7d3d6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+240
-349
lines changed

packages/core-api/src/handlers/blocks/controller.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ export class BlocksController extends Controller {
1919
public async first(request: Hapi.Request, h: Hapi.ResponseToolkit) {
2020
try {
2121
return super.respondWithResource(
22-
app
23-
.get<Contracts.State.StateService>(Container.Identifiers.StateService)
24-
.getStore()
25-
.getGenesisBlock().data,
22+
app.get<Contracts.State.StateStore>(Container.Identifiers.StateStore).getGenesisBlock().data,
2623
"block",
2724
(request.query.transform as unknown) as boolean,
2825
);

packages/core-api/src/handlers/node/controller.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export class NodeController extends Controller {
99
public async status(request: Hapi.Request, h: Hapi.ResponseToolkit) {
1010
try {
1111
const lastBlock = this.blockchain.getLastBlock();
12-
const networkHeight = this.blockchain.p2p.getMonitor().getNetworkHeight();
12+
const networkHeight = app
13+
.get<Contracts.P2P.NetworkMonitor>(Container.Identifiers.PeerNetworkMonitor)
14+
.getNetworkHeight();
1315

1416
return {
1517
data: {
@@ -27,7 +29,9 @@ export class NodeController extends Controller {
2729
public async syncing(request: Hapi.Request, h: Hapi.ResponseToolkit) {
2830
try {
2931
const lastBlock = this.blockchain.getLastBlock();
30-
const networkHeight = this.blockchain.p2p.getMonitor().getNetworkHeight();
32+
const networkHeight = app
33+
.get<Contracts.P2P.NetworkMonitor>(Container.Identifiers.PeerNetworkMonitor)
34+
.getNetworkHeight();
3135

3236
return {
3337
data: {

packages/core-api/src/handlers/peers/controller.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Contracts } from "@arkecosystem/core-kernel";
1+
import { app, Container, Contracts } from "@arkecosystem/core-kernel";
22
import Boom from "@hapi/boom";
33
import Hapi from "@hapi/hapi";
44
import semver from "semver";
@@ -7,7 +7,9 @@ import { Controller } from "../shared/controller";
77

88
export class PeersController extends Controller {
99
public async index(request: Hapi.Request, h: Hapi.ResponseToolkit) {
10-
const allPeers: Contracts.P2P.Peer[] = this.blockchain.p2p.getStorage().getPeers();
10+
const allPeers: Contracts.P2P.Peer[] = app
11+
.get<Contracts.P2P.PeerStorage>(Container.Identifiers.PeerStorage)
12+
.getPeers();
1113

1214
let result = allPeers.sort((a, b) => a.latency - b.latency);
1315
result = request.query.version
@@ -35,7 +37,10 @@ export class PeersController extends Controller {
3537

3638
public async show(request: Hapi.Request, h: Hapi.ResponseToolkit) {
3739
try {
38-
const peers: Contracts.P2P.Peer[] = this.blockchain.p2p.getStorage().getPeers();
40+
const peers: Contracts.P2P.Peer[] = app
41+
.get<Contracts.P2P.PeerStorage>(Container.Identifiers.PeerStorage)
42+
.getPeers();
43+
3944
const peer: Contracts.P2P.Peer = peers.find(p => p.ip === request.params.ip);
4045

4146
if (!peer) {

packages/core-api/src/handlers/transactions/controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ export class TransactionsController extends Controller {
2727
const result = await processor.validate((request.payload as any).transactions);
2828

2929
if (result.broadcast.length > 0) {
30-
app.get<Contracts.P2P.PeerService>(Container.Identifiers.PeerService)
31-
.getMonitor()
32-
.broadcastTransactions(processor.getBroadcastTransactions());
30+
app.get<Contracts.P2P.NetworkMonitor>(Container.Identifiers.PeerNetworkMonitor).broadcastTransactions(
31+
processor.getBroadcastTransactions(),
32+
);
3333
}
3434

3535
return {

packages/core-blockchain/src/blockchain.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,14 @@ import { stateMachine } from "./state-machine";
88

99
const { BlockFactory } = Blocks;
1010

11+
@Container.injectable()
1112
export class Blockchain implements Contracts.Blockchain.Blockchain {
1213
/**
1314
* Get the state of the blockchain.
1415
* @return {StateStore}
1516
*/
1617
get state(): Contracts.State.StateStore {
17-
return app.get<Contracts.State.StateService>(Container.Identifiers.StateService).getStore();
18-
}
19-
20-
/**
21-
* Get the network (p2p) interface.
22-
* @return {PeerService}
23-
*/
24-
get p2p(): Contracts.P2P.PeerService {
25-
return app.get<Contracts.P2P.PeerService>(Container.Identifiers.PeerService);
18+
return app.get<Contracts.State.StateStore>(Container.Identifiers.StateStore);
2619
}
2720

2821
/**
@@ -52,14 +45,15 @@ export class Blockchain implements Contracts.Blockchain.Blockchain {
5245
* @param {Object} options
5346
* @return {void}
5447
*/
55-
constructor(options: { networkStart?: boolean }) {
48+
init(options: { networkStart?: boolean }): this {
5649
// flag to force a network start
5750
this.state.networkStart = !!options.networkStart;
5851

5952
if (this.state.networkStart) {
6053
app.log.warning(
6154
"ARK Core is launched in Genesis Start mode. This is usually for starting the first node on the blockchain. Unless you know what you are doing, this is likely wrong.",
6255
);
56+
6357
app.log.info("Starting ARK Core for a new world, welcome aboard");
6458
}
6559

@@ -80,6 +74,8 @@ export class Blockchain implements Contracts.Blockchain.Blockchain {
8074

8175
// @ts-ignore
8276
this.queue.drain(() => this.dispatch("PROCESSFINISHED"));
77+
78+
return this;
8379
}
8480

8581
/**
@@ -138,7 +134,10 @@ export class Blockchain implements Contracts.Blockchain.Blockchain {
138134
await delay(1000);
139135
}
140136

141-
this.p2p.getMonitor().cleansePeers({ forcePing: true, peerCount: 10 });
137+
app.get<Contracts.P2P.NetworkMonitor>(Container.Identifiers.PeerNetworkMonitor).cleansePeers({
138+
forcePing: true,
139+
peerCount: 10,
140+
});
142141

143142
return true;
144143
}
@@ -179,7 +178,7 @@ export class Blockchain implements Contracts.Blockchain.Blockchain {
179178
* @return {void}
180179
*/
181180
public async updateNetworkStatus(): Promise<void> {
182-
await this.p2p.getMonitor().updateNetworkStatus();
181+
await app.get<Contracts.P2P.NetworkMonitor>(Container.Identifiers.PeerNetworkMonitor).updateNetworkStatus();
183182
}
184183

185184
/**
@@ -417,7 +416,9 @@ export class Blockchain implements Contracts.Blockchain.Blockchain {
417416
const blocktime: number = Managers.configManager.getMilestone(currentBlock.data.height).blocktime;
418417

419418
if (this.state.started && Crypto.Slots.getSlotNumber() * blocktime <= currentBlock.data.timestamp) {
420-
this.p2p.getMonitor().broadcastBlock(currentBlock);
419+
app.get<Contracts.P2P.NetworkMonitor>(Container.Identifiers.PeerNetworkMonitor).broadcastBlock(
420+
currentBlock,
421+
);
421422
}
422423
}
423424

@@ -458,7 +459,7 @@ export class Blockchain implements Contracts.Blockchain.Blockchain {
458459
* Determine if the blockchain is synced.
459460
*/
460461
public isSynced(block?: Interfaces.IBlockData): boolean {
461-
if (!this.p2p.getStorage().hasPeers()) {
462+
if (!app.get<Contracts.P2P.PeerStorage>(Container.Identifiers.PeerStorage).hasPeers()) {
462463
return true;
463464
}
464465

packages/core-blockchain/src/replay/replay-blockchain.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class ReplayBlockchain extends Blockchain {
2121
}
2222

2323
public constructor() {
24-
super({});
24+
super();
2525

2626
this.walletRepository = new Wallets.WalletRepository();
2727
this.walletState = app.resolve<Wallets.WalletState>(Wallets.WalletState).init(this.walletRepository);
@@ -36,10 +36,6 @@ export class ReplayBlockchain extends Blockchain {
3636
this.queue.drain(() => undefined);
3737
}
3838

39-
public get p2p(): Contracts.P2P.PeerService {
40-
return undefined;
41-
}
42-
4339
public get transactionPool(): Contracts.TransactionPool.Connection {
4440
return undefined;
4541
}

packages/core-blockchain/src/service-provider.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@ import { ReplayBlockchain } from "./replay";
66

77
export class ServiceProvider extends Providers.ServiceProvider {
88
public async register(): Promise<void> {
9+
this.app.bind("blockchain.options").toConstantValue(this.config().all());
10+
911
const blockchain: Blockchain = this.config().get("replay")
1012
? new ReplayBlockchain()
11-
: new Blockchain(this.config().all());
13+
: this.app.resolve<Blockchain>(Blockchain).init(this.config().all());
1214

13-
this.app
14-
.get<Contracts.State.StateService>(Container.Identifiers.StateService)
15-
.getStore()
16-
.reset(blockchainMachine);
15+
this.app.get<Contracts.State.StateStore>(Container.Identifiers.StateStore).reset(blockchainMachine);
1716

1817
if (!process.env.CORE_SKIP_BLOCKCHAIN && !this.config().get("replay")) {
1918
await blockchain.start();
2019
}
2120

2221
this.app.bind(Container.Identifiers.BlockchainService).toConstantValue(blockchain);
23-
this.app.bind("blockchain.options").toConstantValue(this.config().all());
2422
}
2523

2624
public async dispose(): Promise<void> {

packages/core-blockchain/src/state-machine.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ blockchainMachine.actionMap = (blockchain: Blockchain) => ({
5252
if (stateStorage.p2pUpdateCounter + 1 > 3) {
5353
logger.info("Network keeps missing blocks.");
5454

55-
const networkStatus = await blockchain.p2p.getMonitor().checkNetworkHealth();
55+
const networkStatus = await app
56+
.get<Contracts.P2P.NetworkMonitor>(Container.Identifiers.PeerNetworkMonitor)
57+
.checkNetworkHealth();
5658
if (networkStatus.forked) {
5759
stateStorage.numberOfBlocksToRollback = networkStatus.blocksToRollback;
5860
event = "FORK";
@@ -112,7 +114,7 @@ blockchainMachine.actionMap = (blockchain: Blockchain) => ({
112114

113115
async init() {
114116
logger = app.log;
115-
stateStorage = app.get<Contracts.State.StateService>(Container.Identifiers.StateService).getStore();
117+
stateStorage = app.get<Contracts.State.StateStore>(Container.Identifiers.StateStore);
116118
blockchainMachine.state = stateStorage;
117119

118120
try {
@@ -154,7 +156,7 @@ blockchainMachine.actionMap = (blockchain: Blockchain) => ({
154156
await blockchain.database.buildWallets();
155157
await blockchain.database.applyRound(block.data.height);
156158
await blockchain.transactionPool.buildWallets();
157-
await blockchain.p2p.getMonitor().start();
159+
await app.get<Contracts.P2P.NetworkMonitor>(Container.Identifiers.PeerNetworkMonitor).start();
158160

159161
return blockchain.dispatch("STARTED");
160162
}
@@ -164,7 +166,7 @@ blockchainMachine.actionMap = (blockchain: Blockchain) => ({
164166

165167
stateStorage.setLastBlock(BlockFactory.fromJson(Managers.configManager.get("genesisBlock")));
166168
await blockchain.database.buildWallets();
167-
await blockchain.p2p.getMonitor().start();
169+
await app.get<Contracts.P2P.NetworkMonitor>(Container.Identifiers.PeerNetworkMonitor).start();
168170

169171
return blockchain.dispatch("STARTED");
170172
}
@@ -180,7 +182,7 @@ blockchainMachine.actionMap = (blockchain: Blockchain) => ({
180182
await blockchain.database.restoreCurrentRound(block.data.height);
181183
await blockchain.transactionPool.buildWallets();
182184

183-
await blockchain.p2p.getMonitor().start();
185+
await app.get<Contracts.P2P.NetworkMonitor>(Container.Identifiers.PeerNetworkMonitor).start();
184186

185187
return blockchain.dispatch("STARTED");
186188
} catch (error) {
@@ -193,8 +195,8 @@ blockchainMachine.actionMap = (blockchain: Blockchain) => ({
193195
async downloadBlocks() {
194196
const lastDownloadedBlock: Interfaces.IBlockData =
195197
stateStorage.lastDownloadedBlock || stateStorage.getLastBlock().data;
196-
const blocks: Interfaces.IBlockData[] = await blockchain.p2p
197-
.getMonitor()
198+
const blocks: Interfaces.IBlockData[] = await app
199+
.get<Contracts.P2P.NetworkMonitor>(Container.Identifiers.PeerNetworkMonitor)
198200
.syncWithNetwork(lastDownloadedBlock.height);
199201

200202
if (blockchain.isStopped) {
@@ -271,7 +273,7 @@ blockchainMachine.actionMap = (blockchain: Blockchain) => ({
271273
logger.info(`Removed ${AppUtils.pluralize("block", random, true)}`);
272274

273275
await blockchain.transactionPool.buildWallets();
274-
await blockchain.p2p.getMonitor().refreshPeersAfterFork();
276+
await app.get<Contracts.P2P.NetworkMonitor>(Container.Identifiers.PeerNetworkMonitor).refreshPeersAfterFork();
275277

276278
blockchain.dispatch("SUCCESS");
277279
},

packages/core-blockchain/src/utils/tick-sync-tracker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ export const tickSyncTracker = (blockCount, count): void => {
88
tracker = {
99
start: new Date().getTime(),
1010
networkHeight: app
11-
.get<Contracts.P2P.PeerService>(Container.Identifiers.PeerService)
12-
.getMonitor()
11+
.get<Contracts.P2P.NetworkMonitor>(Container.Identifiers.PeerNetworkMonitor)
1312
.getNetworkHeight(),
1413
blocksInitial: +count,
1514
blocksDownloaded: +count,

packages/core-database/src/database-service.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
5151
}
5252

5353
public async init(): Promise<void> {
54-
app.get<Contracts.State.StateService>(Container.Identifiers.StateService)
55-
.getStore()
56-
.setGenesisBlock(Blocks.BlockFactory.fromJson(Managers.configManager.get("genesisBlock")));
54+
app.get<Contracts.State.StateStore>(Container.Identifiers.StateStore).setGenesisBlock(
55+
Blocks.BlockFactory.fromJson(Managers.configManager.get("genesisBlock")),
56+
);
5757

5858
if (process.env.CORE_RESET_DATABASE) {
5959
await this.reset();
@@ -228,8 +228,7 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
228228
const end: number = offset + limit - 1;
229229

230230
let blocks: Interfaces.IBlockData[] = app
231-
.get<Contracts.State.StateService>(Container.Identifiers.StateService)
232-
.getStore()
231+
.get<Contracts.State.StateStore>(Container.Identifiers.StateStore)
233232
.getLastBlocksByHeight(start, end, headersOnly);
234233

235234
if (blocks.length !== limit) {
@@ -292,8 +291,7 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
292291

293292
for (const [i, height] of heights.entries()) {
294293
const stateBlocks = app
295-
.get<Contracts.State.StateService>(Container.Identifiers.StateService)
296-
.getStore()
294+
.get<Contracts.State.StateStore>(Container.Identifiers.StateStore)
297295
.getLastBlocksByHeight(height, height, true);
298296

299297
if (Array.isArray(stateBlocks) && stateBlocks.length > 0) {
@@ -320,8 +318,7 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
320318

321319
public async getBlocksForRound(roundInfo?: Contracts.Shared.RoundInfo): Promise<Interfaces.IBlock[]> {
322320
let lastBlock: Interfaces.IBlock = app
323-
.get<Contracts.State.StateService>(Container.Identifiers.StateService)
324-
.getStore()
321+
.get<Contracts.State.StateStore>(Container.Identifiers.StateStore)
325322
.getLastBlock();
326323

327324
if (!lastBlock) {
@@ -372,8 +369,7 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
372369

373370
public async getCommonBlocks(ids: string[]): Promise<Interfaces.IBlockData[]> {
374371
let commonBlocks: Interfaces.IBlockData[] = app
375-
.get<Contracts.State.StateService>(Container.Identifiers.StateService)
376-
.getStore()
372+
.get<Contracts.State.StateStore>(Container.Identifiers.StateStore)
377373
.getCommonBlocks(ids);
378374

379375
if (commonBlocks.length < ids.length) {
@@ -385,8 +381,7 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
385381

386382
public async getRecentBlockIds(): Promise<string[]> {
387383
let blocks: any[] = app
388-
.get<Contracts.State.StateService>(Container.Identifiers.StateService)
389-
.getStore()
384+
.get<Contracts.State.StateStore>(Container.Identifiers.StateStore)
390385
.getLastBlockIds()
391386
.reverse()
392387
.slice(0, 10);
@@ -653,8 +648,7 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
653648

654649
private async createGenesisBlock(): Promise<Interfaces.IBlock> {
655650
const genesisBlock: Interfaces.IBlock = app
656-
.get<Contracts.State.StateService>(Container.Identifiers.StateService)
657-
.getStore()
651+
.get<Contracts.State.StateStore>(Container.Identifiers.StateStore)
658652
.getGenesisBlock();
659653

660654
await this.saveBlock(genesisBlock);
@@ -663,17 +657,13 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
663657
}
664658

665659
private configureState(lastBlock: Interfaces.IBlock): void {
666-
const state: Contracts.State.StateService = app.get<Contracts.State.StateService>(
667-
Container.Identifiers.StateService,
668-
);
669-
670-
state.getStore().setLastBlock(lastBlock);
660+
app.get<Contracts.State.StateStore>(Container.Identifiers.StateStore).setLastBlock(lastBlock);
671661

672662
const { blocktime, block } = Managers.configManager.getMilestone();
673663

674664
const blocksPerDay: number = Math.ceil(86400 / blocktime);
675-
state.getBlocks().resize(blocksPerDay);
676-
state.getTransactions().resize(blocksPerDay * block.maxTransactions);
665+
app.get<any>(Container.Identifiers.StateBlockStore).resize(blocksPerDay);
666+
app.get<any>(Container.Identifiers.StateTransactionStore).resize(blocksPerDay * block.maxTransactions);
677667
}
678668

679669
private async initializeActiveDelegates(height: number): Promise<void> {

packages/core-kernel/src/bootstrap/service-providers/register-service-providers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export class RegisterServiceProviders implements Bootstrapper {
5656
await serviceProviders.register(name);
5757
}
5858
} catch (error) {
59+
console.log(error); // remove after debug
60+
5961
// Determine if the plugin is required to decide how to handle errors.
6062
const isRequired: boolean = await serviceProvider.required();
6163

0 commit comments

Comments
 (0)