apxtrib/api/models/Nations.js

260 lines
8.5 KiB
JavaScript
Raw Normal View History

2023-03-27 05:52:21 +00:00
const bcrypt = require("bcrypt");
const fs = require("fs-extra");
const glob = require("glob");
const jwt = require("jwt-simple");
const axios = require("axios");
2023-05-31 13:19:21 +00:00
const path = require("path");
const conf = require(`${process.env.dirtown}/conf.json`);
const Odmdb = require("./Odmdb.js");
2023-04-13 05:46:35 +00:00
// lowercase 1st letter is normal
2023-05-31 13:19:21 +00:00
const towns = require("./Towns.js");
const pagans = require("./Pagans.js");
2023-01-22 09:53:09 +00:00
/*
Blockchain manager
2023-03-27 05:52:21 +00:00
* Manage network directory of nations and towns
2023-01-22 09:53:09 +00:00
* read Blockchain and search,
* submit a transaction (now) or contract (futur) to store from userA.pubkey to userB.pubkey a number of AXESS
* mine to be able to register a block and create AXESS
* manage APIXP rules 20 M APIXP 1AXESS = 1 block validation
* manage contract = action if something appened validate by a proof of work
*/
2023-04-13 05:46:35 +00:00
const Nations = {};
Nations.init = () => {
console.group("init Nations");
2023-03-27 05:52:21 +00:00
};
2023-04-13 05:46:35 +00:00
2023-05-31 13:19:21 +00:00
Nations.chaintown = (nationId, townId) => {
2023-04-13 05:46:35 +00:00
/**
2023-05-31 13:19:21 +00:00
* if not already exist Add a requested town into conf.towns.push({ "townId": "wall", "nationId": "ants", "dns": "wall-ants.ndda.fr" })
2023-04-13 05:46:35 +00:00
*/
2023-05-31 13:19:21 +00:00
};
Nations.updateobjectsfromfreshesttown = (dnstownlist, objectidx) => {
/**
* Get lasttime update per apxtrib object then choose the latest source and update local town
* if an item exist localy and does not from the town requested
* @Param {array} dnstownlist list of dns to get latest data
* @Param {object} objectidx objectnme:idxfile {agans:"alias_all.json",...}
* @return create/update nationchains/pagans town nation
*/
const localversion = {};
const objlist = Object.keys(objectidx);
objlist.forEach((o) => {
let objconf = {
name: o,
2023-06-28 13:23:17 +00:00
schema: `adminapi/schema/${o}.jsons`,
2023-05-31 13:19:21 +00:00
lastupdate: -1,
};
if (fs.existsSync(`${conf.dirapi}/nationchains/${o}/conf.json`)) {
objconf = fs.readJsonSync(`${conf.dirapi}/nationchains/${o}/conf.json`);
} else {
fs.outputJsonSync(`${conf.dirapi}/nationchains/${o}/conf.json`, objconf);
}
localversion[o] = [conf.dns[0], objconf.lastupdate];
});
//console.log(localversion);
for (let t = 0; t < dnstownlist.length; t++) {
2023-06-28 13:23:17 +00:00
if (conf.townId != dnstownlist[t].townId) { // to avoid update itself
let promiseconf = [];
let objecttotest = [];
objlist.forEach((o) => {
//console.log(`https://${dnstownlist[t].dns}/nationchains/${o}/conf.json`);
objecttotest.push(o);
promiseconf.push(
axios.get(`https://${dnstownlist[t].dns}/nationchains/${o}/conf.json`)
);
});
Promise.all(promiseconf)
.then((reps) => {
let promiseidx = [];
let objecttoupdate = [];
let objlastupdate = [];
for (let i = 0; i < objecttotest.length; i++) {
if (
parseInt(reps[i].data.lastupdate) >
parseInt(localversion[reps[i].data.name][1])
) {
// add promise to get data
/*console.log(
2023-05-31 13:19:21 +00:00
`https://${dnstownlist[t].dns}/nationchains/${
reps[i].data.name
}/idx/${objectidx[reps[i].data.name]}`
);*/
2023-06-28 13:23:17 +00:00
objecttoupdate.push(objecttotest[i]);
objlastupdate.push(reps[i].data.lastupdate);
promiseidx.push(
axios.get(
`https://${dnstownlist[t].dns}/nationchains/${
reps[i].data.name
}/idx/${objectidx[reps[i].data.name]}`
)
2023-05-31 13:19:21 +00:00
);
2023-04-13 05:46:35 +00:00
}
2023-06-28 13:23:17 +00:00
}
Promise.all(promiseidx)
.then((rets) => {
for (let j = 0; j < objecttoupdate.length; j++) {
Odmdb.updatefromidxall(
objecttoupdate[j],
objectidx[objecttoupdate[j]],
rets[j].data,
objlastupdate[j]
);
}
})
.catch((err) => {
console.log("ERR get idx data");
console.log(err);
});
})
.catch((err) => {
console.log("ERR get conf lastupdate");
console.log(err);
});
}
2023-05-31 13:19:21 +00:00
}
};
Nations.synchronizeold = () => {
2023-03-27 05:52:21 +00:00
/*
2023-04-13 05:46:35 +00:00
Run process to communicate with a list of towns to update network and transaction
*/
2023-03-27 05:52:21 +00:00
//update himself then send to other information
if (process.env.NODE_ENV != "prod") {
// Not concerned
return {};
}
const initcurrentinstance = {
fixedIP: "",
lastblocknumber: 0,
firsttimeupdate: 0,
lastimeupdate: 0,
positifupdate: 0,
negatifupdate: 0,
pubkeyadmin: "",
tribeids: [],
logins: [],
knowninstance: [],
};
let currentinstance = initcurrentinstance;
try {
currentinstance = fs.readFileSync(
2023-04-27 04:17:20 +00:00
`${conf.tribes}/${conf.mayorId}/nationchains/nodes/${conf.rootURL}`,
2023-03-27 05:52:21 +00:00
"utf-8"
);
} catch (err) {
console.log("first init");
}
2023-04-27 04:17:20 +00:00
const loginsglob = fs.readJsonSync(`${conf.tmp}/loginsglob.json`, "utf-8");
2023-03-27 05:52:21 +00:00
currentinstance.logins = Object.keys(loginsglob);
currentinstance.tribeids = [...new Set(Object.values(loginsglob))];
currentinstance.instanceknown = glob.Sync(
2023-04-27 04:17:20 +00:00
`${conf.tribes}/${conf.mayorId}/nationchains/nodes/*`
2023-03-27 05:52:21 +00:00
);
//Save it
fs.outputJsonSync(
2023-04-27 04:17:20 +00:00
`${conf.tribes}/${conf.mayorId}/nationchains/nodes/${conf.rootURL}`,
2023-03-27 05:52:21 +00:00
currentinstance
);
// proof of work
// try to find a key based on last block with difficulty
// if find then send to all for update and try to get token
2023-04-13 05:46:35 +00:00
// in any case rerun Nations.synchronize()
2023-03-27 05:52:21 +00:00
currentinstance.instanceknown.forEach((u) => {
2023-04-27 04:17:20 +00:00
if (u != conf.rootURL) {
2023-03-27 05:52:21 +00:00
//send currentinstance info and get back state of
axios
.post(`https://${u}/nationchains/push`, currentinstance)
.then((rep) => {
newdata = rep.payload.moreinfo;
//Available update info
fs.readJson(
2023-04-27 04:17:20 +00:00
`${conf.tribes}/${conf.mayorId}/nationchains/nodes/${u}`,
2023-03-27 05:52:21 +00:00
(err, data) => {
if (err) {
data.negatifupdate += 1;
data.lasttimeupdate = Date.now();
} else {
data.positifupdate += 1;
data.lastimeupdate = Date.now();
data.tribeids = newdata.tribeids;
data.logins = newdata.logins;
data.lastblocknumber = newdata.lastblocknumber;
newdata.knowninstance.forEach((k) => {
if (!data.knowninstance.includes(k)) {
data.knowninstance.push(k);
//init the domain for next update
initcurrentinstance.firsttimeupdate = Date.now();
fs.outputJson(
2023-04-27 04:17:20 +00:00
`${conf.tribes}/${conf.mayorId}/nationchains/nodes/${k}`,
2023-03-27 05:52:21 +00:00
initcurrentinstance,
"utf-8"
);
}
});
}
//save with info
fs.outputJson(
2023-04-27 04:17:20 +00:00
`${conf.tribes}/${conf.mayorId}/nationchains/nodes/${u}`,
2023-03-27 05:52:21 +00:00
data
);
}
);
})
.catch((err) => {
//Not available
data.negatifupdate += 1;
data.lasttimeupdate = Date.now();
fs.outputJson(
2023-04-27 04:17:20 +00:00
`${conf.tribes}/${conf.mayorId}/nationchains/nodes/${u}`,
2023-03-27 05:52:21 +00:00
data
);
});
}
});
2023-01-22 09:53:09 +00:00
};
2023-04-13 05:46:35 +00:00
Nations.create = (conf) => {
2023-03-27 05:52:21 +00:00
/*
2023-04-28 11:21:02 +00:00
@conf from a nationchains/socialworld/setup/townSetup {object, nationId, townId, dns}
2023-03-27 05:52:21 +00:00
@return
*/
const res = {};
2023-05-31 13:19:21 +00:00
if (conf.object == "towns") {
Odmdb.create("nationchains/socialworld/objects", "towns", conf);
2023-03-27 05:52:21 +00:00
}
2023-04-28 11:21:02 +00:00
const nations = fs.readJsonSync(
"./nationchains/nations/idx/nationId_all.json"
2023-03-27 05:52:21 +00:00
);
2023-04-28 11:21:02 +00:00
if (!ObjectKeys(nations).includes(conf.nationId)) {
2023-03-27 05:52:21 +00:00
res.status = 404;
2023-04-28 11:21:02 +00:00
res.info = `your nationId ${conf.nationId} does not exist you have to choose an existing one`;
2023-03-27 05:52:21 +00:00
return res;
}
2023-05-31 13:19:21 +00:00
const towns = fs.readJsonSync("./nationchains/towns/idx/townId_all.json");
2023-04-28 11:21:02 +00:00
if (towns[conf.nationId].includes(conf.townId)) {
2023-03-27 05:52:21 +00:00
res.status = 409;
2023-04-28 11:21:02 +00:00
res.info = `This conf.townId already exist you have to find a unique town name`;
2023-03-27 05:52:21 +00:00
return res;
}
const towndata = {
2023-04-28 11:21:02 +00:00
uuid: conf.townId,
nationid: conf.nationId,
url: `${conf.townId}.${conf.nationId}.${conf.dns}`,
2023-05-31 13:19:21 +00:00
status: conf.dns == "unchain" ? "unchain" : "tochain",
2023-03-27 05:52:21 +00:00
};
2023-05-31 13:19:21 +00:00
const metatown = fs.readJsonSync(
"./nationchains/socialworld/metaobject/towns.json"
);
Odmdb.add(objectpath, towns, metatown, towndata);
2023-03-27 05:52:21 +00:00
fs.outputJsonSync(
2023-04-28 11:21:02 +00:00
`./nationchains/socialworld/objects/towns/${townId}.json`,
2023-03-27 05:52:21 +00:00
towndata
);
2023-05-31 13:19:21 +00:00
res.status = 200;
res.info = `${townId} create for ${nationId} nation`;
return res;
2023-03-27 05:52:21 +00:00
};
2023-01-22 09:53:09 +00:00
2023-04-27 04:17:20 +00:00
module.exports = Nations;