Skip to content

Commit

Permalink
Remove StorageItem.IsConstant (#2351)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Feb 19, 2021
1 parent da622ed commit b1a6ed0
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 121 deletions.
29 changes: 7 additions & 22 deletions src/neo/SmartContract/ApplicationEngine.Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Storage_AsReadOnly = Register("System.Storage.AsReadOnly", nameof(AsReadOnly), 1 << 4, CallFlags.ReadStates);
public static readonly InteropDescriptor System_Storage_Get = Register("System.Storage.Get", nameof(Get), 1 << 15, CallFlags.ReadStates);
public static readonly InteropDescriptor System_Storage_Find = Register("System.Storage.Find", nameof(Find), 1 << 15, CallFlags.ReadStates);
public static readonly InteropDescriptor System_Storage_Put = Register("System.Storage.Put", nameof(Put), 0, CallFlags.States);
public static readonly InteropDescriptor System_Storage_PutEx = Register("System.Storage.PutEx", nameof(PutEx), 0, CallFlags.States);
public static readonly InteropDescriptor System_Storage_Delete = Register("System.Storage.Delete", nameof(Delete), 0, CallFlags.States);
public static readonly InteropDescriptor System_Storage_Put = Register("System.Storage.Put", nameof(Put), 1 << 15, CallFlags.WriteStates);
public static readonly InteropDescriptor System_Storage_Delete = Register("System.Storage.Delete", nameof(Delete), 1 << 15, CallFlags.WriteStates);

protected internal StorageContext GetStorageContext()
{
Expand Down Expand Up @@ -76,16 +75,6 @@ protected internal IIterator Find(StorageContext context, byte[] prefix, FindOpt
}

protected internal void Put(StorageContext context, byte[] key, byte[] value)
{
PutExInternal(context, key, value, StorageFlags.None);
}

protected internal void PutEx(StorageContext context, byte[] key, byte[] value, StorageFlags flags)
{
PutExInternal(context, key, value, flags);
}

private void PutExInternal(StorageContext context, byte[] key, byte[] value, StorageFlags flags)
{
if (key.Length > MaxStorageKeySize || value.Length > MaxStorageValueSize || context.IsReadOnly)
throw new ArgumentException();
Expand All @@ -104,32 +93,28 @@ private void PutExInternal(StorageContext context, byte[] key, byte[] value, Sto
}
else
{
if (item.IsConstant) throw new InvalidOperationException();
if (value.Length == 0)
newDataSize = 1;
newDataSize = 0;
else if (value.Length <= item.Value.Length)
newDataSize = (value.Length - 1) / 4 + 1;
else if (item.Value.Length == 0)
newDataSize = value.Length;
else
newDataSize = (item.Value.Length - 1) / 4 + 1 + value.Length - item.Value.Length;
}
AddGas(newDataSize * StoragePrice);

item.Value = value;
item.IsConstant = flags.HasFlag(StorageFlags.Constant);
}

protected internal void Delete(StorageContext context, byte[] key)
{
if (context.IsReadOnly) throw new ArgumentException();
AddGas(StoragePrice);
StorageKey skey = new StorageKey
Snapshot.Delete(new StorageKey
{
Id = context.Id,
Key = key
};
if (Snapshot.TryGet(skey)?.IsConstant == true)
throw new InvalidOperationException();
Snapshot.Delete(skey);
});
}
}
}
6 changes: 3 additions & 3 deletions src/neo/SmartContract/Native/LedgerContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ internal LedgerContract()

internal override void OnPersist(ApplicationEngine engine)
{
engine.Snapshot.Add(CreateStorageKey(Prefix_BlockHash).AddBigEndian(engine.PersistingBlock.Index), new StorageItem(engine.PersistingBlock.Hash.ToArray(), true));
engine.Snapshot.Add(CreateStorageKey(Prefix_Block).Add(engine.PersistingBlock.Hash), new StorageItem(Trim(engine.PersistingBlock).ToArray(), true));
engine.Snapshot.Add(CreateStorageKey(Prefix_BlockHash).AddBigEndian(engine.PersistingBlock.Index), new StorageItem(engine.PersistingBlock.Hash.ToArray()));
engine.Snapshot.Add(CreateStorageKey(Prefix_Block).Add(engine.PersistingBlock.Hash), new StorageItem(Trim(engine.PersistingBlock).ToArray()));
foreach (Transaction tx in engine.PersistingBlock.Transactions)
{
engine.Snapshot.Add(CreateStorageKey(Prefix_Transaction).Add(tx.Hash), new StorageItem(new TransactionState
{
BlockIndex = engine.PersistingBlock.Index,
Transaction = tx
}, true));
}));
}
}

Expand Down
11 changes: 0 additions & 11 deletions src/neo/SmartContract/StorageFlags.cs

This file was deleted.

18 changes: 5 additions & 13 deletions src/neo/SmartContract/StorageItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ public class StorageItem : ISerializable
{
private byte[] value;
private object cache;
public bool IsConstant;

public int Size => Value.GetVarSize() + sizeof(bool);
public int Size => Value.GetVarSize();

public byte[] Value
{
Expand All @@ -37,22 +36,19 @@ public byte[] Value

public StorageItem() { }

public StorageItem(byte[] value, bool isConstant = false)
public StorageItem(byte[] value)
{
this.value = value;
this.IsConstant = isConstant;
}

public StorageItem(BigInteger value, bool isConstant = false)
public StorageItem(BigInteger value)
{
this.cache = value;
this.IsConstant = isConstant;
}

public StorageItem(IInteroperable interoperable, bool isConstant = false)
public StorageItem(IInteroperable interoperable)
{
this.cache = interoperable;
this.IsConstant = isConstant;
}

public void Add(BigInteger integer)
Expand All @@ -64,21 +60,18 @@ public StorageItem Clone()
{
return new StorageItem
{
Value = Value,
IsConstant = IsConstant
Value = Value
};
}

public void Deserialize(BinaryReader reader)
{
Value = reader.ReadVarBytes();
IsConstant = reader.ReadBoolean();
}

public void FromReplica(StorageItem replica)
{
Value = replica.Value;
IsConstant = replica.IsConstant;
}

public T GetInteroperable<T>() where T : IInteroperable, new()
Expand All @@ -103,7 +96,6 @@ public void FromReplica(StorageItem replica)
public void Serialize(BinaryWriter writer)
{
writer.WriteVarBytes(Value);
writer.Write(IsConstant);
}

public void Set(BigInteger integer)
Expand Down
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/Extensions/NativeContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void DestroyContract(this DataCache snapshot, UInt160 callingScrip
public static void AddContract(this DataCache snapshot, UInt160 hash, ContractState state)
{
var key = new KeyBuilder(NativeContract.ContractManagement.Id, 8).Add(hash);
snapshot.Add(key, new StorageItem(state, false));
snapshot.Add(key, new StorageItem(state));
}

public static void DeleteContract(this DataCache snapshot, UInt160 hash)
Expand Down
8 changes: 3 additions & 5 deletions tests/neo.UnitTests/Ledger/UT_StorageItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public void Value_Set()
public void Size_Get()
{
uut.Value = TestUtils.GetByteArray(10, 0x42);
uut.Size.Should().Be(12); // 2 + 10
uut.Size.Should().Be(11); // 1 + 10
}

[TestMethod]
public void Size_Get_Larger()
{
uut.Value = TestUtils.GetByteArray(88, 0x42);
uut.Size.Should().Be(90); // 2 + 88
uut.Size.Should().Be(89); // 1 + 88
}

[TestMethod]
Expand Down Expand Up @@ -96,7 +96,7 @@ public void Serialize()
}
}

byte[] requiredData = new byte[] { 10, 66, 32, 32, 32, 32, 32, 32, 32, 32, 32, 0 };
byte[] requiredData = new byte[] { 10, 66, 32, 32, 32, 32, 32, 32, 32, 32, 32 };

data.Length.Should().Be(requiredData.Length);
for (int i = 0; i < requiredData.Length; i++)
Expand All @@ -109,11 +109,9 @@ public void Serialize()
public void TestFromReplica()
{
uut.Value = TestUtils.GetByteArray(10, 0x42);
uut.IsConstant = true;
StorageItem dest = new StorageItem();
dest.FromReplica(uut);
dest.Value.Should().BeEquivalentTo(uut.Value);
dest.IsConstant.Should().Be(uut.IsConstant);
}
}
}
2 changes: 0 additions & 2 deletions tests/neo.UnitTests/SmartContract/Native/UT_NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,6 @@ internal static void CheckValidator(ECPoint eCPoint, DataCache.Trackable trackab
st.Should().Be(0);

trackable.Key.Key.Should().BeEquivalentTo(new byte[] { 33 }.Concat(eCPoint.EncodePoint(true)));
trackable.Item.IsConstant.Should().Be(false);
}

internal static void CheckBalance(byte[] account, DataCache.Trackable trackable, BigInteger balance, BigInteger height, ECPoint voteTo)
Expand All @@ -1045,7 +1044,6 @@ internal static void CheckBalance(byte[] account, DataCache.Trackable trackable,
st[2].GetSpan().AsSerializable<ECPoint>().Should().BeEquivalentTo(voteTo); // Votes

trackable.Key.Key.Should().BeEquivalentTo(new byte[] { 20 }.Concat(account));
trackable.Item.IsConstant.Should().Be(false);
}

internal static StorageKey CreateStorageKey(byte prefix, byte[] key = null)
Expand Down
8 changes: 4 additions & 4 deletions tests/neo.UnitTests/SmartContract/UT_InteropPrices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void ApplicationEngineRegularPut()
debugger.StepInto();
var setupPrice = ae.GasConsumed;
debugger.Execute();
(ae.GasConsumed - setupPrice).Should().Be(ae.StoragePrice * (1 + value.Length));
(ae.GasConsumed - setupPrice).Should().Be(ae.StoragePrice * value.Length + (1 << 15) * 30);
}
}

Expand Down Expand Up @@ -99,7 +99,7 @@ public void ApplicationEngineReusedStorage_FullReuse()
debugger.StepInto();
var setupPrice = applicationEngine.GasConsumed;
debugger.Execute();
(applicationEngine.GasConsumed - setupPrice).Should().Be(1 * applicationEngine.StoragePrice);
(applicationEngine.GasConsumed - setupPrice).Should().Be(1 * applicationEngine.StoragePrice + (1 << 15) * 30);
}
}

Expand Down Expand Up @@ -135,7 +135,7 @@ public void ApplicationEngineReusedStorage_PartialReuse()
var setupPrice = ae.GasConsumed;
debugger.StepInto();
debugger.StepInto();
(ae.GasConsumed - setupPrice).Should().Be((1 + (oldValue.Length / 4) + value.Length - oldValue.Length) * ae.StoragePrice);
(ae.GasConsumed - setupPrice).Should().Be((1 + (oldValue.Length / 4) + value.Length - oldValue.Length) * ae.StoragePrice + (1 << 15) * 30);
}
}

Expand Down Expand Up @@ -174,7 +174,7 @@ public void ApplicationEngineReusedStorage_PartialReuseTwice()
debugger.StepInto(); //syscall Storage.GetContext
var setupPrice = ae.GasConsumed;
debugger.StepInto(); //syscall Storage.Put
(ae.GasConsumed - setupPrice).Should().Be((sItem.Value.Length / 4 + 1) * ae.StoragePrice); // = PUT basic fee
(ae.GasConsumed - setupPrice).Should().Be((sItem.Value.Length / 4 + 1) * ae.StoragePrice + (1 << 15) * 30); // = PUT basic fee
}
}

Expand Down
6 changes: 2 additions & 4 deletions tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ public void TestContract_Update()

var storageItem = new StorageItem
{
Value = new byte[] { 0x01 },
IsConstant = false
Value = new byte[] { 0x01 }
};

var storageKey = new StorageKey
Expand Down Expand Up @@ -259,8 +258,7 @@ public void TestStorage_Find()

var storageItem = new StorageItem
{
Value = new byte[] { 0x01, 0x02, 0x03, 0x04 },
IsConstant = true
Value = new byte[] { 0x01, 0x02, 0x03, 0x04 }
};
var storageKey = new StorageKey
{
Expand Down
45 changes: 4 additions & 41 deletions tests/neo.UnitTests/SmartContract/UT_InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,7 @@ public void TestStorage_Get()

var storageItem = new StorageItem
{
Value = new byte[] { 0x01, 0x02, 0x03, 0x04 },
IsConstant = true
Value = new byte[] { 0x01, 0x02, 0x03, 0x04 }
};
snapshot.AddContract(state.Hash, state);
snapshot.Add(storageKey, storageItem);
Expand Down Expand Up @@ -471,8 +470,7 @@ public void TestStorage_Put()
};
var storageItem = new StorageItem
{
Value = new byte[] { 0x01, 0x02, 0x03, 0x04 },
IsConstant = true
Value = new byte[] { 0x01, 0x02, 0x03, 0x04 }
};
snapshot.AddContract(state.Hash, state);
snapshot.Add(storageKey, storageItem);
Expand All @@ -481,10 +479,6 @@ public void TestStorage_Put()
key = new byte[] { 0x01 };
value = new byte[] { 0x02 };
storageContext.IsReadOnly = false;
Assert.ThrowsException<InvalidOperationException>(() => engine.Put(storageContext, key, value));

//success
storageItem.IsConstant = false;
engine.Put(storageContext, key, value);

//value length == 0
Expand All @@ -493,35 +487,6 @@ public void TestStorage_Put()
engine.Put(storageContext, key, value);
}

[TestMethod]
public void TestStorage_PutEx()
{
var snapshot = TestBlockchain.GetTestSnapshot();
var state = TestUtils.GetContract();
var storageKey = new StorageKey
{
Id = 0x42000000,
Key = new byte[] { 0x01 }
};
var storageItem = new StorageItem
{
Value = new byte[] { 0x01, 0x02, 0x03, 0x04 },
IsConstant = false
};
snapshot.AddContract(state.Hash, state);
snapshot.Add(storageKey, storageItem);
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
engine.LoadScript(new byte[] { 0x01 });
var key = new byte[] { 0x01 };
var value = new byte[] { 0x02 };
var storageContext = new StorageContext
{
Id = state.Id,
IsReadOnly = false
};
engine.PutEx(storageContext, key, value, StorageFlags.None);
}

[TestMethod]
public void TestStorage_Delete()
{
Expand All @@ -535,8 +500,7 @@ public void TestStorage_Delete()
};
var storageItem = new StorageItem
{
Value = new byte[] { 0x01, 0x02, 0x03, 0x04 },
IsConstant = false
Value = new byte[] { 0x01, 0x02, 0x03, 0x04 }
};
snapshot.AddContract(state.Hash, state);
snapshot.Add(storageKey, storageItem);
Expand Down Expand Up @@ -601,8 +565,7 @@ public void TestContract_Destroy()
var scriptHash = UInt160.Parse("0xcb9f3b7c6fb1cf2c13a40637c189bdd066a272b4");
var storageItem = new StorageItem
{
Value = new byte[] { 0x01, 0x02, 0x03, 0x04 },
IsConstant = false
Value = new byte[] { 0x01, 0x02, 0x03, 0x04 }
};

var storageKey = new StorageKey
Expand Down
6 changes: 3 additions & 3 deletions tests/neo.UnitTests/SmartContract/UT_SmartContractHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,14 @@ public static void TransactionAdd(DataCache snapshot, params TransactionState[]
{
foreach (TransactionState tx in txs)
{
snapshot.Add(NativeContract.Ledger.CreateStorageKey(Prefix_Transaction, tx.Transaction.Hash), new StorageItem(tx, true));
snapshot.Add(NativeContract.Ledger.CreateStorageKey(Prefix_Transaction, tx.Transaction.Hash), new StorageItem(tx));
}
}

public static void BlocksAdd(DataCache snapshot, UInt256 hash, TrimmedBlock block)
{
snapshot.Add(NativeContract.Ledger.CreateStorageKey(Prefix_BlockHash, block.Index), new StorageItem(hash.ToArray(), true));
snapshot.Add(NativeContract.Ledger.CreateStorageKey(Prefix_Block, hash), new StorageItem(block.ToArray(), true));
snapshot.Add(NativeContract.Ledger.CreateStorageKey(Prefix_BlockHash, block.Index), new StorageItem(hash.ToArray()));
snapshot.Add(NativeContract.Ledger.CreateStorageKey(Prefix_Block, hash), new StorageItem(block.ToArray()));
}
}
}
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/SmartContract/UT_Syscalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void System_Blockchain_GetBlock()
{
BlockIndex = block.Index,
Transaction = tx
}, true));
}));

engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings);
engine.LoadScript(script.ToArray());
Expand Down
Loading

0 comments on commit b1a6ed0

Please sign in to comment.