private void UpdateLedgerFromDelta(Cid deltaHash) { var stateSnapshot = _stateDb.TakeSnapshot(); if (stateSnapshot != -1) { if (_logger.IsEnabled(LogEventLevel.Error)) { _logger.Error("Uncommitted state ({stateSnapshot}) when processing from a branch root {branchStateRoot} starting with delta {deltaHash}", stateSnapshot, null, deltaHash); } } var snapshotStateRoot = _stateProvider.StateRoot; try { if (!_synchroniser.DeltaCache.TryGetOrAddConfirmedDelta(deltaHash, out var nextDeltaInChain)) { _logger.Warning("Failed to retrieve Delta with hash {hash} from the Dfs, ledger has not been updated.", deltaHash); return; } Cid parentCid = Cid.Read(nextDeltaInChain.PreviousDeltaDfsHash.ToByteArray()); if (!_synchroniser.DeltaCache.TryGetOrAddConfirmedDelta(parentCid, out Delta parentDelta)) { _logger.Warning("Failed to retrieve parent Delta with hash {hash} from the Dfs, ledger has not been updated.", deltaHash); return; } ReceiptDeltaTracer tracer = new ReceiptDeltaTracer(nextDeltaInChain, deltaHash); // add here a receipts tracer or similar, depending on what data needs to be stored for each contract _stateProvider.Reset(); _storageProvider.Reset(); _stateProvider.StateRoot = new Keccak(parentDelta.StateRoot?.ToByteArray()); _deltaExecutor.Execute(nextDeltaInChain, tracer); // store receipts if (tracer.Receipts.Any()) { _receipts.Put(deltaHash, tracer.Receipts.ToArray(), nextDeltaInChain.PublicEntries.ToArray()); } _stateDb.Commit(); _latestKnownDelta = deltaHash; WriteLatestKnownDelta(deltaHash); } catch { Restore(stateSnapshot, snapshotStateRoot); } }
private void UpdateLedgerFromDelta(Cid deltaHash) { var stateSnapshot = _stateDb.TakeSnapshot(); var codeSnapshot = _codeDb.TakeSnapshot(); if (stateSnapshot != -1 || codeSnapshot != -1) { if (_logger.IsEnabled(LogEventLevel.Error)) { _logger.Error("Uncommitted state ({stateSnapshot}, {codeSnapshot}) when processing from a branch root {branchStateRoot} starting with delta {deltaHash}", stateSnapshot, codeSnapshot, null, deltaHash); } } var snapshotStateRoot = _stateProvider.StateRoot; // this code should be brought in / used as a reference if reorganization behaviour is known //// if (branchStateRoot != null && _stateProvider.StateRoot != branchStateRoot) //// { //// /* discarding the other branch data - chain reorganization */ //// Metrics.Reorganizations++; //// _storageProvider.Reset(); //// _stateProvider.Reset(); //// _stateProvider.StateRoot = branchStateRoot; //// } try { if (!_synchroniser.DeltaCache.TryGetOrAddConfirmedDelta(deltaHash, out Delta nextDeltaInChain)) { _logger.Warning( "Failed to retrieve Delta with hash {hash} from the Dfs, ledger has not been updated.", deltaHash); return; } // add here a receipts tracer or similar, depending on what data needs to be stored for each contract _deltaExecutor.Execute(nextDeltaInChain, NullTxTracer.Instance); // this code should be brought in / used as a reference if reorganization behaviour is known //// delta testing here (for delta production) //// if ((options & ProcessingOptions.ReadOnlyChain) != 0) //// { //// Restore(stateSnapshot, codeSnapshot, snapshotStateRoot); //// } //// else //// { //// _stateDb.Commit(); //// _codeDb.Commit(); //// } _stateDb.Commit(); _codeDb.Commit(); LatestKnownDelta = deltaHash; } catch { Restore(stateSnapshot, codeSnapshot, snapshotStateRoot); } }