Skip to content

Commit

Permalink
feat(vm): remove testMode, fixup vm, various quick fixes from misc up…
Browse files Browse the repository at this point in the history
…dates issue"
  • Loading branch information
danwbyrne committed Dec 29, 2020
1 parent 5215156 commit 35cce4e
Show file tree
Hide file tree
Showing 22 changed files with 120 additions and 127 deletions.
2 changes: 2 additions & 0 deletions packages/neo-one-client-common/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ const isWildcard = (input: unknown): input is Wildcard => input === '*';

const NEGATIVE_SATOSHI_FIXED8 = new BN(-1);
const TEN_FIXED8 = fixed8FromDecimal(10);
const TWENTY_FIXED_8 = fixed8FromDecimal(20);
const ONE_HUNDRED_FIXED8 = fixed8FromDecimal(100);
const FOUR_HUNDRED_FIXED8 = fixed8FromDecimal(400);
const FIVE_HUNDRED_FIXED8 = fixed8FromDecimal(500);
Expand Down Expand Up @@ -249,6 +250,7 @@ export const common = {
MAX_UINT256,
NEGATIVE_SATOSHI_FIXED8,
TEN_FIXED8,
TWENTY_FIXED_8,
ONE_HUNDRED_FIXED8,
FOUR_HUNDRED_FIXED8,
FIVE_HUNDRED_FIXED8,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface FeelessTransactionModelAdd<

export const MAX_TRANSACTION_ATTRIBUTES = 16;
export const MAX_TRANSACTION_SIZE = 102400;
export const MAX_VALID_UNTIL_BLOCK_INCREMENT = 2102400;
export const MAX_VALID_UNTIL_BLOCK_INCREMENT = 5760; // 24 hours
export const DEFAULT_VERSION = 0;

export class FeelessTransactionModel<
Expand Down
9 changes: 2 additions & 7 deletions packages/neo-one-node-blockchain/src/Blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,6 @@ export class Blockchain {
snapshot: 'clone',
container: transaction,
gas: common.ONE_HUNDRED_FIXED8,
testMode: true,
});
}

Expand All @@ -516,7 +515,6 @@ export class Blockchain {
script,
snapshot: 'main',
container: signers,
gas: common.TEN_FIXED8,
});
}

Expand All @@ -526,8 +524,7 @@ export class Blockchain {
container,
persistingBlock,
offset = 0,
testMode = false,
gas = new BN(0),
gas = common.TEN_FIXED8,
}: RunEngineOptions): CallReceipt {
return this.vm.withSnapshots(({ main, clone }) => {
const handler = snapshot === 'main' ? main : clone;
Expand All @@ -543,11 +540,9 @@ export class Blockchain {
container,
snapshot,
gas,
testMode,
},
(engine) => {
engine.loadScript(script);
engine.setInstructionPointer(offset);
engine.loadScript({ script: script, initialPosition: offset });
engine.execute();

return utils.getCallReceipt(engine, container);
Expand Down
6 changes: 2 additions & 4 deletions packages/neo-one-node-blockchain/src/PersistingBlockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ export class PersistingBlockchain {
trigger: TriggerType.System,
snapshot: 'main',
gas: utils.ZERO,
testMode: true,
},
(engine) => {
engine.loadScript(this.onPersistNativeContractScript);
engine.loadScript({ script: this.onPersistNativeContractScript });
const result = engine.execute();
if (result !== VMState.HALT) {
throw new PersistNativeContractsError();
Expand Down Expand Up @@ -89,10 +88,9 @@ export class PersistingBlockchain {
container: transaction,
snapshot: 'clone',
gas: transaction.systemFee,
testMode: false,
},
(engine) => {
engine.loadScript(transaction.script);
engine.loadScript({ script: transaction.script });
const state = engine.execute();
if (state === VMState.HALT) {
clone.deleteTransaction(transaction.hash);
Expand Down
13 changes: 6 additions & 7 deletions packages/neo-one-node-blockchain/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,15 @@ const verifyContract = async (contract: ContractState, vm: VM, transaction: Tran
container: transaction,
snapshot: 'clone',
gas: new BN(0),
testMode: true,
},
(engine) => {
engine.loadScript(contract.script, CallFlags.None);
engine.setInstructionPointer(verify.offset);
if (init !== undefined) {
engine.setInstructionPointer(init.offset);
}
engine.loadScript({
script: contract.script,
flags: CallFlags.None,
initialPosition: init ? init.offset : verify.offset,
});

engine.loadScript(Buffer.from([]), CallFlags.None);
engine.loadScript({ script: Buffer.from([]), flags: CallFlags.None });
const result = engine.execute();

if (result === VMState.FAULT) {
Expand Down
10 changes: 3 additions & 7 deletions packages/neo-one-node-blockchain/src/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,10 @@ export const verifyWithApplicationEngine = (
init?: ContractMethodDescriptor,
): ExecuteScriptResult =>
vm.withApplicationEngine(
{ trigger: TriggerType.Verification, container: verifiable, snapshot: 'clone', gas, testMode: false },
{ trigger: TriggerType.Verification, container: verifiable, snapshot: 'clone', gas },
(engine) => {
engine.loadScript(verification, CallFlags.None);
engine.setInstructionPointer(offset);
if (init !== undefined) {
engine.setInstructionPointer(init.offset);
}
engine.loadScript(verifiable.witnesses[index].invocation, CallFlags.None);
engine.loadScript({ script: verification, flags: CallFlags.None, initialPosition: init ? init.offset : offset });
engine.loadScript({ script: verifiable.witnesses[index].invocation, flags: CallFlags.None });

const state = engine.execute();
if (state === VMState.FAULT) {
Expand Down
11 changes: 5 additions & 6 deletions packages/neo-one-node-core/src/CallFlags.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
export enum CallFlags {
None = 0,
AllowStates = 0b00000001,
AllowModifyStates = 0b00000010,
ReadStates = 0b00000001,
WriteStates = 0b00000010,
AllowCall = 0b00000100,
AllowNotify = 0b00001000,
// tslint:disable-next-line: no-bitwise
ReadOnly = AllowStates | AllowCall | AllowNotify,
// tslint:disable-next-line: no-bitwise
All = AllowStates | AllowModifyStates | AllowCall | AllowNotify,
States = ReadStates | WriteStates,
ReadOnly = ReadStates | AllowCall,
All = States | AllowCall | AllowNotify,
}
1 change: 0 additions & 1 deletion packages/neo-one-node-core/src/ConsensusData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BinaryWriter, ConsensusDataJSON, crypto, IOHelper, JSONHelper, UInt256 } from '@neo-one/client-common';
import { BN } from 'bn.js';
import { DEFAULT_VALIDATORS_COUNT } from './constants';
import {
createSerializeWire,
DeserializeWireBaseOptions,
Expand Down
7 changes: 2 additions & 5 deletions packages/neo-one-node-core/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { BN } from 'bn.js';

export const MAX_TRANSACTION_SIZE = 102400;
export const DEFAULT_VALIDATORS_COUNT = new BN(7);
export const WITNESS_INVOCATION_SIZE = 663;
export const WITNESS_VERIFICATION_SIZE = 361;
export const WITNESS_INVOCATION_SIZE = 1024;
export const WITNESS_VERIFICATION_SIZE = 1024;
12 changes: 8 additions & 4 deletions packages/neo-one-node-core/src/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export interface ApplicationEngineOptions {
readonly container?: SerializableContainer;
readonly snapshot?: SnapshotName;
readonly gas: BN;
readonly testMode: boolean;
}

export interface RunEngineOptions {
Expand All @@ -38,20 +37,25 @@ export interface RunEngineOptions {
readonly container?: SerializableContainer;
readonly persistingBlock?: Block;
readonly offset?: number;
readonly testMode?: boolean;
readonly gas?: BN;
}

export interface LoadScriptOptions {
readonly script: Buffer;
readonly flags?: CallFlags;
readonly scriptHash?: UInt160;
readonly initialPosition?: number;
}

export interface ApplicationEngine {
readonly trigger: TriggerType;
readonly gasConsumed: BN;
readonly resultStack: readonly StackItem[];
readonly state: VMState;
readonly notifications: readonly StackItem[];
readonly logs: readonly VMLog[];
readonly loadScript: (script: Buffer, flag?: CallFlags) => boolean;
readonly loadScript: (options: LoadScriptOptions) => boolean;
readonly execute: () => VMState;
readonly setInstructionPointer: (position: number) => boolean;
}

export type SnapshotPartial = 'blocks' | 'transactions';
Expand Down
39 changes: 20 additions & 19 deletions packages/neo-one-node-vm/lib/Dispatcher.Engine.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
using Neo;
using Neo.IO;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
Expand Down Expand Up @@ -82,25 +83,33 @@ private dynamic dispatchEngineMethod(EngineMethod method, dynamic args)
case EngineMethod.create:
TriggerType trigger = (TriggerType)args.trigger;
long gas = long.Parse((string)args.gas);
bool testMode = (bool)args.testMode;
IVerifiable container = null;
if (args.container != null)
{
container = deserializeContainer(args.container);
}
return this._create(trigger, container, this.selectSnapshot(args.snapshot, false), gas, testMode);
return this._create(trigger, container, this.selectSnapshot(args.snapshot, false), gas);

case EngineMethod.execute:
return this._execute();

case EngineMethod.loadscript:
Script script = new Script((byte[])args.script);
CallFlags flag = (CallFlags)((byte)args.flag);
return this._loadScript(script, flag);
CallFlags flags = (CallFlags)((byte)args.flags);
UInt160 scriptHash = null;
int initialPosition = 0;

case EngineMethod.setinstructionpointer:
int position = (int)args.position;
return this._setInstructionPointer(position);
if (args.scriptHash != null)
{
scriptHash = UInt160.Parse((string)args.scriptHash);
}

if (args.initialPosition != null)
{
initialPosition = (int)args.intialPosition;
}

return this._loadScript(script, flags, scriptHash, initialPosition);

case EngineMethod.getvmstate:
return this._getVMState();
Expand Down Expand Up @@ -139,10 +148,10 @@ private void disposeEngine()
}
}

private bool _create(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas, bool testMode = false)
private bool _create(TriggerType trigger, IVerifiable container, StoreView snapshot, long gas)
{
this.disposeEngine();
this.engine = ApplicationEngine.Create(trigger, container, snapshot, gas, testMode);
this.engine = ApplicationEngine.Create(trigger, container, snapshot, gas);

return true;
}
Expand All @@ -153,18 +162,10 @@ private VMState _execute()
return this.engine.Execute();
}

private bool _loadScript(Script script, CallFlags flag)
private bool _loadScript(Script script, CallFlags flags, UInt160 hash = null, int initialPosition = 0)
{
this.isEngineInitialized();
this.engine.LoadScript(script, flag);
return true;
}

private bool _setInstructionPointer(int initialPosition)
{
this.isEngineInitialized();
this.engine.CurrentContext.InstructionPointer = initialPosition;

this.engine.LoadScript(script, flags, hash, initialPosition);
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/neo-one-node-vm/lib/Dispatcher.Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Neo.Persistence;
using Neo.VM;
using NEOONE.Storage;
using System.Threading;

namespace NEOONE
{
Expand Down Expand Up @@ -36,7 +37,9 @@ private dynamic dispatchSnapshotMethod(SnapshotMethod method, dynamic args)
{
case SnapshotMethod.snapshot_blocks_add:
UInt256 blockHash = new UInt256((byte[])args.hash);
Console.WriteLine("got block hash");
byte[] blockSerialized = (byte[])args.block;
Console.WriteLine("got block serialized");
Block block = new Block();
using (MemoryStream ms = new MemoryStream(blockSerialized))
using (BinaryReader reader = new BinaryReader(ms))
Expand All @@ -45,6 +48,7 @@ private dynamic dispatchSnapshotMethod(SnapshotMethod method, dynamic args)
}

var tmp = block.Hash.ToString();
Console.WriteLine("tmp block hash calculated");

return this._snapshotBlocksAdd(this.selectSnapshot(args.snapshot), blockHash, block);

Expand Down
8 changes: 6 additions & 2 deletions packages/neo-one-node-vm/lib/Dispatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
<PackageReference Include="RocksDbNative" Version="6.2.2" />
<PackageReference Include="RocksDbSharp" Version="6.2.2" />
<PackageReference Include="Neo.VM" Version="3.0.0-preview3" />
<PackageReference Include="Neo-N1-Fork" Version="3.0.1-preview3" />
<PackageReference Include="Neo.VM" Version="3.0.0-preview4" />
<!-- <PackageReference Include="Neo-N1-Fork" Version="3.0.1-preview3" /> -->
</ItemGroup>

<ItemGroup>
<ProjectReference Include="/Users/danielbyrne/neo/src/neo/neo.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions packages/neo-one-node-vm/lib/ReturnHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ public class ProtocolSettingsReturn
public string[] seedList;
public int millisecondsPerBlock;
public int memoryPoolMaxTransactions;
public int maxTraceableBlocks;
public dynamic nativeActivations;

public ProtocolSettingsReturn(ProtocolSettings value)
{
Expand All @@ -185,6 +187,8 @@ public ProtocolSettingsReturn(ProtocolSettings value)
this.seedList = value.SeedList;
this.millisecondsPerBlock = Convert.ToInt32(value.MillisecondsPerBlock);
this.memoryPoolMaxTransactions = value.MemoryPoolMaxTransactions;
this.maxTraceableBlocks = Convert.ToInt32(value.MaxTraceableBlocks);
this.nativeActivations = value.MaxTraceableBlocks;
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions packages/neo-one-node-vm/lib/SnapshotHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public ChangeSet(StoreView snapshot)
{
set.Add(new Change("transaction", value));
}

foreach (var value in snapshot.Contracts.GetChangeSet())
{
set.Add(new Change("contract", value));
}
foreach (var value in snapshot.Storages.GetChangeSet())
{
set.Add(new Change("storage", value));
Expand Down
Loading

0 comments on commit 35cce4e

Please sign in to comment.