@ethersphere/bee-factory
Version:
Local Ethereum Swarm development stack
64 lines • 2.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateTraffic = generateTraffic;
const bee_js_1 = require("@ethersphere/bee-js");
const config_1 = require("./../config");
const REQUEST_OPTIONS = { timeout: 30000 };
const TRAFFIC_TIMEOUT_MS = 10 * 60000; // 10 minutes
const PEER_WAIT_TIMEOUT_MS = 2 * 60000; // 2 minutes
async function generateTraffic() {
const work = async () => {
const uploaderBee = new bee_js_1.Bee(`http://localhost:${config_1.BEE_NODES[1].apiPort}`);
const chequeMonitorBee = new bee_js_1.Bee(`http://localhost:${config_1.BEE_NODES[2].apiPort}`);
await waitForSpecificPeer(config_1.BEE_NODES[1].apiPort, config_1.BEE_NODES[2].apiPort, PEER_WAIT_TIMEOUT_MS);
const batchId = await uploaderBee.buyStorage(bee_js_1.Size.fromGigabytes(1), bee_js_1.Duration.fromWeeks(2), undefined, REQUEST_OPTIONS);
while (true) {
const claimableChequeCount = await getClaimableChequeNumber(chequeMonitorBee);
if (claimableChequeCount >= 1) {
break;
}
for (let i = 0; i < 1000; i++) {
await uploadRandomData(uploaderBee, batchId);
}
}
};
let timer;
const timeout = new Promise((_, reject) => {
timer = setTimeout(() => reject(new Error(`generateTraffic timed out after ${TRAFFIC_TIMEOUT_MS / 1000}s — node 1 never issued a cheque to node 2`)), TRAFFIC_TIMEOUT_MS);
});
try {
return await Promise.race([work(), timeout]);
}
finally {
clearTimeout(timer);
}
}
async function waitForSpecificPeer(uploaderPort, peerPort, timeoutMs) {
const uploaderBee = new bee_js_1.Bee(`http://localhost:${uploaderPort}`);
const peerBee = new bee_js_1.Bee(`http://localhost:${peerPort}`);
const { overlay } = await peerBee.getNodeAddresses(REQUEST_OPTIONS);
const overlayHex = overlay.toHex();
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const peers = await uploaderBee.getPeers(REQUEST_OPTIONS);
if (peers.some(p => p.address === overlayHex))
return;
await new Promise(r => setTimeout(r, 2000));
}
throw new Error(`node 1 did not connect to node 2 within ${timeoutMs / 1000}s`);
}
async function getClaimableChequeNumber(bee) {
const { lastcheques } = await bee.getLastCheques(REQUEST_OPTIONS);
let count = 0;
for (const cheque of lastcheques) {
if (cheque.lastreceived === null) {
continue;
}
count++;
}
return count;
}
async function uploadRandomData(bee, batchId) {
await bee.uploadData(batchId, crypto.getRandomValues(new Uint8Array(4096)), undefined, REQUEST_OPTIONS);
}
//# sourceMappingURL=traffic_generator.js.map