Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Remove OpCode.HASH160 and OpCode.HASH256 #146

Merged
merged 4 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 8 additions & 8 deletions src/neo-vm/Debugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ namespace Neo.VM
public class Debugger
{
private readonly ExecutionEngine engine;
private readonly Dictionary<byte[], HashSet<uint>> break_points = new Dictionary<byte[], HashSet<uint>>(new HashComparer());
private readonly Dictionary<Script, HashSet<uint>> break_points = new Dictionary<Script, HashSet<uint>>();

public Debugger(ExecutionEngine engine)
{
this.engine = engine;
}

public void AddBreakPoint(byte[] script_hash, uint position)
public void AddBreakPoint(Script script, uint position)
{
if (!break_points.TryGetValue(script_hash, out HashSet<uint> hashset))
if (!break_points.TryGetValue(script, out HashSet<uint> hashset))
{
hashset = new HashSet<uint>();
break_points.Add(script_hash, hashset);
break_points.Add(script, hashset);
}
hashset.Add(position);
}
Expand All @@ -36,16 +36,16 @@ private void ExecuteAndCheckBreakPoints()
engine.ExecuteNext();
if (engine.State == VMState.NONE && engine.InvocationStack.Count > 0 && break_points.Count > 0)
{
if (break_points.TryGetValue(engine.CurrentContext.ScriptHash, out HashSet<uint> hashset) && hashset.Contains((uint)engine.CurrentContext.InstructionPointer))
if (break_points.TryGetValue(engine.CurrentContext.Script, out HashSet<uint> hashset) && hashset.Contains((uint)engine.CurrentContext.InstructionPointer))
engine.State = VMState.BREAK;
}
}

public bool RemoveBreakPoint(byte[] script_hash, uint position)
public bool RemoveBreakPoint(Script script, uint position)
{
if (!break_points.TryGetValue(script_hash, out HashSet<uint> hashset)) return false;
if (!break_points.TryGetValue(script, out HashSet<uint> hashset)) return false;
if (!hashset.Remove(position)) return false;
if (hashset.Count == 0) break_points.Remove(script_hash);
if (hashset.Count == 0) break_points.Remove(script);
return true;
}

Expand Down
23 changes: 4 additions & 19 deletions src/neo-vm/ExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,43 +50,28 @@ public Instruction NextInstruction
}
}

/// <summary>
/// Cached script hash
/// </summary>
public byte[] ScriptHash
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return Script.ScriptHash;
}
}

public byte[] CallingScriptHash { get; }

/// <summary>
/// Constructor
/// </summary>
/// <param name="script">Script</param>
/// <param name="callingScriptHash">Script hash of the calling script</param>
/// <param name="rvcount">Number of items to be returned</param>
internal ExecutionContext(Script script, byte[] callingScriptHash, int rvcount)
: this(script, callingScriptHash, rvcount, new RandomAccessStack<StackItem>(), new RandomAccessStack<StackItem>())
internal ExecutionContext(Script script, int rvcount)
: this(script, rvcount, new RandomAccessStack<StackItem>(), new RandomAccessStack<StackItem>())
{
}

private ExecutionContext(Script script, byte[] callingScriptHash, int rvcount, RandomAccessStack<StackItem> stack, RandomAccessStack<StackItem> alt)
private ExecutionContext(Script script, int rvcount, RandomAccessStack<StackItem> stack, RandomAccessStack<StackItem> alt)
{
this.RVCount = rvcount;
this.Script = script;
this.EvaluationStack = stack;
this.AltStack = alt;
this.CallingScriptHash = callingScriptHash;
}

internal ExecutionContext Clone()
{
return new ExecutionContext(Script, ScriptHash, 0, EvaluationStack, AltStack);
return new ExecutionContext(Script, 0, EvaluationStack, AltStack);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
23 changes: 5 additions & 18 deletions src/neo-vm/ExecutionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public class ExecutionEngine : IDisposable
public IInteropService Service { get; }
public RandomAccessStack<ExecutionContext> InvocationStack { get; } = new RandomAccessStack<ExecutionContext>();
public RandomAccessStack<StackItem> ResultStack { get; } = new RandomAccessStack<StackItem>();
public ExecutionContext CurrentContext => InvocationStack.Peek();
public byte[] EntryScriptHash { get; private set; }
public ExecutionContext CurrentContext => InvocationStack.Count > 0 ? InvocationStack.Peek() : null;
public ExecutionContext CallingContext => InvocationStack.Count > 1 ? InvocationStack.Peek(1) : null;
public ExecutionContext EntryContext => InvocationStack.Count > 0 ? InvocationStack.Peek(InvocationStack.Count - 1) : null;
public VMState State { get; internal protected set; } = VMState.BREAK;

public ExecutionEngine(IScriptContainer container, ICrypto crypto, IInteropService service = null)
Expand Down Expand Up @@ -809,18 +810,6 @@ private bool ExecuteInstruction()
context.EvaluationStack.Push(sha.ComputeHash(x));
break;
}
case OpCode.HASH160:
{
byte[] x = context.EvaluationStack.Pop().GetByteArray();
context.EvaluationStack.Push(Crypto.Hash160(x));
break;
}
case OpCode.HASH256:
{
byte[] x = context.EvaluationStack.Pop().GetByteArray();
context.EvaluationStack.Push(Crypto.Hash256(x));
break;
}
case OpCode.CHECKSIG:
{
byte[] pubkey = context.EvaluationStack.Pop().GetByteArray();
Expand Down Expand Up @@ -1192,14 +1181,12 @@ protected virtual void LoadContext(ExecutionContext context)
{
if (InvocationStack.Count >= MaxInvocationStackSize)
throw new InvalidOperationException();
if (EntryScriptHash is null)
EntryScriptHash = context.ScriptHash;
InvocationStack.Push(context);
}

public ExecutionContext LoadScript(byte[] script, byte[] callingScriptHash = null, int rvcount = -1)
public ExecutionContext LoadScript(byte[] script, int rvcount = -1)
{
ExecutionContext context = new ExecutionContext(new Script(Crypto, script), callingScriptHash, rvcount);
ExecutionContext context = new ExecutionContext(new Script(script), rvcount);
LoadContext(context);
return context;
}
Expand Down
17 changes: 0 additions & 17 deletions src/neo-vm/HashComparer.cs

This file was deleted.

4 changes: 0 additions & 4 deletions src/neo-vm/ICrypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
{
public interface ICrypto
{
byte[] Hash160(byte[] message);

byte[] Hash256(byte[] message);

bool VerifySignature(byte[] message, byte[] signature, byte[] pubkey);
}
}
8 changes: 0 additions & 8 deletions src/neo-vm/OpCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,6 @@ public enum OpCode : byte
/// </summary>
SHA256 = 0xA8,
/// <summary>
/// The input is hashed using Hash160: first with SHA-256 and then with RIPEMD-160.
/// </summary>
HASH160 = 0xA9,
/// <summary>
/// The input is hashed using Hash256: twice with SHA-256.
/// </summary>
HASH256 = 0xAA,
/// <summary>
/// The publickey and signature are taken from main stack. Verifies if transaction was signed by given publickey and a boolean output is put on top of the main stack.
/// </summary>
CHECKSIG = 0xAC,
Expand Down
31 changes: 1 addition & 30 deletions src/neo-vm/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,9 @@ namespace Neo.VM
{
public class Script
{
private byte[] _scriptHash = null;

private readonly byte[] _value;
private readonly ICrypto _crypto;
private readonly Dictionary<int, Instruction> _instructions = new Dictionary<int, Instruction>();

/// <summary>
/// Cached script hash
/// </summary>
public byte[] ScriptHash
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_scriptHash == null) _scriptHash = _crypto.Hash160(_value);
return _scriptHash;
}
}

/// <summary>
/// Script length
/// </summary>
Expand Down Expand Up @@ -53,22 +37,9 @@ public OpCode this[int index]
/// <summary>
/// Constructor
/// </summary>
/// <param name="crypto">Crypto</param>
/// <param name="script">Script</param>
public Script(ICrypto crypto, byte[] script)
{
_crypto = crypto;
_value = script;
}

/// <summary>
/// Constructor
/// </summary>
/// <param name="hash">Hash</param>
/// <param name="script">Script</param>
internal Script(byte[] hash, byte[] script)
public Script(byte[] script)
{
_scriptHash = hash;
_value = script;
}

Expand Down
15 changes: 0 additions & 15 deletions src/neo-vm/Unsafe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,5 @@ public static bool NotZero(byte[] x)
}
return false;
}

/// <summary>
/// Convert byte array to int32
/// </summary>
/// <param name="value">Value (must be checked before this call)</param>
/// <param name="startIndex">Start index</param>
/// <returns>Integer</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ToInt32(byte[] value, int startIndex)
{
fixed (byte* pbyte = &value[startIndex])
{
return *(int*)pbyte;
}
}
}
}
9 changes: 0 additions & 9 deletions tests/neo-vm.Tests/Cryptography/ECC/ECCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ private ECCurve(BigInteger Q, BigInteger A, BigInteger B, BigInteger N, byte[] G
this.G = ECPoint.DecodePoint(G, this);
}

public static readonly ECCurve Secp256k1 = new ECCurve
(
BigInteger.Parse("00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", NumberStyles.AllowHexSpecifier),
BigInteger.Zero,
7,
BigInteger.Parse("00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", NumberStyles.AllowHexSpecifier),
("04" + "79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798" + "483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8").FromHexString()
);

public static readonly ECCurve Secp256r1 = new ECCurve
(
BigInteger.Parse("00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF", NumberStyles.AllowHexSpecifier),
Expand Down
Loading