Exemple #1
0
    def __init__(
        self,
        config_data: Dict,
        hera: herapy.Aergo,
        aergo_oracle: str,
    ):
        self.hera = hera
        self.config_data = config_data
        self.aergo_oracle = aergo_oracle
        self.aergo_id = query_aergo_id(self.hera, self.aergo_oracle)

        current_validators = query_aergo_validators(self.hera,
                                                    self.aergo_oracle)
        logger.info("\"Validators: %s\"", current_validators)

        # create all channels with validators
        self.channels: List[grpc._channel.Channel] = []
        self.stubs: List[BridgeOperatorStub] = []
        assert len(current_validators) == len(self.config_data['validators']),\
            "Validators in config file must match bridge validators " \
            "when starting (current validators connection needed to make "\
            "updates).\nExpected validators: {}".format(current_validators)
        for i, validator in enumerate(self.config_data['validators']):
            assert current_validators[i] == validator['addr'], \
                "Validators in config file must match bridge validators " \
                "when starting (current validators connection needed to make "\
                "updates).\nExpected validators: {}".format(current_validators)
            ip = validator['ip']
            channel = grpc.insecure_channel(ip)
            stub = BridgeOperatorStub(channel)
            self.channels.append(channel)
            self.stubs.append(stub)

        self.pool = Pool(len(self.stubs))
Exemple #2
0
    def monitor_settings(self):
        """Check if a modification of bridge settings is requested by seeing
        if the config file has been changed and try to update the bridge
        contract (gather 2/3 validators signatures).

        """
        config_data = load_config_data(self.config_file_path)
        t_anchor, t_final = query_aergo_tempo(self.hera, self.aergo_bridge)
        unfreeze_fee = query_unfreeze_fee(self.hera, self.aergo_bridge)
        config_t_anchor = (config_data['networks'][self.aergo_net]['bridges']
                           [self.eth_net]['t_anchor'])
        if t_anchor != config_t_anchor:
            logger.info(
                '\"Anchoring periode update requested: %s\"', config_t_anchor)
            self.update_t_anchor(config_t_anchor)
        config_t_final = (config_data['networks'][self.aergo_net]['bridges']
                          [self.eth_net]['t_final'])
        if t_final != config_t_final:
            logger.info('\"Finality update requested: %s\"', config_t_final)
            self.update_t_final(config_t_final)
        config_unfreeze_fee = (config_data['networks'][self.aergo_net]
                               ['bridges'][self.eth_net]['unfreeze_fee'])
        if unfreeze_fee != config_unfreeze_fee:
            logger.info(
                '\"Unfreeze fee update requested: %s\"', config_unfreeze_fee)
            self.update_unfreeze_fee(config_unfreeze_fee)
        if self.oracle_update:
            validators = query_aergo_validators(self.hera, self.aergo_oracle)
            config_validators = \
                [val['addr'] for val in config_data['validators']]
            if validators != config_validators:
                logger.info(
                    '\"Validator set update requested: %s\"',
                    config_validators
                )
                if self.update_validators(config_validators):
                    self.val_connect.use_new_validators(config_data)
            oracle = query_aergo_oracle(self.hera, self.aergo_bridge)
            config_oracle = (config_data['networks'][self.aergo_net]['bridges']
                             [self.eth_net]['oracle'])
            if oracle != config_oracle:
                logger.info('\"Oracle change requested: %s\"', config_oracle)
                self.update_oracle(config_oracle)
Exemple #3
0
    def is_valid_eth_validators(self, config_vals, val_msg):
        """ Check if the Ethereum validator set update requested matches the local
        validator setting.

        """
        # check destination nonce is correct
        nonce = int(
            self.hera.query_sc_state(self.aergo_oracle,
                                     ["_sv__nonce"]).var_proofs[0].value)
        if nonce != val_msg.destination_nonce:
            return ("Incorrect Nonce, got: {}, expected: {}".format(
                val_msg.destination_nonce, nonce))
        # check new validators are different from current ones to prevent
        # update spamming
        current_validators = query_aergo_validators(self.hera,
                                                    self.aergo_oracle)
        if current_validators == config_vals:
            return "Not voting for a new validator set"
        # check validators are same in config file
        if config_vals != val_msg.validators:
            return ("Invalid validator set, got: {}, expected: {}".format(
                val_msg.validators, config_vals))
        return None
Exemple #4
0
def check_bridge_status(root_path: str, config_data: Dict, aergo_net: str,
                        eth_net: str, auto_update: bool, oracle_update: bool):
    logger.info("\"Connect Aergo and Ethereum\"")
    hera = herapy.Aergo()
    hera.connect(config_data['networks'][aergo_net]['ip'])

    ip = config_data['networks'][eth_net]['ip']
    web3 = Web3(Web3.HTTPProvider(ip))
    eth_poa = config_data['networks'][eth_net]['isPOA']
    if eth_poa:
        web3.middleware_onion.inject(geth_poa_middleware, layer=0)
    assert web3.isConnected()

    # remember bridge contracts
    # eth bridge
    bridge_abi_path = (
        config_data['networks'][eth_net]['bridges'][aergo_net]['bridge_abi'])
    with open(root_path + bridge_abi_path, "r") as f:
        bridge_abi = f.read()
    eth_bridge_addr = (
        config_data['networks'][eth_net]['bridges'][aergo_net]['addr'])
    # eth oracle
    oracle_abi_path = (
        config_data['networks'][eth_net]['bridges'][aergo_net]['oracle_abi'])
    with open(root_path + oracle_abi_path, "r") as f:
        oracle_abi = f.read()
    eth_oracle_addr = (
        config_data['networks'][eth_net]['bridges'][aergo_net]['oracle'])
    # aergo contracts
    aergo_bridge = (
        config_data['networks'][aergo_net]['bridges'][eth_net]['addr'])
    aergo_oracle = (
        config_data['networks'][aergo_net]['bridges'][eth_net]['oracle'])

    # check validators are correct and warn the validator will vote for
    # a new validator set
    aergo_vals = query_aergo_validators(hera, aergo_oracle)
    eth_vals = query_eth_validators(web3, eth_oracle_addr, oracle_abi)
    logger.info("\"Current Aergo validators : %s\"", aergo_vals)
    logger.info("\"Current Ethereum validators : %s\"", eth_vals)
    # get the current t_anchor and t_final for both sides of bridge
    t_anchor_aergo, t_final_aergo = query_aergo_tempo(hera, aergo_bridge)
    t_anchor_eth, t_final_eth = query_eth_tempo(web3, eth_bridge_addr,
                                                bridge_abi)
    logger.info("\"%s <- %s (t_final=%s) : t_anchor=%s\"", aergo_net, eth_net,
                t_final_aergo, t_anchor_aergo)
    logger.info("\"%s (t_final=%s) -> %s : t_anchor=%s\"", aergo_net,
                t_final_eth, eth_net, t_anchor_eth)

    if auto_update:
        if oracle_update:
            logger.warning(
                "\"WARNING: This validator will vote for updating the oracle\""
            )
        logger.warning(
            "\"WARNING: This validator will vote for settings update in "
            "config.json\"")
        if len(aergo_vals) != len(eth_vals):
            logger.warning(
                "\"WARNING: different number of validators on both sides of "
                "the bridge\"")
        if len(config_data['validators']) != len(aergo_vals):
            logger.warning(
                "\"WARNING: This validator is voting for a new set of aergo "
                "validators\"")
        if len(config_data['validators']) != len(eth_vals):
            logger.warning(
                "\"WARNING: This validator is voting for a new set of eth "
                "validators\"")
        for i, validator in enumerate(config_data['validators']):
            try:
                if validator['addr'] != aergo_vals[i]:
                    logger.warning(
                        "\"WARNING: This validator is voting for a new set of "
                        "aergo validators\"")
            except IndexError:
                # new validators index larger than current validators
                pass
            try:
                if validator['eth-addr'] != eth_vals[i]:
                    logger.warning(
                        "\"WARNING: This validator is voting for a new set of "
                        "eth validators\"")
            except IndexError:
                # new validators index larger than current validators
                pass

        t_anchor_aergo_c = (
            config_data['networks'][aergo_net]['bridges'][eth_net]['t_anchor'])
        t_final_aergo_c = (
            config_data['networks'][aergo_net]['bridges'][eth_net]['t_final'])
        t_anchor_eth_c = (
            config_data['networks'][eth_net]['bridges'][aergo_net]['t_anchor'])
        t_final_eth_c = (
            config_data['networks'][eth_net]['bridges'][aergo_net]['t_final'])
        if t_anchor_aergo_c != t_anchor_aergo:
            logger.warning(
                "\"WARNING: This validator is voting to update anchoring"
                " periode on aergo\"")
        if t_final_aergo_c != t_final_aergo:
            logger.warning(
                "\"WARNING: This validator is voting to update finality of eth"
                " on aergo\"")
        if t_anchor_eth_c != t_anchor_eth:
            logger.warning(
                "\"WARNING: This validator is voting to update anchoring"
                " periode on eth\"")
        if t_final_eth_c != t_final_eth:
            logger.warning(
                "\"WARNING: This validator is voting to update finality of"
                " aergo on eth\"")

    aergo_id = query_aergo_id(hera, aergo_oracle)
    eth_id = query_eth_id(web3, eth_oracle_addr, oracle_abi)

    return aergo_id, eth_id