bittensor icon indicating copy to clipboard operation
bittensor copied to clipboard

Migrate Delegate Info to onchain Indentities

Open distributedstatemachine opened this issue 1 year ago • 0 comments

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

  • Implement a new function to call the delegateInfo_getDelegateIdentities RPC method.
  • Update the DelegatesDetails class to match the IdentityInfo structure.
  • Update all relevant parts of the codebase to use the new RPC method instead of the GitHub link.
  • Ensure backward compatibility or provide a migration path for existing users.
  • Update documentation and comments to reflect the new method of retrieving delegate identities.

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

  • Do we need to implement a fallback mechanism to the GitHub method in case the RPC call fails?
  • Should we implement caching for the delegate identities to reduce the number of RPC calls?
  • How do we handle the case where a delegate's identity is not available through the RPC method?
  • How should we handle the additional fields in the IdentityInfo structure that weren't present in the GitHub data (legal, riot, email, pgp_fingerprint, image, twitter)?

Related Links

distributedstatemachine avatar Jul 01 '24 10:07 distributedstatemachine