Exemple #1
0
        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);
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        private async Task <object> SendChainFromWebSockets(AspNetWebSocketContext context)
        {
            var handler = new BlockChainHandler();

            byte[]          buffer          = new byte[1024 * 1024];
            ChainSerializer chainSerializer = new ChainSerializer();

            var encode_tuple            = chainSerializer.Encode(connector.GetLocalChain().BlockChain);
            ArraySegment <byte> segment =
                new ArraySegment <byte>(chainSerializer.ConcateByteArray(encode_tuple));

            WebSocket socket = context.WebSocket;

            try
            {
                handler.OnOpen();

                WebSocketReceiveResult receiveResult;

                while (socket.State == WebSocketState.Open)
                {
                    do
                    {
                        receiveResult = await socket.ReceiveAsync(
                            segment, CancellationToken.None);

                        if (receiveResult.MessageType == WebSocketMessageType.Close)
                        {
                            await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
                        }
                    } while (!receiveResult.EndOfMessage);

                    await socket.SendAsync(segment, WebSocketMessageType.Binary, true, CancellationToken.None);
                }
            }

            catch (Exception ex)
            {
                log.Error("Error when respond blockchain " + ex.Message);
                throw new InvalidBlockException("Error when respond blockchain ");
            }
            finally
            {
                handler.OnClose();
            }

            var processTask = handler.ProcessWebSocketRequestAsync(context);

            return(processTask);
        }