Skip to content

Commit 6053f22

Browse files
authored
Added merkle hash calculation for block transactions (#687)
* Fixed #620 - Added merkle hash calculation for block transactions * Added e2e test - fixed some bugs * Improved streamer toArray
1 parent caaede0 commit 6053f22

File tree

10 files changed

+160
-54
lines changed

10 files changed

+160
-54
lines changed

e2e/infrastructure/PersistentHarvesting.spec.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { AccountKeyLinkTransaction } from '../../src/model/transaction/AccountKe
2323
import { PersistentDelegationRequestTransaction } from '../../src/model/transaction/PersistentDelegationRequestTransaction';
2424
import { VrfKeyLinkTransaction } from '../../src/model/transaction/VrfKeyLinkTransaction';
2525
import { NodeKeyLinkTransaction } from '../../src/model/transaction/NodeKeyLinkTransaction';
26-
import { Duration } from 'js-joda';
2726

2827
describe('PersistentHarvesting', () => {
2928
const helper = new IntegrationTestHelper();
@@ -32,8 +31,6 @@ describe('PersistentHarvesting', () => {
3231
let networkType: NetworkType;
3332
let remoteAccount: Account;
3433

35-
const epochAdjustment = Duration.ofSeconds(1573430400);
36-
3734
const vrfKeyPair = Account.createFromPrivateKey(
3835
'82798EA9A2D2D202AFCCC82C40A287780BCA3C7F7A2FD5B754832804C6BE1BAA',
3936
NetworkType.MIJIN_TEST,
@@ -62,7 +59,7 @@ describe('PersistentHarvesting', () => {
6259
describe('AccountKeyLinkTransaction', () => {
6360
it('standalone', () => {
6461
const accountLinkTransaction = AccountKeyLinkTransaction.create(
65-
Deadline.create(epochAdjustment),
62+
Deadline.create(helper.epochAdjustment),
6663
remoteAccount.publicKey,
6764
LinkAction.Link,
6865
networkType,
@@ -77,7 +74,7 @@ describe('PersistentHarvesting', () => {
7774
describe('VrfKeyLinkTransaction', () => {
7875
it('standalone', () => {
7976
const vrfKeyLinkTransaction = VrfKeyLinkTransaction.create(
80-
Deadline.create(epochAdjustment),
77+
Deadline.create(helper.epochAdjustment),
8178
vrfKeyPair.publicKey,
8279
LinkAction.Link,
8380
networkType,
@@ -92,7 +89,7 @@ describe('PersistentHarvesting', () => {
9289
describe('NodeKeyLinkTransaction', () => {
9390
it('standalone', () => {
9491
const nodeKeyLinkTransaction = NodeKeyLinkTransaction.create(
95-
Deadline.create(epochAdjustment),
92+
Deadline.create(helper.epochAdjustment),
9693
'cfd84eca83508bbee954668e4aecca736caefa615367da76afe6985d695381db',
9794
LinkAction.Link,
9895
networkType,
@@ -112,7 +109,7 @@ describe('PersistentHarvesting', () => {
112109
describe('transactions', () => {
113110
it('should create delegated harvesting transaction', () => {
114111
const tx = PersistentDelegationRequestTransaction.createPersistentDelegationRequestTransaction(
115-
Deadline.create(epochAdjustment),
112+
Deadline.create(helper.epochAdjustment),
116113
remoteAccount.privateKey,
117114
vrfKeyPair.privateKey,
118115
'cfd84eca83508bbee954668e4aecca736caefa615367da76afe6985d695381db',

e2e/infrastructure/SecretLockHttp.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { SecretLockTransaction } from '../../src/model/transaction/SecretLockTra
2828
import { LockHashAlgorithm } from '../../src/model/lock/LockHashAlgorithm';
2929
import { sha3_256 } from 'js-sha3';
3030
import { Crypto } from '../../src/core/crypto';
31-
import { Duration } from 'js-joda';
3231

3332
describe('SecretLockHttp', () => {
3433
const helper = new IntegrationTestHelper();
@@ -38,7 +37,6 @@ describe('SecretLockHttp', () => {
3837
let generationHash: string;
3938
let networkType: NetworkType;
4039
let secret: string;
41-
const epochAdjustment = Duration.ofSeconds(1573430400);
4240

4341
before(() => {
4442
return helper.start({ openListener: true }).then(() => {
@@ -64,7 +62,7 @@ describe('SecretLockHttp', () => {
6462
describe('Create a hash lock', () => {
6563
it('Announce SecretLockTransaction', () => {
6664
const secretLockTransaction = SecretLockTransaction.create(
67-
Deadline.create(epochAdjustment),
65+
Deadline.create(helper.epochAdjustment),
6866
helper.createNetworkCurrency(10, false),
6967
UInt64.fromUint(100),
7068
LockHashAlgorithm.Op_Sha3_256,

e2e/service/AccountService.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { IntegrationTestHelper } from '../infrastructure/IntegrationTestHelper';
2323
import { AccountService } from '../../src/service/AccountService';
2424
import { NamespaceRegistrationTransaction } from '../../src/model/transaction/NamespaceRegistrationTransaction';
2525
import { NamespaceId } from '../../src/model/namespace/NamespaceId';
26-
import { Duration } from 'js-joda';
2726

2827
describe('AccountService', () => {
2928
const helper = new IntegrationTestHelper();
@@ -33,7 +32,6 @@ describe('AccountService', () => {
3332
let accountService: AccountService;
3433
let namespaceId: NamespaceId;
3534
const name = 'root-test-namespace-' + Math.floor(Math.random() * 10000);
36-
const epochAdjustment = Duration.ofSeconds(1573430400);
3735

3836
before(() => {
3937
return helper.start({ openListener: true }).then(() => {
@@ -56,7 +54,7 @@ describe('AccountService', () => {
5654
describe('Create a namespace', () => {
5755
it('Announce NamespaceRegistrationTransaction', () => {
5856
const registerNamespaceTransaction = NamespaceRegistrationTransaction.createRootNamespace(
59-
Deadline.create(epochAdjustment),
57+
Deadline.create(helper.epochAdjustment),
6058
name,
6159
UInt64.fromUint(300000),
6260
networkType,

e2e/service/BlockService.spec.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { BlockService } from '../../src/service/BlockService';
2828
import { IntegrationTestHelper } from '../infrastructure/IntegrationTestHelper';
2929
import { TransactionGroup } from '../../src/infrastructure/TransactionGroup';
3030
import { TransactionStatement } from '../../src/model/receipt/TransactionStatement';
31-
import { Duration } from 'js-joda';
31+
import { BlockRepository } from '../../src/infrastructure/BlockRepository';
3232

3333
describe('BlockService', () => {
3434
const helper = new IntegrationTestHelper();
@@ -40,7 +40,7 @@ describe('BlockService', () => {
4040
let blockService: BlockService;
4141
let transactionRepository: TransactionRepository;
4242
let receiptRepository: ReceiptRepository;
43-
const epochAdjustment = Duration.ofSeconds(1573430400);
43+
let blockRepository: BlockRepository;
4444

4545
before(() => {
4646
return helper.start({ openListener: true }).then(() => {
@@ -50,6 +50,7 @@ describe('BlockService', () => {
5050
networkType = helper.networkType;
5151
transactionRepository = helper.repositoryFactory.createTransactionRepository();
5252
receiptRepository = helper.repositoryFactory.createReceiptRepository();
53+
blockRepository = helper.repositoryFactory.createBlockRepository();
5354
blockService = new BlockService(helper.repositoryFactory);
5455
});
5556
});
@@ -66,7 +67,7 @@ describe('BlockService', () => {
6667
describe('Create a transfer', () => {
6768
it('Announce TransferTransaction', () => {
6869
const transferTransaction = TransferTransaction.create(
69-
Deadline.create(epochAdjustment),
70+
Deadline.create(helper.epochAdjustment),
7071
account2.address,
7172
[NetworkCurrencyLocal.createAbsolute(1)],
7273
PlainMessage.create('test-message'),
@@ -107,4 +108,13 @@ describe('BlockService', () => {
107108
expect(validationResult).to.be.true;
108109
});
109110
});
111+
112+
describe('Calculate merkler transaction root hash', () => {
113+
it('Calculate merkler transaction root hash', async () => {
114+
const calculated = await blockService.calculateTransactionsMerkleRootHash(UInt64.fromUint(1)).toPromise();
115+
const block = await blockRepository.getBlockByHeight(UInt64.fromUint(1)).toPromise();
116+
const rootHash = block.blockTransactionsHash;
117+
expect(rootHash).to.be.equal(calculated);
118+
});
119+
});
110120
});

e2e/service/TransactionService.spec.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import { assert, expect } from 'chai';
18-
import { Duration } from 'js-joda';
1918
import { Convert } from '../../src/core/format/Convert';
2019
import { TransactionRepository } from '../../src/infrastructure/TransactionRepository';
2120
import { Account } from '../../src/model/account/Account';
@@ -60,7 +59,6 @@ describe('TransactionService', () => {
6059
let networkType: NetworkType;
6160
let transactionService: TransactionService;
6261
let transactionRepository: TransactionRepository;
63-
const epochAdjustment = Duration.ofSeconds(1573430400);
6462

6563
before(() => {
6664
return helper.start({ openListener: true }).then(() => {
@@ -86,7 +84,7 @@ describe('TransactionService', () => {
8684

8785
function buildAggregateTransaction(): AggregateTransaction {
8886
const transferTransaction = TransferTransaction.create(
89-
Deadline.create(epochAdjustment),
87+
Deadline.create(helper.epochAdjustment),
9088
addressAlias,
9189
[NetworkCurrencyLocal.createAbsolute(1), new Mosaic(mosaicAlias, UInt64.fromUint(1))],
9290
PlainMessage.create('test-message'),
@@ -95,7 +93,7 @@ describe('TransactionService', () => {
9593
);
9694
// Unlink MosaicAlias
9795
const mosaicAliasTransactionUnlink = MosaicAliasTransaction.create(
98-
Deadline.create(epochAdjustment),
96+
Deadline.create(helper.epochAdjustment),
9997
AliasAction.Unlink,
10098
mosaicAlias,
10199
mosaicId,
@@ -107,7 +105,7 @@ describe('TransactionService', () => {
107105
const nonce = MosaicNonce.createRandom();
108106
newMosaicId = MosaicId.createFromNonce(nonce, account.address);
109107
const mosaicDefinitionTransaction = MosaicDefinitionTransaction.create(
110-
Deadline.create(epochAdjustment),
108+
Deadline.create(helper.epochAdjustment),
111109
nonce,
112110
newMosaicId,
113111
MosaicFlags.create(true, true, false),
@@ -118,7 +116,7 @@ describe('TransactionService', () => {
118116
);
119117

120118
const mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.create(
121-
Deadline.create(epochAdjustment),
119+
Deadline.create(helper.epochAdjustment),
122120
newMosaicId,
123121
MosaicSupplyChangeAction.Increase,
124122
UInt64.fromUint(200000),
@@ -128,7 +126,7 @@ describe('TransactionService', () => {
128126

129127
// Link namespace with new MosaicId
130128
const mosaicAliasTransactionRelink = MosaicAliasTransaction.create(
131-
Deadline.create(epochAdjustment),
129+
Deadline.create(helper.epochAdjustment),
132130
AliasAction.Link,
133131
mosaicAlias,
134132
newMosaicId,
@@ -138,7 +136,7 @@ describe('TransactionService', () => {
138136

139137
// Use new mosaicAlias in metadata
140138
const mosaicMetadataTransaction = MosaicMetadataTransaction.create(
141-
Deadline.create(epochAdjustment),
139+
Deadline.create(helper.epochAdjustment),
142140
account.address,
143141
UInt64.fromUint(5),
144142
mosaicAlias,
@@ -148,7 +146,7 @@ describe('TransactionService', () => {
148146
helper.maxFee,
149147
);
150148
return AggregateTransaction.createComplete(
151-
Deadline.create(epochAdjustment),
149+
Deadline.create(helper.epochAdjustment),
152150
[
153151
transferTransaction.toAggregate(account.publicAccount),
154152
mosaicAliasTransactionUnlink.toAggregate(account.publicAccount),
@@ -172,7 +170,7 @@ describe('TransactionService', () => {
172170
it('Announce NamespaceRegistrationTransaction', () => {
173171
const namespaceName = 'root-test-namespace-' + Math.floor(Math.random() * 10000);
174172
const registerNamespaceTransaction = NamespaceRegistrationTransaction.createRootNamespace(
175-
Deadline.create(epochAdjustment),
173+
Deadline.create(helper.epochAdjustment),
176174
namespaceName,
177175
UInt64.fromUint(20),
178176
networkType,
@@ -189,7 +187,7 @@ describe('TransactionService', () => {
189187
it('Announce NamespaceRegistrationTransaction', () => {
190188
const namespaceName = 'root-test-namespace-' + Math.floor(Math.random() * 10000);
191189
const registerNamespaceTransaction = NamespaceRegistrationTransaction.createRootNamespace(
192-
Deadline.create(epochAdjustment),
190+
Deadline.create(helper.epochAdjustment),
193191
namespaceName,
194192
UInt64.fromUint(50),
195193
networkType,
@@ -205,7 +203,7 @@ describe('TransactionService', () => {
205203
describe('Setup test AddressAlias', () => {
206204
it('Announce addressAliasTransaction', () => {
207205
const addressAliasTransaction = AddressAliasTransaction.create(
208-
Deadline.create(epochAdjustment),
206+
Deadline.create(helper.epochAdjustment),
209207
AliasAction.Link,
210208
addressAlias,
211209
account.address,
@@ -224,7 +222,7 @@ describe('TransactionService', () => {
224222
const nonce = MosaicNonce.createRandom();
225223
mosaicId = MosaicId.createFromNonce(nonce, account.address);
226224
const mosaicDefinitionTransaction = MosaicDefinitionTransaction.create(
227-
Deadline.create(epochAdjustment),
225+
Deadline.create(helper.epochAdjustment),
228226
nonce,
229227
mosaicId,
230228
MosaicFlags.create(true, true, false),
@@ -243,7 +241,7 @@ describe('TransactionService', () => {
243241
describe('MosaicSupplyChangeTransaction', () => {
244242
it('standalone', () => {
245243
const mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.create(
246-
Deadline.create(epochAdjustment),
244+
Deadline.create(helper.epochAdjustment),
247245
mosaicId,
248246
MosaicSupplyChangeAction.Increase,
249247
UInt64.fromUint(200000),
@@ -259,7 +257,7 @@ describe('TransactionService', () => {
259257
describe('Setup MosaicAlias', () => {
260258
it('Announce MosaicAliasTransaction', () => {
261259
const mosaicAliasTransaction = MosaicAliasTransaction.create(
262-
Deadline.create(epochAdjustment),
260+
Deadline.create(helper.epochAdjustment),
263261
AliasAction.Link,
264262
mosaicAlias,
265263
mosaicId,
@@ -275,7 +273,7 @@ describe('TransactionService', () => {
275273
describe('Create Transfer with alias', () => {
276274
it('Announce TransferTransaction', () => {
277275
const transferTransaction = TransferTransaction.create(
278-
Deadline.create(epochAdjustment),
276+
Deadline.create(helper.epochAdjustment),
279277
addressAlias,
280278
[NetworkCurrencyLocal.createAbsolute(1), new Mosaic(mosaicAlias, UInt64.fromUint(1))],
281279
PlainMessage.create('test-message'),
@@ -313,7 +311,7 @@ describe('TransactionService', () => {
313311
describe('Transfer mosaic to account 3', () => {
314312
it('Announce TransferTransaction', () => {
315313
const transferTransaction = TransferTransaction.create(
316-
Deadline.create(epochAdjustment),
314+
Deadline.create(helper.epochAdjustment),
317315
account3.address,
318316
[new Mosaic(mosaicAlias, UInt64.fromUint(1))],
319317
PlainMessage.create('test-message'),
@@ -331,7 +329,7 @@ describe('TransactionService', () => {
331329
const transactions: SignedTransaction[] = [];
332330
// 1. Transfer A -> B
333331
const transaction1 = TransferTransaction.create(
334-
Deadline.create(epochAdjustment),
332+
Deadline.create(helper.epochAdjustment),
335333
account2.address,
336334
[new Mosaic(mosaicAlias, UInt64.fromUint(1))],
337335
PlainMessage.create('test-message'),
@@ -342,7 +340,7 @@ describe('TransactionService', () => {
342340

343341
// 2. Transfer C -> D
344342
const transaction2 = TransferTransaction.create(
345-
Deadline.create(epochAdjustment),
343+
Deadline.create(helper.epochAdjustment),
346344
cosignAccount4.address,
347345
[new Mosaic(mosaicAlias, UInt64.fromUint(1))],
348346
PlainMessage.create('test-message'),

0 commit comments

Comments
 (0)