150 lines
4.8 KiB
JavaScript
Raw Normal View History

const { Bee, BeeDebug } = require('@ethersphere/bee-js')
class BeePair {
/**
* @param {BeeDebug} chequeReceiverBeeDebug
* @param {Bee} uploaderBee
* @param {BeeDebug} uploaderBeeDebug
* @param {string} uploaderStamp
*/
constructor(chequeReceiverBeeDebug, uploaderBee, uploaderBeeDebug, uploaderStamp) {
this.chequeReceiverBeeDebug = chequeReceiverBeeDebug
this.uploaderBee = uploaderBee
this.uploaderBeeDebug = uploaderBeeDebug
this.uploaderStamp = uploaderStamp
}
}
feat: bee 1.0.0 (#41) * feat: new Price Oracle bytecode * refactor: remove file smth.txt * feat: new PostageStamp contract * feat: change contract deploy workflow * feat: bee-1.0.0-rc2 compatible runner * feat: build from source (#47) * feat: extend build-envrionment.sh with building bee image from source * ci: new workflow option parameter - beeVersionAsCommitHash * fix(ci): try to overwrite BEE_VERSION sys variable * fix: do not export BEE_VERSION after env build * fix(ci): run build-environment without source command * fix(ci): try to retrieve built BEE_VERSION value * fix: save build BEE_VERSION value * fix(ci): retrieve built image tag in the last step * refactor: echoerr * refactor(ci): export sys variables * feat: disable warmup time (#46) * feat: disable warmup time * fix: place warmup-time to the correct place * fix: put quote back where it truly belongs * feat: state commit (#45) * refactor: remove payment treshold option because it causes performance issues * docs: design planned parameters for the new build workflow * feat: build environment with traffic gen option * fix(log): rephrase traffic gen log * feat: special bee version tagging when state commit happens * build: new env variable STATE_COMMIT * ci: STATE_COMMIT * feat: state commit scripts * refactor: destroy containers after state producing * refactor: remove unnecessary echo * fix: blockchain version at state commit * build: bumo bee-js version * build: update package-lock * fix: fixes * ci: build environment workflow with state commit * refactor: buy larger stamp * fix: publish workflow * refactor: increase stamp depth * fix: bee version fetch at commit * refactor: start containers normally instead of ephemeral for debugging * fix(ci): add chown for bee user on bee-data-dirs in order to write bee state * fix: try out the permission on bee data dir with 777 chmod * fix(ci): give folder permission in the build environment phrase * refactor(ci): raise sleep between uploads in order to generate cheques * refactor: wait 11 secs after batch purchase * fix: commit version tag string true instead of boolean * fix: add state commit check for set COMMIT_VERSION_TAG * chore: bump bee version Co-authored-by: Cafe137 <77121044+Cafe137@users.noreply.github.com>
2021-06-21 16:10:55 +02:00
const SLEEP_BETWEEN_UPLOADS_MS = 1000
const POSTAGE_STAMPS_AMOUNT = '10000'
const POSTAGE_STAMPS_DEPTH = 22
/**
* 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
}
/**
* @param {BeePair} beePair
*/
async function uploadRandomBytes(beePair, seed = 500, bytes = 1024 * 4 * 400) {
const randomBytes = randomByteArray(bytes, seed)
const reference = await beePair.uploaderBee.uploadData(beePair.uploaderStamp, randomBytes)
console.log(`${beePair.uploaderBee.url} uploaded ${bytes} bytes to ${reference}`)
}
/**
* Generate traffic on Bee node(s)
*
* @param {BeePair[]} beePairs
*/
async function genTrafficOnOpenPorts(beePairs) {
const promises = beePairs.map(beePair => {
return uploadRandomBytes(beePair, Date.now())
})
await Promise.all(promises)
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
/**
*
* Generates cheques on the given Bee API EP
*
* The hosts parameter has to be assimetric in the API;DEBUG_API paired string
* because on the API EP the data will be generated, so the cheques should be
*
* @param {string[]} hosts API;DEBUG_API URL strings of the target Bee (e.g. http://localhost:1635;http://localhost:11633;http://localhost:11635)
* @param {number} minCheques
*/
async function genTrafficLoop(hosts, minCheques) {
const promises = hosts.map(async host => {
const [chequeReceiverBeeDebugUrl, uploaderBeeUrl, uploaderBeeDebugUrl] = host.split(';')
const chequeReceiverBeeDebug = new BeeDebug(chequeReceiverBeeDebugUrl)
const uploaderBee = new Bee(uploaderBeeUrl)
const uploaderBeeDebug = new BeeDebug(uploaderBeeDebugUrl)
console.log(`Creating postage stamp on ${uploaderBeeDebugUrl}...`)
const postageBatchId = await uploaderBeeDebug.createPostageBatch(POSTAGE_STAMPS_AMOUNT, POSTAGE_STAMPS_DEPTH, {
waitForUsable: true,
})
console.log(`Generated ${postageBatchId} postage stamp on ${uploaderBeeDebugUrl}.`)
return new BeePair(chequeReceiverBeeDebug, uploaderBee, uploaderBeeDebug, postageBatchId)
})
const bees = await Promise.all(promises)
while (true) {
await genTrafficOnOpenPorts(bees)
if (!isNaN(minCheques)) {
const beesUncashedCheques = []
for (const beePair of bees) {
const { chequeReceiverBeeDebug } = beePair
const { lastcheques } = await chequeReceiverBeeDebug.getLastCheques()
const incomingCheques = lastcheques.filter(cheque => !!cheque.lastreceived)
const uncashedCheques = []
const lastCashOutPromises = incomingCheques.map(({ peer }) => chequeReceiverBeeDebug.getLastCashoutAction(peer))
const lastCashOuts = await Promise.all(lastCashOutPromises)
for (const [index, lastCashOut] of lastCashOuts.entries()) {
if (BigInt(lastCashOut.uncashedAmount) > 0) {
uncashedCheques.push(incomingCheques[index])
}
}
beesUncashedCheques.push(uncashedCheques)
}
if (beesUncashedCheques.every(uncashedCheques => uncashedCheques.length >= minCheques)) {
console.log(`Generated at least ${minCheques} for every node on the given Debug API endpoints`)
process.exit()
} else {
console.log(
`There is not enough uncashed cheques on Bee node(s)`,
beesUncashedCheques.map(beeCheques => beeCheques.length),
)
}
}
await sleep(SLEEP_BETWEEN_UPLOADS_MS)
}
}
let inputArray = process.argv.slice(2)
// if there is no related input to the minimum required cheques count,
// then the traffic generation will go infinitely
let minCheques = parseInt(inputArray[0])
let hosts = inputArray.slice(1)
if (hosts.length === 0) {
hosts = ['http://localhost:1635;http://localhost:11633;http://localhost:11635']
}
genTrafficLoop(hosts, minCheques)