public void UpdateLocalStateRoot(uint height, List <DataCache.Trackable> change_set) { using StateSnapshot state_snapshot = Singleton.GetSnapshot(); foreach (var item in change_set) { switch (item.State) { case TrackState.Added: state_snapshot.Trie.Put(item.Key, item.Item); break; case TrackState.Changed: state_snapshot.Trie.Put(item.Key, item.Item); break; case TrackState.Deleted: state_snapshot.Trie.Delete(item.Key); break; } } UInt256 root_hash = state_snapshot.Trie.Root.Hash; StateRoot state_root = new StateRoot { Index = height, RootHash = root_hash, Witness = null, }; state_snapshot.AddLocalStateRoot(state_root); state_snapshot.Commit(); UpdateCurrentSnapshot(); CheckValidatedStateRoot(height); }
public void UpdateLocalStateRootSnapshot(uint height, List <DataCache.Trackable> change_set) { _state_snapshot = Singleton.GetSnapshot(); foreach (var item in change_set) { switch (item.State) { case TrackState.Added: _state_snapshot.Trie.Put(item.Key, item.Item); break; case TrackState.Changed: _state_snapshot.Trie.Put(item.Key, item.Item); break; case TrackState.Deleted: _state_snapshot.Trie.Delete(item.Key); break; } } UInt256 root_hash = _state_snapshot.Trie.Root.Hash; StateRoot state_root = new StateRoot { Version = StateRoot.CurrentVersion, Index = height, RootHash = root_hash, Witness = null, }; _state_snapshot.AddLocalStateRoot(state_root); }
public void AddValidatedStateRoot(StateRoot state_root) { if (state_root?.Witness is null) { throw new ArgumentException(nameof(state_root) + " missing witness in invalidated state root"); } snapshot.Put(Keys.StateRoot(state_root.Index), state_root.ToArray()); snapshot.Put(Keys.CurrentValidatedRootIndex, BitConverter.GetBytes(state_root.Index)); }
private void OnStatePayload(ExtensiblePayload payload) { StateRoot state_root = null; try { state_root = payload.Data?.AsSerializable <StateRoot>(); } catch (Exception ex) { Utility.Log(nameof(StateStore), LogLevel.Warning, " invalid state root " + ex.Message); return; } if (state_root != null) { OnNewStateRoot(state_root); } }
private bool OnNewStateRoot(StateRoot state_root) { if (state_root?.Witness is null) { return(false); } if (ValidatedRootIndex != null && state_root.Index <= ValidatedRootIndex) { return(false); } if (LocalRootIndex is null) { throw new InvalidOperationException(nameof(StateStore) + " could not get local root index"); } if (LocalRootIndex < state_root.Index && state_root.Index < LocalRootIndex + MaxCacheCount) { cache.Add(state_root.Index, state_root); return(true); } using var state_snapshot = Singleton.GetSnapshot(); StateRoot local_root = state_snapshot.GetStateRoot(state_root.Index); if (local_root is null || local_root.Witness != null) { return(false); } if (!state_root.Verify(StatePlugin.System.Settings, StatePlugin.System.StoreView)) { return(false); } if (local_root.RootHash != state_root.RootHash) { return(false); } state_snapshot.AddValidatedStateRoot(state_root); state_snapshot.Commit(); UpdateCurrentSnapshot(); system.Verifier?.Tell(new VerificationService.ValidatedRootPersisted { Index = state_root.Index }); return(true); }
private bool OnNewStateRoot(StateRoot state_root) { if (state_root?.Witness is null) { return(false); } if (state_root.Index <= ValidatedRootIndex) { return(false); } if (LocalRootIndex < state_root.Index && state_root.Index < LocalRootIndex + MaxCacheCount) { cache.Add(state_root.Index, state_root); return(true); } using var state_snapshot = Singleton.GetSnapshot(); StateRoot local_root = state_snapshot.GetStateRoot(state_root.Index); if (local_root is null || local_root.Witness != null) { return(false); } using var snapshot = Blockchain.Singleton.GetSnapshot(); if (!state_root.Verify(snapshot)) { return(false); } if (local_root.RootHash != state_root.RootHash) { return(false); } state_snapshot.AddValidatedStateRoot(state_root); state_snapshot.Commit(); UpdateCurrentSnapshot(); //Tell validation service return(true); }
public void AddLocalStateRoot(StateRoot state_root) { snapshot.Put(Keys.StateRoot(state_root.Index), state_root.ToArray()); snapshot.Put(Keys.CurrentLocalRootIndex, BitConverter.GetBytes(state_root.Index)); }