Unable to define a `jiff-instance` when trying to expose as an API endpoint.
Hi, I'm trying to port sum to be an API I can call via cURL requests from the terminal. I am first connecting it, then checking if the connection exists, and then sending an input of 10. The error is either something along the lines of jiff_instance not found or sadd not defined depending on my try-catch blocks. I console log the jiff instance, and there is an object that is define (and would print undefined if I didn't connect first). It's a drop-in on the summation example, everything else is the same except server.js presented below.
const express = require('express');
const bodyParser = require('body-parser');
const mpc = require('./mpc');
const app = express();
const port = 8080;
app.use(bodyParser.json());
// Store jiff instances by computation_id
let jiffInstances = {};
app.post('/connect', (req, res) => {
const hostname = req.body.hostname;
const computation_id = req.body.computation_id;
const options = req.body.options;
console.log('Connecting to the server and initializing the JIFF instance with options', options);
let jiff_instance = mpc.connect(hostname, computation_id, options);
jiffInstances[computation_id] = jiff_instance; // Store instance
res.json({ id: jiff_instance.id });
});
app.post('/compute', (req, res) => {
const input = req.body.input;
const computation_id = req.body.computation_id; // Use computation_id to retrieve the correct instance
let jiff_instance = jiffInstances[computation_id]; // Retrieve instance
if (!jiff_instance) {
return res.status(400).json({ error: "Invalid computation_id or JIFF instance not found" });
}
console.log(jiff_instance);
const partyCount = req.body.partyCount || 2;
console.log('Performing MPC computation with input', input);
mpc.compute(input, jiff_instance).then(result => {
console.log('MPC computation result', result);
res.json({ result });
jiff_instance.disconnect(true, true);
delete jiffInstances[computation_id]; // Optionally remove the instance after use
});
});
app.get('/get_jiff_instance_list', (req, res) => {
res.json(Object.keys(jiffInstances));
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
I'm not really sure why you are getting this error here but seems to be an issue with your setup:
Are you running a different instance of your server (on a different port) for every party you have, or just one instance and connecting to it multiple times? If the later, you will run into issues because you would be overriding the different instances that correspond to different parties in your jiffInstances map.
What lines do you get this error in?
Do you wait until all parties connect before calling /compute?