示例#1
0
 private void ValidateBlockHash(Block block, int nonce, string hash)
 {
     if (!ProofOfWork.IsProofValid(block.Difficulty, block.Index, block.BlockDataHash, block.PreviousBlockHash, block.CreatedDate, nonce, hash))
     {
         throw new Exception("Invalid proof of work");
     }
 }
示例#2
0
        static void Main(string[] args)
        {
            IProofOfWork proofOfWork  = new ProofOfWork();
            ICryptoUtil  cryptoUtil   = new CryptoUtil();
            string       minerAddress = defaultMinerAddress;
            string       nodeAddress  = defaultNodeAddress;

            if (args.Length > 0)
            {
                if (cryptoUtil.IsAddressValid(args[0]))
                {
                    minerAddress = args[0];
                }
                else
                {
                    Output.WriteError($"Prvided address is invalid: {args[0]}. Fallback to default address: {defaultMinerAddress}");
                }
            }

            if (args.Length > 1)
            {
                nodeAddress = args[1];
            }

            Console.WriteLine($"Statring mining for address {minerAddress}");
            Console.WriteLine($"Statring mining for node: {nodeAddress}");

            Stopwatch  sw    = Stopwatch.StartNew();
            BlockInput input = Get <BlockInput>(nodeAddress + "/api/mining/getBockForMine/" + minerAddress);

            while (true)
            {
                sw.Restart();

                Boolean blockFound = false;
                int     nonce      = 0;

                string precomputedData = proofOfWork.PrecomputeData(input.BlockIndex, input.BlockHash, input.PrevBlockHash, input.Timestamp);

                Console.WriteLine($"New job started at : " + DateTime.Now + " for block index " + input.BlockIndex);


                string data;
                string blockHash;
                while (!blockFound && nonce < int.MaxValue)
                {
                    blockHash = proofOfWork.Compute(precomputedData, nonce);

                    if (proofOfWork.IsProofValid(blockHash, input.Difficulty))
                    {
                        var blockFoundResult = MakePost(nodeAddress + "/api/mining/noncefound", new BlockMinedRequest {
                            MinerAddress = minerAddress, Nonce = nonce, Hash = blockHash
                        });
                        if (blockFoundResult)
                        {
                            Output.WriteSuccess($"Block mined. Nonce: {nonce} , Hash: {blockHash}");
                        }
                        else
                        {
                            Output.WriteError("Block mined, but not accepted :(");
                        }
                        blockFound = true;
                    }

                    if (blockFound || (nonce % 1000 == 0 && sw.Elapsed >= timeLimit))
                    {
                        var requestedBlockToMine = Get <BlockInput>(nodeAddress + "/api/mining/getBockForMine/" + minerAddress);
                        if (blockFound || requestedBlockToMine.BlockHash != input.BlockHash || requestedBlockToMine.BlockIndex != input.BlockIndex)
                        {
                            input = requestedBlockToMine;
                            break;
                        }
                        sw.Restart();
                    }
                    nonce++;
                }
            }
        }