B4: chain tables, HD index assignment and signing key reserved to the worker (DAT-81..84, ARC-78)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
davideandClaude Sonnet 5.5 committed 2026-11-16 15:53:50 +01:00
1 parent 0092596b07
commit f1d09075a1
18 files changed
+741 -8

No files matched your search

@@ -0,0 +1,110 @@
# Generated by Django 5.2.19 on 2026-11-16 14:50
import django.db.models.deletion
import django.db.models.functions.text
import django.db.models.lookups
import django.utils.timezone
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('lottery', '0003_console_and_reports'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Utxo',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('txid', models.CharField(max_length=64)),
('vout', models.PositiveIntegerField()),
('amount_sats', models.BigIntegerField()),
('address', models.CharField(max_length=128)),
('confirmed_height', models.PositiveIntegerField(blank=True, null=True)),
('spent_txid', models.CharField(blank=True, max_length=64, null=True)),
('created_at', models.DateTimeField(default=django.utils.timezone.now)),
('reserved_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='reserved_utxos', to='lottery.chaintransaction')),
('round', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='utxos', to='lottery.round')),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='utxos', to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Deposit',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('status', models.CharField(choices=[('detected', 'Detected'), ('credited', 'Credited'), ('orphaned', 'Orphaned')], default='detected', max_length=16)),
('detected_at', models.DateTimeField(default=django.utils.timezone.now)),
('credited_at', models.DateTimeField(blank=True, null=True)),
('movement', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='deposit', to='lottery.movement')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='deposits', to=settings.AUTH_USER_MODEL)),
('utxo', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='deposit', to='lottery.utxo')),
],
),
migrations.CreateModel(
name='DepositAddress',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('derivation_index', models.PositiveIntegerField(unique=True)),
('address', models.CharField(max_length=128, unique=True)),
('network', models.CharField(max_length=16)),
('created_at', models.DateTimeField(default=django.utils.timezone.now)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='deposit_address', to=settings.AUTH_USER_MODEL)),
],
options={
'constraints': [models.CheckConstraint(condition=models.Q(('network__in', ['mainnet', 'regtest'])), name='lottery_depositaddress_network_valid')],
},
),
migrations.CreateModel(
name='TransactionVersion',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('txid', models.CharField(max_length=64, unique=True)),
('raw_hex', models.TextField(blank=True, default='')),
('created_at', models.DateTimeField(default=django.utils.timezone.now)),
('transaction', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='versions', to='lottery.chaintransaction')),
],
options={
'constraints': [models.CheckConstraint(condition=django.db.models.lookups.Exact(django.db.models.functions.text.Length('txid'), 64), name='lottery_txversion_txid_length')],
},
),
migrations.AddIndex(
model_name='utxo',
index=models.Index(fields=['address'], name='lottery_utx_address_daa4dd_idx'),
),
migrations.AddIndex(
model_name='utxo',
index=models.Index(fields=['user'], name='lottery_utx_user_id_2ccdc9_idx'),
),
migrations.AddConstraint(
model_name='utxo',
constraint=models.UniqueConstraint(fields=('txid', 'vout'), name='lottery_utxo_outpoint_unique'),
),
migrations.AddConstraint(
model_name='utxo',
constraint=models.CheckConstraint(condition=models.Q(('amount_sats__gt', 0)), name='lottery_utxo_amount_positive'),
),
migrations.AddConstraint(
model_name='utxo',
constraint=models.CheckConstraint(condition=django.db.models.lookups.Exact(django.db.models.functions.text.Length('txid'), 64), name='lottery_utxo_txid_length'),
),
migrations.AddConstraint(
model_name='utxo',
constraint=models.CheckConstraint(condition=models.Q(('spent_txid__isnull', True), django.db.models.lookups.Exact(django.db.models.functions.text.Length('spent_txid'), 64), _connector='OR'), name='lottery_utxo_spent_txid_length'),
),
migrations.AddConstraint(
model_name='utxo',
constraint=models.CheckConstraint(condition=models.Q(('user__isnull', True), ('round__isnull', True), _connector='OR'), name='lottery_utxo_user_or_round'),
),
migrations.AddConstraint(
model_name='deposit',
constraint=models.CheckConstraint(condition=models.Q(('status__in', ['detected', 'credited', 'orphaned'])), name='lottery_deposit_status_valid'),
),
migrations.AddConstraint(
model_name='deposit',
constraint=models.CheckConstraint(condition=models.Q(models.Q(('status', 'credited'), _negated=True), models.Q(('movement__isnull', False), ('credited_at__isnull', False)), _connector='OR'), name='lottery_deposit_credited_has_movement'),
),
]
@@ -0,0 +1,20 @@
"""Transaction versions are append-only in the database too (DAT-83): a raw `UPDATE` or `DELETE` is refused.
SQLite syntax, like the audit triggers (ADR-55)."""
from django.db import migrations
CREATE = [
"""CREATE TRIGGER lottery_txversion_no_update BEFORE UPDATE ON lottery_transactionversion
BEGIN SELECT RAISE(ABORT, 'transaction versions are append-only'); END""",
"""CREATE TRIGGER lottery_txversion_no_delete BEFORE DELETE ON lottery_transactionversion
BEGIN SELECT RAISE(ABORT, 'transaction versions are append-only'); END""",
]
DROP = [
"DROP TRIGGER IF EXISTS lottery_txversion_no_update",
"DROP TRIGGER IF EXISTS lottery_txversion_no_delete",
]
class Migration(migrations.Migration):
dependencies = [("lottery", "0004_chain_tables")]
operations = [migrations.RunSQL(sql=CREATE, reverse_sql=DROP)]