/
database-service.test.ts
289 lines (232 loc) · 12.2 KB
/
database-service.test.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
import "jest-extended";
import "./mocks/core-container";
import { app } from "@arkecosystem/core-container";
import { Database, EventEmitter } from "@arkecosystem/core-interfaces";
import { TransactionHandlerRegistry } from "@arkecosystem/core-transactions";
import { Address, Bignum, constants, models, Transaction, transactionBuilder } from "@arkecosystem/crypto";
import { Wallet, WalletManager } from "../../../packages/core-database/src";
import { DatabaseService } from "../../../packages/core-database/src/database-service";
import { roundCalculator } from "../../../packages/core-utils/dist";
import { genesisBlock } from "../../utils/fixtures/testnet/block-model";
import { DatabaseConnectionStub } from "./__fixtures__/database-connection-stub";
import { StateStorageStub } from "./__fixtures__/state-storage-stub";
const { Block } = models;
const { SATOSHI, TransactionTypes } = constants;
let connection: Database.IConnection;
let databaseService: DatabaseService;
let walletManager: Database.IWalletManager;
let container;
let emitter: EventEmitter.EventEmitter;
beforeAll(() => {
container = app;
// @ts-ignore
emitter = container.resolvePlugin<EventEmitter.EventEmitter>("event-emitter");
connection = new DatabaseConnectionStub();
walletManager = new WalletManager();
});
beforeEach(() => {
jest.restoreAllMocks();
});
function createService() {
const service = new DatabaseService({}, connection, walletManager, null, null, null, null);
service.emitter = emitter;
return service;
}
describe("Database Service", () => {
it("should listen for emitter events during constructor", () => {
jest.spyOn(emitter, "on");
jest.spyOn(emitter, "once");
databaseService = createService();
expect(emitter.on).toHaveBeenCalledWith("state.started", expect.toBeFunction());
expect(emitter.on).toHaveBeenCalledWith("wallet.created.cold", expect.toBeFunction());
});
describe("applyBlock", () => {
it("should applyBlock", async () => {
jest.spyOn(walletManager, "applyBlock").mockImplementation(block => block);
jest.spyOn(emitter, "emit");
databaseService = createService();
jest.spyOn(databaseService, "applyRound").mockImplementation(() => null); // test applyRound logic separately
await databaseService.applyBlock(genesisBlock);
expect(walletManager.applyBlock).toHaveBeenCalledWith(genesisBlock);
expect(emitter.emit).toHaveBeenCalledWith("block.applied", genesisBlock.data);
genesisBlock.transactions.forEach(tx =>
expect(emitter.emit).toHaveBeenCalledWith("transaction.applied", tx.data),
);
});
});
describe("getBlocksByHeight", () => {
it("should deliver blocks for the given heights", async () => {
const requestHeightsLow = [1, 5, 20];
const requestHeightsHigh = [100, 200, 500];
const stateStorageStub = new StateStorageStub();
// @ts-ignore
jest.spyOn(stateStorageStub, "getLastBlocksByHeight").mockImplementation((heightFrom, heightTo) => {
if (requestHeightsHigh[0] <= heightFrom) {
return [{ height: heightFrom, fromState: true }];
}
return undefined;
});
jest.spyOn(container, "has").mockReturnValue(true);
jest.spyOn(container, "resolve").mockReturnValue(stateStorageStub);
databaseService = createService();
connection.blocksRepository = {
findByHeights: (heights: number[]) => heights.map(h => ({ height: Number(h), fromDb: true })),
} as any; // FIXME: use Database.IBlocksRepository
let requestHeights = requestHeightsHigh;
let blocks = await databaseService.getBlocksByHeight(requestHeights);
expect(stateStorageStub.getLastBlocksByHeight).toHaveBeenCalled();
expect(blocks).toBeArray();
expect(blocks.length).toBe(requestHeights.length);
for (let i = 0; i < requestHeights.length; i++) {
expect(blocks[i].height).toBe(requestHeights[i]);
expect(blocks[i].fromState).toBe(true);
}
requestHeights = [...requestHeightsLow, ...requestHeightsHigh];
blocks = await databaseService.getBlocksByHeight(requestHeights);
expect(stateStorageStub.getLastBlocksByHeight).toHaveBeenCalled();
expect(blocks).toBeArray();
expect(blocks.length).toBe(requestHeights.length);
for (let i = 0; i < requestHeights.length; i++) {
expect(blocks[i].height).toBe(requestHeights[i]);
if (requestHeightsHigh.includes(requestHeights[i])) {
expect(blocks[i].fromState).toBe(true);
} else {
expect(blocks[i].fromDb).toBe(true);
}
}
jest.spyOn(container, "has").mockReturnValue(false);
blocks = await databaseService.getBlocksByHeight(requestHeights);
expect(blocks).toBeArray();
expect(blocks.length).toBe(requestHeights.length);
for (let i = 0; i < requestHeights.length; i++) {
expect(blocks[i].height).toBe(requestHeights[i]);
expect(blocks[i].fromDb).toBe(true);
}
});
});
describe("getBlocksForRound", () => {
it("should fetch blocks using lastBlock in state-storage", async () => {
const stateStorageStub = new StateStorageStub();
jest.spyOn(stateStorageStub, "getLastBlock").mockReturnValue(null);
jest.spyOn(container, "has").mockReturnValue(true);
jest.spyOn(container, "resolve").mockReturnValue(stateStorageStub);
databaseService = createService();
jest.spyOn(databaseService, "getLastBlock").mockReturnValue(null);
const blocks = await databaseService.getBlocksForRound();
expect(blocks).toBeEmpty();
expect(stateStorageStub.getLastBlock).toHaveBeenCalled();
expect(databaseService.getLastBlock).not.toHaveBeenCalled();
});
it("should fetch blocks using lastBlock in database", async () => {
jest.spyOn(container, "has").mockReturnValue(false);
databaseService = createService();
jest.spyOn(databaseService, "getLastBlock").mockReturnValue(null);
const blocks = await databaseService.getBlocksForRound();
expect(blocks).toBeEmpty();
expect(databaseService.getLastBlock).toHaveBeenCalled();
});
it("should fetch blocks from lastBlock height", async () => {
databaseService = createService();
// @ts-ignore
jest.spyOn(databaseService, "getLastBlock").mockReturnValue(genesisBlock);
// @ts-ignore
jest.spyOn(databaseService, "getBlocks").mockReturnValue([]);
jest.spyOn(container, "has").mockReturnValue(false);
const blocks = await databaseService.getBlocksForRound();
expect(blocks).toBeEmpty();
expect(databaseService.getBlocks).toHaveBeenCalledWith(
1,
container.getConfig().getMilestone(genesisBlock.data.height).activeDelegates,
);
});
});
/* TODO: Testing a method that's private. This needs a replacement by testing a public method instead */
describe("calcPreviousActiveDelegates", () => {
it("should calculate the previous delegate list", async () => {
databaseService = createService();
walletManager = new WalletManager();
const initialHeight = 52;
// Create delegates
for (const transaction of genesisBlock.transactions) {
if (transaction.type === TransactionTypes.DelegateRegistration) {
const wallet = walletManager.findByPublicKey(transaction.data.senderPublicKey);
wallet.username = Transaction.fromBytes(transaction.serialized).data.asset.delegate.username;
wallet.address = Address.fromPublicKey(transaction.data.senderPublicKey);
walletManager.reindex(wallet);
}
}
const keys = {
passphrase: "this is a secret passphrase",
publicKey: "02c71ab1a1b5b7c278145382eb0b535249483b3c4715a4fe6169d40388bbb09fa7",
privateKey: "dcf4ead2355090279aefba91540f32e93b15c541ecb48ca73071f161b4f3e2e3",
address: "D64cbDctaiADEH7NREnvRQGV27bnb1v2kE",
};
// Beginning of round 2 with all delegates 0 vote balance.
const roundInfo1 = roundCalculator.calculateRound(initialHeight);
const delegatesRound2 = walletManager.loadActiveDelegateList(roundInfo1);
// Prepare sender wallet
const transactionHandler = TransactionHandlerRegistry.get(TransactionTypes.Transfer);
const originalApply = transactionHandler.canBeApplied;
transactionHandler.canBeApplied = jest.fn(() => true);
const sender = new Wallet(keys.address);
sender.publicKey = keys.publicKey;
walletManager.reindex(sender);
// Apply 51 blocks, where each increases the vote balance of a delegate to
// reverse the current delegate order.
const blocksInRound = [];
for (let i = 0; i < 51; i++) {
const transfer = transactionBuilder
.transfer()
.amount((i + 1) * SATOSHI)
.recipientId(delegatesRound2[i].address)
.sign(keys.passphrase)
.build();
// Vote for itself
walletManager.findByPublicKey(delegatesRound2[i].publicKey).vote = delegatesRound2[i].publicKey;
// walletManager.byPublicKey[delegatesRound2[i].publicKey].vote = delegatesRound2[i].publicKey;
const block = Block.create(
{
version: 0,
timestamp: 0,
height: initialHeight + i,
numberOfTransactions: 1,
totalAmount: transfer.data.amount,
totalFee: new Bignum(0.1),
reward: new Bignum(2),
payloadLength: 0,
payloadHash: "a".repeat(64),
transactions: [transfer.data],
},
keys,
);
block.data.generatorPublicKey = keys.publicKey;
walletManager.applyBlock(block);
blocksInRound.push(block);
}
// The delegates from round 2 are now reversed in rank in round 3.
const roundInfo2 = roundCalculator.calculateRound(initialHeight + 51);
const delegatesRound3 = walletManager.loadActiveDelegateList(roundInfo2);
for (let i = 0; i < delegatesRound3.length; i++) {
expect(delegatesRound3[i].rate).toBe(i + 1);
expect(delegatesRound3[i].publicKey).toBe(delegatesRound2[delegatesRound3.length - i - 1].publicKey);
}
// @ts-ignore
jest.spyOn(databaseService, "getBlocksForRound").mockReturnValue(blocksInRound);
databaseService.walletManager = walletManager;
// Necessary for revertRound to not blow up.
// @ts-ignore
walletManager.allByUsername = jest.fn(() => {
const usernames = Object.values((walletManager as any).byUsername);
usernames.push(sender);
return usernames;
});
// Finally recalculate the round 2 list and compare against the original list
const restoredDelegatesRound2 = await (databaseService as any).calcPreviousActiveDelegates(roundInfo2);
for (let i = 0; i < restoredDelegatesRound2.length; i++) {
expect(restoredDelegatesRound2[i].rate).toBe(i + 1);
expect(restoredDelegatesRound2[i].publicKey).toBe(delegatesRound2[i].publicKey);
}
transactionHandler.canBeApplied = originalApply;
});
});
});