Exemplo n.º 1
0
    def accumulation_hook(self, db_session):
        # Check event requirements
        if len(self.event.attributes) == 1 and \
                self.event.attributes[0]['type'] == 'AccountId':

            account_id = self.event.attributes[0]['value'].replace('0x', '')

            self.block._accounts_reaped.append(account_id)

            account_audit = AccountAudit(
                account_id=account_id,
                block_id=self.event.block_id,
                extrinsic_idx=self.event.extrinsic_idx,
                event_idx=self.event.event_idx,
                type_id=ACCOUNT_AUDIT_TYPE_REAPED
            )

            account_audit.save(db_session)

            # Insert account index audit record

            new_account_index_audit = AccountIndexAudit(
                account_index_id=None,
                account_id=account_id,
                block_id=self.event.block_id,
                extrinsic_idx=self.event.extrinsic_idx,
                event_idx=self.event.event_idx,
                type_id=ACCOUNT_INDEX_AUDIT_TYPE_REAPED
            )

            new_account_index_audit.save(db_session)
Exemplo n.º 2
0
    def process_genesis(self, block):

        # Set block time of parent block
        child_block = Block.query(
            self.db_session).filter_by(parent_hash=block.hash).first()
        block.set_datetime(child_block.datetime)

        # Retrieve genesis accounts
        genesis_account_page_count = self.substrate.get_runtime_state(
            module="Indices",
            storage_function="NextEnumSet",
            block_hash=block.hash).get('result', 0)

        # Get Accounts on EnumSet
        block.count_accounts_new = 0
        block.count_accounts = 0

        for enum_set_nr in range(0, genesis_account_page_count + 1):

            genesis_accounts = self.substrate.get_runtime_state(
                module="Indices",
                storage_function="EnumSet",
                params=[enum_set_nr],
                block_hash=block.hash).get('result')

            if genesis_accounts:
                block.count_accounts_new += len(genesis_accounts)
                block.count_accounts += len(genesis_accounts)

                for idx, account_id in enumerate(genesis_accounts):
                    account_audit = AccountAudit(
                        account_id=account_id.replace('0x', ''),
                        block_id=block.id,
                        extrinsic_idx=None,
                        event_idx=None,
                        type_id=ACCOUNT_AUDIT_TYPE_NEW)

                    account_audit.save(self.db_session)

                    account_index_id = enum_set_nr * 64 + idx

                    account_index_audit = AccountIndexAudit(
                        account_index_id=account_index_id,
                        account_id=account_id.replace('0x', ''),
                        block_id=block.id,
                        extrinsic_idx=None,
                        event_idx=None,
                        type_id=ACCOUNT_INDEX_AUDIT_TYPE_NEW)

                    account_index_audit.save(self.db_session)

        block.save(self.db_session)

        # Create initial session
        initial_session_event = NewSessionEventProcessor(block, Event(), None)
        initial_session_event.add_session(db_session=self.db_session,
                                          session_id=0)
Exemplo n.º 3
0
    def accumulation_hook(self, db_session):

        # Check event requirements
        if len(self.event.attributes) == 2 and \
                self.event.attributes[0]['type'] == 'AccountId' and self.event.attributes[1]['type'] == 'Balance':

            account_id = self.event.attributes[0]['value'].replace('0x', '')
            balance = self.event.attributes[1]['value']

            self.block._accounts_new.append(account_id)

            account_audit = AccountAudit(
                account_id=account_id,
                block_id=self.event.block_id,
                extrinsic_idx=self.event.extrinsic_idx,
                event_idx=self.event.event_idx,
                type_id=ACCOUNT_AUDIT_TYPE_NEW)

            account_total = Account.query(db_session).filter_by(
                id=account_audit.account_id).count()
            #print('NewAccountEventProcessor get account_total {} =='.format(account_total))

            if account_total <= 0:
                account = Account(id=account_audit.account_id,
                                  address=bech32.encode(
                                      HRP,
                                      bytes().fromhex(
                                          account_audit.account_id)),
                                  created_at_block=account_audit.block_id,
                                  updated_at_block=account_audit.block_id,
                                  balance=0)
                if account_audit.type_id == ACCOUNT_AUDIT_TYPE_REAPED:
                    account.count_reaped += 1
                    account.is_reaped = True

                elif account_audit.type_id == ACCOUNT_AUDIT_TYPE_NEW:
                    account.is_reaped = False

                account.updated_at_block = account_audit.block_id
                # If reaped but does not exist, create new account for now
                if account_audit.type_id != ACCOUNT_AUDIT_TYPE_NEW:
                    account.is_reaped = True
                    account.count_reaped = 1
                account.shard_num = 0

                #print('NewAccountEventProcessor start add  account {} =='.format(account))

                account.save(db_session)
                # db_session.commit()

            account_audit.save(db_session)
Exemplo n.º 4
0
    def accumulation_revert(self, db_session):

        for item in AccountIndexAudit.query(db_session).filter_by(block_id=self.block.id):
            db_session.delete(item)

        for item in AccountAudit.query(db_session).filter_by(block_id=self.block.id):
            db_session.delete(item)
Exemplo n.º 5
0
    def sequencing_hook(self, db_session, parent_block_data, parent_sequenced_block_data):

        for account_audit in AccountAudit.query(db_session).filter_by(block_id=self.block.id).order_by('event_idx'):
            try:
                account = Account.query(db_session).filter_by(id=account_audit.account_id).one()

                if account_audit.type_id == ACCOUNT_AUDIT_TYPE_REAPED:
                    account.count_reaped += 1
                    account.is_reaped = True

                elif account_audit.type_id == ACCOUNT_AUDIT_TYPE_NEW:
                    account.is_reaped = False

                account.updated_at_block = self.block.id

            except NoResultFound:

                account = Account(
                    id=account_audit.account_id,
                    address=ss58_encode(account_audit.account_id, SUBSTRATE_ADDRESS_TYPE),
                    created_at_block=self.block.id,
                    updated_at_block=self.block.id,
                    balance=0
                )

                # If reaped but does not exist, create new account for now
                if account_audit.type_id != ACCOUNT_AUDIT_TYPE_NEW:
                    account.is_reaped = True
                    account.count_reaped = 1

            account.save(db_session)
Exemplo n.º 6
0
    def sequencing_hook(self, db_session, parent_block_data,
                        parent_sequenced_block_data):

        for account_audit in AccountAudit.query(db_session).filter_by(
                block_id=self.block.id).order_by('event_idx'):
            try:
                account = Account.query(db_session).filter_by(
                    id=account_audit.account_id).one()

                if account_audit.type_id == ACCOUNT_AUDIT_TYPE_REAPED:
                    account.count_reaped += 1
                    account.is_reaped = True

                elif account_audit.type_id == ACCOUNT_AUDIT_TYPE_NEW:
                    account.is_reaped = False

                account.updated_at_block = self.block.id

            except NoResultFound:

                account = Account(id=account_audit.account_id,
                                  address=bech32.encode(
                                      HRP,
                                      bytes().fromhex(
                                          account_audit.account_id)),
                                  created_at_block=self.block.id,
                                  updated_at_block=self.block.id,
                                  balance=0)

                # If reaped but does not exist, create new account for now
                if account_audit.type_id != ACCOUNT_AUDIT_TYPE_NEW:
                    account.is_reaped = True
                    account.count_reaped = 1
            account.shard_num = random.randint(1, 10000)
            account.save(db_session)
Exemplo n.º 7
0
    def accumulation_hook(self, db_session):
        # Check event requirements
        if len(self.event.attributes) == 1 and \
                self.event.attributes[0]['type'] == 'AccountId':

            account_id = self.event.attributes[0]['value'].replace('0x', '')
        else:
            raise ValueError('Event doensn\'t meet requirements')

        self.block._accounts_reaped.append(account_id)

        account_audit = AccountAudit(account_id=account_id,
                                     block_id=self.event.block_id,
                                     extrinsic_idx=self.event.extrinsic_idx,
                                     event_idx=self.event.event_idx,
                                     type_id=ACCOUNT_AUDIT_TYPE_REAPED)

        account_audit.save(db_session)
Exemplo n.º 8
0
    def accumulation_hook(self, db_session):

        # Check event requirements
        if len(self.event.attributes) == 1 and \
                self.event.attributes[0]['type'] == 'AccountId':

            account_id = self.event.attributes[0]['value'].replace('0x', '')

            self.block._accounts_new.append(account_id)

            account_audit = AccountAudit(
                account_id=account_id,
                block_id=self.event.block_id,
                extrinsic_idx=self.event.extrinsic_idx,
                event_idx=self.event.event_idx,
                type_id=ACCOUNT_AUDIT_TYPE_NEW)

            account_audit.save(db_session)
Exemplo n.º 9
0
    def sequencing_hook(self, db_session, parent_block_data,
                        parent_sequenced_block_data):

        for account_audit in AccountAudit.query(db_session).filter_by(
                block_id=self.block.id).order_by('event_idx'):
            try:
                account = Account.query(db_session).filter_by(
                    id=account_audit.account_id).one()

                if account_audit.type_id == ACCOUNT_AUDIT_TYPE_REAPED:
                    account.count_reaped += 1
                    account.is_reaped = True

                elif account_audit.type_id == ACCOUNT_AUDIT_TYPE_NEW:
                    account.is_reaped = False

                account.updated_at_block = self.block.id

            except NoResultFound:

                account = Account(id=account_audit.account_id,
                                  address=ss58_encode(account_audit.account_id,
                                                      SUBSTRATE_ADDRESS_TYPE),
                                  hash_blake2b=blake2_256(
                                      binascii.unhexlify(
                                          account_audit.account_id)),
                                  created_at_block=self.block.id,
                                  updated_at_block=self.block.id,
                                  balance=0)

                # Retrieve index in corresponding account
                account_index = AccountIndex.query(db_session).filter_by(
                    account_id=account.id).first()

                if account_index:

                    account.index_address = account_index.short_address
                    account.save(db_session)

                # # If reaped but does not exist, create new account for now
                # if account_audit.type_id != ACCOUNT_AUDIT_TYPE_NEW:
                #     account.is_reaped = True
                #     account.count_reaped = 1

            account.save(db_session)
Exemplo n.º 10
0
    def sequencing_hook(self, db_session, parent_block_data, parent_sequenced_block_data):

        for account_audit in AccountAudit.query(db_session).filter_by(block_id=self.block.id).order_by('event_idx'):
            try:
                account = Account.query(db_session).filter_by(id=account_audit.account_id).one()

                if account_audit.type_id == ACCOUNT_AUDIT_TYPE_REAPED:
                    account.count_reaped += 1
                    account.is_reaped = True

                elif account_audit.type_id == ACCOUNT_AUDIT_TYPE_NEW:
                    account.is_reaped = False

                account.updated_at_block = self.block.id

            except NoResultFound:

                account = Account(
                    id=account_audit.account_id,
                    address=ss58_encode(account_audit.account_id, SUBSTRATE_ADDRESS_TYPE),
                    created_at_block=self.block.id,
                    updated_at_block=self.block.id,
                    balance=0
                )
                ### account balance
                substrate = SubstrateInterface(SUBSTRATE_RPC_URL)
                balance = substrate.get_storage(
                    block_hash=None,
                    module='Balances',
                    function='FreeBalance',
                    params=account_audit.account_id,
                    return_scale_type='Balance',
                    hasher='Blake2_256') or 0

                account.balance = balance
                # If reaped but does not exist, create new account for now
                if account_audit.type_id != ACCOUNT_AUDIT_TYPE_NEW:
                    account.is_reaped = True
                    account.count_reaped = 1

            account.save(db_session)
Exemplo n.º 11
0
    def process_genesis(self, block):

        # Set block time of parent block
        child_block = Block.query(self.db_session).filter_by(parent_hash=block.hash).first()
        block.set_datetime(child_block.datetime)

        # Retrieve genesis accounts
        if settings.get_versioned_setting('SUBSTRATE_STORAGE_INDICES', block.spec_version_id) == 'Accounts':

            # Get accounts from storage keys
            storage_key_prefix = self.substrate.generate_storage_hash(
                storage_module='System',
                storage_function='Account',
                metadata_version=settings.SUBSTRATE_METADATA_VERSION
            )

            rpc_result = self.substrate.rpc_request(
                'state_getKeys',
                [storage_key_prefix, block.hash]
            ).get('result')
            # Extract accounts from storage key
            genesis_accounts = [storage_key[-64:] for storage_key in rpc_result if len(storage_key) == 162]

            for account_id in genesis_accounts:
                account_audit = AccountAudit(
                    account_id=account_id,
                    block_id=block.id,
                    extrinsic_idx=None,
                    event_idx=None,
                    type_id=settings.ACCOUNT_AUDIT_TYPE_NEW
                )

                account_audit.save(self.db_session)

        elif settings.get_versioned_setting('SUBSTRATE_STORAGE_INDICES', block.spec_version_id) == 'EnumSet':

            genesis_account_page_count = self.substrate.get_runtime_state(
                module="Indices",
                storage_function="NextEnumSet",
                block_hash=block.hash
            ).get('result', 0)

            # Get Accounts on EnumSet
            block.count_accounts_new = 0
            block.count_accounts = 0

            for enum_set_nr in range(0, genesis_account_page_count + 1):

                genesis_accounts = self.substrate.get_runtime_state(
                    module="Indices",
                    storage_function="EnumSet",
                    params=[enum_set_nr],
                    block_hash=block.hash
                ).get('result')

                if genesis_accounts:
                    block.count_accounts_new += len(genesis_accounts)
                    block.count_accounts += len(genesis_accounts)

                    for idx, account_id in enumerate(genesis_accounts):
                        account_audit = AccountAudit(
                            account_id=account_id.replace('0x', ''),
                            block_id=block.id,
                            extrinsic_idx=None,
                            event_idx=None,
                            type_id=settings.ACCOUNT_AUDIT_TYPE_NEW
                        )

                        account_audit.save(self.db_session)

                        account_index_id = enum_set_nr * 64 + idx

                        account_index_audit = AccountIndexAudit(
                            account_index_id=account_index_id,
                            account_id=account_id.replace('0x', ''),
                            block_id=block.id,
                            extrinsic_idx=None,
                            event_idx=None,
                            type_id=settings.ACCOUNT_INDEX_AUDIT_TYPE_NEW
                        )

                        account_index_audit.save(self.db_session)

        block.save(self.db_session)

        # Add hardcoded account like treasury stored in settings
        for account_id in settings.SUBSTRATE_TREASURY_ACCOUNTS:
            account_audit = AccountAudit(
                account_id=account_id,
                block_id=block.id,
                extrinsic_idx=None,
                event_idx=None,
                data={'is_treasury': True},
                type_id=settings.ACCOUNT_AUDIT_TYPE_NEW
            )

            account_audit.save(self.db_session)

        # Check for sudo accounts
        try:
            # Update sudo key
            sudo_key = self.substrate.get_runtime_state(
                module='Sudo',
                storage_function='Key',
                block_hash=block.hash
            ).get('result')

            account_audit = AccountAudit(
                account_id=sudo_key.replace('0x', ''),
                block_id=block.id,
                extrinsic_idx=None,
                event_idx=None,
                data={'is_sudo': True},
                type_id=settings.ACCOUNT_AUDIT_TYPE_NEW
            )

            account_audit.save(self.db_session)
        except ValueError:
            pass

        # Create initial session
        initial_session_event = NewSessionEventProcessor(
            block=block, event=Event(), substrate=self.substrate
        )

        if settings.get_versioned_setting('NEW_SESSION_EVENT_HANDLER', block.spec_version_id):
            initial_session_event.add_session(db_session=self.db_session, session_id=0)
        else:
            initial_session_event.add_session_old(db_session=self.db_session, session_id=0)
Exemplo n.º 12
0
    def sequencing_hook(self, db_session, parent_block_data, parent_sequenced_block_data):

        for account_audit in AccountAudit.query(db_session).filter_by(block_id=self.block.id).order_by('event_idx'):
            try:
                account = Account.query(db_session).filter_by(id=account_audit.account_id).one()

                if account_audit.type_id == settings.ACCOUNT_AUDIT_TYPE_REAPED:
                    account.count_reaped += 1
                    account.is_reaped = True

                elif account_audit.type_id == settings.ACCOUNT_AUDIT_TYPE_NEW:
                    account.is_reaped = False

                account.updated_at_block = self.block.id

            except NoResultFound:

                account = Account(
                    id=account_audit.account_id,
                    address=ss58_encode(account_audit.account_id, settings.SUBSTRATE_ADDRESS_TYPE),
                    hash_blake2b=blake2_256(binascii.unhexlify(account_audit.account_id)),
                    is_treasury=(account_audit.data or {}).get('is_treasury', False),
                    is_sudo=(account_audit.data or {}).get('is_sudo', False),
                    was_sudo=(account_audit.data or {}).get('is_sudo', False),
                    created_at_block=self.block.id,
                    updated_at_block=self.block.id
                )

                # Retrieve index in corresponding account
                account_index = AccountIndex.query(db_session).filter_by(account_id=account.id).first()

                if account_index:

                    account.index_address = account_index.short_address

                # Retrieve and set initial balance
                try:
                    account_info_data = self.substrate.get_runtime_state(
                        module='System',
                        storage_function='Account',
                        params=['0x{}'.format(account.id)],
                        block_hash=self.block.hash
                    ).get('result')

                    if account_info_data:

                        account.balance_free = account_info_data["data"]["free"]
                        account.balance_reserved = account_info_data["data"]["reserved"]
                        account.balance_total = account_info_data["data"]["free"] + account_info_data["data"]["reserved"]
                        account.nonce = account_info_data["nonce"]
                except ValueError:
                    pass

                # # If reaped but does not exist, create new account for now
                # if account_audit.type_id != ACCOUNT_AUDIT_TYPE_NEW:
                #     account.is_reaped = True
                #     account.count_reaped = 1

            account.save(db_session)

        # Until SUDO and batch calls are processed separately we need to do a safety check to be sure we include all
        # accounts that have activity (lookup in account_index) in current block
        # TODO implement calls

        for search_index in db_session.query(SearchIndex.account_id).filter(
                SearchIndex.block_id == self.block.id,
                SearchIndex.account_id.notin_(db_session.query(Account.id))
        ).distinct():

            account = Account(
                id=search_index.account_id,
                address=ss58_encode(search_index.account_id, settings.SUBSTRATE_ADDRESS_TYPE),
                hash_blake2b=blake2_256(binascii.unhexlify(search_index.account_id)),
                created_at_block=self.block.id,
                updated_at_block=self.block.id
            )

            try:
                account_info_data = self.substrate.get_runtime_state(
                    module='System',
                    storage_function='Account',
                    params=['0x{}'.format(account.id)],
                    block_hash=self.block.hash
                ).get('result')

                if account_info_data:
                    account.balance_free = account_info_data["data"]["free"]
                    account.balance_reserved = account_info_data["data"]["reserved"]
                    account.balance_total = account_info_data["data"]["free"] + account_info_data["data"]["reserved"]
                    account.nonce = account_info_data["nonce"]
            except ValueError:
                pass

            account.save(db_session)
Exemplo n.º 13
0
    def process_genesis(self, block):
        substrate = SubstrateInterface(SUBSTRATE_RPC_URL)

        # Set block time of parent block
        child_block = Block.query(
            self.db_session).filter_by(parent_hash=block.hash).first()
        block.set_datetime(child_block.datetime)

        # Retrieve genesis accounts
        storage_call = RuntimeStorage.query(self.db_session).filter_by(
            module_id='indices',
            name='NextEnumSet',
            spec_version=block.spec_version_id).first()

        if storage_call:
            genesis_account_page_count = substrate.get_storage(
                block_hash=block.hash,
                module="Indices",
                function="NextEnumSet",
                return_scale_type=storage_call.get_return_type(),
                hasher=storage_call.type_hasher) or 0

            # Get Accounts on EnumSet
            storage_call = RuntimeStorage.query(self.db_session).filter_by(
                module_id='indices',
                name='EnumSet',
                spec_version=block.spec_version_id).first()

            if storage_call:

                block.count_accounts_new = 0
                block.count_accounts = 0

                for enum_set_nr in range(0, genesis_account_page_count + 1):

                    account_index_u32 = U32()
                    account_index_u32.encode(enum_set_nr)

                    genesis_accounts = substrate.get_storage(
                        block_hash=block.hash,
                        module="Indices",
                        function="EnumSet",
                        params=account_index_u32.data.data.hex(),
                        return_scale_type=storage_call.get_return_type(),
                        hasher=storage_call.type_hasher)

                    if genesis_accounts:
                        block.count_accounts_new += len(genesis_accounts)
                        block.count_accounts += len(genesis_accounts)

                        for idx, account_id in enumerate(genesis_accounts):
                            account_audit = AccountAudit(
                                account_id=account_id.replace('0x', ''),
                                block_id=block.id,
                                extrinsic_idx=None,
                                event_idx=None,
                                type_id=ACCOUNT_AUDIT_TYPE_NEW)

                            account_audit.save(self.db_session)

                            account_index_id = enum_set_nr * 64 + idx

                            account_index_audit = AccountIndexAudit(
                                account_index_id=account_index_id,
                                account_id=account_id.replace('0x', ''),
                                block_id=block.id,
                                extrinsic_idx=None,
                                event_idx=None,
                                type_id=ACCOUNT_INDEX_AUDIT_TYPE_NEW)

                            account_index_audit.save(self.db_session)

        block.save(self.db_session)

        # Create initial session
        initial_session_event = NewSessionEventProcessor(block, Event(), None)
        initial_session_event.add_session(db_session=self.db_session,
                                          session_id=0)