Skip to content

Commit

Permalink
fix(compiler): replace system.iterator.create, plus some other compil…
Browse files Browse the repository at this point in the history
…er stuff
  • Loading branch information
spencercorwin committed May 26, 2021
1 parent bf2abc7 commit 0f157bb
Show file tree
Hide file tree
Showing 55 changed files with 728 additions and 217 deletions.
2 changes: 2 additions & 0 deletions packages/neo-one-client-common/src/models/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export enum SysCall {
'System.Storage.Find' = 'System.Storage.Find',
'System.Storage.Put' = 'System.Storage.Put',
'System.Storage.Delete' = 'System.Storage.Delete',
'System.Iterator.Print' = 'System.Iterator.Print',
}

export enum SysCallHashNum {
Expand Down Expand Up @@ -287,6 +288,7 @@ export enum SysCallHashNum {
'System.Storage.Find' = 0xdf30b89a,
'System.Storage.Put' = 0xe63f1884,
'System.Storage.Delete' = 0x2f58c5ed,
'System.Iterator.Print' = 0x4dff5eda,
}

export type SysCallName = keyof typeof SysCall;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class ContractManifest extends ContractManifestModel<ContractABI, Contrac
const { array } = assertStructStackItem(stackItem);
const name = array[0].getString();
const groups = assertArrayStackItem(array[1]).array.map((group) => ContractGroup.fromStackItem(group));
if (assertMapStackItem(array[2]).count !== 2) {
if (assertMapStackItem(array[2]).count !== 0) {
throw new InvalidFormatError('Expected manifest features map to have two properties');
}
const supportedStandards = assertArrayStackItem(array[3]).array.map((std) => std.getString());
Expand Down
6 changes: 5 additions & 1 deletion packages/neo-one-node/src/startFullNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ export const startFullNode = async ({

const storage = levelupStorage({
db,
context: { network: blockchainSettings.network, validatorsCount: blockchainSettings.validatorsCount },
context: {
network: blockchainSettings.network,
validatorsCount: blockchainSettings.validatorsCount,
maxValidUntilBlockIncrement: blockchainSettings.maxValidUntilBlockIncrement,
},
});

disposable = composeDisposable(async () => storage.close(), disposable);
Expand Down
3 changes: 3 additions & 0 deletions packages/neo-one-smart-contract-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"@types/levelup": "^3.1.1",
"@types/lodash": "^4.14.138",
"@types/memdown": "^3.0.0",
"rxjs": "^6.5.3",
"fs-extra": "^8.1.0",
"rocksdb": "^4.1.0",
"app-root-dir": "^1.0.2",
"bignumber.js": "^9.0.0",
"gulp": "~4.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Context } from '../../Context';
import { createContextForPath, createContextForSnippet } from '../../createContext';
import { EXECUTE_OPTIONS_DEFAULT, ExecuteOptions, executeScript } from './executeScript';
import { checkResult } from './extractors';
import { startNode } from './startNode';

const execute = async (
context: Context,
Expand Down Expand Up @@ -44,3 +45,38 @@ export const executeSnippet = async (snippetPath: string, options: ExecuteOption

return execute(context, sourceFile, options);
};

const getContractString = (testIn: string) => `
import { SmartContract } from '@neo-one/smart-contract';
const test = () => {
${testIn}
}
export class TestContract extends SmartContract {
public readonly properties = {
groups: [],
permissions: [],
trusts: "*",
};
public run(): void {
test();
}
}
`;

export const executeStringWithContract = async (code: string) => {
const node = await startNode();
const contract = await node.addContract(getContractString(code));

return node.executeString(`
import { Address, SmartContract } from '@neo-one/smart-contract';
interface Contract {
run(): void;
}
const contract = SmartContract.for<Contract>(Address.from('${contract.address}'));
contract.run();
`);
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import {
import { Blockchain } from '@neo-one/node-blockchain';
import { NativeContainer } from '@neo-one/node-native';
import { test as testNet } from '@neo-one/node-neo-settings';
import { storage } from '@neo-one/node-storage-levelup';
import { storage, streamToObservable } from '@neo-one/node-storage-levelup';
import { blockchainSettingsToProtocolSettings, Dispatcher } from '@neo-one/node-vm';
// import fs from 'fs-extra';
import LevelUp from 'levelup';
import MemDown from 'memdown';
import { toArray } from 'rxjs/operators';
// import RocksDB from 'rocksdb';
import { RawSourceMap } from 'source-map';
import ts from 'typescript';
Expand All @@ -29,6 +30,17 @@ export const EXECUTE_OPTIONS_DEFAULT = {
ignoreWarnings: false,
};

// tslint:disable-next-line: no-any
const getUpdateVMMemoryStore = (vm: Dispatcher, db: any) => async () => {
const updates = await streamToObservable<{ readonly key: Buffer; readonly value: Buffer }>(() =>
db.createReadStream(),
)
.pipe(toArray())
.toPromise();

vm.updateStore(updates);
};

export const executeScript = async (
diagnostics: ReadonlyArray<ts.Diagnostic>,
compiledCode: string,
Expand All @@ -42,18 +54,24 @@ export const executeScript = async (
// await fs.ensureDir(path);
// const db = LevelUp(RocksDB(path));
const settings = testNet();
const dispatcher = new Dispatcher({
const vm = new Dispatcher({
// levelDBPath: path,
protocolSettings: blockchainSettingsToProtocolSettings(settings),
});
const db = LevelUp(MemDown());
const blockchain = await Blockchain.create({
settings,
storage: storage({
context: { network: settings.network, validatorsCount: settings.validatorsCount },
db: LevelUp(MemDown()),
context: {
network: settings.network,
validatorsCount: settings.validatorsCount,
maxValidUntilBlockIncrement: settings.maxValidUntilBlockIncrement,
},
db,
}),
vm: dispatcher,
vm,
native: new NativeContainer(settings),
onPersist: getUpdateVMMemoryStore(vm, db),
});

throwOnDiagnosticErrorOrWarning(diagnostics, ignoreWarnings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DiagnosticCode } from '../../../../DiagnosticCode';

describe('Array.prototype.entries', () => {
test('should return an iterator over the array', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x = [1, 2, 3];
x.entries();
let y = x.entries();
Expand Down Expand Up @@ -32,7 +32,7 @@ describe('Array.prototype.entries', () => {
});

test('should return an iterator over an empty array', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x: Array<number> = [];
const y = x.entries();
Expand All @@ -43,7 +43,7 @@ describe('Array.prototype.entries', () => {
});

test('should return an iterator over an array with one undefined element', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x = [undefined];
const y = x.entries();
Expand All @@ -58,7 +58,7 @@ describe('Array.prototype.entries', () => {
});

test('should return an iterator over the array as possible object', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
interface Arr<T> {
entries(): IterableIterator<[T, number]>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DiagnosticCode } from '../../../../DiagnosticCode';

describe('Array.prototype[Symbol.iterator]', () => {
test('should return an iterator over the array', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x = [1, 2, 3];
x[Symbol.iterator]();
let y = x[Symbol.iterator]();
Expand All @@ -28,7 +28,7 @@ describe('Array.prototype[Symbol.iterator]', () => {
});

test('should return an iterator over an empty array', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x: Array<number> = [];
const y = x[Symbol.iterator]();
Expand All @@ -39,7 +39,7 @@ describe('Array.prototype[Symbol.iterator]', () => {
});

test('should return an iterator over an array with one undefined element', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x = [undefined];
const y = x[Symbol.iterator]();
Expand All @@ -53,7 +53,7 @@ describe('Array.prototype[Symbol.iterator]', () => {
});

test('should return an iterator over the array as possible object', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
interface Arr<T> {
[Symbol.iterator](): IterableIterator<T>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import { DiagnosticCode } from '../../../../DiagnosticCode';

describe('Map.prototype.forEach', () => {
test('should apply a function over a map', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x = new Map<string, number>();
x.set('a', 1);
x.set('b', 2);
x.set('c', 3);
let result = 0;
let valueResult = 0;
const y = x.forEach((value) => {
valueResult += value;
Expand All @@ -22,7 +21,7 @@ describe('Map.prototype.forEach', () => {
});

test('should apply a function over a map with key', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x = new Map<string, number>();
x.set('a', 1);
x.set('b', 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DiagnosticCode } from '../../../../DiagnosticCode';

describe('Map.prototype[Symbol.iterator]', () => {
test('should return an iterator over the map', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x = new Map<string, number>();
x.set('a', 1);
x.set('b', 2);
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('Map.prototype[Symbol.iterator]', () => {
});

test('should return an iterator over an empty map', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x = new Map<string, number>();
const y = x[Symbol.iterator]();
Expand All @@ -45,7 +45,7 @@ describe('Map.prototype[Symbol.iterator]', () => {
});

test('should return an iterator over the map as possible object', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
interface Mp<K, V> {
readonly set: (key: K, value: V) => this;
[Symbol.iterator](): IterableIterator<[K, V]>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DiagnosticCode } from '../../../../DiagnosticCode';

describe('Set.prototype.forEach', () => {
test('should apply a function over a set', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x = new Set<number>();
x.add(1);
x.add(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DiagnosticCode } from '../../../../DiagnosticCode';

describe('Set.prototype[Symbol.iterator]', () => {
test('should return an iterator over the set', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x = new Set<number>();
x.add(1);
x.add(2);
Expand Down Expand Up @@ -31,7 +31,7 @@ describe('Set.prototype[Symbol.iterator]', () => {
});

test('should return an iterator over an empty set', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x: Set<number> = new Set();
const y = x[Symbol.iterator]();
Expand All @@ -42,7 +42,7 @@ describe('Set.prototype[Symbol.iterator]', () => {
});

test('should return an iterator over an set with one undefined element', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
const x = new Set<number | undefined>();
x.add(undefined);
const y = x[Symbol.iterator]();
Expand All @@ -57,7 +57,7 @@ describe('Set.prototype[Symbol.iterator]', () => {
});

test('should return an iterator over the set as possible object', async () => {
await helpers.executeString(`
await helpers.executeStringWithContract(`
interface St<T> {
[Symbol.iterator](): IterableIterator<T>;
readonly add: (value: T) => this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ArrayEntries extends BuiltinInstanceMemberCall {
// [map]
sb.emitHelper(node, options, sb.helpers.arrToMap);
// [iterator]
sb.emitSysCall(node, 'System.Iterator.Create');
sb.emitHelper(node, options, sb.helpers.createMapIterator);
// [val]
sb.emitHelper(
node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ArrayIterator extends BuiltinInstanceMemberCall {
// [map]
sb.emitHelper(node, options, sb.helpers.arrToMap);
// [iterator]
sb.emitSysCall(node, 'System.Iterator.Create');
sb.emitHelper(node, options, sb.helpers.createMapIterator);
// [val]
sb.emitHelper(node, options, sb.helpers.createEnumeratorIterableIterator({}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class MapForEach extends BuiltinInstanceMemberCall {
// [map]
sb.emitHelper(node, options, sb.helpers.unwrapMap);
// [iterator]
sb.emitSysCall(node, 'System.Iterator.Create');
sb.emitHelper(node, options, sb.helpers.createMapIterator);
// [objectVal, iterator]
sb.visit(tsUtils.argumented.getArguments(node)[0], options);
// []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class MapIterator extends BuiltinInstanceMemberCall {
// [map]
sb.emitHelper(node, options, sb.helpers.unwrapMap);
// [iterator]
sb.emitSysCall(node, 'System.Iterator.Create');
sb.emitHelper(node, options, sb.helpers.createMapIterator);
// [val]
sb.emitHelper(node, options, sb.helpers.createIteratorIterableIterator({ deserializeKey: true }));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ export class SetForEach extends BuiltinInstanceMemberCall {
sb.emitHelper(node, options, sb.helpers.unwrapMap);
// [keysArr]
sb.emitOp(node, 'KEYS');
// [keysMap]
sb.emitHelper(node, options, sb.helpers.arrToMap);
// [iterator]
sb.emitSysCall(node, 'System.Iterator.Create');
sb.emitHelper(node, options, sb.helpers.createMapIterator);
// [objectVal, iterator]
sb.visit(tsUtils.argumented.getArguments(node)[0], options);
// []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ export class SetIterator extends BuiltinInstanceMemberCall {
sb.emitHelper(node, options, sb.helpers.unwrapMap);
// [keysArr]
sb.emitOp(node, 'KEYS');
// [keysMap]
sb.emitHelper(node, options, sb.helpers.arrToMap);
// [iterator]
sb.emitSysCall(node, 'System.Iterator.Create');
sb.emitHelper(node, options, sb.helpers.createMapIterator);
// [val]
sb.emitHelper(node, options, sb.helpers.createEnumeratorIterableIterator({ deserializeKey: true }));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export enum GlobalProperty {
Cache = 6,
CacheStorage = 7,
DeleteCacheStorage = 8,
DeleteIteratorCacheStorage = 9,
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class ArrConcatHelper extends Helper {
// [map, result]
sb.emitHelper(node, options, sb.helpers.arrToMap);
// [enumerator, result]
sb.emitSysCall(node, 'System.Iterator.Create');
sb.emitHelper(node, options, sb.helpers.createMapIterator);
// [result]
sb.emitHelper(
node,
Expand Down
Loading

0 comments on commit 0f157bb

Please sign in to comment.