Skip to content

Commit

Permalink
Merge bdc044e into 4c901f4
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Jan 30, 2021
2 parents 4c901f4 + bdc044e commit 154520e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# HSD Release Notes & Changelog

## unreleased

### API changes

- The `stats` field included in `namestate.toJSON()` includes extra data if the name
is in a TRANSFER state.

## v2.3.0

### Node changes
Expand Down
17 changes: 16 additions & 1 deletion lib/covenants/namestate.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,8 @@ class NameState extends bio.Struct {
biddingPeriod,
revealPeriod,
renewalWindow,
auctionMaturity
auctionMaturity,
transferLockup
} = network.names;

const openPeriod = treeInterval + 1;
Expand Down Expand Up @@ -817,6 +818,20 @@ class NameState extends bio.Struct {
stats.hoursUntilReopen = Number(hours.toFixed(2));
}

// Add these details if name is in mid-transfer
if (this.transfer !== 0) {
const start = this.transfer;
const end = start + transferLockup;
const blocks = end - height;
const hours = ((blocks * spacing) / 60 / 60);

stats.transferLockupStart = start;
stats.transferLockupEnd = end;

stats.blocksUntilValidFinalize = blocks;
stats.hoursUntilValidFinalize = Number(hours.toFixed(2));
}

return stats;
}

Expand Down
90 changes: 90 additions & 0 deletions test/auction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ describe('Auction', function() {

let snapshot = null;

let transferBlock, transferLockupEnd, blocksUntilValidFinalize;

it('should open chain and miner', async () => {
await chain.open();
await miner.open();
Expand Down Expand Up @@ -372,6 +374,94 @@ describe('Auction', function() {
assert.bufferEqual(root, chain.db.txn.rootHash());
});

it('should not have transfer stats in JSON yet', async () => {
const ns = await chain.db.getNameStateByName(NAME1);
const {stats} = ns.getJSON(chain.height, network);
assert.ok(stats.renewalPeriodStart);
assert.ok(stats.renewalPeriodEnd);
assert.ok(stats.blocksUntilExpire);
assert.ok(stats.daysUntilExpire);
assert.ok(!stats.transferLockupStart);
assert.ok(!stats.transferLockupEnd);
assert.ok(!stats.blocksUntilValidFinalize);
assert.ok(!stats.hoursUntilValidFinalize);
});

it('should transfer a name', async () => {
const mtx = await winner.createTransfer(NAME1, runnerup.getReceive());

const job = await cpu.createJob();
job.addTX(mtx.toTX(), mtx.view);
job.refresh();
const block = await job.mineAsync();
const entry = await chain.add(block);
assert(entry);
transferBlock = entry.height;
});

it('should be in a transfer state', async () => {
const ns = await chain.db.getNameStateByName(NAME1);
assert.strictEqual(ns.transfer, transferBlock);
assert(ns.transfer !== 0);
});

it('should have transfer stats', async () => {
const ns = await chain.db.getNameStateByName(NAME1);
const {stats} = ns.getJSON(chain.height, network);
assert.ok(stats.renewalPeriodStart);
assert.ok(stats.renewalPeriodEnd);
assert.ok(stats.blocksUntilExpire);
assert.ok(stats.daysUntilExpire);
assert.ok(stats.transferLockupStart);
assert.ok(stats.transferLockupEnd);
assert.ok(stats.blocksUntilValidFinalize);
assert.ok(stats.hoursUntilValidFinalize);

transferLockupEnd = stats.transferLockupEnd;
blocksUntilValidFinalize = stats.blocksUntilValidFinalize;
});

it('should finalize at expected height', async () => {
const mtx = await winner.createFinalize(NAME1);

// Attempt to confirm the FINALIZE in a block.
// If it fails, mine an empty block instead.
// Repeat until the chain height completes the
// transfer lockup period and the FINALIZE is valid.
let count = 0;
let entry;
for (;;) {
try {
const job = await cpu.createJob();
job.addTX(mtx.toTX(), mtx.view);
job.refresh();
const block = await job.mineAsync();
entry = await chain.add(block);

// exit loop when FINALIZE is finally confirmed without error
assert.strictEqual(block.txs.length, 2);
count++;
break;
} catch(e) {
assert.strictEqual(e.reason, 'bad-finalize-maturity');

// Ok, fine - mine a block without the FINALIZE
const job = await cpu.createJob();
job.refresh();
const block = await job.mineAsync();
entry = await chain.add(block);

// just a coinbase
assert.strictEqual(block.txs.length, 1);

count++;
}
}

assert.strictEqual(count, blocksUntilValidFinalize);
assert.strictEqual(transferLockupEnd, entry.height);
});

it('should cleanup', async () => {
await miner.close();
await chain.close();
Expand Down

0 comments on commit 154520e

Please sign in to comment.