Exemplo n.º 1
0
def test_raises_error_if_primary_and_backup_both_failed():
    executor = ThreadPoolExecutor(2)
    client = jsonrpc.Client("url",
                            rs=jsonrpc.RequestWithBackups(backups=["url"],
                                                          executor=executor))
    with pytest.raises(jsonrpc.NetworkError):
        assert client.get_currencies()

    client = jsonrpc.Client("url",
                            rs=jsonrpc.RequestWithBackups(backups=["url"],
                                                          executor=executor,
                                                          fallback=True))
    with pytest.raises(jsonrpc.NetworkError):
        assert client.get_currencies()
Exemplo n.º 2
0
def test_update_last_known_state():
    client = jsonrpc.Client("url")
    assert client.get_last_known_state() is not None

    client.update_last_known_state(2, 2, 2)
    assert client.get_last_known_state() is not None
    assert client.get_last_known_state().chain_id == 2
    assert client.get_last_known_state().version == 2
    assert client.get_last_known_state().timestamp_usecs == 2

    # chain id mismatch will raise invalid server response instead of
    # stale response error
    with pytest.raises(jsonrpc.InvalidServerResponse):
        client.update_last_known_state(1, 1, 1)

    with pytest.raises(jsonrpc.StaleResponseError):
        client.update_last_known_state(2, 1, 2)
    with pytest.raises(jsonrpc.StaleResponseError):
        client.update_last_known_state(2, 2, 1)
    with pytest.raises(jsonrpc.StaleResponseError):
        client.update_last_known_state(2, 1, 1)

    client.update_last_known_state(2, 2, 2)
    assert client.get_last_known_state().chain_id == 2
    assert client.get_last_known_state().version == 2
    assert client.get_last_known_state().timestamp_usecs == 2

    client.update_last_known_state(2, 3, 3)
    assert client.get_last_known_state().chain_id == 2
    assert client.get_last_known_state().version == 3
    assert client.get_last_known_state().timestamp_usecs == 3
Exemplo n.º 3
0
def get_supported_network_currencies() -> Tuple[str]:
    # TODO - error handling
    api = jsonrpc.Client(JSON_RPC_URL)
    supported_currency_info = api.get_currencies()

    return tuple(_.code for _ in supported_currency_info
                 if _.code == DiemCurrency.XUS)
Exemplo n.º 4
0
    def __init__(self):
        liquidity_custody_account_name = os.getenv(
            "LIQUIDITY_CUSTODY_ACCOUNT_NAME", "liquidity")
        self.chain_id = int(os.environ["CHAIN_ID"])
        self.vasp = Vasp(jsonrpc.Client(os.environ["JSON_RPC_URL"]),
                         liquidity_custody_account_name)

        super().__init__(self.vasp.address_str)
Exemplo n.º 5
0
def test_fallback_to_backups_when_primary_failed():
    client = jsonrpc.Client(
        "primary",
        rs=jsonrpc.RequestWithBackups(backups=["backup"],
                                      executor=ThreadPoolExecutor(2),
                                      fallback=True),
    )
    client._send_http_request = gen_metadata_response(client, fail="primary")
    for _ in range(10):
        assert client.get_metadata().script_hash_allow_list == ["backup"]
Exemplo n.º 6
0
def test_fallback_to_second_if_first_failed():
    executor = ThreadPoolExecutor(2)
    rs = jsonrpc.RequestWithBackups(backups=["backup"], executor=executor)
    client = jsonrpc.Client("primary", rs=rs)

    # primary will fail immediately, backup is slow but success
    client._send_http_request = gen_metadata_response(client,
                                                      fail="primary",
                                                      snap="backup")
    assert client.get_metadata().script_hash_allow_list == ["backup"]
    executor.shutdown()
Exemplo n.º 7
0
def test_fallback_strategy_always_returns_primary_response_if_it_successes():
    client = jsonrpc.Client(
        "primary",
        rs=jsonrpc.RequestWithBackups(backups=["backup"],
                                      executor=ThreadPoolExecutor(2),
                                      fallback=True),
    )
    client._send_http_request = gen_metadata_response(client)
    for _ in range(10):
        metadata = client.get_metadata()
        assert metadata.script_hash_allow_list == ["primary"]
Exemplo n.º 8
0
def test_first_success_strategy_returns_first_completed_success_response():
    executor = ThreadPoolExecutor(2)
    rs = jsonrpc.RequestWithBackups(backups=["backup"], executor=executor)
    client = jsonrpc.Client("primary", rs=rs)

    client._send_http_request = gen_metadata_response(client, snap="primary")
    assert client.get_metadata().script_hash_allow_list == ["backup"]

    client._send_http_request = gen_metadata_response(client, snap="backup")
    assert client.get_metadata().script_hash_allow_list == ["primary"]
    executor.shutdown()
Exemplo n.º 9
0
    def __init__(self, config: Dict[str, Any]) -> None:
        self.sync_interval_ms = config["sync_interval_ms"]
        self.accounts = config["accounts"]

        self.diem_node_uri = config["diem_node_uri"]
        self.progress_file_path = config["progress_file_path"]
        self.fetch_batch_size = 10
        self.processor = config.get("processor", process_incoming_txn)

        logger.info(f"Loaded LRWPubSubClient with config: {config}")

        self.client = jsonrpc.Client(self.diem_node_uri)
        self.progress = FileProgressStorage(self.progress_file_path)
Exemplo n.º 10
0
def sync_db():
    client = jsonrpc.Client(JSON_RPC_URL)
    onchain_account = client.get_account(VASP_ADDRESS)

    up_to_version = (
        Transaction.query.filter_by(type=TransactionType.EXTERNAL)
        .order_by(desc(Transaction.blockchain_version))
        .first()
        .blockchain_version
    )

    if sync_required(onchain_account, up_to_version):
        sync(client, onchain_account, up_to_version)
    else:
        logger.info("balances equal, no synchronization required")
Exemplo n.º 11
0
def sync_db():
    client = jsonrpc.Client(JSON_RPC_URL)
    up_to_version = client.get_metadata().version

    onchain_account = client.get_account(VASP_ADDRESS)

    transactions_in_db = Transaction.query.filter_by(
        type=TransactionType.EXTERNAL)
    count = transactions_in_db.count()

    if count > 0:
        db_latest_version = (transactions_in_db.order_by(
            desc(Transaction.blockchain_version)).first().blockchain_version)
        if db_latest_version is not None:
            up_to_version = db_latest_version

    if sync_required(onchain_account, up_to_version):
        sync(client, onchain_account, up_to_version)
    else:
        logger.info("balances equal, no synchronization required")
Exemplo n.º 12
0
def from_config(config: config.Config) -> Context:
    return Context(
        config=config,
        jsonrpc_client=jsonrpc.Client(config.json_rpc_url),
        custody=stubs.custody.from_env(),
    )
    args = parser.parse_args()
    private_key = args.private_key
    auth_key = args.auth_key
    refill_amount_human = args.amount
    threshold_amount_human = args.threshold
    currencies = args.currencies
    network_chainid = args.network_chainid
    network_json_rpc_url = args.network_json_rpc_url
    sleep_interval = args.sleep_interval

if auth_key is None:
    print("Must get an authentication key to watch!")
    sys.exit(1)

diem_client = jsonrpc.Client(network_json_rpc_url)

watched_account_auth_key = AuthKey(bytes.fromhex(auth_key))
watched_account_addr = watched_account_auth_key.account_address()
watched_account_addr_hex = utils.account_address_hex(watched_account_addr)

refill_amount = int(refill_amount_human) * 1_000_000  # to microdiem
threshold_amount = int(threshold_amount_human) * 1_000_000  # to microdiem
sleep_interval = float(sleep_interval)

while True:
    print(f'current datetime: {datetime.now().isoformat().replace("T", " ")}')

    for currency in currencies:
        currency_balance = next(
            b.amount
Exemplo n.º 14
0
from diem import jsonrpc, testnet, LocalAccount, utils, stdlib, diem_types
import time
client = jsonrpc.Client(testnet.JSON_RPC_URL)


## wallet method: GETBALANCE
def getBalance(address):
    info = client.get_account(address)
    return info.balances


def listAccount(parent_vasp, child_vasp, initialBalance):
    currency = testnet.TEST_CURRENCY_CODE
    seq_num = client.get_account_sequence(parent_vasp.account_address)
    raw_txn = diem_types.RawTransaction(
        sender=parent_vasp.account_address,
        sequence_number=seq_num,
        payload=diem_types.TransactionPayload__Script(
            stdlib.encode_create_child_vasp_account_script(
                coin_type=utils.currency_code(currency),
                child_address=child_vasp.account_address,
                auth_key_prefix=child_vasp.auth_key.prefix(),
                add_all_currencies=False,
                child_initial_balance=initialBalance,
            )),
        max_gas_amount=1_000_000,
        gas_unit_price=0,
        gas_currency_code=currency,
        expiration_timestamp_secs=int(time.time()) + 30,
        chain_id=testnet.CHAIN_ID,
    )
Exemplo n.º 15
0
def to_compact_json(d: dict) -> str:
    return json.dumps(d, separators=(",", ":"))


if __name__ == "__main__":
    chain = os.getenv("BLOCKCHAIN", "testnet")

    if chain == "premainnet":
        print(f"Configuring for premainnet (chain ID: {PREMAINNET_CHAIN_ID})")

        vasp_private_key = os.environ["LIQUIDITY_ACCOUNT_PRIVATE_KEY"]
        account = LocalAccount.from_private_key_hex(vasp_private_key)

        json_rpc_url = "https://premainnet.diem.com/v1"
        jsonrpc_client = jsonrpc.Client(json_rpc_url)
        jsonrpc_client.must_get_account(account.account_address)
        print("Premainnet account is valid")

        lp_custody_private_keys = to_compact_json(
            {liquidity_account_name: vasp_private_key}
        )

        print(f"Creating {ENV_FILE_NAME} file")
        with open(ENV_FILE_NAME, "w") as dotenv:
            dotenv.write(f"LIQUIDITY_CUSTODY_ACCOUNT_NAME={liquidity_account_name}\n")
            dotenv.write(f"CUSTODY_PRIVATE_KEYS={lp_custody_private_keys}\n")
            dotenv.write(f"LIQUIDITY_VASP_ADDR={account.account_address.to_hex()}\n")
            dotenv.write(f"JSON_RPC_URL={json_rpc_url}\n")
            dotenv.write(f"CHAIN_ID={PREMAINNET_CHAIN_ID}\n")
    elif chain == "testnet":
Exemplo n.º 16
0
def test_invalid_server_url():
    client = jsonrpc.Client("url")
    with pytest.raises(jsonrpc.NetworkError):
        client.get_currencies()
Exemplo n.º 17
0
 def __init__(self):
     liquidity_custody_account_name = os.getenv(
         "LIQUIDITY_CUSTODY_ACCOUNT_NAME", "liquidity")
     super().__init__(jsonrpc.Client(JSON_RPC_URL),
                      liquidity_custody_account_name)