示例#1
0
        public async Task Test_Validate_Before_Attach()
        {
            Block block = null;
            bool  validateResult;

            validateResult = await _blockValidationProvider.ValidateBeforeAttachAsync(block);

            validateResult.ShouldBeFalse();

            block          = new Block();
            validateResult = await _blockValidationProvider.ValidateBeforeAttachAsync(block);

            validateResult.ShouldBeFalse();

            block.Header   = new BlockHeader();
            validateResult = await _blockValidationProvider.ValidateBeforeAttachAsync(block);

            validateResult.ShouldBeFalse();

            block.Body     = new BlockBody();
            validateResult = await _blockValidationProvider.ValidateBeforeAttachAsync(block);

            validateResult.ShouldBeFalse();

            block.Body.TransactionIds.Add(Hash.Empty);
            block.Header.ChainId = 1234;
            validateResult       = await _blockValidationProvider.ValidateBeforeAttachAsync(block);

            validateResult.ShouldBeFalse();

            block.Header.ChainId = 0;
            validateResult       = await _blockValidationProvider.ValidateBeforeAttachAsync(block);

            validateResult.ShouldBeFalse();

            block.Header.Height = AElfConstants.GenesisBlockHeight;
            validateResult      = await _blockValidationProvider.ValidateBeforeAttachAsync(block);

            validateResult.ShouldBeFalse();

            block.Header           = _kernelTestHelper.GenerateBlock(9, HashHelper.ComputeFrom("PreviousBlockHash")).Header;
            block.Header.Signature =
                ByteString.CopyFrom(CryptoHelper.SignWithPrivateKey(_kernelTestHelper.KeyPair.PrivateKey,
                                                                    block.GetHash().ToByteArray()));
            validateResult = await _blockValidationProvider.ValidateBeforeAttachAsync(block);

            validateResult.ShouldBeFalse();

            block.Header.MerkleTreeRootOfTransactions = block.Body.CalculateMerkleTreeRoot();
            block.Header.Time = TimestampHelper.GetUtcNow() + TimestampHelper.DurationFromMinutes(30);
            validateResult    = await _blockValidationProvider.ValidateBeforeAttachAsync(block);

            validateResult.ShouldBeFalse();

            block.Header.Time = TimestampHelper.GetUtcNow();
            validateResult    = await _blockValidationProvider.ValidateBeforeAttachAsync(block);

            validateResult.ShouldBeTrue();
        }
示例#2
0
        private bool IsOldBlock(BlockHeader header)
        {
            var limit = TimestampHelper.GetUtcNow()
                        - TimestampHelper.DurationFromMinutes(NetworkConstants.DefaultMaxBlockAgeToBroadcastInMinutes);

            if (header.Time < limit)
            {
                return(true);
            }

            return(false);
        }
示例#3
0
        public async Task BroadcastAnnounce_Test()
        {
            var blockHeader = _kernelTestHelper.GenerateBlock(10, Hash.FromString("test")).Header;

            //old block
            blockHeader.Time = TimestampHelper.GetUtcNow() - TimestampHelper.DurationFromMinutes(20);
            await _networkService.BroadcastAnnounceAsync(blockHeader, false);

            //known block
            blockHeader.Time = TimestampHelper.GetUtcNow();
            await _networkService.BroadcastAnnounceAsync(blockHeader, false);

            //broadcast again
            blockHeader = _kernelTestHelper.GenerateBlock(11, Hash.FromString("new")).Header;
            await _networkService.BroadcastAnnounceAsync(blockHeader, false);
        }
示例#4
0
        public async Task BroadcastAnnouncement_OldBlock_Test()
        {
            var blockHeader = OsCoreTestHelper.CreateFakeBlockHeader(chainId: 1, height: 2);

            blockHeader.Time = TimestampHelper.GetUtcNow() -
                               TimestampHelper.DurationFromMinutes(
                NetworkConstants.DefaultMaxBlockAgeToBroadcastInMinutes + 1);

            await _networkService.BroadcastAnnounceAsync(blockHeader);

            foreach (var peer in _testContext.MockedPeers)
            {
                peer.Verify(p => p.TryAddKnownBlock(blockHeader.GetHash()), Times.Never);
            }

            foreach (var peer in _testContext.MockedPeers)
            {
                peer.Verify(p => p.EnqueueAnnouncement(It.Is <BlockAnnouncement>(ba => ba.BlockHash == blockHeader.GetHash()),
                                                       It.IsAny <Action <NetworkException> >()), Times.Never);
            }
        }
示例#5
0
        public void DurationFromMinutes_Test()
        {
            var result = TimestampHelper.DurationFromMinutes(1);

            result.Seconds.ShouldBe(60);
        }