public void StressPeak() { var addressCache = new AddressCache(new TimeSpan(0, 0, 2)); Assert.True(addressCache.Add(IPAddress.Parse("1.1.1.1"))); Assert.True(addressCache.Add(IPAddress.Parse("2.2.2.2"))); Assert.True(addressCache.Add(IPAddress.Parse("3.3.3.3"))); Assert.True(addressCache.Add(IPAddress.Parse("4.4.4.4"))); Assert.True(addressCache.Add(IPAddress.Parse("5.5.5.5"))); Assert.AreEqual("5.5.5.5", addressCache.Peek().ToString()); Thread.Sleep(1000); Assert.True(addressCache.Add(IPAddress.Parse("6.6.6.6"))); Assert.AreEqual("6.6.6.6", addressCache.Peek().ToString()); Thread.Sleep(1000); Assert.True(addressCache.Add(IPAddress.Parse("5.5.5.5"))); Assert.AreEqual("5.5.5.5", addressCache.Peek().ToString()); Thread.Sleep(2000); Assert.Null(addressCache.Peek()); }
public void Block() { var addressCache = new AddressCache(new TimeSpan(0, 0, 2)); var thread = new Thread(() => { addressCache.Take(); Assert.Fail(); }); thread.Start(); Thread.Sleep(1000); thread.Abort(); thread = new Thread(() => { Assert.AreEqual("1.1.1.1", addressCache.Take().ToString()); Assert.Pass(); }); thread.Start(); Thread.Sleep(1000); Assert.True(addressCache.Add(IPAddress.Parse("1.1.1.1"))); }
//This is a window encoder for the VCDIFF format //if you are not including a checksum simply pass 0 to checksum //it will be ignored public WindowEncoder(long dictionarySize, uint checksum, ChecksumFormat checksumFormat, bool interleaved) { this.Checksum = checksum; this.ChecksumFormat = checksumFormat; this.IsInterleaved = interleaved; this.dictionarySize = dictionarySize; // The encoder currently doesn't support encoding with a custom table // will be added in later since it will be easy as decoding is already implemented maxMode = AddressCache.DefaultLast; table = CodeTable.DefaultTable; addrCache = new AddressCache(); targetLength = 0; lastOpcodeIndex = -1; instrMap = InstructionMap.Instance; //Separate buffers for each type if not interleaved if (!interleaved) { instructionAndSizes = new MemoryStream(); dataForAddAndRun = new MemoryStream(); addressForCopy = new MemoryStream(); } else { instructionAndSizes = dataForAddAndRun = addressForCopy = new MemoryStream(); } }
/// <summary> /// Gets the address parameter from the RabbitMQ message /// </summary> /// <param name="message">JsonRPC consumer message</param> private static void AddAddress(JToken message) { using (DBEntities db = new DBEntities(dbContextOptions.Options)) { if (message["params"] is JObject) { Addresses.Add(message["params"].Value <string>()); _currency.ImportAddress(message["params"].Value <string>()); var addressCache = new AddressCache() { Address = message["params"].Value <string>(), Currency = CurrencyName }; db.Addresses.Add(addressCache); } else if (message["params"] is JArray) { foreach (var address in (JArray)message["params"]) { Addresses.Add(address.ToString()); _currency.ImportAddress(address.ToString()); var addressCache = new AddressCache() { Address = address.ToString(), Currency = CurrencyName }; db.Addresses.Add(addressCache); } } db.SaveChanges(); } }
/// <summary> /// Reads the custom code table, if there is one /// </summary> void ReadCodeTable() { // The length given includes the nearSize and sameSize bytes int compressedTableLength = IOHelper.ReadBigEndian7BitEncodedInt(delta) - 2; int nearSize = IOHelper.CheckedReadByte(delta); int sameSize = IOHelper.CheckedReadByte(delta); byte[] compressedTableData = IOHelper.CheckedReadBytes(delta, compressedTableLength); byte[] defaultTableData = CodeTable.Default.GetBytes(); MemoryStream tableOriginal = new MemoryStream(defaultTableData, false); MemoryStream tableDelta = new MemoryStream(compressedTableData, false); byte[] decompressedTableData = new byte[1536]; MemoryStream tableOutput = new MemoryStream(decompressedTableData, true); VcdiffDecoder.Decode(tableOriginal, tableDelta, tableOutput); if (tableOutput.Position != 1536) { throw new VcdiffFormatException("Compressed code table was incorrect size"); } _codeTable = new CodeTable(decompressedTableData); _cache = new AddressCache(nearSize, sameSize); }
public void Add() { var addressCache = new AddressCache(new TimeSpan(0, 0, 10)); Assert.True(addressCache.Add(IPAddress.Parse("1.1.1.1"))); Assert.False(addressCache.Add(IPAddress.Parse("1.1.1.1"))); Assert.False(addressCache.Add(null)); }
public void ExpiredRemove() { var addressCache = new AddressCache(new TimeSpan(0, 0, 2)); Assert.True(addressCache.Add(IPAddress.Parse("1.1.1.1"))); Thread.Sleep(2000); Assert.False(addressCache.Remove(IPAddress.Parse("1.1.1.1"))); }
public void Remove() { var addressCache = new AddressCache(new TimeSpan(0, 0, 2)); Assert.True(addressCache.Add(IPAddress.Parse("1.1.1.1"))); Assert.False(addressCache.Remove(IPAddress.Parse("2.2.2.2"))); Assert.True(addressCache.Remove(IPAddress.Parse("1.1.1.1"))); }
public Monkeys(TimeSpan maxCacheAge, int count = 100, int actionCount = 100, int actionDelayInMs = 100, int taskStartDelay = 100) { _count = count; _actionCount = actionCount; _actionDelayInMs = actionDelayInMs; _taskStartDelay = taskStartDelay; _addressCache = new AddressCache(maxCacheAge); }
public void TestRemoveExpired() { var addressCache = new AddressCache(new TimeSpan(0, 0, 2)); Assert.True(addressCache.Add(IPAddress.Parse("1.1.1.1"))); Assert.True(addressCache.Add(IPAddress.Parse("2.2.2.2"))); Thread.Sleep(1000); Assert.True(addressCache.Add(IPAddress.Parse("3.3.3.3"))); Assert.True(addressCache.Add(IPAddress.Parse("4.4.4.4"))); Thread.Sleep(1000); Assert.True(addressCache.Remove(IPAddress.Parse("4.4.4.4"))); }
public string GetServerName(string hostOrIp) { if (Settings.Enabled && IPAddress.TryParse(hostOrIp, out var addr)) { var nodes = GetNodesByIP(addr).ToList(); if (nodes.Count == 1) { return(nodes[0].PrettyName); } } return(AddressCache.GetHostName(hostOrIp)); }
public void Peek() { var addressCache = new AddressCache(new TimeSpan(0, 0, 10)); Assert.Null(addressCache.Peek()); Assert.True(addressCache.Add(IPAddress.Parse("1.1.1.1"))); Assert.AreEqual("1.1.1.1", addressCache.Peek().ToString()); Assert.True(addressCache.Add(IPAddress.Parse("2.2.2.2"))); Assert.AreEqual("2.2.2.2", addressCache.Peek().ToString()); }
public void Expired() { var addressCache = new AddressCache(new TimeSpan(0, 0, 2)); Assert.True(addressCache.Add(IPAddress.Parse("1.1.1.1"))); Assert.True(addressCache.Add(IPAddress.Parse("2.2.2.2"))); Thread.Sleep(1000); Assert.True(addressCache.Add(IPAddress.Parse("3.3.3.3"))); Assert.True(addressCache.Add(IPAddress.Parse("4.4.4.4"))); Thread.Sleep(1000); Assert.AreEqual("4.4.4.4", addressCache.Take().ToString()); Assert.AreEqual("3.3.3.3", addressCache.Take().ToString()); Assert.Null(addressCache.Peek()); }
public void Remove() { var addressCache = new AddressCache(new TimeSpan(0, 0, 2)); Assert.True(addressCache.Add(IPAddress.Parse("1.1.1.1"))); Assert.True(addressCache.Add(IPAddress.Parse("2.2.2.2"))); Assert.True(addressCache.Add(IPAddress.Parse("3.3.3.3"))); Assert.True(addressCache.Add(IPAddress.Parse("4.4.4.4"))); Assert.True(addressCache.Remove(IPAddress.Parse("4.4.4.4"))); Assert.True(addressCache.Remove(IPAddress.Parse("2.2.2.2"))); Assert.AreEqual("3.3.3.3", addressCache.Take().ToString()); Assert.AreEqual("1.1.1.1", addressCache.Take().ToString()); }
/// <summary> /// The main decoder loop for the data /// </summary> /// <param name="w">the window decoder</param> /// <param name="dictionary">the dictionary data</param> /// <param name="target">the target data</param> /// <param name="sout">the out stream</param> /// <param name="customTable">custom table if any. Default is null.</param> public BodyDecoder(WindowDecoder w, IByteBuffer dictionary, IByteBuffer target, ByteStreamWriter sout, CustomCodeTableDecoder customTable = null) { if (customTable != null) { this.customTable = customTable; addressCache = new AddressCache((byte)customTable.NearSize, (byte)customTable.SameSize); } else { addressCache = new AddressCache(); } window = w; this.sout = sout; this.dict = dictionary; this.target = target; targetData = new List <byte>(); }
/// <summary> /// The main decoder loop for the data /// </summary> /// <param name="w">the window decoder</param> /// <param name="source">The source dictionary data</param> /// <param name="delta">The delta</param> /// <param name="decodedTarget">the out stream</param> /// <param name="customTable">custom table if any. Default is null.</param> public BodyDecoder(WindowDecoder w, IByteBuffer source, IByteBuffer delta, Stream decodedTarget, CustomCodeTableDecoder?customTable = null) { if (customTable != null) { this.customTable = customTable; addressCache = new AddressCache(customTable.NearSize, customTable.SameSize); } else { addressCache = new AddressCache(); } window = w; this.outputStream = decodedTarget; this.source = source; this.delta = delta; this.targetData = new MemoryStream(); }
/// <summary> /// The main decoder loop for the data /// </summary> /// <param name="w">the window decoder</param> /// <param name="source">The source dictionary data</param> /// <param name="delta">The delta</param> /// <param name="decodedTarget">the out stream</param> /// <param name="customTable">custom table if any. Default is null.</param> public BodyDecoder(WindowDecoder <TWindowDecoderByteBuffer> w, TSourceBuffer source, TDeltaBuffer delta, Stream decodedTarget, CustomCodeTableDecoder?customTable = null) { if (customTable != null) { this.customTable = customTable; addressCache = new AddressCache(customTable.NearSize, customTable.SameSize); } else { addressCache = new AddressCache(); } window = w; this.outputStream = decodedTarget; this.source = source; this.delta = delta; this.targetData = Pool.MemoryStreamManager.GetStream(nameof(BodyDecoder <TWindowDecoderByteBuffer, TSourceBuffer, TDeltaBuffer>), (int)w.TargetWindowLength); }
List <MinerContext> CreateMinerContexts(int minerCount) { BitcoinWitPubKeyAddress[] mineToAddresses = AddressCache.GetMineToAddresses(minerCount); var contexts = new List <MinerContext>(minerCount); for (int i = 0; i < minerCount; i++) { // if the device index is disabled by configuration, don't create MinerContext if (this.appConfiguration.DisabledDeviceIndices.Contains(i)) { continue; } // this gets us the latest block template, including a unique extra nonce var blockTemplate = this.blockTemplateCache.GetClonedBlockTemplateLocked(); // calculate the start value for block time var earliestTimeVsTime = blockTemplate.curtime + 1; // curtime takes into account adjusted time and prev block median time past var earliestTimeVsPrevTime = blockTemplate.previousblocktime + 1; // x1 consensus requires the block time > last block time var newPoWBlockMinTime = Math.Max(earliestTimeVsTime, earliestTimeVsPrevTime); // time is guaranteed to be > time(prev block) and most likely curtime +1 // the header of this block is complete, with the lowest valid time, and a nonce of 0 var block = blockTemplate.CreateTemplateBlock(newPoWBlockMinTime, false, mineToAddresses[i].ScriptPubKey, null, null); var minerContext = new MinerContext { Name = $"{this.appConfiguration.ClientId} #{i}", ThreadStartedUtc = DateTime.UtcNow, StartNonce = 0, MaxNonce = uint.MaxValue - 1, ThreadIndex = i, SlimBlock = block }; contexts.Add(minerContext); if (!this.deviceController.PerfCounters.ContainsKey(i)) { this.deviceController.PerfCounters.TryAdd(i, new PerfCounter()); } } return(contexts); }
public RedisModule(IConfiguration config, PollingService poller, AddressCache addressCache) : base(config, poller) { Connections = LoadRedisConnections(Settings, addressCache); Instances = Connections.Select(rci => new RedisInstance(this, rci)) .Where(rsi => rsi.TryAddToGlobalPollers()) .ToList(); Hosts = Instances.GroupBy(i => i.ConnectionInfo.Server) .Select(g => { g.Key.Instances = g.ToList(); return(g.Key); }) .ToList(); ReplicationGroups = Hosts.Where(h => h.ReplicationGroupName.HasValue()) .GroupBy(h => h.ReplicationGroupName) .Select(g => new RedisReplicationGroup(g.Key, g.ToList())) .ToList(); HostNames = new HashSet <string>(Hosts.Select(h => h.HostName), StringComparer.OrdinalIgnoreCase); }
public DashboardModule(IConfiguration config, ILoggerFactory loggerFactory, PollingService poller, AddressCache addressCache) : base(config, poller) { AddressCache = addressCache; var providers = Settings.Providers; if (providers?.Any() != true) { Providers.Add(new EmptyDataProvider(this, "EmptyDataProvider")); return; } Enabled = true; foreach (var p in providers.All) { p?.Normalize(); } // Add each provider type here if (providers.Bosun != null) { Providers.Add(new BosunDataProvider(this, providers.Bosun)); } if (providers.Orion != null) { Providers.Add(new OrionDataProvider(this, providers.Orion)); } if (providers.WMI != null && OperatingSystem.IsWindows()) { Providers.Add(new WmiDataProvider(this, providers.WMI)); } if (providers.SignalFx != null) { Providers.Add(new SignalFxDataProvider(this, providers.SignalFx, loggerFactory.CreateLogger <SignalFxDataProvider>())); } Providers.ForEach(p => p.TryAddToGlobalPollers()); AllCategories = Settings.Categories .Select(sc => new DashboardCategory(this, sc)) .ToList(); }
void IParsingLifetimeEventHandler.ParsingCompleted() { AddressCache.Clear(); }
public void Output(Stream outputStream) { int lengthOfDelta = CalculateLengthOfTheDeltaEncoding(); int windowSize = lengthOfDelta + 1 + VarIntBE.CalcInt32Length((int)dictionarySize) + VarIntBE.CalcInt32Length(0); VarIntBE.CalcInt32Length(lengthOfDelta); //Google's Checksum Implementation Support if (this.ChecksumFormat != ChecksumFormat.None) { outputStream.WriteByte((byte)VCDiffWindowFlags.VCDSOURCE | (byte)VCDiffWindowFlags.VCDCHECKSUM); //win indicator } else { outputStream.WriteByte((byte)VCDiffWindowFlags.VCDSOURCE); //win indicator } VarIntBE.AppendInt32((int)dictionarySize, outputStream); //dictionary size VarIntBE.AppendInt32(0, outputStream); //dictionary start position 0 is default aka encompass the whole dictionary VarIntBE.AppendInt32(lengthOfDelta, outputStream); //length of delta //begin of delta encoding long sizeBeforeDelta = outputStream.Position; VarIntBE.AppendInt32((int)targetLength, outputStream); //final target length after decoding outputStream.WriteByte(0x00); // uncompressed // [Here is where a secondary compressor would be used // if the encoder and decoder supported that feature.] //non interleaved then it is separeat areas for each type if (!IsInterleaved) { VarIntBE.AppendInt32((int)dataForAddAndRun.Length, outputStream); //length of add/run VarIntBE.AppendInt32((int)instructionAndSizes.Length, outputStream); //length of instructions and sizes VarIntBE.AppendInt32((int)addressForCopy.Length, outputStream); //length of addresses for copys switch (this.ChecksumFormat) { //Google Checksum Support case ChecksumFormat.SDCH: VarIntBE.AppendInt64(this.Checksum, outputStream); break; // Xdelta checksum support. case ChecksumFormat.Xdelta3: { Span <byte> checksumBytes = stackalloc [] { (byte)(this.Checksum >> 24), (byte)(this.Checksum >> 16), (byte)(this.Checksum >> 8), (byte)(this.Checksum & 0x000000FF) }; outputStream.Write(checksumBytes); break; } } outputStream.Write(dataForAddAndRun.GetBuffer().AsSpan(0, (int)dataForAddAndRun.Length)); //data section for adds and runs outputStream.Write(instructionAndSizes.GetBuffer().AsSpan(0, (int)instructionAndSizes.Length)); //data for instructions and sizes outputStream.Write(addressForCopy.GetBuffer().AsSpan(0, (int)addressForCopy.Length)); //data for addresses section copys } else { //interleaved everything is woven in and out in one block VarIntBE.AppendInt32(0, outputStream); //length of add/run VarIntBE.AppendInt32((int)instructionAndSizes.Length, outputStream); //length of instructions and sizes + other data for interleaved VarIntBE.AppendInt32(0, outputStream); //length of addresses for copys //Google Checksum Support if (this.ChecksumFormat == ChecksumFormat.SDCH) { VarIntBE.AppendInt64(Checksum, outputStream); } outputStream.Write(instructionAndSizes.GetBuffer().AsSpan(0, (int)instructionAndSizes.Length)); //data for instructions and sizes, in interleaved it is everything } //end of delta encoding long sizeAfterDelta = outputStream.Position; if (lengthOfDelta != sizeAfterDelta - sizeBeforeDelta) { throw new IOException("Delta output length does not match"); } dataForAddAndRun.SetLength(0); instructionAndSizes.SetLength(0); addressForCopy.SetLength(0); if (targetLength == 0) { throw new IOException("Empty target window"); } addrCache = new AddressCache(); }
async Task EnsureMineToAddresses() { var mineToAddresses = new List <BitcoinWitPubKeyAddress>(); if (this.appConfiguration.MineToAddress != null) { mineToAddresses.Add(this.appConfiguration.MineToAddress); this.logger.LogInformation($"Mining to address {this.appConfiguration.MineToAddress} (set in x1-producer.config)."); AddressCache.SetMineToAddresses(mineToAddresses); return; } this.logger.LogInformation("Trying to extract mine-to-addresses from the wallet. The wallet must contain at least one unspent output."); while (true) { try { var getUnspentOutputsResponse = await this.rpcClient.GetUnspentOutputs(); if (getUnspentOutputsResponse.Status != 200) { throw new InvalidOperationException(getUnspentOutputsResponse.StatusText); } var unspentOutputs = getUnspentOutputsResponse.Result.result .Where(x => x.address != null) .DistinctBy(x => x.address).ToArray(); if (unspentOutputs.Length == 0) { throw new InvalidOperationException($"Unable to extract addresses for mining from the wallet because it doesn't contain unspent outputs. " + $"You can set an address in x1-producer config: minetoaddress=x11..."); } bool useMiningLabelPrefix = unspentOutputs.Any(x => HasMiningLabelPrefix(x.label)); this.logger.LogInformation(useMiningLabelPrefix ? $"Extracting mine-to addresses where its label is prefixed with '{MiningLabelPrefix}'." : $"Using all addresses as mine-to addresses, because no address has its label prefixed with '{MiningLabelPrefix}'."); foreach (var output in unspentOutputs) { if (!output.spendable || !output.solvable) { continue; } if (useMiningLabelPrefix && !HasMiningLabelPrefix(output.label)) { continue; } try { // use every address that is a valid WPKH address. mineToAddresses.Add(new BitcoinWitPubKeyAddress(output.address, C.Network)); } // ReSharper disable once EmptyGeneralCatchClause catch { } } if (mineToAddresses.Count > 0) { this.logger.LogInformation($"Imported {mineToAddresses.Count} addresses for mining."); AddressCache.SetMineToAddresses(mineToAddresses); return; } throw new InvalidOperationException("No addresses for mining."); } catch (Exception e) { this.logger.LogError($"Error retrieving addresses for mining: {e.Message} Retrying..."); await Task.Delay(2000); } } }
private List <RedisConnectionInfo> LoadRedisConnections(RedisSettings settings, AddressCache addressCache) { var result = new List <RedisConnectionInfo>(); var defaultServerInstances = settings.Defaults.Instances; var allServerInstances = settings.AllServers.Instances ?? Enumerable.Empty <RedisSettings.Instance>(); foreach (var s in settings.Servers) { var server = new RedisHost(this, s); var count = result.Count; // Add instances that belong to any servers foreach (var asi in allServerInstances) { result.Add(new RedisConnectionInfo(server, asi, addressCache)); } // Add instances defined on this server foreach (var si in s.Instances) { result.Add(new RedisConnectionInfo(server, si, addressCache)); } // If we have no instances added at this point, defaults it is! if (defaultServerInstances != null && count == result.Count) { foreach (var dsi in defaultServerInstances) { result.Add(new RedisConnectionInfo(server, dsi, addressCache)); } } } return(result); }
public void Output(ByteStreamWriter sout) { int lengthOfDelta = CalculateLengthOfTheDeltaEncoding(); int windowSize = lengthOfDelta + 1 + VarIntBE.CalcInt32Length((int)dictionarySize) + VarIntBE.CalcInt32Length(0); VarIntBE.CalcInt32Length(lengthOfDelta); //Google's Checksum Implementation Support if (hasChecksum) { sout.writeByte((byte)VCDiffWindowFlags.VCDSOURCE | (byte)VCDiffWindowFlags.VCDCHECKSUM); //win indicator } else { sout.writeByte((byte)VCDiffWindowFlags.VCDSOURCE); //win indicator } VarIntBE.AppendInt32((int)dictionarySize, sout); //dictionary size VarIntBE.AppendInt32(0, sout); //dictionary start position 0 is default aka encompass the whole dictionary VarIntBE.AppendInt32(lengthOfDelta, sout); //length of delta //begin of delta encoding Int64 sizeBeforeDelta = sout.Position; VarIntBE.AppendInt32((int)targetLength, sout); //final target length after decoding sout.writeByte(0x00); //uncompressed // [Here is where a secondary compressor would be used // if the encoder and decoder supported that feature.] //non interleaved then it is separata areas for each type if (!interleaved) { VarIntBE.AppendInt32(dataForAddAndRun.Count, sout); //length of add/run VarIntBE.AppendInt32(instructionAndSizes.Count, sout); //length of instructions and sizes VarIntBE.AppendInt32(addressForCopy.Count, sout); //length of addresses for copys //Google Checksum Support if (hasChecksum) { VarIntBE.AppendInt64(checksum, sout); } sout.writeBytes(dataForAddAndRun.ToArray()); //data section for adds and runs sout.writeBytes(instructionAndSizes.ToArray()); //data for instructions and sizes sout.writeBytes(addressForCopy.ToArray()); //data for addresses section copys } else { //interleaved everything is woven in and out in one block VarIntBE.AppendInt32(0, sout); //length of add/run VarIntBE.AppendInt32(instructionAndSizes.Count, sout); //length of instructions and sizes + other data for interleaved VarIntBE.AppendInt32(0, sout); //length of addresses for copys //Google Checksum Support if (hasChecksum) { VarIntBE.AppendInt64(checksum, sout); } sout.writeBytes(instructionAndSizes.ToArray()); //data for instructions and sizes, in interleaved it is everything } //end of delta encoding Int64 sizeAfterDelta = sout.Position; if (lengthOfDelta != sizeAfterDelta - sizeBeforeDelta) { Console.WriteLine("Delta output length does not match"); } dataForAddAndRun.Clear(); instructionAndSizes.Clear(); addressForCopy.Clear(); if (targetLength == 0) { Console.WriteLine("Empty target window"); } addrCache = new AddressCache(); }
internal RedisConnectionInfo(RedisHost server, RedisSettings.Instance settings, AddressCache addressCache) { Server = server; Settings = settings; AddressCache = addressCache; }