Skip to content

Commit

Permalink
Merge PR #555 from 'pinheadmz/expired1'
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Sep 28, 2023
2 parents 2ede2c2 + 7da5443 commit 7fe7ce4
Show file tree
Hide file tree
Showing 2 changed files with 444 additions and 89 deletions.
203 changes: 114 additions & 89 deletions lib/covenants/namestate.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class NameState extends bio.Struct {
if (this.isClaimable(height, network))
return false;

// If we haven't been renewed in a year, start over.
// If we haven't been renewed in two years, start over.
if (height >= this.renewal + network.names.renewalWindow)
return true;

Expand Down Expand Up @@ -728,7 +728,8 @@ class NameState extends bio.Struct {
assert((height >>> 0) === height);
assert(network && network.names);

const spacing = network.pow.targetSpacing;
const {blocksPerDay} = network.pow;
const blocksPerHour = blocksPerDay / 24;

const {
treeInterval,
Expand All @@ -737,103 +738,127 @@ class NameState extends bio.Struct {
revealPeriod,
renewalWindow,
auctionMaturity,
transferLockup
transferLockup,
claimPeriod
} = network.names;

const openPeriod = treeInterval + 1;

const stats = {};

if (this.isOpening(height, network)) {
const start = this.height;
const end = this.height + openPeriod;
const blocks = end - height;
const hours = ((blocks * spacing) / 60 / 60);
let state = this.state(height, network);

stats.openPeriodStart = start;
stats.openPeriodEnd = end;

stats.blocksUntilBidding = blocks;
stats.hoursUntilBidding = Number(hours.toFixed(2));
}

if (this.isLocked(height, network)) {
const start = this.height;
const end = this.height + lockupPeriod;
const blocks = end - height;
const hours = ((blocks * spacing) / 60 / 60);

stats.lockupPeriodStart = start;
stats.lockupPeriodEnd = end;

stats.blocksUntilClosed = blocks;
stats.hoursUntilClosed = Number(hours.toFixed(2));
}

if (this.isBidding(height, network)) {
const start = this.height + openPeriod;
const end = start + biddingPeriod;
const blocks = end - height;
const hours = ((blocks * spacing) / 60 / 60);

stats.bidPeriodStart = start;
stats.bidPeriodEnd = end;

stats.blocksUntilReveal = blocks;
stats.hoursUntilReveal = Number(hours.toFixed(2));
}

if (this.isReveal(height, network)) {
const start = this.height + openPeriod + biddingPeriod;
const end = start + revealPeriod;
const blocks = end - height;
const hours = ((blocks * spacing) / 60 / 60);

stats.revealPeriodStart = start;
stats.revealPeriodEnd = end;

stats.blocksUntilClose = blocks;
stats.hoursUntilClose = Number(hours.toFixed(2));
}

if (this.isClosed(height, network)) {
const start = this.renewal;
const end = start + renewalWindow;
const blocks = end - height;
const days = ((blocks * spacing) / 60 / 60 / 24);

stats.renewalPeriodStart = start;
stats.renewalPeriodEnd = end;

stats.blocksUntilExpire = blocks;
stats.daysUntilExpire = Number(days.toFixed(2));
}

if (this.isRevoked(height, network)) {
const start = this.revoked;
const end = start + auctionMaturity;
const blocks = end - height;
const hours = ((blocks * spacing) / 60 / 60);

stats.revokePeriodStart = start;
stats.revokePeriodEnd = end;
// Special case for a state that is not a state:
// EXPIRED but not revoked.
const EXPIRED = -1;
if (this.isExpired(height, network)) {
if (this.owner.isNull())
return null;

stats.blocksUntilReopen = blocks;
stats.hoursUntilReopen = Number(hours.toFixed(2));
if (state !== states.REVOKED)
state = EXPIRED;
}

// 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));
switch (state) {
case states.OPENING: {
const start = this.height;
const end = this.height + openPeriod;
const blocks = end - height;
const hours = blocks / blocksPerHour;

stats.openPeriodStart = start;
stats.openPeriodEnd = end;

stats.blocksUntilBidding = blocks;
stats.hoursUntilBidding = Number(hours.toFixed(2));
break;
}
case states.LOCKED: {
const start = this.height;
const end = this.height + lockupPeriod;
const blocks = end - height;
const hours = blocks / blocksPerHour;

stats.lockupPeriodStart = start;
stats.lockupPeriodEnd = end;

stats.blocksUntilClosed = blocks;
stats.hoursUntilClosed = Number(hours.toFixed(2));
break;
}
case states.BIDDING: {
const start = this.height + openPeriod;
const end = start + biddingPeriod;
const blocks = end - height;
const hours = blocks / blocksPerHour;

stats.bidPeriodStart = start;
stats.bidPeriodEnd = end;

stats.blocksUntilReveal = blocks;
stats.hoursUntilReveal = Number(hours.toFixed(2));
break;
}
case states.REVEAL: {
const start = this.height + openPeriod + biddingPeriod;
const end = start + revealPeriod;
const blocks = end - height;
const hours = blocks / blocksPerHour;

stats.revealPeriodStart = start;
stats.revealPeriodEnd = end;

stats.blocksUntilClose = blocks;
stats.hoursUntilClose = Number(hours.toFixed(2));
break;
}
case states.CLOSED: {
const start = this.renewal;
const normalEnd = start + renewalWindow;
const end = this.claimed ? Math.max(claimPeriod, normalEnd) : normalEnd;
const blocks = end - height;
const days = blocks / blocksPerDay;

stats.renewalPeriodStart = start;
stats.renewalPeriodEnd = end;

stats.blocksUntilExpire = blocks;
assert(stats.blocksUntilExpire >= 0);
stats.daysUntilExpire = Number(days.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 / blocksPerHour;

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

stats.blocksUntilValidFinalize = blocks;
stats.hoursUntilValidFinalize = Number(hours.toFixed(2));
}
break;
}
case states.REVOKED: {
const start = this.revoked;
const end = start + auctionMaturity;
const blocks = end - height;
const hours = blocks / blocksPerHour;

stats.revokePeriodStart = start;
stats.revokePeriodEnd = end;

stats.blocksUntilReopen = blocks;
stats.hoursUntilReopen = Number(hours.toFixed(2));
break;
}
case EXPIRED: {
const expired = this.renewal + renewalWindow;
stats.blocksSinceExpired = height - expired;
break;
}
}

return stats;
Expand Down
Loading

0 comments on commit 7fe7ce4

Please sign in to comment.