add checks to see if the name is in the renewal grace period

This commit is contained in:
Jude Nelson
2017-09-26 16:29:20 -04:00
parent d880b43031
commit 6063d4b5e5

View File

@@ -33,6 +33,7 @@ from . import *
from ..config import *
from ..operations import *
from ..hashing import *
from ..scripts import get_namespace_from_name
import virtualchain
from db import *
@@ -614,7 +615,7 @@ class BlockstackDB( virtualchain.StateEngine ):
"""
cur = self.db.cursor()
return namedb_get_namespace_ready( cur, namespace_id )
return namedb_get_namespace_ready( cur, namespace_id, self.lastblock )
@autofill( "opcode" )
@@ -1112,6 +1113,60 @@ class BlockstackDB( virtualchain.StateEngine ):
return namedb_get_name( cur, name, block_number ) is None
@classmethod
def get_name_deadlines( self, name_rec, namespace_rec, block_number ):
"""
Get the expiry and renewal deadlines for a (registered) name.
NOTE: expire block here is NOT the block at which the owner loses the name, but the block at which lookups fail.
The name owner has until renewal_deadline to renew the name.
Return {'expire_block': ..., 'renewal_deadline': ...} on success
Return None if the namespace isn't ready yet
"""
if namespace_rec['op'] != NAMESPACE_READY:
# name cannot be in grace period, since the namespace is not ready
return None
namespace_id = namespace_rec['namespace_id']
namespace_lifetime_multiplier = get_epoch_namespace_lifetime_multiplier( block_number, namespace_id )
namespace_lifetime_grace_period = get_epoch_namespace_lifetime_grace_period( block_number, namespace_id )
expire_block = max(namespace_rec['ready_block'], name_rec['last_renewed']) + (namespace_rec['lifetime'] * namespace_lifetime_multiplier)
renewal_deadline = expire_block + namespace_lifetime_grace_period
return {'expire_block': expire_block, 'renewal_deadline': renewal_deadline}
def is_name_in_grace_period(self, name, block_number):
"""
Given a name and block number, determine if it is in the renewal grace period at that block.
* names in revealed but not ready namespaces are never expired, unless the namespace itself is expired;
* names in ready namespaces enter the grace period once max(ready_block, renew_block) + lifetime - grace_period blocks passes
Return True if so
Return False if not, or if the name does not exist.
"""
cur = self.db.cursor()
name_rec = namedb_get_name(cur, name, block_number, include_expired=False)
if name_rec is None:
# expired already or doesn't exist
return False
namespace_id = get_namespace_from_name(name)
namespace_rec = namedb_get_namespace(cur, namespace_id, block_number, include_history=False)
if namespace_rec is None:
return False
grace_info = BlockstackDB.get_name_deadlines(name_rec, namespace_rec, block_number)
if grace_info is None:
# namespace isn't ready yet
return False
return (block_number >= grace_info['expire_block'] and block_number < grace_info['renewal_deadline'])
def is_name_registered( self, name ):
"""
Given the fully-qualified name, is it registered, not revoked, and not expired