コード例 #1
0
        public void GlobalSetup()
        {
            ByteCode = Bytes.FromHexString(Environment.GetEnvironmentVariable("NETH.BENCHMARK.BYTECODE"));
            Console.WriteLine($"Running benchmark for bytecode {ByteCode?.ToHexString()}");
            IDb             codeDb  = new StateDb();
            ISnapshotableDb stateDb = new StateDb();

            _stateProvider = new StateProvider(stateDb, codeDb, LimboLogs.Instance);
            _stateProvider.CreateAccount(Address.Zero, 1000.Ether());
            _stateProvider.Commit(_spec);

            _storageProvider = new StorageProvider(stateDb, _stateProvider, LimboLogs.Instance);
            _virtualMachine  = new VirtualMachine(_stateProvider, _storageProvider, _blockhashProvider, MainnetSpecProvider.Instance, LimboLogs.Instance);

            _environment = new ExecutionEnvironment();
            _environment.ExecutingAccount = Address.Zero;
            _environment.CodeSource       = Address.Zero;
            _environment.Originator       = Address.Zero;
            _environment.Sender           = Address.Zero;
            _environment.CodeInfo         = new CodeInfo(ByteCode);
            _environment.GasPrice         = 0;
            _environment.Value            = 0;
            _environment.TransferValue    = 0;
            _environment.CurrentBlock     = _header;

            _evmState = new EvmState(long.MaxValue, _environment, ExecutionType.Transaction, false, true, false);
        }
コード例 #2
0
        public void GlobalSetup()
        {
            TrieStore      trieStore = new(new MemDb(), new OneLoggerLogManager(NullLogger.Instance));
            IKeyValueStore codeDb    = new MemDb();

            _stateProvider = new StateProvider(trieStore, codeDb, new OneLoggerLogManager(NullLogger.Instance));
            _stateProvider.CreateAccount(Address.Zero, 1000.Ether());
            _stateProvider.Commit(_spec);

            _storageProvider = new StorageProvider(trieStore, _stateProvider, new OneLoggerLogManager(NullLogger.Instance));

            _worldState = new WorldState(_stateProvider, _storageProvider);
            Console.WriteLine(MuirGlacier.Instance);
            _virtualMachine = new VirtualMachine(_blockhashProvider, MainnetSpecProvider.Instance, new OneLoggerLogManager(NullLogger.Instance));

            _environment = new ExecutionEnvironment
            {
                ExecutingAccount   = Address.Zero,
                CodeSource         = Address.Zero,
                Caller             = Address.Zero,
                CodeInfo           = new CodeInfo(Bytecode),
                Value              = 0,
                TransferValue      = 0,
                TxExecutionContext = new TxExecutionContext(_header, Address.Zero, 0)
            };

            _evmState = new EvmState(100_000_000L, _environment, ExecutionType.Transaction, true, _worldState.TakeSnapshot(), false);
        }
コード例 #3
0
        public void Can_dispose_after_init()
        {
            EvmState evmState = CreateEvmState();

            evmState.InitStacks();
            evmState.Dispose();
        }
コード例 #4
0
ファイル: EvmStateTests.cs プロジェクト: uzbekdev1/nethermind
        public void Nothing_to_commit()
        {
            EvmState parentEvmState = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);
            EvmState evmState       = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);

            evmState.CommitToParent(parentEvmState);
        }
コード例 #5
0
        public void GlobalSetup()
        {
            ByteCode = Bytes.FromHexString(Environment.GetEnvironmentVariable("NETH.BENCHMARK.BYTECODE") ?? string.Empty);
            Console.WriteLine($"Running benchmark for bytecode {ByteCode?.ToHexString()}");

            TrieStore      trieStore = new(new MemDb(), new OneLoggerLogManager(NullLogger.Instance));
            IKeyValueStore codeDb    = new MemDb();

            _stateProvider = new StateProvider(trieStore, codeDb, new OneLoggerLogManager(NullLogger.Instance));
            _stateProvider.CreateAccount(Address.Zero, 1000.Ether());
            _stateProvider.Commit(_spec);

            _storageProvider = new StorageProvider(trieStore, _stateProvider, new OneLoggerLogManager(NullLogger.Instance));

            _worldState = new WorldState(_stateProvider, _storageProvider);

            _virtualMachine = new VirtualMachine(_blockhashProvider, MainnetSpecProvider.Instance, LimboLogs.Instance);

            _environment = new ExecutionEnvironment
            {
                ExecutingAccount   = Address.Zero,
                CodeSource         = Address.Zero,
                Caller             = Address.Zero,
                CodeInfo           = new CodeInfo(ByteCode),
                Value              = 0,
                TransferValue      = 0,
                TxExecutionContext = new TxExecutionContext(_header, Address.Zero, 0)
            };

            _evmState = new EvmState(long.MaxValue, _environment, ExecutionType.Transaction, true, _worldState.TakeSnapshot(), false);
        }
コード例 #6
0
ファイル: EvmStateTests.cs プロジェクト: uzbekdev1/nethermind
        public void Can_dispose_after_init()
        {
            EvmState evmState = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);

            evmState.InitStacks();
            evmState.Dispose();
        }
コード例 #7
0
ファイル: EvmStateTests.cs プロジェクト: uzbekdev1/nethermind
        public void Things_are_cold_to_start_with()
        {
            EvmState    evmState    = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);
            StorageCell storageCell = new StorageCell(TestItem.AddressA, 1);

            evmState.IsCold(TestItem.AddressA).Should().BeTrue();
            evmState.IsCold(storageCell).Should().BeTrue();
        }
コード例 #8
0
ファイル: EvmStateTests.cs プロジェクト: uzbekdev1/nethermind
        public void Commit_adds_refunds()
        {
            EvmState parentEvmState = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);
            EvmState evmState       = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);

            evmState.CommitToParent(parentEvmState);
            parentEvmState.Refund.Should().Be(evmState.Refund);
        }
コード例 #9
0
        public void Things_are_cold_to_start_with()
        {
            EvmState    evmState    = CreateEvmState();
            StorageCell storageCell = new(TestItem.AddressA, 1);

            evmState.IsCold(TestItem.AddressA).Should().BeTrue();
            evmState.IsCold(storageCell).Should().BeTrue();
        }
コード例 #10
0
        public void Nothing_to_commit()
        {
            EvmState parentEvmState = CreateEvmState();

            using (EvmState evmState = CreateEvmState(parentEvmState))
            {
                evmState.CommitToParent(parentEvmState);
            }
        }
コード例 #11
0
        public void Can_warm_address_up_twice()
        {
            EvmState evmState = CreateEvmState();
            Address  address  = TestItem.AddressA;

            evmState.WarmUp(address);
            evmState.WarmUp(address);
            evmState.IsCold(address).Should().BeFalse();
        }
コード例 #12
0
ファイル: EvmStateTests.cs プロジェクト: uzbekdev1/nethermind
        public void Can_warm_address_up_twice()
        {
            EvmState evmState = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);
            Address  address  = TestItem.AddressA;

            evmState.WarmUp(address);
            evmState.WarmUp(address);
            evmState.IsCold(address).Should().BeFalse();
        }
コード例 #13
0
ファイル: EvmStateTests.cs プロジェクト: uzbekdev1/nethermind
        public void Address_to_commit_keeps_it_warm()
        {
            EvmState parentEvmState = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);
            EvmState evmState       = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);

            evmState.WarmUp(TestItem.AddressA);

            evmState.CommitToParent(parentEvmState);
            parentEvmState.IsCold(TestItem.AddressA).Should().BeFalse();
        }
コード例 #14
0
ファイル: EvmStateTests.cs プロジェクト: uzbekdev1/nethermind
        public void Can_warm_storage_up_twice()
        {
            EvmState    evmState    = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);
            Address     address     = TestItem.AddressA;
            StorageCell storageCell = new StorageCell(address, 1);

            evmState.WarmUp(storageCell);
            evmState.WarmUp(storageCell);
            evmState.IsCold(storageCell).Should().BeFalse();
        }
コード例 #15
0
ファイル: EvmStateTests.cs プロジェクト: uzbekdev1/nethermind
        public void Destroy_list_is_committed()
        {
            EvmState parentEvmState = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);
            EvmState evmState       = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);

            evmState.DestroyList.Add(Address.Zero);

            evmState.CommitToParent(parentEvmState);
            parentEvmState.DestroyList.Contains(Address.Zero).Should().BeTrue();
        }
コード例 #16
0
        public void Can_warm_storage_up_twice()
        {
            EvmState    evmState    = CreateEvmState();
            Address     address     = TestItem.AddressA;
            StorageCell storageCell = new(address, 1);

            evmState.WarmUp(storageCell);
            evmState.WarmUp(storageCell);
            evmState.IsCold(storageCell).Should().BeFalse();
        }
コード例 #17
0
        public void Address_to_restore_keeps_it_cold()
        {
            EvmState parentEvmState = CreateEvmState();

            using (EvmState evmState = CreateEvmState(parentEvmState))
            {
                evmState.WarmUp(TestItem.AddressA);
            }

            parentEvmState.IsCold(TestItem.AddressA).Should().BeTrue();
        }
コード例 #18
0
        public void Restore_doesnt_add_refunds()
        {
            EvmState parentEvmState = CreateEvmState();

            using (EvmState evmState = CreateEvmState(parentEvmState))
            {
                evmState.Refund = 333;
            }

            parentEvmState.Refund.Should().Be(0);
        }
コード例 #19
0
        public void Destroy_list_is_restored()
        {
            EvmState parentEvmState = CreateEvmState();

            using (EvmState evmState = CreateEvmState(parentEvmState))
            {
                evmState.DestroyList.Add(Address.Zero);
            }

            parentEvmState.DestroyList.Contains(Address.Zero).Should().BeFalse();
        }
コード例 #20
0
ファイル: EvmStateTests.cs プロジェクト: uzbekdev1/nethermind
        public void Logs_are_committed()
        {
            EvmState parentEvmState = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);
            EvmState evmState       = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);
            LogEntry logEntry       = new LogEntry(Address.Zero, Bytes.Empty, Array.Empty <Keccak>());

            evmState.Logs.Add(logEntry);

            evmState.CommitToParent(parentEvmState);
            parentEvmState.Logs.Contains(logEntry).Should().BeTrue();
        }
コード例 #21
0
        public void Address_to_commit_keeps_it_warm()
        {
            EvmState parentEvmState = CreateEvmState();

            using (EvmState evmState = CreateEvmState(parentEvmState))
            {
                evmState.WarmUp(TestItem.AddressA);
                evmState.CommitToParent(parentEvmState);
            }

            parentEvmState.IsCold(TestItem.AddressA).Should().BeFalse();
        }
コード例 #22
0
        public void Commit_adds_refunds()
        {
            EvmState parentEvmState = CreateEvmState();

            using (EvmState evmState = CreateEvmState(parentEvmState))
            {
                evmState.Refund = 333;
                evmState.CommitToParent(parentEvmState);
            }

            parentEvmState.Refund.Should().Be(333);
        }
コード例 #23
0
        public void Storage_to_restore_keeps_it_cold()
        {
            EvmState    parentEvmState = CreateEvmState();
            StorageCell storageCell    = new(TestItem.AddressA, 1);

            using (EvmState evmState = CreateEvmState(parentEvmState))
            {
                evmState.WarmUp(storageCell);
            }

            parentEvmState.IsCold(storageCell).Should().BeTrue();
        }
コード例 #24
0
        public void Logs_are_restored()
        {
            EvmState parentEvmState = CreateEvmState();
            LogEntry logEntry       = new(Address.Zero, Bytes.Empty, Array.Empty <Keccak>());

            using (EvmState evmState = CreateEvmState(parentEvmState))
            {
                evmState.Logs.Add(logEntry);
            }

            parentEvmState.Logs.Contains(logEntry).Should().BeFalse();
        }
コード例 #25
0
        public void Destroy_list_is_committed()
        {
            EvmState parentEvmState = CreateEvmState();

            using (EvmState evmState = CreateEvmState(parentEvmState))
            {
                evmState.DestroyList.Add(Address.Zero);
                evmState.CommitToParent(parentEvmState);
            }

            parentEvmState.DestroyList.Contains(Address.Zero).Should().BeTrue();
        }
コード例 #26
0
        public void Storage_to_commit_keeps_it_warm()
        {
            EvmState    parentEvmState = CreateEvmState();
            StorageCell storageCell    = new(TestItem.AddressA, 1);

            using (EvmState evmState = CreateEvmState(parentEvmState))
            {
                evmState.WarmUp(storageCell);
                evmState.CommitToParent(parentEvmState);
            }

            parentEvmState.IsCold(storageCell).Should().BeFalse();
        }
コード例 #27
0
ファイル: EvmStateTests.cs プロジェクト: uzbekdev1/nethermind
        public void Can_warm_up_many()
        {
            EvmState evmState = new EvmState(10000, new ExecutionEnvironment(), ExecutionType.Call, true, false);

            for (int i = 0; i < TestItem.Addresses.Length; i++)
            {
                evmState.WarmUp(TestItem.Addresses[i]);
                evmState.WarmUp(new StorageCell(TestItem.Addresses[i], 1));
            }

            for (int i = 0; i < TestItem.Addresses.Length; i++)
            {
                evmState.IsCold(TestItem.Addresses[i]).Should().BeFalse();
                evmState.IsCold(new StorageCell(TestItem.Addresses[i], 1)).Should().BeFalse();
            }
        }
コード例 #28
0
        public void Can_warm_up_many()
        {
            EvmState evmState = CreateEvmState();

            for (int i = 0; i < TestItem.Addresses.Length; i++)
            {
                evmState.WarmUp(TestItem.Addresses[i]);
                evmState.WarmUp(new StorageCell(TestItem.Addresses[i], 1));
            }

            for (int i = 0; i < TestItem.Addresses.Length; i++)
            {
                evmState.IsCold(TestItem.Addresses[i]).Should().BeFalse();
                evmState.IsCold(new StorageCell(TestItem.Addresses[i], 1)).Should().BeFalse();
            }
        }
コード例 #29
0
 private static EvmState CreateEvmState(EvmState parentEvmState = null, bool isContinuation = false) =>
 parentEvmState is null
         ? new EvmState(10000,
                        new ExecutionEnvironment(),
                        ExecutionType.Call,
                        true,
                        Snapshot.Empty,
                        isContinuation)
         : new EvmState(10000,
                        new ExecutionEnvironment(),
                        ExecutionType.Call,
                        false,
                        Snapshot.Empty,
                        0,
                        0,
                        false,
                        parentEvmState,
                        isContinuation,
                        false);
コード例 #30
0
        protected void RunTest(VirtualMachineTest test)
        {
            TestContext.WriteLine($"Running {test.GetType().FullName}");

            VirtualMachine       machine     = new VirtualMachine(_stateProvider, _storageProvider, _blockhashProvider, _specProvider, _logManager);
            ExecutionEnvironment environment = new ExecutionEnvironment();

            environment.Value            = test.Execution.Value;
            environment.CallDepth        = 0;
            environment.Sender           = test.Execution.Caller;
            environment.ExecutingAccount = test.Execution.Address;


            BlockHeader header = new BlockHeader(
                Keccak.OfAnEmptyString,
                Keccak.OfAnEmptySequenceRlp,
                test.Environment.CurrentCoinbase,
                test.Environment.CurrentDifficulty,
                test.Environment.CurrentNumber,
                (long)test.Environment.CurrentGasLimit,
                test.Environment.CurrentTimestamp, Bytes.Empty);

            environment.CurrentBlock = header;

            environment.GasPrice   = test.Execution.GasPrice;
            environment.InputData  = test.Execution.Data;
            environment.CodeInfo   = new CodeInfo(test.Execution.Code);
            environment.Originator = test.Execution.Origin;

            foreach (KeyValuePair <Address, AccountState> accountState in test.Pre)
            {
                foreach (KeyValuePair <UInt256, byte[]> storageItem in accountState.Value.Storage)
                {
                    _storageProvider.Set(new StorageCell(accountState.Key, storageItem.Key), storageItem.Value);
                    if (accountState.Key.Equals(test.Execution.Address))
                    {
                        _storageProvider.Set(new StorageCell(accountState.Key, storageItem.Key), storageItem.Value);
                    }
                }

                _stateProvider.UpdateCode(accountState.Value.Code);

                _stateProvider.CreateAccount(accountState.Key, accountState.Value.Balance);
                Keccak codeHash = _stateProvider.UpdateCode(accountState.Value.Code);
                _stateProvider.UpdateCodeHash(accountState.Key, codeHash, Olympic.Instance);
                for (int i = 0; i < accountState.Value.Nonce; i++)
                {
                    _stateProvider.IncrementNonce(accountState.Key);
                }
            }

            EvmState state = new EvmState((long)test.Execution.Gas, environment, ExecutionType.Transaction, true, false);

            _storageProvider.Commit();
            _stateProvider.Commit(Olympic.Instance);

            TransactionSubstate substate = machine.Run(state, NullTxTracer.Instance);

            if (test.Out == null)
            {
                Assert.NotNull(substate.Error);
                return;
            }

            Assert.True(Bytes.AreEqual(test.Out, substate.Output),
                        $"Exp: {test.Out.ToHexString(true)} != Actual: {substate.Output.ToHexString(true)}");
            Assert.AreEqual((long)test.Gas, state.GasAvailable, "gas available");
            foreach (KeyValuePair <Address, AccountState> accountState in test.Post)
            {
                bool    accountExists = _stateProvider.AccountExists(accountState.Key);
                UInt256 balance       = accountExists ? _stateProvider.GetBalance(accountState.Key) : 0;
                UInt256 nonce         = accountExists ? _stateProvider.GetNonce(accountState.Key) : 0;
                Assert.AreEqual(accountState.Value.Balance, balance, $"{accountState.Key} Balance");
                Assert.AreEqual(accountState.Value.Nonce, nonce, $"{accountState.Key} Nonce");

                // TODO: not testing properly 0 balance accounts
                if (accountExists)
                {
                    byte[] code = _stateProvider.GetCode(accountState.Key);
                    Assert.AreEqual(accountState.Value.Code, code, $"{accountState.Key} Code");
                }

                foreach (KeyValuePair <UInt256, byte[]> storageItem in accountState.Value.Storage)
                {
                    byte[] value = _storageProvider.Get(new StorageCell(accountState.Key, storageItem.Key));
                    Assert.True(Bytes.AreEqual(storageItem.Value, value),
                                $"Storage[{accountState.Key}_{storageItem.Key}] Exp: {storageItem.Value.ToHexString(true)} != Actual: {value.ToHexString(true)}");
                }
            }
        }