コード例 #1
0
        public void Should_Not_Validate_ContractCall()
        {
            var callTx = new ContractTxData(1, 1000, (Gas)10000, uint160.Zero, "Test");

            var sut = new AllowedCodeHashLogic(this.hashChecker.Object, this.hashingStrategy.Object);

            sut.CheckContractTransaction(callTx, 0);

            this.hashingStrategy.Verify(h => h.Hash(It.IsAny <byte[]>()), Times.Never);
            this.hashChecker.Verify(h => h.CheckHashWhitelisted(It.IsAny <byte[]>()), Times.Never);
        }
コード例 #2
0
        public void Should_Allow_Code_With_Valid_Hash()
        {
            var code = RandomUtils.GetBytes(2048);

            byte[] hash = HashHelper.Keccak256(code);

            this.hashingStrategy.Setup(h => h.Hash(code)).Returns(hash);
            this.hashChecker.Setup(h => h.CheckHashWhitelisted(hash)).Returns(true);

            var tx = new ContractTxData(1, 1000, (Gas)10000, code);

            var sut = new AllowedCodeHashLogic(this.hashChecker.Object, this.hashingStrategy.Object);

            sut.CheckContractTransaction(tx, 0);

            this.hashChecker.Verify(h => h.CheckHashWhitelisted(hash), Times.Once);
        }
コード例 #3
0
        public void Should_Throw_ConsensusErrorException_If_Hash_Not_Allowed()
        {
            var code = RandomUtils.GetBytes(2048);

            byte[] hash = HashHelper.Keccak256(code);

            this.hashingStrategy.Setup(h => h.Hash(code)).Returns(hash);
            this.hashChecker.Setup(h => h.CheckHashWhitelisted(hash)).Returns(false);

            var sut = new AllowedCodeHashLogic(this.hashChecker.Object, this.hashingStrategy.Object);

            var tx = new ContractTxData(1, 1000, (Gas)10000, code);

            Assert.Throws <ConsensusErrorException>(() => sut.CheckContractTransaction(tx, 0));

            this.hashChecker.Verify(h => h.CheckHashWhitelisted(hash), Times.Once);
        }
        /// <inheritdoc/>
        public override void CheckTransaction(MempoolValidationContext context)
        {
            TxOut scTxOut = context.Transaction.TryGetSmartContractTxOut();

            if (scTxOut == null)
            {
                // No SC output to validate.
                return;
            }

            ContractTxData txData = ContractTransactionChecker.GetContractTxData(this.callDataSerializer, scTxOut);

            if (!txData.IsCreateContract)
            {
                return;
            }

            byte[] hashedCode = this.hashingStrategy.Hash(txData.ContractExecutionCode);

            if (!this.whitelistedHashChecker.CheckHashWhitelisted(hashedCode))
            {
                AllowedCodeHashLogic.ThrowInvalidCode();
            }
        }