public void Resets_on_position_reset() { int capacity = Resettable.StartCapacity; int currentPosition = Resettable.StartCapacity; int[] array = new int[Resettable.StartCapacity]; array[0] = 30; Resettable <int> .Reset(ref array, ref capacity, ref currentPosition); Assert.AreEqual(Resettable.StartCapacity, array.Length); Assert.AreEqual(0, array[0]); }
public void Does_not_resize_when_capacity_was_in_use() { int capacity = Resettable.StartCapacity; int currentPosition = Resettable.StartCapacity; int[] array = new int[Resettable.StartCapacity]; Resettable <int> .IncrementPosition(ref array, ref capacity, ref currentPosition); Assert.AreEqual(Resettable.StartCapacity * Resettable.ResetRatio, array.Length); Resettable <int> .Reset(ref array, ref capacity, ref currentPosition, Resettable.StartCapacity); Assert.AreEqual(Resettable.StartCapacity * Resettable.ResetRatio, array.Length); }
public void Can_resize_down() { int capacity = Resettable.StartCapacity; int currentPosition = capacity; int[] array = new int[Resettable.StartCapacity]; Resettable <int> .IncrementPosition(ref array, ref capacity, ref currentPosition); Assert.AreEqual(Resettable.StartCapacity * Resettable.ResetRatio, array.Length); currentPosition -= 2; Resettable <int> .Reset(ref array, ref capacity, ref currentPosition, Resettable.StartCapacity); Assert.AreEqual(Resettable.StartCapacity, array.Length); }
public void Delays_downsizing() { int capacity = Resettable.StartCapacity; int currentPosition = Resettable.StartCapacity * Resettable.ResetRatio; int[] array = new int[Resettable.StartCapacity]; Resettable <int> .IncrementPosition(ref array, ref capacity, ref currentPosition); Assert.AreEqual(Resettable.StartCapacity * Resettable.ResetRatio * Resettable.ResetRatio, array.Length); Resettable <int> .Reset(ref array, ref capacity, ref currentPosition); Assert.AreEqual(Resettable.StartCapacity * Resettable.ResetRatio * Resettable.ResetRatio, array.Length); Resettable <int> .Reset(ref array, ref capacity, ref currentPosition); Assert.AreEqual(Resettable.StartCapacity * Resettable.ResetRatio, array.Length); }
public void Commit(IStorageTracer tracer) { if (_currentPosition == -1) { if (_logger.IsTrace) { _logger.Trace("No storage changes to commit"); } return; } if (_logger.IsTrace) { _logger.Trace("Committing storage changes"); } if (_changes[_currentPosition] == null) { throw new InvalidOperationException($"Change at current position {_currentPosition} was null when commiting {nameof(StorageProvider)}"); } if (_changes[_currentPosition + 1] != null) { throw new InvalidOperationException($"Change after current position ({_currentPosition} + 1) was not null when commiting {nameof(StorageProvider)}"); } HashSet <Address> toUpdateRoots = new HashSet <Address>(); bool isTracing = tracer != null; Dictionary <StorageCell, ChangeTrace> trace = null; if (isTracing) { trace = new Dictionary <StorageCell, ChangeTrace>(); } for (int i = 0; i <= _currentPosition; i++) { Change change = _changes[_currentPosition - i]; if (!isTracing && change.ChangeType == ChangeType.JustCache) { continue; } if (_committedThisRound.Contains(change.StorageCell)) { if (isTracing && change.ChangeType == ChangeType.JustCache) { trace[change.StorageCell] = new ChangeTrace(change.Value, trace[change.StorageCell].After); } continue; } if (isTracing && change.ChangeType == ChangeType.JustCache) { tracer.ReportStorageRead(change.StorageCell); } _committedThisRound.Add(change.StorageCell); if (change.ChangeType == ChangeType.Destroy) { continue; } int forAssertion = _intraBlockCache[change.StorageCell].Pop(); if (forAssertion != _currentPosition - i) { throw new InvalidOperationException($"Expected checked value {forAssertion} to be equal to {_currentPosition} - {i}"); } switch (change.ChangeType) { case ChangeType.Destroy: break; case ChangeType.JustCache: break; case ChangeType.Update: if (_logger.IsTrace) { _logger.Trace($" Update {change.StorageCell.Address}_{change.StorageCell.Index} V = {change.Value.ToHexString(true)}"); } StorageTree tree = GetOrCreateStorage(change.StorageCell.Address); Metrics.StorageTreeWrites++; toUpdateRoots.Add(change.StorageCell.Address); tree.Set(change.StorageCell.Index, change.Value); if (isTracing) { trace[change.StorageCell] = new ChangeTrace(change.Value); } break; default: throw new ArgumentOutOfRangeException(); } } foreach (Address address in toUpdateRoots) { // since the accounts could be empty accounts that are removing (EIP-158) if (_stateProvider.AccountExists(address)) { Keccak root = RecalculateRootHash(address); _stateProvider.UpdateStorageRoot(address, root); } } Resettable <Change> .Reset(ref _changes, ref _capacity, ref _currentPosition, StartCapacity); _committedThisRound.Reset(); _intraBlockCache.Reset(); _originalValues.Reset(); if (isTracing) { ReportChanges(tracer, trace); } }