Ejemplo n.º 1
0
        internal bool Mine(Block seed, CancellationToken token = default(CancellationToken))
        {
            var rnd       = new Random();
            var nonceSeed = new byte[sizeof(ulong)];

            rnd.NextBytes(nonceSeed);

            ulong nonce = BitConverter.ToUInt64(nonceSeed, 0);

            while (!token.IsCancellationRequested)
            {
                seed.Nonce     = nonce++;
                seed.Timestamp = DateTime.UtcNow;

                var data    = MessagePack.MessagePackSerializer.Serialize(seed);
                var blockId = BlockUtils.ComputeBlockId(data);
                if (CryptoUtils.Difficulty(blockId) > seed.Difficulty)
                {
                    seed.Id       = ReadonlyBytes.CopyFrom(blockId);
                    seed.Original = data;
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        public static Block DeserializeBlock(byte[] data)
        {
            var block = MessagePack.MessagePackSerializer.Deserialize <Block>(data);

            block.Original = data;
            block.Id       = ReadonlyBytes.CopyFrom(ComputeBlockId(data));
            return(block);
        }
Ejemplo n.º 3
0
        internal Transaction CreateCoinbaseTransaction(int height, byte[] recipient)
        {
            var transaction = new Transaction
            {
                Timestamp  = DateTime.UtcNow,
                InEntries  = new List <InEntry>(),
                OutEntries = new List <OutEntry>
                {
                    new OutEntry
                    {
                        Amount        = BlockUtils.GetCoinbaseAmount(height: 0),
                        RecipientHash = ReadonlyBytes.CopyFrom(recipient),
                    },
                },
            };

            var data = transaction.Original = MessagePack.MessagePackSerializer.Serialize(transaction);

            transaction.Id = ReadonlyBytes.CopyFrom(BlockUtils.ComputeTransactionId(data));
            return(transaction);
        }
Ejemplo n.º 4
0
 internal void ProcessBlock(byte[] original, ReadonlyBytes previousHash)
 {
     lock (this) {
     }
 }