def watchdog_check(kzcashd):
    printdbg('in watchdog_check')
    for wd in Watchdog.expired(kzcashd):
        printdbg(('\tFound expired watchdog [%s], voting to delete' % wd.object_hash))
        wd.vote(kzcashd, VoteSignals.delete, VoteOutcomes.yes)
    active_wd = Watchdog.active(kzcashd)
    active_count = active_wd.count()
    if (0 == active_count):
        printdbg('\tNo watchdogs exist... submitting new one.')
        wd = Watchdog(created_at=int(time.time()))
        wd.submit(kzcashd)
    else:
        wd_list = sorted(active_wd, key=(lambda wd: wd.object_hash))
        winner = wd_list.pop()
        printdbg(('\tFound winning watchdog [%s], voting VALID' % winner.object_hash))
        winner.vote(kzcashd, VoteSignals.valid, VoteOutcomes.yes)
        for wd in wd_list:
            printdbg(('\tFound losing watchdog [%s], voting DELETE' % wd.object_hash))
            wd.vote(kzcashd, VoteSignals.delete, VoteOutcomes.yes)
    printdbg('leaving watchdog_check')
def watchdog_check(kzcashd):
    printdbg('in watchdog_check')
    for wd in Watchdog.expired(kzcashd):
        printdbg(('\tFound expired watchdog [%s], voting to delete' % wd.object_hash))
        wd.vote(kzcashd, VoteSignals.delete, VoteOutcomes.yes)
    active_wd = Watchdog.active(kzcashd)
    active_count = active_wd.count()
    if (0 == active_count):
        printdbg('\tNo watchdogs exist... submitting new one.')
        wd = Watchdog(created_at=int(time.time()))
        wd.submit(kzcashd)
    else:
        wd_list = sorted(active_wd, key=(lambda wd: wd.object_hash))
        winner = wd_list.pop()
        printdbg(('\tFound winning watchdog [%s], voting VALID' % winner.object_hash))
        winner.vote(kzcashd, VoteSignals.valid, VoteOutcomes.yes)
        for wd in wd_list:
            printdbg(('\tFound losing watchdog [%s], voting DELETE' % wd.object_hash))
            wd.vote(kzcashd, VoteSignals.delete, VoteOutcomes.yes)
    printdbg('leaving watchdog_check')