Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes will be documented in this file.
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## 12.0.0
- [Breaking change: Implementation of topological sort to sort custom types by their type dependencies (change attribute visibility)](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/224)

## 11.0.0
- [Breaking change: Sender is now mandatory when constructing a transaction](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/TBD)
Expand Down
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.

40 changes: 30 additions & 10 deletions src/smartcontracts/typesystem/abiRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { EndpointDefinition, EndpointParameterDefinition } from "./endpoint";

export class AbiRegistry {
readonly interfaces: ContractInterface[] = [];
readonly customTypes: CustomType[] = [];
private customTypes: CustomType[] = [];

static create(json: { name: string; endpoints: any[]; types: any[] }): AbiRegistry {
let registry = new AbiRegistry().extend(json);
Expand Down Expand Up @@ -49,17 +49,37 @@ export class AbiRegistry {
}

private sortCustomTypesByDependencies() {
// TODO: Improve consistency of the sorting function (and make sure the sorting is stable): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
this.customTypes.sort((a: CustomType, b: CustomType) => {
const bDependsOnA = b.getNamesOfDependencies().indexOf(a.getName()) > -1;
if (bDependsOnA) {
// Sort "a" before "b".
return -1;
// Use of topological sort algorithm to sort custom types by dependencies.
let dependencies: { [key: string]: string[] } = {};
let visited: { [key: string]: boolean } = {};
this.customTypes.forEach((type: CustomType) => {
dependencies[type.getName()] = type.getNamesOfDependencies();
visited[type.getName()] = false;
});
let sortedArray = new Array<CustomType>();

const topologicalSortUtil = (name: string, visited: { [key: string]: boolean }, sortedArray: CustomType[]) => {
visited[name] = true;

for (const dependency of dependencies[name]) {
if (!this.customTypes.find((e) => e.getName() == dependency)) continue;
if (!visited[dependency]) {
topologicalSortUtil(dependency, visited, sortedArray);
}
}
const type = this.customTypes.find((e) => e.getName() == name);
if (type) {
sortedArray.push(type);
}
};

// Sort "b" before "a".
return 1;
});
for (const type of this.customTypes) {
if (!visited[type.getName()]) {
topologicalSortUtil(type.getName(), visited, sortedArray);
}
}

this.customTypes = sortedArray;
}

getInterface(name: string): ContractInterface {
Expand Down
24 changes: 12 additions & 12 deletions src/testdata/custom-types-out-of-order.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
}
]
},
"TypeC": {
"type": "struct",
"fields": [
{
"name": "foobar",
"type": "u64"
}
]
},
"EsdtTokenType": {
"type": "enum",
"variants": [
Expand All @@ -35,15 +44,6 @@
}
]
},
"TypeA": {
"type": "struct",
"fields": [
{
"name": "b",
"type": "TypeB"
}
]
},
"TypeB": {
"type": "struct",
"fields": [
Expand All @@ -53,12 +53,12 @@
}
]
},
"TypeC": {
"TypeA": {
"type": "struct",
"fields": [
{
"name": "foobar",
"type": "u64"
"name": "b",
"type": "TypeB"
}
]
}
Expand Down