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)
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)
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)
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)
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)
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)
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)