示例#1
0
 public BlockChain(Nonced <BlockChain> previousBlock, DateTime timestamp, int Target, string Message)
 {
     this.PreviousBlock = previousBlock;
     this.Timestamp     = timestamp;
     this.Target        = Target;
     this.Message       = Message;
 }
示例#2
0
        public void TestBlockchainInvalidViaWrongHash()
        {
            BlockChain genesisBlockTemplate = new BlockChain(null, DateTime.Now, 4, "Howdy World!");

            Assert.True(new BlockChainValidator().Validate(genesisBlockTemplate));
            var invalidGenesisBlock = new Nonced <BlockChain>(genesisBlockTemplate, 1337);

            Assert.False(new BlockChainValidator().Validate(invalidGenesisBlock));
        }
示例#3
0
        public bool Validate(Nonced <BlockChain> chain)
        {
            DagNode chainNode     = IpfsDagSerialization.MapToDag(chain);
            var     chainNodeHash = Base58.Decode(chainNode.Hash);

            if (Miner.FoundGoldenNonce(chainNodeHash, chain.Value.Target))
            {
                return(Validate(chain.Value));
            }
            return(false);
        }
示例#4
0
        private void addCompetingBlock(Nonced <BlockChain> block)
        {
            var valid = new BlockChainValidator().Validate(block);

            if (valid)
            {
                if (currentBlock == null)
                {
                    currentBlock = block;
                }

                // descended from block
                // Below the tree
                if (block.Value.GetBlockchainDepth() > currentBlock.Value.GetBlockchainDepth())
                {
                }
            }
        }
示例#5
0
        public void StartMining(CancellationToken allMiningCancellationToken)
        {
            while (ContinueMining(allMiningCancellationToken))
            {
                CullCandidateBlocks();

                // We need to wait for a transaction before we start mining.
                if (currentTransaction == null)
                {
                    allMiningCancellationToken.WaitHandle.WaitOne(50);
                    fullStopCancellationToken.WaitHandle.WaitOne(50);
                    continue;
                }

                if (currentBlock == null)
                {
                    currentBlock = BegForCurrentBlock();
                    if (currentBlock == null)
                    {
                        // If cancelled or didn't find a block
                        break;
                    }
                    continue;
                }

                currentBlock = GetLongestBlock();

                if (currentBlock == null)
                {
                    continue;
                }

                BlockChain candidateChain = new BlockChain(currentBlock, DateTime.Now, GetCurrentDifficulty(), currentTransaction);
                var        minedResult    = Miner.Mine(candidateChain, candidateChain.Target, allMiningCancellationToken);

                if (minedResult != null)
                {
                    competingBlocks.Add(minedResult);
                    OnFoundBlock?.Invoke(this, new BlockFoundEventArgs(minedResult));
                }
            }
        }
示例#6
0
 public BlockFoundEventArgs(Nonced <BlockChain> minedResult)
 {
     FoundBlock = minedResult;
 }
示例#7
0
 public BlockChainProtocol(Nonced <BlockChain> startingBlock, CancellationToken fullStopCancellationToken)
 {
     competingBlocks.Add(startingBlock);
     currentBlock = startingBlock;
     this.fullStopCancellationToken = fullStopCancellationToken;
 }