Skip to content
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

Adjust the Charge for Storage Writting #1441

Merged
merged 60 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
805d370
Rebase
lock9 Dec 16, 2019
21ced64
Refactor, more tests
lock9 Dec 17, 2019
a376c48
Discarding changes
lock9 Dec 17, 2019
0239d04
Fixes, tests and refactor
lock9 Dec 19, 2019
92b256a
Calculating final fee during onPersist
lock9 Dec 19, 2019
26c50fa
Rebase
lock9 Dec 19, 2019
1fb19e4
dotnet format + missing change
lock9 Dec 19, 2019
daa4e2b
Minor improvements
lock9 Dec 19, 2019
42c6dd3
fix
lock9 Dec 19, 2019
c2aa683
Removing empty lines
lock9 Dec 19, 2019
cb33f47
add comment
eryeer Feb 6, 2020
50762dc
merge conflict
eryeer Feb 6, 2020
6238206
remove checkStorageContext
eryeer Feb 6, 2020
a600683
update ut
eryeer Feb 6, 2020
b5b0fb4
revert modification
eryeer Feb 7, 2020
96aca15
revert
eryeer Feb 7, 2020
5074592
add recycle reward gas
eryeer Feb 7, 2020
764c67d
update
eryeer Feb 7, 2020
54efe6a
fix ut
eryeer Feb 7, 2020
3174c0d
fix
eryeer Feb 7, 2020
51643d7
add OnRecycleRewardGas test
eryeer Feb 7, 2020
7131999
optimise
eryeer Feb 10, 2020
b5dac3d
update
eryeer Feb 10, 2020
1da5ebf
optimise
eryeer Feb 11, 2020
3996853
Merge branch 'master' into storage-payback
eryeer Feb 11, 2020
dfc3d7a
Removing extra space
vncoelho Feb 11, 2020
a856d44
update comment
eryeer Feb 12, 2020
ddac1f5
update static
eryeer Feb 12, 2020
5c61cbd
format
eryeer Feb 12, 2020
cbb8531
revert virtual
eryeer Feb 14, 2020
c661c41
format
eryeer Feb 14, 2020
1bf3815
update
eryeer Feb 18, 2020
5be0d71
remove ThrowIfNot
eryeer Feb 18, 2020
3d21376
update TryGet and engine notify
eryeer Feb 19, 2020
938af95
format
eryeer Feb 19, 2020
c39bb89
format
eryeer Feb 19, 2020
b8a6824
Update ApplicationEngine.cs
shargon Feb 19, 2020
ca9ab68
update mint
eryeer Feb 20, 2020
1196f86
Merge branch 'storage-payback' of github.com:eryeer/neoUT into storag…
eryeer Feb 20, 2020
d233346
format
eryeer Feb 20, 2020
e7c5d23
Fix typo
shargon Feb 20, 2020
e567bd3
Simplify
shargon Feb 20, 2020
45eaf5e
Merge branch 'master' into storage-payback
eryeer Feb 21, 2020
8eb8cf4
Merge branch 'master' into storage-payback
eryeer Feb 27, 2020
ed1d0d6
Merge branch 'master' into storage-payback
eryeer Feb 28, 2020
7134701
reset, charge only for increment of data, add put basic fee
Mar 3, 2020
1cd0e25
fix ut
Mar 3, 2020
7b0ad73
fix ut
Mar 3, 2020
d638d5c
Merge branch 'master' into storage-payback
Mar 4, 2020
0fe0c2e
Clean
shargon Mar 4, 2020
0e0b63c
add throw in GetStoragePrice if non-interface
Mar 5, 2020
bec4313
Merge branch 'storage-payback' of https://github.com/eryeer/neoUT int…
Mar 5, 2020
a4893df
fix ut
Mar 5, 2020
8acfe41
Merge branch 'master' into storage-payback
eryeer Mar 10, 2020
ffad20a
fix GetPrice parameters
Mar 11, 2020
c9e6b7a
Merge branch 'master' into storage-payback
Mar 12, 2020
2b1939e
Update InteropService.Storage.cs
erikzhang Mar 12, 2020
d683efd
add check: skeyValue.Value is null
Mar 12, 2020
073849f
reset, fix ut
Mar 12, 2020
5242131
adjust Storage.Delete price = 1 * GasPerByte
Mar 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public partial class ApplicationEngine : ExecutionEngine
public IVerifiable ScriptContainer { get; }
public StoreView Snapshot { get; }
public long GasConsumed { get; private set; } = 0;

public UInt160 CurrentScriptHash => CurrentContext?.GetState<ExecutionContextState>().ScriptHash;
public UInt160 CallingScriptHash => CurrentContext?.GetState<ExecutionContextState>().CallingScriptHash;
public UInt160 EntryScriptHash => EntryContext?.GetState<ExecutionContextState>().ScriptHash;
Expand Down Expand Up @@ -78,7 +79,7 @@ public override void Dispose()

protected override bool OnSysCall(uint method)
{
if (!AddGas(InteropService.GetPrice(method, CurrentContext.EvaluationStack)))
if (!AddGas(InteropService.GetPrice(method, this)))
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
return false;
return InteropService.Invoke(this, method);
}
Expand Down
14 changes: 10 additions & 4 deletions src/neo/SmartContract/InteropDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class InteropDescriptor
public uint Hash { get; }
internal Func<ApplicationEngine, bool> Handler { get; }
public long Price { get; }
public Func<EvaluationStack, long> PriceCalculator { get; }
public Func<ApplicationEngine, long> PriceCalculator { get; }
public TriggerType AllowedTriggers { get; }
public CallFlags RequiredCallFlags { get; }

Expand All @@ -19,12 +19,18 @@ internal InteropDescriptor(string method, Func<ApplicationEngine, bool> handler,
this.Price = price;
}

internal InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, Func<EvaluationStack, long> priceCalculator, TriggerType allowedTriggers, CallFlags requiredCallFlags)
internal InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, Func<ApplicationEngine, long> priceCalculator, TriggerType allowedTriggers, CallFlags requiredCallFlags)
: this(method, handler, allowedTriggers, requiredCallFlags)
{
this.PriceCalculator = priceCalculator;
}

internal InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, Func<EvaluationStack, long> priceCalculator, TriggerType allowedTriggers, CallFlags requiredCallFlags)
: this(method, handler, allowedTriggers, requiredCallFlags)
{
this.PriceCalculator = (engine) => priceCalculator(engine.CurrentContext.EvaluationStack);
}

private InteropDescriptor(string method, Func<ApplicationEngine, bool> handler, TriggerType allowedTriggers, CallFlags requiredCallFlags)
{
this.Method = method;
Expand All @@ -34,9 +40,9 @@ private InteropDescriptor(string method, Func<ApplicationEngine, bool> handler,
this.RequiredCallFlags = requiredCallFlags;
}

public long GetPrice(EvaluationStack stack)
public long GetPrice(ApplicationEngine applicationEngine)
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
{
return PriceCalculator is null ? Price : PriceCalculator(stack);
return PriceCalculator is null ? Price : PriceCalculator(applicationEngine);
}

public static implicit operator uint(InteropDescriptor descriptor)
Expand Down
29 changes: 27 additions & 2 deletions src/neo/SmartContract/InteropService.Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,34 @@ public static class Storage
public static readonly InteropDescriptor PutEx = Register("System.Storage.PutEx", Storage_PutEx, GetStoragePrice, TriggerType.Application, CallFlags.AllowModifyStates);
public static readonly InteropDescriptor Delete = Register("System.Storage.Delete", Storage_Delete, 0_01000000, TriggerType.Application, CallFlags.AllowModifyStates);

private static long GetStoragePrice(EvaluationStack stack)
private static long GetStoragePrice(ApplicationEngine engine)
{
return (stack.Peek(1).GetByteLength() + stack.Peek(2).GetByteLength()) * GasPerByte;
var stack = engine.CurrentContext.EvaluationStack;
var key = stack.Peek(1);
var value = stack.Peek(2);
var newDataSize = value.IsNull ? 0 : value.GetByteLength();
if (!(engine.CurrentContext.EvaluationStack.Peek() is InteropInterface _interface))
throw new InvalidOperationException();

StorageContext context = _interface.GetInterface<StorageContext>();
StorageKey skey = new StorageKey
{
Id = context.Id,
Key = key.GetSpan().ToArray()
};
var skeyValue = engine.Snapshot.Storages.TryGet(skey);
if (skeyValue is null || skeyValue.Value is null || skeyValue.Value.Length == 0)
return (key.GetByteLength() + newDataSize) * GasPerByte;

var currentOccupiedBytes = skeyValue.Value.Length;
if (newDataSize <= currentOccupiedBytes)
{
return 1 * GasPerByte;
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
return (newDataSize - currentOccupiedBytes) * GasPerByte;
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
}
}

private static bool PutExInternal(ApplicationEngine engine, StorageContext context, byte[] key, byte[] value, StorageFlags flags)
Expand Down
11 changes: 9 additions & 2 deletions src/neo/SmartContract/InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ static InteropService()
t.GetFields()[0].GetValue(null);
}

public static long GetPrice(uint hash, EvaluationStack stack)
public static long GetPrice(uint hash, ApplicationEngine applicationEngine)
{
return methods[hash].GetPrice(stack);
return methods[hash].GetPrice(applicationEngine);
eryeer marked this conversation as resolved.
Show resolved Hide resolved
}

public static IEnumerable<InteropDescriptor> SupportedMethods()
Expand Down Expand Up @@ -50,5 +50,12 @@ private static InteropDescriptor Register(string method, Func<ApplicationEngine,
methods.Add(descriptor.Hash, descriptor);
return descriptor;
}

private static InteropDescriptor Register(string method, Func<ApplicationEngine, bool> handler, Func<ApplicationEngine, long> priceCalculator, TriggerType allowedTriggers, CallFlags requiredCallFlags)
{
InteropDescriptor descriptor = new InteropDescriptor(method, handler, priceCalculator, allowedTriggers, requiredCallFlags);
methods.Add(descriptor.Hash, descriptor);
return descriptor;
}
}
}
206 changes: 200 additions & 6 deletions tests/neo.UnitTests/SmartContract/UT_InteropPrices.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Ledger;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.VM;
using System;

namespace Neo.UnitTests.SmartContract
{
[TestClass]
public class UT_InteropPrices
{
[TestInitialize]
public void Initialize()
{
TestBlockchain.InitializeMockNeoSystem();
}

[TestMethod]
public void ApplicationEngineFixedPrices()
{
Expand All @@ -16,23 +25,23 @@ public void ApplicationEngineFixedPrices()
using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, null, 0))
{
ae.LoadScript(SyscallSystemRuntimeCheckWitnessHash);
InteropService.GetPrice(InteropService.Runtime.CheckWitness, ae.CurrentContext.EvaluationStack).Should().Be(0_00030000L);
InteropService.GetPrice(InteropService.Runtime.CheckWitness, ae).Should().Be(0_00030000L);
}

// System.Storage.GetContext: 9bf667ce (price is 1)
byte[] SyscallSystemStorageGetContextHash = new byte[] { 0x68, 0x9b, 0xf6, 0x67, 0xce };
using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, null, 0))
{
ae.LoadScript(SyscallSystemStorageGetContextHash);
InteropService.GetPrice(InteropService.Storage.GetContext, ae.CurrentContext.EvaluationStack).Should().Be(0_00000400L);
InteropService.GetPrice(InteropService.Storage.GetContext, ae).Should().Be(0_00000400L);
}

// System.Storage.Get: 925de831 (price is 100)
byte[] SyscallSystemStorageGetHash = new byte[] { 0x68, 0x92, 0x5d, 0xe8, 0x31 };
using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, null, 0))
{
ae.LoadScript(SyscallSystemStorageGetHash);
InteropService.GetPrice(InteropService.Storage.Get, ae.CurrentContext.EvaluationStack).Should().Be(0_01000000L);
InteropService.GetPrice(InteropService.Storage.Get, ae).Should().Be(0_01000000L);
}
}

Expand All @@ -47,7 +56,7 @@ public void ApplicationEngineVariablePrices()
ae.LoadScript(SyscallContractCreateHash00);
debugger.StepInto(); // PUSHDATA1
debugger.StepInto(); // PUSHDATA1
InteropService.GetPrice(InteropService.Contract.Create, ae.CurrentContext.EvaluationStack).Should().Be(0_00300000L);
InteropService.GetPrice(InteropService.Contract.Create, ae).Should().Be(0_00300000L);
}

// System.Storage.Put: e63f1884 (requires push key and value)
Expand All @@ -59,7 +68,8 @@ public void ApplicationEngineVariablePrices()
debugger.StepInto(); // push 03 (length 1)
debugger.StepInto(); // push 03 (length 1)
debugger.StepInto(); // push 00
InteropService.GetPrice(InteropService.Storage.Put, ae.CurrentContext.EvaluationStack).Should().Be(200000L);
Action act = () => InteropService.GetPrice(InteropService.Storage.Put, ae);
act.Should().Throw<InvalidOperationException>();
}

// System.Storage.PutEx: 73e19b3a (requires push key and value)
Expand All @@ -71,8 +81,192 @@ public void ApplicationEngineVariablePrices()
debugger.StepInto(); // push 03 (length 1)
debugger.StepInto(); // push 03 (length 1)
debugger.StepInto(); // push 00
InteropService.GetPrice(InteropService.Storage.PutEx, ae.CurrentContext.EvaluationStack).Should().Be(200000L);
Action act = () => InteropService.GetPrice(InteropService.Storage.Put, ae);
act.Should().Throw<InvalidOperationException>();
}
}

/// <summary>
/// Put without previous content (should charge per byte used)
/// </summary>
[TestMethod]
public void ApplicationEngineRegularPut()
{
var key = new byte[] { (byte)OpCode.PUSH1 };
var value = new byte[] { (byte)OpCode.PUSH1 };

byte[] script = CreatePutScript(key, value);

ContractState contractState = TestUtils.GetContract(script);
contractState.Manifest.Features = ContractFeatures.HasStorage;

StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key);
StorageItem sItem = TestUtils.GetStorageItem(null);

var snapshot = Blockchain.Singleton.GetSnapshot();
snapshot.Storages.Add(skey, sItem);
snapshot.Contracts.Add(script.ToScriptHash(), contractState);

using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, testMode: true))
{
Debugger debugger = new Debugger(ae);
ae.LoadScript(script);
debugger.StepInto();
debugger.StepInto();
debugger.StepInto();
var setupPrice = ae.GasConsumed;
var defaultDataPrice = InteropService.GetPrice(InteropService.Storage.Put, ae);
defaultDataPrice.Should().Be(InteropService.Storage.GasPerByte * (key.Length + value.Length));
var expectedCost = defaultDataPrice + setupPrice;
debugger.Execute();
ae.GasConsumed.Should().Be(expectedCost);
}
}

/// <summary>
/// Reuses the same amount of storage. Should cost 0.
/// </summary>
[TestMethod]
public void ApplicationEngineReusedStorage_FullReuse()
{
var key = new byte[] { (byte)OpCode.PUSH1 };
var value = new byte[] { (byte)OpCode.PUSH1 };

byte[] script = CreatePutScript(key, value);

ContractState contractState = TestUtils.GetContract(script);
contractState.Manifest.Features = ContractFeatures.HasStorage;

StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key);
StorageItem sItem = TestUtils.GetStorageItem(value);

var snapshot = Blockchain.Singleton.GetSnapshot();
snapshot.Storages.Add(skey, sItem);
snapshot.Contracts.Add(script.ToScriptHash(), contractState);

using (ApplicationEngine applicationEngine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, testMode: true))
{
Debugger debugger = new Debugger(applicationEngine);
applicationEngine.LoadScript(script);
debugger.StepInto();
debugger.StepInto();
debugger.StepInto();
var setupPrice = applicationEngine.GasConsumed;
var reusedDataPrice = InteropService.GetPrice(InteropService.Storage.Put, applicationEngine);
reusedDataPrice.Should().Be(1 * InteropService.Storage.GasPerByte);
debugger.Execute();
var expectedCost = reusedDataPrice + setupPrice;
applicationEngine.GasConsumed.Should().Be(expectedCost);
}
}

/// <summary>
/// Reuses one byte and allocates a new one
/// It should only pay for the second byte.
/// </summary>
[TestMethod]
public void ApplicationEngineReusedStorage_PartialReuse()
{
var key = new byte[] { (byte)OpCode.PUSH1 };
var oldValue = new byte[] { (byte)OpCode.PUSH1 };
var value = new byte[] { (byte)OpCode.PUSH1, (byte)OpCode.PUSH1 };

byte[] script = CreatePutScript(key, value);

ContractState contractState = TestUtils.GetContract(script);
contractState.Manifest.Features = ContractFeatures.HasStorage;

StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key);
StorageItem sItem = TestUtils.GetStorageItem(oldValue);

var snapshot = Blockchain.Singleton.GetSnapshot();
snapshot.Storages.Add(skey, sItem);
snapshot.Contracts.Add(script.ToScriptHash(), contractState);

using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, testMode: true))
{
Debugger debugger = new Debugger(ae);
ae.LoadScript(script);
debugger.StepInto();
debugger.StepInto();
debugger.StepInto();
var setupPrice = ae.GasConsumed;
var reusedDataPrice = InteropService.GetPrice(InteropService.Storage.Put, ae);
reusedDataPrice.Should().Be(1 * InteropService.Storage.GasPerByte);
debugger.StepInto();
var expectedCost = reusedDataPrice + setupPrice;
debugger.StepInto();
ae.GasConsumed.Should().Be(expectedCost);
}
}

/// <summary>
/// Use put for the same key twice.
/// Pays for 1 extra byte for the first Put and 1 byte for the second basic fee (as value2.length == value1.length).
/// </summary>
[TestMethod]
public void ApplicationEngineReusedStorage_PartialReuseTwice()
{
var key = new byte[] { (byte)OpCode.PUSH1 };
var oldValue = new byte[] { (byte)OpCode.PUSH1 };
var value = new byte[] { (byte)OpCode.PUSH1, (byte)OpCode.PUSH1 };

byte[] script = CreateMultiplePutScript(key, value);

ContractState contractState = TestUtils.GetContract(script);
contractState.Manifest.Features = ContractFeatures.HasStorage;

StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key);
StorageItem sItem = TestUtils.GetStorageItem(oldValue);

var snapshot = Blockchain.Singleton.GetSnapshot();
snapshot.Storages.Add(skey, sItem);
snapshot.Contracts.Add(script.ToScriptHash(), contractState);

using (ApplicationEngine ae = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, testMode: true))
{
Debugger debugger = new Debugger(ae);
ae.LoadScript(script);
debugger.StepInto(); //push key
debugger.StepInto(); //push value
debugger.StepInto(); //syscall Storage.GetContext
var setupPrice = ae.GasConsumed;
var incrementDataPrice = InteropService.GetPrice(InteropService.Storage.Put, ae);
incrementDataPrice.Should().Be(1 * InteropService.Storage.GasPerByte);
debugger.StepInto(); // syscall Storage.Put

debugger.StepInto(); //push key
debugger.StepInto(); //push value
debugger.StepInto();
setupPrice = ae.GasConsumed;
var reusedDataPrice = InteropService.GetPrice(InteropService.Storage.Put, ae);
reusedDataPrice.Should().Be(1 * InteropService.Storage.GasPerByte); // = PUT basic fee
}
}

private byte[] CreateMultiplePutScript(byte[] key, byte[] value, int times = 2)
{
var scriptBuilder = new ScriptBuilder();

for (int i = 0; i < times; i++)
{
scriptBuilder.EmitPush(value);
scriptBuilder.EmitPush(key);
scriptBuilder.EmitSysCall(InteropService.Storage.GetContext);
scriptBuilder.EmitSysCall(InteropService.Storage.Put);
}

return scriptBuilder.ToArray();
}

private byte[] CreatePutScript(byte[] key, byte[] value)
{
var scriptBuilder = new ScriptBuilder();
scriptBuilder.EmitPush(value);
scriptBuilder.EmitPush(key);
scriptBuilder.EmitSysCall(InteropService.Storage.GetContext);
scriptBuilder.EmitSysCall(InteropService.Storage.Put);
return scriptBuilder.ToArray();
}
}
}
Loading