Skip to content

Commit

Permalink
work :p
Browse files Browse the repository at this point in the history
git-svn-id: svn://forre.st/undns@1218 470744a7-cac9-478e-843e-5ec1b25c69e8
  • Loading branch information
forrest committed Apr 6, 2011
1 parent eaa8f8a commit 28ca319
Showing 1 changed file with 141 additions and 101 deletions.
242 changes: 141 additions & 101 deletions server.py
Expand Up @@ -136,9 +136,13 @@ def __init__(self, data):
(self.previous_hash, self.pos, self.timestamp, self.total_difficulty, self.message, self.difficulty), self.nonce = json.loads(data) (self.previous_hash, self.pos, self.timestamp, self.total_difficulty, self.message, self.difficulty), self.nonce = json.loads(data)
if isinstance(self.previous_hash, unicode): if isinstance(self.previous_hash, unicode):
self.previous_hash = str(self.previous_hash) self.previous_hash = str(self.previous_hash)
def is_valid(self):
if self.previous_hash is None:
a
else:
a


class BlockDictWrapper(object): class BlockDictWrapper(object):
# int -> Block : str -> str
def __init__(self, inner): def __init__(self, inner):
self._inner = inner self._inner = inner
def __len__(self): def __len__(self):
Expand Down Expand Up @@ -191,6 +195,11 @@ def __init__(self, *args, **kwargs):
#) #)
self.best_block = None self.best_block = None
self.best_block_callbacks = [] self.best_block_callbacks = []

try:
self.best_block = self.blocks[open(db_prefix + '.best').read()]
except:
traceback.print_exc()


def joinNetwork(self, *args, **kwargs): def joinNetwork(self, *args, **kwargs):
node.Node.joinNetwork(self, *args, **kwargs) node.Node.joinNetwork(self, *args, **kwargs)
Expand Down Expand Up @@ -286,17 +295,17 @@ def try_to_do_something(self):
if previous_block is None: if previous_block is None:
previous_hash = None previous_hash = None
pos = 0 pos = 0
timestamp = int(time.time()) timestamp = int(time.time()*1000)
message = {self.port: 1} # XXX insert self.requests message = {self.port: 1} # XXX insert self.requests
difficulty = GENESIS_DIFFICULTY difficulty = GENESIS_DIFFICULTY
total_difficulty = 0 + difficulty total_difficulty = 0 + difficulty
else: else:
previous_hash = previous_block.hash_id previous_hash = previous_block.hash_id
pos = previous_block.pos + 1 pos = previous_block.pos + 1
timestamp = max(previous_block.timestamp, int(time.time())) timestamp = max(previous_block.timestamp, int(time.time()*1000))
message = dict((int(k), int(v)) for k, v in previous_block.message.iteritems()) # XXX insert self.requests message = dict((int(k), int(v)) for k, v in previous_block.message.iteritems()) # XXX insert self.requests
message[self.port] = message.get(self.port, 0) + 1 message[self.port] = message.get(self.port, 0) + 1

if pos < 25: if pos < 25:
difficulty = GENESIS_DIFFICULTY difficulty = GENESIS_DIFFICULTY
else: else:
Expand All @@ -311,7 +320,7 @@ def try_to_do_something(self):
dt = previous_block.timestamp - cur.timestamp dt = previous_block.timestamp - cur.timestamp
if dt == 0: if dt == 0:
dt = 1 dt = 1
difficulty = difficulty_sum * 10 // dt difficulty = difficulty_sum * 10000 // dt


total_difficulty = previous_block.total_difficulty + difficulty total_difficulty = previous_block.total_difficulty + difficulty


Expand All @@ -333,108 +342,112 @@ def abort(d=d):
self.say("generated", result.pos, result.message, result.difficulty, self.received_block(result)) self.say("generated", result.pos, result.message, result.difficulty, self.received_block(result))


def received_block(self, block, from_node=None, depth=0): def received_block(self, block, from_node=None, depth=0):
try: try:
print block.data print block.data
if block.hash_id in self.verified: if block.hash_id in self.verified:
return "already verified" return "already verified"

if block.hash_difficulty % block.difficulty != 0:
return "invalid nonce"

if block.timestamp > time.time() + 60 * 10:
return "block is from the future!"

# this needs to change ... it should compare against all blocks, not the best verified block
#if self.best_block is not None and block.pos < self.best_block.pos - 16:
# return "you lose"

if block.pos == 0:
if block.previous_hash is not None:
return "genesis block can't refer to previous..."

if block.difficulty != GENESIS_DIFFICULTY:
return "genesis difficulty"

if block.total_difficulty != block.difficulty:
return "genesis total_difficulty"

self.blocks[block.hash_id] = block
self.referrers.setdefault(block.previous_hash, set()).add(block.hash_id)
self.say("g_received", block.pos, block.message)
self.verified_block(block, from_node, depth=depth + 1)
elif block.previous_hash not in self.verified:
self.blocks[block.hash_id] = block
self.referrers.setdefault(block.previous_hash, set()).add(block.hash_id)
self.say("h_received", block.pos, block.message)

b = block
while True:
print 1
assert b.previous_hash is not None, b.__dict__
if b.previous_hash not in self.blocks:
print .5
if from_node is None:
if not self.peers:
print 2
return
from_node = random.choice(self.peers)
def got_block(datas):
print datas
self.requests_in_progress.remove(b.previous_hash)
for data in reversed(datas):
block2 = Block(data)
try:
self.received_block(block2, from_node)
except:
traceback.print_exc()
def got_error(fail):
print fail
self.requests_in_progress.remove(b.previous_hash)
if b.previous_hash in self.requests_in_progress:
print 3
print "not requesting!", block.pos
return "waiting on other request ..."
print 4
print "requesting", b.previous_hash
self.requests_in_progress.add(b.previous_hash)
from_node.get_blocks(b.previous_hash, 20).addCallbacks(got_block, got_error)
return "waiting on block.."
b = self.blocks[b.previous_hash]
else:
previous_block = self.blocks[block.previous_hash]


if block.pos != previous_block.pos + 1: if block.hash_difficulty % block.difficulty != 0:
return "pos needs to advance by 1" return "invalid nonce"


if block.timestamp < previous_block.timestamp: if block.timestamp > (time.time() + 60 * 10) * 1000:
return "timestamp must not decrease" return "block is from the future!"


if block.total_difficulty != previous_block.total_difficulty + block.difficulty: # this needs to change ... it should compare against all blocks, not the best verified block
return "genesis total_difficulty" #if self.best_block is not None and block.pos < self.best_block.pos - 16:
# return "you lose"


if block.pos < 25: if block.previous_hash is None:
difficulty = GENESIS_DIFFICULTY if block.pos != 0:
return "not first"

if block.difficulty != GENESIS_DIFFICULTY:
return "genesis difficulty"

if block.total_difficulty != block.difficulty:
return "genesis total_difficulty"

self.blocks[block.hash_id] = block
self.referrers.setdefault(block.previous_hash, set()).add(block.hash_id)
self.say("g_received", block.pos, block.message)
self.verified_block(block, from_node, depth=depth + 1)
return
if block.previous_hash not in self.verified:
self.blocks[block.hash_id] = block
self.referrers.setdefault(block.previous_hash, set()).add(block.hash_id)
self.say("h_received", block.pos, block.message)

b = block
while True:
print 1
assert b.previous_hash is not None, b.__dict__
if b.previous_hash not in self.blocks:
print .5
if from_node is None:
if not self.peers:
print 2
return
from_node = random.choice(self.peers)
def got_block(datas):
print datas
self.requests_in_progress.remove(b.previous_hash)
for data in reversed(datas):
block2 = Block(data)
try:
self.received_block(block2, from_node)
except:
traceback.print_exc()
def got_error(fail):
print fail
self.requests_in_progress.remove(b.previous_hash)
if b.previous_hash in self.requests_in_progress:
print 3
print "not requesting!", block.pos
return "waiting on other request ..."
print 4
print "requesting", b.previous_hash
self.requests_in_progress.add(b.previous_hash)
from_node.get_blocks(b.previous_hash, 100).addCallbacks(got_block, got_error)
return "waiting on block.."
b = self.blocks[b.previous_hash]
return
else: else:
difficulty_sum = 0 previous_block = self.blocks[block.previous_hash]
cur = previous_block
for i in xrange(LOOKBEHIND):
if cur.previous_hash is None:
break
difficulty_sum += cur.difficulty
cur = self.blocks[cur.previous_hash]


# want each block to take 10 seconds if block.pos != previous_block.pos + 1:
difficulty = difficulty_sum * 10 // (previous_block.timestamp - cur.timestamp) return "pos needs to advance by 1"


if block.difficulty != difficulty: if block.timestamp < previous_block.timestamp:
return "difficulty must follow pattern (%i != %i)" % (block.difficulty, difficulty) return "timestamp must not decrease"


self.blocks[block.hash_id] = block if block.total_difficulty != previous_block.total_difficulty + block.difficulty:
self.referrers.setdefault(block.previous_hash, set()).add(block.hash_id) return "genesis total_difficulty"
self.say("i_received", block.pos, block.difficulty, block.timestamp, block.message)
self.verified_block(block, depth=depth + 1) if block.pos < 25:
except: difficulty = GENESIS_DIFFICULTY
traceback.print_exc() else:
cur = self.blocks[block.previous_hash]
for i in xrange(LOOKBEHIND):
if cur.previous_hash is None:
break
cur = self.blocks[cur.previous_hash]

# want each block to take 10 seconds
difficulty_sum = previous_block.total_difficulty - cur.total_difficulty
dt = previous_block.timestamp - cur.timestamp
if dt == 0:
dt = 1
difficulty = difficulty_sum * 10000 // dt

if block.difficulty != difficulty:
return "difficulty must follow pattern (%i != %i)" % (block.difficulty, difficulty)

self.blocks[block.hash_id] = block
self.referrers.setdefault(block.previous_hash, set()).add(block.hash_id)
self.say("i_received", block.pos, block.difficulty, block.timestamp, block.message)
self.verified_block(block, depth=depth + 1)
except:
traceback.print_exc()


def verified_block(self, block, from_node=None, depth=0): def verified_block(self, block, from_node=None, depth=0):
assert block.previous_hash is None or block.previous_hash in self.verified assert block.previous_hash is None or block.previous_hash in self.verified
Expand All @@ -461,11 +474,38 @@ def verified_block(self, block, from_node=None, depth=0):
self.say("new best", block.pos, block.message) self.say("new best", block.pos, block.message)
self.best_block = block self.best_block = block


open(db_prefix + '.best', 'w').write(self.best_block.hash_id)
self.blocks._inner._inner.sync()

cbs = self.best_block_callbacks cbs = self.best_block_callbacks
self.best_block_callbacks = [] self.best_block_callbacks = []
for cb in cbs: for cb in cbs:
cb() cb()


def try_to_verify(self, block):
assert block.hash_id in self.blocks

if block.previous_hash is None:
if block.pos != 0:
return "only first block doesn't need a reference"

if block.difficulty != GENESIS_DIFFICULTY:
return "genesis difficulty"

if block.total_difficulty != block.difficulty:
return "genesis total_difficulty"

self.blocks[block.hash_id] = block
self.referrers.setdefault(block.previous_hash, set()).add(block.hash_id)
self.say("g_received", block.pos, block.message)
self.verified_block(block, from_node, depth=depth + 1)
a
else:
if block.pos <= 0:
return "invalid position"

a

def __del__(self): def __del__(self):
print "DELETED" print "DELETED"


Expand Down

0 comments on commit 28ca319

Please sign in to comment.