public void ReceiveBlock(IBlockchainPeer sender, IBlock block) { try { // TODO: full block check // TODO: implement remembering the last block of the main chain for fork verification // TODO: implement a pool of orphan blocks in case of receiving a block for which its parent block was not previously received //fChain.AddBlock(); // TODO: what should be the mechanism of protection against flooding here?! //BroadcastBlock(block); } catch { // if !block.IsCorrect(), then do nothing } }
/// <summary> /// The function accepts a pending transaction from another node. /// </summary> public void ReceiveTransaction(IBlockchainPeer sender, Transaction transaction) { try { // TODO: check that the transaction is not in the pool (if received earlier from another node and has not yet been processed) // or blockchain (if an old, long-processed transaction has been received) // TODO: transaction validation should take into account the scenario when the receive order is violated and the transaction // tries to modify data not yet added by a transaction that is not in the pool (leave pending) if (VerifyTransactions(transaction)) { AddPendingTransaction(transaction); // TODO: what should be the mechanism of protection against flooding here?! //BroadcastTransaction(transaction); } } catch { // if !trx.IsCorrect(), then do nothing } }
/// <summary> /// Replenishment of the blockchain with a list of blocks received from a peer. /// </summary> public void ReceiveBlocksResponse(IBlockchainPeer peer, string json) { var newBlocks = Helpers.DeserializeBlocks(json); if (!newBlocks.IsCorrect()) { throw new MethodResultException("newBlocks", "Error receive block chain. The chain is incorrect."); } var lastBlock = fDataProvider.GetLastBlock(); if (lastBlock != null && lastBlock.Index == 0) { // A node only has a genesis block // Therefore, the response will come with the full chain, including the genesis block fDataProvider.ClearBlocks(); } foreach (var blk in newBlocks) { fDataProvider.AddBlock(blk); ProcessBlockTransactions(blk); } }
/// <summary> /// Received from the connected peer a response with the characteristics (index, hash) of the last block in the chain. /// </summary> public void ReceiveChainStateRequest(IBlockchainPeer peer, long peerLastBlockIndex, string peerLastBlockHash) { var lastBlock = fDataProvider.GetLastBlock(); if (lastBlock != null && peerLastBlockIndex < lastBlock.Index) { if (peerLastBlockIndex > 0) { // The chain has the specified common block? var peerLastBlock = fDataProvider.FindBlock(peerLastBlockHash); if (peerLastBlock != null && peerLastBlock.Index == peerLastBlockIndex) { var blocks = fDataProvider.GetBlocks(peerLastBlockIndex + 1); SendBlocks(peer, blocks); } } else { // A peer only has a genesis block var blocks = fDataProvider.GetBlocks(0); SendBlocks(peer, blocks); } } }
public void SendBlock(IBlockchainPeer peer, IBlock block) { }
public void SendTransaction(IBlockchainPeer peer, ITransaction transaction) { }
public bool SendBlocks(IBlockchainPeer peer, IList <Block> blocks) { return(false); }
/// <summary> /// Send connected peers a request for characteristics (index, hash) of the last blocks in their chains. /// </summary> private void SendChainStateRequest(IBlockchainPeer peer, long selfLastBlockIndex, string selfLastBlockHash) { // dummy }