/// <summary> /// Create a new instance of UpdateWatch. /// </summary> /// <param name="opts">Filled options object to configure update watch</param> /// <param name="logger"></param> public UpdateWatch(UpdateWatchOptions opts, ILogger logger) { // Verify dependencies _configProvider = opts.ConfigurationProvider ?? throw new ArgumentException("Options didn't carry a configuration provider implementation"); _dcc = opts.DockerControl ?? throw new ArgumentException("Options didn't carry a docker compose control implementation"); _cw = opts.ContractWrapper ?? throw new ArgumentException("Options didn't carry a ContractWrapper implementation"); _logger = logger ?? throw new ArgumentException("No logger was supplied."); _waitTime = opts.WaitTimeAfterUpdate; _updating = false; // verify scalar options if (string.IsNullOrWhiteSpace(opts.RpcEndpoint)) { throw new ArgumentException("Options didn't provide an rpc url"); } if (string.IsNullOrWhiteSpace(opts.ContractAddress)) { throw new ArgumentException("Options didn't provide a contract address"); } if (string.IsNullOrWhiteSpace(opts.ValidatorAddress)) { throw new ArgumentException("Options didn't provide a validator address"); } if (string.IsNullOrWhiteSpace(opts.DockerStackPath)) { throw new ArgumentException("Options didn't provide a docker stack path"); } // Instantiate needed objects _sc = new StateCompare(opts.ConfigurationProvider); _stackPath = opts.DockerStackPath; }
public void UpdateWatchOptionsSetGetTest(string contractAddr, string rpc, string validatorAddr, string path) { IDockerControl mdcc = new MockDockerControl(); IConfigurationProvider cp = new MockConfigProvider(); IContractWrapper cw = new MockContractWrapper(); UpdateWatchOptions watchOpts = new UpdateWatchOptions { RpcEndpoint = rpc, ContractAddress = contractAddr, ValidatorAddress = validatorAddr, DockerStackPath = path, DockerControl = mdcc, ConfigurationProvider = cp, ContractWrapper = cw, WaitTimeAfterUpdate = 12345 }; Assert.Equal(rpc, watchOpts.RpcEndpoint); Assert.Equal(contractAddr, watchOpts.ContractAddress); Assert.Equal(validatorAddr, watchOpts.ValidatorAddress); Assert.Equal(path, watchOpts.DockerStackPath); Assert.Equal(mdcc, watchOpts.DockerControl); Assert.Equal(cw, watchOpts.ContractWrapper); Assert.Equal(cp, watchOpts.ConfigurationProvider); Assert.Equal(12345, watchOpts.WaitTimeAfterUpdate); }
public void ShouldThrowOnMissingOptions(UpdateWatchOptions badOpts, string expectedMessage) { Action ctor = () => { _ = new UpdateWatch(badOpts, new MockLogger()); }; ctor.Should() .Throw <ArgumentException>() .WithMessage(expectedMessage); }
public void ShouldBuildWithDefaults() { Hashtable envHt = new Hashtable(); UpdateWatchOptions watchOpts = ConfigBuilder.BuildConfigurationFromEnvironment(envHt); // Verify correct env reading watchOpts.RpcEndpoint.Should().Be("http://localhost:8545"); watchOpts.ValidatorAddress.Should().Be(string.Empty); watchOpts.ContractAddress.Should().Be(string.Empty); watchOpts.DockerStackPath.Should().Be("./demo-stack"); }
public void UpdateWatchOptionsDefaultsTest() { UpdateWatchOptions sca = new UpdateWatchOptions(); // Interfaces should be null Assert.Null(sca.ConfigurationProvider); Assert.Null(sca.DockerControl); // Strings should be empty Assert.Equal(string.Empty, sca.ContractAddress); Assert.Equal(string.Empty, sca.RpcEndpoint); Assert.Equal(string.Empty, sca.ValidatorAddress); Assert.Equal(string.Empty, sca.DockerStackPath); }
private static void Main() { ILogger logger = new ConsoleLogger(); logger.Log("EWF NodeControl"); // config stuff IDictionary env = Environment.GetEnvironmentVariables(); UpdateWatchOptions watchOpts = ConfigBuilder.BuildConfigurationFromEnvironment(env); // Read key password string secretPath = Path.Combine(watchOpts.DockerStackPath, ".secret"); if (!File.Exists(secretPath)) { logger.Log("Unable to read secret. Exiting."); return; } string keyPw = File.ReadAllText(secretPath).TrimEnd('\r', '\n'); if (!File.Exists(watchOpts.ValidatorKeyFile)) { logger.Log("Unable to read parity key file. Exiting."); return; } string encKey = File.ReadAllText(watchOpts.ValidatorKeyFile); // Add dependencies watchOpts.ConfigurationProvider = new ConfigurationFileHandler(Path.Combine(watchOpts.DockerStackPath, ".env")); watchOpts.DockerControl = new LinuxDockerControl(logger); watchOpts.ContractWrapper = new ContractWrapper(watchOpts.ContractAddress, watchOpts.RpcEndpoint, watchOpts.ValidatorAddress, logger, keyPw, encKey, watchOpts.BlockNumberPersistFile); // instantiate the update watch UpdateWatch uw = new UpdateWatch(watchOpts, logger); // start watching uw.StartWatch(); // Block main thread while (true) { Thread.Sleep(60000); } }
public void ShouldBuildFromEnvironment() { Hashtable envHt = new Hashtable { { "CONTRACT_ADDRESS", "0x12345" }, { "STACK_PATH", "/foo/path" }, { "RPC_ENDPOINT", "http://my.rpc.endpoint" }, { "VALIDATOR_ADDRESS", "0xabfed12345" } }; UpdateWatchOptions watchOpts = ConfigBuilder.BuildConfigurationFromEnvironment(envHt); // Verify correct env reading watchOpts.RpcEndpoint.Should().Be("http://my.rpc.endpoint"); watchOpts.ValidatorAddress.Should().Be("0xabfed12345"); watchOpts.ContractAddress.Should().Be("0x12345"); watchOpts.DockerStackPath.Should().Be("/foo/path"); }