Ejemplo n.º 1
0
def get_response_as_html(response_json):
    validator = Validator.from_dict(response_json["validator"])
    hours = round(response_json["num_seconds_left"] / (TimeUnit.Hour * 1.0), 2)
    html = u"".join((
        u"<p>Current slots: {}</p>\n".format(response_json["slots"]),
        u"<p>Current epoch uptime: {}</p>\n".format(validator.uptime_as_pct),
        u"<p>If we lower the bid by adding key the slots will be: {}</p>\n".format(
            response_json.get("slots_after_lowering_bid", "N/A")),
        u"<p>If we increase the bid by removing a key the slots will be: {}</p>\n".format(
            response_json.get("slots_after_increasing_bid", "N/A")),
        u"<p>Epoch progress: {} blocks left ({} hours)</p>\n".format(response_json["num_blocks_left"], hours),
        u"<table><tr><td>Slot(s)</td><td>Validator Name</td><td>Bid per slot</td><td>Uptime</td></tr>\n"
    ))

    slot = 1
    for validator_json in response_json["validators"]:
        validator = Validator.from_dict(validator_json)
        slot_range = SlotRange(slot, slot + validator.num_slots - 1)
        class_name = 'other-validator'
        if validator.address == config.VALIDATOR_ADDR:
            class_name = 'robo-validator'
        html += u"<tr class='{}'><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>\n".format(
            class_name, str(slot_range), validator.name, validator.bid, validator.uptime_as_pct)
        slot = slot_range.end + 1

    html += u"</table>\n"
    return html
Ejemplo n.º 2
0
def get_validator_add_key(validator):
    missing_key = get_missing_key(validator)
    if not missing_key:
        return None, None
    bls_keys = [missing_key] + validator.bls_keys
    num_slots = len(bls_keys)
    bid = validator.bid * validator.num_slots / (1.0 * num_slots)
    return Validator(validator.address, validator.name, bid, bls_keys,
                     num_slots, validator.uptime), missing_key
def get_response_as_text(response_json):
    text = ""
    slot = 1
    for validator_json in response_json["validators"]:
        validator = Validator.from_dict(validator_json)
        slot_range = SlotRange(slot, slot + validator.num_slots - 1)
        text += u"{}: {} ({})\n".format(str(slot_range), validator.name,
                                        validator.bid)
        slot = slot_range.end + 1

    validator = Validator.from_dict(response_json["validator"])
    hours = round(response_json["num_seconds_left"] / (TimeUnit.Hour * 1.0), 2)
    text += u"".join(
        (u"Name: {}\n".format(validator.name),
         u"Current slots: {}\n".format(response_json["slots"]),
         u"Current epoch uptime: {}\n".format(validator.uptime_as_pct),
         u"Current bid: {}\n".format(validator.bid),
         u"BLS keys: {}\n".format(u", ".join(validator.bls_keys)),
         u"If we lower the bid by adding key the slots will be: {}\n".format(
             response_json.get("slots_after_lowering_bid", "N/A")),
         u"If we increase the bid by removing a key the slots will be: {}\n".
         format(response_json.get("slots_after_increasing_bid", "N/A")),
         u"Epoch progress: {} blocks left ({} hours)\n".format(
             response_json["num_blocks_left"],
             hours), u"Polling interval seconds: {}\n".format(
                 response_json["interval_seconds"]),
         u"Debug data: {}\n".format(response_json["debug"])))

    removed_bls_key = response_json.get("removed_bls_key")
    if removed_bls_key:
        text += u"Increased the bid by removing key {}\n".format(
            removed_bls_key)

    added_bls_key = response_json.get("added_bls_key")
    if added_bls_key:
        text += u"Decreased the bid by adding key {}\n".format(added_bls_key)

    new_slots = response_json.get("new_slots")
    if new_slots:
        text += u"New slots after taking action: {}\n".format(
            response_json["new_slots"])

    return text
Ejemplo n.º 4
0
    def testIsBetween(self):
        self.assertFalse(Validator.isBetween(None, 2, 10))
        self.assertFalse(Validator.isBetween('', -1, 2))
        self.assertFalse(Validator.isBetween(0, 1, 1))
        self.assertFalse(Validator.isBetween(1, 1, 0))

        self.assertTrue(Validator.isBetween(1, 0, 1))
        self.assertTrue(Validator.isBetween(1, 1, 2))
        self.assertTrue(Validator.isBetween(1, 0, 3))
Ejemplo n.º 5
0
    def testIsInt(self):
        self.assertTrue(Validator.isInt(10))
        self.assertTrue(Validator.isInt(0))
        self.assertTrue(Validator.isInt(-3))

        self.assertFalse(Validator.isInt(10.2))
        self.assertFalse(Validator.isInt(None))
        self.assertFalse(Validator.isInt(''))
        self.assertFalse(Validator.isInt('Aron'))
Ejemplo n.º 6
0
def get_validator_remove_key(validator):
    if len(validator.bls_keys) == 1:
        return None, None
    removed_key = [
        key for key in config.BLS_KEYS if key in validator.bls_keys
    ][-1]
    bls_keys = [key for key in validator.bls_keys if key != removed_key]
    num_slots = len(bls_keys)
    bid = validator.bid * validator.num_slots / (1.0 * num_slots)
    return Validator(validator.address, validator.name, bid, bls_keys,
                     num_slots, validator.uptime), removed_key
Ejemplo n.º 7
0
def extract_validator(info_json):
    validator_json = info_json['validator']
    uptime = get_uptime(info_json)
    delegations = validator_json['delegations']
    name = validator_json['name']
    address = validator_json['address']
    bls_keys = validator_json['bls-public-keys']
    slots = len(bls_keys)
    staked_amount = sum([delegation['amount']
                         for delegation in delegations]) * OneUnit.Wei
    bid = int(round(staked_amount / (len(bls_keys) * 1.0)))
    return Validator(address, name, bid, bls_keys, slots, uptime)
Ejemplo n.º 8
0
    def testIsvalidEmail(self):
        self.assertFalse(Validator.isValidEmail('abc'))
        self.assertFalse(Validator.isValidEmail('abc.gmail'))
        self.assertFalse(Validator.isValidEmail('abc@gmail'))
        self.assertFalse(Validator.isValidEmail('abc@@mail.com'))

        self.assertTrue(Validator.isValidEmail('*****@*****.**'))
        self.assertTrue(Validator.isValidEmail('*****@*****.**'))

        data = {'username': '******', 'email': '*****@*****.**'}
        self.assertTrue(Validator.isValidPlayer(data))
Ejemplo n.º 9
0
 def testIsPrintable(self):
     self.assertFalse(Validator.isPrintable('\n'))
     self.assertFalse(Validator.isPrintable('abc\n'))
     self.assertTrue(Validator.isPrintable('abc'))
Ejemplo n.º 10
0
 def testMinLength(self):
     self.assertFalse(Validator.minLength(None, 2))
     self.assertFalse(Validator.minLength(None, -2))
     self.assertTrue(Validator.minLength('ab', 2))
     self.assertFalse(Validator.minLength('', 2))
     self.assertTrue(Validator.minLength('abcdefgh', 2))
Ejemplo n.º 11
0
 def testIsGreaterThanOrEqual(self):
     self.assertFalse(Validator.isGreaterThanOrEqual(None, 10))
     self.assertFalse(Validator.isGreaterThanOrEqual('abc', 10))
     self.assertFalse(Validator.isGreaterThanOrEqual(9, 10))
     self.assertTrue(Validator.isGreaterThanOrEqual(10, 10))
     self.assertTrue(Validator.isGreaterThanOrEqual(11, 10))
Ejemplo n.º 12
0
 def testIsLessThanOrEqual(self):
     self.assertFalse(Validator.isLessThanOrEqual(None, 10))
     self.assertFalse(Validator.isLessThanOrEqual('abc', 10))
     self.assertFalse(Validator.isLessThanOrEqual(11, 10))
     self.assertTrue(Validator.isLessThanOrEqual(10, 10))
     self.assertTrue(Validator.isLessThanOrEqual(8, 10))
Ejemplo n.º 13
0
 def testIsLengthBetween(self):
     self.assertFalse(Validator.isLengthBetween(None, 2, 10))
     self.assertFalse(Validator.isLengthBetween('abcd', 5, 10))
     self.assertFalse(Validator.isLengthBetween('abcdefghijk', 5, 10))
     self.assertTrue(Validator.isLengthBetween('abcdef', 5, 10))
     self.assertTrue(Validator.isLengthBetween('abcdefghij', 5, 10))
Ejemplo n.º 14
0
 def testMaxLength(self):
     self.assertFalse(Validator.maxLength(None, 10))
     self.assertFalse(Validator.maxLength(None, -2))
     self.assertTrue(Validator.maxLength('abcd', 4))
     self.assertTrue(Validator.maxLength('', 2))
     self.assertFalse(Validator.maxLength('abcdefghijk', 10))