Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ddk-manager/src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,26 @@ impl Contract {
}
}

/// Get the fee rate per virtual byte for a contract.
pub fn get_fee_rate_per_vb(&self) -> u64 {
match self {
Contract::Offered(o) | Contract::Rejected(o) => o.fee_rate_per_vb,
Contract::Accepted(a) => a.offered_contract.fee_rate_per_vb,
Contract::Signed(s) | Contract::Confirmed(s) | Contract::Refunded(s) => {
s.accepted_contract.offered_contract.fee_rate_per_vb
}
Contract::PreClosed(p) => {
p.signed_contract
.accepted_contract
.offered_contract
.fee_rate_per_vb
}
Contract::FailedAccept(f) => f.offered_contract.fee_rate_per_vb,
Contract::FailedSign(f) => f.accepted_contract.offered_contract.fee_rate_per_vb,
Contract::Closed(_) => 0,
}
}

/// Get the profit and loss for a contract.
pub fn get_pnl(&self) -> SignedAmount {
match self {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE block DROP CONSTRAINT block_pkey;
ALTER TABLE block ADD PRIMARY KEY (wallet_name, hash);
CREATE INDEX idx_block_height ON block (height);
-- NOT VALID: anchors may reference block rows that no longer exist.
ALTER TABLE anchor_tx
ADD FOREIGN KEY (wallet_name, block_hash)
REFERENCES block (wallet_name, hash) NOT VALID;
52 changes: 52 additions & 0 deletions ddk/src/storage/postgres/migrations/0004_block_by_height.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-- BDK's local chain is a map of height -> block hash: a reorg REPLACES the
-- hash at a height. Keying blocks by (wallet_name, hash) let the old and the
-- new hash accumulate at the same height, and the reader picked one of them
-- nondeterministically. Re-key the table by (wallet_name, height).

-- Anchors carry their full anchor block inside the JSONB payload and may
-- legitimately reference blocks that are no longer part of the sparse local
-- chain after a reorg, so anchor_tx must not require a matching block row;
-- the constraint also made reorg deletes of block rows fail and roll back
-- the whole persist transaction.
DO $$
DECLARE r RECORD;
BEGIN
FOR r IN
SELECT conname
FROM pg_constraint
WHERE conrelid = 'anchor_tx'::regclass
AND contype = 'f'
AND confrelid = 'block'::regclass
LOOP
EXECUTE format('ALTER TABLE anchor_tx DROP CONSTRAINT %I', r.conname);
END LOOP;
END $$;

-- Deduplicate reorg leftovers. Prefer the hash an anchor still references;
-- tie-break on the greater hash. The next wallet sync repairs a wrong pick.
DELETE FROM block b
WHERE NOT EXISTS (
SELECT 1 FROM anchor_tx a
WHERE a.wallet_name = b.wallet_name AND a.block_hash = b.hash)
AND EXISTS (
SELECT 1
FROM block b2
JOIN anchor_tx a2
ON a2.wallet_name = b2.wallet_name AND a2.block_hash = b2.hash
WHERE b2.wallet_name = b.wallet_name
AND b2.height = b.height
AND b2.hash <> b.hash);

DELETE FROM block b
WHERE EXISTS (
SELECT 1 FROM block b2
WHERE b2.wallet_name = b.wallet_name
AND b2.height = b.height
AND b2.hash > b.hash);

ALTER TABLE block DROP CONSTRAINT block_pkey;
ALTER TABLE block ADD PRIMARY KEY (wallet_name, height);

-- Redundant now: the primary key covers (wallet_name, height) and every query
-- filters on wallet_name first.
DROP INDEX IF EXISTS idx_block_height;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP TABLE IF EXISTS spk_cache;
ALTER TABLE tx DROP COLUMN IF EXISTS last_evicted;
ALTER TABLE tx DROP COLUMN IF EXISTS first_seen;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- BDK's tx_graph changeset also carries first_seen (first time a tx was seen
-- in the mempool) and last_evicted (last time it was missing from the
-- mempool). Both were silently dropped on persist; losing last_evicted can
-- resurrect an RBF-replaced or evicted transaction as unconfirmed after a
-- restart.
ALTER TABLE tx ADD COLUMN first_seen BIGINT;
ALTER TABLE tx ADD COLUMN last_evicted BIGINT;

-- The keychain indexer changeset carries a cache of derived script pubkeys.
CREATE TABLE spk_cache (
wallet_name TEXT NOT NULL,
descriptor_id BYTEA NOT NULL,
spk_index INTEGER NOT NULL,
script BYTEA NOT NULL,
PRIMARY KEY (wallet_name, descriptor_id, spk_index)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE keychain ALTER COLUMN last_revealed SET DEFAULT 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- A fresh keychain row read back last_revealed = 0, which BDK interprets as
-- "derivation index 0 has been revealed", so the wallet skipped its first
-- address. NULL is the correct "nothing revealed yet" value. Existing rows at
-- 0 are ambiguous (genuinely revealed index 0, or never revealed) and are left
-- untouched; update_last_revealed only clamps upward, so the cost is at most
-- one skipped address on a wallet that never revealed anything.
ALTER TABLE keychain ALTER COLUMN last_revealed DROP DEFAULT;
Loading
Loading