Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): remove createMapIterator in some places #2413

Merged
merged 2 commits into from
Jun 1, 2021
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
76 changes: 39 additions & 37 deletions packages/neo-one-client-core/src/sc/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ export const getForwardValues = ({
);
};

const createForwardValueArgs = (
parameters: readonly ABIParameter[],
events: readonly ContractEventDescriptorClient[],
) => (
// tslint:disable-next-line no-any readonly-array
...args: any[]
) => getForwardValues({ parameters, events, args });
const createForwardValueArgs =
(parameters: readonly ABIParameter[], events: readonly ContractEventDescriptorClient[]) =>
(
// tslint:disable-next-line no-any readonly-array
...args: any[]
) =>
getForwardValues({ parameters, events, args });

export const convertActions = ({
actions,
Expand Down Expand Up @@ -161,38 +161,40 @@ export const filterLogs = (actions: ReadonlyArray<Event | Log>): readonly Log[]
const isInvokeReceipt = (value: any): value is InvokeReceipt<ContractParameter> =>
typeof value === 'object' && value.result !== undefined && value.events !== undefined && value.logs !== undefined;

const createForwardValueReturn = (returnType: ABIReturn, forwardEvents: readonly ContractEventDescriptorClient[]) => (
receiptOrValue: InvokeReceipt<ContractParameter> | ContractParameter,
// tslint:disable-next-line no-any
): any => {
if (isInvokeReceipt(receiptOrValue)) {
const actions = convertActions({
actions: receiptOrValue.raw.actions,
events: forwardEvents,
});
const foundForwardEvents = filterEvents(actions);
const events = _.uniqBy(
_.sortBy(receiptOrValue.events.concat(foundForwardEvents), [(event: Event) => event.index]),
(event: Event) => event.index,
);
if (receiptOrValue.result.state === 'HALT') {
const value = convertContractParameter({ type: returnType, parameter: receiptOrValue.result.value });

return {
...receiptOrValue,
events,
result: {
...receiptOrValue.result,
value,
},
};
}
const createForwardValueReturn =
(returnType: ABIReturn, forwardEvents: readonly ContractEventDescriptorClient[]) =>
(
receiptOrValue: InvokeReceipt<ContractParameter> | ContractParameter,
// tslint:disable-next-line no-any
): any => {
if (isInvokeReceipt(receiptOrValue)) {
const actions = convertActions({
actions: receiptOrValue.raw.actions,
events: forwardEvents,
});
const foundForwardEvents = filterEvents(actions);
const events = _.uniqBy(
_.sortBy(receiptOrValue.events.concat(foundForwardEvents), [(event: Event) => event.index]),
(event: Event) => event.index,
);
if (receiptOrValue.result.state === 'HALT') {
const value = convertContractParameter({ type: returnType, parameter: receiptOrValue.result.value });

return {
...receiptOrValue,
events,
result: {
...receiptOrValue.result,
value,
},
};
}

return { ...receiptOrValue, events };
}
return { ...receiptOrValue, events };
}

return convertContractParameter({ type: returnType, parameter: receiptOrValue });
};
return convertContractParameter({ type: returnType, parameter: receiptOrValue });
};

export const getParametersObject = ({
abiParameters,
Expand Down
56 changes: 32 additions & 24 deletions packages/neo-one-client-core/src/sc/createSmartContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ export const getParamsAndOptions = ({
readonly transfer?: Transfer;
readonly hash?: Hash256String;
} => {
const { requiredArgs, forwardOptions, options: optionsIn, transfer, hash } = getParamAndOptionsResults({
const {
requiredArgs,
forwardOptions,
options: optionsIn,
transfer,
hash,
} = getParamAndOptionsResults({
parameters,
args,
});
Expand Down Expand Up @@ -186,33 +192,35 @@ export const getParamsAndOptions = ({
};
};

const createCall = ({
definition,
client,
func: { name, parameters = [], returnType, receive = false },
}: {
readonly definition: SmartContractDefinition;
readonly client: Client;
readonly func: ContractMethodDescriptorClient;
// tslint:disable-next-line no-any
}) => async (...args: any[]): Promise<Return | undefined> => {
const { params, network, address } = getParamsAndOptions({
const createCall =
({
definition,
parameters,
args,
receive,
client,
});
func: { name, parameters = [], returnType, receive = false },
}: {
readonly definition: SmartContractDefinition;
readonly client: Client;
readonly func: ContractMethodDescriptorClient;
// tslint:disable-next-line no-any
}) =>
async (...args: any[]): Promise<Return | undefined> => {
const { params, network, address } = getParamsAndOptions({
definition,
parameters,
args,
receive,
client,
});

// TODO: this needs to be reverted when we change how we call contracts
const receipt = await client.__call(network, address, name, [name, ...params]);
// TODO: this needs to be reverted when we change how we call contracts
const receipt = await client.__call(network, address, name, [name, ...params]);

return common.convertCallResult({
returnType,
receipt,
sourceMaps: definition.sourceMaps,
});
};
return common.convertCallResult({
returnType,
receipt,
sourceMaps: definition.sourceMaps,
});
};

const createInvoke = ({
definition,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,57 @@
import { helpers } from '../../../../__data__';
import { DiagnosticCode } from '../../../../DiagnosticCode';

const properties = `
public readonly properties = {
groups: [],
permissions: [],
trusts: "*",
};
`;

describe('Block', () => {
test('properties', async () => {
const node = await helpers.startNode();
const block = await node.readClient.getBlock(0);
const contract = await node.addContract(`
import { Block, Hash256, Address, SmartContract } from '@neo-one/smart-contract';

const test = () => {
const genesisHash = Hash256.from('${block.header.hash}');
Block.for(0);
Block.for(genesisHash);
let block = Block.for(0);
block = Block.for(genesisHash);

assertEqual(block.hash, genesisHash);
assertEqual(block.version, ${block.header.version});
assertEqual(block.previousHash, Hash256.from('${block.header.previousBlockHash}'));
assertEqual(block.index, ${block.header.index});
assertEqual(block.primaryIndex, ${block.header.primaryIndex});
assertEqual(block.merkleRoot, Hash256.from('${block.header.merkleRoot}'));
assertEqual(block.time, ${block.header.time});
assertEqual(block.nextConsensus, Address.from('${block.header.nextConsensus}'));
assertEqual(block.transactionsLength, 0);
assertEqual(block instanceof Block, true);
}

export class BlockContract extends SmartContract {
${properties}

public run(): void {
test();
}
}
`);

await node.executeString(`
import { Block, Hash256, Address } from '@neo-one/smart-contract';

const genesisHash = Hash256.from('${block.hash}');
Block.for(0);
Block.for(genesisHash);
let block = Block.for(0);
block = Block.for(genesisHash);

assertEqual(block.hash, genesisHash);
assertEqual(block.version, ${block.version});
assertEqual(block.previousHash, Hash256.from('${block.previousBlockHash}'));
assertEqual(block.index, ${block.index});
assertEqual(block.time, ${block.time});
assertEqual(block.nextConsensus, Address.from('${block.nextConsensus}'));
assertEqual(block.transactionsLength, 5);
assertEqual(block instanceof Block, true);
import { Address, SmartContract } from '@neo-one/smart-contract';

interface Contract {
run(): void;
}
const contract = SmartContract.for<Contract>(Address.from('${contract.address}'));
contract.run();
`);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ describe('Set', () => {
`);
});

test('can create new set with args', async () => {
await helpers.executeString(`
const x = new Set<string>(['neo']);

assertEqual(x.has('neo'), true);
`);
});

test('cannot be extended', async () => {
await helpers.compileString(
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ export class BuiltinSlotInstanceMemberCall extends BuiltinInstanceMemberCall {
sb.emitPushInt(node, this.slot);
// [callable]
sb.emitOp(node, 'PICKITEM');
// [0, callable]
sb.emitPushInt(node, 0);
// [argsarr, callable]
sb.emitOp(node, 'NEWARRAY');
sb.emitOp(node, 'NEWARRAY0');
// [callable, argsarr]
sb.emitOp(node, 'SWAP');
// [val]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ export class ArrayConcat extends BuiltinInstanceMemberCall {
};

const handleOther = () => {
// [number, val]
sb.emitPushInt(node, 0);
// [arr, val]
sb.emitOp(node, 'NEWARRAY');
sb.emitOp(node, 'NEWARRAY0');
// [arr, val, arr]
sb.emitOp(node, 'TUCK');
// [val, arr, arr]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class SetValue extends BuiltinNew {
sb.emitHelper(
node,
options,
sb.helpers.arrReduce({
sb.helpers.arrReduceWithoutIterator({
each: () => {
// [map, val, map]
sb.emitOp(node, 'TUCK');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ContractGroup,
ContractMethodDescriptorClient,
ContractPermission,
UInt160Hex,
ContractPermissionDescriptor,
WildcardContainer,
} from '@neo-one/client-common';
import { tsUtils } from '@neo-one/ts-utils';
Expand Down Expand Up @@ -134,7 +134,7 @@ export interface SmartContractInfoManifest {
readonly supportedStandards: readonly string[];
readonly abi: SmartContractInfoABI;
readonly permissions: readonly ContractPermission[];
readonly trusts: WildcardContainer<UInt160Hex>;
readonly trusts: WildcardContainer<ContractPermissionDescriptor>;
}

export interface SmartContractInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,21 @@ export class ArrConcatHelper extends Helper {
public emit(sb: ScriptBuilder, node: ts.Node, optionsIn: VisitOptions): void {
const options = sb.pushValueOptions(optionsIn);

// [map, result]
sb.emitHelper(node, options, sb.helpers.arrToMap);
// [enumerator, result]
sb.emitHelper(node, options, sb.helpers.createMapIterator);
// [result]
// [accum, right]
sb.emitOp(node, 'SWAP');
// [arr]
sb.emitHelper(
node,
options,
sb.helpers.forLoop({
condition: () => {
// [enumerator, result, enumerator]
sb.emitOp(node, 'TUCK');
// [boolean, result, enumerator]
sb.emitSysCall(node, 'System.Iterator.Next');
},
sb.helpers.arrReduceWithoutIterator({
each: () => {
// [result, enumerator, result]
sb.emitOp(node, 'TUCK');
// [enumerator, result, enumerator, result]
sb.emitOp(node, 'OVER');
// [value, result, enumerator, result]
sb.emitHelper(node, options, sb.helpers.getMapIteratorValue);
// [enumerator, result]
// [accum, accum, val]
sb.emitOp(node, 'DUP');
// [val, accum, accum]
sb.emitOp(node, 'ROT');
// [accum]
sb.emitOp(node, 'APPEND');
},
cleanup: () => {
// [result]
sb.emitOp(node, 'NIP');
},
}),
);
}
Expand Down
Loading