forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Add] GetStorage for ContractState (neo-project#3615)
* [Add] GetStorage for ContractState * Add more parameters and unit tests * Changed names * Update src/Neo/Extensions/SmartContract/ContractStateExtensions.cs Co-authored-by: nan01ab <[email protected]> * Update src/Neo/Extensions/SmartContract/ContractStateExtensions.cs Co-authored-by: nan01ab <[email protected]> * Update src/Neo/Extensions/SmartContract/ContractStateExtensions.cs Co-authored-by: nan01ab <[email protected]> * Updated RPC server to use `FindStorage` * updated namespace * Fixed errors --------- Co-authored-by: Shargon <[email protected]> Co-authored-by: Jimmy <[email protected]> Co-authored-by: nan01ab <[email protected]>
- Loading branch information
1 parent
2c521a0
commit f65c193
Showing
3 changed files
with
154 additions
and
3 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/Neo/Extensions/SmartContract/ContractStateExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// ContractStateExtensions.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 Neo.Persistence; | ||
using Neo.SmartContract; | ||
using Neo.SmartContract.Native; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Neo.Extensions | ||
{ | ||
public static class ContractStateExtensions | ||
{ | ||
/// <summary> | ||
/// Get Storage value by storage map key. | ||
/// </summary> | ||
/// <param name="contractState"></param> | ||
/// <param name="snapshot">Snapshot of the database.</param> | ||
/// <param name="storageKey">Key in the storage map.</param> | ||
/// <returns>Storage value of the item.</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="contractState"/> or <paramref name="snapshot"/> is null</exception> | ||
public static StorageItem GetStorage(this ContractState contractState, DataCache snapshot, byte[] storageKey) | ||
{ | ||
if (contractState is null) | ||
throw new ArgumentNullException(nameof(contractState)); | ||
|
||
if (snapshot is null) | ||
throw new ArgumentNullException(nameof(snapshot)); | ||
|
||
if (storageKey is null) | ||
storageKey = []; | ||
|
||
return snapshot.TryGet(StorageKey.CreateSearchPrefix(contractState.Id, storageKey)); | ||
} | ||
|
||
/// <summary> | ||
/// All storage items stored in the given contract. | ||
/// </summary> | ||
/// <param name="contractState"></param> | ||
/// <param name="snapshot">Snapshot of the database.</param> | ||
/// <param name="prefix">Prefix of the key.</param> | ||
/// <param name="seekDirection"></param> | ||
/// <returns>All storage of the given contract.</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="contractState"/> or <paramref name="snapshot"/> is null</exception> | ||
public static IEnumerable<(StorageKey Key, StorageItem Value)> FindStorage(this ContractState contractState, DataCache snapshot, byte[] prefix = null, SeekDirection seekDirection = SeekDirection.Forward) | ||
{ | ||
if (contractState is null) | ||
throw new ArgumentNullException(nameof(contractState)); | ||
|
||
if (snapshot is null) | ||
throw new ArgumentNullException(nameof(snapshot)); | ||
|
||
if (prefix is null) | ||
prefix = []; | ||
|
||
return snapshot.Find(StorageKey.CreateSearchPrefix(contractState.Id, prefix), seekDirection); | ||
} | ||
|
||
/// <summary> | ||
/// All storage items stored in the given contract. | ||
/// </summary> | ||
/// <param name="contractManagement"></param> | ||
/// <param name="snapshot">Snapshot of the database.</param> | ||
/// <param name="prefix">Prefix of the key.</param> | ||
/// <param name="contractId">Id of the contract.</param> | ||
/// <param name="seekDirection"></param> | ||
/// <returns>All storage of the given contract.</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="snapshot"/> is null</exception> | ||
public static IEnumerable<(StorageKey Key, StorageItem Value)> FindContractStorage(this ContractManagement contractManagement, DataCache snapshot, int contractId, byte[] prefix = null, SeekDirection seekDirection = SeekDirection.Forward) | ||
{ | ||
if (snapshot is null) | ||
throw new ArgumentNullException(nameof(snapshot)); | ||
|
||
if (prefix is null) | ||
prefix = []; | ||
|
||
return snapshot.Find(StorageKey.CreateSearchPrefix(contractId, prefix), seekDirection); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
tests/Neo.UnitTests/Extensions/UT_ContractStateExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// UT_ContractStateExtensions.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.Extensions; | ||
using Neo.SmartContract; | ||
using Neo.SmartContract.Native; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Neo.UnitTests.Extensions | ||
{ | ||
[TestClass] | ||
public class UT_ContractStateExtensions | ||
{ | ||
private NeoSystem system; | ||
|
||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
system = TestBlockchain.TheNeoSystem; | ||
} | ||
|
||
[TestCleanup] | ||
public void Clean() | ||
{ | ||
TestBlockchain.ResetStore(); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetStorage() | ||
{ | ||
var contractStorage = NativeContract.ContractManagement.FindContractStorage(system.StoreView, NativeContract.NEO.Id); | ||
|
||
Assert.IsNotNull(contractStorage); | ||
|
||
var neoContract = NativeContract.ContractManagement.GetContractById(system.StoreView, NativeContract.NEO.Id); | ||
|
||
contractStorage = neoContract.FindStorage(system.StoreView); | ||
|
||
Assert.IsNotNull(contractStorage); | ||
|
||
contractStorage = neoContract.FindStorage(system.StoreView, [20]); | ||
|
||
Assert.IsNotNull(contractStorage); | ||
|
||
UInt160 address = "0x9f8f056a53e39585c7bb52886418c7bed83d126b"; | ||
var item = neoContract.GetStorage(system.StoreView, [20, .. address.ToArray()]); | ||
|
||
Assert.IsNotNull(item); | ||
Assert.AreEqual(100_000_000, item.GetInteroperable<AccountState>().Balance); | ||
} | ||
} | ||
} |