/// <summary> /// Applies an internally generated contract method call message to the current state. /// </summary> public StateTransitionResult Apply(IState state, InternalCallMessage message) { bool enoughBalance = this.EnsureSenderHasEnoughBalance(state, message.From, message.Amount); if (!enoughBalance) { return(StateTransitionResult.Fail((Gas)0, StateTransitionErrorKind.InsufficientBalance)); } byte[] contractCode = state.ContractState.GetCode(message.To); if (contractCode == null || contractCode.Length == 0) { return(StateTransitionResult.Fail((Gas)0, StateTransitionErrorKind.NoCode)); } // For internal calls we need to add the value contained in the contract invocation transaction // to the internal transfer list. This must occur before we apply the message to the state. // For external calls we do not need to do this. state.AddInternalTransfer(new TransferInfo { From = message.From, To = message.To, Value = message.Amount }); StateTransitionResult result = this.ApplyCall(state, message, contractCode); return(result); }
/// <summary> /// Applies an internally generated contract method call message to the current state. /// </summary> public StateTransitionResult Apply(IState state, InternalCallMessage message) { bool enoughBalance = this.EnsureSenderHasEnoughBalance(state, message.From, message.Amount); if (!enoughBalance) { return(StateTransitionResult.Fail((Gas)0, StateTransitionErrorKind.InsufficientBalance)); // Trivial - just return and let the MethodCall gas account for it. } var gasMeter = new GasMeter(message.GasLimit); gasMeter.Spend((Gas)GasPriceList.BaseCost); byte[] contractCode = state.ContractState.GetCode(message.To); if (contractCode == null || contractCode.Length == 0) { return(StateTransitionResult.Fail(gasMeter.GasConsumed, StateTransitionErrorKind.NoCode)); } // For internal calls we need to add the value contained in the contract invocation transaction // to the internal transfer list. This must occur before we apply the message to the state. // For external calls we do not need to do this. state.AddInternalTransfer(new TransferInfo(message.From, message.To, message.Amount)); StateTransitionResult result = this.ApplyCall(state, message, contractCode, gasMeter); return(result); }
/// <summary> /// Applies an internally generated contract method call message to the current state. /// </summary> public StateTransitionResult Apply(InternalCallMessage message) { bool enoughBalance = this.EnsureContractHasEnoughBalance(message.From, message.Amount); if (!enoughBalance) { return(StateTransitionResult.Fail((Gas)0, StateTransitionErrorKind.InsufficientBalance)); } byte[] contractCode = this.intermediateState.GetCode(message.To); if (contractCode == null || contractCode.Length == 0) { return(StateTransitionResult.Fail((Gas)0, StateTransitionErrorKind.NoCode)); } StateTransitionResult result = this.ApplyCall(message, contractCode); // For successful internal calls we need to add the transfer to the internal transfer list. // For external calls we do not need to do this. if (result.IsSuccess) { this.internalTransfers.Add(new TransferInfo { From = message.From, To = message.To, Value = message.Amount }); } return(result); }
///<inheritdoc /> public ITransferResult Call( ISmartContractState smartContractState, Address addressTo, ulong amountToTransfer, string methodName, object[] parameters, ulong gasLimit = 0) { // For a method call, send all the gas unless an amount was selected.Should only call trusted methods so re - entrance is less problematic. ulong gasBudget = (gasLimit != 0) ? gasLimit : smartContractState.GasMeter.GasAvailable; var message = new InternalCallMessage( addressTo.ToUint160(this.network), smartContractState.Message.ContractAddress.ToUint160(this.network), amountToTransfer, (Gas)gasBudget, new MethodCall(methodName, parameters) ); var result = this.state.Apply(message); return(result.IsSuccess ? TransferResult.Transferred(result.Success.ExecutionResult) : TransferResult.Failed()); }
///<inheritdoc /> public ITransferResult Call( ISmartContractState smartContractState, Address addressTo, ulong amountToTransfer, string methodName, object[] parameters, ulong gasLimit = 0) { Gas gasRemaining = smartContractState.GasMeter.GasAvailable; // For a method call, send all the gas unless an amount was selected.Should only call trusted methods so re - entrance is less problematic. ulong gasBudget = (gasLimit != 0) ? gasLimit : gasRemaining; if (gasRemaining < gasBudget || gasRemaining < GasPriceList.BaseCost) { return(TransferResult.Failed()); } var message = new InternalCallMessage( addressTo.ToUint160(this.network), smartContractState.Message.ContractAddress.ToUint160(this.network), amountToTransfer, (Gas)gasBudget, new MethodCall(methodName, parameters) ); // Create a snapshot of the current state IState newState = this.state.Snapshot(); // Apply the message to the snapshot StateTransitionResult result = this.stateProcessor.Apply(newState, message); // Transition the current state to the new state if (result.IsSuccess) { this.state.TransitionTo(newState); } smartContractState.GasMeter.Spend(result.GasConsumed); return(result.IsSuccess ? TransferResult.Transferred(result.Success.ExecutionResult) : TransferResult.Failed()); }