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

New: add more storagemap get methods #835

Merged
merged 10 commits into from
Dec 26, 2023
212 changes: 207 additions & 5 deletions src/Neo.SmartContract.Framework/Services/StorageMap.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (C) 2015-2023 The Neo Project.
//
// The Neo.SmartContract.Framework is free software distributed under the MIT
// software license, see the accompanying file LICENSE in the main directory
// of the project or http://www.opensource.org/licenses/mit-license.php
//
// The Neo.SmartContract.Framework is free software distributed under the MIT
// software license, see the accompanying file LICENSE in the main directory
// of the project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

Expand Down Expand Up @@ -114,6 +114,89 @@ public class StorageMap
[Syscall("System.Storage.Get")]
public extern ByteString Get(ByteString key);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
public extern ByteString GetUInt160(ByteString key);
Jim8y marked this conversation as resolved.
Show resolved Hide resolved

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
public extern ByteString GetUInt256(ByteString key);
Jim8y marked this conversation as resolved.
Show resolved Hide resolved

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
public extern ByteString GetECPoint(ByteString key);
Jim8y marked this conversation as resolved.
Show resolved Hide resolved

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
[OpCode(OpCode.CONVERT, StackItemType.Buffer)]
public extern byte[] GetByteArray(ByteString key);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
public extern ByteString GetString(ByteString key);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
[OpCode(OpCode.CONVERT, StackItemType.Integer)]
public extern int GetInteger(ByteString key);
shargon marked this conversation as resolved.
Show resolved Hide resolved

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
[OpCode(OpCode.NOT)]
[OpCode(OpCode.NOT)]
public extern bool GetBoolean(ByteString key);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
[OpCode(OpCode.DUP)]
[OpCode(OpCode.ISNULL)]
[OpCode(OpCode.JMPIFNOT, "0x06")]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.PUSH0)]
[OpCode(OpCode.JMP, "0x04")]
[OpCode(OpCode.CONVERT, StackItemType.Integer)]
public extern int GetIntegerOrZero(ByteString key);
Jim8y marked this conversation as resolved.
Show resolved Hide resolved

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
Expand All @@ -123,6 +206,89 @@ public class StorageMap
[Syscall("System.Storage.Get")]
public extern ByteString Get(byte[] key);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
public extern ByteString GetUInt160(byte[] key);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
public extern ByteString GetUInt256(byte[] key);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
public extern ByteString GetECPoint(byte[] key);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
[OpCode(OpCode.CONVERT, StackItemType.Buffer)]
public extern byte[] GetByteArray(byte[] key);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
public extern ByteString GetString(byte[] key);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
[OpCode(OpCode.CONVERT, StackItemType.Integer)]
public extern int GetInteger(byte[] key);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
[OpCode(OpCode.NOT)]
[OpCode(OpCode.NOT)]
public extern bool GetBoolean(byte[] key);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Get")]
[OpCode(OpCode.DUP)]
[OpCode(OpCode.ISNULL)]
[OpCode(OpCode.JMPIFNOT, "0x06")]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.PUSH0)]
[OpCode(OpCode.JMP, "0x04")]
[OpCode(OpCode.CONVERT, StackItemType.Integer)]
public extern int GetIntegerOrZero(byte[] key);

public object GetObject(ByteString key)
{
ByteString value = Get(key);
Expand Down Expand Up @@ -197,6 +363,42 @@ public object GetObject(byte[] key)
[Syscall("System.Storage.Put")]
public extern void Put(byte[] key, BigInteger value);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Put")]
public extern void Put(ByteString key, bool value);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Put")]
public extern void Put(byte[] key, bool value);
Copy link
Member

Choose a reason for hiding this comment

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

Considering that opcodes were equal, weren't it be better if aggregated? Maybe there is a simple way for such cases.


[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Put")]
public extern void Put(ByteString key, byte[] value);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.REVERSE3)]
[OpCode(OpCode.CAT)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Storage.Put")]
public extern void Put(byte[] key, byte[] value);

public void PutObject(ByteString key, object value)
{
Put(key, StdLib.Serialize(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,28 @@ public void Test_Index()
Assert.AreEqual(VM.VMState.HALT, testengine.State);
Assert.AreEqual(value, result.Pop().GetString());
}

[TestMethod]
public void Test_NewGetMethods()
{
testengine.Reset();
var result = testengine.ExecuteTestCaseStandard("testNewGetMethods");
Assert.AreEqual(1, result.Count);
Assert.AreEqual(VM.VMState.HALT, testengine.State);
Assert.AreEqual(true, result.Pop().GetBoolean());
}

[TestMethod]
public void Test_NewGetByteArray()
{
testengine.Reset();
var result = testengine.ExecuteTestCaseStandard("testNewGetByteArray");
var testArr = new byte[] { 0x00, 0x01 };
Assert.AreEqual(1, result.Count);
var res = result.Pop().GetSpan().ToArray();
Assert.AreEqual(VM.VMState.HALT, testengine.State);
Assert.AreEqual(testArr[0], res[0]);
Assert.AreEqual(testArr[1], res[1]);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Neo.SmartContract.Framework.Services;
using Neo.SmartContract.Framework;

namespace Neo.SmartContract.Framework.UnitTests.TestClasses
{

public class Contract_Storage : SmartContract
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
// There is no main here, it can be auto generation.
Expand Down Expand Up @@ -93,6 +95,48 @@ public static byte[] TestGetByteArray(byte[] key)
return (byte[])value;
}

public static bool TestNewGetMethods()
{
var prefix = new byte[] { 0x00, 0xFF };
var context = Storage.CurrentContext;
var storage = new StorageMap(context, prefix);

var boolValue = true;
var intValue = 123;
var stringValue = "hello world";
var uint160Value = UInt160.Zero;
var uint256Value = UInt256.Zero;

storage.Put("bool", boolValue);
storage.Put("int", intValue);
storage.Put("string", stringValue);
storage.Put("uint160", uint160Value);
storage.Put("uint256", uint256Value);

var boolValue2 = storage.GetBoolean("bool");
var intValue2 = storage.GetInteger("int");
var stringValue2 = storage.GetString("string");
var uint160Value2 = storage.GetUInt160("uint160");
var uint256Value2 = storage.GetUInt256("uint256");

return boolValue == boolValue2
&& intValue == intValue2
&&stringValue == stringValue2
&& uint160Value == uint160Value2
&& uint256Value == uint256Value2;
}

public static byte[] TestNewGetByteArray()
{
var prefix = new byte[] { 0x00, 0xFF };
var context = Storage.CurrentContext;
var storage = new StorageMap(context, prefix);
var byteArray = new byte[] { 0x00, 0x01 };
storage.Put("byteArray", byteArray);
var byteArray2 = storage.GetByteArray("byteArray");
return byteArray2;
}

#endregion

public static bool TestPutReadOnly(byte[] key, byte[] value)
Expand Down