From 6fec20eddb393c179501e5beb992a6606221dbfb Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Tue, 26 Jan 2021 16:07:34 +0800 Subject: [PATCH 1/5] Add initial values for native contracts --- src/neo/SmartContract/ApplicationEngine.cs | 4 +- .../Native/ContractManagement.cs | 3 +- src/neo/SmartContract/Native/NameService.cs | 14 ++--- .../SmartContract/Native/NonfungibleToken.cs | 10 ++-- .../SmartContract/Native/OracleContract.cs | 8 +-- .../SmartContract/Native/PolicyContract.cs | 54 ++++++++----------- .../SmartContract/Native/UT_PolicyContract.cs | 7 +-- 7 files changed, 48 insertions(+), 52 deletions(-) diff --git a/src/neo/SmartContract/ApplicationEngine.cs b/src/neo/SmartContract/ApplicationEngine.cs index 936eaac7f6..c5857228da 100644 --- a/src/neo/SmartContract/ApplicationEngine.cs +++ b/src/neo/SmartContract/ApplicationEngine.cs @@ -59,8 +59,8 @@ protected ApplicationEngine(TriggerType trigger, IVerifiable container, DataCach this.Snapshot = snapshot; this.PersistingBlock = persistingBlock; this.gas_amount = gas; - this.exec_fee_factor = snapshot is null ? PolicyContract.DefaultExecFeeFactor : NativeContract.Policy.GetExecFeeFactor(Snapshot); - this.StoragePrice = snapshot is null ? PolicyContract.DefaultStoragePrice : NativeContract.Policy.GetStoragePrice(Snapshot); + this.exec_fee_factor = snapshot is null || persistingBlock?.Index == 0 ? PolicyContract.DefaultExecFeeFactor : NativeContract.Policy.GetExecFeeFactor(Snapshot); + this.StoragePrice = snapshot is null || persistingBlock?.Index == 0 ? PolicyContract.DefaultStoragePrice : NativeContract.Policy.GetStoragePrice(Snapshot); } protected internal void AddGas(long gas) diff --git a/src/neo/SmartContract/Native/ContractManagement.cs b/src/neo/SmartContract/Native/ContractManagement.cs index 2bf9bd9231..416bfa594a 100644 --- a/src/neo/SmartContract/Native/ContractManagement.cs +++ b/src/neo/SmartContract/Native/ContractManagement.cs @@ -65,7 +65,7 @@ internal ContractManagement() private int GetNextAvailableId(DataCache snapshot) { - StorageItem item = snapshot.GetAndChange(CreateStorageKey(Prefix_NextAvailableId), () => new StorageItem(1)); + StorageItem item = snapshot.GetAndChange(CreateStorageKey(Prefix_NextAvailableId)); int value = (int)(BigInteger)item; item.Add(1); return value; @@ -74,6 +74,7 @@ private int GetNextAvailableId(DataCache snapshot) internal override void Initialize(ApplicationEngine engine) { engine.Snapshot.Add(CreateStorageKey(Prefix_MinimumDeploymentFee), new StorageItem(10_00000000)); + engine.Snapshot.Add(CreateStorageKey(Prefix_NextAvailableId), new StorageItem(0)); } internal override void OnPersist(ApplicationEngine engine) diff --git a/src/neo/SmartContract/Native/NameService.cs b/src/neo/SmartContract/Native/NameService.cs index 1afd544cdc..4e6bec1d36 100644 --- a/src/neo/SmartContract/Native/NameService.cs +++ b/src/neo/SmartContract/Native/NameService.cs @@ -36,6 +36,8 @@ internal NameService() internal override void Initialize(ApplicationEngine engine) { + base.Initialize(engine); + engine.Snapshot.Add(CreateStorageKey(Prefix_Roots), new StorageItem(new StringList())); engine.Snapshot.Add(CreateStorageKey(Prefix_DomainPrice), new StorageItem(10_00000000)); } @@ -68,7 +70,7 @@ private void AddRoot(ApplicationEngine engine, string root) { if (!rootRegex.IsMatch(root)) throw new ArgumentException(null, nameof(root)); if (!CheckCommittee(engine)) throw new InvalidOperationException(); - StringList roots = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_Roots), () => new StorageItem(new StringList())).GetInteroperable(); + StringList roots = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_Roots)).GetInteroperable(); int index = roots.BinarySearch(root); if (index >= 0) throw new InvalidOperationException("The name already exists."); roots.Insert(~index, root); @@ -76,7 +78,7 @@ private void AddRoot(ApplicationEngine engine, string root) public IEnumerable GetRoots(DataCache snapshot) { - return snapshot.TryGet(CreateStorageKey(Prefix_Roots))?.GetInteroperable() ?? Enumerable.Empty(); + return snapshot[CreateStorageKey(Prefix_Roots)].GetInteroperable(); } [ContractMethod(0_03000000, CallFlags.WriteStates)] @@ -101,8 +103,8 @@ public bool IsAvailable(DataCache snapshot, string name) if (names.Length != 2) throw new ArgumentException(null, nameof(name)); byte[] hash = GetKey(Utility.StrictUTF8.GetBytes(name)); if (snapshot.TryGet(CreateStorageKey(Prefix_Token).Add(hash)) is not null) return false; - StringList roots = snapshot.TryGet(CreateStorageKey(Prefix_Roots))?.GetInteroperable(); - if (roots is null || roots.BinarySearch(names[1]) < 0) throw new InvalidOperationException(); + StringList roots = snapshot[CreateStorageKey(Prefix_Roots)].GetInteroperable(); + if (roots.BinarySearch(names[1]) < 0) throw new InvalidOperationException(); return true; } @@ -115,8 +117,8 @@ private bool Register(ApplicationEngine engine, string name, UInt160 owner) if (!engine.CheckWitnessInternal(owner)) throw new InvalidOperationException(); byte[] hash = GetKey(Utility.StrictUTF8.GetBytes(name)); if (engine.Snapshot.TryGet(CreateStorageKey(Prefix_Token).Add(hash)) is not null) return false; - StringList roots = engine.Snapshot.TryGet(CreateStorageKey(Prefix_Roots))?.GetInteroperable(); - if (roots is null || roots.BinarySearch(names[1]) < 0) throw new InvalidOperationException(); + StringList roots = engine.Snapshot[CreateStorageKey(Prefix_Roots)].GetInteroperable(); + if (roots.BinarySearch(names[1]) < 0) throw new InvalidOperationException(); engine.AddGas(GetPrice(engine.Snapshot)); NameState state = new NameState { diff --git a/src/neo/SmartContract/Native/NonfungibleToken.cs b/src/neo/SmartContract/Native/NonfungibleToken.cs index 8fa3696fe5..35f799a57f 100644 --- a/src/neo/SmartContract/Native/NonfungibleToken.cs +++ b/src/neo/SmartContract/Native/NonfungibleToken.cs @@ -62,12 +62,17 @@ protected NonfungibleToken() protected virtual byte[] GetKey(byte[] tokenId) => tokenId; + internal override void Initialize(ApplicationEngine engine) + { + engine.Snapshot.Add(CreateStorageKey(Prefix_TotalSupply), new StorageItem(BigInteger.Zero)); + } + protected void Mint(ApplicationEngine engine, TokenState token) { engine.Snapshot.Add(CreateStorageKey(Prefix_Token).Add(GetKey(token.Id)), new StorageItem(token)); NFTAccountState account = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_Account).Add(token.Owner), () => new StorageItem(new NFTAccountState())).GetInteroperable(); account.Add(token.Id); - engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_TotalSupply), () => new StorageItem(BigInteger.Zero)).Add(1); + engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_TotalSupply)).Add(1); PostTransfer(engine, null, token.Owner, token.Id); } @@ -93,8 +98,7 @@ private protected void Burn(ApplicationEngine engine, StorageKey key) [ContractMethod(0_01000000, CallFlags.ReadStates)] public BigInteger TotalSupply(DataCache snapshot) { - StorageItem storage = snapshot.TryGet(CreateStorageKey(Prefix_TotalSupply)); - return storage ?? BigInteger.Zero; + return snapshot[CreateStorageKey(Prefix_TotalSupply)]; } [ContractMethod(0_01000000, CallFlags.ReadStates)] diff --git a/src/neo/SmartContract/Native/OracleContract.cs b/src/neo/SmartContract/Native/OracleContract.cs index 5e4e710de0..f2f48b9389 100644 --- a/src/neo/SmartContract/Native/OracleContract.cs +++ b/src/neo/SmartContract/Native/OracleContract.cs @@ -127,7 +127,7 @@ private static byte[] GetUrlHash(string url) internal override void Initialize(ApplicationEngine engine) { - engine.Snapshot.Add(CreateStorageKey(Prefix_RequestId), new StorageItem(BitConverter.GetBytes(0ul))); + engine.Snapshot.Add(CreateStorageKey(Prefix_RequestId), new StorageItem(BigInteger.Zero)); } internal override void PostPersist(ApplicationEngine engine) @@ -184,13 +184,13 @@ private void Request(ApplicationEngine engine, string url, string filter, string //Increase the request id StorageItem item_id = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_RequestId)); - ulong id = BitConverter.ToUInt64(item_id.Value) + 1; - item_id.Value = BitConverter.GetBytes(id); + ulong id = (ulong)(BigInteger)item_id; + item_id.Add(1); //Put the request to storage if (ContractManagement.GetContract(engine.Snapshot, engine.CallingScriptHash) is null) throw new InvalidOperationException(); - engine.Snapshot.Add(CreateStorageKey(Prefix_Request).Add(item_id.Value), new StorageItem(new OracleRequest + engine.Snapshot.Add(CreateStorageKey(Prefix_Request).AddBigEndian(id), new StorageItem(new OracleRequest { OriginalTxid = GetOriginalTxid(engine), GasForResponse = gasForResponse, diff --git a/src/neo/SmartContract/Native/PolicyContract.cs b/src/neo/SmartContract/Native/PolicyContract.cs index dfe4f8d32d..eb60511042 100644 --- a/src/neo/SmartContract/Native/PolicyContract.cs +++ b/src/neo/SmartContract/Native/PolicyContract.cs @@ -27,52 +27,50 @@ internal PolicyContract() { } + internal override void Initialize(ApplicationEngine engine) + { + engine.Snapshot.Add(CreateStorageKey(Prefix_MaxTransactionsPerBlock), new StorageItem(512)); + engine.Snapshot.Add(CreateStorageKey(Prefix_FeePerByte), new StorageItem(1000)); + engine.Snapshot.Add(CreateStorageKey(Prefix_MaxBlockSize), new StorageItem(1024 * 256)); + engine.Snapshot.Add(CreateStorageKey(Prefix_MaxBlockSystemFee), new StorageItem(9000 * GAS.Factor)); // For the transfer method of NEP5, the maximum persisting time is about three seconds. + engine.Snapshot.Add(CreateStorageKey(Prefix_ExecFeeFactor), new StorageItem(DefaultExecFeeFactor)); + engine.Snapshot.Add(CreateStorageKey(Prefix_StoragePrice), new StorageItem(DefaultStoragePrice)); + } + [ContractMethod(0_01000000, CallFlags.ReadStates)] public uint GetMaxTransactionsPerBlock(DataCache snapshot) { - StorageItem item = snapshot.TryGet(CreateStorageKey(Prefix_MaxTransactionsPerBlock)); - if (item is null) return 512; - return (uint)(BigInteger)item; + return (uint)(BigInteger)snapshot[CreateStorageKey(Prefix_MaxTransactionsPerBlock)]; } [ContractMethod(0_01000000, CallFlags.ReadStates)] public uint GetMaxBlockSize(DataCache snapshot) { - StorageItem item = snapshot.TryGet(CreateStorageKey(Prefix_MaxBlockSize)); - if (item is null) return 1024 * 256; - return (uint)(BigInteger)item; + return (uint)(BigInteger)snapshot[CreateStorageKey(Prefix_MaxBlockSize)]; } [ContractMethod(0_01000000, CallFlags.ReadStates)] public long GetMaxBlockSystemFee(DataCache snapshot) { - StorageItem item = snapshot.TryGet(CreateStorageKey(Prefix_MaxBlockSystemFee)); - if (item is null) return 9000 * (long)GAS.Factor; // For the transfer method of NEP5, the maximum persisting time is about three seconds. - return (long)(BigInteger)item; + return (long)(BigInteger)snapshot[CreateStorageKey(Prefix_MaxBlockSystemFee)]; } [ContractMethod(0_01000000, CallFlags.ReadStates)] public long GetFeePerByte(DataCache snapshot) { - StorageItem item = snapshot.TryGet(CreateStorageKey(Prefix_FeePerByte)); - if (item is null) return 1000; - return (long)(BigInteger)item; + return (long)(BigInteger)snapshot[CreateStorageKey(Prefix_FeePerByte)]; } [ContractMethod(0_01000000, CallFlags.ReadStates)] public uint GetExecFeeFactor(DataCache snapshot) { - StorageItem item = snapshot.TryGet(CreateStorageKey(Prefix_ExecFeeFactor)); - if (item is null) return DefaultExecFeeFactor; - return (uint)(BigInteger)item; + return (uint)(BigInteger)snapshot[CreateStorageKey(Prefix_ExecFeeFactor)]; } [ContractMethod(0_01000000, CallFlags.ReadStates)] public uint GetStoragePrice(DataCache snapshot) { - StorageItem item = snapshot.TryGet(CreateStorageKey(Prefix_StoragePrice)); - if (item is null) return DefaultStoragePrice; - return (uint)(BigInteger)item; + return (uint)(BigInteger)snapshot[CreateStorageKey(Prefix_StoragePrice)]; } [ContractMethod(0_01000000, CallFlags.ReadStates)] @@ -86,8 +84,7 @@ private void SetMaxBlockSize(ApplicationEngine engine, uint value) { if (value > Message.PayloadMaxSize) throw new ArgumentOutOfRangeException(nameof(value)); if (!CheckCommittee(engine)) throw new InvalidOperationException(); - StorageItem storage = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_MaxBlockSize), () => new StorageItem()); - storage.Set(value); + engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_MaxBlockSize)).Set(value); } [ContractMethod(0_03000000, CallFlags.WriteStates)] @@ -95,8 +92,7 @@ private void SetMaxTransactionsPerBlock(ApplicationEngine engine, uint value) { if (value > Block.MaxTransactionsPerBlock) throw new ArgumentOutOfRangeException(nameof(value)); if (!CheckCommittee(engine)) throw new InvalidOperationException(); - StorageItem storage = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_MaxTransactionsPerBlock), () => new StorageItem()); - storage.Set(value); + engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_MaxTransactionsPerBlock)).Set(value); } [ContractMethod(0_03000000, CallFlags.WriteStates)] @@ -104,8 +100,7 @@ private void SetMaxBlockSystemFee(ApplicationEngine engine, long value) { if (value <= 4007600) throw new ArgumentOutOfRangeException(nameof(value)); if (!CheckCommittee(engine)) throw new InvalidOperationException(); - StorageItem storage = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_MaxBlockSystemFee), () => new StorageItem()); - storage.Set(value); + engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_MaxBlockSystemFee)).Set(value); } [ContractMethod(0_03000000, CallFlags.WriteStates)] @@ -113,8 +108,7 @@ private void SetFeePerByte(ApplicationEngine engine, long value) { if (value < 0 || value > 1_00000000) throw new ArgumentOutOfRangeException(nameof(value)); if (!CheckCommittee(engine)) throw new InvalidOperationException(); - StorageItem storage = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_FeePerByte), () => new StorageItem()); - storage.Set(value); + engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_FeePerByte)).Set(value); } [ContractMethod(0_03000000, CallFlags.WriteStates)] @@ -122,8 +116,7 @@ private void SetExecFeeFactor(ApplicationEngine engine, uint value) { if (value == 0 || value > MaxExecFeeFactor) throw new ArgumentOutOfRangeException(nameof(value)); if (!CheckCommittee(engine)) throw new InvalidOperationException(); - StorageItem storage = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_ExecFeeFactor), () => new StorageItem()); - storage.Set(value); + engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_ExecFeeFactor)).Set(value); } [ContractMethod(0_03000000, CallFlags.WriteStates)] @@ -131,8 +124,7 @@ private void SetStoragePrice(ApplicationEngine engine, uint value) { if (value == 0 || value > MaxStoragePrice) throw new ArgumentOutOfRangeException(nameof(value)); if (!CheckCommittee(engine)) throw new InvalidOperationException(); - StorageItem storage = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_StoragePrice), () => new StorageItem()); - storage.Set(value); + engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_StoragePrice)).Set(value); } [ContractMethod(0_03000000, CallFlags.WriteStates)] @@ -143,7 +135,7 @@ private bool BlockAccount(ApplicationEngine engine, UInt160 account) var key = CreateStorageKey(Prefix_BlockedAccount).Add(account); if (engine.Snapshot.Contains(key)) return false; - engine.Snapshot.Add(key, new StorageItem(new byte[] { 0x01 })); + engine.Snapshot.Add(key, new StorageItem(Array.Empty())); return true; } diff --git a/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs b/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs index 309707d005..ede06e3665 100644 --- a/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs +++ b/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs @@ -7,7 +7,6 @@ using Neo.SmartContract; using Neo.SmartContract.Native; using Neo.UnitTests.Extensions; -using Neo.UnitTests.IO.Caching; using System; using System.Linq; @@ -207,12 +206,11 @@ public void Check_SetFeePerByte() [TestMethod] public void Check_SetBaseExecFee() { - var snapshot = Blockchain.Singleton.GetSnapshot(); + var snapshot = _snapshot.CreateSnapshot(); // Fake blockchain Block block = new Block() { Index = 1000, PrevHash = UInt256.Zero }; - NativeContract.Policy.Initialize(ApplicationEngine.Create(TriggerType.Application, null, snapshot, block, 0)); // Without signature @@ -251,12 +249,11 @@ public void Check_SetBaseExecFee() [TestMethod] public void Check_SetStoragePrice() { - var snapshot = Blockchain.Singleton.GetSnapshot(); + var snapshot = _snapshot.CreateSnapshot(); // Fake blockchain Block block = new Block() { Index = 1000, PrevHash = UInt256.Zero }; - NativeContract.Policy.Initialize(ApplicationEngine.Create(TriggerType.Application, null, snapshot, block, 0)); // Without signature From 6c0138e1bfb33ee5b4117a70c261593217718f08 Mon Sep 17 00:00:00 2001 From: Shargon Date: Tue, 26 Jan 2021 09:41:49 +0100 Subject: [PATCH 2/5] GetRequest as BigEndian --- src/neo/SmartContract/Native/OracleContract.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Native/OracleContract.cs b/src/neo/SmartContract/Native/OracleContract.cs index f2f48b9389..6ea9866037 100644 --- a/src/neo/SmartContract/Native/OracleContract.cs +++ b/src/neo/SmartContract/Native/OracleContract.cs @@ -104,7 +104,7 @@ private UInt256 GetOriginalTxid(ApplicationEngine engine) public OracleRequest GetRequest(DataCache snapshot, ulong id) { - return snapshot.TryGet(CreateStorageKey(Prefix_Request).Add(id))?.GetInteroperable(); + return snapshot.TryGet(CreateStorageKey(Prefix_Request).AddBigEndian(id))?.GetInteroperable(); } public IEnumerable<(ulong, OracleRequest)> GetRequests(DataCache snapshot) From b59c608d64cbeaf1ac688a25e2d11fa15f2ea8b2 Mon Sep 17 00:00:00 2001 From: Shargon Date: Tue, 26 Jan 2021 09:49:33 +0100 Subject: [PATCH 3/5] Check endianness in GetRequests --- src/neo/SmartContract/Native/OracleContract.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/neo/SmartContract/Native/OracleContract.cs b/src/neo/SmartContract/Native/OracleContract.cs index 6ea9866037..269e3c0800 100644 --- a/src/neo/SmartContract/Native/OracleContract.cs +++ b/src/neo/SmartContract/Native/OracleContract.cs @@ -8,6 +8,7 @@ using Neo.VM; using Neo.VM.Types; using System; +using System.Buffers.Binary; using System.Collections.Generic; using System.Linq; using System.Numerics; @@ -109,7 +110,10 @@ public OracleRequest GetRequest(DataCache snapshot, ulong id) public IEnumerable<(ulong, OracleRequest)> GetRequests(DataCache snapshot) { - return snapshot.Find(new KeyBuilder(Id, Prefix_Request).ToArray()).Select(p => (BitConverter.ToUInt64(p.Key.Key, 1), p.Value.GetInteroperable())); + return snapshot.Find(new KeyBuilder(Id, Prefix_Request).ToArray()) + .Select(p => (BitConverter.IsLittleEndian ? + BinaryPrimitives.ReverseEndianness(BitConverter.ToUInt64(p.Key.Key, 1)) : + BitConverter.ToUInt64(p.Key.Key, 1), p.Value.GetInteroperable())); } public IEnumerable<(ulong, OracleRequest)> GetRequestsByUrl(DataCache snapshot, string url) From 2341c89ce2e0063bacb3f7007402b89d8078a82e Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Tue, 26 Jan 2021 16:49:34 +0800 Subject: [PATCH 4/5] Fix --- src/neo/SmartContract/Native/OracleContract.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/neo/SmartContract/Native/OracleContract.cs b/src/neo/SmartContract/Native/OracleContract.cs index 269e3c0800..ccbff42f30 100644 --- a/src/neo/SmartContract/Native/OracleContract.cs +++ b/src/neo/SmartContract/Native/OracleContract.cs @@ -110,10 +110,10 @@ public OracleRequest GetRequest(DataCache snapshot, ulong id) public IEnumerable<(ulong, OracleRequest)> GetRequests(DataCache snapshot) { - return snapshot.Find(new KeyBuilder(Id, Prefix_Request).ToArray()) + return snapshot.Find(CreateStorageKey(Prefix_Request).ToArray()) .Select(p => (BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(BitConverter.ToUInt64(p.Key.Key, 1)) : - BitConverter.ToUInt64(p.Key.Key, 1), p.Value.GetInteroperable())); + BitConverter.ToUInt64(p.Key.Key, 1), p.Value.GetInteroperable())); } public IEnumerable<(ulong, OracleRequest)> GetRequestsByUrl(DataCache snapshot, string url) @@ -121,7 +121,7 @@ public OracleRequest GetRequest(DataCache snapshot, ulong id) IdList list = snapshot.TryGet(CreateStorageKey(Prefix_IdList).Add(GetUrlHash(url)))?.GetInteroperable(); if (list is null) yield break; foreach (ulong id in list) - yield return (id, snapshot[CreateStorageKey(Prefix_Request).Add(id)].GetInteroperable()); + yield return (id, snapshot[CreateStorageKey(Prefix_Request).AddBigEndian(id)].GetInteroperable()); } private static byte[] GetUrlHash(string url) @@ -144,7 +144,7 @@ internal override void PostPersist(ApplicationEngine engine) if (response is null) continue; //Remove the request from storage - StorageKey key = CreateStorageKey(Prefix_Request).Add(response.Id); + StorageKey key = CreateStorageKey(Prefix_Request).AddBigEndian(response.Id); OracleRequest request = engine.Snapshot.TryGet(key)?.GetInteroperable(); if (request == null) continue; engine.Snapshot.Delete(key); From a306a294c47765044e0d7fc3fc8d2e313e877c95 Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Tue, 26 Jan 2021 16:58:50 +0800 Subject: [PATCH 5/5] Optimize --- src/neo/SmartContract/Native/OracleContract.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/neo/SmartContract/Native/OracleContract.cs b/src/neo/SmartContract/Native/OracleContract.cs index ccbff42f30..8bb656540c 100644 --- a/src/neo/SmartContract/Native/OracleContract.cs +++ b/src/neo/SmartContract/Native/OracleContract.cs @@ -110,10 +110,7 @@ public OracleRequest GetRequest(DataCache snapshot, ulong id) public IEnumerable<(ulong, OracleRequest)> GetRequests(DataCache snapshot) { - return snapshot.Find(CreateStorageKey(Prefix_Request).ToArray()) - .Select(p => (BitConverter.IsLittleEndian ? - BinaryPrimitives.ReverseEndianness(BitConverter.ToUInt64(p.Key.Key, 1)) : - BitConverter.ToUInt64(p.Key.Key, 1), p.Value.GetInteroperable())); + return snapshot.Find(CreateStorageKey(Prefix_Request).ToArray()).Select(p => (BinaryPrimitives.ReadUInt64BigEndian(p.Key.Key.AsSpan(1)), p.Value.GetInteroperable())); } public IEnumerable<(ulong, OracleRequest)> GetRequestsByUrl(DataCache snapshot, string url)