Skip to content

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
Develop

See merge request company-projects/Meadow!39
  • Loading branch information
zone117x committed Oct 5, 2018
2 parents 0844018 + 30aead3 commit 707d64b
Show file tree
Hide file tree
Showing 37 changed files with 445 additions and 90 deletions.
2 changes: 1 addition & 1 deletion src/Meadow.Core/Meadow.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<PackageReference Include="SolcNet" Version="1.1.106" />
<PackageReference Include="System.Buffers" Version="4.5.0" />
<PackageReference Include="System.Memory" Version="4.5.1" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.1" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
</ItemGroup>

<ItemGroup>
Expand Down
17 changes: 9 additions & 8 deletions src/Meadow.CoverageReport/Debugging/ExecutionTraceAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Meadow.CoverageReport.Debugging.Variables.Pairing;
using Meadow.CoverageReport.Debugging.Variables.Storage;
using Meadow.CoverageReport.Models;
using Meadow.JsonRpc.Client;
using Meadow.JsonRpc.Types.Debugging;
using SolcNet.DataDescription.Output;
using SolcNet.DataDescription.Parsing;
Expand Down Expand Up @@ -996,13 +997,13 @@ private int ParseScopes(int traceIndex, int scopeDepth, int callDepth, Execution
}


public VariableValuePair[] GetLocalVariables()
public VariableValuePair[] GetLocalVariables(IJsonRpcClient rpcClient = null)
{
// Obtain the local variables for the last trace point.
return GetLocalVariables(ExecutionTrace.Tracepoints.Length - 1);
return GetLocalVariables(ExecutionTrace.Tracepoints.Length - 1, rpcClient);
}

public VariableValuePair[] GetLocalVariables(int traceIndex)
public VariableValuePair[] GetLocalVariables(int traceIndex, IJsonRpcClient rpcClient = null)
{
// Obtain the scope for this trace index
ExecutionTraceScope currentScope = GetScope(traceIndex);
Expand Down Expand Up @@ -1038,7 +1039,7 @@ public VariableValuePair[] GetLocalVariables(int traceIndex)
}

// Parse our variable's value.
object value = variable.ValueParser.ParseFromStack(tracePoint.Stack, variable.StackIndex, memory, StorageManager);
object value = variable.ValueParser.ParseFromStack(tracePoint.Stack, variable.StackIndex, memory, StorageManager, rpcClient);

// Add our local variable to our results
result.Add(new VariableValuePair(variable, value));
Expand All @@ -1048,13 +1049,13 @@ public VariableValuePair[] GetLocalVariables(int traceIndex)
return result.ToArray();
}

public VariableValuePair[] GetStateVariables()
public VariableValuePair[] GetStateVariables(IJsonRpcClient rpcClient = null)
{
// Obtain the state variables for the last trace point.
return GetStateVariables(ExecutionTrace.Tracepoints.Length - 1);
return GetStateVariables(ExecutionTrace.Tracepoints.Length - 1, rpcClient);
}

public VariableValuePair[] GetStateVariables(int traceIndex)
public VariableValuePair[] GetStateVariables(int traceIndex, IJsonRpcClient rpcClient = null)
{
// Obtain the scope for this trace index
ExecutionTraceScope currentScope = GetScope(traceIndex);
Expand Down Expand Up @@ -1098,7 +1099,7 @@ public VariableValuePair[] GetStateVariables(int traceIndex)
if (!stateVariable.Declaration.Constant)
{
// Parse our variable from storage
variableValue = stateVariable.ValueParser.ParseFromStorage(StorageManager, stateVariable.StorageLocation);
variableValue = stateVariable.ValueParser.ParseFromStorage(StorageManager, stateVariable.StorageLocation, rpcClient);
}

// Add the state variable to our result list.
Expand Down
36 changes: 28 additions & 8 deletions src/Meadow.CoverageReport/Debugging/Variables/BaseVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public abstract class BaseVariable
/// <summary>
/// The name of this variable.
/// </summary>
public string Name => Declaration.Name;
public string Name { get; private set; }
/// <summary>
/// The AST node which describes the type for this variable.
/// </summary>
public AstElementaryTypeName AstTypeName { get; private set; }
/// <summary>
/// The base type string of this variable, which is likened to the full <see cref="Type"/> but with location information stripped.
/// </summary>
Expand All @@ -43,21 +47,37 @@ public abstract class BaseVariable
#endregion

#region Constructor
protected BaseVariable(AstVariableDeclaration declaration)
{
// Set our properties
Declaration = declaration;

// Initialize by name and type.
Initialize(Declaration.Name, Declaration.TypeName);
}

/// <summary>
/// Initializes the variable with a given declaration, parsing all relevant type information and associating the appropriate value parser.
/// </summary>
/// <param name="declaration">The ast variable declaration which constitutes the solidity declaration of the variable we wish to interface with.</param>
public BaseVariable(AstVariableDeclaration declaration)
/// <param name="name">The name to use to describe the variable.</param>
/// <param name="astTypeName">The AST node which describes the type name for this variable.</param>
protected BaseVariable(string name, AstElementaryTypeName astTypeName)
{
// Set our properties
Declaration = declaration;
BaseType = VarTypes.ParseTypeComponents(Declaration.TypeName.TypeDescriptions.TypeString).baseType;
GenericType = VarTypes.GetGenericType(BaseType);
ValueParser = VarTypes.GetVariableObject(Declaration.TypeName, VariableLocation);
// Initialize by name and type.
Initialize(name, astTypeName);
}
#endregion

#region Functions
protected void Initialize(string name, AstElementaryTypeName astTypeName)
{
// Set our properties
Name = name;
AstTypeName = astTypeName;
BaseType = VarParser.ParseTypeComponents(AstTypeName.TypeDescriptions.TypeString).baseType;
GenericType = VarParser.GetGenericType(BaseType);
ValueParser = VarParser.GetVariableObject(AstTypeName, VariableLocation);
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Meadow.CoverageReport.Debugging.Variables.Pairing
{
/// <summary>
/// Represents a mapping key-value pair.
/// </summary>
public struct MappingKeyValuePair
{
#region Properties
public readonly VariableValuePair Key;
public readonly VariableValuePair Value;
#endregion

#region Constructor
public MappingKeyValuePair(VariableValuePair key, VariableValuePair value)
{
// Set our properties
Key = key;
Value = value;
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override VarLocation VariableLocation
get
{
// Obtain our base value
AstVariableStorageLocation storageLoc = Declaration.StorageLocation;
AstVariableStorageLocation storageLoc = Declaration?.StorageLocation ?? AstVariableStorageLocation.Default;

// If our location is stated as the default location, we return our specific values.
switch (storageLoc)
Expand All @@ -49,6 +49,12 @@ public override VarLocation VariableLocation

#region Constructor
public StateVariable(AstVariableDeclaration declaration) : base(declaration)
{
// Set our declaration
Declaration = declaration;
}

public StateVariable(string name, AstElementaryTypeName astTypeName) : base(name, astTypeName)
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Meadow.CoverageReport.AstTypes;
using Meadow.CoverageReport.Debugging.Variables.Enums;
using Meadow.CoverageReport.Debugging.Variables.Storage;
using Meadow.JsonRpc.Client;
using System;
using System.Collections.Generic;
using System.Numerics;
Expand All @@ -23,13 +24,13 @@ public class VarArray : VarRefBase
public VarArray(AstArrayTypeName type, VarLocation location) : base(type)
{
// Obtain our array size
ArraySize = VarTypes.ParseArrayTypeComponents(BaseType).arraySize;
ArraySize = VarParser.ParseArrayTypeComponents(BaseType).arraySize;

// Set our type name
ArrayTypeName = type;

// Set our element parser with the given array element/base type.
ElementObject = VarTypes.GetVariableObject(ArrayTypeName.BaseType, location);
ElementObject = VarParser.GetVariableObject(ArrayTypeName.BaseType, location);

// Define our bounds variables
int storageSlotCount = 1;
Expand Down Expand Up @@ -97,7 +98,7 @@ public override object ParseDereferencedFromMemory(Memory<byte> memory, int offs
return arrayElements;
}

public override object ParseFromStorage(StorageManager storageManager, StorageLocation storageLocation)
public override object ParseFromStorage(StorageManager storageManager, StorageLocation storageLocation, IJsonRpcClient rpcClient = null)
{
// Obtain our storage value for our given storage location.
Memory<byte> storageData = storageManager.ReadStorageSlot(storageLocation.SlotKey, storageLocation.DataOffset, SizeBytes);
Expand Down Expand Up @@ -129,7 +130,7 @@ public override object ParseFromStorage(StorageManager storageManager, StorageLo
for (int i = 0; i < arrayElements.Length; i++)
{
// Decode our element at this index
arrayElements[i] = ElementObject.ParseFromStorage(storageManager, elementLocation);
arrayElements[i] = ElementObject.ParseFromStorage(storageManager, elementLocation, rpcClient);

// Determine how to iterate, dependent on if the array is dynamically sized or not, as described earlier,
// (since it could compact multiple elements into a single storage slot).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Meadow.CoverageReport.AstTypes;
using Meadow.CoverageReport.Debugging.Variables.Enums;
using Meadow.CoverageReport.Debugging.Variables.Storage;
using Meadow.JsonRpc.Client;
using System;
using System.Numerics;

Expand All @@ -25,10 +26,10 @@ public VarBase(AstElementaryTypeName type)
Type = type;

// Obtain the components of our type and set them.
BaseType = VarTypes.ParseTypeComponents(type.TypeDescriptions.TypeString).baseType;
BaseType = VarParser.ParseTypeComponents(type.TypeDescriptions.TypeString).baseType;

// Obtain our generic type.
GenericType = VarTypes.GetGenericType(BaseType);
GenericType = VarParser.GetGenericType(BaseType);
}
#endregion

Expand All @@ -46,7 +47,7 @@ public virtual object ParseData(Memory<byte> data)
throw new NotImplementedException();
}

public virtual object ParseFromStack(Data[] stack, int stackIndex, Memory<byte> memory, StorageManager storageManager)
public virtual object ParseFromStack(Data[] stack, int stackIndex, Memory<byte> memory, StorageManager storageManager, IJsonRpcClient rpcClient = null)
{
// If we exceeded our stack size
if (stack.Length <= stackIndex)
Expand All @@ -68,7 +69,7 @@ public virtual object ParseFromMemory(Memory<byte> memory, int offset)
return ParseData(data);
}

public virtual object ParseFromStorage(StorageManager storageManager, StorageLocation storageLocation)
public virtual object ParseFromStorage(StorageManager storageManager, StorageLocation storageLocation, IJsonRpcClient rpcClient = null)
{
// Obtain our storage value for our given storage location.
Memory<byte> storageValue = storageManager.ReadStorageSlot(storageLocation.SlotKey, storageLocation.DataOffset, SizeBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Meadow.CoverageReport.AstTypes;
using Meadow.CoverageReport.Debugging.Variables.Enums;
using Meadow.CoverageReport.Debugging.Variables.Storage;
using Meadow.JsonRpc.Client;
using System;
using System.Collections.Generic;
using System.Numerics;
Expand Down Expand Up @@ -34,7 +35,7 @@ public override object ParseDereferencedFromMemory(Memory<byte> memory, int offs
return memory.Slice(offset + UInt256.SIZE, (int)length);
}

public override object ParseFromStorage(StorageManager storageManager, StorageLocation storageLocation)
public override object ParseFromStorage(StorageManager storageManager, StorageLocation storageLocation, IJsonRpcClient rpcClient = null)
{
// Obtain our storage value for our given storage location.
Memory<byte> storageData = storageManager.ReadStorageSlot(storageLocation.SlotKey, storageLocation.DataOffset, SizeBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class VarFixedBytes : VarBase
public VarFixedBytes(AstElementaryTypeName type) : base(type)
{
// Determine the size of our fixed array.
int sizeBytes = VarTypes.GetFixedArraySizeInBytes(BaseType);
int sizeBytes = VarParser.GetFixedArraySizeInBytes(BaseType);

// Initialize our bounds
InitializeBounds(1, sizeBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class VarInt : VarBase
public VarInt(AstElementaryTypeName type) : base(type)
{
// Obtain our size in bytes
int sizeBytes = VarTypes.GetIntegerSizeInBytes(BaseType, GenericType);
int sizeBytes = VarParser.GetIntegerSizeInBytes(BaseType, GenericType);

// Initialize our bounds
InitializeBounds(1, sizeBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using Meadow.Core.Utils;
using Meadow.CoverageReport.AstTypes;
using Meadow.CoverageReport.Debugging.Variables.Enums;
using Meadow.CoverageReport.Debugging.Variables.Pairing;
using Meadow.CoverageReport.Debugging.Variables.Storage;
using Meadow.JsonRpc.Client;
using System;
using System.Collections.Generic;
using System.Numerics;
Expand All @@ -28,10 +30,60 @@ public VarMapping(AstMappingTypeName type) : base(type)
#endregion

#region Functions
public override object ParseFromStorage(StorageManager storageManager, StorageLocation storageLocation)
public override object ParseFromStorage(StorageManager storageManager, StorageLocation storageLocation, IJsonRpcClient rpcClient = null)
{
// TODO: Implement
return null;
// Create our result array
var results = new List<MappingKeyValuePair>();

// We'll want to loop for every key in storage at this point
var storage = storageManager.ExecutionTrace.Tracepoints[storageManager.TraceIndex].Storage;
foreach (Memory<byte> storageKey in storage.Keys)
{
// Define our current key (in case we must iterate upward through children to reach this mapping).
Memory<byte> currentStorageKey = storageKey;

// The point from which we will iterate/recursive upwards on.
IterateNested:

// Try to obtain a preimage from this key
var storageKeyPreimage = rpcClient?.GetHashPreimage(currentStorageKey.ToArray())?.Result;
if (storageKeyPreimage != null)
{
// Verify our pre-image is 2 WORDs in length.
if (storageKeyPreimage.Length == UInt256.SIZE * 2)
{
// If so, slice the latter WORD (parent location) and compare it to our current storage location
var derivedBaseLocation = storageKeyPreimage.Slice(UInt256.SIZE);
if (storageLocation.SlotKey.Span.SequenceEqual(derivedBaseLocation))
{
// Obtain our value hashed with our parent location (original key to our mapping).
byte[] originalStorageKeyData = storageKeyPreimage.Slice(0, UInt256.SIZE);

// Obtain our key and value's variable-value-pair.
StateVariable storageKeyVariable = new StateVariable($"K[{results.Count}]", MappingTypeName.KeyType);
StateVariable storageValueVariable = new StateVariable($"V[{results.Count}]", MappingTypeName.ValueType);

// Obtain our resulting key-value pair.
var keyValuePair = new MappingKeyValuePair(
new VariableValuePair(storageKeyVariable, storageKeyVariable.ValueParser.ParseData(originalStorageKeyData)),
new VariableValuePair(storageValueVariable, storageValueVariable.ValueParser.ParseFromStorage(storageManager, new StorageLocation(currentStorageKey, 0), rpcClient)));

// Add our key-value pair to the results.
results.Add(keyValuePair);
}
else
{
// This derived location is not referencing this location. Iterate upward on it to determine if it's a child.
currentStorageKey = derivedBaseLocation;
goto IterateNested;
}
}
}

}

// Return our results array.
return results.ToArray();
}
#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Meadow.CoverageReport.AstTypes;
using Meadow.CoverageReport.Debugging.Variables.Enums;
using Meadow.CoverageReport.Debugging.Variables.Storage;
using Meadow.JsonRpc.Client;

namespace Meadow.CoverageReport.Debugging.Variables.UnderlyingTypes
{
Expand Down Expand Up @@ -49,7 +50,7 @@ public override object ParseFromMemory(Memory<byte> memory, int offset)
return ParseDereferencedFromMemory(memory, (int)pointer);
}

public override object ParseFromStack(Data[] stack, int stackIndex, Memory<byte> memory, StorageManager storageManager)
public override object ParseFromStack(Data[] stack, int stackIndex, Memory<byte> memory, StorageManager storageManager, IJsonRpcClient rpcClient = null)
{
// If we exceeded our stack size
if (stack.Length <= stackIndex)
Expand All @@ -75,7 +76,7 @@ public override object ParseFromStack(Data[] stack, int stackIndex, Memory<byte>
StorageLocation storageLocation = new StorageLocation(stackEntryData, 0);

// Parse our value from storage. (Using our stack data as a storage key)
return ParseFromStorage(storageManager, storageLocation);
return ParseFromStorage(storageManager, storageLocation, rpcClient);

default:
throw new VarResolvingException("Could not parse variable from stack using reference type because the provided underlying location is invalid.");
Expand Down

0 comments on commit 707d64b

Please sign in to comment.