Skip to content

Commit

Permalink
Hide the prefix in the store (neo-project#1070)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and Luchuan committed Jan 10, 2020
1 parent 96f3704 commit aa5172c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
8 changes: 4 additions & 4 deletions neo/Consensus/ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ namespace Neo.Consensus
internal class ConsensusContext : IDisposable, ISerializable
{
/// <summary>
/// Prefix for saving consensus state.
/// Key for saving consensus state.
/// </summary>
public const byte CN_Context = 0xf4;
private static readonly byte[] ConsensusStateKey = { 0xf4 };

public Block Block;
public byte ViewNumber;
Expand Down Expand Up @@ -143,7 +143,7 @@ public uint GetPrimaryIndex(byte viewNumber)

public bool Load()
{
byte[] data = store.Get(CN_Context, new byte[0]);
byte[] data = store.Get(ConsensusStateKey);
if (data is null || data.Length == 0) return false;
using (MemoryStream ms = new MemoryStream(data, false))
using (BinaryReader reader = new BinaryReader(ms))
Expand Down Expand Up @@ -401,7 +401,7 @@ public void Reset(byte viewNumber)

public void Save()
{
store.PutSync(CN_Context, new byte[0], this.ToArray());
store.PutSync(ConsensusStateKey, this.ToArray());
}

public void Serialize(BinaryWriter writer)
Expand Down
12 changes: 6 additions & 6 deletions neo/Persistence/LevelDB/LevelDBStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public void Dispose()
db.Dispose();
}

public override byte[] Get(byte prefix, byte[] key)
public override byte[] Get(byte[] key)
{
if (!db.TryGet(ReadOptions.Default, SliceBuilder.Begin(prefix).Add(key), out Slice slice))
if (!db.TryGet(ReadOptions.Default, key, out Slice slice))
return null;
return slice.ToArray();
}
Expand Down Expand Up @@ -81,14 +81,14 @@ public override MetaDataCache<HashIndexState> GetHeaderHashIndex()
return new DbMetaDataCache<HashIndexState>(db, null, null, Prefixes.IX_CurrentHeader);
}

public override void Put(byte prefix, byte[] key, byte[] value)
public override void Put(byte[] key, byte[] value)
{
db.Put(WriteOptions.Default, SliceBuilder.Begin(prefix).Add(key), value);
db.Put(WriteOptions.Default, key, value);
}

public override void PutSync(byte prefix, byte[] key, byte[] value)
public override void PutSync(byte[] key, byte[] value)
{
db.Put(new WriteOptions { Sync = true }, SliceBuilder.Begin(prefix).Add(key), value);
db.Put(new WriteOptions { Sync = true }, key, value);
}
}
}
6 changes: 3 additions & 3 deletions neo/Persistence/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ public abstract class Store : IPersistence
MetaDataCache<HashIndexState> IPersistence.BlockHashIndex => GetBlockHashIndex();
MetaDataCache<HashIndexState> IPersistence.HeaderHashIndex => GetHeaderHashIndex();

public abstract byte[] Get(byte prefix, byte[] key);
public abstract byte[] Get(byte[] key);
public abstract DataCache<UInt256, TrimmedBlock> GetBlocks();
public abstract DataCache<UInt256, TransactionState> GetTransactions();
public abstract DataCache<UInt160, ContractState> GetContracts();
public abstract DataCache<StorageKey, StorageItem> GetStorages();
public abstract DataCache<UInt32Wrapper, HeaderHashList> GetHeaderHashList();
public abstract MetaDataCache<HashIndexState> GetBlockHashIndex();
public abstract MetaDataCache<HashIndexState> GetHeaderHashIndex();
public abstract void Put(byte prefix, byte[] key, byte[] value);
public abstract void PutSync(byte prefix, byte[] key, byte[] value);
public abstract void Put(byte[] key, byte[] value);
public abstract void PutSync(byte[] key, byte[] value);

public abstract Snapshot GetSnapshot();
}
Expand Down

0 comments on commit aa5172c

Please sign in to comment.