-
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.
* don't use snapshot directly in plugin * persistence and ut * fix mpt * precommit * fix commit * rm LocalRootHashIndex * fix ut * pre commit * fix clone view * change name * rename * abstract * comment * fix ReadOnlyView * rm precommit * rm HashState * add MPTDataCache * optimze * optimize * remove blank line * StateRoot verify fee * expose Root in MPTDataCache * fix some and ut * merge master * master * fix mpt ut * fix Storages and name * rename * add comment * proof prefix * fix * format * format * format * add StateRoot ut * reset mpt prefix * rm GetMessage * throw exception when no script hash in state root * UpdateLocalStateRoot when Storages changed * rename Co-authored-by: Tommo-L <[email protected]>
- Loading branch information
ZhangTao
and
Tommo-L
authored
Jun 29, 2020
1 parent
f68769f
commit 4775de5
Showing
18 changed files
with
312 additions
and
13 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
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 RootHash; | ||
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, | ||
RootHash = RootHash, | ||
Witness = Witness, | ||
}; | ||
} | ||
|
||
void ICloneable<StateRoot>.FromReplica(StateRoot replica) | ||
{ | ||
Version = replica.Version; | ||
Index = replica.Index; | ||
RootHash = replica.RootHash; | ||
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(); | ||
RootHash = 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(RootHash); | ||
} | ||
|
||
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"] = RootHash.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.