Skip to content

Commit d802e39

Browse files
committed
StateService: allow to enable StateService plugin from arbitraty height
Do not require StateService plugin to be enabled from genesis block. Node can build MPT based on the current contract storage state from scratch at the arbitrary height. This feature is required to avoid chain resync from genesis for those nodes who don't have StateService plugin enabled. Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
1 parent e0498f6 commit d802e39

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/Plugins/StateService/StatePlugin.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,25 @@ void ICommittingHandler.Blockchain_Committing_Handler(NeoSystem system, Block bl
102102
IReadOnlyList<ApplicationExecuted> applicationExecutedList)
103103
{
104104
if (system.Settings.Network != StateServiceSettings.Default.Network) return;
105+
106+
// Rebuild MPT from scratch on node bootstrap if StateService plugin is not up-to-date wrt the current block height.
107+
if (block.Index > 0)
108+
{
109+
using var stateSnapshot = StateStore.Singleton.GetSnapshot();
110+
var persistedHeight = block.Index - 1;
111+
var stateRoot = stateSnapshot.GetStateRoot(persistedHeight);
112+
if (stateRoot is null)
113+
{
114+
using var coreSnapshot = system.GetSnapshotCache();
115+
StateStore.Singleton.UpdateLocalStateRootSnapshot(persistedHeight,
116+
coreSnapshot.Find(SeekDirection.Forward)
117+
.Where(si => si.Key.Id != NativeContract.Ledger.Id)
118+
.Select(si => new KeyValuePair<StorageKey, DataCache.Trackable>(si.Key, new DataCache.Trackable(si.Value, TrackState.Added)))
119+
.ToList());
120+
StateStore.Singleton.UpdateLocalStateRoot(persistedHeight);
121+
}
122+
}
123+
105124
var root = StateStore.Singleton.UpdateLocalStateRootSnapshot(block.Index,
106125
snapshot.GetChangeSet()
107126
.Where(p => p.Value.State != TrackState.None && p.Key.Id != NativeContract.Ledger.Id)

0 commit comments

Comments
 (0)