Skip to content

Instantly share code, notes, and snippets.

@pinheadmz
Created August 1, 2019 16:16
Show Gist options
  • Save pinheadmz/fc9b4d0fcbb4041ebd8e938f9f52b92d to your computer and use it in GitHub Desktop.
Save pinheadmz/fc9b4d0fcbb4041ebd8e938f9f52b92d to your computer and use it in GitHub Desktop.
/* eslint-env mocha */
/* eslint prefer-arrow-callback: "off" */
'use strict';
const assert = require('bsert');
const Network = require('../lib/protocol/network');
const FullNode = require('../lib/node/fullnode');
const Address = require('../lib/primitives/address');
const rules = require('../lib/covenants/rules');
const network = Network.get('regtest');
const node = new FullNode({
memory: true,
network: 'regtest',
plugins: [require('../lib/wallet/plugin')]
});
const {wdb} = node.require('walletdb');
const name = rules.grindName(10, 20, network);
let wallet, alice, bob, aliceMiner, bobMiner;
async function mineBlocks(n, addr) {
addr = addr ? addr : new Address().toString('regtest');
for (let i = 0; i < n; i++) {
const block = await node.miner.mineBlock(null, addr);
await node.chain.add(block);
}
}
describe('One wallet, two accounts, one name', function() {
before(async () => {
await node.open();
wallet = await wdb.create();
alice = await wallet.createAccount({name: 'alice'});
bob = await wallet.createAccount({name: 'bob'});
aliceMiner = await alice.receiveAddress();
bobMiner = await bob.receiveAddress();
});
after(async () => {
await node.close();
});
it('should fund both accounts', async () => {
await mineBlocks(10, aliceMiner);
await mineBlocks(10, bobMiner);
// Wallet rescan is an effective way to ensure that
// wallet and chain are synced before proceeding.
await wdb.rescan(0);
const aliceBal = await wallet.getBalance('alice');
const bobBal = await wallet.getBalance('bob');
assert(aliceBal.confirmed === 2000 * 10 * 1e6);
assert(bobBal.confirmed === 2000 * 10 * 1e6);
});
it('should open an auction and proceed to REVEAL phase', async () => {
await wallet.sendOpen(name, false, {account: 'alice'});
await mineBlocks(network.names.treeInterval + 2);
let ns = await node.chain.db.getNameStateByName(name);
assert(ns.isBidding(node.chain.height, network));
await wdb.rescan(0);
await wallet.sendBid(name, 100000, 200000, {account: 'alice'});
await wallet.sendBid(name, 50000, 200000, {account: 'bob'});
await mineBlocks(network.names.biddingPeriod);
ns = await node.chain.db.getNameStateByName(name);
assert(ns.isReveal(node.chain.height, network));
await wdb.rescan(0);
const walletBids = await wallet.getBidsByName(name);
assert.strictEqual(walletBids.length, 2);
for (const bid of walletBids)
assert(bid.own);
});
it('should send REVEAL from one account', async () => {
await wallet.sendReveal(name, {account: 'alice'});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment