Skip to content

Commit

Permalink
Merge pull request #514 from pinheadmz/free1
Browse files Browse the repository at this point in the history
Mining: set minimum block weight to 5000, parse as configuration option
  • Loading branch information
chjj committed Nov 22, 2020
2 parents c85d9b4 + 05a9b02 commit 25173cf
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## unreleased

### Node changes

- `FullNode` now parses option `--min-weight=<number>` (`min-weight: <number>` in
hsd.conf or `minWeight: <number>` in JavaScript object instantiation).
When assembling a block template, if there are not enough fee-paying transactions available,
the miner will add transactions up to the minimum weight that would normally be
ignored for being "free" (paying a fee below policy limit). The default value is
raised from `0` to `5000` (a 1-in, 2-out BID transaction has a weight of about `889`).

### Wallet changes

- Fixes a bug that ignored the effect of sending or receiving a FINALIZE on a
Expand Down
1 change: 1 addition & 0 deletions lib/node/fullnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class FullNode extends Node {
address: this.config.array('coinbase-address'),
coinbaseFlags: this.config.str('coinbase-flags'),
preverify: this.config.bool('preverify'),
minWeight: this.config.uint('min-weight'),
maxWeight: this.config.uint('max-weight'),
reservedWeight: this.config.uint('reserved-weight'),
reservedSigops: this.config.uint('reserved-sigops')
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ exports.MEMPOOL_MAX_ORPHANS = 100;
* @default
*/

exports.MIN_BLOCK_WEIGHT = 0;
exports.MIN_BLOCK_WEIGHT = 5000;

/**
* Maximum block weight to be mined.
Expand Down
173 changes: 173 additions & 0 deletions test/miner-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/* eslint-env mocha */

'use strict';

const assert = require('bsert');

const WorkerPool = require('../lib/workers/workerpool');
const Chain = require('../lib/blockchain/chain');
const Mempool = require('../lib/mempool/mempool');
const Miner = require('../lib/mining/miner');
const Address = require('../lib/primitives/address');
const MTX = require('../lib/primitives/mtx');
const MemWallet = require('./util/memwallet');
const {BufferSet} = require('buffer-map');

const workers = new WorkerPool({
enabled: true
});

const chain = new Chain({
network: 'regtest',
memory: true,
workers
});

const mempool = new Mempool({
chain,
memory: true,
workers
});

const miner = new Miner({
chain,
mempool,
workers
});

const wallet = new MemWallet({
network: 'regtest'
});

// Dummy address to receive
const addr = new Address({
version: 0,
hash: Buffer.alloc(20, 0x88)
});

chain.on('connect', async (entry, block, view) => {
await mempool._addBlock(entry, block.txs, view);
wallet.addBlock(entry, block.txs);
});

describe('Miner', function() {
before(async () => {
await workers.open();
await chain.open();
await mempool.open();
await miner.open();
});

after(async () => {
await miner.close();
await mempool.close();
await workers.close();
await chain.close();
});

let walletAddr;
const txids = new BufferSet();

it('should generate 20 blocks to wallet address', async () => {
walletAddr = wallet.createReceive().getAddress();

for (let i = 1; i <= 20; i++) {
assert.bufferEqual(chain.tip.hash, mempool.tip);
const block = await miner.mineBlock(chain.tip, walletAddr);
await chain.add(block);
assert.bufferEqual(chain.tip.hash, mempool.tip);
assert.strictEqual(chain.tip.height, i);
}
});

it('should mine block with 10 ok-fee transactions', async () => {
const value = 1 * 1e6;
const fee = 1000;

for (let i = 0; i < 10; i++) {
const change = wallet.createChange().getAddress();
const coin = wallet.getCoins()[i];
const mtx = new MTX();
mtx.addCoin(coin);
mtx.addOutput(addr, value);
mtx.addOutput(change, coin.value - value - fee);
wallet.sign(mtx);
const tx = mtx.toTX();
wallet.addTX(tx);
txids.add(tx.hash());

await mempool.addTX(tx, -1);
}

assert.strictEqual(mempool.map.size, 10);

const block = await miner.mineBlock(chain.tip, addr);
await chain.add(block);

// All 10 TXs are in the block, cleared from the mempool
assert.strictEqual(mempool.map.size, 0);
assert.strictEqual(block.txs.length, 11);
for (let i = 1; i < block.txs.length; i++) {
assert(txids.has(block.txs[i].hash()));
}
});

it('should not include free transactions in block', async () => {
// Clear
txids.clear();
assert.strictEqual(txids.size, 0);

// Miner does not have any room for free TXs
miner.options.minWeight = 0;

const addr = new Address({
version: 0,
hash: Buffer.alloc(20, 0x88)
});

const value = 1 * 1e6;
const fee = 0;

for (let i = 0; i < 10; i++) {
const change = wallet.createChange().getAddress();
const coin = wallet.getCoins()[i];
const mtx = new MTX();
mtx.addCoin(coin);
mtx.addOutput(addr, value);
mtx.addOutput(change, coin.value - value - fee);
wallet.sign(mtx);
const tx = mtx.toTX();
wallet.addTX(tx);
txids.add(tx.hash());

await mempool.addTX(tx, -1);
}

assert.strictEqual(mempool.map.size, 10);

const block = await miner.mineBlock(chain.tip, addr);
await chain.add(block);

// All 10 TXs are still in mempool, nothing in block except coinbase
assert.strictEqual(mempool.map.size, 10);
assert.strictEqual(block.txs.length, 1);
});

it('should include free transactions in block with minWeight', async () => {
// Now the miner has allocated space for free TXs
miner.options.minWeight = 10000;

// Transactions are still in mempool from last test
assert.strictEqual(mempool.map.size, 10);

const block = await miner.mineBlock(chain.tip, addr);
await chain.add(block);

// All 10 TXs are in the block, cleared from the mempool
assert.strictEqual(mempool.map.size, 0);
assert.strictEqual(block.txs.length, 11);
for (let i = 1; i < block.txs.length; i++) {
assert(txids.has(block.txs[i].hash()));
}
});
});

0 comments on commit 25173cf

Please sign in to comment.