diff --git a/bench/.gitignore b/bench/.gitignore new file mode 100644 index 000000000..e69de29bb diff --git a/bench/bech32.js b/bench/bech32.js deleted file mode 100644 index 002f81830..000000000 --- a/bench/bech32.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; - -const Address = require('../lib/primitives/address'); -const random = require('bcrypto/lib/random'); -const bench = require('./bench'); -const addrs = []; - -{ - const end = bench('serialize'); - for (let i = 0; i < 100000; i++) { - const addr = Address.fromProgram(0, random.randomBytes(20)); - addrs.push(addr.toBech32()); - } - end(100000); -} - -{ - const end = bench('parse'); - for (let i = 0; i < 100000; i++) { - const b32 = addrs[i]; - const addr = Address.fromBech32(b32); - addrs[i] = addr; - } - end(100000); -} - -console.error(addrs[0][0]); diff --git a/bench/bench.js b/bench/bench.js deleted file mode 100644 index 8e486c082..000000000 --- a/bench/bench.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -module.exports = function bench(name) { - const start = process.hrtime(); - return function end(ops) { - const elapsed = process.hrtime(start); - const time = elapsed[0] + elapsed[1] / 1e9; - const rate = ops / time; - - console.log('%s: ops=%d, time=%d, rate=%s', - name, ops, time, rate.toFixed(5)); - }; -}; diff --git a/bench/buffer.js b/bench/buffer.js deleted file mode 100644 index 847a0e94e..000000000 --- a/bench/buffer.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -const BufferWriter = require('../lib/utils/writer'); -const StaticWriter = require('../lib/utils/staticwriter'); -const common = require('../test/util/common'); -const bench = require('./bench'); - -const tx5 = common.readTX('tx5'); - -{ - const [tx] = tx5.getTX(); - const end = bench('serialize (static-writer)'); - for (let i = 0; i < 10000; i++) { - tx.refresh(); - const {size} = tx.getWitnessSizes(); - const bw = new StaticWriter(size); - tx.toWitnessWriter(bw); - bw.render(); - } - end(10000); -} - -{ - const [tx] = tx5.getTX(); - const end = bench('serialize (buffer-writer)'); - for (let i = 0; i < 10000; i++) { - tx.refresh(); - const bw = new BufferWriter(); - tx.toWitnessWriter(bw); - bw.render(); - } - end(10000); -} diff --git a/bench/chacha.js b/bench/chacha.js deleted file mode 100644 index a1f3161c2..000000000 --- a/bench/chacha.js +++ /dev/null @@ -1,56 +0,0 @@ -'use strict'; - -const ChaCha20 = require('bcrypto/lib/chacha20'); -const Poly1305 = require('bcrypto/lib/poly1305'); -const hash256 = require('bcrypto/lib/hash256'); -const bench = require('./bench'); - -console.log('note: rate measured in kb/s'); - -const chacha = new ChaCha20(); -const poly = new Poly1305(); -const key = Buffer.alloc(32, 0x02); -const iv = Buffer.from('0102030405060708', 'hex'); -const chunk = Buffer.allocUnsafe(32); -const data = Buffer.allocUnsafe(32); - -for (let i = 0; i < 32; i++) - chunk[i] = i; - -for (let i = 0; i < 32; i++) - data[i] = i & 0xff; - -chacha.init(key, iv, 0); -poly.init(key); - -{ - const end = bench('encrypt'); - for (let i = 0; i < 1000000; i++) - chacha.encrypt(chunk); - end(1000000 * 32 / 1024); -} - -{ - const end = bench('update'); - for (let i = 0; i < 1000000; i++) - poly.update(data); - end(1000000 * 32 / 1024); -} - -{ - const end = bench('finish'); - for (let i = 0; i < 1000000; i++) { - poly.init(key); - poly.update(data); - poly.finish(); - } - end(1000000 * 32 / 1024); -} - -// For reference: -{ - const end = bench('sha256'); - for (let i = 0; i < 1000000; i++) - hash256.digest(data); - end(1000000 * 32 / 1024); -} diff --git a/bench/coins.js b/bench/coins.js deleted file mode 100644 index fea14577f..000000000 --- a/bench/coins.js +++ /dev/null @@ -1,34 +0,0 @@ -'use strict'; - -const CoinView = require('../lib/coins/coinview'); -const BufferReader = require('../lib/utils/reader'); -const StaticWriter = require('../lib/utils/writer'); -const common = require('../test/util/common'); -const bench = require('./bench'); - -const [tx, view] = common.readTX('tx3').getTX(); - -{ - const end = bench('serialize'); - - for (let i = 0; i < 10000000; i++) { - const bw = new StaticWriter(view.getSize(tx)); - view.toWriter(bw, tx).render(); - } - - end(10000000); -} - -{ - const bw = new StaticWriter(view.getSize(tx)); - const raw = view.toWriter(bw, tx).render(); - - const end = bench('parse'); - - for (let i = 0; i < 10000000; i++) { - const br = new BufferReader(raw); - CoinView.fromReader(br, tx); - } - - end(10000000); -} diff --git a/bench/merkle.js b/bench/merkle.js deleted file mode 100644 index b182e02f8..000000000 --- a/bench/merkle.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict'; - -const assert = require('assert'); -const merkle = require('bcrypto/lib/merkle'); -const random = require('bcrypto/lib/random'); -const bench = require('./bench'); - -const leaves = []; - -for (let i = 0; i < 3000; i++) - leaves.push(random.randomBytes(32)); - -{ - const end = bench('tree'); - for (let i = 0; i < 1000; i++) { - const [n, m] = merkle.createTree(leaves.slice()); - assert(n); - assert(!m); - } - end(1000); -} diff --git a/bench/mnemonic.js b/bench/mnemonic.js deleted file mode 100644 index ce9f250e4..000000000 --- a/bench/mnemonic.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -const assert = require('assert'); -const bench = require('./bench'); -const HD = require('../lib/hd'); -const Mnemonic = require('../lib/hd/mnemonic'); - -const mnemonic = new Mnemonic(); -HD.fromMnemonic(mnemonic); - -const phrase = mnemonic.getPhrase(); - -assert.strictEqual(Mnemonic.fromPhrase(phrase).getPhrase(), phrase); - -{ - const end = bench('fromPhrase'); - for (let i = 0; i < 10000; i++) - Mnemonic.fromPhrase(phrase); - end(10000); -} diff --git a/bench/script.js b/bench/script.js deleted file mode 100644 index 5d64b10f1..000000000 --- a/bench/script.js +++ /dev/null @@ -1,17 +0,0 @@ -'use strict'; - -const random = require('bcrypto/lib/random'); -const Script = require('../lib/script/script'); -const bench = require('./bench'); - -const hashes = []; - -for (let i = 0; i < 100000; i++) - hashes.push(random.randomBytes(20)); - -{ - const end = bench('hash'); - for (let i = 0; i < hashes.length; i++) - Script.fromPubkeyhash(hashes[i]); - end(100000); -} diff --git a/bench/tx.js b/bench/tx.js deleted file mode 100644 index 3e4ddebd4..000000000 --- a/bench/tx.js +++ /dev/null @@ -1,200 +0,0 @@ -'use strict'; - -const random = require('bcrypto/lib/random'); -const Address = require('../lib/primitives/address'); -const TX = require('../lib/primitives/tx'); -const Script = require('../lib/script/script'); -const MTX = require('../lib/primitives/mtx'); -const consensus = require('../lib/protocol/consensus'); -const common = require('../test/util/common'); -const bench = require('./bench'); - -const tx3 = common.readTX('tx3'); -const tx5 = common.readTX('tx5'); -const tx10 = common.readTX('tx10'); - -{ - const raw = tx5.getRaw(); - const end = bench('parse'); - - for (let i = 0; i < 10000; i++) - TX.fromRaw(raw); - - end(10000); -} - -{ - const [tx, view] = tx5.getTX(); - const end = bench('sigops'); - - for (let i = 0; i < 100000; i++) - tx.getSigops(view); - - end(100000); -} - -{ - const [tx] = tx5.getTX(); - const end = bench('serialize'); - - for (let i = 0; i < 10000; i++) { - tx._raw = null; - tx.toRaw(); - } - - end(10000); -} - -{ - const [tx] = tx3.getTX(); - const end = bench('hash'); - - for (let i = 0; i < 30000; i++) { - tx.hash(); - tx._hash = null; - } - - end(30000); -} - -{ - const [tx] = tx5.getTX(); - const end = bench('witness hash'); - - for (let i = 0; i < 30000; i++) { - tx.witnessHash(); - tx._whash = null; - } - - end(30000); -} - -{ - const [tx] = tx5.getTX(); - const end = bench('sanity'); - - for (let i = 0; i < 10000; i++) - tx.isSane(); - - end(10000); -} - -{ - const [tx] = tx5.getTX(); - const end = bench('input hashes'); - - for (let i = 0; i < 10000; i++) - tx.getInputHashes(null, 'hex'); - - end(10000); -} - -{ - const [tx] = tx5.getTX(); - const end = bench('output hashes'); - - for (let i = 0; i < 10000; i++) - tx.getOutputHashes('hex'); - - end(10000); -} - -{ - const [tx] = tx5.getTX(); - const end = bench('all hashes'); - - for (let i = 0; i < 10000; i++) - tx.getHashes(null, 'hex'); - - end(10000); -} - -{ - const [tx, view] = tx3.getTX(); - const end = bench('verify'); - - for (let i = 0; i < 30000; i++) - tx.verify(view, Script.flags.VERIFY_P2SH); - - end(30000 * tx.inputs.length); -} - -{ - const [tx, view] = tx3.getTX(); - const {script} = view.getOutputFor(tx.inputs[0]); - const end = bench('sighash'); - - for (let i = 0; i < 1000000; i++) - tx.signatureHashV0(0, script, Script.hashType.ALL); - - end(1000000); -} - -{ - const [tx, view] = tx3.getTX(); - const end = bench('fee'); - - for (let i = 0; i < 10000; i++) - tx.getFee(view); - - end(10000); -} - -{ - const [tx, view] = tx10.getTX(); - const flags = Script.flags.VERIFY_P2SH | Script.flags.VERIFY_DERSIG; - const end = bench('verify multisig'); - - for (let i = 0; i < 30000; i++) - tx.verify(view, flags); - - end(30000 * tx.inputs.length); -} - -const mtx = new MTX(); - -for (let i = 0; i < 100; i++) { - mtx.addInput({ - prevout: { - hash: consensus.NULL_HASH, - index: 0 - }, - script: new Script() - .pushData(Buffer.allocUnsafe(9)) - .pushData(random.randomBytes(33)) - .compile() - }); - mtx.addOutput({ - address: Address.fromHash(random.randomBytes(20)), - value: 0 - }); -} - -const tx2 = mtx.toTX(); - -{ - const end = bench('input hashes'); - - for (let i = 0; i < 10000; i++) - tx2.getInputHashes(null, 'hex'); - - end(10000); -} - -{ - const end = bench('output hashes'); - - for (let i = 0; i < 10000; i++) - tx2.getOutputHashes('hex'); - - end(10000); -} - -{ - const end = bench('all hashes'); - - for (let i = 0; i < 10000; i++) - tx2.getHashes(null, 'hex'); - - end(10000); -} diff --git a/bench/walletdb.js b/bench/walletdb.js deleted file mode 100644 index a399919ba..000000000 --- a/bench/walletdb.js +++ /dev/null @@ -1,132 +0,0 @@ -'use strict'; - -const bench = require('./bench'); -const random = require('bcrypto/lib/random'); -const WalletDB = require('../lib/wallet/walletdb'); -const MTX = require('../lib/primitives/mtx'); -const Outpoint = require('../lib/primitives/outpoint'); - -function dummy() { - const hash = random.randomBytes(32).toString('hex'); - return new Outpoint(hash, 0); -} - -const walletdb = new WalletDB({ - name: 'wallet-test', - db: 'memory', - resolution: false, - verify: false -}); - -(async () => { - // Open and Create - await walletdb.open(); - - const wallet = await walletdb.create(); - const addrs = []; - let tx; - - // Accounts - { - const jobs = []; - for (let i = 0; i < 1000; i++) - jobs.push(wallet.createAccount({})); - - const end = bench('accounts'); - const result = await Promise.all(jobs); - end(1000); - - for (const addr of result) - addrs.push(addr.receive.getAddress()); - } - - // Keys - { - const jobs = []; - for (let i = 0; i < 1000; i++) { - for (let j = 0; j < 10; j++) - jobs.push(wallet.createReceive(i)); - } - - const end = bench('keys'); - const result = await Promise.all(jobs); - end(1000 * 10); - - for (const addr of result) - addrs.push(addr.getAddress()); - } - - // TX deposit - { - const jobs = []; - for (let i = 0; i < 10000; i++) { - const mtx = new MTX(); - mtx.addOutpoint(dummy()); - mtx.addOutput(addrs[(i + 0) % addrs.length], 50460); - mtx.addOutput(addrs[(i + 1) % addrs.length], 50460); - mtx.addOutput(addrs[(i + 2) % addrs.length], 50460); - mtx.addOutput(addrs[(i + 3) % addrs.length], 50460); - tx = mtx.toTX(); - - jobs.push(walletdb.addTX(tx)); - } - - const end = bench('deposit'); - await Promise.all(jobs); - end(10000); - } - - // TX redemption - { - const jobs = []; - for (let i = 0; i < 10000; i++) { - const mtx = new MTX(); - mtx.addTX(tx, 0); - mtx.addTX(tx, 1); - mtx.addTX(tx, 2); - mtx.addTX(tx, 3); - mtx.addOutput(addrs[(i + 0) % addrs.length], 50460); - mtx.addOutput(addrs[(i + 1) % addrs.length], 50460); - mtx.addOutput(addrs[(i + 2) % addrs.length], 50460); - mtx.addOutput(addrs[(i + 3) % addrs.length], 50460); - tx = mtx.toTX(); - - jobs.push(walletdb.addTX(tx)); - } - - const end = bench('redemption'); - await Promise.all(jobs); - end(10000); - } - - // Balance - { - const end = bench('balance'); - await wallet.getBalance(); - end(1); - } - - // Coins - { - const end = bench('coins'); - await wallet.getCoins(); - end(1); - } - - // Create - { - const end = bench('create'); - const options = { - rate: 10000, - outputs: [{ - value: 50460, - address: addrs[0] - }] - }; - await wallet.createTX(options); - end(1); - } -})().catch((err) => { - console.error(err); - process.exit(1); -}); diff --git a/docs/.gitignore b/docs/.gitignore new file mode 100644 index 000000000..e69de29bb