private bool OnUnvalidatedBlockCreated(object sender, BlockCreatedEventArgs ev) { if (ev.Block.Header.MagicNumber != _networkIdentifier) { return(false); } var blockExists = true; if (sender != this) { try { _blockchainRepo.GetBlockByHash(ev.Block.Header.Hash, ev.Block.Header.MagicNumber); } catch (KeyNotFoundException) { blockExists = false; } } if (!blockExists) { CheckForDifficultyUpdate(); var target = BlockchainConstants.MaximumTarget / difficulty; try { _blockValidator.ValidateBlock(ev.Block, target, _blockchain, true, true); _logger.LogInformation("Received block from remote node"); _logger.LogDebug("Current height: {0}", _blockchain.CurrentHeight); // Do not restart the task because that's buggy (it stops, but doesn't start) foreach (var tx in ev.Block.Transactions) { _txPool.RemoveTransaction(tx); } } catch (BlockRejectedException ex) { _logger.LogInformation("Block with hash {0} was rejected: {1}", ex.Block.Header.Hash, ex.Message); return(false); } catch (TransactionRejectedException ex) { _logger.LogInformation("Block with transaction hash {0} was rejected: {1}", ex.Transaction.Hash, ex.Message); return(false); } } return(true); }
public IEnumerable <Sku> GetSkuWithHistory(string createdSkuBlockHash, int skuTxIndex, string netId) { var transactions = _transactionRepo.GetAll(netId).OfType <StateTransaction>(); var skuHistoryList = new List <Sku>(); var skuCreationBlock = _blockchainRepo .GetBlockByHash(createdSkuBlockHash, netId); var skuCreationTransaction = skuCreationBlock .Transactions.OfType <StateTransaction>() .ToList() [skuTxIndex]; var skuCreationData = JsonConvert.DeserializeObject <SkuData>(skuCreationTransaction.Data); var allOtherSkuTransactions = transactions.Where(tx => tx.SkuBlockHash == createdSkuBlockHash && tx.SkuTxIndex == skuTxIndex && (tx.Action == TransactionAction.ChangeSku.ToString() || tx.Action == TransactionAction.CreateSupply.ToString() || tx.Action == TransactionAction.TransferSupply.ToString() || tx.Action == TransactionAction.DestroySupply.ToString()) ); skuHistoryList.Add(new Sku(skuCreationBlock, skuCreationTransaction, skuCreationData)); // Fill the list with transactions about this SKU foreach (var skuTransaction in allOtherSkuTransactions) { // todo handle repo exceptions var block = _blockchainRepo.GetBlockByTransactionHash(skuTransaction.Hash, netId); try { var skuChange = new Sku(block, skuTransaction, JsonConvert.DeserializeObject <SkuData>(skuTransaction.Data)); skuHistoryList.Add(skuChange); } catch (JsonReaderException) { var skuChange = new Sku(block, skuTransaction, skuCreationData); skuHistoryList.Add(skuChange); } } return(skuHistoryList); }
private StateTransaction CheckSkuBlockHashAndTxIndex(string skuBlockHash, int skuTxIndex, TransactionAction expectedTxAction, string netId) { try { var block = _blockchainRepository.GetBlockByHash(skuBlockHash, netId); var transaction = block.Transactions.ToList()[skuTxIndex]; if (transaction.Action != expectedTxAction.ToString()) { throw new TransactionRejectedException("Invalid transaction action in SkuBlock. Expected action: " + expectedTxAction.ToString()); } return((StateTransaction)transaction); } catch (KeyNotFoundException e) { throw new TransactionRejectedException(e.Message); } catch (IndexOutOfRangeException) { throw new TransactionRejectedException("SKU transaction does not exist in SkuBlock"); } }
private IEnumerable <Block> GetBlocksFromHash(string beginHash, string stopHash, bool includeBeginBlock) { var blocks = new List <Block>(); bool stopSearching = false; var bLockchain = _blockchainRepo.GetChainByNetId(_netId); // We fetch the blockchain to put a threadlock on it. lock (bLockchain) { var previousBlock = _blockchainRepo.GetBlockByHash(beginHash, _netId); var i = 0; if (includeBeginBlock) { blocks.Add(previousBlock); } while (i < NetworkConstants.MaxHeadersInMessage && !stopSearching) { // Stale blocks / side chains are not supported here try { previousBlock = _blockchainRepo.GetBlockByPreviousHash(previousBlock.Header.Hash, _netId); blocks.Add(previousBlock); if (previousBlock.Header.Hash == stopHash) { stopSearching = true; } } catch (KeyNotFoundException) { stopSearching = true; // No more blocks found } i++; } } return(blocks); }