-
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 all 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
05aaade
Merge pull request #2 from neo-project/master
39da07b
Merge pull request #5 from neo-project/master
caa9426
Merge branch 'master' of github.com:neo-project/neo
aa06d4c
Merge branch 'master' of github.com:neo-project/neo
144d8d1
Merge branch 'master' of github.com:neo-project/neo
71dc2e8
Merge branch 'master' of github.com:neo-project/neo
62d34b3
Merge branch 'master' of github.com:neo-project/neo
f114093
don't use snapshot directly in plugin
b85d68e
Merge branch 'master' of github.com:neo-project/neo
a8faab8
persistence and ut
294e9b9
fix mpt
bc945a5
precommit
6ef3ccb
fix commit
e5cdb62
Merge branch 'neox-persistence' of github.com:KickSeason/neo into neo…
2cd85d0
rm LocalRootHashIndex
cfccec9
fix ut
ef8c14c
pre commit
d2cc98d
fix clone view
559831e
change name
88eeff2
rename
f97c15a
abstract
2b20fc8
comment
1fd6136
fix ReadOnlyView
4b818a2
rm precommit
0c07d4b
rm HashState
fcefc80
add MPTDataCache
45505c9
optimze
d9c5ee2
optimize
550c72e
remove blank line
5b6924f
StateRoot verify fee
9e98192
Merge branch 'master' into neox-persistence
fb09bb0
expose Root in MPTDataCache
7172da6
Merge branch 'neox-persistence' of github.com:KickSeason/neo into neo…
b197725
fix some and ut
586e751
Merge branch 'master' into neox-persistence
b81db5b
Merge branch 'master' of github.com:neo-project/neo
e69cdab
Merge branch 'master' into neox-persistence
2376eb2
merge master
a42998f
master
10f2f3c
fix mpt ut
b1b5f12
Merge branch 'neox-persistence' of github.com:KickSeason/neo into neo…
fae1d14
fix Storages and name
520d03b
rename
80235bc
add comment
a3b491d
proof prefix
aeff67c
fix
1337cd7
format
17b67e6
format
b018556
format
404185a
add StateRoot ut
2acfacf
reset mpt prefix
0d7490a
rm GetMessage
d68ba68
Merge branch 'master' into neox-persistence
59c8706
throw exception when no script hash in state root
ac6a24b
UpdateLocalStateRoot when Storages changed
924a6d6
Merge branch 'master' into neox-persistence
00e9d9f
Merge branch 'master' into neox-persistence
6fe285a
Merge branch 'master' into neox-persistence
e56c433
Merge branch 'master' into neox-persistence
26aa1de
Merge branch 'master' into neox-persistence
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