Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
fboucquez committed Nov 27, 2020
1 parent 52e81a5 commit 76a2f98
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 47 deletions.
3 changes: 2 additions & 1 deletion e2e/service/StateProofService.spec.ts
Expand Up @@ -22,7 +22,8 @@ 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 url = 'http://api-01.us-west-2.0.10.0.x.symboldev.network:3000';
const url = 'http://localhost:3000';
const repositoryFactory = new RepositoryFactoryHttp(url);
const service = new StateProofService(repositoryFactory);
const stateCounts = 50;
Expand Down
76 changes: 37 additions & 39 deletions src/model/namespace/NamespaceInfo.ts
Expand Up @@ -23,12 +23,10 @@ import {
NamespacePathBuilder,
RootNamespaceHistoryBuilder,
} from 'catbuffer-typescript';
import { Address } from '../account/Address';
import { Address } from '../account';
import { UInt64 } from '../UInt64';
import { Alias } from './Alias';
import { NamespaceId } from './NamespaceId';
import { NamespaceRegistrationType } from './NamespaceRegistrationType';
import Long = require('long');

/**
* Object containing information of a namespace.
Expand Down Expand Up @@ -142,40 +140,53 @@ export class NamespaceInfo {
* @return {Uint8Array}
*/
public serialize(fullPath: NamespaceInfo[]): Uint8Array {
const root = fullPath.find((n) => n.registrationType === NamespaceRegistrationType.RootNamespace);
if (!root) {
throw new Error('Cannot find root namespace info.');
if (!this.isRoot()) {
throw new Error('A namespace must be a root in order to serialize');
}
const id: NamespaceIdDto = root.id.toBuilder();
const ownerAddress: AddressDto = root.ownerAddress.toBuilder();
const id: NamespaceIdDto = this.id.toBuilder();
const ownerAddress: AddressDto = this.ownerAddress.toBuilder();
const lifetime: NamespaceLifetimeBuilder = new NamespaceLifetimeBuilder(
new HeightDto(root.startHeight.toDTO()),
new HeightDto(root.endHeight.toDTO()),
new HeightDto(this.startHeight.toDTO()),
new HeightDto(this.endHeight.toDTO()),
);
const rootAlias = this.getAliasBuilder(root);
const paths: NamespacePathBuilder[] = this.getNamespacePath(fullPath, root.id);
const rootAlias = this.getAliasBuilder(this);
const sortedChildren = this.sortPath(fullPath, this.id);
if (fullPath.length != sortedChildren.length) {
throw new Error('Some of the children do not belong to this root namespace');
}
const paths: NamespacePathBuilder[] = sortedChildren.map((p) => this.toNamespacePathBuilder(p));
return new RootNamespaceHistoryBuilder(1, id, ownerAddress, lifetime, rootAlias, paths).serialize();
}

/**
* Generate the namespace full path builder
* Sorts the namespaces to tree form.
* @param namespaces Full path of namespaces
* @param rootId Root namespace id
* @returns {NamespacePathBuilder[]}
* @returns {NamespaceInfo[]}
*/
private getNamespacePath(namespaces: NamespaceInfo[], rootId: NamespaceId): NamespacePathBuilder[] {
const path: NamespacePathBuilder[] = [];
const level1 = this.sortNamespaceInfo(namespaces.filter((n) => n.depth === 2 && n.parentId.equals(rootId)));
level1.forEach((n) => {
const level2 = this.sortNamespaceInfo(namespaces.filter((l) => l.depth === 3 && l.parentId.equals(n.id)));
path.push(new NamespacePathBuilder([n.id.toBuilder()], this.getAliasBuilder(n)));
if (level2.length) {
level2.forEach((l) => {
path.push(new NamespacePathBuilder([n.id.toBuilder(), l.id.toBuilder()], this.getAliasBuilder(l)));
private sortPath(namespaces: NamespaceInfo[], rootId: NamespaceId): NamespaceInfo[] {
const addTree = (nodes: NamespaceInfo[], parentId: NamespaceId, treeList: NamespaceInfo[]): NamespaceInfo[] => {
nodes
.filter((r) => r.isSubnamespace() && r.parentNamespaceId().equals(parentId))
.sort((a, b) => a.id.id.compare(b.id.id))
.forEach((n) => {
treeList.push(n);
addTree(nodes, n.id, treeList);
});
}
});
return path;
return treeList;
};
return addTree(namespaces, rootId, []);
}

/**
* it converts node info into a path builder
* @param n the node
*/
private toNamespacePathBuilder(n: NamespaceInfo): NamespacePathBuilder {
return new NamespacePathBuilder(
n.levels.slice(1).map((l) => l.toBuilder()),
this.getAliasBuilder(n),
);
}

/**
Expand All @@ -190,17 +201,4 @@ export class NamespaceInfo {
namespaceInfo.alias.address?.toBuilder(),
);
}

/**
* Sort namespace info by namespace id
* @param info array of namespace info
* @returns {NamespaceInfo[]}
*/
private sortNamespaceInfo(info: NamespaceInfo[]): NamespaceInfo[] {
return info.sort((a, b) => {
const long_a = Long.fromBits(a.id.id.lower, a.id.id.higher, true);
const long_b = Long.fromBits(b.id.id.lower, b.id.id.higher, true);
return long_a.compare(long_b);
});
}
}
7 changes: 1 addition & 6 deletions src/model/transaction/TransferTransaction.ts
Expand Up @@ -26,7 +26,6 @@ import {
UnresolvedMosaicBuilder,
UnresolvedMosaicIdDto,
} from 'catbuffer-typescript';
import * as Long from 'long';
import { Convert } from '../../core/format';
import { DtoMapping, UnresolvedMapping } from '../../core/utils';
import { Address, PublicAccount, UnresolvedAddress } from '../account';
Expand Down Expand Up @@ -186,11 +185,7 @@ export class TransferTransaction extends Transaction {
* @returns {Mosaic[]}
*/
public sortMosaics(): Mosaic[] {
return this.mosaics.sort((a, b) => {
const long_a = Long.fromBits(a.id.id.lower, a.id.id.higher, true);
const long_b = Long.fromBits(b.id.id.lower, b.id.id.higher, true);
return long_a.compare(long_b);
});
return this.mosaics.sort((a, b) => a.id.id.compare(b.id.id));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/service/StateProofService.ts
Expand Up @@ -89,7 +89,7 @@ export class StateProofService {
const namespaceRepo = this.repositoryFactory.createNamespaceRepository();
return namespaceRepo
.streamer()
.search({ level0: root.id })
.search({ level0: root.id, registrationType: NamespaceRegistrationType.SubNamespace })
.pipe(toArray())
.pipe(
mergeMap((fullPath) => {
Expand Down

0 comments on commit 76a2f98

Please sign in to comment.