Skip to content

Commit

Permalink
add RootHashIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
KickSeason committed May 22, 2020
1 parent 579439c commit 1dbfb93
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 14 deletions.
8 changes: 4 additions & 4 deletions neo/Ledger/BlockChain.State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Neo.Ledger
public sealed partial class Blockchain : UntypedActor
{
public class ImportRoots { public IEnumerable<StateRoot> Roots; }
public long StateHeight => currentSnapshot.StateHeight == uint.MaxValue ? -1 : (long)currentSnapshot.StateHeight;
public long StateHeight => currentSnapshot.StateHeight;
private static uint StateRootEnableIndex => ProtocolSettings.Default.StateRootEnableIndex;
private readonly Dictionary<uint, StateRoot> stateRootCache = new Dictionary<uint, StateRoot>();

Expand Down Expand Up @@ -103,9 +103,9 @@ private StateRootVerifyFlag PersistCnStateRoot(StateRoot stateRoot)
var localState = snapshot.StateRoots.GetAndChange(stateRoot.Index);
if (localState.StateRoot.Root == stateRoot.Root && localState.StateRoot.PreHash == stateRoot.PreHash)
{
HashIndexState hashIndexState = snapshot.StateRootHashIndex.GetAndChange();
hashIndexState.Index = stateRoot.Index;
hashIndexState.Hash = stateRoot.Hash;
RootHashIndex rootHashIndex = snapshot.StateRootHashIndex.GetAndChange();
rootHashIndex.Index = stateRoot.Index;
rootHashIndex.Hash = stateRoot.Hash;
localState.StateRoot = stateRoot;
localState.Flag = StateRootVerifyFlag.Verified;
}
Expand Down
51 changes: 51 additions & 0 deletions neo/Ledger/RootHashIndex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Neo.IO;
using Neo.IO.Json;
using System.IO;

namespace Neo.Ledger
{
public class RootHashIndex : StateBase, ICloneable<RootHashIndex>
{
public UInt256 Hash = UInt256.Zero;
public long Index = -1;

public override int Size => base.Size + Hash.Size + sizeof(long);

RootHashIndex ICloneable<RootHashIndex>.Clone()
{
return new RootHashIndex
{
Hash = Hash,
Index = Index
};
}

public override void Deserialize(BinaryReader reader)
{
base.Deserialize(reader);
Hash = reader.ReadSerializable<UInt256>();
Index = reader.ReadInt64();
}

void ICloneable<RootHashIndex>.FromReplica(RootHashIndex replica)
{
Hash = replica.Hash;
Index = replica.Index;
}

public override void Serialize(BinaryWriter writer)
{
base.Serialize(writer);
writer.Write(Hash);
writer.Write(Index);
}

public override JObject ToJson()
{
JObject json = base.ToJson();
json["hash"] = Hash.ToString();
json["index"] = Index;
return json;
}
}
}
2 changes: 1 addition & 1 deletion neo/Persistence/CloneSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class CloneSnapshot : Snapshot
public override MetaDataCache<ValidatorsCountState> ValidatorsCount { get; }
public override MetaDataCache<HashIndexState> BlockHashIndex { get; }
public override MetaDataCache<HashIndexState> HeaderHashIndex { get; }
public override MetaDataCache<HashIndexState> StateRootHashIndex { get; }
public override MetaDataCache<RootHashIndex> StateRootHashIndex { get; }

public CloneSnapshot(Snapshot snapshot)
{
Expand Down
2 changes: 1 addition & 1 deletion neo/Persistence/IPersistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public interface IPersistence
MetaDataCache<ValidatorsCountState> ValidatorsCount { get; }
MetaDataCache<HashIndexState> BlockHashIndex { get; }
MetaDataCache<HashIndexState> HeaderHashIndex { get; }
MetaDataCache<HashIndexState> StateRootHashIndex { get; }
MetaDataCache<RootHashIndex> StateRootHashIndex { get; }
}
}
4 changes: 2 additions & 2 deletions neo/Persistence/LevelDB/DbSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class DbSnapshot : Snapshot
public override MetaDataCache<ValidatorsCountState> ValidatorsCount { get; }
public override MetaDataCache<HashIndexState> BlockHashIndex { get; }
public override MetaDataCache<HashIndexState> HeaderHashIndex { get; }
public override MetaDataCache<HashIndexState> StateRootHashIndex { get; }
public override MetaDataCache<RootHashIndex> StateRootHashIndex { get; }

public DbSnapshot(DB db)
{
Expand All @@ -48,7 +48,7 @@ public DbSnapshot(DB db)
ValidatorsCount = new DbMetaDataCache<ValidatorsCountState>(db, options, batch, Prefixes.IX_ValidatorsCount);
BlockHashIndex = new DbMetaDataCache<HashIndexState>(db, options, batch, Prefixes.IX_CurrentBlock);
HeaderHashIndex = new DbMetaDataCache<HashIndexState>(db, options, batch, Prefixes.IX_CurrentHeader);
StateRootHashIndex = new DbMetaDataCache<HashIndexState>(db, options, batch, Prefixes.IX_CurrentStateRoot);
StateRootHashIndex = new DbMetaDataCache<RootHashIndex>(db, options, batch, Prefixes.IX_CurrentStateRoot);
}

public override void Commit()
Expand Down
4 changes: 2 additions & 2 deletions neo/Persistence/LevelDB/LevelDBStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public override MetaDataCache<HashIndexState> GetHeaderHashIndex()
return new DbMetaDataCache<HashIndexState>(db, null, null, Prefixes.IX_CurrentHeader);
}

public override MetaDataCache<HashIndexState> GetStateRootHashIndex()
public override MetaDataCache<RootHashIndex> GetStateRootHashIndex()
{
return new DbMetaDataCache<HashIndexState>(db, null, null, Prefixes.IX_CurrentStateRoot);
return new DbMetaDataCache<RootHashIndex>(db, null, null, Prefixes.IX_CurrentStateRoot);
}

public override void Put(byte prefix, byte[] key, byte[] value)
Expand Down
4 changes: 2 additions & 2 deletions neo/Persistence/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public abstract class Snapshot : IDisposable, IPersistence, IScriptTable
public abstract MetaDataCache<ValidatorsCountState> ValidatorsCount { get; }
public abstract MetaDataCache<HashIndexState> BlockHashIndex { get; }
public abstract MetaDataCache<HashIndexState> HeaderHashIndex { get; }
public abstract MetaDataCache<HashIndexState> StateRootHashIndex { get; }
public abstract MetaDataCache<RootHashIndex> StateRootHashIndex { get; }

public uint Height => BlockHashIndex.Get().Index;
public uint HeaderHeight => HeaderHashIndex.Get().Index;
public uint StateHeight => StateRootHashIndex.Get().Index;
public long StateHeight => StateRootHashIndex.Get().Index;
public UInt256 CurrentBlockHash => BlockHashIndex.Get().Hash;
public UInt256 CurrentHeaderHash => HeaderHashIndex.Get().Hash;

Expand Down
4 changes: 2 additions & 2 deletions neo/Persistence/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class Store : IPersistence
MetaDataCache<ValidatorsCountState> IPersistence.ValidatorsCount => GetValidatorsCount();
MetaDataCache<HashIndexState> IPersistence.BlockHashIndex => GetBlockHashIndex();
MetaDataCache<HashIndexState> IPersistence.HeaderHashIndex => GetHeaderHashIndex();
MetaDataCache<HashIndexState> IPersistence.StateRootHashIndex => GetStateRootHashIndex();
MetaDataCache<RootHashIndex> IPersistence.StateRootHashIndex => GetStateRootHashIndex();
public abstract byte[] Get(byte prefix, byte[] key);
public abstract DataCache<UInt256, BlockState> GetBlocks();
public abstract DataCache<UInt256, TransactionState> GetTransactions();
Expand All @@ -37,7 +37,7 @@ public abstract class Store : IPersistence
public abstract MetaDataCache<ValidatorsCountState> GetValidatorsCount();
public abstract MetaDataCache<HashIndexState> GetBlockHashIndex();
public abstract MetaDataCache<HashIndexState> GetHeaderHashIndex();
public abstract MetaDataCache<HashIndexState> GetStateRootHashIndex();
public abstract MetaDataCache<RootHashIndex> GetStateRootHashIndex();
public abstract void Put(byte prefix, byte[] key, byte[] value);
public abstract void PutSync(byte prefix, byte[] key, byte[] value);

Expand Down

0 comments on commit 1dbfb93

Please sign in to comment.