281 lines
12 KiB
Python
281 lines
12 KiB
Python
"""B4 — chain tables and who holds the keys: DAT-81..DAT-84, TX-19, ARC-72, ARC-78, ADR-58."""
|
|
|
|
import json
|
|
import os
|
|
import stat
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from django.contrib.auth import get_user_model
|
|
from django.core.management import call_command
|
|
from django.db import IntegrityError, connection, transaction
|
|
from embit.bip32 import HDKey
|
|
from plm_engine import hd
|
|
from plm_engine.network import MAINNET, REGTEST
|
|
|
|
from apps.lottery import chain, keys
|
|
from apps.lottery.addresses import participant_address
|
|
from apps.lottery.models import (
|
|
ChainTransaction,
|
|
Deposit,
|
|
DepositAddress,
|
|
Movement,
|
|
Round,
|
|
TransactionVersion,
|
|
TransactionVersionImmutable,
|
|
Utxo,
|
|
)
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
User = get_user_model()
|
|
TXID = "ab" * 32
|
|
OTHER = "cd" * 32
|
|
|
|
|
|
@pytest.fixture
|
|
def xpub(settings, tmp_path):
|
|
"""A fresh master key, its public account key configured as the web process would have it."""
|
|
settings.PLM_ACCOUNT_XPUB = keys.generate_keys(tmp_path / "keys", settings.PLM_PROFILE)
|
|
return settings.PLM_ACCOUNT_XPUB
|
|
|
|
|
|
def make_user(name="mario"):
|
|
return User.objects.create_user(name, password="x" * 12)
|
|
|
|
|
|
def make_tx(user=None):
|
|
return ChainTransaction.objects.create(kind="bet", user=user or make_user("tx-owner"))
|
|
|
|
|
|
# --- DAT-81: addresses and HD indexes ------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_a_player_gets_one_address_and_always_the_same(xpub, settings):
|
|
user = make_user()
|
|
first = chain.assign_deposit_address(user)
|
|
assert first.derivation_index == 0 and first.address.startswith(settings.PLM_PROFILE.hrp + "1")
|
|
assert chain.assign_deposit_address(user) == first and DepositAddress.objects.count() == 1
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_two_players_never_share_an_index(xpub):
|
|
a, b = chain.assign_deposit_address(make_user("a")), chain.assign_deposit_address(make_user("b"))
|
|
assert (a.derivation_index, b.derivation_index) == (0, 1) and a.address != b.address
|
|
with pytest.raises(IntegrityError), transaction.atomic(): # the database decides, not the code
|
|
DepositAddress.objects.create(user=make_user("c"), derivation_index=1, address="rplm1other", network="regtest")
|
|
|
|
|
|
_ASSIGN = """
|
|
import django, json, sys
|
|
django.setup()
|
|
from django.contrib.auth import get_user_model
|
|
from apps.lottery.chain import assign_deposit_address
|
|
user = get_user_model().objects.get(username=sys.argv[1])
|
|
print(json.dumps(assign_deposit_address(user).derivation_index))
|
|
"""
|
|
|
|
|
|
def test_concurrent_assignments_all_get_distinct_indexes(tmp_path, settings):
|
|
"""ARC-72, DAT-81: six processes ask for an address at once on one database file; six different indexes."""
|
|
env = {"PATH": os.environ["PATH"], "PYTHONPATH": str(ROOT), "DJANGO_SECRET_KEY": "k" * 60,
|
|
"DJANGO_DB_PATH": str(tmp_path / "site.sqlite3"), "DJANGO_SETTINGS_MODULE": "config.settings",
|
|
"PLM_ACCOUNT_XPUB": keys.generate_keys(tmp_path / "keys", settings.PLM_PROFILE)}
|
|
subprocess.run([sys.executable, "manage.py", "migrate", "--noinput", "-v", "0"], cwd=ROOT, env=env, check=True)
|
|
names = [f"p{i}" for i in range(6)]
|
|
setup = "import django; django.setup()\nfrom django.contrib.auth import get_user_model as g\n" + "".join(
|
|
f"g().objects.create_user({n!r}, password='x'*12)\n" for n in names)
|
|
subprocess.run([sys.executable, "-c", setup], cwd=ROOT, env=env, check=True)
|
|
procs = [subprocess.Popen([sys.executable, "-c", _ASSIGN, n], cwd=ROOT, env=env, stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE, text=True) for n in names]
|
|
outputs = [p.communicate(timeout=120) for p in procs]
|
|
assert all(p.returncode == 0 for p in procs), [o[1] for o in outputs]
|
|
assert sorted(json.loads(o[0].strip().splitlines()[-1]) for o in outputs) == list(range(6))
|
|
|
|
|
|
def test_derivation_is_identical_for_the_same_input():
|
|
master = HDKey.from_seed(bytes(range(32)), version=REGTEST.embit["xprv"])
|
|
private = hd.account_key(master, REGTEST)
|
|
public = HDKey.from_base58(private.to_public().to_base58(version=REGTEST.embit["xpub"]))
|
|
for index in (0, 1, 41):
|
|
assert hd.user_address(public, index, REGTEST) == hd.user_address(private, index, REGTEST)
|
|
assert hd.user_address(public, index, REGTEST) == hd.user_address(public, index, REGTEST)
|
|
assert hd.pool_address(public, 7, REGTEST) == hd.pool_address(private, 7, REGTEST)
|
|
assert hd.user_address(public, 0, REGTEST) != hd.user_address(public, 1, REGTEST)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_the_participant_address_on_a_real_ledger_is_the_deposit_address(xpub, settings):
|
|
settings.LEDGER_BACKEND = "chain"
|
|
user = make_user()
|
|
assert participant_address(user) == chain.assign_deposit_address(user).address
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_assigning_without_a_public_key_fails_loudly(settings):
|
|
settings.PLM_ACCOUNT_XPUB = ""
|
|
with pytest.raises(keys.SigningKeyUnavailable):
|
|
chain.assign_deposit_address(make_user())
|
|
assert not DepositAddress.objects.exists()
|
|
|
|
|
|
# --- DAT-82: UTXO ----------------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_an_outpoint_is_stored_once():
|
|
Utxo.objects.create(txid=TXID, vout=0, amount_sats=5, address="rplm1a")
|
|
Utxo.objects.create(txid=TXID, vout=1, amount_sats=5, address="rplm1a")
|
|
with pytest.raises(IntegrityError), transaction.atomic():
|
|
Utxo.objects.create(txid=TXID, vout=0, amount_sats=7, address="rplm1a")
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_a_utxo_belongs_to_a_player_or_a_round_never_both():
|
|
Utxo.objects.create(txid=TXID, vout=0, amount_sats=5, address="a", user=make_user())
|
|
round_ = Round.objects.create()
|
|
Utxo.objects.create(txid=TXID, vout=1, amount_sats=5, address="a", round=round_)
|
|
with pytest.raises(IntegrityError), transaction.atomic():
|
|
Utxo.objects.create(txid=TXID, vout=2, amount_sats=5, address="a", user=make_user("x"), round=round_)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@pytest.mark.parametrize("fields", [{"amount_sats": 0}, {"amount_sats": -1}, {"txid": "ab" * 31}, {"spent_txid": "short"}])
|
|
def test_amounts_and_txids_are_checked_by_the_database(fields):
|
|
data = {"txid": TXID, "vout": 0, "amount_sats": 5, "address": "a", **fields}
|
|
with pytest.raises(IntegrityError), transaction.atomic():
|
|
Utxo.objects.create(**data)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_a_utxo_can_be_reserved_by_a_transaction_and_the_transaction_is_protected():
|
|
tx = make_tx()
|
|
Utxo.objects.create(txid=TXID, vout=0, amount_sats=5, address="a", reserved_by=tx)
|
|
from django.db.models import ProtectedError
|
|
|
|
with pytest.raises(ProtectedError):
|
|
tx.delete()
|
|
|
|
|
|
# --- DAT-83: versions ---------------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_every_version_of_a_transaction_is_kept():
|
|
tx = make_tx()
|
|
chain.register_version(tx, TXID, "00aa")
|
|
chain.register_version(tx, OTHER, "00bb") # an RBF replacement
|
|
tx.refresh_from_db()
|
|
assert tx.current_txid == OTHER and tx.raw_hex == "00bb"
|
|
assert set(tx.versions.values_list("txid", flat=True)) == {TXID, OTHER}
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_registering_a_version_twice_is_one_version():
|
|
tx = make_tx()
|
|
chain.register_version(tx, TXID, "00aa")
|
|
chain.register_version(tx, TXID, "00aa")
|
|
assert tx.versions.count() == 1
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_a_txid_belongs_to_one_transaction():
|
|
chain.register_version(make_tx(), TXID, "00aa")
|
|
with pytest.raises(chain.TxidConflict):
|
|
chain.register_version(make_tx(make_user("other")), TXID, "00aa")
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_versions_cannot_be_changed_or_deleted():
|
|
version = chain.register_version(make_tx(), TXID, "00aa")
|
|
with pytest.raises(TransactionVersionImmutable):
|
|
version.raw_hex = "ff"
|
|
version.save()
|
|
with pytest.raises(TransactionVersionImmutable):
|
|
version.delete()
|
|
with pytest.raises(TransactionVersionImmutable):
|
|
TransactionVersion.objects.all().update(raw_hex="")
|
|
with pytest.raises(Exception, match="append-only"), transaction.atomic(): # and so does the database
|
|
with connection.cursor() as cursor:
|
|
cursor.execute("UPDATE lottery_transactionversion SET raw_hex = 'x'")
|
|
with pytest.raises(Exception, match="append-only"), transaction.atomic():
|
|
with connection.cursor() as cursor:
|
|
cursor.execute("DELETE FROM lottery_transactionversion")
|
|
|
|
|
|
# --- DAT-84: deposits -------------------------------------------------------------------------------------
|
|
|
|
|
|
def make_utxo(user, vout=0):
|
|
return Utxo.objects.create(txid=TXID, vout=vout, amount_sats=100, address="a", user=user)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_an_output_is_one_deposit():
|
|
user = make_user()
|
|
utxo = make_utxo(user)
|
|
Deposit.objects.create(user=user, utxo=utxo)
|
|
with pytest.raises(IntegrityError), transaction.atomic():
|
|
Deposit.objects.create(user=user, utxo=utxo)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_a_credited_deposit_has_its_movement_and_date():
|
|
from django.utils import timezone
|
|
|
|
user = make_user()
|
|
with pytest.raises(IntegrityError), transaction.atomic():
|
|
Deposit.objects.create(user=user, utxo=make_utxo(user), status="credited")
|
|
movement = Movement.objects.create(user=user, kind="deposit", status="confirmed", amount_sats=100, confirmed_at=timezone.now())
|
|
ok = Deposit.objects.create(user=user, utxo=make_utxo(user, 1), status="credited", movement=movement, credited_at=timezone.now())
|
|
assert ok.movement == movement
|
|
|
|
|
|
# --- ARC-78: who holds the keys ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_the_web_process_cannot_load_the_signing_key(settings, tmp_path):
|
|
keys.generate_keys(tmp_path / "keys", settings.PLM_PROFILE)
|
|
settings.PLM_MASTER_KEY_FILE = str(tmp_path / "keys" / "master.key")
|
|
settings.PLM_KEY_ENCRYPTION_KEY_FILE = str(tmp_path / "keys" / "master.encryption.key")
|
|
settings.PROCESS_ROLE = "web" # the files are there, and it still refuses
|
|
with pytest.raises(keys.SigningKeyUnavailable):
|
|
keys.signing_account_key()
|
|
|
|
|
|
def test_the_worker_can_and_the_files_are_private(settings, tmp_path):
|
|
xpub = keys.generate_keys(tmp_path / "keys", settings.PLM_PROFILE)
|
|
for name in ("master.key", "master.encryption.key"):
|
|
assert stat.S_IMODE(os.stat(tmp_path / "keys" / name).st_mode) == 0o600
|
|
settings.PLM_MASTER_KEY_FILE = str(tmp_path / "keys" / "master.key")
|
|
settings.PLM_KEY_ENCRYPTION_KEY_FILE = str(tmp_path / "keys" / "master.encryption.key")
|
|
settings.PROCESS_ROLE = "worker"
|
|
private = keys.signing_account_key()
|
|
# the private key and the published public key are the same account
|
|
public = HDKey.from_base58(xpub)
|
|
assert hd.user_address(private, 3, settings.PLM_PROFILE) == hd.user_address(public, 3, settings.PLM_PROFILE)
|
|
|
|
|
|
def test_a_wrong_encryption_key_does_not_load(settings, tmp_path):
|
|
keys.generate_keys(tmp_path / "a", settings.PLM_PROFILE)
|
|
keys.generate_keys(tmp_path / "b", settings.PLM_PROFILE)
|
|
settings.PLM_MASTER_KEY_FILE = str(tmp_path / "a" / "master.key")
|
|
settings.PLM_KEY_ENCRYPTION_KEY_FILE = str(tmp_path / "b" / "master.encryption.key")
|
|
settings.PROCESS_ROLE = "worker"
|
|
with pytest.raises(keys.SigningKeyUnavailable):
|
|
keys.signing_account_key()
|
|
|
|
|
|
def test_keys_are_never_overwritten(settings, tmp_path):
|
|
keys.generate_keys(tmp_path / "keys", settings.PLM_PROFILE)
|
|
with pytest.raises(FileExistsError):
|
|
keys.generate_keys(tmp_path / "keys", settings.PLM_PROFILE)
|
|
|
|
|
|
def test_init_keys_prints_only_the_public_key(settings, tmp_path, capsys):
|
|
call_command("init_keys", str(tmp_path / "k"))
|
|
out = capsys.readouterr().out
|
|
assert "PLM_ACCOUNT_XPUB=tpub" in out and "xprv" not in out and "tprv" not in out
|