def test_wait_certificate(self):
        addr = random_name(20)
        certs = generate_certs(WaitTimer.fixed_duration_blocks)
        txn_ids, block_hash = generate_txn_ids(10)

        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        self.assertIsNotNone(wait_timer)
        while not wait_timer.is_expired(time.time()):
            time.sleep(1)

        wait_cert = WaitCertificate.create_wait_certificate(
            wait_timer,
            block_hash)
        self.assertIsNotNone(wait_cert)

        ewc = wait_cert.enclave_wait_certificate
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)
        str(wait_cert)

        swc = WaitCertificate.deserialize_wait_certificate(
            wait_cert.serialized_cert,
            wait_cert.signature
        )
        self.assertEquals(wait_cert.previous_certificate_id,
                          swc.previous_certificate_id)
        self.assertEquals(wait_cert.local_mean, swc.local_mean)
        self.assertEquals(wait_cert.request_time, swc.request_time)
        self.assertEquals(wait_cert.duration, swc.duration)
        self.assertEquals(wait_cert.signature, swc.signature)
        self.assertEquals(wait_cert.identifier, swc.identifier)
        self.assertEquals(wait_cert.validator_address, swc.validator_address)
        self.assertEquals(wait_cert.block_hash, swc.block_hash)

        swc.is_valid_wait_certificate(addr, certs, txn_ids)
        dwc = wait_cert.dump()
        swc = WaitCertificate.deserialize_wait_certificate(
            dwc["SerializedCert"],
            dwc["Signature"]
        )
        swc.is_valid_wait_certificate(addr, certs, txn_ids)

        self.assertEquals(wait_cert.previous_certificate_id,
                          swc.previous_certificate_id)
        self.assertEquals(wait_cert.local_mean, swc.local_mean)
        self.assertEquals(wait_cert.request_time, swc.request_time)
        self.assertEquals(wait_cert.duration, swc.duration)
        self.assertEquals(wait_cert.signature, swc.signature)
        self.assertEquals(wait_cert.identifier, swc.identifier)
        self.assertEquals(wait_cert.validator_address, swc.validator_address)
        self.assertEquals(wait_cert.block_hash, swc.block_hash)
    def test_is_valid_wait_certificate_2(self):
        addr = random_name(20)
        certs = generate_certs(WaitTimer.fixed_duration_blocks)
        txn_ids, block_hash = generate_txn_ids(10)
        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        self.assertIsNotNone(wait_timer)
        while not wait_timer.is_expired(time.time()):
            time.sleep(1)

        wait_cert = WaitCertificate.create_wait_certificate(
            wait_timer,
            block_hash)
        self.assertIsNotNone(wait_cert)

        # Verify class changes don't affect validity
        d = wait_cert.duration
        wait_cert.duration = 0
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)
        wait_cert.duration = d

        lm = wait_cert.local_mean
        wait_cert.local_mean = wait_cert.local_mean - 1
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)
        wait_cert.local_mean = lm

        pc = wait_cert.previous_certificate_id
        wait_cert.previous_certificate_id = random_name(
            pe_sim.IDENTIFIER_LENGTH)
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)
        wait_cert.previous_certificate_id = pc

        self.check_enclave_timer_tampering(wait_cert, addr, certs, txn_ids)
예제 #3
0
    def test_is_valid_wait_certificate_2(self):
        addr = random_name(20)
        certs = generate_certs(WaitTimer.fixed_duration_blocks)
        txn_ids, block_hash = generate_txn_ids(10)
        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        self.assertIsNotNone(wait_timer)
        while not wait_timer.is_expired(time.time()):
            time.sleep(1)

        wait_cert = WaitCertificate.create_wait_certificate(
            wait_timer, block_hash)
        self.assertIsNotNone(wait_cert)

        # Verify class changes don't affect validity
        d = wait_cert.duration
        wait_cert.duration = 0
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)
        wait_cert.duration = d

        lm = wait_cert.local_mean
        wait_cert.local_mean = wait_cert.local_mean - 1
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)
        wait_cert.local_mean = lm

        pc = wait_cert.previous_certificate_id
        wait_cert.previous_certificate_id = random_name(
            pe_sim.IDENTIFIER_LENGTH)
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)
        wait_cert.previous_certificate_id = pc

        self.check_enclave_timer_tampering(wait_cert, addr, certs, txn_ids)
예제 #4
0
    def test_wait_certificate(self):
        addr = random_name(20)
        certs = generate_certs(WaitTimer.fixed_duration_blocks)
        txn_ids, block_hash = generate_txn_ids(10)

        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        self.assertIsNotNone(wait_timer)
        while not wait_timer.is_expired(time.time()):
            time.sleep(1)

        wait_cert = WaitCertificate.create_wait_certificate(
            wait_timer, block_hash)
        self.assertIsNotNone(wait_cert)

        ewc = wait_cert.enclave_wait_certificate
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)
        str(wait_cert)

        swc = WaitCertificate.deserialize_wait_certificate(
            wait_cert.serialized_cert, wait_cert.signature)
        self.assertEquals(wait_cert.previous_certificate_id,
                          swc.previous_certificate_id)
        self.assertEquals(wait_cert.local_mean, swc.local_mean)
        self.assertEquals(wait_cert.request_time, swc.request_time)
        self.assertEquals(wait_cert.duration, swc.duration)
        self.assertEquals(wait_cert.signature, swc.signature)
        self.assertEquals(wait_cert.identifier, swc.identifier)
        self.assertEquals(wait_cert.validator_address, swc.validator_address)
        self.assertEquals(wait_cert.block_hash, swc.block_hash)

        swc.is_valid_wait_certificate(addr, certs, txn_ids)
        dwc = wait_cert.dump()
        swc = WaitCertificate.deserialize_wait_certificate(
            dwc["SerializedCert"], dwc["Signature"])
        swc.is_valid_wait_certificate(addr, certs, txn_ids)

        self.assertEquals(wait_cert.previous_certificate_id,
                          swc.previous_certificate_id)
        self.assertEquals(wait_cert.local_mean, swc.local_mean)
        self.assertEquals(wait_cert.request_time, swc.request_time)
        self.assertEquals(wait_cert.duration, swc.duration)
        self.assertEquals(wait_cert.signature, swc.signature)
        self.assertEquals(wait_cert.identifier, swc.identifier)
        self.assertEquals(wait_cert.validator_address, swc.validator_address)
        self.assertEquals(wait_cert.block_hash, swc.block_hash)
예제 #5
0
    def create_wait_certificate(self):
        """Create a wait certificate for the journal based on the wait timer.
        """
        with self._lock:
            hasher = hashlib.sha256()
            for tid in self.TransactionIDs:
                hasher.update(tid)
            block_hash = hasher.hexdigest()

            self.WaitCertificate = WaitCertificate.create_wait_certificate(
                self.WaitTimer, block_hash)
            if self.WaitCertificate:
                self.WaitTimer = None
    def create_wait_certificate(self):
        """Create a wait certificate for the journal based on the wait timer.
        """
        with self._lock:
            hasher = hashlib.sha256()
            for tid in self.TransactionIDs:
                hasher.update(tid)
            block_hash = hasher.hexdigest()

            self.WaitCertificate = WaitCertificate.create_wait_certificate(
                self.WaitTimer,
                block_hash)
            if self.WaitCertificate:
                self.WaitTimer = None
    def test_is_valid_wait_certificate(self):
        addr = random_name(20)
        certs = generate_certs(WaitTimer.fixed_duration_blocks)
        txn_ids, block_hash = generate_txn_ids(10)
        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        self.assertIsNotNone(wait_timer)
        while not wait_timer.is_expired(time.time()):
            time.sleep(1)

        wait_cert = WaitCertificate.create_wait_certificate(
            wait_timer,
            block_hash)
        self.assertIsNotNone(wait_cert)
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)

        # invalid list
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(None, certs, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate({}, certs, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate([], certs, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(555, certs, txn_ids)

        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, None, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, "test", txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, {}, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, 545, txn_ids)

        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, certs, None)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, certs, {})
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, certs, "test")
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, certs, 333)
예제 #8
0
    def test_is_valid_wait_certificate(self):
        addr = random_name(20)
        certs = generate_certs(WaitTimer.fixed_duration_blocks)
        txn_ids, block_hash = generate_txn_ids(10)
        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        self.assertIsNotNone(wait_timer)
        while not wait_timer.is_expired(time.time()):
            time.sleep(1)

        wait_cert = WaitCertificate.create_wait_certificate(
            wait_timer, block_hash)
        self.assertIsNotNone(wait_cert)
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)

        # invalid list
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(None, certs, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate({}, certs, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate([], certs, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(555, certs, txn_ids)

        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, None, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, "test", txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, {}, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, 545, txn_ids)

        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, certs, None)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, certs, {})
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, certs, "test")
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, certs, 333)
    def check_enclave_timer_tampering(self, wait_cert, oid, certs, txn_ids):
        # now we are going to tamper with the members of the enclave wait
        # timer object
        swc = wait_cert.serialized_cert
        ewt = wait_cert.enclave_wait_certificate

        d = ewt.duration
        ewt.duration = 0
        wait_cert.serialized_cert = ewt.serialize()
        r = wait_cert.is_valid_wait_certificate(oid, certs, txn_ids)
        self.assertFalse(r)
        ewt.duration = d

        lm = ewt.local_mean
        ewt.local_mean = wait_cert.local_mean - 1
        wait_cert.serialized_cert = ewt.serialize()
        r = wait_cert.is_valid_wait_certificate(oid, certs, txn_ids)
        self.assertFalse(r)
        ewt.local_mean = lm

        pc = wait_cert.previous_certificate_id
        ewt.previous_certificate_id = random_name(pe_sim.IDENTIFIER_LENGTH)
        ewt.serialized_cert = ewt.serialize()
        r = wait_cert.is_valid_wait_certificate(oid, certs, txn_ids)
        self.assertFalse(r)
        ewt.previous_certificate_id = pc

        # start up case, no previous certs and NULL_IDENTIFIER
        addr = random_name(20)
        block_hash = random_name(32)
        wait_timer = WaitTimer.create_wait_timer(addr, [])
        self.assertIsNotNone(wait_timer)
        wait_cert = WaitCertificate.create_wait_certificate(
            wait_timer,
            block_hash)
        self.assertIsNotNone(wait_cert)
        r = wait_cert.is_valid_wait_certificate(oid, [], txn_ids)
        self.assertTrue(r)
예제 #10
0
    def check_enclave_timer_tampering(self, wait_cert, oid, certs, txn_ids):
        # now we are going to tamper with the members of the enclave wait
        # timer object
        swc = wait_cert.serialized_cert
        ewt = wait_cert.enclave_wait_certificate

        d = ewt.duration
        ewt.duration = 0
        wait_cert.serialized_cert = ewt.serialize()
        r = wait_cert.is_valid_wait_certificate(oid, certs, txn_ids)
        self.assertFalse(r)
        ewt.duration = d

        lm = ewt.local_mean
        ewt.local_mean = wait_cert.local_mean - 1
        wait_cert.serialized_cert = ewt.serialize()
        r = wait_cert.is_valid_wait_certificate(oid, certs, txn_ids)
        self.assertFalse(r)
        ewt.local_mean = lm

        pc = wait_cert.previous_certificate_id
        ewt.previous_certificate_id = random_name(pe_sim.IDENTIFIER_LENGTH)
        ewt.serialized_cert = ewt.serialize()
        r = wait_cert.is_valid_wait_certificate(oid, certs, txn_ids)
        self.assertFalse(r)
        ewt.previous_certificate_id = pc

        # start up case, no previous certs and NULL_IDENTIFIER
        addr = random_name(20)
        block_hash = random_name(32)
        wait_timer = WaitTimer.create_wait_timer(addr, [])
        self.assertIsNotNone(wait_timer)
        wait_cert = WaitCertificate.create_wait_certificate(
            wait_timer, block_hash)
        self.assertIsNotNone(wait_cert)
        r = wait_cert.is_valid_wait_certificate(oid, [], txn_ids)
        self.assertTrue(r)
    def __init__(self, minfo=None):
        """Constructor for the PoetTransactionBlock class.

        Args:
            minfo (dict): A dict of values for initializing
                PoetTransactionBlocks.
        """
        if minfo is None:
            minfo = {}
        super(PoetTransactionBlock, self).__init__(minfo)

        self._lock = RLock()
        self.WaitTimer = None
        self.WaitCertificate = None

        if 'WaitCertificate' in minfo:
            wc = minfo.get('WaitCertificate')
            serialized = wc.get('SerializedCert')
            signature = wc.get('Signature')
            self.WaitCertificate = \
                WaitCertificate.deserialize_wait_certificate(
                    serialized, signature)

        self.AggregateLocalMean = 0.0
예제 #12
0
    def __init__(self, minfo=None):
        """Constructor for the PoetTransactionBlock class.

        Args:
            minfo (dict): A dict of values for initializing
                PoetTransactionBlocks.
        """
        if minfo is None:
            minfo = {}
        super(PoetTransactionBlock, self).__init__(minfo)

        self._lock = RLock()
        self.WaitTimer = None
        self.WaitCertificate = None

        if 'WaitCertificate' in minfo:
            wc = minfo.get('WaitCertificate')
            serialized = wc.get('SerializedCert')
            signature = wc.get('Signature')
            self.WaitCertificate = \
                WaitCertificate.deserialize_wait_certificate(
                    serialized, signature)

        self.AggregateLocalMean = 0.0