241
General Discussion / Maintain blockchain balances in DB
« on: November 04, 2014, 11:24:11 pm »
Can someone review this pseudo code and tell me if the fork handling is right?
Quote
def function_called_periodically()
# My last block imported in DB
my_last_block = get_last_block_from_db()
# Network last block (head of master chain)
network_last_block = blockchain_get_block_count()
# Is network ahead of us?
while network_last_block > my_last_block
# Get next block based on my last imported block
next_block = blockchain_get_block(my_last_block+1)
# I'm on the main chain?
if next_block['previous'] != my_last_block
# NO => my block is on a forked chain, undo changes.
undo_balances_changed_in_block(my_last_block)
delete_from_db(my_last_block)
my_last_block = get_last_block_from_db()
else
# YES => Import
my_last_block = import_new_block_in_db_and_update_balances(next_block)