working on it ...

Filters

Explore Public Snippets

Sort by

Found 56 snippets matching: coiniumserv-develop

    public by Geometry  87810  2  4  0

    PubKeyToScript: For POS coins - used to format wallet address pubkey to use in generation transaction's output.

    For POS coins - used to format wallet address pubkey to use in generation transaction's output. nodejs: https://github.com/zone117x/node-stratum-pool/blob/3586ec0d7374b2acc5a72d5ef597da26f0e39d54/lib/util.js#L243 nodejs: http://runnable.com/me/VCFHE0RrZnwbsQ6y
    using System;
    using System.IO;
    using System.Linq;
    using CoiniumServ.Coin.Address;
    using CoiniumServ.Coin.Address.Exceptions;
    using CoiniumServ.Cryptology;
    using CoiniumServ.Utils.Extensions;
    using CoiniumServ.Utils.Numerics;
    using Gibbed.IO;
    using Serilog;
    
    /// <summary>
    /// For POS coins - used to format wallet address pubkey to use in generation transaction's output.
    /// </summary>
    /// <param name="key"></param>
    /// <example>
    /// nodejs: https://github.com/zone117x/node-stratum-pool/blob/3586ec0d7374b2acc5a72d5ef597da26f0e39d54/lib/util.js#L243
    /// nodejs: http://runnable.com/me/VCFHE0RrZnwbsQ6y
    /// </example>
    /// <returns></returns>
    public static byte[] PubKeyToScript(string key)
    {
        var pubKey = key.HexToByteArray();
    
        if (pubKey.Length != 33)
        {
            Log.Error("invalid pubkey length for {0:l}", key);
            return null;
        }
    
        byte[] result;
    
        using (var stream = new MemoryStream())
        {
            stream.WriteValueU8(0x21);
            stream.WriteBytes(pubKey);
            stream.WriteValueU8(0xac);
            result = stream.ToArray();
        }
    
        return result;
    }

    public by Geometry  78392  0  4  0

    SerializeHeader: Block headers are sent in a headers packet in response to a getheaders message.

    Block headers are sent in a headers packet in response to a getheaders message. https://en.bitcoin.it/wiki/Protocol_specification#Block_Headers nodejs: https://github.com/zone117x/node-stratum-pool/blob/master/lib/blockTemplate.js#L85
    using System;
    using System.IO;
    using System.Text;
    using CoiniumServ.Jobs;
    using CoiniumServ.Utils.Extensions;
    using CoiniumServ.Utils.Helpers;
    using Gibbed.IO;
    
    /// <summary>
    /// Block headers are sent in a headers packet in response to a getheaders message.
    /// </summary>
    /// <remarks>
    /// https://en.bitcoin.it/wiki/Protocol_specification#Block_Headers
    /// </remarks>
    /// <example>
    /// nodejs: https://github.com/zone117x/node-stratum-pool/blob/master/lib/blockTemplate.js#L85
    /// </example>
    /// <param name="job"></param>
    /// <param name="merkleRoot"></param>
    /// <param name="nTime"></param>
    /// <param name="nonce"></param>
    /// <returns></returns>
    public static byte[] SerializeHeader(IJob job, byte[] merkleRoot, UInt32 nTime, UInt32 nonce)
    {
        byte[] result;
    
        using (var stream = new MemoryStream())
        {
            stream.WriteValueU32(nonce.BigEndian());
            stream.WriteValueU32(Convert.ToUInt32(job.EncodedDifficulty, 16).BigEndian());
            stream.WriteValueU32(nTime.BigEndian());
            stream.WriteBytes(merkleRoot);
            stream.WriteBytes(job.PreviousBlockHash.HexToByteArray());
            stream.WriteValueU32(job.BlockTemplate.Version.BigEndian());
    
            result = stream.ToArray();
            result = result.ReverseBytes();
        }
    
        return result;
    }

    public by Geometry  58934  0  4  0

    RegisterCommandGroups: Finds and registers commands that exist in assembly.

    Finds and registers commands that exist in assembly.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using Serilog;
    
    private static readonly Dictionary<CommandGroupAttribute, CommandGroup> CommandGroups = new Dictionary<CommandGroupAttribute, CommandGroup>();
    
    static CommandManager()
            {
                RegisterCommandGroups();
            }
    
    /// <summary>
    /// Finds and registers commands that exist in assembly.
    /// </summary>
    private static void RegisterCommandGroups()
    {
        foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
        {
            if (!type.IsSubclassOf(typeof(CommandGroup))) 
                continue;
    
            var attributes = (CommandGroupAttribute[])type.GetCustomAttributes(typeof(CommandGroupAttribute), true);
            if (attributes.Length == 0) 
                continue;
    
            var groupAttribute = attributes[0];
            if (CommandGroups.ContainsKey(groupAttribute))
                Log.Warning("There exists an already registered command group named '{0}'.", groupAttribute.Name);
    
            var commandGroup = (CommandGroup)Activator.CreateInstance(type);
            commandGroup.Register(groupAttribute);
            CommandGroups.Add(groupAttribute, commandGroup);
        }
    }

    public by Geometry  1412  1  4  0

    GetReadableDifficulty: Returns given difficulty value as human readable string.

    Returns given difficulty value as human readable string.
    using System;
    using System.Globalization;
    
    /// <summary>
    /// Returns given difficulty value as human readable string.
    /// </summary>
    /// <param name="difficulty"></param>
    /// <returns></returns>
    public static string GetReadableDifficulty(this double difficulty)
    {
        var index = -1;
        var rate = difficulty;
    
        var units = new[] {"K", "M", "B", "T", "Q"};
    
        if (difficulty < 1000)
            return difficulty.ToString(CultureInfo.InvariantCulture);
    
        do
        {
            rate = rate/1000;
            index++;
        } while (rate > 1000);
    
        return string.Format("{0:0.00} {1}", rate, units[index]);
    }

    public by Geometry  1472  0  4  0

    SerializeNumber: Used to format height and date when putting into script signature:

    Used to format height and date when putting into script signature: Used to format height and date when putting into script signature: https://en.bitcoin.it/wiki/Script https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki#specification Serialized CScript python: http://runnable.com/U3Hb26U1918Zx0NR/bitcoin-coinbase-serialize-number-py
    using System;
    using System.IO;
    using System.Text;
    using CoiniumServ.Jobs;
    using CoiniumServ.Utils.Extensions;
    using CoiniumServ.Utils.Helpers;
    using Gibbed.IO;
    
    /// <summary>
    /// Used to format height and date when putting into script signature:
    /// </summary>
    /// <remarks>
    /// Used to format height and date when putting into script signature: https://en.bitcoin.it/wiki/Script
    /// </remarks>
    /// <specification>https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki#specification</specification>
    /// <param name="value"></param>
    /// <returns>Serialized CScript</returns>
    /// <example>
    /// python: http://runnable.com/U3Hb26U1918Zx0NR/bitcoin-coinbase-serialize-number-python
    /// nodejs: http://runnable.com/U3HgCVY2RIAjrw9I/bitcoin-coinbase-serialize-number-nodejs-for-node-js
    /// </example>
    public static byte[] SerializeNumber(int value)
    {
    
        if (value >= 1 && value <= 16)
            return new byte[] { 0x01, (byte)value };
    
        var buffer = new byte[9];
        byte lenght = 1;
    
        while (value > 127)
        {
            buffer[lenght++] = (byte)(value & 0xff);
            value >>= 8;
        }
    
        buffer[0] = lenght;
        buffer[lenght++] = (byte)value;
    
        return buffer.Slice(0, lenght);
    }

    public by Geometry  1361  1  4  0

    ArgumentNotNull: Arguments the not null.

    Arguments the not null. The argument.
    using System;
    using System.Collections.Generic;
    using System.Linq.Expressions;
    
    /// <summary>
    /// Arguments the not null.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="argument">The argument.</param>
    /// <exception cref="System.ArgumentNullException"></exception>
    public static void ArgumentNotNull<T>(Expression<Func<T>> argument) where T : class
    {
        string name = GetName(argument);
        ArgumentNotNull((argument.Compile())(), name);
    }

    public by Geometry  1251  2  5  0

    Combine: Combines given three byte arrays. <remarks> Originally from: http://stackoverflow.com/a/415839/170181 </remarks>

    Combines given three byte arrays. Originally from: http://stackoverflow.com/a/415839/170181
    using System;
    
    /// <summary>
    /// Combines given three byte arrays.
    /// <remarks>
    /// Originally from: http://stackoverflow.com/a/415839/170181
    /// </remarks>
    /// </summary>
    /// <param name="first"></param>
    /// <param name="second"></param>
    /// <param name="third"></param>
    /// <returns></returns>
    public static byte[] Combine(byte[] first, byte[] second, byte[] third)
    {
        var ret = new byte[first.Length + second.Length + third.Length];
        Buffer.BlockCopy(first, 0, ret, 0, first.Length);
        Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
        Buffer.BlockCopy(third, 0, ret, first.Length + second.Length, third.Length);
        return ret;
    }

    public by Geometry  1329  1  4  0

    MaxBy: Returns the maximal element of the given sequence, based on the given projection.

    // https://code.google.com/p/morelinq/source/browse/MoreLinq/ Returns the maximal element of the given sequence, based on the given projection. If more than one element has the maximal projected value, the first one encountered will be returned. This overload uses the default comparer for
    using System;
    using System.Collections.Generic;
    
    // https://code.google.com/p/morelinq/source/browse/MoreLinq/MaxBy.cs
    
    /// <summary>
    /// Returns the maximal element of the given sequence, based on
    /// the given projection.
    /// </summary>
    /// <remarks>
    /// If more than one element has the maximal projected value, the first
    /// one encountered will be returned. This overload uses the default comparer
    /// for the projected type. This operator uses immediate execution, but
    /// only buffers a single result (the current maximal element).
    /// </remarks>
    /// <typeparam name="TSource">Type of the source sequence</typeparam>
    /// <typeparam name="TKey">Type of the projected element</typeparam>
    /// <param name="source">Source sequence</param>
    /// <param name="selector">Selector to use to pick the results to compare</param>
    /// <returns>The maximal element, according to the projection.</returns>
    /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null</exception>
    /// <exception cref="InvalidOperationException"><paramref name="source"/> is empty</exception>
    public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source,
        Func<TSource, TKey> selector)
    {
        return source.MaxBy(selector, Comparer<TKey>.Default);
    }

    public by Geometry  1319  1  4  0

    Parse: Parses a given line from console as a command if any.

    Parses a given line from console as a command if any. The line to be parsed.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using Serilog;
    
    private static readonly Dictionary<CommandGroupAttribute, CommandGroup> CommandGroups = new Dictionary<CommandGroupAttribute, CommandGroup>();
    
    static CommandManager()
            {
                RegisterCommandGroups();
            }
    
    /// <summary>
    /// Parses a given line from console as a command if any.
    /// </summary>
    /// <param name="line">The line to be parsed.</param>
    public static void Parse(string line)
    {
        string output = string.Empty;
        string command;
        string parameters;
        var found = false;
    
        if (line == null) 
            return;
    
        if (line.Trim() == string.Empty) 
            return;
    
        if (!ExtractCommandAndParameters(line, out command, out parameters))
        {
            output = "Unknown command: " + line;
            Log.Information(output);
            return;
        }
    
        foreach (var pair in CommandGroups)
        {
            if (pair.Key.Name != command) 
                continue;
    
            output = pair.Value.Handle(parameters);
            found = true;
            break;
        }
    
        if (found == false)
            output = string.Format("Unknown command: {0} {1}", command, parameters);
    
        if (output != string.Empty)
            Log.Information(output);
    }

    public by Geometry  1315  1  4  0

    GetReadableHashrate: Returns given hashrate value as human readable string.

    Returns given hashrate value as human readable string.
    using System;
    using System.Globalization;
    
    /// <summary>
    /// Returns given hashrate value as human readable string.
    /// </summary>
    /// <param name="hashrate"></param>
    /// <returns></returns>
    public static string GetReadableHashrate(this UInt64 hashrate)
    {
        var index = -1;
        double rate = hashrate;
    
        var units = new[] {"KH/s", "MH/s", "GH/s", "TH/s", "PH/s", "EH/s", "ZH/s", "YH/s"};
    
        do
        {
            rate = rate/1000;
            index++;
        } while (rate > 1000);
    
        return string.Format("{0:0.00} {1}", rate, units[index]);
    }
    • Public Snippets
    • Channels Snippets