Skip to content

Commit

Permalink
fibonacci
Browse files Browse the repository at this point in the history
  • Loading branch information
Scooletz committed Jan 16, 2025
1 parent 838bbd3 commit 4e48a4c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/Nethermind/Nethermind.Evm.Test/FibonacciComputeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Diagnostics;
using FluentAssertions;
using Nethermind.Consensus.Tracing;
using Nethermind.Evm.Tracing;
using Nethermind.Evm.Tracing.GethStyle;
using Nethermind.Evm.TransactionProcessing;
using NUnit.Framework;

namespace Nethermind.Evm.Test;

public class FibonacciComputeTests : VirtualMachineTestsBase
{
[Test]
public void Run()
{
byte[] data = [9, 255];

byte[] code = Prepare.EvmCode
.PushData(data)
.COMMENT("1st/2nd fib number")
.PushData(0)
.PushData(1)
.COMMENT("MAINLOOP:")
.JUMPDEST()
.DUPx(3)
.ISZERO()
.PushData(26 + data.Length)
.JUMPI()
.COMMENT("fib step")
.DUPx(2)
.DUPx(2)
.ADD()
.SWAPx(2)
.POP()
.SWAPx(1)
.COMMENT("decrement fib step counter")
.SWAPx(2)
.PushData(1)
.SWAPx(1)
.SUB()
.SWAPx(2)
.PushData(5 + data.Length).COMMENT("goto MAINLOOP")
.JUMP()
.COMMENT("CLEANUP:")
.JUMPDEST()
.SWAPx(2)
.POP()
.POP()
.COMMENT("done: requested fib number is the only element on the stack!")
.STOP()
.Done;

ITxTracer tracer = Debugger.IsAttached
? new GethLikeTxMemoryTracer(GethTraceOptions.Default)
: NullTxTracer.Instance;

TransactionResult result = ExecuteWithTracer(code, tracer);
result.Success.Should().BeTrue();
}
}
7 changes: 7 additions & 0 deletions src/Nethermind/Nethermind.Evm.Test/VirtualMachineTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ protected TestAllTracerWithOutput Execute(params byte[] code)
{
return Execute(Activation, code);
}

protected TransactionResult ExecuteWithTracer(byte[] code, ITxTracer tracer)
{
(Block block, Transaction transaction) = PrepareTx(Activation, 200_000, code);
return _processor.Execute(transaction, block.Header, tracer);
}

protected TestAllTracerWithOutput Execute(Transaction tx)
{
return Execute(Activation, tx);
Expand Down

0 comments on commit 4e48a4c

Please sign in to comment.