mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Introduced fallback contract execution (#388)
This commit is contained in:
@@ -6,7 +6,7 @@ const filename = "file.dat";
|
||||
const autofilePrefix = "autofile";
|
||||
const autofileSize = 1 * 1024 * 1024;
|
||||
|
||||
const diagnosticContract = async (ctx) => {
|
||||
const diagnosticContract = async (ctx, readOnly = false) => {
|
||||
|
||||
// Collection of per-user promises to wait for. Each promise completes when inputs for that user is processed.
|
||||
const userHandlers = [];
|
||||
@@ -134,5 +134,16 @@ const diagnosticContract = async (ctx) => {
|
||||
|
||||
}
|
||||
|
||||
const fallback = async (ctx) => {
|
||||
console.log(`Fallback mode: Non consensus execution count: ${ctx.nonConsensusRounds}`);
|
||||
}
|
||||
|
||||
|
||||
const hpc = new HotPocket.Contract();
|
||||
hpc.init(diagnosticContract);
|
||||
hpc.init({
|
||||
"consensus": async (ctx) => { await diagnosticContract(ctx); },
|
||||
"consensus_fallback": async (ctx) => { await fallback(ctx); },
|
||||
"read_req": async (ctx) => { await diagnosticContract(ctx, true); }
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -5,10 +5,10 @@ const exectsFile = "exects.txt";
|
||||
|
||||
// HP smart contract is defined as a function which takes HP ExecutionContext as an argument.
|
||||
// HP considers execution as complete, when this function completes and all the NPL message callbacks are complete.
|
||||
const echoContract = async (ctx) => {
|
||||
const contract = async (ctx, readonly = false) => {
|
||||
|
||||
// We just save execution timestamp as an example state file change.
|
||||
if (!ctx.readonly) {
|
||||
if (!readonly) {
|
||||
fs.appendFileSync(exectsFile, "ts:" + ctx.timestamp + "\n");
|
||||
|
||||
const stats = fs.statSync(exectsFile);
|
||||
@@ -56,7 +56,7 @@ const echoContract = async (ctx) => {
|
||||
// ctx.unl.find("<public key hex>");
|
||||
|
||||
// NPL messages example.
|
||||
// if (!ctx.readonly) {
|
||||
// if (!readonly) {
|
||||
// // Start listening to incoming NPL messages before we send ours.
|
||||
// const promise = new Promise((resolve, reject) => {
|
||||
// let timeout = setTimeout(() => {
|
||||
@@ -84,5 +84,33 @@ const echoContract = async (ctx) => {
|
||||
// await ctx.updateConfig(config);
|
||||
}
|
||||
|
||||
const fallback = async (ctx) => {
|
||||
console.log(`Fallback mode: Non consensus execution count: ${ctx.nonConsensusRounds}`);
|
||||
// NPL messages example.
|
||||
// Start listening to incoming NPL messages before we send ours.
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
let timeout = setTimeout(() => {
|
||||
reject('NPL timeout.');
|
||||
}, 2000);
|
||||
|
||||
let list = [];
|
||||
ctx.unl.onMessage((node, msg) => {
|
||||
console.log(`${node.publicKey} said ${msg} to me.`);
|
||||
list.push(msg);
|
||||
if (list.length == ctx.unl.list().length) {
|
||||
clearTimeout(timeout);
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
await ctx.unl.send("Hello");
|
||||
await promise;
|
||||
}
|
||||
|
||||
const hpc = new HotPocket.Contract();
|
||||
hpc.init(echoContract);
|
||||
hpc.init({
|
||||
"consensus": async (ctx) => { await contract(ctx, false); },
|
||||
"consensus_fallback": async (ctx) => { await fallback(ctx); },
|
||||
"read_req": async (ctx) => { await contract(ctx, true); }
|
||||
});
|
||||
@@ -2,7 +2,7 @@ const HotPocket = require("hotpocket-nodejs-contract");
|
||||
const fs = require('fs');
|
||||
const bson = require('bson');
|
||||
|
||||
const fileContract = async (ctx) => {
|
||||
const fileContract = async (ctx, readonly = false) => {
|
||||
|
||||
for (const user of ctx.users.list()) {
|
||||
|
||||
@@ -54,7 +54,7 @@ const fileContract = async (ctx) => {
|
||||
}));
|
||||
}
|
||||
}
|
||||
else if (msg.type == "download") {
|
||||
else if (readonly && msg.type == "download") {
|
||||
if (fs.existsSync(msg.fileName)) {
|
||||
const fileContent = fs.readFileSync(msg.fileName);
|
||||
await user.send(bson.serialize({
|
||||
@@ -76,5 +76,14 @@ const fileContract = async (ctx) => {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const fallback = async (ctx) => {
|
||||
console.log(`Fallback mode: Non consensus execution count: ${ctx.nonConsensusRounds}`);
|
||||
}
|
||||
|
||||
const hpc = new HotPocket.Contract();
|
||||
hpc.init(fileContract, HotPocket.clientProtocols.bson);
|
||||
hpc.init({
|
||||
"consensus": async (ctx) => { await fileContract(ctx); },
|
||||
"consensus_fallback": async (ctx) => { await fallback(ctx); },
|
||||
"read_req": async (ctx) => { await fileContract(ctx, true); }
|
||||
}, HotPocket.clientProtocols.bson);
|
||||
|
||||
14
examples/nodejs_contract/package-lock.json
generated
14
examples/nodejs_contract/package-lock.json
generated
@@ -6,7 +6,7 @@
|
||||
"": {
|
||||
"dependencies": {
|
||||
"bson": "4.0.4",
|
||||
"hotpocket-nodejs-contract": "0.5.10",
|
||||
"hotpocket-nodejs-contract": "0.7.0",
|
||||
"seedrandom": "3.0.5"
|
||||
}
|
||||
},
|
||||
@@ -37,9 +37,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/hotpocket-nodejs-contract": {
|
||||
"version": "0.5.10",
|
||||
"resolved": "https://registry.npmjs.org/hotpocket-nodejs-contract/-/hotpocket-nodejs-contract-0.5.10.tgz",
|
||||
"integrity": "sha512-1Cw9WcyoQmJabGkjRbM4FZ1HMvUI3z6IVTbhi0BDiD+K1c6bHROPgNKbIHCSL15+Z5xvbHpeuOOPLGogGtdC7Q=="
|
||||
"version": "0.7.0",
|
||||
"resolved": "https://registry.npmjs.org/hotpocket-nodejs-contract/-/hotpocket-nodejs-contract-0.7.0.tgz",
|
||||
"integrity": "sha512-MHIhGGzPBsKkmXMZrr8L3ze6cSqlSKQ+V2J59/fSS7FDeo5+Z983ZOwt1aBvaLmkN22YOvtLVdBUn1EEamryOg=="
|
||||
},
|
||||
"node_modules/ieee754": {
|
||||
"version": "1.1.13",
|
||||
@@ -82,9 +82,9 @@
|
||||
}
|
||||
},
|
||||
"hotpocket-nodejs-contract": {
|
||||
"version": "0.5.10",
|
||||
"resolved": "https://registry.npmjs.org/hotpocket-nodejs-contract/-/hotpocket-nodejs-contract-0.5.10.tgz",
|
||||
"integrity": "sha512-1Cw9WcyoQmJabGkjRbM4FZ1HMvUI3z6IVTbhi0BDiD+K1c6bHROPgNKbIHCSL15+Z5xvbHpeuOOPLGogGtdC7Q=="
|
||||
"version": "0.7.0",
|
||||
"resolved": "https://registry.npmjs.org/hotpocket-nodejs-contract/-/hotpocket-nodejs-contract-0.7.0.tgz",
|
||||
"integrity": "sha512-MHIhGGzPBsKkmXMZrr8L3ze6cSqlSKQ+V2J59/fSS7FDeo5+Z983ZOwt1aBvaLmkN22YOvtLVdBUn1EEamryOg=="
|
||||
},
|
||||
"ieee754": {
|
||||
"version": "1.1.13",
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"build-diag": "ncc build diagnostic_contract.js -o dist/diagnostic-contract"
|
||||
},
|
||||
"dependencies": {
|
||||
"hotpocket-nodejs-contract": "0.5.10",
|
||||
"hotpocket-nodejs-contract": "0.7.0",
|
||||
"bson": "4.0.4",
|
||||
"seedrandom": "3.0.5"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user