public bool HandleBlockFromPeer(BlockInfo blockWithTransactions, ECDSAPublicKey publicKey) { Logger.LogTrace("HandleBlockFromPeer"); lock (_blocksLock) { var block = blockWithTransactions.Block; var receipts = blockWithTransactions.Transactions; Logger.LogDebug( $"Got block {block.Header.Index} with hash {block.Hash.ToHex()} from peer {publicKey.ToHex()}"); var myHeight = _blockManager.GetHeight(); if (block.Header.Index != myHeight + 1) { Logger.LogTrace( $"Skipped block {block.Header.Index} from peer {publicKey.ToHex()}: our height is {myHeight}"); return(false); } if (!block.TransactionHashes.ToHashSet().SetEquals(receipts.Select(r => r.Hash))) { try { var needHashes = string.Join(", ", block.TransactionHashes.Select(x => x.ToHex())); var gotHashes = string.Join(", ", receipts.Select(x => x.Hash.ToHex())); Logger.LogTrace( $"Skipped block {block.Header.Index} from peer {publicKey.ToHex()}: expected hashes [{needHashes}] got hashes [{gotHashes}]"); } catch (Exception e) { Logger.LogWarning($"Failed to get transaction receipts for tx hash: {e}"); } return(false); } var error = _blockManager.VerifySignatures(block, true); if (error != OperatingError.Ok) { Logger.LogTrace($"Skipped block {block.Header.Index} from peer {publicKey.ToHex()}: invalid multisig with error: {error}"); return(false); } // This is to tell consensus manager to terminate current era, since we trust given multisig OnSignedBlockReceived?.Invoke(this, block.Header.Index); error = _stateManager.SafeContext(() => { if (_blockManager.GetHeight() + 1 == block.Header.Index) { return(_blockManager.Execute(block, receipts, commit: true, checkStateHash: true)); } Logger.LogTrace( $"We have blockchain with height {_blockManager.GetHeight()} but got block {block.Header.Index}"); return(OperatingError.BlockAlreadyExists); }); if (error == OperatingError.BlockAlreadyExists) { Logger.LogTrace( $"Skipped block {block.Header.Index} from peer {publicKey.ToHex()}: block already exists"); return(true); } if (error != OperatingError.Ok) { Logger.LogWarning( $"Unable to persist block {block.Header.Index} (current height {_blockManager.GetHeight()}), got error {error}, dropping peer"); return(false); } lock (_peerHasBlocks) Monitor.PulseAll(_peerHasBlocks); return(true); } }