-
Notifications
You must be signed in to change notification settings - Fork 380
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
IL Virtual Machine #3888
Draft
Scooletz
wants to merge
1
commit into
master
Choose a base branch
from
il-vm
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
IL Virtual Machine #3888
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
237 changes: 237 additions & 0 deletions
237
src/Nethermind/Nethermind.Evm.Test/IlVirtualMachineTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
// Copyright (c) 2021 Demerzel Solutions Limited | ||
// This file is part of the Nethermind library. | ||
// | ||
// The Nethermind library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The Nethermind library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>. | ||
// | ||
|
||
using System; | ||
using System.Buffers.Binary; | ||
using System.Diagnostics; | ||
using Nethermind.Core.Test; | ||
using Nethermind.Logging; | ||
using NUnit.Framework; | ||
|
||
namespace Nethermind.Evm.Test; | ||
|
||
public class IlVirtualMachineTests : VirtualMachineTestsBase | ||
{ | ||
protected override ILogManager GetLogManager() => new NUnitLogManager(); | ||
|
||
[Test] | ||
public void PC_POP() | ||
{ | ||
Machine.BuildILForNext(); | ||
|
||
byte[] code = Prepare.EvmCode | ||
.Op(Instruction.PC) | ||
.Op(Instruction.POP) | ||
.Done; | ||
|
||
TestAllTracerWithOutput result = Execute(code); | ||
|
||
Console.WriteLine(result.Error); | ||
} | ||
|
||
[Test] | ||
public void Jump_Invalid() | ||
{ | ||
Machine.BuildILForNext(); | ||
|
||
byte[] code = Prepare.EvmCode | ||
.PushData(1) | ||
.Op(Instruction.JUMP) | ||
.Done; | ||
|
||
TestAllTracerWithOutput result = Execute(code); | ||
|
||
Assert.AreEqual(result.Error, EvmExceptionType.InvalidJumpDestination.ToString()); | ||
} | ||
|
||
[Test] | ||
public void Jump_Valid() | ||
{ | ||
Machine.BuildILForNext(); | ||
|
||
byte[] code = Prepare.EvmCode | ||
.PushData(3) | ||
.Op(Instruction.JUMP) | ||
.Op(Instruction.JUMPDEST) | ||
.Done; | ||
|
||
TestAllTracerWithOutput result = Execute(code); | ||
|
||
Console.WriteLine(result.Error); | ||
} | ||
|
||
[Test] | ||
public void Jumpi_InvalidCondition() | ||
{ | ||
Machine.BuildILForNext(); | ||
|
||
byte[] code = Prepare.EvmCode | ||
.PushData(0) // invalid condition | ||
.PushData(1) // address | ||
.Op(Instruction.JUMPI) | ||
.Done; | ||
|
||
TestAllTracerWithOutput result = Execute(code); | ||
|
||
Console.WriteLine(result.Error); | ||
} | ||
|
||
[Test] | ||
public void Jumpi_ValidCondition_InvalidAddress() | ||
{ | ||
Machine.BuildILForNext(); | ||
|
||
byte[] code = Prepare.EvmCode | ||
.PushData(1) // valid condition | ||
.PushData(1) // address | ||
.Op(Instruction.JUMPI) | ||
.Done; | ||
|
||
TestAllTracerWithOutput result = Execute(code); | ||
|
||
Assert.AreEqual(result.Error, EvmExceptionType.InvalidJumpDestination.ToString()); | ||
} | ||
|
||
[Test] | ||
public void Jumpi_ValidCondition_ValidAddress() | ||
{ | ||
Machine.BuildILForNext(); | ||
|
||
byte[] code = Prepare.EvmCode | ||
.PushData(1) // valid condition | ||
.PushData(5) // address | ||
.Op(Instruction.JUMPI) | ||
.Op(Instruction.JUMPDEST) | ||
.Done; | ||
|
||
TestAllTracerWithOutput result = Execute(code); | ||
|
||
Console.WriteLine(result.Error); | ||
} | ||
|
||
[Test] | ||
public void Sub() | ||
{ | ||
Machine.BuildILForNext(); | ||
|
||
byte[] code = Prepare.EvmCode | ||
.PushData(1) | ||
.PushData(2) | ||
.PushData(1) | ||
.PushData(4) | ||
.Op(Instruction.SUB) // 4 - 1 = 3 | ||
.Op(Instruction.SUB) // 3 - 2 = 1 | ||
.Op(Instruction.SUB) // 1 - 1 = 0 | ||
.Done; | ||
|
||
TestAllTracerWithOutput result = Execute(code); | ||
|
||
Console.WriteLine(result.Error); | ||
} | ||
|
||
[Test] | ||
public void Dup() | ||
{ | ||
Machine.BuildILForNext(); | ||
|
||
byte[] code = Prepare.EvmCode | ||
.PushData(1) | ||
.Op(Instruction.DUP1) | ||
.Op(Instruction.SUB) | ||
.Op(Instruction.POP) | ||
.Done; | ||
|
||
TestAllTracerWithOutput result = Execute(code); | ||
|
||
Console.WriteLine(result.Error); | ||
} | ||
|
||
[Test] | ||
public void Swap() | ||
{ | ||
Machine.BuildILForNext(); | ||
|
||
byte[] code = Prepare.EvmCode | ||
.PushData(2) | ||
.PushData(1) | ||
.Op(Instruction.SWAP1) | ||
.Op(Instruction.SUB) | ||
.Done; | ||
|
||
TestAllTracerWithOutput result = Execute(code); | ||
|
||
Console.WriteLine(result.Error); | ||
} | ||
|
||
//[Test] | ||
//public void OutOfGas() | ||
//{ | ||
// // an infinite loop | ||
// byte[] code = Prepare.EvmCode | ||
// .Op(Instruction.JUMPDEST) | ||
// .PushData(0) | ||
// .Op(Instruction.JUMP) | ||
// .Done; | ||
|
||
// TestAllTracerWithOutput result = Execute(BlockNumber, 10000, code); | ||
|
||
// Assert.AreEqual(result.Error, EvmExceptionType.OutOfGas.ToString()); | ||
//} | ||
|
||
[TestCase(true, TestName = "IL")] | ||
[TestCase(false, TestName = "Interpreter")] | ||
[Explicit] | ||
public void Long_Loop(bool isIL) | ||
{ | ||
if (isIL) | ||
{ | ||
Machine.BuildILForNext(); | ||
} | ||
|
||
const int loopCount = 200_000; | ||
byte[] repeat = new byte[4]; | ||
BinaryPrimitives.TryWriteInt32BigEndian(repeat, loopCount); | ||
|
||
Prepare code = Prepare.EvmCode | ||
.PushData(repeat) | ||
.Op(Instruction.JUMPDEST) // counter | ||
.PushData(1) // counter, 1 | ||
.Op(Instruction.SWAP1) // 1, counter | ||
.Op(Instruction.SUB) // counter-1 | ||
.Op(Instruction.DUP1) // counter-1, counter-1 | ||
.PushData(1 + repeat.Length) // counter-1, counter-1, 2 | ||
.Op(Instruction.JUMPI); // counter-1 | ||
|
||
if (isIL) | ||
{ | ||
// differentiate by adding one point | ||
code = code.Op(Instruction.POP); | ||
} | ||
|
||
// warmup | ||
Execute(BlockNumber, 10_000, code.Done); | ||
|
||
Stopwatch sw = Stopwatch.StartNew(); | ||
|
||
TestAllTracerWithOutput result = Execute(BlockNumber, 7_000_000, code.Done); | ||
|
||
const int n = 1_000; | ||
|
||
Console.WriteLine($"Execution of {loopCount} took {sw.Elapsed} taking {((double)sw.ElapsedMilliseconds * n) / loopCount}ms per {n} spins"); | ||
Console.WriteLine(result.Error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this difference needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember.