| 45 | } |
| 46 | |
| 47 | Value luavm_executescript(const Array& params, bool fHelp) { |
| 48 | if (fHelp || params.size() < 2 || params.size() > 5) { |
| 49 | throw runtime_error( |
| 50 | "luavm_executescript \"addr\" \"script_path\" [\"arguments\"] [amount] [symbol:fee:unit]\n" |
| 51 | "\nexecutes the script in vm simulator, and then returns the execution status.\n" |
| 52 | "\nthe execution include submitluacontractdeploytx and submitluacontractcalltx.\n" |
| 53 | "\nArguments:\n" |
| 54 | "1.\"addr\": (string required) contract owner address from this wallet\n" |
| 55 | "2.\"script_path\": (string required), the file path of the app script\n" |
| 56 | "3.\"arguments\": (string, optional) contract method invoke content (Hex encode required)\n" |
| 57 | "4.\"amount\": (numeric, optional) amount of WICC to send to app account\n" |
| 58 | "5.\"symbol:fee:unit\": (string:numeric:string, optional) fee paid for miner, default is WICC:110010000:sawi\n" |
| 59 | "\nResult vm execute detail\n" |
| 60 | "\nResult:\n" |
| 61 | "\nExamples:\n" |
| 62 | + HelpExampleCli("luavm_executescript","\"WiZx6rrsBn9sHjwpvdwtMNNX2o31s3DEHH\" \"/tmp/lua/script.lua\"") |
| 63 | + "\nAs json rpc call\n" |
| 64 | + HelpExampleRpc("luavm_executescript", "\"WiZx6rrsBn9sHjwpvdwtMNNX2o31s3DEHH\", \"/tmp/lua/script.lua\"")); |
| 65 | } |
| 66 | |
| 67 | string luaScriptFilePath = GetAbsolutePath(params[1].get_str()).string(); |
| 68 | if (luaScriptFilePath.empty()) |
| 69 | throw JSONRPCError(RPC_SCRIPT_FILEPATH_NOT_EXIST, "Lua Script file not exist!"); |
| 70 | |
| 71 | if (luaScriptFilePath.compare(0, LUA_CONTRACT_LOCATION_PREFIX.size(), LUA_CONTRACT_LOCATION_PREFIX.c_str()) != 0) |
| 72 | throw JSONRPCError(RPC_SCRIPT_FILEPATH_INVALID, "Lua Script file not inside /tmp/lua dir or its subdir!"); |
| 73 | HeightType height = chainActive.Height(); |
| 74 | std::tuple<bool, string> syntax = CLuaVM::CheckScriptSyntax(luaScriptFilePath.c_str(), height); |
| 75 | bool bOK = std::get<0>(syntax); |
| 76 | if (!bOK) |
| 77 | throw JSONRPCError(RPC_INVALID_PARAMETER, std::get<1>(syntax)); |
| 78 | |
| 79 | FILE* file = fopen(luaScriptFilePath.c_str(), "rb+"); |
| 80 | if (!file) |
| 81 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Open script file (" + luaScriptFilePath + ") error"); |
| 82 | |
| 83 | long lSize; |
| 84 | fseek(file, 0, SEEK_END); |
| 85 | lSize = ftell(file); |
| 86 | rewind(file); |
| 87 | |
| 88 | if (lSize <= 0 || lSize > MAX_CONTRACT_CODE_SIZE) { // contract script file size must be <= 64 KB) |
| 89 | fclose(file); |
| 90 | throw JSONRPCError( |
| 91 | RPC_INVALID_PARAMETER, |
| 92 | (lSize == -1) ? "File size is unknown" |
| 93 | : ((lSize == 0) ? "File is empty" : "File size exceeds 64 KB limit")); |
| 94 | } |
| 95 | |
| 96 | // allocate memory to contain the whole file: |
| 97 | char *buffer = (char*) malloc(sizeof(char) * lSize); |
| 98 | if (buffer == NULL) { |
| 99 | fclose(file); |
| 100 | throw runtime_error("allocate memory failed"); |
| 101 | } |
| 102 | if (fread(buffer, 1, lSize, file) != (size_t) lSize) { |
| 103 | free(buffer); |
| 104 | fclose(file); |
nothing calls this directly
no test coverage detected