public void InternalCall_Balance_Error() { var internalCallMessage = new InternalCallMessage( uint160.One, uint160.Zero, 10, (Gas)(GasPriceList.BaseCost + 100000), new MethodCall("Test", new object[] { }) ); var state = new Mock <IState>(); // Setup the balance with less than the required amount. state.Setup(s => s.GetBalance(internalCallMessage.From)) .Returns(internalCallMessage.Amount - 1); var stateProcessor = new StateProcessor(this.vm.Object, this.addressGenerator.Object); StateTransitionResult result = stateProcessor.Apply(state.Object, internalCallMessage); state.Verify(s => s.GetBalance(internalCallMessage.From)); Assert.True(result.IsFailure); Assert.NotNull(result.Error); Assert.Null(result.Error.VmError); Assert.Equal(StateTransitionErrorKind.InsufficientBalance, result.Error.Kind); Assert.Equal((Gas)0, result.GasConsumed); }
public void InternalCall_Code_Null() { var internalCallMessage = new InternalCallMessage( uint160.One, uint160.Zero, 10, (Gas)(GasPriceList.BaseCost + 100000), new MethodCall("Test", new object[] { }) ); this.contractStateRoot .Setup(sr => sr.GetCode(internalCallMessage.To)) .Returns((byte[])null); var state = new Mock <IState>(); state.Setup(s => s.GetBalance(internalCallMessage.From)) .Returns(internalCallMessage.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, internalCallMessage); this.contractStateRoot.Verify(sr => sr.GetCode(internalCallMessage.To), Times.Once); Assert.True(result.IsFailure); Assert.NotNull(result.Error); Assert.Null(result.Error.VmError); Assert.Equal(StateTransitionErrorKind.NoCode, result.Error.Kind); Assert.Equal((Gas)GasPriceList.BaseCost, result.GasConsumed); }
public void InternalCall_Vm_Error() { var vmExecutionResult = VmExecutionResult.Fail(VmExecutionErrorKind.InvocationFailed, "Error"); // Code must have a length to pass precondition checks. var code = new byte[1]; var typeName = "Test"; var internalCallMessage = new InternalCallMessage( uint160.One, uint160.Zero, 10, (RuntimeObserver.Gas)(GasPriceList.BaseCost + 100000), new MethodCall("Test", new object[] { }) ); this.vm.Setup(v => v.ExecuteMethod( It.IsAny <ISmartContractState>(), It.IsAny <ExecutionContext>(), internalCallMessage.Method, code, typeName)) .Returns(vmExecutionResult); this.contractStateRoot .Setup(sr => sr.GetCode(internalCallMessage.To)) .Returns(code); this.contractStateRoot .Setup(sr => sr.GetContractType(internalCallMessage.To)) .Returns(typeName); var state = new Mock <IState>(); state.Setup(s => s.GetBalance(internalCallMessage.From)) .Returns(internalCallMessage.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, internalCallMessage); state.Verify(s => s.CreateSmartContractState(state.Object, It.IsAny <RuntimeObserver.IGasMeter>(), internalCallMessage.To, internalCallMessage, this.contractStateRoot.Object)); this.vm.Verify( v => v.ExecuteMethod( It.IsAny <ISmartContractState>(), It.IsAny <ExecutionContext>(), internalCallMessage.Method, code, typeName), Times.Once); Assert.True(result.IsFailure); Assert.NotNull(result.Error); Assert.Equal(result.Error.VmError, vmExecutionResult.Error.Message); Assert.Equal(StateTransitionErrorKind.VmError, result.Error.Kind); Assert.Equal(GasPriceList.BaseCost, result.GasConsumed); }
public void InternalCall_Success() { // The difference between an internal and an external call: // - Internal call performs a balance check before execution // - Internal call appends a new internal transfer if successful var vmExecutionResult = VmExecutionResult.Success(true, "Test"); var code = new byte[1]; var typeName = "Test"; var internalCallMessage = new InternalCallMessage( uint160.One, uint160.Zero, 10, (Gas)(GasPriceList.BaseCost + 100000), new MethodCall("Test", new object[] {}) ); this.contractStateRoot .Setup(sr => sr.GetCode(internalCallMessage.To)) .Returns(code); this.contractStateRoot .Setup(sr => sr.GetContractType(internalCallMessage.To)) .Returns(typeName); this.vm.Setup(v => v.ExecuteMethod(It.IsAny <ISmartContractState>(), internalCallMessage.Method, code, typeName)) .Returns(vmExecutionResult); var state = new Mock <IState>(); // Return the sent amount + 1 state.Setup(s => s.GetBalance(internalCallMessage.From)).Returns(internalCallMessage.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, internalCallMessage); // Verify we check the balance of the sender first state.Verify(s => s.GetBalance(internalCallMessage.From)); // Verify we get the code from the destination address' code cache this.contractStateRoot.Verify(s => s.GetCode(internalCallMessage.To), Times.Once); // Verify we set up the smart contract state state.Verify(s => s.CreateSmartContractState(state.Object, It.IsAny <GasMeter>(), internalCallMessage.To, internalCallMessage, this.contractStateRoot.Object)); // Verify the VM was invoked this.vm.Verify(v => v.ExecuteMethod(It.IsAny <ISmartContractState>(), internalCallMessage.Method, code, typeName), Times.Once); // Verify the value was added to the internal transfer list state.Verify(s => s.AddInternalTransfer(It.Is <TransferInfo>(t => t.From == internalCallMessage.From && t.To == internalCallMessage.To && t.Value == internalCallMessage.Amount))); Assert.True(result.IsSuccess); Assert.NotNull(result.Success); Assert.Equal(internalCallMessage.To, result.Success.ContractAddress); Assert.Equal(vmExecutionResult.Result, result.Success.ExecutionResult); Assert.Equal(GasPriceList.BaseCost, result.GasConsumed); }