2023-01-02 13:52:05 -06:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<head>
|
|
|
|
<title>Availability test</title>
|
|
|
|
<link type="text/css" rel="stylesheet" href="/index.css" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="availability-test"></div>
|
|
|
|
</body>
|
|
|
|
<script type="module">
|
|
|
|
import { Box } from "/classes/box.js";
|
|
|
|
import { Scene } from "/classes/scene.js";
|
2023-01-03 12:00:12 -06:00
|
|
|
import { Expert } from "/classes/expert.js";
|
2023-01-02 13:52:05 -06:00
|
|
|
import { Bench } from "/classes/bench.js";
|
|
|
|
import { Business } from "/classes/business.js";
|
|
|
|
import { Availability } from "/classes/availability.js";
|
|
|
|
import { delay } from "/util.js";
|
|
|
|
import { Forum } from "/classes/forum.js";
|
|
|
|
import { Public } from "/classes/public.js";
|
|
|
|
|
|
|
|
const DELAY_INTERVAL = 500;
|
|
|
|
|
|
|
|
const rootElement = document.getElementById("availability-test");
|
|
|
|
const rootBox = new Box("rootBox", rootElement).flex();
|
|
|
|
|
|
|
|
const scene = (window.scene = new Scene("Availability test", rootBox).log(
|
|
|
|
"sequenceDiagram"
|
|
|
|
));
|
|
|
|
|
|
|
|
const members = (window.members = []);
|
2023-01-03 12:00:12 -06:00
|
|
|
const newExpert = async () => {
|
2023-01-02 13:52:05 -06:00
|
|
|
const index = members.length;
|
2023-01-03 12:00:12 -06:00
|
|
|
const name = `Expert${index + 1}`;
|
|
|
|
const member = await new Expert(name, scene).initialize();
|
2023-01-02 13:52:05 -06:00
|
|
|
members.push(member);
|
|
|
|
return member;
|
|
|
|
};
|
|
|
|
|
2023-01-03 12:00:12 -06:00
|
|
|
const member1 = await newExpert();
|
|
|
|
const member2 = await newExpert();
|
|
|
|
await newExpert();
|
2023-01-02 13:52:05 -06:00
|
|
|
const bench = (window.bench = new Bench("Bench", scene));
|
|
|
|
const forum = (window.forum = new Forum(bench, "Forum", scene));
|
|
|
|
const availability = (window.bench = new Availability(
|
|
|
|
bench,
|
|
|
|
"Availability",
|
|
|
|
scene
|
|
|
|
));
|
|
|
|
const business = (window.business = new Business(
|
|
|
|
bench,
|
|
|
|
forum,
|
|
|
|
availability,
|
|
|
|
"Business",
|
|
|
|
scene
|
|
|
|
));
|
|
|
|
const requestor = new Public("Public", scene);
|
|
|
|
|
|
|
|
const updateDisplayValues = async () => {
|
2023-01-03 01:26:55 -06:00
|
|
|
for (const member of members) {
|
|
|
|
member.setValue(
|
|
|
|
"rep",
|
|
|
|
bench.reputations.getTokens(member.reputationPublicKey)
|
|
|
|
);
|
|
|
|
}
|
2023-01-02 13:52:05 -06:00
|
|
|
bench.setValue("total rep", bench.getTotalReputation());
|
|
|
|
await scene.renderSequenceDiagram();
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateDisplayValuesAndDelay = async () => {
|
|
|
|
await updateDisplayValues();
|
|
|
|
await delay(DELAY_INTERVAL);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getActiveWorker = async () => {
|
|
|
|
let worker;
|
|
|
|
let request;
|
|
|
|
for (const member of members) {
|
|
|
|
request = await member.getAssignedWork(availability, business);
|
|
|
|
if (request) {
|
|
|
|
worker = member;
|
|
|
|
worker.actions.getAssignedWork.log(worker, availability);
|
|
|
|
worker.activate();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { worker, request };
|
|
|
|
};
|
|
|
|
|
|
|
|
const voteForWorkEvidence = async (worker, pool) => {
|
|
|
|
for (const member of members) {
|
|
|
|
if (member !== worker) {
|
|
|
|
await member.castVote(pool, {
|
|
|
|
position: true,
|
|
|
|
stake: 1,
|
|
|
|
anonymous: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
await updateDisplayValuesAndDelay();
|
|
|
|
|
|
|
|
// Populate availability pool
|
|
|
|
await member1.registerAvailability(availability, 1);
|
|
|
|
await member2.registerAvailability(availability, 1);
|
|
|
|
await updateDisplayValuesAndDelay();
|
|
|
|
|
|
|
|
// Submit work request
|
|
|
|
await requestor.submitRequest(
|
|
|
|
business,
|
|
|
|
{ fee: 100 },
|
|
|
|
{ please: "do some work" }
|
|
|
|
);
|
|
|
|
await updateDisplayValuesAndDelay();
|
|
|
|
|
|
|
|
// Receive work request
|
|
|
|
const { worker, request } = await getActiveWorker();
|
|
|
|
|
|
|
|
// Submit work evidence
|
|
|
|
const pool = await worker.submitWork(
|
|
|
|
business,
|
|
|
|
request.id,
|
|
|
|
{
|
|
|
|
here: "is some evidence of work product",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tokenLossRatio: 1,
|
|
|
|
duration: 1000,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
worker.deactivate();
|
|
|
|
await updateDisplayValuesAndDelay();
|
|
|
|
|
|
|
|
// Vote on work evidence
|
|
|
|
await voteForWorkEvidence(worker, pool);
|
|
|
|
await updateDisplayValuesAndDelay();
|
|
|
|
|
|
|
|
// Wait for validation pool duration to elapse
|
|
|
|
await delay(1000);
|
|
|
|
|
|
|
|
// Distribute reputation awards and fees
|
|
|
|
await pool.evaluateWinningConditions();
|
|
|
|
await updateDisplayValuesAndDelay();
|
|
|
|
|
|
|
|
// This should throw an exception since the pool is already resolved
|
|
|
|
try {
|
|
|
|
await pool.evaluateWinningConditions();
|
|
|
|
} catch (e) {
|
|
|
|
if (e.message.match(/Validation pool has already been resolved/)) {
|
|
|
|
console.log(
|
|
|
|
"Caught expected error: Validation pool has already been resolved"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.error("Unexpected error");
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|