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

[Neo Core Storage] Implicit methods and tests #3403

Merged
merged 4 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions src/Neo/SmartContract/StorageItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,15 @@ public static implicit operator BigInteger(StorageItem item)
item.cache ??= new BigInteger(item.value.Span);
return (BigInteger)item.cache;
}

public static implicit operator StorageItem(BigInteger value)
Copy link
Member

Choose a reason for hiding this comment

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

I'm just wondering, do you have a use-case for these conversion operators in the code? Usually when we introduce such helpers, there's a need to use them in the code and we replace some real code with these helpers.

Copy link
Member

@cschuchardt88 cschuchardt88 Jul 11, 2024

Choose a reason for hiding this comment

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

Doesn't matter how it done. As long as the do the same thing. We have direct access to the class. So, no need for extension methods. Plus this doesn't convert to BigInteger its allows us to compare the two.

Copy link
Contributor Author

@Jim8y Jim8y Jul 11, 2024

Choose a reason for hiding this comment

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

I'm just wondering, do you have a use-case for these conversion operators in the code? Usually when we introduce such helpers, there's a need to use them in the code and we replace some real code with these helpers.

Does it matter ?

{
return new StorageItem(value);
}

public static implicit operator StorageItem(byte[] value)
{
return new StorageItem(value);
}
}
}
10 changes: 10 additions & 0 deletions src/Neo/SmartContract/StorageKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Neo.Cryptography;
using System;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;

namespace Neo.SmartContract
{
Expand Down Expand Up @@ -79,5 +80,14 @@ public byte[] ToArray()
}
return cache;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator StorageKey(byte[] value) => new StorageKey(value);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator StorageKey(ReadOnlyMemory<byte> value) => new StorageKey(value.Span.ToArray());

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator StorageKey(ReadOnlySpan<byte> value) => new StorageKey(value.ToArray());
}
}
74 changes: 74 additions & 0 deletions tests/Neo.UnitTests/SmartContract/UT_Storage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// UT_Storage.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository 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.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Linq;
using System.Numerics;

namespace Neo.UnitTests.SmartContract
{
[TestClass]
public class UT_Storage
{
[TestMethod]
public void TestStorageKey()
{
// Test data
byte[] keyData = [0x00, 0x00, 0x00, 0x00, 0x12];
var keyMemory = new ReadOnlyMemory<byte>(keyData);

// Test implicit conversion from byte[] to StorageKey
StorageKey storageKeyFromArray = keyData;
Assert.AreEqual(0, storageKeyFromArray.Id);
Assert.IsTrue(keyMemory.Span.ToArray().Skip(sizeof(int)).SequenceEqual(storageKeyFromArray.Key.Span.ToArray()));

// Test implicit conversion from ReadOnlyMemory<byte> to StorageKey
StorageKey storageKeyFromMemory = keyMemory;
Assert.AreEqual(0, storageKeyFromMemory.Id);
Assert.IsTrue(keyMemory.Span.ToArray().Skip(sizeof(int)).SequenceEqual(storageKeyFromMemory.Key.Span.ToArray()));

// Test CreateSearchPrefix method
byte[] prefix = { 0xAA };
var searchPrefix = StorageKey.CreateSearchPrefix(0, prefix);
var expectedPrefix = BitConverter.GetBytes(0).Concat(prefix).ToArray();
Assert.IsTrue(expectedPrefix.SequenceEqual(searchPrefix));

// Test Equals method
var storageKey1 = new StorageKey { Id = 0, Key = keyMemory };
var storageKey2 = new StorageKey { Id = 0, Key = keyMemory };
var storageKeyDifferentId = new StorageKey { Id = 0 + 1, Key = keyMemory };
var storageKeyDifferentKey = new StorageKey { Id = 0, Key = new ReadOnlyMemory<byte>([0x04]) };
Assert.AreEqual(storageKey1, storageKey2);
Assert.AreNotEqual(storageKey1, storageKeyDifferentId);
Assert.AreNotEqual(storageKey1, storageKeyDifferentKey);
}

[TestMethod]
public void TestStorageItem()
{
// Test data
byte[] keyData = [0x00, 0x00, 0x00, 0x00, 0x12];
BigInteger bigInteger = new BigInteger(1234567890);

// Test implicit conversion from byte[] to StorageItem
StorageItem storageItemFromArray = keyData;
Assert.IsTrue(keyData.SequenceEqual(storageItemFromArray.Value.Span.ToArray()));

// Test implicit conversion from BigInteger to StorageItem
StorageItem storageItemFromBigInteger = bigInteger;
Assert.AreEqual(bigInteger, (BigInteger)storageItemFromBigInteger);
}
}
}
Loading