Skip to content

Instantly share code, notes, and snippets.

@jgrizzled
Created June 10, 2015 11:43
Show Gist options
  • Save jgrizzled/82ffb2e3d57d89159f70 to your computer and use it in GitHub Desktop.
Save jgrizzled/82ffb2e3d57d89159f70 to your computer and use it in GitHub Desktop.
//bloatSpam.js
//Spams contract storage transactions to bloater
//Usage: bloatSpam(numTx, numLoops, useFaucet=true, fromAccount=eth.accounts[0])
//numTx: number of transactions per batch
//numLoops: number of transaction batches
//fromAccount (optional): local account to send from
//useFaucet (optional): use decentralized faucet to replenish funds
//reddit.com/u/BroughtToUByCarlsJr
//Function that pauses script until pending transactions are cleared
//Call after sending transactions to ensure the rest of the script operates on the new state
//Usage: waitForPendingTx()
//maxBlocksToWait (optional): max number of blocks to wait
function waitForPendingTx(maxBlocksToWait) {
if(maxBlocksToWait === undefined || maxBlocksToWait < 1)
maxBlocksToWait = 60; //~10 min
console.log("Waiting for pending transactions...");
numLoops = 0;
sleep(10); //time to allow new Tx to become pending
while(eth.pendingTransactions().length > 0) {
admin.debug.waitForBlocks(eth.blockNumber+1);
numLoops++;
if(numLoops >= maxBlocksToWait) {
console.log("Max blocks to wait reached");
return;
}
}
console.log("Pending transactions cleared");
}
//import faucet contract
var faucet = eth.contract(JSON.parse(unescape((eth.getTransaction('0x8101fb4120fb359ec07310f554920947890fa622c9a2429c5c10a16e6a00b0dd').input).replace('0x','').replace(/(..)/g, '%$1')))).at('0xf0adc306641b0ec9a4d159c9f6a1f29d5b97672c');
//import bloater contract
bloaterMetadata = {"source":"contract bloater { bytes32[] public shit; uint public pilesOfShit; function bloat(bytes32 newshit, uint loops) { for(uint i = 0; i \u003c loops; i++) { shit.length++; shit[shit.length-1] = newshit; pilesOfShit++; } } }","language":"Solidity","languageVersion":"0","compilerVersion":"0.9.23","abiDefinition":[{"constant":true,"inputs":[],"name":"pilesOfShit","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"newshit","type":"bytes32"},{"name":"loops","type":"uint256"}],"name":"bloat","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"shit","outputs":[{"name":"","type":"bytes32"}],"type":"function"}],"userDoc":{"methods":{}},"developerDoc":{"methods":{}}};
var bloater = eth.contract(bloaterMetadata.abiDefinition).at(registrar.addr('bloater'));
//generates random data
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
return result;
}
allowedChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
function bloatSpam(numTx, numLoops, useFaucet, fromAccount) {
if(numTx === undefined || numTx < 1 || numLoops === undefined || numLoops < 1) {
console.log("Error: bad arguments");
return;
}
fromAccount = fromAccount || eth.accounts[0];
useFaucet = useFaucet || true;
beginningPOS = bloater.pilesOfShit();
for(i = 0; i < numLoops; i++) {
weiNeeded = web3.toWei(10,'szabo')*eth.getBlock('latest').gasLimit*numTx;
if(eth.getBalance(fromAccount) < weiNeeded) {
if(useFaucet) {
while(eth.getBalance(fromAccount) < weiNeeded) {
console.log('Requesting ether from faucet...');
while(faucet.getRemainingBlocks.call() > 0)
admin.debug.waitForBlocks(eth.blockNumber + 1);
faucet.getWei.sendTransaction({from: fromAccount});
waitForPendingTx();
}
console.log('Got ether from faucet!');
}else {
console.log('Insufficient balance.');
break;
}
}
console.log("Starting Tx batch " + (i+1));
expectedShit = 0;
currentPOS = Number(bloater.pilesOfShit());
for(j = 0; j < numTx; j++) {
shitToStore = randomString(32, allowedChars);
console.log("Sending Tx with data: " + shitToStore);
numCopies = Math.floor(eth.getBlock('latest').gasLimit/30640-4763/6128);
expectedShit += numCopies;
bloater.bloat.sendTransaction(shitToStore, numCopies, {from: fromAccount, gas: (eth.getBlock('latest').gasLimit)});
}
waitForPendingTx();
if(Number(bloater.pilesOfShit()) >= currentPOS + expectedShit)
console.log('All shit successful');
else
console.log('Copies failed to store: ' + (currentPOS + expectedShit - Number(bloater.pilesOfShit())));
}
console.log("Total storage difference: " + (Number(bloater.pilesOfShit()) - beginningPOS));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment