Exemple #1
0
        public void HasNeededServicesTest(NodeServiceFlags flags, BlockchainState state, bool expected)
        {
            var cs = new ClientSettings();

            Helper.SetReadonlyProperty(cs, nameof(cs.Blockchain), new MockBlockchain()
            {
                _stateToReturn = state
            });
            bool actual = cs.HasNeededServices(flags);

            Assert.Equal(expected, actual);
        }
Exemple #2
0
        public bool ValidateGenesisBlock(BlockHashed newBlock, out BlockchainState blockchainState)
        {
            var expectedSignedGenesis = Genesis.GetBlockData(_cryptography, newBlock.Signed.Data.TimeStamp);
            var result = ValidateParent(0, Genesis.Hash, newBlock.Signed.Data,
                                        out blockchainState);

            if (result)
            {
                ValidateSignature(expectedSignedGenesis.Stamp, newBlock.Signed);
                ValidateBlockHash(expectedSignedGenesis, newBlock.HashTarget);
            }

            return(result);
        }
Exemple #3
0
        public bool ValidateNewBlock(BlockHashed lastBlock, BlockHashed newBlock, out BlockchainState blockchainState)
        {
            var result = ValidateParent(lastBlock.Signed.Data.Index + 1, lastBlock.HashTarget.Hash, newBlock.Signed.Data,
                                        out blockchainState);

            if (result)
            {
                ValidateHashTarget(lastBlock, newBlock);
                ValidateSignature(newBlock.Signed.Stamp, newBlock.Signed);
                ValidateBlockHash(newBlock);
                ValidateTransactions(newBlock.Signed.Data.Transactions);
            }

            return(result);
        }
Exemple #4
0
        private bool ValidateBlock(BlockHashed newBlock, BlockHashed lastBlock, out BlockchainState blockchainState)
        {
            bool result;

            if (lastBlock == null)
            {
                result = ValidateGenesisBlock(newBlock, out blockchainState);
            }
            else
            {
                result = ValidateNewBlock(lastBlock, newBlock, out blockchainState);
                if (result)
                {
                    RemovePendingTransactions(newBlock.Signed.Data.Transactions);
                }
            }

            return(result);
        }
Exemple #5
0
        private static bool ValidateParent(int expectedHeight, Hash expectedHash, BlockData newBlock, out BlockchainState blockchainState)
        {
            if (newBlock.Index > expectedHeight)
            {
                blockchainState = BlockchainState.NeedSync;
                return(false);
            }

            if (newBlock.Index != expectedHeight)
            {
                throw new BlockchainValidationException($"New block Height {newBlock.Index} do not match the expected Height {expectedHeight}");
            }
            if (newBlock.ParentHash != expectedHash)
            {
                throw new BlockchainValidationException($"New block parent hash {newBlock.ParentHash} do not match the expected {expectedHash}");
            }
            blockchainState = BlockchainState.Healty;
            return(true);
        }