Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary Regtest Guide #51

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions guides-markdown/regtest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Create a Local Regtest Blockchain

```post-author
Jonathan Gonzalez
```
```post-description
This example creates a local Regtest Chain.
```

## Regtest Chain Example
The Regtest Network is a local Blockchain in which individuals can almost
instantaneously generate blocks on demand. Including the option to generate private satoshis
with no real-world value. This is ideal for testing Bitcoin Applications.

This example generates two consecutive blocks, with transaction confirmations
in the Users WalletDB:


```javascript
const FullNode = require('bcoin/lib/node/fullnode');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets make the imports a little more generalized for different setups. In most of the guides we usually forst do const bcoin = require('bcoin') and then pull the modules from there (e.g. const FullNode = bcoin.fullnode; etc.) that way, people just need to change one line depending on where their version of bcoin is installed.

const consensus = require('bcoin/lib/protocol/consensus');

const node = new FullNode({
network: 'regtest',
db: 'memory',
prefix: '/home/.bcoin/regtest',
apiKey: 'bikeshed',
nodes: '127.0.0.1',
port: 48444,
env: true,
logFile: true,
logConsole: true,
logLevel: 'debug',
persistent: true,
workers: true,
listen: true,
plugins: [require('bcoin/lib/wallet/plugin')]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you pull the wallet plugin import into the same section as the others and assign it a variable.

});

process.on('unhandledRejection', (err, promise) => {
throw err;
});

(async function connection() {
await node.open();
await node.connect();
await node.pool.connect();
await node.pool.startSync();
consensus.COINBASE_MATURITY = 0;
})().then(async function miner() {
const wdb = node.require('walletdb');
await node.miner.addAddress(wdb.primary.getReceive());

const tip = node.chain.tip;
const job = await node.miner.createJob(tip);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the job variable used anywhere or can we just leave it as await node.miner.createJob(tip) without the variable assignment?

const entry = await node.chain.getEntry(node.chain.tip.hash);
const block = await node.miner.mineBlock(entry);

await node.chain.add(block);
await node.relay(node.chain.db.state);

const block2 = await node.miner.mineBlock();
const tx2 = block2.txs[1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tx2 also doesn't look like it's used anywhere.

await block2.refresh(true);
await node.chain.add(block2);

const nextEntry = await node.chain.getEntryByHeight(node.chain.height);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I ran the script I got an error here because chain doesn't have a getEntryByHeight method. chainDB does though so node.chain.db.getEntryByHeight should be fine.

Also, it doesn't look like nextEntry is used anywhere


const balance = wdb.primary.getBalance();
await balance;
});


```