opentensor / bittensor

Internet-scale Neural Networks
https://www.bittensor.com/
MIT License
883 stars 305 forks source link

Migrate Delegate Info to onchain Indentities #2089

Open distributedstatemachine opened 3 months ago

distributedstatemachine commented 3 months ago

Description

Currently, the delegation rewards distribution system in our blockchain project retrieves delegate identities from a GitHub link. We need to migrate this process to use the new blockchain RPC method delegateInfo_getDelegateIdentities.

This change will affect multiple parts of our codebase, including the delegate information retrieval, display, and related commands. The new RPC method will return delegate identity information in the IdentityInfo structure.

Acceptance Criteria

Tasks

  1. Update the DelegatesDetails class in bittensor/commands/utils.py to match the IdentityInfo struct in subtensor:
@dataclass
class DelegatesDetails:
    display: str
    additional: List[Tuple[str, str]]
    web: str
    legal: Optional[str] = None
    riot: Optional[str] = None
    email: Optional[str] = None
    pgp_fingerprint: Optional[str] = None
    image: Optional[str] = None
    twitter: Optional[str] = None

    @classmethod
    def from_chain_data(cls, data: Dict[str, Any]) -> "DelegatesDetails":
        return cls(
            display=data.get('display', ''),
            additional=data.get('additional', []),
            web=data.get('web', ''),
            legal=data.get('legal'),
            riot=data.get('riot'),
            email=data.get('email'),
            pgp_fingerprint=data.get('pgp_fingerprint'),
            image=data.get('image'),
            twitter=data.get('twitter')
        )
  1. Implement a new function in the Subtensor class to call the delegateInfo_getDelegateIdentities RPC method:
class Subtensor:
    ...
    def get_delegate_identities(self, block: Optional[int] = None) -> Dict[str, DelegatesDetails]:
        @retry(delay=1, tries=3, backoff=2, max_delay=4, logger=_logger)
        def make_substrate_call_with_retry():
            block_hash = None if block is None else self.substrate.get_block_hash(block)
            return self.substrate.rpc_request(
                method="delegateInfo_getDelegateIdentities",
                params=[block_hash] if block_hash else [],
            )

        json_body = make_substrate_call_with_retry()

        if not (result := json_body.get("result", None)):
            return {}

        return self._parse_delegate_identities(result)

    def _parse_delegate_identities(self, raw_data: List[int]) -> Dict[str, DelegatesDetails]:
        # Implement parsing logic here
        # Convert the raw_data (Vec<u8>) into a dictionary of DelegatesDetails
        # See how similar rpcs do this 
        pass
  1. Update the get_delegates_details function in bittensor/commands/utils.py:
def get_delegates_details(subtensor: "bittensor.subtensor") -> Optional[Dict[str, DelegatesDetails]]:
    try:
        return subtensor.get_delegate_identities()
    except Exception:
        return None  # Fail silently
  1. Update any parts of the codebase that call registered_delegate_info and fetch it through the github link

  2. Update unit tests to mock the new RPC call instead of the GitHub API call and use the new DelegatesDetails structure.

  3. Update documentation and comments throughout the codebase to reflect the new method of retrieving delegate identities and the new DelegatesDetails structure.

Additional Considerations

Related Links