-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[neox] persistence #1692
Closed
Closed
[neox] persistence #1692
Changes from 56 commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
3f083d2
Merge pull request #1 from neo-project/master
ZhangTao1596 05aaade
Merge pull request #2 from neo-project/master
ZhangTao1596 39da07b
Merge pull request #5 from neo-project/master
ZhangTao1596 caa9426
Merge branch 'master' of github.com:neo-project/neo
ZhangTao1596 aa06d4c
Merge branch 'master' of github.com:neo-project/neo
ZhangTao1596 144d8d1
Merge branch 'master' of github.com:neo-project/neo
ZhangTao1596 71dc2e8
Merge branch 'master' of github.com:neo-project/neo
ZhangTao1596 62d34b3
Merge branch 'master' of github.com:neo-project/neo
ZhangTao1596 f114093
don't use snapshot directly in plugin
ZhangTao1596 b85d68e
Merge branch 'master' of github.com:neo-project/neo
ZhangTao1596 a8faab8
persistence and ut
ZhangTao1596 294e9b9
fix mpt
ZhangTao1596 bc945a5
precommit
ZhangTao1596 6ef3ccb
fix commit
ZhangTao1596 e5cdb62
Merge branch 'neox-persistence' of github.com:KickSeason/neo into neo…
ZhangTao1596 2cd85d0
rm LocalRootHashIndex
ZhangTao1596 cfccec9
fix ut
ZhangTao1596 ef8c14c
pre commit
ZhangTao1596 d2cc98d
fix clone view
ZhangTao1596 559831e
change name
ZhangTao1596 88eeff2
rename
ZhangTao1596 f97c15a
abstract
ZhangTao1596 2b20fc8
comment
ZhangTao1596 1fd6136
fix ReadOnlyView
ZhangTao1596 4b818a2
rm precommit
ZhangTao1596 0c07d4b
rm HashState
ZhangTao1596 fcefc80
add MPTDataCache
45505c9
optimze
d9c5ee2
optimize
550c72e
remove blank line
5b6924f
StateRoot verify fee
ZhangTao1596 9e98192
Merge branch 'master' into neox-persistence
ZhangTao1596 fb09bb0
expose Root in MPTDataCache
ZhangTao1596 7172da6
Merge branch 'neox-persistence' of github.com:KickSeason/neo into neo…
ZhangTao1596 b197725
fix some and ut
ZhangTao1596 586e751
Merge branch 'master' into neox-persistence
b81db5b
Merge branch 'master' of github.com:neo-project/neo
ZhangTao1596 e69cdab
Merge branch 'master' into neox-persistence
ZhangTao1596 2376eb2
merge master
ZhangTao1596 a42998f
master
ZhangTao1596 10f2f3c
fix mpt ut
ZhangTao1596 b1b5f12
Merge branch 'neox-persistence' of github.com:KickSeason/neo into neo…
ZhangTao1596 fae1d14
fix Storages and name
ZhangTao1596 520d03b
rename
ZhangTao1596 80235bc
add comment
ZhangTao1596 a3b491d
proof prefix
ZhangTao1596 aeff67c
fix
ZhangTao1596 1337cd7
format
ZhangTao1596 17b67e6
format
ZhangTao1596 b018556
format
ZhangTao1596 404185a
add StateRoot ut
ZhangTao1596 2acfacf
reset mpt prefix
0d7490a
rm GetMessage
ZhangTao1596 d68ba68
Merge branch 'master' into neox-persistence
ZhangTao1596 59c8706
throw exception when no script hash in state root
ZhangTao1596 ac6a24b
UpdateLocalStateRoot when Storages changed
ZhangTao1596 924a6d6
Merge branch 'master' into neox-persistence
ZhangTao1596 00e9d9f
Merge branch 'master' into neox-persistence
ZhangTao1596 6fe285a
Merge branch 'master' into neox-persistence
ZhangTao1596 e56c433
Merge branch 'master' into neox-persistence
ZhangTao1596 26aa1de
Merge branch 'master' into neox-persistence
ZhangTao1596 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,122 @@ | ||
using Neo.Cryptography; | ||
using Neo.IO; | ||
using Neo.IO.Json; | ||
using Neo.Ledger; | ||
using Neo.Persistence; | ||
using Neo.SmartContract; | ||
using Neo.SmartContract.Native; | ||
using System; | ||
using System.IO; | ||
|
||
namespace Neo.Network.P2P.Payloads | ||
{ | ||
public class StateRoot : ICloneable<StateRoot>, IInventory | ||
{ | ||
public byte Version; | ||
public uint Index; | ||
public UInt256 Root; | ||
public Witness Witness; | ||
|
||
InventoryType IInventory.InventoryType => InventoryType.StateRoot; | ||
|
||
private UInt256 _hash = null; | ||
|
||
public UInt256 Hash | ||
{ | ||
get | ||
{ | ||
if (_hash == null) | ||
{ | ||
_hash = new UInt256(Crypto.Hash256(this.GetHashData())); | ||
} | ||
return _hash; | ||
} | ||
} | ||
|
||
Witness[] IVerifiable.Witnesses | ||
{ | ||
get | ||
{ | ||
return new[] { Witness }; | ||
} | ||
set | ||
{ | ||
if (value.Length != 1) throw new ArgumentException(); | ||
Witness = value[0]; | ||
} | ||
} | ||
|
||
public int Size => | ||
sizeof(byte) + //Version | ||
sizeof(uint) + //Index | ||
UInt256.Length + //Root | ||
Witness.Size; //Witness | ||
|
||
StateRoot ICloneable<StateRoot>.Clone() | ||
{ | ||
return new StateRoot | ||
{ | ||
Version = Version, | ||
Index = Index, | ||
Root = Root, | ||
Witness = Witness, | ||
}; | ||
} | ||
|
||
void ICloneable<StateRoot>.FromReplica(StateRoot replica) | ||
{ | ||
Version = replica.Version; | ||
Index = replica.Index; | ||
Root = replica.Root; | ||
Witness = replica.Witness; | ||
} | ||
|
||
public void Deserialize(BinaryReader reader) | ||
{ | ||
this.DeserializeUnsigned(reader); | ||
Witness = reader.ReadSerializable<Witness>(); | ||
} | ||
|
||
public void DeserializeUnsigned(BinaryReader reader) | ||
{ | ||
Version = reader.ReadByte(); | ||
Index = reader.ReadUInt32(); | ||
Root = reader.ReadSerializable<UInt256>(); | ||
} | ||
|
||
public void Serialize(BinaryWriter writer) | ||
{ | ||
this.SerializeUnsigned(writer); | ||
writer.Write(Witness); | ||
} | ||
|
||
public void SerializeUnsigned(BinaryWriter writer) | ||
{ | ||
writer.Write(Version); | ||
writer.Write(Index); | ||
writer.Write(Root); | ||
} | ||
|
||
public bool Verify(StoreView snapshot) | ||
{ | ||
return this.VerifyWitnesses(snapshot, 1_00000000); | ||
} | ||
|
||
public virtual UInt160[] GetScriptHashesForVerifying(StoreView snapshot) | ||
{ | ||
var script_hash = Blockchain.Singleton.GetBlock(Index)?.NextConsensus; | ||
if (script_hash is null) throw new System.InvalidOperationException("No script hash for state root verifying"); | ||
return new UInt160[] { script_hash }; | ||
} | ||
|
||
public JObject ToJson() | ||
{ | ||
var json = new JObject(); | ||
json["version"] = Version; | ||
json["index"] = Index; | ||
json["stateroot"] = Root.ToString(); | ||
json["witness"] = Witness.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,53 @@ | ||
|
||
using Neo.Cryptography.MPT; | ||
using Neo.IO; | ||
using Neo.IO.Caching; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Neo.Persistence | ||
{ | ||
internal class MPTDataCache<TKey, TValue> : DataCache<TKey, TValue> | ||
where TKey : IEquatable<TKey>, ISerializable, new() | ||
where TValue : class, ICloneable<TValue>, ISerializable, new() | ||
{ | ||
private MPTTrie<TKey, TValue> mptTrie; | ||
|
||
public MPTNode Root => mptTrie.Root; | ||
|
||
public MPTDataCache(IReadOnlyStore store, UInt256 root) | ||
{ | ||
mptTrie = new MPTTrie<TKey, TValue>(store as ISnapshot, root); | ||
} | ||
|
||
protected override void AddInternal(TKey key, TValue value) | ||
{ | ||
mptTrie.Put(key, value); | ||
} | ||
|
||
protected override void DeleteInternal(TKey key) | ||
{ | ||
mptTrie.Delete(key); | ||
} | ||
|
||
protected override IEnumerable<(TKey Key, TValue Value)> FindInternal(byte[] key_prefix) | ||
{ | ||
return mptTrie.Find(key_prefix); | ||
} | ||
|
||
protected override TValue GetInternal(TKey key) | ||
{ | ||
return mptTrie[key]; | ||
} | ||
|
||
protected override TValue TryGetInternal(TKey key) | ||
{ | ||
return mptTrie[key]; | ||
} | ||
|
||
protected override void UpdateInternal(TKey key, TValue value) | ||
{ | ||
mptTrie.Put(key, value); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we require a
MPTDataCache
maybe we should modify https://github.com/neo-project/neo/pull/1692/files#diff-de4093105173aee33135b364a5a08edaR17 to be aMPTDataCache
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we can't convert
DataClone
toMPTCache
afterCreateSnapshot
method.neo/src/neo/IO/Caching/DataCache.cs
Lines 104 to 107 in 835fdee