예제 #1
0
    def test_centralized_oracle_factory_rollback(self):
        oracle = CentralizedOracleFactory()

        # saving event_description to IPFS
        event_description_json = {
            'title': oracle.event_description.title,
            'description': oracle.event_description.description,
            'resolutionDate':
            oracle.event_description.resolution_date.isoformat(),
            'outcomes': oracle.event_description.outcomes
        }

        ipfs_hash = self.ipfs_api.post(event_description_json)

        block = {
            'number': oracle.creation_block,
            'timestamp': self.to_timestamp(oracle.creation_date_time)
        }

        oracle_address = oracle.address

        oracle_event = {
            'name':
            'CentralizedOracleCreation',
            'address':
            oracle.factory,
            'params': [{
                'name': 'creator',
                'value': oracle.creator
            }, {
                'name': 'centralizedOracle',
                'value': oracle_address,
            }, {
                'name': 'ipfsHash',
                'value': ipfs_hash
            }]
        }
        oracle.delete()

        CentralizedOracleFactoryReceiver().save(oracle_event, block)
        created_oracle = CentralizedOracle.objects.get(address=oracle_address)
        self.assertIsNotNone(created_oracle.pk)
        # Rollback
        CentralizedOracleFactoryReceiver().rollback(oracle_event, block)
        with self.assertRaises(CentralizedOracle.DoesNotExist):
            CentralizedOracle.objects.get(address=oracle_address)

        # Rollback over nonexistent centralized oracle should fail
        self.assertRaises(Exception,
                          CentralizedOracleFactoryReceiver().rollback,
                          oracle_event, block)
예제 #2
0
    def test_centralized_oracle_receiver(self):
        oracle = CentralizedOracleFactory()
        # saving event_description to IPFS
        event_description_json = {
            'title': oracle.event_description.title,
            'description': oracle.event_description.description,
            'resolutionDate':
            oracle.event_description.resolution_date.isoformat(),
            'outcomes': oracle.event_description.outcomes
        }

        ipfs_hash = self.ipfs_api.post(event_description_json)

        block = {
            'number': oracle.creation_block,
            'timestamp': self.to_timestamp(oracle.creation_date_time)
        }

        oracle_address = oracle.address
        oracle_event = {
            'name':
            'CentralizedOracleCreation',
            'address':
            oracle.factory,
            'params': [{
                'name': 'creator',
                'value': oracle.creator
            }, {
                'name': 'centralizedOracle',
                'value': oracle_address,
            }, {
                'name': 'ipfsHash',
                'value': ipfs_hash
            }]
        }
        oracle.delete()
        CentralizedOracleFactoryReceiver().save(oracle_event, block)
        saved_oracle1 = CentralizedOracle.objects.get(address=oracle_address)
        self.assertIsNotNone(saved_oracle1.pk)

        # Cannot save twice
        oracle2 = CentralizedOracleFactory()
        oracle_event.get('params')[0].update({'value': oracle2.creator})
        oracle_event.get('params')[2].update(
            {'value': oracle2.event_description.ipfs_hash})
        instance = CentralizedOracleFactoryReceiver().save(oracle_event, block)
        self.assertIsNone(instance)
        saved_oracle2 = CentralizedOracle.objects.get(address=oracle_address)
        self.assertEqual(saved_oracle1.event_description.ipfs_hash,
                         saved_oracle2.event_description.ipfs_hash)
        self.assertEqual(saved_oracle1.creator, saved_oracle2.creator)
예제 #3
0
    def test_create_centralized_oracle_scalar_event_with_outcomes(self):
        oracle = CentralizedOracleFactory()

        block = {
            'number': oracle.creation_block,
            'timestamp': mktime(oracle.creation_date_time.timetuple())
        }

        oracle_event = {
            'address':
            oracle.factory,
            'params': [{
                'name': 'creator',
                'value': oracle.creator
            }, {
                'name': 'centralizedOracle',
                'value': oracle.address,
            }, {
                'name': 'ipfsHash',
                'value': 'something unknown'
            }]
        }
        # remove test oracle before creating it again
        oracle.delete()
        s = CentralizedOracleSerializer(data=oracle_event, block=block)
        # ipfs_hash not saved to IPFS
        self.assertFalse(s.is_valid(), s.errors)
        # oracle.event_description
        event_description_json = {
            'title': oracle.event_description.title,
            'description': oracle.event_description.description,
            'resolutionDate':
            oracle.event_description.resolution_date.isoformat(),
            'outcomes': [],
            'unit': 'unit',
            'decimals': 2
        }

        # save event_description to IPFS
        ipfs_hash = self.ipfs.post(event_description_json)
        oracle_event.get('params')[2]['value'] = ipfs_hash

        s = CentralizedOracleSerializer(data=oracle_event, block=block)
        self.assertTrue(s.is_valid(), s.errors)
        instance = s.save()
        self.assertIsNotNone(instance)

        self.assertEqual(
            ScalarEventDescription.objects.filter(
                ipfs_hash=instance.event_description.ipfs_hash).count(), 1)
예제 #4
0
    def test_create_centralized_oracle(self):
        oracle = CentralizedOracleFactory()

        block = {
            'number': oracle.creation_block,
            'timestamp': mktime(oracle.creation_date_time.timetuple())
        }

        oracle_event = {
            'address':
            oracle.factory,
            'params': [{
                'name': 'creator',
                'value': oracle.creator
            }, {
                'name': 'centralizedOracle',
                'value': oracle.address,
            }, {
                'name': 'ipfsHash',
                'value': oracle.event_description.ipfs_hash[1:-7] + ''
            }]
        }
        oracle.delete()
        s = CentralizedOracleSerializer(data=oracle_event, block=block)
        # ipfs_hash not saved to IPFS
        self.assertFalse(s.is_valid(), s.errors)
        # oracle.event_description
        event_description_json = {
            'title': oracle.event_description.title,
            'description': oracle.event_description.description,
            'resolutionDate':
            oracle.event_description.resolution_date.isoformat(),
            'outcomes': ['Yes', 'No']
        }

        # save event_description to IPFS
        ipfs_hash = self.ipfs.post(event_description_json)
        oracle_event.get('params')[2]['value'] = ipfs_hash

        s = CentralizedOracleSerializer(data=oracle_event, block=block)
        self.assertTrue(s.is_valid(), s.errors)
        instance = s.save()
        self.assertIsNotNone(instance)
예제 #5
0
    def test_create_centralized_oracle_instance(self):
        oracle = CentralizedOracleFactory()
        oracle.delete()
        # oracle.event_description
        event_description_json = {
            'title': oracle.event_description.title,
            'description': oracle.event_description.description,
            'resolutionDate':
            oracle.event_description.resolution_date.isoformat(),
            'outcomes': oracle.event_description.outcomes
        }

        block = {
            'number': oracle.creation_block,
            'timestamp': mktime(oracle.creation_date_time.timetuple())
        }

        oracle_event = {
            'address':
            oracle.factory,
            'params': [{
                'name': 'creator',
                'value': oracle.creator
            }, {
                'name': 'centralizedOracle',
                'value': oracle.address,
            }, {
                'name': 'ipfsHash',
                'value': oracle.event_description.ipfs_hash
            }]
        }

        s = CentralizedOracleInstanceSerializer(data=oracle_event, block=block)
        self.assertTrue(s.is_valid(), s.errors)
        instance = s.save()
        self.assertIsNotNone(instance)