-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[neox-2.x] apply mpt to storages and persist roots (#1575)
* apply mpt to storages and save roots * fix ut * use genesis hash as root store key and handle null options
- Loading branch information
zhangtao
authored
Apr 20, 2020
1 parent
3f400f6
commit 797e1a2
Showing
12 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using Neo.IO; | ||
using Neo.IO.Json; | ||
using Neo.Network.P2P.Payloads; | ||
using System.IO; | ||
|
||
namespace Neo.Ledger | ||
{ | ||
public enum StateRootVerifyFlag : byte | ||
{ | ||
Unverified = 0x00, | ||
Verified = 0x01, | ||
Invalid = 0x03, | ||
} | ||
|
||
public class StateRootState : StateBase, ICloneable<StateRootState> | ||
{ | ||
public StateRootVerifyFlag Flag; | ||
public StateRoot StateRoot; | ||
|
||
public override int Size => base.Size + sizeof(StateRootVerifyFlag) + StateRoot.Size; | ||
|
||
StateRootState ICloneable<StateRootState>.Clone() | ||
{ | ||
return new StateRootState | ||
{ | ||
Flag = Flag, | ||
StateRoot = StateRoot, | ||
}; | ||
} | ||
|
||
public override void Deserialize(BinaryReader reader) | ||
{ | ||
base.Deserialize(reader); | ||
Flag = (StateRootVerifyFlag)reader.ReadByte(); | ||
StateRoot = reader.ReadSerializable<StateRoot>(); | ||
} | ||
|
||
void ICloneable<StateRootState>.FromReplica(StateRootState replica) | ||
{ | ||
Flag = replica.Flag; | ||
StateRoot = replica.StateRoot; | ||
} | ||
|
||
public override void Serialize(BinaryWriter writer) | ||
{ | ||
base.Serialize(writer); | ||
writer.Write((byte)Flag); | ||
writer.Write(StateRoot); | ||
} | ||
|
||
public override JObject ToJson() | ||
{ | ||
JObject json = new JObject(); | ||
json["flag"] = Flag; | ||
json["stateroot"] = StateRoot.ToJson(); | ||
return json; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Neo.IO; | ||
using Neo.IO.Caching; | ||
using Neo.IO.Data.LevelDB; | ||
using Neo.Trie.MPT; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Neo.Persistence.LevelDB | ||
{ | ||
public class DbCacheWithTrie<TKey, TValue> : DataCache<TKey, TValue> | ||
where TKey : IEquatable<TKey>, ISerializable, new() | ||
where TValue : class, ICloneable<TValue>, ISerializable, new() | ||
{ | ||
private readonly DB db; | ||
private readonly ReadOptions options; | ||
private readonly WriteBatch batch; | ||
private readonly byte prefix; | ||
private MPTTrie mptTrie; | ||
private DbTrieStore trieDb; | ||
|
||
public DbCacheWithTrie(DB db, ReadOptions options, WriteBatch batch, byte prefix) | ||
{ | ||
this.db = db; | ||
this.options = options ?? ReadOptions.Default; | ||
this.batch = batch; | ||
this.prefix = prefix; | ||
this.trieDb = new DbTrieStore(db, options, batch, Prefixes.DATA_MPT); | ||
this.mptTrie = new MPTTrie(trieDb.GetRoot(), trieDb); | ||
} | ||
|
||
protected override void AddInternal(TKey key, TValue value) | ||
{ | ||
batch?.Put(prefix, key, value); | ||
mptTrie.Put(key.ToArray(), value.ToArray()); | ||
} | ||
|
||
public override void DeleteInternal(TKey key) | ||
{ | ||
batch?.Delete(prefix, key); | ||
mptTrie.TryDelete(key.ToArray()); | ||
} | ||
|
||
protected override IEnumerable<KeyValuePair<TKey, TValue>> FindInternal(byte[] key_prefix) | ||
{ | ||
return db.Find(options, SliceBuilder.Begin(prefix).Add(key_prefix), (k, v) => new KeyValuePair<TKey, TValue>(k.ToArray().AsSerializable<TKey>(1), v.ToArray().AsSerializable<TValue>())); | ||
} | ||
|
||
protected override TValue GetInternal(TKey key) | ||
{ | ||
return db.Get<TValue>(options, prefix, key); | ||
} | ||
|
||
protected override TValue TryGetInternal(TKey key) | ||
{ | ||
return db.TryGet<TValue>(options, prefix, key); | ||
} | ||
|
||
protected override void UpdateInternal(TKey key, TValue value) | ||
{ | ||
batch?.Put(prefix, key, value); | ||
mptTrie.Put(key.ToArray(), value.ToArray()); | ||
} | ||
|
||
public override void Commit() | ||
{ | ||
base.Commit(); | ||
trieDb.PutRoot(mptTrie.GetRoot()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Neo.IO.Data.LevelDB; | ||
using Neo.Trie; | ||
using Neo.Trie.MPT; | ||
using Neo.Ledger; | ||
|
||
namespace Neo.Persistence.LevelDB | ||
{ | ||
public class DbTrieStore : IKVStore | ||
{ | ||
private readonly DB db; | ||
private readonly ReadOptions options; | ||
private readonly WriteBatch batch; | ||
private readonly byte prefix; | ||
|
||
private readonly byte[] ROOT_KEY = Blockchain.GenesisBlock.Hash.ToArray(); | ||
|
||
public DbTrieStore(DB db, ReadOptions options, WriteBatch batch, byte prefix) | ||
{ | ||
this.db = db; | ||
this.options = options ?? ReadOptions.Default; | ||
this.batch = batch; | ||
this.prefix = prefix; | ||
} | ||
|
||
private byte[] StoreKey(byte[] key) | ||
{ | ||
return new byte[] { prefix }.Concat(key); | ||
} | ||
|
||
public byte[] Get(byte[] key) | ||
{ | ||
var result = db.TryGet(options, StoreKey(key), out Slice value); | ||
return result ? value.ToArray() : null; | ||
} | ||
|
||
public void Put(byte[] key, byte[] value) | ||
{ | ||
batch.Put(StoreKey(key), value); | ||
} | ||
|
||
public UInt256 GetRoot() | ||
{ | ||
var result = db.TryGet(options, StoreKey(ROOT_KEY), out Slice value); | ||
return result ? new UInt256(value.ToArray()) : null; | ||
} | ||
|
||
public void PutRoot(UInt256 root) | ||
{ | ||
if (root is null) return; | ||
batch.Put(StoreKey(ROOT_KEY), root.ToArray()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters