public void InternalCreate_Vm_Error()
        {
            var newContractAddress = uint160.One;

            var vmExecutionResult = VmExecutionResult.Error(new ContractErrorMessage("Error"));

            // Code must have a length to pass precondition checks.
            var code     = new byte[1];
            var typeName = "Test";

            var internalCreateMessage = new InternalCreateMessage(
                uint160.Zero,
                10,
                (Gas)(GasPriceList.BaseCost + 100000),
                new object[] { },
                typeName
                );

            this.vm.Setup(v =>
                          v.Create(It.IsAny <IStateRepository>(),
                                   It.IsAny <ISmartContractState>(),
                                   It.IsAny <byte[]>(),
                                   It.IsAny <object[]>(),
                                   It.IsAny <string>()))
            .Returns(vmExecutionResult);

            // Need to return code for the sender
            this.contractStateRoot
            .Setup(sr => sr.GetCode(internalCreateMessage.From))
            .Returns(code);

            var state = new Mock <IState>();

            state.Setup(s => s.GetBalance(internalCreateMessage.From)).Returns(internalCreateMessage.Amount + 1);
            state.SetupGet(s => s.ContractState).Returns(this.contractStateRoot.Object);
            state.Setup(s => s.GenerateAddress(It.IsAny <IAddressGenerator>())).Returns(newContractAddress);

            var stateProcessor = new StateProcessor(this.vm.Object, this.addressGenerator.Object);

            StateTransitionResult result = stateProcessor.Apply(state.Object, internalCreateMessage);

            state.Verify(s => s.CreateSmartContractState(state.Object, It.IsAny <GasMeter>(), newContractAddress, internalCreateMessage, this.contractStateRoot.Object));

            this.vm.Verify(
                v => v.Create(
                    this.contractStateRoot.Object,
                    It.IsAny <ISmartContractState>(),
                    code,
                    internalCreateMessage.Parameters,
                    internalCreateMessage.Type),
                Times.Once);

            Assert.True(result.IsFailure);
            Assert.NotNull(result.Error);
            Assert.Equal(result.Error.VmError, vmExecutionResult.ErrorMessage);
            Assert.Equal(StateTransitionErrorKind.VmError, result.Error.Kind);
            Assert.Equal(GasPriceList.BaseCost, result.GasConsumed);
        }
        public void Contract_Transfer_To_Other_Contract_VM_Error()
        {
            var vmExecutionResult = VmExecutionResult.Error(new ContractErrorMessage("Error"));

            // Code must have a length to pass precondition checks.
            var code     = new byte[1];
            var typeName = "Test";

            var contractTransferMessage = new ContractTransferMessage(
                uint160.One,
                uint160.Zero,
                10,
                (Gas)(GasPriceList.BaseCost + 100000)
                );

            this.vm.Setup(v => v.ExecuteMethod(
                              It.IsAny <ISmartContractState>(),
                              contractTransferMessage.Method,
                              code,
                              typeName))
            .Returns(vmExecutionResult);

            this.contractStateRoot
            .Setup(sr => sr.GetCode(contractTransferMessage.To))
            .Returns(code);

            this.contractStateRoot
            .Setup(sr => sr.GetContractType(contractTransferMessage.To))
            .Returns(typeName);

            var state = new Mock <IState>();

            state.Setup(s => s.GetBalance(contractTransferMessage.From))
            .Returns(contractTransferMessage.Amount + 1);
            state.SetupGet(s => s.ContractState).Returns(this.contractStateRoot.Object);

            var stateProcessor = new StateProcessor(this.vm.Object, this.addressGenerator.Object);

            StateTransitionResult result = stateProcessor.Apply(state.Object, contractTransferMessage);

            state.Verify(s => s.CreateSmartContractState(state.Object, It.IsAny <GasMeter>(), contractTransferMessage.To, contractTransferMessage, this.contractStateRoot.Object));

            this.vm.Verify(
                v => v.ExecuteMethod(
                    It.IsAny <ISmartContractState>(),
                    contractTransferMessage.Method,
                    code,
                    typeName),
                Times.Once);

            Assert.True(result.IsFailure);
            Assert.NotNull(result.Error);
            Assert.Equal(result.Error.VmError, vmExecutionResult.ErrorMessage);
            Assert.Equal(StateTransitionErrorKind.VmError, result.Error.Kind);
            Assert.Equal(GasPriceList.BaseCost, result.GasConsumed);
        }
        public void ExternalCreate_Vm_Error()
        {
            var newContractAddress = uint160.One;
            var vmExecutionResult  = VmExecutionResult.Error(new ContractErrorMessage("Error"));

            var externalCreateMessage = new ExternalCreateMessage(
                uint160.Zero,
                10,
                (Gas)(GasPriceList.BaseCost + 100000),
                new byte[0],
                null
                );

            this.vm.Setup(v => v.Create(this.contractStateRoot.Object, It.IsAny <ISmartContractState>(), externalCreateMessage.Code, externalCreateMessage.Parameters, null))
            .Returns(vmExecutionResult);

            var state = new Mock <IState>();

            state.SetupGet(s => s.ContractState).Returns(this.contractStateRoot.Object);
            state.Setup(s => s.GenerateAddress(It.IsAny <IAddressGenerator>())).Returns(newContractAddress);

            var stateProcessor = new StateProcessor(this.vm.Object, this.addressGenerator.Object);

            StateTransitionResult result = stateProcessor.Apply(state.Object, externalCreateMessage);

            state.Verify(s => s.GenerateAddress(this.addressGenerator.Object), Times.Once);

            this.contractStateRoot.Verify(ts => ts.CreateAccount(newContractAddress), Times.Once);

            this.vm.Verify(v => v.Create(this.contractStateRoot.Object, It.IsAny <ISmartContractState>(), externalCreateMessage.Code, externalCreateMessage.Parameters, null), Times.Once);

            Assert.False(result.IsSuccess);
            Assert.True(result.IsFailure);
            Assert.NotNull(result.Error);
            Assert.Equal(vmExecutionResult.ErrorMessage, result.Error.VmError);
            Assert.Equal(StateTransitionErrorKind.VmError, result.Error.Kind);
            Assert.Equal(GasPriceList.BaseCost, result.GasConsumed);
        }