Skip to content

Commit

Permalink
Revert "fix-native-serialize"
Browse files Browse the repository at this point in the history
This reverts commit 2e64bc4.
  • Loading branch information
shargon committed Oct 4, 2020
1 parent 2e64bc4 commit d1b1887
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.
5 changes: 1 addition & 4 deletions src/neo/Ledger/StorageItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ namespace Neo.Ledger
{
public class StorageItem : ICloneable<StorageItem>, ISerializable
{
public const int MaxInteropEntries = 32;
public const int MaxIteropEntrySize = 512;

private byte[] value;
private object cache;
public bool IsConstant;
Expand Down Expand Up @@ -89,7 +86,7 @@ void ICloneable<StorageItem>.FromReplica(StorageItem replica)
if (cache is null)
{
var interoperable = new T();
interoperable.FromStackItem(BinarySerializer.Deserialize(value, MaxInteropEntries, MaxIteropEntrySize));
interoperable.FromStackItem(BinarySerializer.Deserialize(value, 16, 34));
cache = interoperable;
}
value = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ECPoint[] GetDesignatedByRole(StoreView snapshot, Role role)
[ContractMethod(0, CallFlags.AllowModifyStates)]
private void DesignateAsRole(ApplicationEngine engine, Role role, ECPoint[] nodes)
{
if (nodes.Length == 0 || nodes.Length > StorageItem.MaxInteropEntries) throw new ArgumentException(nameof(nodes));
if (nodes.Length == 0) throw new ArgumentException(nameof(nodes));
if (!Enum.IsDefined(typeof(Role), role))
throw new ArgumentOutOfRangeException(nameof(role));
if (!CheckCommittee(engine)) throw new InvalidOperationException();
Expand Down
7 changes: 2 additions & 5 deletions src/neo/SmartContract/Native/Oracle/OracleContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Neo.SmartContract.Native.Oracle
{
public sealed class OracleContract : NativeContract
{
private const int MaxUrlLength = byte.MaxValue;
private const int MaxUrlLength = 256;
private const int MaxFilterLength = 128;
private const int MaxCallbackLength = 32;
private const int MaxUserDataLength = 512;
Expand Down Expand Up @@ -157,10 +157,7 @@ private void Request(ApplicationEngine engine, string url, string filter, string
}));

//Add the id to the IdList
var list = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_IdList).Add(GetUrlHash(url)), () => new StorageItem(new IdList())).GetInteroperable<IdList>();
if (list.Count + 1 > StorageItem.MaxInteropEntries)
throw new InvalidOperationException("There are a lot of pending responses for this url");
list.Add(id);
engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_IdList).Add(GetUrlHash(url)), () => new StorageItem(new IdList())).GetInteroperable<IdList>().Add(id);
}

[ContractMethod(0_01000000, CallFlags.None)]
Expand Down
1 change: 0 additions & 1 deletion src/neo/SmartContract/Native/PolicyContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ private bool BlockAccount(ApplicationEngine engine, UInt160 account)
StorageItem storage = engine.Snapshot.Storages.GetOrAdd(key, () => new StorageItem(new byte[1]));
List<UInt160> accounts = storage.GetSerializableList<UInt160>();
if (accounts.Contains(account)) return false;
if ((accounts.Count + 1) > StorageItem.MaxInteropEntries) throw new ArgumentException("Maximum number of blocked accounts exceeded");
engine.Snapshot.Storages.GetAndChange(key);
accounts.Add(account);
accounts.Sort();
Expand Down
57 changes: 40 additions & 17 deletions src/neo/SmartContract/Native/Tokens/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Array = Neo.VM.Types.Array;

namespace Neo.SmartContract.Native.Tokens
{
Expand Down Expand Up @@ -69,20 +70,20 @@ private BigInteger CalculateBonus(StoreView snapshot, BigInteger value, uint sta
if (value.IsZero || start >= end) return BigInteger.Zero;
if (value.Sign < 0) throw new ArgumentOutOfRangeException(nameof(value));

GasRecord gasRecord = snapshot.Storages[CreateStorageKey(Prefix_GasPerBlock)].GetInteroperable<GasRecord>();
BigInteger sum = 0;
foreach (var gasRecord in snapshot.Storages.Find(CreateStorageKey(Prefix_GasPerBlock).ToArray())
.Select(u => (index: BitConverter.ToUInt32(u.Key.Key, u.Key.Key.Length - sizeof(uint)), gasPerBlock: (BigInteger)u.Value))
.Where(u => u.index < end)
.OrderByDescending(u => u.index))
for (var i = gasRecord.Count - 1; i >= 0; i--)
{
if (gasRecord.index > start)
var currentIndex = gasRecord[i].Index;
if (currentIndex >= end) continue;
if (currentIndex > start)
{
sum += gasRecord.gasPerBlock * (end - gasRecord.index);
end = gasRecord.index;
sum += gasRecord[i].GasPerBlock * (end - currentIndex);
end = currentIndex;
}
else
{
sum += gasRecord.gasPerBlock * (end - start);
sum += gasRecord[i].GasPerBlock * (end - start);
break;
}
}
Expand All @@ -104,7 +105,11 @@ internal override void Initialize(ApplicationEngine engine)

// Initialize economic parameters

engine.Snapshot.Storages.Add(CreateStorageKey(Prefix_GasPerBlock).Add(0u), new StorageItem(5 * GAS.Factor));
engine.Snapshot.Storages.Add(CreateStorageKey(Prefix_GasPerBlock), new StorageItem(new GasRecord
{
(0, 5 * GAS.Factor)
}));

Mint(engine, Blockchain.GetConsensusAddress(Blockchain.StandbyValidators), TotalAmount);
}

Expand Down Expand Up @@ -139,23 +144,24 @@ private bool SetGasPerBlock(ApplicationEngine engine, BigInteger gasPerBlock)
if (gasPerBlock < 0 || gasPerBlock > 10 * GAS.Factor)
throw new ArgumentOutOfRangeException(nameof(gasPerBlock));
if (!CheckCommittee(engine)) return false;

uint index = engine.Snapshot.PersistingBlock.Index + 1;
StorageItem entry = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_GasPerBlock).Add(index), () => new StorageItem(gasPerBlock));
entry.Set(gasPerBlock);
GasRecord gasRecord = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_GasPerBlock)).GetInteroperable<GasRecord>();
if (gasRecord[^1].Index == index)
gasRecord[^1] = (index, gasPerBlock);
else
gasRecord.Add((index, gasPerBlock));
return true;
}

[ContractMethod(0_01000000, CallFlags.AllowStates)]
public BigInteger GetGasPerBlock(StoreView snapshot)
{
var index = snapshot.PersistingBlock.Index;
foreach (var gasRecord in snapshot.Storages.Find(CreateStorageKey(Prefix_GasPerBlock).ToArray())
.Select(u => (index: BitConverter.ToUInt32(u.Key.Key, u.Key.Key.Length - sizeof(uint)), gasPerBlock: (BigInteger)u.Value))
.OrderByDescending(u => u.index))
GasRecord gasRecord = snapshot.Storages[CreateStorageKey(Prefix_GasPerBlock)].GetInteroperable<GasRecord>();
for (var i = gasRecord.Count - 1; i >= 0; i--)
{
if (gasRecord.index <= index)
return gasRecord.gasPerBlock;
if (gasRecord[i].Index <= index)
return gasRecord[i].GasPerBlock;
}
throw new InvalidOperationException();
}
Expand Down Expand Up @@ -325,5 +331,22 @@ public StackItem ToStackItem(ReferenceCounter referenceCounter)
return new Struct(referenceCounter) { Registered, Votes };
}
}

private sealed class GasRecord : List<(uint Index, BigInteger GasPerBlock)>, IInteroperable
{
public void FromStackItem(StackItem stackItem)
{
foreach (StackItem item in (Array)stackItem)
{
Struct @struct = (Struct)item;
Add(((uint)@struct[0].GetInteger(), @struct[1].GetInteger()));
}
}

public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new Array(referenceCounter, this.Select(p => new Struct(referenceCounter, new StackItem[] { p.Index, p.GasPerBlock })));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -618,11 +618,6 @@ public void TestEconomicParameter()
(VM.Types.Boolean, bool) result1 = Check_SetGasPerBlock(snapshot, 10 * NativeContract.GAS.Factor);
result1.Item2.Should().BeTrue();
result1.Item1.GetBoolean().Should().BeTrue();

snapshot.PersistingBlock.Index++;
result = Check_GetGasPerBlock(snapshot);
result.Item2.Should().BeTrue();
result.Item1.Should().Be(10 * NativeContract.GAS.Factor);
}

[TestMethod]
Expand Down

1 comment on commit d1b1887

@shargon
Copy link
Member Author

@shargon shargon commented on d1b1887 Oct 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Miss branch sorry, reverted

Please sign in to comment.