Skip to content

Commit

Permalink
New: add more storagemap get methods (#835)
Browse files Browse the repository at this point in the history
* add more storagemap get methods

* Update tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Storage.cs

* fix return type

* ECPoint and BigInteger

* add ECPoint

* Fix ut

---------

Co-authored-by: Shargon <shargon@gmail.com>
  • Loading branch information
Jim8y and shargon committed Dec 26, 2023
1 parent 93003c1 commit 2e95da7
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 5 deletions.
213 changes: 208 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 All @@ -15,6 +15,7 @@
using Neo.SmartContract.Framework.Native;
using System.Numerics;
using System.Runtime.InteropServices;
using Neo.Cryptography.ECC;

namespace Neo.SmartContract.Framework.Services
{
Expand Down Expand Up @@ -114,6 +115,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 UInt160 GetUInt160(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 UInt256 GetUInt256(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 ECPoint GetECPoint(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.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 BigInteger GetInteger(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.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 BigInteger GetIntegerOrZero(ByteString key);

[CallingConvention(CallingConvention.Cdecl)]
[OpCode(OpCode.UNPACK)]
[OpCode(OpCode.DROP)]
Expand All @@ -123,6 +207,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 UInt160 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 UInt256 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 ECPoint 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 BigInteger 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 BigInteger GetIntegerOrZero(byte[] key);

public object GetObject(ByteString key)
{
ByteString value = Get(key);
Expand Down Expand Up @@ -197,6 +364,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);

[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,4 +1,6 @@
using Neo.Cryptography.ECC;
using Neo.SmartContract.Framework.Services;
using Neo.SmartContract.Framework;

namespace Neo.SmartContract.Framework.UnitTests.TestClasses
{
Expand Down Expand Up @@ -93,6 +95,65 @@ 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)new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09
};
var uint256Value = (UInt256) new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01
};
var ecPointValue = (ECPoint)new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02
};

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

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");
var ecPointValue2 = storage.GetECPoint("ecpoint");

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

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

0 comments on commit 2e95da7

Please sign in to comment.