cs-codex-dist-tests/ProjectPlugins/GethPlugin/GethNode.cs

173 lines
6.1 KiB
C#
Raw Normal View History

2023-09-21 08:56:48 +00:00
using Core;
using KubernetesWorkflow.Types;
2023-09-21 08:56:48 +00:00
using Logging;
2023-12-19 14:30:46 +00:00
using Nethereum.ABI.FunctionEncoding.Attributes;
2023-09-19 11:39:24 +00:00
using Nethereum.Contracts;
using Nethereum.RPC.Eth.DTOs;
2023-09-15 14:27:08 +00:00
using NethereumWorkflow;
2024-04-13 07:19:20 +00:00
using NethereumWorkflow.BlockUtils;
2023-12-20 08:48:22 +00:00
using Utils;
2023-09-15 13:52:02 +00:00
namespace GethPlugin
{
2023-09-21 08:56:48 +00:00
public interface IGethNode : IHasContainer
2023-09-15 13:52:02 +00:00
{
2023-09-20 08:13:29 +00:00
GethDeployment StartResult { get; }
2023-09-15 14:27:08 +00:00
2023-09-19 09:51:59 +00:00
Ether GetEthBalance();
Ether GetEthBalance(IHasEthAddress address);
2023-09-20 08:13:29 +00:00
Ether GetEthBalance(EthAddress address);
2023-12-15 10:02:06 +00:00
string SendEth(IHasEthAddress account, Ether eth);
string SendEth(EthAddress account, Ether eth);
2023-09-19 11:39:24 +00:00
TResult Call<TFunction, TResult>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new();
2023-12-15 10:02:06 +00:00
string SendTransaction<TFunction>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new();
Transaction GetTransaction(string transactionHash);
2023-09-19 11:58:45 +00:00
decimal? GetSyncedBlockNumber();
bool IsContractAvailable(string abi, string contractAddress);
GethBootstrapNode GetBootstrapRecord();
2024-03-27 14:39:42 +00:00
List<EventLog<TEvent>> GetEvents<TEvent>(string address, BlockInterval blockRange) where TEvent : IEventDTO, new();
2023-12-20 08:48:22 +00:00
List<EventLog<TEvent>> GetEvents<TEvent>(string address, TimeRange timeRange) where TEvent : IEventDTO, new();
2024-03-27 14:39:42 +00:00
BlockInterval ConvertTimeRangeToBlockRange(TimeRange timeRange);
2024-04-13 07:19:20 +00:00
BlockTimeEntry GetBlockForNumber(ulong number);
2023-09-15 13:52:02 +00:00
}
public class DeploymentGethNode : BaseGethNode, IGethNode
2023-09-15 13:52:02 +00:00
{
2023-09-19 09:51:59 +00:00
private readonly ILog log;
public DeploymentGethNode(ILog log, GethDeployment startResult)
2023-09-15 13:52:02 +00:00
{
2023-09-19 09:51:59 +00:00
this.log = log;
StartResult = startResult;
2023-09-15 13:52:02 +00:00
}
2023-09-20 08:13:29 +00:00
public GethDeployment StartResult { get; }
2023-09-21 08:56:48 +00:00
public RunningContainer Container => StartResult.Container;
2023-09-15 13:52:02 +00:00
public GethBootstrapNode GetBootstrapRecord()
{
var address = StartResult.Container.GetInternalAddress(GethContainerRecipe.ListenPortTag);
return new GethBootstrapNode(
publicKey: StartResult.PubKey,
ipAddress: address.Host.Replace("http://", ""),
port: address.Port
);
}
protected override NethereumInteraction StartInteraction()
{
2024-09-23 08:52:12 +00:00
var address = StartResult.Container.GetAddress(GethContainerRecipe.HttpPortTag);
var account = StartResult.Account;
var creator = new NethereumInteractionCreator(log, address.Host, address.Port, account.PrivateKey);
return creator.CreateWorkflow();
}
}
public class CustomGethNode : BaseGethNode, IGethNode
{
private readonly ILog log;
private readonly string gethHost;
private readonly int gethPort;
private readonly string privateKey;
public GethDeployment StartResult => throw new NotImplementedException();
public RunningContainer Container => throw new NotImplementedException();
public CustomGethNode(ILog log, string gethHost, int gethPort, string privateKey)
{
this.log = log;
this.gethHost = gethHost;
this.gethPort = gethPort;
this.privateKey = privateKey;
}
public GethBootstrapNode GetBootstrapRecord()
{
throw new NotImplementedException();
}
protected override NethereumInteraction StartInteraction()
{
var creator = new NethereumInteractionCreator(log, gethHost, gethPort, privateKey);
return creator.CreateWorkflow();
}
}
public abstract class BaseGethNode
{
2023-09-19 09:51:59 +00:00
public Ether GetEthBalance()
{
return StartInteraction().GetEthBalance().Eth();
}
public Ether GetEthBalance(IHasEthAddress owner)
{
return GetEthBalance(owner.EthAddress);
}
2023-09-20 08:13:29 +00:00
public Ether GetEthBalance(EthAddress address)
2023-09-19 09:51:59 +00:00
{
return StartInteraction().GetEthBalance(address.Address).Eth();
}
2023-12-15 10:02:06 +00:00
public string SendEth(IHasEthAddress owner, Ether eth)
2023-09-19 09:51:59 +00:00
{
2023-12-15 10:02:06 +00:00
return SendEth(owner.EthAddress, eth);
2023-09-19 09:51:59 +00:00
}
2023-12-15 10:02:06 +00:00
public string SendEth(EthAddress account, Ether eth)
2023-09-19 09:51:59 +00:00
{
2023-12-15 10:02:06 +00:00
return StartInteraction().SendEth(account.Address, eth.Eth);
2023-09-19 11:39:24 +00:00
}
public TResult Call<TFunction, TResult>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new()
{
return StartInteraction().Call<TFunction, TResult>(contractAddress, function);
}
2023-12-15 10:02:06 +00:00
public string SendTransaction<TFunction>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new()
2023-09-19 11:39:24 +00:00
{
2023-12-15 10:02:06 +00:00
return StartInteraction().SendTransaction(contractAddress, function);
2023-09-19 11:39:24 +00:00
}
public Transaction GetTransaction(string transactionHash)
{
return StartInteraction().GetTransaction(transactionHash);
}
2023-09-19 11:58:45 +00:00
public decimal? GetSyncedBlockNumber()
{
return StartInteraction().GetSyncedBlockNumber();
}
public bool IsContractAvailable(string abi, string contractAddress)
{
return StartInteraction().IsContractAvailable(abi, contractAddress);
}
2024-03-27 14:39:42 +00:00
public List<EventLog<TEvent>> GetEvents<TEvent>(string address, BlockInterval blockRange) where TEvent : IEventDTO, new()
2023-12-19 14:30:46 +00:00
{
2024-02-19 14:20:12 +00:00
return StartInteraction().GetEvents<TEvent>(address, blockRange);
2023-12-19 14:30:46 +00:00
}
2023-12-20 08:48:22 +00:00
public List<EventLog<TEvent>> GetEvents<TEvent>(string address, TimeRange timeRange) where TEvent : IEventDTO, new()
2023-12-19 14:30:46 +00:00
{
2024-03-27 14:39:42 +00:00
return StartInteraction().GetEvents<TEvent>(address, ConvertTimeRangeToBlockRange(timeRange));
2023-12-19 14:30:46 +00:00
}
2024-03-27 14:39:42 +00:00
public BlockInterval ConvertTimeRangeToBlockRange(TimeRange timeRange)
2024-02-19 14:20:12 +00:00
{
return StartInteraction().ConvertTimeRangeToBlockRange(timeRange);
}
2024-04-13 07:19:20 +00:00
public BlockTimeEntry GetBlockForNumber(ulong number)
{
return StartInteraction().GetBlockForNumber(number);
}
protected abstract NethereumInteraction StartInteraction();
2023-09-15 13:52:02 +00:00
}
}