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

Tests and refactoring #3

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/AutomatedFlowTest/AutomatedFlowTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>

</Project>
296 changes: 296 additions & 0 deletions src/AutomatedFlowTest/AutomatedStateHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
using MasternodeSetupTool;

using NodeType = MasternodeSetupTool.NodeType;

public class AutomatedStateHandler : IStateHandler
{
private Configuration configuration;
private ILogger logger;

public AutomatedStateHandler(Configuration configuration, ILogger logger)
{
this.configuration = configuration;
this.logger = logger;
}

public void Error(string message)
{
this.logger.Log($"Error: {message}");
}

public void Error(Exception exception)
{
this.logger.Log($"Error: {exception}");
}

public void Error(string message, Exception exception)
{
this.logger.Log($"Error: {message}");
this.logger.Log($"Error: {exception}");
}

public void Info(string message, string? updateTag = null)
{
this.logger.Log($"Info: {message}");
}

public async Task OnAlreadyMember()
{
this.logger.Log("This node is already a member");
}

public async Task<string?> OnAskCreatePassword(NodeType nodeType)
{
this.logger.Log($"Asked to create a password for {nodeType} wallet");

if (nodeType == NodeType.MainChain)
{
return this.configuration.collateralWalletPassword;
}
else
{
return this.configuration.miningWalletPassword;
}
}

public async Task<bool> OnAskForEULA()
{
this.logger.Log($"Asked for EULA");
return this.configuration.confirmEULA;
}

public async Task<bool> OnAskForMnemonicConfirmation(NodeType nodeType, string mnemonic)
{
this.logger.Log($"Asked for mnemonic confirmation");
this.logger.Log($"Mnemonic: {mnemonic}");
return this.configuration.confirmMnemonic;
}

public async Task<bool> OnAskForNewFederationKey()
{
this.logger.Log($"Asked for new federation key");
return this.configuration.confirmNewFederationKey;
}

public async Task<string?> OnAskForPassphrase(NodeType nodeType)
{
this.logger.Log($"Asked to create a passphrase for {nodeType} wallet");

if (nodeType == NodeType.MainChain)
{
return this.configuration.collateralWalletPassphrase;
}
else
{
return this.configuration.miningWalletPassphrase;
}
}

public async Task<string?> OnAskForUserMnemonic(NodeType nodeType)
{
this.logger.Log($"Asked a mnemonic for {nodeType} wallet");

if (nodeType == NodeType.MainChain)
{
return this.configuration.collateralWalletMnemonic;
}
else
{
return this.configuration.miningWalletMnemonic;
}
}

public async Task<string?> OnAskForWalletName(NodeType nodeType, bool newWallet)
{
this.logger.Log($"Asked a wallet name for {nodeType} wallet");

if (nodeType == NodeType.MainChain)
{
return this.configuration.collateralWalletName;
}
else
{
return this.configuration.miningWalletName;
}
}

public async Task<string?> OnAskForWalletPassword(NodeType nodeType)
{
this.logger.Log($"Asked a password for {nodeType} wallet");

if (nodeType == NodeType.MainChain)
{
return this.configuration.collateralWalletPassword;
}
else
{
return this.configuration.miningWalletPassword;
}
}

public async Task<WalletSource?> OnAskForWalletSource(NodeType nodeType)
{
this.logger.Log($"Asked a wallet source for {nodeType} wallet");

if (nodeType == NodeType.MainChain)
{
return this.configuration.collateralWalletSource;
}
else
{
return this.configuration.miningWalletSource;
}
}

public async Task<bool> OnAskReenterPassword(NodeType nodeType)
{
this.logger.Log($"Asked to reenter password");

return this.configuration.confirmReenterPassword;
}

public async Task<bool> OnAskToRunIfAlreadyMember()
{
this.logger.Log($"Already a member, stopping");
return this.configuration.confirmRunIfAlreadyMember;
}

public async Task<string?> OnChooseAddress(List<AddressItem> addresses, NodeType nodeType)
{
string? address = (nodeType == NodeType.MainChain
? this.configuration.collateralWalletAddress
: this.configuration.miningWalletAddress)
?? addresses.FirstOrDefault().Address;

this.logger.Log($"Choosing address {address}");
return address;
}

public async Task<string?> OnChooseWallet(List<WalletItem> wallets, NodeType nodeType)
{
string? walletName = (nodeType == NodeType.MainChain
? this.configuration.collateralWalletName
: this.configuration.miningWalletName)
?? wallets.FirstOrDefault().Name;

this.logger.Log($"Choosing {nodeType} wallet {walletName}");
return walletName;
}

public async Task OnCreateWalletFailed(NodeType nodeType)
{
this.logger.Log($"{nodeType} wallet creation failed");
}

public async Task OnFederationKeyMissing()
{
this.logger.Log($"Missing federation key");
}

public async Task OnMissingRegistrationFee(string address)
{
this.logger.Log($"Missing registration fee on address: {address}");
}

public async Task OnMnemonicExists(NodeType nodeType)
{
this.logger.Log($"{nodeType} wallet mnemonic already exists");
}

public async Task OnMnemonicIsInvalid(NodeType nodeType)
{
this.logger.Log($"{nodeType} wallet mnemonic is invalid");
}

public async Task OnNodeFailedToStart(NodeType nodeType, string? reason = null)
{
this.logger.Log($"{nodeType} node failed to start");
this.logger.Log($"Reason: {reason}");
}

public async Task OnProgramVersionAvailable(string? version)
{
this.logger.Log($"App version: {version ?? "null"}");
}

public async Task OnRegistrationCanceled()
{
this.logger.Log($"Registration canceled");
}

public async Task OnRegistrationComplete()
{
this.logger.Log($"Registration complete");
}

public async Task OnRegistrationFailed()
{
this.logger.Log($"Registration failed");
}

public async Task OnRestoreWalletFailed(NodeType nodeType)
{
this.logger.Log($"{nodeType} wallet restore failed");
}

public async Task OnResyncFailed(NodeType nodeType)
{
this.logger.Log($"{nodeType} wallet resync failed");
}

public async Task OnShowNewFederationKey(string pubKey, string savePath)
{
this.logger.Log($"New pubKey is: {pubKey}");
this.logger.Log($"New savePath is: {savePath}");
}

public async Task OnShowWalletAddress(NodeType nodeType, string address)
{
this.logger.Log($"{nodeType} wallet address is {address}");
}

public async Task OnShowWalletName(NodeType nodeType, string walletName)
{
this.logger.Log($"{nodeType} wallet name is {walletName}");
}

public async Task OnStart()
{
this.logger.Log($"Started");
}

public async Task OnWaitingForCollateral()
{
this.logger.Log($"Waiting for collateral");
}

public async Task OnWaitingForRegistrationFee()
{
this.logger.Log($"Waiting for registration fee");
}

public async Task OnWalletExistsOrInvalid(NodeType nodeType)
{
this.logger.Log($"{nodeType} wallet exists or invalid");
}

public async Task OnWalletNameExists(NodeType nodeType)
{
this.logger.Log($"{nodeType} wallet name already exists");
}

public async Task OnWalletSynced(NodeType nodeType)
{
this.logger.Log($"{nodeType} wallet synced");
}

public async Task OnWalletSyncing(NodeType nodeType, int progress)
{
this.logger.Log($"{nodeType} wallet is syncing, {progress}%");
}

public interface ILogger
{
void Log(string message);
}
}
20 changes: 20 additions & 0 deletions src/AutomatedFlowTest/CompositeLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace AutomatedFlowTest
{
public class CompositeLogger: AutomatedStateHandler.ILogger
{
private IEnumerable<AutomatedStateHandler.ILogger> Loggers;

public CompositeLogger(IEnumerable<AutomatedStateHandler.ILogger> loggers)
{
this.Loggers = loggers;
}

public void Log(string message)
{
foreach (AutomatedStateHandler.ILogger logger in this.Loggers)
{
logger.Log(message);
}
}
}
}
38 changes: 38 additions & 0 deletions src/AutomatedFlowTest/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using MasternodeSetupTool;
using NBitcoin;

public class Configuration
{
public FlowType flowType = FlowType.SetupNode;

public bool writeConsoleLog = true;
public string? logFilePath = null;

public NetworkType networkType = NetworkType.Testnet;

public WalletSource collateralWalletSource = WalletSource.UseExistingWallet;
public WalletSource miningWalletSource = WalletSource.UseExistingWallet;

public bool confirmEULA = true;
public bool confirmNewFederationKey = true;
public bool confirmRunIfAlreadyMember = true;
public bool confirmMnemonic = true;
public bool confirmReenterPassword = true;

public string? collateralWalletName = "TestWallet";
public string? miningWalletName = "TestWallet";
public string collateralWalletPassword = "12345";
public string miningWalletPassword = "12345";
public string collateralWalletPassphrase = "";
public string miningWalletPassphrase = "";
public string collateralWalletMnemonic = "";
public string miningWalletMnemonic = "";
public string? collateralWalletAddress = null;
public string? miningWalletAddress = null;
}


public enum FlowType
{
RunNode, SetupNode
}
10 changes: 10 additions & 0 deletions src/AutomatedFlowTest/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace AutomatedFlowTest
{
public class ConsoleLogger : AutomatedStateHandler.ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
}
Loading