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 Plugin RPCServer] Rpc parameters. Part I #3457

Merged
merged 21 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
61 changes: 61 additions & 0 deletions src/Plugins/RpcServer/Model/BlockHashOrIndex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// BlockHashOrIndex.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 System.Diagnostics.CodeAnalysis;

namespace Neo.Plugins.RpcServer.Model;

public class BlockHashOrIndex
cschuchardt88 marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly object _value;

public BlockHashOrIndex(uint index)
{
_value = index;
}

public BlockHashOrIndex(UInt256 hash)
{
_value = hash;
}

public bool IsIndex => _value is uint;

public static bool TryParse(string value, [NotNullWhen(true)] out BlockHashOrIndex? blockHashOrIndex)

Check warning on line 32 in src/Plugins/RpcServer/Model/BlockHashOrIndex.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 32 in src/Plugins/RpcServer/Model/BlockHashOrIndex.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 32 in src/Plugins/RpcServer/Model/BlockHashOrIndex.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 32 in src/Plugins/RpcServer/Model/BlockHashOrIndex.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 32 in src/Plugins/RpcServer/Model/BlockHashOrIndex.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 32 in src/Plugins/RpcServer/Model/BlockHashOrIndex.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 32 in src/Plugins/RpcServer/Model/BlockHashOrIndex.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 32 in src/Plugins/RpcServer/Model/BlockHashOrIndex.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (uint.TryParse(value, out var index))
{
blockHashOrIndex = new BlockHashOrIndex(index);
return true;
}
if (UInt256.TryParse(value, out var hash))
{
blockHashOrIndex = new BlockHashOrIndex(hash);
return true;
}
blockHashOrIndex = null;
return false;
}

public uint AsIndex()
{
if (_value is uint intValue)
return intValue;
throw new RpcException(RpcError.InvalidParams.WithData($"Value {_value} is not a valid block index"));
}

public UInt256 AsHash()
{
if (_value is UInt256 hash)
return hash;
throw new RpcException(RpcError.InvalidParams.WithData($"Value {_value} is not a valid block hash"));
}
}
80 changes: 80 additions & 0 deletions src/Plugins/RpcServer/Model/ContractNameOrHashOrId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// ContractNameOrHashOrId.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 System.Diagnostics.CodeAnalysis;

namespace Neo.Plugins.RpcServer.Model;

public class ContractNameOrHashOrId
{
private readonly object _value;

public ContractNameOrHashOrId(int id)
{
_value = id;
}

public ContractNameOrHashOrId(UInt160 hash)
{
_value = hash;
}

public ContractNameOrHashOrId(string name)
{
_value = name;
}

public bool IsId => _value is int;
public bool IsHash => _value is UInt160;
public bool IsName => _value is string;

public static bool TryParse(string value, [NotNullWhen(true)] out ContractNameOrHashOrId? contractNameOrHashOrId)

Check warning on line 39 in src/Plugins/RpcServer/Model/ContractNameOrHashOrId.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Plugins/RpcServer/Model/ContractNameOrHashOrId.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Plugins/RpcServer/Model/ContractNameOrHashOrId.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Plugins/RpcServer/Model/ContractNameOrHashOrId.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Plugins/RpcServer/Model/ContractNameOrHashOrId.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Plugins/RpcServer/Model/ContractNameOrHashOrId.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Plugins/RpcServer/Model/ContractNameOrHashOrId.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Plugins/RpcServer/Model/ContractNameOrHashOrId.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (int.TryParse(value, out var id))
{
contractNameOrHashOrId = new ContractNameOrHashOrId(id);
return true;
}
if (UInt160.TryParse(value, out var hash))
{
contractNameOrHashOrId = new ContractNameOrHashOrId(hash);
return true;
}
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved

if (value.Length > 0)
{
contractNameOrHashOrId = new ContractNameOrHashOrId(value);
return true;
}
contractNameOrHashOrId = null;
return false;
}

public int AsId()
{
if (_value is int intValue)
return intValue;
throw new RpcException(RpcError.InvalidParams.WithData($"Value {_value} is not a valid contract id"));
}

public UInt160 AsHash()
{
if (_value is UInt160 hash)
return hash;
throw new RpcException(RpcError.InvalidParams.WithData($"Value {_value} is not a valid contract hash"));
}
public string AsName()
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
if (_value is string name)
return name;
throw new RpcException(RpcError.InvalidParams.WithData($"Value {_value} is not a valid contract name"));
}
}
20 changes: 20 additions & 0 deletions src/Plugins/RpcServer/RpcMethodWithParamsAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// RpcMethodWithParamsAttribute.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 System;

namespace Neo.Plugins.RpcServer;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class RpcMethodWithParamsAttribute : Attribute
{
public string Name { get; set; }
}
Loading
Loading