public BlockChain(Nonced <BlockChain> previousBlock, DateTime timestamp, int Target, string Message) { this.PreviousBlock = previousBlock; this.Timestamp = timestamp; this.Target = Target; this.Message = Message; }
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)); }
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); }
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()) { } } }
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)); } } }
public BlockFoundEventArgs(Nonced <BlockChain> minedResult) { FoundBlock = minedResult; }
public BlockChainProtocol(Nonced <BlockChain> startingBlock, CancellationToken fullStopCancellationToken) { competingBlocks.Add(startingBlock); currentBlock = startingBlock; this.fullStopCancellationToken = fullStopCancellationToken; }