Beispiel #1
0
# )


# TODO: make this a test, mock 'cmkd' and tie a test block height to a
# timestamp, ensure only unit testing a within_window method
#
# also, create the `within_window` or similar method & use that.
#
bh = 131112
bh_epoch = cmkd.block_height_to_epoch(bh)

fudge = 72000
window_start = 1483689082 - fudge
window_end = 1483753726 + fudge

print("Window start: %s" % misc.epoch2str(window_start))
print("Window end: %s" % misc.epoch2str(window_end))
print("\nbh_epoch: %s" % misc.epoch2str(bh_epoch))


if (bh_epoch < window_start or bh_epoch > window_end):
    print("outside of window!")
else:
    print("Within window, we're good!")

# pdb.set_trace()
# cmkd.get_object_list()
# ==============================================================================
# pdb.set_trace()
1
Beispiel #2
0
def create_superblock(proposals, event_block_height, budget_max, sb_epoch_time):
    from models import Superblock, GovernanceObject, Proposal
    from constants import SUPERBLOCK_FUDGE_WINDOW

    # don't create an empty superblock
    if (len(proposals) == 0):
        printdbg("No proposals, cannot create an empty superblock.")
        return None

    budget_allocated = Decimal(0)
    fudge = SUPERBLOCK_FUDGE_WINDOW  # fudge-factor to allow for slighly incorrect estimates

    payments = []
    for proposal in proposals:
        fmt_string = "name: %s, rank: %4d, hash: %s, amount: %s <= %s"

        # skip proposals that are too expensive...
        if (budget_allocated + proposal.payment_amount) > budget_max:
            printdbg(
                fmt_string % (
                    proposal.name,
                    proposal.rank,
                    proposal.object_hash,
                    proposal.payment_amount,
                    "skipped (blows the budget)",
                )
            )
            continue

        # skip proposals if the SB isn't within the Proposal time window...
        window_start = proposal.start_epoch - fudge
        window_end = proposal.end_epoch + fudge

        printdbg("\twindow_start: %s" % epoch2str(window_start))
        printdbg("\twindow_end: %s" % epoch2str(window_end))
        printdbg("\tsb_epoch_time: %s" % epoch2str(sb_epoch_time))

        if (sb_epoch_time < window_start or sb_epoch_time > window_end):
            printdbg(
                fmt_string % (
                    proposal.name,
                    proposal.rank,
                    proposal.object_hash,
                    proposal.payment_amount,
                    "skipped (SB time is outside of Proposal window)",
                )
            )
            continue

        printdbg(
            fmt_string % (
                proposal.name,
                proposal.rank,
                proposal.object_hash,
                proposal.payment_amount,
                "adding",
            )
        )

        # else add proposal and keep track of total budget allocation
        budget_allocated += proposal.payment_amount

        payment = {'address': proposal.payment_address,
                   'amount': "{0:.8f}".format(proposal.payment_amount),
                   'proposal': "{}".format(proposal.object_hash)}
        payments.append(payment)

    # don't create an empty superblock
    if not payments:
        printdbg("No proposals made the cut!")
        return None

    # 'payments' now contains all the proposals for inclusion in the
    # Superblock, but needs to be sorted by proposal hash descending
    payments.sort(key=lambda k: k['proposal'], reverse=True)

    sb = Superblock(
        event_block_height=event_block_height,
        payment_addresses='|'.join([pd['address'] for pd in payments]),
        payment_amounts='|'.join([pd['amount'] for pd in payments]),
        proposal_hashes='|'.join([pd['proposal'] for pd in payments]),
    )
    printdbg("generated superblock: %s" % sb.__dict__)

    return sb
Beispiel #3
0
def create_superblock(proposals, event_block_height, budget_max, sb_epoch_time, maxgovobjdatasize):
    from models import Superblock, GovernanceObject, Proposal
    from constants import SUPERBLOCK_FUDGE_WINDOW

    # don't create an empty superblock
    if (len(proposals) == 0):
        printdbg("No proposals, cannot create an empty superblock.")
        return None

    budget_allocated = Decimal(0)
    fudge = SUPERBLOCK_FUDGE_WINDOW  # fudge-factor to allow for slightly incorrect estimates

    payments = []

    for proposal in proposals:
        fmt_string = "name: %s, rank: %4d, hash: %s, amount: %s <= %s"

        # skip proposals that are too expensive...
        if (budget_allocated + proposal.payment_amount) > budget_max:
            printdbg(
                fmt_string % (
                    proposal.name,
                    proposal.rank,
                    proposal.object_hash,
                    proposal.payment_amount,
                    "skipped (blows the budget)",
                )
            )
            continue

        # skip proposals if the SB isn't within the Proposal time window...
        window_start = proposal.start_epoch - fudge
        window_end = proposal.end_epoch + fudge

        printdbg("\twindow_start: %s" % epoch2str(window_start))
        printdbg("\twindow_end: %s" % epoch2str(window_end))
        printdbg("\tsb_epoch_time: %s" % epoch2str(sb_epoch_time))

        if (sb_epoch_time < window_start or sb_epoch_time > window_end):
            printdbg(
                fmt_string % (
                    proposal.name,
                    proposal.rank,
                    proposal.object_hash,
                    proposal.payment_amount,
                    "skipped (SB time is outside of Proposal window)",
                )
            )
            continue

        printdbg(
            fmt_string % (
                proposal.name,
                proposal.rank,
                proposal.object_hash,
                proposal.payment_amount,
                "adding",
            )
        )

        payment = {'address': proposal.payment_address,
                   'amount': "{0:.8f}".format(proposal.payment_amount),
                   'proposal': "{}".format(proposal.object_hash)}

        # calculate current sb data size
        sb_temp = Superblock(
            event_block_height=event_block_height,
            payment_addresses='|'.join([pd['address'] for pd in payments]),
            payment_amounts='|'.join([pd['amount'] for pd in payments]),
            proposal_hashes='|'.join([pd['proposal'] for pd in payments])
        )
        data_size = len(sb_temp.comboded_serialise())

        if data_size > maxgovobjdatasize:
            printdbg("MAX_GOVERNANCE_OBJECT_DATA_SIZE limit reached!")
            break

        # else add proposal and keep track of total budget allocation
        budget_allocated += proposal.payment_amount
        payments.append(payment)

    # don't create an empty superblock
    icbet payments:
        printdbg("No proposals made the cut!")
        return None