Ejemplo n.º 1
0
        public void CreateValidBlock_CallsValidator_HappyFlow()
        {
            var expectedHash      = "hash";
            var privateKey        = "privkey";
            var expectedTimestamp = 1;
            var transactions      = new List <AbstractTransaction>()
            {
                new StateTransaction(null, "to", null, 0, 5000, 1, TransactionAction.ClaimCoinbase.ToString(), null, 0)
            };
            var blockchain = new Blockchain(new List <Block>()
            {
                new Block(new BlockHeader(_netId, 1, "merkleroot", 1, null).Finalize("firsthash", "sig"), transactions)
            }, _netId);
            var sut = new PowBlockCreator(_timestamperMock.Object, _blockValidatorMock.Object, _blockFinalizer.Object, _transactionValidator.Object);
            var transactionsList = _transactions.ToList();

            _timestamperMock.Setup(m => m.GetCurrentUtcTimestamp())
            .Returns(expectedTimestamp);
            _blockFinalizer.Setup(m => m.CalculateHash(It.IsAny <Block>())).Returns(expectedHash);
            _blockFinalizer.Setup(m => m.FinalizeBlock(It.IsAny <Block>(), expectedHash, privateKey));
            _blockValidatorMock.Setup(m => m.ValidateBlock(It.IsAny <Block>(), It.IsAny <BigDecimal>(), blockchain, true, true));
            _transactionValidator.Setup(m => m.CalculateMerkleRoot(transactionsList)).Returns("abc");
            _blockFinalizer.Setup(m => m.FinalizeBlock(It.IsAny <Block>(), expectedHash, privateKey));

            var result = sut.CreateValidBlockAndAddToChain(privateKey, blockchain, _protocol, transactionsList, 1, _maximumTarget, CancellationToken.None);

            Assert.AreEqual(blockchain.NetIdentifier, result.Header.MagicNumber);
            Assert.AreEqual("firsthash", result.Header.PreviousHash);
            Assert.AreEqual(_protocol, result.Header.Version);
            Assert.AreEqual(expectedTimestamp, result.Header.Timestamp);
            Assert.AreEqual("abc", result.Header.MerkleRoot);
            Assert.AreEqual(transactionsList, result.Transactions);
            Assert.AreEqual(1UL, result.Header.Nonce);
        }
Ejemplo n.º 2
0
        public void CreateValidBlock_CallsValidator_GenesisHappyFlow()
        {
            var expectedHash      = "hash";
            var privateKey        = "privkey";
            var expectedTimestamp = 1;
            var blockchain        = new Blockchain(_netId);
            var sut = new PowBlockCreator(_timestamperMock.Object, _blockValidatorMock.Object, _blockFinalizer.Object, _transactionValidator.Object);
            var transactionsList = _transactions.ToList();

            _timestamperMock.Setup(m => m.GetCurrentUtcTimestamp())
            .Returns(expectedTimestamp);
            _blockFinalizer.Setup(m => m.CalculateHash(It.IsAny <Block>())).Returns(expectedHash);
            _blockFinalizer.Setup(m => m.FinalizeBlock(It.IsAny <Block>(), expectedHash, privateKey));
            _blockValidatorMock.Setup(m => m.ValidateBlock(It.IsAny <Block>(), It.IsAny <BigDecimal>(), blockchain, true, true));
            _transactionValidator.Setup(m => m.CalculateMerkleRoot(transactionsList)).Returns("abc");
            _blockFinalizer.Setup(m => m.FinalizeBlock(It.IsAny <Block>(), expectedHash, privateKey));

            var result = sut.CreateValidBlockAndAddToChain(privateKey, blockchain, _protocol, transactionsList, 1, _maximumTarget, CancellationToken.None);

            Assert.AreEqual(blockchain.NetIdentifier, result.Header.MagicNumber);
            Assert.AreEqual(null, result.Header.PreviousHash);
            Assert.AreEqual(_protocol, result.Header.Version);
            Assert.AreEqual(expectedTimestamp, result.Header.Timestamp);
            Assert.AreEqual("abc", result.Header.MerkleRoot);
            Assert.AreEqual(transactionsList, result.Transactions);
            Assert.AreEqual(1UL, result.Header.Nonce);
        }
Ejemplo n.º 3
0
        private void CreateValidBlockThatShouldThrowExceptionOnInvalidDifficulty(BigDecimal difficulty)
        {
            var expectedExceptionMessage = "Difficulty cannot be zero.";
            var sut = new PowBlockCreator(_timestamperMock.Object, _blockValidatorMock.Object, _blockFinalizer.Object, _transactionValidator.Object);

            var ex = Assert.ThrowsException <DifficultyCalculationException>(
                () => sut.CreateValidBlockAndAddToChain("privkey", new Blockchain(_netId), _protocol, _transactions, difficulty, _maximumTarget, CancellationToken.None)
                );

            Assert.AreEqual(expectedExceptionMessage, ex.Message);
        }
Ejemplo n.º 4
0
        public void CreateValidBlock_ThrowsException_MiningCanceled()
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            var sut = new PowBlockCreator(_timestamperMock.Object, _blockValidatorMock.Object, _blockFinalizer.Object, _transactionValidator.Object);
            var veryHardDifficulty = BigInteger.Parse("00000000000000000000000000000000000000000000000000000000000000F", NumberStyles.HexNumber);
            var transactionsList   = _transactions.ToList();

            _timestamperMock.Setup(m => m.GetCurrentUtcTimestamp())
            .Returns(1);
            _transactionValidator.Setup(m => m.CalculateMerkleRoot(transactionsList)).Returns("abc");;

            var ex = Assert.ThrowsException <OperationCanceledException>(
                () =>
            {
                cts.Cancel();
                sut.CreateValidBlockAndAddToChain("privkey", new Blockchain(_netId), _protocol, transactionsList, veryHardDifficulty, _maximumTarget, cts.Token);
            }
                );
        }
Ejemplo n.º 5
0
        public void CreateValidBlockOverload_Uses_ConstantValues()
        {
            BigDecimal difficulty = 1;
            string     expectedNetworkIdentifier = "testnet";
            uint       expectedProtocolVersion   = 1;
            BigDecimal expectedMaximumTarget     = BigInteger.Parse("0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NumberStyles.HexNumber);
            var        blockchain      = new Blockchain(expectedNetworkIdentifier);
            var        expectedBlock   = new Block(new BlockHeader(expectedNetworkIdentifier, expectedProtocolVersion, "abc", 123, null), _transactions);
            var        selfCallingMock = new Mock <PowBlockCreator>(MockBehavior.Strict, new object[] { _timestamperMock.Object, _blockValidatorMock.Object, _blockFinalizer.Object, _transactionValidator.Object });

            selfCallingMock.Setup(m => m.CreateValidBlockAndAddToChain("privkey", blockchain, _transactions, difficulty)).CallBase();
            selfCallingMock.Setup(m => m.CreateValidBlockAndAddToChain("privkey", blockchain, expectedProtocolVersion, _transactions, difficulty, expectedMaximumTarget, CancellationToken.None))
            .Returns(expectedBlock);
            PowBlockCreator sut = selfCallingMock.Object;

            var result = sut.CreateValidBlockAndAddToChain("privkey", blockchain, _transactions, difficulty);

            Assert.AreEqual(expectedBlock, result);
            selfCallingMock.VerifyAll();
        }