Skip to content

Commit

Permalink
Charge gas amount for skipped REST function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
platfowner committed Jul 11, 2023
1 parent edcc6c6 commit 52416cf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 42 deletions.
2 changes: 1 addition & 1 deletion common/common-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ class CommonUtil {
// TODO(platfowner): Consider some code (e.g. IN_LOCKUP_PERIOD, INSUFFICIENT_BALANCE) no failure
// so that their transactions are not reverted.
static isFailedFuncResultCode(code) {
return code !== FunctionResultCode.SUCCESS;
return code !== FunctionResultCode.SUCCESS && code !== FunctionResultCode.SKIP;
}

static isAppPath(parsedPath) {
Expand Down
1 change: 1 addition & 0 deletions common/result-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const FailedTxPrecheckCodeSet = new Set([
// If they are altered and deployed, the full sync of the blockchain nodes can fail.
const FunctionResultCode = {
SUCCESS: 0,
SKIP: 20000, // Normal skip
FAILURE: 20001, // Normal failure
INTERNAL_ERROR: 20002, // Something went wrong but don't know why
// Transfer
Expand Down
86 changes: 46 additions & 40 deletions db/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ class Functions {
}
} else if (functionEntry.function_type === FunctionTypes.REST) {
// NOTE: Skipped when the event source is null.
if (NodeConfigs.ENABLE_REST_FUNCTION_CALL &&
eventSource !== null &&
functionEntry.function_url) {
if (functionEntry.function_url) {
const restFunctionUrlWhitelist = this.db.getRestFunctionsUrlWhitelist();
if (!CommonUtil.isWhitelistedUrl(functionEntry.function_url, restFunctionUrlWhitelist)) {
// NOTE: Skipped when the function url is not in the whitelist.
Expand All @@ -255,43 +253,51 @@ class Functions {
`function_url '${functionEntry.function_url}' with:\n` +
formattedParams);
}
const newAuth = Object.assign(
{}, auth, { fid: functionEntry.function_id, fids: this.getFids() });
promises.push(axios.post(functionEntry.function_url, {
fid: functionEntry.function_id,
function: functionEntry,
valuePath,
functionPath,
value,
prevValue,
params,
timestamp,
executedAt,
transaction,
blockNumber,
blockTime,
options,
eventSource,
auth: newAuth,
chainId: blockchainParams.chainId,
networkId: blockchainParams.networkId,
}, {
timeout: NodeConfigs.REST_FUNCTION_CALL_TIMEOUT_MS
}).catch((error) => {
if (DevFlags.enableRichFunctionLogging) {
logger.error(
`Failed to trigger REST function [[ ${functionEntry.function_id} ]] of ` +
`function_url '${functionEntry.function_url}' with error: \n` +
`${JSON.stringify(error)}` +
formattedParams);
}
failCount++;
return true;
}));
funcResults[functionEntry.function_id] = {
code: FunctionResultCode.SUCCESS,
bandwidth_gas_amount: blockchainParams.restFunctionCallGasAmount,
};
if (NodeConfigs.ENABLE_REST_FUNCTION_CALL && eventSource !== null) {
const newAuth = Object.assign(
{}, auth, { fid: functionEntry.function_id, fids: this.getFids() });
promises.push(axios.post(functionEntry.function_url, {
fid: functionEntry.function_id,
function: functionEntry,
valuePath,
functionPath,
value,
prevValue,
params,
timestamp,
executedAt,
transaction,
blockNumber,
blockTime,
options,
eventSource,
auth: newAuth,
chainId: blockchainParams.chainId,
networkId: blockchainParams.networkId,
}, {
timeout: NodeConfigs.REST_FUNCTION_CALL_TIMEOUT_MS
}).catch((error) => {
if (DevFlags.enableRichFunctionLogging) {
logger.error(
`Failed to trigger REST function [[ ${functionEntry.function_id} ]] of ` +
`function_url '${functionEntry.function_url}' with error: \n` +
`${JSON.stringify(error)}` +
formattedParams);
}
failCount++;
return true;
}));
funcResults[functionEntry.function_id] = {
code: FunctionResultCode.SUCCESS,
bandwidth_gas_amount: blockchainParams.restFunctionCallGasAmount,
};
} else {
// Rest function trigger is skipped.
funcResults[functionEntry.function_id] = {
code: FunctionResultCode.SKIP,
bandwidth_gas_amount: blockchainParams.restFunctionCallGasAmount,
};
}
triggerCount++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2723,7 +2723,7 @@ describe("Functions", () => {
return triggerRes.func_promises.then((resp) => {
assert.deepEqual(resp, {
func_count: 1,
trigger_count: 0,
trigger_count: 1,
fail_count: 0,
});
});
Expand Down

0 comments on commit 52416cf

Please sign in to comment.