Exemplo n.º 1
0
        public static GetUncommittedBlockHandle DeserializeGetUncommittedBlockHandle(ReadOnlySpan <byte> message)
        {
            var editor             = new SpanBinaryReader(message);
            var header             = ParseMessageHeader(ref editor, GetUncommittedBlockHandle.Info);
            var uncommittedBlockId = TempBlockId.Create(ref editor);

            return(new GetUncommittedBlockHandle(header.RequestId, header.ClientId, uncommittedBlockId));
        }
Exemplo n.º 2
0
        public void AddValidBlocksWithForkAllUncommitted()
        {
            _chain  = new SimpleBlockchain();
            _tmpId1 = _chain.OpenFirstBlock().BlockId;

            _tmpId2 = _chain.OpenBlock(_1).BlockId;
            _tmpId3 = _chain.OpenBlock(_2).BlockId;
            // Second child for second block
            _tmpId4 = _chain.OpenBlock(_2).BlockId;
        }
Exemplo n.º 3
0
 public OpenedBlock(
     uint requestId,
     uint clientId,
     TempBlockId uncommittedBlockId,
     BlockAlias alias)
     : base(requestId, clientId, MessageType.OpenedBlock)
 {
     UncommittedBlockId = uncommittedBlockId;
     Alias = alias;
 }
Exemplo n.º 4
0
 public UncommittedBlockInformation(
     uint requestId,
     uint clientId,
     TempBlockId uncommittedBlockId,
     BlockAlias alias,
     int height,
     BlockAlias parent) :
     base(requestId, clientId, MessageType.UncommittedBlockInfo)
 {
     BlockHeight        = height;
     UncommittedBlockId = uncommittedBlockId;
     Alias  = alias;
     Parent = parent;
 }
Exemplo n.º 5
0
        public void terab_uxto_get_blockinfo_uncommitted()
        {
            TempBlockId blockUcid = terab_utxo_open_block();

            _socket.ExpectConnected(() => true);
            _socket.ExpectConnected(() => true);
            _socket.ExpectConnected(() => true);
            _socket.ExpectAvailable(() => GetBlockInformation.SizeInBytes);

            _socket.ExpectReceive(data =>
            {
                data.Clear();
                Assert.True(data.Length >= GetBlockInformation.SizeInBytes);

                var getInfo = new GetBlockInformation(1, 0, BlockAlias.Genesis);
                MessageSerializers.ClientSerializeGetBlockInformation(getInfo, data);

                return(GetBlockInformation.SizeInBytes);
            });

            _socket.ExpectConnected(() => true);
            _socket.ExpectAvailable(() => 0);

            _socket.ExpectConnected(() => true);
            _socket.ExpectSend(data =>
            {
                Assert.Equal(UncommittedBlockInformation.SizeInBytes, data.Length);
                var actualBlockInfo = MessageSerializers.ClientDeserializeUncommittedBlockInfo(data);
                Assert.Equal(BlockAlias.Genesis, actualBlockInfo.Alias);
                Assert.Equal(BlockAlias.GenesisParent, actualBlockInfo.Parent);
                Assert.Equal(0, actualBlockInfo.BlockHeight);
                Assert.Equal(blockUcid, actualBlockInfo.UncommittedBlockId);
                return(UncommittedBlockInformation.SizeInBytes);
            });

            _dispatcher.ListenToConnections();

            _controllerThread.DoWork();

            _dispatcher.SendResponses();

            _socket.ExpectAllDone();
        }
Exemplo n.º 6
0
 public GetUncommittedBlockHandle(uint requestId, uint clientId, TempBlockId uncommittedBlockId) :
     base(requestId, clientId, MessageType.GetUncommittedBlockHandle)
 {
     UncommittedBlockId = uncommittedBlockId;
 }
Exemplo n.º 7
0
        /// <summary>
        /// Reads blockchain information from a stream in the following order:
        ///  - number of blocks to read
        ///  - blocks with Hash256 representing the blockId first, then int
        ///         representing the parentAlias
        /// The blockHeight is calculated for each block based on the
        /// blockHeight of its parent, so it should not be stored.
        /// The corresponding Serialization method is <see cref="WriteTo"/>.
        /// </summary>
        /// <returns>A new list representing the blockchain.</returns>
        public static SimpleBlockchain ReadFrom(BinaryReader reader)
        {
            var committedBlocks   = new List <CommittedBlock>();
            var uncommittedBlocks = new List <UncommittedBlock>();
            var maxBlockHeight    = 0; // track the blockHeight so that it doesn't have to be recalculated afterward

            // Read blocks
            var committedBlockCount = reader.ReadInt32();

            for (var i = 0; i < committedBlockCount; i++)
            {
                var blockId     = new BlockId(reader.ReadHash256());
                var blockAlias  = new BlockAlias(reader.ReadUInt32());
                var parentAlias = new BlockAlias(reader.ReadUInt32());

                // Only the first block can have parentAlias 0.
                if (parentAlias == BlockAlias.GenesisParent && !blockId.Equals(BlockId.Genesis) ||
                    blockAlias.IsPrior(parentAlias))
                {
                    throw new FormatException(
                              $"Parent alias {parentAlias} is an invalid parent for block {blockAlias}.");
                }

                if (!TryFindCommitted(parentAlias, committedBlocks, out var parentBlock) &&
                    !blockId.Equals(BlockId.Genesis))
                {
                    throw new ArgumentNullException($"No parent {parentAlias} found.");
                }

                // The first block, which is the only one to have parent 0, has block height 0.
                var blockHeight = parentAlias == BlockAlias.GenesisParent
                    ? 0
                    : parentBlock.BlockHeight + 1;
                var newBlock = new CommittedBlock(blockHeight, blockId, blockAlias, parentAlias);

                if (blockHeight > maxBlockHeight)
                {
                    maxBlockHeight = blockHeight;
                }

                committedBlocks.Add(newBlock);
            }

            var numUncommmittedBlocks = reader.ReadInt32();

            for (var i = 0; i < numUncommmittedBlocks; i++)
            {
                var blockId     = new TempBlockId(reader.ReadHash128());
                var blockAlias  = new BlockAlias(reader.ReadUInt32());
                var parentAlias = new BlockAlias(reader.ReadUInt32());

                // Only the first block can have parentAlias 0.
                if ((uncommittedBlocks.Count != 0 || committedBlocks.Count != 0) &&
                    parentAlias == BlockAlias.GenesisParent || blockAlias.IsPrior(parentAlias))
                {
                    throw new FormatException(
                              $"Parent alias {parentAlias} is an invalid parent for block {blockAlias}.");
                }

                int blockHeight;
                if (TryFindCommitted(parentAlias, committedBlocks, out var parentBlock))
                {
                    blockHeight = parentBlock.BlockHeight + 1;
                }
                else
                {
                    if (uncommittedBlocks.Count == 0 && committedBlocks.Count == 0 &&
                        parentAlias == BlockAlias.GenesisParent) // we're adding the first block
                    {
                        blockHeight = 1;
                    }
                    else
                    {
                        if (!TryFindUncommitted(parentAlias, uncommittedBlocks, out var uParentBlock))
                        {
                            throw new ArgumentNullException($"No parent {parentAlias} found.");
                        }
                        blockHeight = uParentBlock.BlockHeight + 1;
                    }
                }

                var newBlock = new UncommittedBlock(blockHeight, blockId, blockAlias, parentAlias);

                if (blockHeight > maxBlockHeight)
                {
                    maxBlockHeight = blockHeight;
                }

                uncommittedBlocks.Add(newBlock);
            }

            return(new SimpleBlockchain(committedBlocks, uncommittedBlocks, maxBlockHeight));
        }