Skip to content

Commit

Permalink
Merge 189945d into 853f426
Browse files Browse the repository at this point in the history
  • Loading branch information
rg911 committed Dec 1, 2020
2 parents 853f426 + 189945d commit 8e1fd41
Show file tree
Hide file tree
Showing 100 changed files with 1,873 additions and 203 deletions.
4 changes: 1 addition & 3 deletions e2e/infrastructure/IntegrationTestHelper.ts
Expand Up @@ -65,9 +65,7 @@ export class IntegrationTestHelper {
return this.toAccounts(configResult.addresses);
}
private async loadBootstrap(): Promise<{ accounts: string[]; apiUrl: string; addresses: Addresses }> {
// const target = 'target/bootstrap-test';
const target = '../symbol-bootstrap/target/bootstrap';
// const target = '../catapult-rest/rest/target';
const target = process.env.REST_DEV || true ? '../catapult-rest/rest/target' : 'target/bootstrap-test';
console.log('Loading bootstrap server');
const addresses = BootstrapUtils.loadExistingAddresses(target);
return this.toAccounts(addresses);
Expand Down
2 changes: 1 addition & 1 deletion e2e/infrastructure/NamespaceHttp.spec.ts
Expand Up @@ -45,7 +45,7 @@ describe('NamespaceHttp', () => {
return helper.close();
});

const validateMerkle = async (namespaceId: NamespaceId) => {
const validateMerkle = async (namespaceId: NamespaceId): Promise<void> => {
const merkleInfo = await namespaceRepository.getNamespaceMerkle(namespaceId).toPromise();
expect(merkleInfo.raw).to.not.be.undefined;
};
Expand Down
1 change: 0 additions & 1 deletion e2e/infrastructure/PersistentHarvesting.spec.ts
Expand Up @@ -151,7 +151,6 @@ describe('PersistentHarvesting', () => {
networkType,
helper.maxFee,
);
console.log(tx.message.toDTO());

const signedTransaction = tx.signWith(account, generationHash);
return helper.announce(signedTransaction);
Expand Down
2 changes: 1 addition & 1 deletion e2e/infrastructure/TransactionHttp.spec.ts
Expand Up @@ -1577,7 +1577,7 @@ describe('TransactionHttp', () => {
});
});

describe('searchTransactions using steamer', () => {
describe('searchTransactions using streamer', () => {
it('should return transaction info given address', async () => {
const streamer = new TransactionPaginationStreamer(transactionRepository);
const transactionsNoStreamer = await transactionRepository
Expand Down
2 changes: 1 addition & 1 deletion e2e/infrastructure/TransactionSearch.spec.ts
Expand Up @@ -81,7 +81,7 @@ describe('TransactionSearch', () => {
});
});

describe('searchTransactions using steamer', () => {
describe('searchTransactions using streamer', () => {
it('should return transaction info given address', async () => {
const transactionRepository = helper.repositoryFactory.createTransactionRepository();
const streamer = new TransactionPaginationStreamer(transactionRepository);
Expand Down
134 changes: 134 additions & 0 deletions e2e/service/StateProofService.spec.ts
@@ -0,0 +1,134 @@
/*
* Copyright 2020 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from 'chai';
import { Observable } from 'rxjs';
import { take, toArray } from 'rxjs/operators';
import { Order, RepositoryFactoryHttp, SearchCriteria, SearcherRepository } from '../../src/infrastructure';
import { NamespaceRegistrationType } from '../../src/model/namespace';
import { StateMerkleProof } from '../../src/model/state';
import { StateProofService } from '../../src/service';

const url = 'http://api-01.us-west-2.0.10.0.x.symboldev.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(url);
const service = new StateProofService(repositoryFactory);
const stateCounts = 50;
//Process the latest data first.
const order = Order.Desc;

// TODO, create dynamic it tests

async function test<E, C extends SearchCriteria>(
path: string,
repository: SearcherRepository<E, C>,
merkleMethod: (state: E) => Observable<StateMerkleProof>,
getId: (state: E) => string,
criteria: C = { order, pageSize: stateCounts } as C,
): Promise<void> {
const streamer = repository.streamer();
const infos = await streamer.search(criteria).pipe(take(stateCounts), toArray()).toPromise();
const promises = infos.map(async (info) => {
const idText = getId(info);
const stateUrl = `${url}/${path}/${idText}`;
try {
const merkle = await merkleMethod(info).toPromise();
expect(merkle).to.not.undefined;
if (merkle.valid) console.log(stateUrl + ' ' + merkle.valid);
else {
console.error(stateUrl + ' ' + merkle.valid);
}
} catch (e) {
console.error(stateUrl + ' ' + e);
console.error(e);
}
});
await Promise.all(promises);
}

describe('StateProofService', () => {
it('Mosaics', async () => {
await test(
'mosaics',
repositoryFactory.createMosaicRepository(),
(info) => service.mosaic(info),
(info) => info.id.toHex(),
);
});

it('Namespaces', async () => {
await test(
'namespaces',
repositoryFactory.createNamespaceRepository(),
(info) => service.namespaces(info),
(info) => info.id.toHex(),
{ order, pageSize: stateCounts, registrationType: NamespaceRegistrationType.RootNamespace },
);
});

it('Accounts', async () => {
await test(
'accounts',
repositoryFactory.createAccountRepository(),
(info) => service.account(info),
(info) => info.address.plain(),
);
});

it('Hash Lock', async () => {
await test(
'lock/hash',
repositoryFactory.createHashLockRepository(),
(info) => service.hashLock(info),
(info) => info.hash,
);
});

it('Secret Lock', async () => {
await test(
'lock/secret',
repositoryFactory.createSecretLockRepository(),
(info) => service.secretLock(info),
(info) => info.compositeHash,
);
});

it('Account restrictions', async () => {
await test(
'restrictions/account',
repositoryFactory.createRestrictionAccountRepository(),
(info) => service.accountRestriction(info),
(info) => info.address.plain(),
);
});

it('Mosaic restrictions', async () => {
await test(
'restrictions/mosaic',
repositoryFactory.createRestrictionMosaicRepository(),
(info) => service.mosaicRestriction(info),
(info) => info.compositeHash,
);
});

it('Metadata', async () => {
await test(
'metadata',
repositoryFactory.createMetadataRepository(),
(info) => service.metadata(info),
(info) => info.metadataEntry.compositeHash,
);
});
});
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -99,7 +99,7 @@
"dependencies": {
"@js-joda/core": "^3.2.0",
"bluebird": "^3.7.2",
"catbuffer-typescript": "0.0.24-alpha-202011210642",
"catbuffer-typescript": "0.0.24-alpha-202011261917",
"crypto-js": "^4.0.0",
"diff": "^4.0.2",
"futoin-hkdf": "^1.3.2",
Expand All @@ -113,7 +113,7 @@
"ripemd160": "^2.0.2",
"rxjs": "^6.6.3",
"rxjs-compat": "^6.6.3",
"symbol-openapi-typescript-fetch-client": "0.10.1-SNAPSHOT.202011191848",
"symbol-openapi-typescript-fetch-client": "0.10.1-SNAPSHOT.202012011711",
"tweetnacl": "^1.0.3",
"ws": "^7.3.1"
},
Expand Down
34 changes: 32 additions & 2 deletions src/core/utils/DtoMapping.ts
Expand Up @@ -15,7 +15,7 @@
*/

import { Duration } from '@js-joda/core';
import { AccountRestrictionsInfoDTO } from 'symbol-openapi-typescript-fetch-client';
import { AccountRestrictionsInfoDTO, MerkleTreeBranchDTO, MerkleTreeLeafDTO } from 'symbol-openapi-typescript-fetch-client';
import { MerkleStateInfoDTO } from 'symbol-openapi-typescript-fetch-client/src/models/index';
import { Address } from '../../model/account/Address';
import { MerkleStateInfo } from '../../model/blockchain/MerkleStateInfo';
Expand All @@ -25,6 +25,11 @@ import { AccountRestrictions } from '../../model/restriction/AccountRestrictions
import { AddressRestrictionFlag } from '../../model/restriction/AddressRestrictionFlag';
import { MosaicRestrictionFlag } from '../../model/restriction/MosaicRestrictionFlag';
import { OperationRestrictionFlag } from '../../model/restriction/OperationRestrictionFlag';
import { MerkleTree } from '../../model/state/MerkleTree';
import { MerkleTreeBranch } from '../../model/state/MerkleTreeBranch';
import { MerkleTreeBranchLink } from '../../model/state/MerkleTreeBranchLink';
import { MerkleTreeLeaf } from '../../model/state/MerkleTreeLeaf';
import { MerkleTreeNodeType } from '../../model/state/MerkleTreeNodeType';

export class DtoMapping {
/**
Expand All @@ -34,6 +39,7 @@ export class DtoMapping {
*/
public static extractAccountRestrictionFromDto(accountRestrictions: AccountRestrictionsInfoDTO): AccountRestrictions {
return new AccountRestrictions(
accountRestrictions.accountRestrictions.version,
accountRestrictions['id'],
Address.createFromEncoded(accountRestrictions.accountRestrictions.address),
accountRestrictions.accountRestrictions.restrictions.map((prop) => {
Expand Down Expand Up @@ -162,6 +168,30 @@ export class DtoMapping {
* @param dto the dto
*/
public static toMerkleStateInfo(dto: MerkleStateInfoDTO): MerkleStateInfo {
return new MerkleStateInfo(dto.raw);
if (!dto.tree) {
return new MerkleStateInfo(dto.raw, MerkleTree.fromRaw(dto.raw));
}

const leaf = dto.tree.find((tree) => tree.type.valueOf() === MerkleTreeNodeType.Leaf) as MerkleTreeLeafDTO;
const tree = new MerkleTree(
dto.tree
.filter((tree) => tree.type.valueOf() === MerkleTreeNodeType.Branch)
.map((b) => {
const branch = b as MerkleTreeBranchDTO;
return new MerkleTreeBranch(
branch.type.valueOf(),
branch.path,
branch.encodedPath,
branch.nibbleCount,
branch.linkMask,
branch.links.map((link) => new MerkleTreeBranchLink(link.bit, link.link)),
branch.branchHash,
);
}),
leaf
? new MerkleTreeLeaf(leaf.type.valueOf(), leaf.path, leaf.encodedPath, leaf.nibbleCount, leaf.value, leaf.leafHash)
: undefined,
);
return new MerkleStateInfo(dto.raw, tree);
}
}
6 changes: 6 additions & 0 deletions src/infrastructure/AccountHttp.ts
Expand Up @@ -24,6 +24,7 @@ import { Mosaic, MosaicId } from '../model/mosaic';
import { AccountRepository } from './AccountRepository';
import { Http } from './Http';
import { Page } from './Page';
import { AccountPaginationStreamer } from './paginationStreamer';
import { AccountSearchCriteria } from './searchCriteria';

/**
Expand Down Expand Up @@ -88,6 +89,10 @@ export class AccountHttp extends Http implements AccountRepository {
);
}

public streamer(): AccountPaginationStreamer {
return new AccountPaginationStreamer(this);
}

/**
* Returns the merkle information of the given account.
*
Expand All @@ -106,6 +111,7 @@ export class AccountHttp extends Http implements AccountRepository {
*/
public static toAccountInfo(dto: AccountInfoDTO): AccountInfo {
return new AccountInfo(
dto.account.version,
dto.id,
Address.createFromEncoded(dto.account.address),
UInt64.fromNumericString(dto.account.addressHeight),
Expand Down
4 changes: 2 additions & 2 deletions src/infrastructure/AccountRepository.ts
Expand Up @@ -17,15 +17,15 @@
import { Observable } from 'rxjs';
import { AccountInfo, Address } from '../model/account';
import { MerkleStateInfo } from '../model/blockchain';
import { Searcher } from './paginationStreamer';
import { SearcherRepository } from './paginationStreamer';
import { AccountSearchCriteria } from './searchCriteria';

/**
* Account interface repository.
*
* @since 1.0
*/
export interface AccountRepository extends Searcher<AccountInfo, AccountSearchCriteria> {
export interface AccountRepository extends SearcherRepository<AccountInfo, AccountSearchCriteria> {
/**
* Gets an AccountInfo for an account.
* @param address Address
Expand Down
13 changes: 11 additions & 2 deletions src/infrastructure/BlockHttp.ts
Expand Up @@ -29,6 +29,7 @@ import { UInt64 } from '../model/UInt64';
import { BlockRepository } from './BlockRepository';
import { Http } from './Http';
import { Page } from './Page';
import { BlockPaginationStreamer } from './paginationStreamer';
import { BlockSearchCriteria } from './searchCriteria/BlockSearchCriteria';

/**
Expand Down Expand Up @@ -82,6 +83,10 @@ export class BlockHttp extends Http implements BlockRepository {
);
}

public streamer(): BlockPaginationStreamer {
return new BlockPaginationStreamer(this);
}

/**
* This method maps a BlockInfoDTO from rest to the SDK's BlockInfo model object.
*
Expand Down Expand Up @@ -126,8 +131,12 @@ export class BlockHttp extends Http implements BlockRepository {
const importanceBlockInfoDto = dto.block as ImportanceBlockDTO;
return DtoMapping.assign(normalBlock, {
votingEligibleAccountsCount: importanceBlockInfoDto.votingEligibleAccountsCount,
harvestingEligibleAccountsCount: UInt64.fromNumericString(importanceBlockInfoDto.harvestingEligibleAccountsCount),
totalVotingBalance: UInt64.fromNumericString(importanceBlockInfoDto.totalVotingBalance),
harvestingEligibleAccountsCount: importanceBlockInfoDto.harvestingEligibleAccountsCount
? UInt64.fromNumericString(importanceBlockInfoDto.harvestingEligibleAccountsCount)
: undefined,
totalVotingBalance: importanceBlockInfoDto.totalVotingBalance
? UInt64.fromNumericString(importanceBlockInfoDto.totalVotingBalance)
: undefined,
previousImportanceBlockHash: importanceBlockInfoDto.previousImportanceBlockHash,
}) as NemesisImportanceBlockInfo;
} else {
Expand Down
11 changes: 5 additions & 6 deletions src/infrastructure/BlockRepository.ts
Expand Up @@ -15,18 +15,17 @@
*/

import { Observable } from 'rxjs';
import { BlockInfo } from '../model/blockchain/BlockInfo';
import { MerkleProofInfo } from '../model/blockchain/MerkleProofInfo';
import { UInt64 } from '../model/UInt64';
import { Searcher } from './paginationStreamer/Searcher';
import { BlockSearchCriteria } from './searchCriteria/BlockSearchCriteria';
import { UInt64 } from '../model';
import { BlockInfo, MerkleProofInfo } from '../model/blockchain';
import { SearcherRepository } from './paginationStreamer';
import { BlockSearchCriteria } from './searchCriteria';

/**
* Blockchain interface repository.
*
* @since 1.0
*/
export interface BlockRepository extends Searcher<BlockInfo, BlockSearchCriteria> {
export interface BlockRepository extends SearcherRepository<BlockInfo, BlockSearchCriteria> {
/**
* Gets a BlockInfo for a given block height
* @param height - Block height
Expand Down

0 comments on commit 8e1fd41

Please sign in to comment.