private static ExecutionState Runtime_MintToken(RuntimeVM vm) { vm.ExpectStackSize(4); var source = vm.PopAddress(); var destination = vm.PopAddress(); var symbol = vm.PopString("symbol"); var rom = vm.PopBytes("rom"); var ram = vm.PopBytes("ram"); BigInteger seriesID; if (vm.ProtocolVersion >= 4) { seriesID = vm.PopNumber("series"); } else { seriesID = 0; } var tokenID = vm.MintToken(symbol, source, destination, rom, ram, seriesID); var result = new VMObject(); result.SetValue(tokenID); vm.Stack.Push(result); return(ExecutionState.Running); }
private static ExecutionState Nexus_CreateToken(RuntimeVM vm) { vm.ExpectStackSize(7); var owner = vm.PopAddress(); var symbol = vm.PopString("symbol"); var name = vm.PopString("name"); var maxSupply = vm.PopNumber("maxSupply"); var decimals = (int)vm.PopNumber("decimals"); var flags = vm.PopEnum <TokenFlags>("flags"); var script = vm.PopBytes("script"); ContractInterface abi; if (vm.ProtocolVersion >= 4) { var abiBytes = vm.PopBytes("abi bytes"); abi = ContractInterface.FromBytes(abiBytes); } else { abi = new ContractInterface(); } vm.CreateToken(owner, symbol, name, maxSupply, decimals, flags, script, abi); return(ExecutionState.Running); }
private static ExecutionState Runtime_AESEncrypt(RuntimeVM vm) { vm.ExpectStackSize(2); var data = vm.PopBytes("data"); var key = vm.PopBytes("key"); var encryptedData = CryptoExtensions.AESGCMEncrypt(data, key); var result = new VMObject(); result.SetValue(encryptedData); vm.Stack.Push(result); return(ExecutionState.Running); }
private static ExecutionState Nexus_CreateTokenSeries(RuntimeVM vm) { vm.ExpectStackSize(5); var from = vm.PopAddress(); var symbol = vm.PopString("symbol"); var seriesID = vm.PopNumber("series ID"); var maxSupply = vm.PopNumber("max supply"); var mode = vm.PopEnum <TokenSeriesMode>("mode"); var script = vm.PopBytes("script"); var abiBytes = vm.PopBytes("abi bytes"); var abi = ContractInterface.FromBytes(abiBytes); vm.CreateTokenSeries(symbol, from, seriesID, maxSupply, mode, script, abi); return(ExecutionState.Running); }
private static ExecutionState Nexus_CreateOrganization(RuntimeVM vm) { vm.ExpectStackSize(4); var source = vm.PopAddress(); var ID = vm.PopString("id"); var name = vm.PopString("name"); var script = vm.PopBytes("script"); vm.CreateOrganization(source, ID, name, script); return(ExecutionState.Running); }
private static ExecutionState Nexus_SetTokenPlatformHash(RuntimeVM vm) { vm.ExpectStackSize(3); var symbol = vm.PopString("symbol"); var platform = vm.PopString("platform"); var bytes = vm.PopBytes("hash"); var hash = new Hash(bytes.Skip(1).ToArray()); vm.SetTokenPlatformHash(symbol, platform, hash); return(ExecutionState.Running); }
private static ExecutionState Task_Start(RuntimeVM vm) { vm.ExpectStackSize(1); var contractName = vm.PopString("contract"); var methodBytes = vm.PopBytes("method bytes"); var from = vm.PopAddress(); var frequency = (int)vm.PopNumber("frequency"); var mode = vm.PopEnum <TaskFrequencyMode>("mode"); var gasLimit = vm.PopNumber("gas limit"); var method = ContractMethod.FromBytes(methodBytes); var task = vm.StartTask(from, contractName, method, frequency, mode, gasLimit); var result = new VMObject(); result.SetValue(task.ID); vm.Stack.Push(result); return(ExecutionState.Running); }
private static ExecutionState Runtime_UpgradeContract(RuntimeVM vm) { var tx = vm.Transaction; Throw.IfNull(tx, nameof(tx)); var pow = tx.Hash.GetDifficulty(); vm.Expect(pow >= (int)ProofOfWork.Minimal, "expected proof of work"); vm.ExpectStackSize(1); var from = vm.PopAddress(); vm.Expect(from.IsUser, "address must be user"); vm.Expect(vm.IsStakeMaster(from), "needs to be master"); vm.Expect(vm.IsWitness(from), "invalid witness"); var contractName = vm.PopString("contractName"); var contractAddress = SmartContract.GetAddressForName(contractName); var deployed = vm.Chain.IsContractDeployed(vm.Storage, contractAddress); vm.Expect(deployed, $"{contractName} does not exist"); byte[] script; ContractInterface abi; bool isNative = Nexus.IsNativeContract(contractName); vm.Expect(!isNative, "cannot upgrade native contract"); bool isToken = ValidationUtils.IsValidTicker(contractName); script = vm.PopBytes("contractScript"); var abiBytes = vm.PopBytes("contractABI"); abi = ContractInterface.FromBytes(abiBytes); var fuelCost = vm.GetGovernanceValue(Nexus.FuelPerContractDeployTag); // governance value is in usd fiat, here convert from fiat to fuel amount fuelCost = vm.GetTokenQuote(DomainSettings.FiatTokenSymbol, DomainSettings.FuelTokenSymbol, fuelCost); // burn the "cost" tokens vm.BurnTokens(DomainSettings.FuelTokenSymbol, from, fuelCost); // ABI validation ValidateABI(vm, contractName, abi, isNative); SmartContract oldContract; if (isToken) { oldContract = vm.Nexus.GetTokenContract(vm.Storage, contractName); } else { oldContract = vm.Chain.GetContractByName(vm.Storage, contractName); } vm.Expect(oldContract != null, "could not fetch previous contract"); vm.Expect(abi.Implements(oldContract.ABI), "new abi does not implement all methods of previous abi"); vm.Expect(vm.InvokeTrigger(false, script, contractName, abi, AccountTrigger.OnUpgrade.ToString(), from) == TriggerResult.Success, "OnUpgrade trigger failed"); if (isToken) { vm.Nexus.UpgradeTokenContract(vm.RootStorage, contractName, script, abi); } else { vm.Chain.UpgradeContract(vm.Storage, contractName, script, abi); } vm.Notify(EventKind.ContractUpgrade, from, contractName); return(ExecutionState.Running); }
private static ExecutionState Runtime_DeployContract(RuntimeVM vm) { var tx = vm.Transaction; Throw.IfNull(tx, nameof(tx)); var pow = tx.Hash.GetDifficulty(); vm.Expect(pow >= (int)ProofOfWork.Minimal, "expected proof of work"); vm.ExpectStackSize(1); var from = vm.PopAddress(); vm.Expect(from.IsUser, "address must be user"); if (vm.Nexus.HasGenesis) { //Runtime.Expect(org != DomainSettings.ValidatorsOrganizationName, "cannot deploy contract via this organization"); vm.Expect(vm.IsStakeMaster(from), "needs to be master"); } vm.Expect(vm.IsWitness(from), "invalid witness"); var contractName = vm.PopString("contractName"); var contractAddress = SmartContract.GetAddressForName(contractName); var deployed = vm.Chain.IsContractDeployed(vm.Storage, contractAddress); // TODO if (vm.ProtocolVersion >= 2) { vm.Expect(!deployed, $"{contractName} is already deployed"); } else if (deployed) { return(ExecutionState.Running); } byte[] script; ContractInterface abi; bool isNative = Nexus.IsNativeContract(contractName); if (isNative) { if (contractName == "validator" && vm.GenesisAddress == Address.Null) { vm.Nexus.Initialize(from); } script = new byte[] { (byte)Opcode.RET }; var contractInstance = vm.Nexus.GetNativeContractByAddress(contractAddress); abi = contractInstance.ABI; } else { if (ValidationUtils.IsValidTicker(contractName)) { throw new VMException(vm, "use createToken instead for this kind of contract"); } else { vm.Expect(ValidationUtils.IsValidIdentifier(contractName), "invalid contract name"); } var isReserved = ValidationUtils.IsReservedIdentifier(contractName); if (isReserved && vm.IsWitness(vm.GenesisAddress)) { isReserved = false; } vm.Expect(!isReserved, $"name '{contractName}' reserved by system"); script = vm.PopBytes("contractScript"); var abiBytes = vm.PopBytes("contractABI"); abi = ContractInterface.FromBytes(abiBytes); var fuelCost = vm.GetGovernanceValue(Nexus.FuelPerContractDeployTag); // governance value is in usd fiat, here convert from fiat to fuel amount fuelCost = vm.GetTokenQuote(DomainSettings.FiatTokenSymbol, DomainSettings.FuelTokenSymbol, fuelCost); // burn the "cost" tokens vm.BurnTokens(DomainSettings.FuelTokenSymbol, from, fuelCost); } // ABI validation ValidateABI(vm, contractName, abi, isNative); var success = vm.Chain.DeployContractScript(vm.Storage, from, contractName, contractAddress, script, abi); vm.Expect(success, $"deployment of {contractName} failed"); var constructor = abi.FindMethod(SmartContract.ConstructorName); if (constructor != null) { vm.CallContext(contractName, constructor, from); } vm.Notify(EventKind.ContractDeploy, from, contractName); return(ExecutionState.Running); }