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

feat(runtime.getnotifications): add notification tracking during vm exec #1804

Merged
merged 1 commit into from
Oct 9, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ exports[`syscalls Neo.Storage.Find 1`] = `
Array [
IteratorStackItem {
"mutableCount": 1,
"referenceID": 15,
"referenceID": 29,
"value": StackItemIterator {
"enumerator": Object {
"next": [Function],
Expand Down Expand Up @@ -997,6 +997,22 @@ exports[`syscalls System.Runtime.GetInvocationCounter 3`] = `Array []`;

exports[`syscalls System.Runtime.GetInvocationCounter 4`] = `Array []`;

exports[`syscalls System.Runtime.GetNotifications 1`] = `Array []`;

exports[`syscalls System.Runtime.GetNotifications 2`] = `Array []`;

exports[`syscalls System.Runtime.GetNotifications 3`] = `Array []`;

exports[`syscalls System.Runtime.GetNotifications 4`] = `Array []`;

exports[`syscalls System.Runtime.GetNotifications 5`] = `Array []`;

exports[`syscalls System.Runtime.GetNotifications 6`] = `Array []`;

exports[`syscalls System.Runtime.GetNotifications 7`] = `Array []`;

exports[`syscalls System.Runtime.GetNotifications 8`] = `Array []`;

exports[`syscalls System.Runtime.GetTime 1`] = `Array []`;

exports[`syscalls System.Runtime.GetTime 2`] = `Array []`;
Expand Down
68 changes: 68 additions & 0 deletions packages/neo-one-node-vm/src/__tests__/syscalls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,74 @@ const SYSCALLS = [
gas: FEES[400],
},

{
name: 'System.Runtime.GetNotifications',
result: [
new ArrayStackItem([
new ArrayStackItem([
new UInt160StackItem(common.bufferToUInt160(Buffer.alloc(20, 0))),
new UInt256StackItem(common.bufferToUInt256(Buffer.alloc(32, 3))),
]),
new ArrayStackItem([
new UInt160StackItem(common.bufferToUInt160(Buffer.alloc(20, 1))),
new ArrayStackItem([
new UInt256StackItem(common.bufferToUInt256(Buffer.alloc(32, 3))),
new BufferStackItem(Buffer.alloc(10, 0)),
]),
]),
]),
],
options: {
stack: [new BufferStackItem(Buffer.alloc(0, 0))],
notifications: [
{
scriptHash: common.bufferToUInt160(Buffer.alloc(20, 0)),
args: new UInt256StackItem(common.bufferToUInt256(Buffer.alloc(32, 3))),
},
{
scriptHash: common.bufferToUInt160(Buffer.alloc(20, 1)),
args: new ArrayStackItem([
new UInt256StackItem(common.bufferToUInt256(Buffer.alloc(32, 3))),
new BufferStackItem(Buffer.alloc(10, 0)),
]),
},
],
},
gas: FEES[10_000],
},

{
name: 'System.Runtime.GetNotifications',
result: [
new ArrayStackItem([
new ArrayStackItem([
new UInt160StackItem(common.bufferToUInt160(Buffer.alloc(20, 1))),
new ArrayStackItem([
new UInt256StackItem(common.bufferToUInt256(Buffer.alloc(32, 3))),
new BufferStackItem(Buffer.alloc(10, 0)),
]),
]),
]),
],
options: {
stack: [new UInt160StackItem(common.bufferToUInt160(Buffer.alloc(20, 1)))],
notifications: [
{
scriptHash: common.bufferToUInt160(Buffer.alloc(20, 0)),
args: new UInt256StackItem(common.bufferToUInt256(Buffer.alloc(32, 3))),
},
{
scriptHash: common.bufferToUInt160(Buffer.alloc(20, 1)),
args: new ArrayStackItem([
new UInt256StackItem(common.bufferToUInt256(Buffer.alloc(32, 3))),
new BufferStackItem(Buffer.alloc(10, 0)),
]),
},
],
},
gas: FEES[10_000],
},

{
name: 'Neo.Crypto.CheckSig',
result: [new BooleanStackItem(true)],
Expand Down
7 changes: 7 additions & 0 deletions packages/neo-one-node-vm/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export interface CreatedContracts {
}
export type InvocationCounter = Record<string, number | undefined>;

export interface VMNotification {
readonly scriptHash: UInt160;
readonly args: StackItem;
}

export interface Options {
readonly depth: number;
readonly stack: ExecutionStack;
Expand All @@ -71,6 +76,7 @@ export interface Options {
readonly entryScriptHash: UInt160;
readonly returnValueCount: number;
readonly stackCount: number;
readonly notifications: readonly VMNotification[];
readonly invocationCounter: InvocationCounter;
readonly pc?: number;
}
Expand All @@ -89,6 +95,7 @@ export interface ExecutionContext {
readonly options?: Options;
}) => Promise<ExecutionContext>;
};
readonly notifications: readonly VMNotification[];
readonly code: Buffer;
readonly scriptHashStack: readonly UInt160[];
readonly scriptHash: UInt160;
Expand Down
5 changes: 5 additions & 0 deletions packages/neo-one-node-vm/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ const run = async ({ context: contextIn }: { readonly context: ExecutionContext
init: context.init,
engine: context.engine,
code: context.code,
notifications: context.notifications,
scriptHashStack: context.scriptHashStack,
scriptHash: context.scriptHash,
callingScriptHash: context.callingScriptHash,
Expand Down Expand Up @@ -182,6 +183,7 @@ export const executeScript = async ({
// tslint:enable no-unnecessary-initializer
entryScriptHash = callingScriptHash,
depth = 1,
notifications = [],
stack = [],
stackAlt = [],
createdContracts = {},
Expand Down Expand Up @@ -213,6 +215,7 @@ export const executeScript = async ({
executeScript,
},
code,
notifications,
scriptHashStack,
scriptHash,
callingScriptHash,
Expand Down Expand Up @@ -294,6 +297,7 @@ export const execute = async ({
depth: scripts.length - idx,
stack: [],
stackAlt: [],
notifications: [],
createdContracts: {},
scriptHashStack: [entryScriptHash],
scriptHash,
Expand All @@ -308,6 +312,7 @@ export const execute = async ({
depth: scripts.length - idx,
stack: context.stack,
stackAlt: context.stackAlt,
notifications: context.notifications,
createdContracts: context.createdContracts,
scriptHashStack: context.scriptHashStack,
scriptHash,
Expand Down
24 changes: 21 additions & 3 deletions packages/neo-one-node-vm/src/syscalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,12 @@ export const SYSCALLS: { readonly [K in SysCallEnum]: CreateSysCall } = {
});
}

return { context };
return {
context: {
...context,
notifications: context.notifications.concat([{ scriptHash: context.scriptHash, args: args[0] }]),
},
};
},
}),

Expand Down Expand Up @@ -489,9 +494,22 @@ export const SYSCALLS: { readonly [K in SysCallEnum]: CreateSysCall } = {
out: 1,
fee: FEES[10_000],
invoke: async ({ context, args }) => {
// need to track notifications
const scriptHash = args[0].asBuffer().length === 0 ? undefined : args[0].asUInt160();
const notifications =
scriptHash === undefined
? context.notifications
: context.notifications.filter((notification) => common.uInt160Equal(notification.scriptHash, scriptHash));

return { context, results: [] };
return {
context,
results: [
new ArrayStackItem(
notifications.map(
(notification) => new ArrayStackItem([new UInt160StackItem(notification.scriptHash), notification.args]),
),
),
],
};
},
}),

Expand Down