public static void ValidateBlock(Block block)
        {
            // first validate the Nonce matches the difficulty mask
            string mineHash = Sha256Hash.Hash(BitConverter.GetBytes(block.Nonce), block.PreviousBlockHash);

            if (!Regex.IsMatch(mineHash, block.DifficultyMask))
            {
                throw new Exception("Nonce does not match difficulty mask");
            }

            for (int i = 0; i < block.Transactions.Count; i++)
            {
                Transaction t = block.Transactions[i];
                TransactionValidatorService.ValidateTransaction(t);
            }
        }
Пример #2
0
        public void FromGenesisPoW()
        {
            Core core = new Core();

            EcdsaKeyPair genesisWalletKp = new EcdsaKeyPair(Globals.Keys.GenesisPrivateKey);
            EcdsaKeyPair otherKp         = new EcdsaKeyPair();

            Block genesis = core.GenesisBlock;

            // find nonce for nextBlock
            Regex difficultyTestRegex = new Regex(genesis.DifficultyMask);

            UInt16 testNonce = 0;

            while (true)
            {
                string mineHash = Sha256Hash.Hash(BitConverter.GetBytes(testNonce), genesis.PreviousBlockHash);

                if (difficultyTestRegex.IsMatch(mineHash))
                {
                    break;
                }

                testNonce++;
            }

            Block nextBlock = BlockFactory.GenerateBlock(genesisWalletKp.Public);

            Transaction nextTransaction = new Transaction();

            nextTransaction.Inputs.Add(new TransactionInput {
                PreviousTransactionHash = genesis.Transactions[0].Hash, PreviousTransactionOutIndex = 0
            });
            nextTransaction.Outputs.Add(new TransactionOutput
            {
                Amount = 1M,
                To     = otherKp.Public
            });

            nextTransaction.Sign(genesisWalletKp.Private);

            nextBlock.Nonce = testNonce;
            nextBlock.Transactions.Add(nextTransaction);

            BlockValidatorService.ValidateBlock(nextBlock);
        }
Пример #3
0
        internal string ComputeHash()
        {
            List <byte> allBytes = new List <byte>();

            foreach (TransactionInput txIn in this.Inputs)
            {
                allBytes.AddRange(ChainParams.Encoder.GetBytes(txIn.PreviousTransactionHash));
            }

            foreach (TransactionOutput txOut in this.Outputs)
            {
                allBytes.AddRange((byte[])txOut.Amount);
                allBytes.AddRange(ChainParams.Encoder.GetBytes(Sha256Hash.Hash(txOut.To)));
            }

            return(Sha256Hash.Hash(allBytes.ToArray()));
        }