public void TestGenesisEncode() { Chain chain = new Chain(Genesis.CreateGenesis()); ChainSerializer serializer = new ChainSerializer(); var encrypt = serializer.Encode(chain.BlockChain); var decrypt = serializer.Decode(serializer.ConcateByteArray(encrypt)); Assert.AreEqual(((List <Block>)decrypt).Count, chain.BlockChain.Count); }
public void TestChainEncode() { IBlockRepository subscriber = new BlockRepository(); var blocks = subscriber.GetBlocks(); Chain chain = new Chain(blocks); ChainSerializer serializer = new ChainSerializer(); var encrypt = serializer.Encode(chain.BlockChain); var decrypt = serializer.Decode(serializer.ConcateByteArray(encrypt)); Assert.AreEqual(((List <Block>)decrypt).Count, chain.BlockChain.Count); }
public async Task GetBlockChainRequestHandler(AspNetWebSocketContext webSocketContext) { WebSocket webSocket = webSocketContext.WebSocket; ArraySegment <Byte> receiveData = new ArraySegment <Byte>(new Byte[maxMessageSize]); var cancelationToken = new CancellationToken(); while (webSocket.State == WebSocketState.Open) { HexString rawBlockChain; using (MemoryStream stream = new MemoryStream(maxMessageSize)) { WebSocketReceiveResult result = await webSocket.ReceiveAsync(receiveData, cancelationToken); if (result.MessageType == WebSocketMessageType.Close) { await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, cancelationToken); } else { await stream.WriteAsync(receiveData.Array, receiveData.Offset, receiveData.Count); stream.Seek(0, SeekOrigin.Begin); using (BinaryReader reader = new BinaryReader(stream)) { rawBlockChain = new HexString(reader.ReadBytes((int)stream.Length)); } ChainSerializer serializer = new ChainSerializer(); this.GlobalChain = serializer.Decode(rawBlockChain.ToByteArray()); } } } }
private async void ProccessRequest(HttpListenerContext httpListenerContext) { WebSocketContext webSocketContext = null; string ipAddress; try { webSocketContext = await httpListenerContext.AcceptWebSocketAsync(subProtocol : null); ipAddress = httpListenerContext.Request.RemoteEndPoint.ToString(); } catch (Exception) { httpListenerContext.Response.StatusCode = 500; httpListenerContext.Response.Close(); return; } WebSocket webSocket = webSocketContext.WebSocket; try { byte[] receiveBuffer = new byte[256]; ChainSerializer chainSerializer = new ChainSerializer(); IEnumerable <Block> blocks; BlockRepository subscriber = new BlockRepository(); ArraySegment <byte> segment = new ArraySegment <byte>(receiveBuffer); byte[] block = new byte[256]; while (true) { using (MemoryStream stream = new MemoryStream()) { WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync( segment, CancellationToken.None); if (receiveResult.MessageType == WebSocketMessageType.Close) { await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None); } else { await stream.WriteAsync(segment.Array, segment.Offset, segment.Count); stream.Seek(0, SeekOrigin.Begin); do { using (BinaryReader reader = new BinaryReader(stream)) { block = reader.ReadBytes((int)stream.Length); } } while (!receiveResult.EndOfMessage); blocks = chainSerializer.Decode(block); if (blocks.Count() > 0) { var block_list = blocks.ToList(); var localBlocks = subscriber.GetBlocks(); if (localBlocks.Any(b => b.Hash == block_list[0].Hash)) { return; } else { subscriber.AddBlock(block_list[0]); } } } } } } catch (Exception ex) { log.Error("Error when adding block, reason: " + ex.Message); httpListenerContext.Response.StatusCode = 500; // httpListenerContext.Response.Close(); return; } finally { if (webSocket != null) { webSocket.Dispose(); } } }