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!30
  • Loading branch information
zone117x committed Oct 3, 2018
2 parents 2a491e5 + e4a8ec1 commit e161227
Show file tree
Hide file tree
Showing 19 changed files with 356 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Perform thorough testing of Solidity codebases. Generate HTML and JSON code cove

<img src="/images/screenshot3.png?raw=true" width="800" />

Solidity debugger extension for Visual Studio Code supporting beakpoints, stepping, rewinding, call stacks, local & state variable inspection.
Solidity debugger extension for Visual Studio Code supporting breakpoints, stepping, rewinding, call stacks, local & state variable inspection.

---

Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageProjectUrl>https://github.com/hosho</PackageProjectUrl>
<RepositoryUrl>https://github.com/hosho</RepositoryUrl>
<PackageLicenseUrl>https://www.gnu.org/licenses/gpl-3.0.en.html</PackageLicenseUrl>
<NoWarn>$(NoWarn);1591;1573;NU1603;NU1701;CS1701</NoWarn>
<NoWarn>$(NoWarn);1591;1573;NU1603</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)\solution.ruleset</CodeAnalysisRuleSet>
<DefineConstants>$(DefineConstants);LANG_7_3</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Meadow.CoverageReport/ReportGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ static string HashSha256(string content)
sourceFileLine.LiteralSourceCodeLine = lines[lineNum];
sourceFileLine.LineNumber = lineNum + 1;
sourceFileLine.Offset = charCount;
sourceFileLine.Length = sourceFileLine.LiteralSourceCodeLine.Length;
sourceFileLine.Length = UTF8.GetByteCount(sourceFileLine.LiteralSourceCodeLine);

sourceFileLine.CorrelatedAstNodes = SourceLineMatching.MatchAstNodesToSourceFileLine(analysis, sourceFileLine).ToArray();

Expand Down
2 changes: 1 addition & 1 deletion src/Meadow.DebugExampleTests/GlobalSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public static class GlobalSetup
[AssemblyInitialize]
public static async Task Init(TestContext testContext)
{
await Global.Init(testContext);
Global.HideSolidityFromReport("mocks", "IgnoreContract.sol");
await Task.CompletedTask;
}

[AssemblyCleanup]
Expand Down
80 changes: 62 additions & 18 deletions src/Meadow.TestNode/TestNodeServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,6 @@ public Task<UInt256> GasPrice()
return Task.FromResult(new UInt256(TestChain.MinimumGasPrice));
}

public Task<UInt256> EstimateGas(CallParams callParams, DefaultBlockParameter blockParameter)
{
// Obtain a state from this block parameter.
State postBlockState = GetStateFromBlockParameters(blockParameter);
if (postBlockState == null)
{
postBlockState = TestChain.Chain.State;
}

// We execute our call on the current state and obtain our result
var result = HandleCall(callParams, postBlockState);

// Return our result.
var gasState = result.EVM.GasState;
return Task.FromResult((UInt256)(gasState.InitialGas - gasState.Gas));
}

public Task<Address> Coinbase()
{
// Return our address
Expand Down Expand Up @@ -329,6 +312,67 @@ public Task<Hash> SendRawTransaction(byte[] signedData)
return ProcessTransactionInternal(transaction, targetInformation.deploying, targetInformation.targetDeployedAddress);
}

public Task<UInt256> EstimateGas(CallParams callParams, DefaultBlockParameter blockParameter)
{
// Verify our block parameters are for the latest block.
if (blockParameter.ParameterType == BlockParameterType.Earliest ||
blockParameter.ParameterType == BlockParameterType.Pending)
{
return Task.FromException<UInt256>(new NotImplementedException("EstimateGas does not support estimations at earlier or pending points in the chain."));
}

if (blockParameter.ParameterType == BlockParameterType.BlockNumber && TestChain.Chain.State.CurrentBlock.Header.BlockNumber != blockParameter.BlockNumber)
{
return Task.FromException<UInt256>(new NotImplementedException("EstimateGas only supports estimations at the current block number in the chain."));
}

// Snapshot our state at this height in block
ulong snapshotID = Snapshot().Result;

// Define our resulting transaction receipt and exception (in case one occurs)
TransactionReceipt transactionReceipt = null;
Exception transactionException = null;

// Try to process our transaction and obtain the receipt.
try
{
// Process the transaction and obtain the transaction hash.
var transactionHash = SendTransaction(new TransactionParams()
{
From = callParams.From,
To = callParams.To,
Data = callParams.Data,
Gas = callParams.Gas,
GasPrice = callParams.GasPrice,
Nonce = null,
Value = callParams.Value
}).Result;

// Obtain our transaction receipt.
transactionReceipt = GetTransactionReceipt(transactionHash).Result;
}
catch (Exception ex)
{
// An exception occurred, so we set our transaction exception.
transactionException = ex;
}

// Revert to our previous state
Revert(snapshotID);

// Remove the snapshot from our lookup
Snapshots.Remove(snapshotID);

// If our exception is not null, return it
if (transactionException != null)
{
return Task.FromException<UInt256>(transactionException);
}

// Return our gas used.
return Task.FromResult((UInt256)transactionReceipt.GasUsed);
}

public Task<Hash> SendTransaction(TransactionParams transactionParams)
{
// Create our default parameters
Expand Down Expand Up @@ -1037,7 +1081,7 @@ private BigInteger BlockNumberFromBlockParameters(DefaultBlockParameter blockPar
return TestChain.Chain.GetHeadBlock().Header.BlockNumber;
}
else if (blockParameter.ParameterType == BlockParameterType.Pending)
{
{
// TODO: verify this..
// Test node instantly mine blocks so there is no pending block, use latest block instead
return TestChain.Chain.GetHeadBlock().Header.BlockNumber;
Expand Down
134 changes: 134 additions & 0 deletions src/Meadow.UnitTestTemplate.ParallelTest/GasUsageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using Meadow.Core.EthTypes;
using Meadow.Core.Utils;
using Meadow.JsonRpc.Types;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace Meadow.UnitTestTemplate.ParallelTest
{
[ParallelTestClass]
public class GasUsageTests : ContractTest
{
PrecompilesContract _precompiles;
BasicContract _basicContract;

protected override async Task BeforeEach()
{
// Deploy our test contract
_basicContract = await BasicContract.New($"TestName", true, 34, RpcClient, new TransactionParams { From = Accounts[0], Gas = 4712388 }, Accounts[0]);
_precompiles = await PrecompilesContract.New(RpcClient, new TransactionParams { From = Accounts[0], Gas = 4712388 }, Accounts[0]);

}

[TestMethod]
public async Task EcRecoverTest()
{
var gas = await _precompiles.testECRecover(
new byte[] { 0xc9, 0xf1, 0xc7, 0x66, 0x85, 0x84, 0x5e, 0xa8, 0x1c, 0xac, 0x99, 0x25, 0xa7, 0x56, 0x58, 0x87, 0xb7, 0x77, 0x1b, 0x34, 0xb3, 0x5e, 0x64, 0x1c, 0xca, 0x85, 0xdb, 0x9f, 0xef, 0xd0, 0xe7, 0x1f },
0x1c,
BigIntegerConverter.GetBytes(BigInteger.Parse("68932463183462156574914988273446447389145511361487771160486080715355143414637", CultureInfo.InvariantCulture)),
BigIntegerConverter.GetBytes(BigInteger.Parse("47416572686988136438359045243120473513988610648720291068939984598262749281683", CultureInfo.InvariantCulture)))
.EstimateGas();

Assert.AreEqual(32437, gas);
}

[TestMethod]
public async Task Sha256Test()
{
var gas = await _precompiles.testSha256("hello world").EstimateGas();
Assert.AreEqual(24063, gas);
}

[TestMethod]
public async Task Ripemd160Test()
{
var gas = await _precompiles.testRipemd160("hello world").EstimateGas();
Assert.AreEqual(24671, gas);
}

[DataTestMethod]
public async Task IdentityTest()
{
// Create a list of input strings to test echoing with the identity precompile.
string[] inputStrings =
{
"",
"okayTestTest!",
"okayTest\x85TEST\x00TEST"
};

UInt256[] resultGasUsages =
{
23065, 24413, 24770
};

// Loop for each input to test echoing
for (int i = 0; i < inputStrings.Length; i++)
{
// Encode our string as bytes, and try echoing it back. Obtain gas usage.
var gas = await _precompiles.testIdentity(Encoding.UTF8.GetBytes(inputStrings[i])).EstimateGas();

// Assert our gas usage
Assert.AreEqual(resultGasUsages[i], gas);
}
}

[TestMethod]
public async Task ModExpTest()
{
// Test the modexp precompile.
var gas = await _precompiles.testModExp(
BigIntegerConverter.GetBytes(BigInteger.Parse("1212121323543453245345678346345737475734753745737774573475377734577", CultureInfo.InvariantCulture)),
BigIntegerConverter.GetBytes(BigInteger.Parse("3", CultureInfo.InvariantCulture)),
BigIntegerConverter.GetBytes(BigInteger.Parse("4345328123928357434573234217343477", CultureInfo.InvariantCulture)))
.EstimateGas();

Assert.AreEqual(29900, gas);
}


[TestMethod]
public async Task VerifyInt()
{
var gas = await _basicContract.verifyInt(778899).EstimateGas();
Assert.AreEqual(22280, gas);
}

[TestMethod]
public async Task CallAndTransact()
{
var gas1 = await _basicContract.incrementValCounter().EstimateGas();
Assert.AreEqual(42017, gas1);

var gas2 = await _basicContract.getValCounter().EstimateGas();
Assert.AreEqual(22202, gas2);
}

[TestMethod]
public async Task FallbackNonpayable()
{
var gas = await _basicContract.FallbackFunction.EstimateGas();
Assert.AreEqual(21064, gas);
}

[TestMethod]
public async Task AccountAddressEcho()
{
var gas = await _basicContract.echoAddress(Accounts[1]).EstimateGas();
Assert.AreEqual(23269, gas);
}

[TestMethod]
public async Task EmitEvent()
{
var gas = await _basicContract.emitTheEvent().EstimateGas();
Assert.AreEqual(64709, gas);
}
}
}
2 changes: 1 addition & 1 deletion src/Meadow.UnitTestTemplate.ParallelTest/GlobalSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class GlobalSetup
[AssemblyInitialize]
public static async Task Init(TestContext testContext)
{
await Global.Init(testContext);
await Task.CompletedTask;
}

[AssemblyCleanup]
Expand Down
2 changes: 1 addition & 1 deletion src/Meadow.UnitTestTemplate.Test/GlobalSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public static class GlobalSetup
[AssemblyInitialize]
public static async Task Init(TestContext testContext)
{
await Global.Init(testContext);
Global.HideSolidityFromReport("mocks", "IgnoreContract.sol");
await Task.CompletedTask;
}

[AssemblyCleanup]
Expand Down
6 changes: 0 additions & 6 deletions src/Meadow.UnitTestTemplate/ContractTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ public async Task OnTestInitialize()
{
try
{

if (!Global.IsInitialized)
{
throw new Exception("Test harness has not been initialized. Ensure \"Global.Init()\" has been called from a [AssemblyInitialize] method.");
}

// With parallel tests, all code should execute in pairs (main vs. external for every test)
// so we need to ensure sequential execution to avoid issues with other tests running and trying
// to snapshot, execute, restore on the external node, ending up with a race condition.
Expand Down

0 comments on commit e161227

Please sign in to comment.