codex-factory/scripts/gen-traffic.js
nugaon 9e366130b8
feat: 0.6.0 Bee (#4)
* feat: postage stamp

* chore: remove empty postage stamp sol

* fix: start the nodes with the new '--full-node' flag

* chore: blockchain image 1.1.1 - updated factory bin (#29)

* fix: add network id to the bee statup params

* fix: change payment treshold limit to the possible min value

* feat: add port-maps to the environment sh

* fix: specify the same chainId as the networkId

* feat: expose blockchain 9545 port to localhost

* feat: extended postage stamp contract

* refactor: remove price oracle setups

* feat: updated the gen-traffic to work with postage stamps (#28)

* feat: updated the gen-traffic to work with postage stamps

* chore: update to latest bee-js version

Co-authored-by: Vojtech Simetka <vojtech@simetka.cz>
2021-05-20 17:41:57 +02:00

83 lines
2.4 KiB
JavaScript

const axios = require('axios').default;
const Bee = require('@ethersphere/bee-js').Bee;
const SLEEP_BETWEEN_UPLOADS_MS = 100
const POSTAGE_STAMPS_AMOUNT = BigInt(10000)
const POSTAGE_STAMPS_DEPTH = 20
/**
* Lehmer random number generator with seed (minstd_rand in C++11)
* !!! Very fast but not well distributed pseudo-random function !!!
*
* @param seed Seed for the pseudo-random generator
*/
function lrng(seed) {
return () => ((2 ** 31 - 1) & (seed = Math.imul(48271, seed))) / 2 ** 31
}
/**
* Utility function for generating random Buffer
* !!! IT IS NOT CRYPTO SAFE !!!
* For that use `crypto.randomBytes()`
*
* @param length Number of bytes to generate
* @param seed Seed for the pseudo-random generator
*/
function randomByteArray(length, seed = 500) {
const rand = lrng(seed)
const buf = new Uint8Array(length)
for (let i = 0; i < length; ++i) {
buf[i] = (rand() * 0xff) << 0
}
return buf
}
async function trafficGen(bee, postageBatchId, seed = 500, bytes = 1024 * 4 * 400) {
const randomBytes = randomByteArray(bytes, seed)
const ref = await bee.uploadData(postageBatchId, randomBytes)
console.log(`Generated ${bytes} bytes traffic, the random data's root reference: ${ref}`)
}
/**
* Generate traffic on Bee node(s)
*
* @param bees Array of Bee instances and postage batches where the random generated data will be sent to.
*/
async function genTrafficOnOpenPorts(bees) {
const promises = bees.map(({bee, postageBatchId}) => {
console.log(`Generate Swarm Chunk traffic on ${bee.url}...`)
return trafficGen(bee, postageBatchId, new Date().getTime())
})
await Promise.all(promises)
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function genTrafficLoop(hosts) {
const promisses = hosts.map(async (host) => {
const bee = new Bee(host)
console.log(`Generating postage stamps on ${host}...`)
const postageBatchId = await bee.createPostageBatch(POSTAGE_STAMPS_AMOUNT, POSTAGE_STAMPS_DEPTH)
console.log(`Generated ${postageBatchId} postage stamp on ${host}...`)
return {bee, postageBatchId}
})
const bees = await Promise.all(promisses)
while(true) {
await genTrafficOnOpenPorts(bees)
await sleep(SLEEP_BETWEEN_UPLOADS_MS)
}
}
const inputArray = process.argv.slice(2)
const hosts = inputArray.length > 0 ? inputArray : [ 'http://localhost:1633' ]
genTrafficLoop(hosts)