diff --git a/src/Plugins/RpcServer/RpcServer.Blockchain.cs b/src/Plugins/RpcServer/RpcServer.Blockchain.cs index a4fda6e738..cf3c16b59d 100644 --- a/src/Plugins/RpcServer/RpcServer.Blockchain.cs +++ b/src/Plugins/RpcServer/RpcServer.Blockchain.cs @@ -119,15 +119,13 @@ protected virtual JToken GetContractState(JArray _params) { if (int.TryParse(_params[0].AsString(), out int contractId)) { - var contracts = NativeContract.ContractManagement.GetContractById(system.StoreView, contractId); - return contracts?.ToJson().NotNull_Or(RpcError.UnknownContract); - } - else - { - UInt160 script_hash = ToScriptHash(_params[0].AsString()); - ContractState contract = NativeContract.ContractManagement.GetContract(system.StoreView, script_hash); - return contract?.ToJson().NotNull_Or(RpcError.UnknownContract); + var contractState = NativeContract.ContractManagement.GetContractById(system.StoreView, contractId); + return contractState.NotNull_Or(RpcError.UnknownContract).ToJson(); } + + var scriptHash = ToScriptHash(_params[0].AsString()); + var contract = NativeContract.ContractManagement.GetContract(system.StoreView, scriptHash); + return contract.NotNull_Or(RpcError.UnknownContract).ToJson(); } private static UInt160 ToScriptHash(string keyword) diff --git a/tests/Neo.Plugins.RpcServer.Tests/UT_Result.cs b/tests/Neo.Plugins.RpcServer.Tests/UT_Result.cs new file mode 100644 index 0000000000..aa6a8fe308 --- /dev/null +++ b/tests/Neo.Plugins.RpcServer.Tests/UT_Result.cs @@ -0,0 +1,26 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// UT_Result.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; + +namespace Neo.Plugins.RpcServer.Tests; + +[TestClass] +public class UT_Result +{ + [TestMethod] + public void TestNotNull_Or() + { + ContractState? contracts = null; + Assert.ThrowsException(() => contracts.NotNull_Or(RpcError.UnknownContract).ToJson()); + } +}