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(); }
private bool IsOldBlock(BlockHeader header) { var limit = TimestampHelper.GetUtcNow() - TimestampHelper.DurationFromMinutes(NetworkConstants.DefaultMaxBlockAgeToBroadcastInMinutes); if (header.Time < limit) { return(true); } return(false); }
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); }
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); } }
public void DurationFromMinutes_Test() { var result = TimestampHelper.DurationFromMinutes(1); result.Seconds.ShouldBe(60); }