diff --git a/packages/kotlin-sdk/sdk/schemas/org.dashfoundation.dashsdk.persistence.DashDatabase/15.json b/packages/kotlin-sdk/sdk/schemas/org.dashfoundation.dashsdk.persistence.DashDatabase/15.json new file mode 100644 index 00000000000..ed27781b3eb --- /dev/null +++ b/packages/kotlin-sdk/sdk/schemas/org.dashfoundation.dashsdk.persistence.DashDatabase/15.json @@ -0,0 +1,4202 @@ +{ + "formatVersion": 1, + "database": { + "version": 15, + "identityHash": "4cdbf8acfcfa07b5745b6aacd7f9d5bb", + "entities": [ + { + "tableName": "wallets", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`walletId` BLOB NOT NULL, `walletGroupId` BLOB NOT NULL, `networkRaw` INTEGER, `name` TEXT, `walletDescription` TEXT, `birthHeight` INTEGER NOT NULL, `syncedHeight` INTEGER NOT NULL, `lastSynced` INTEGER NOT NULL, `lastAppliedChainLockBytes` BLOB, `lastAppliedChainLockHeight` INTEGER, `dashPayBackfillFloor` INTEGER, `dashPayBackfillRewoundFrom` INTEGER, `dashPayBackfillCovered` BLOB, `isImported` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`walletId`))", + "fields": [ + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "walletGroupId", + "columnName": "walletGroupId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER" + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT" + }, + { + "fieldPath": "walletDescription", + "columnName": "walletDescription", + "affinity": "TEXT" + }, + { + "fieldPath": "birthHeight", + "columnName": "birthHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "syncedHeight", + "columnName": "syncedHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSynced", + "columnName": "lastSynced", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastAppliedChainLockBytes", + "columnName": "lastAppliedChainLockBytes", + "affinity": "BLOB" + }, + { + "fieldPath": "lastAppliedChainLockHeight", + "columnName": "lastAppliedChainLockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "dashPayBackfillFloor", + "columnName": "dashPayBackfillFloor", + "affinity": "INTEGER" + }, + { + "fieldPath": "dashPayBackfillRewoundFrom", + "columnName": "dashPayBackfillRewoundFrom", + "affinity": "INTEGER" + }, + { + "fieldPath": "dashPayBackfillCovered", + "columnName": "dashPayBackfillCovered", + "affinity": "BLOB" + }, + { + "fieldPath": "isImported", + "columnName": "isImported", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId" + ] + }, + "indices": [ + { + "name": "index_wallets_networkRaw", + "unique": false, + "columnNames": [ + "networkRaw" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_wallets_networkRaw` ON `${TABLE_NAME}` (`networkRaw`)" + }, + { + "name": "index_wallets_walletGroupId", + "unique": false, + "columnNames": [ + "walletGroupId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_wallets_walletGroupId` ON `${TABLE_NAME}` (`walletGroupId`)" + } + ] + }, + { + "tableName": "accounts", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `walletId` BLOB NOT NULL, `accountType` INTEGER NOT NULL, `accountIndex` INTEGER NOT NULL, `accountTypeName` TEXT NOT NULL, `balanceConfirmed` INTEGER NOT NULL, `balanceUnconfirmed` INTEGER NOT NULL, `externalHighestUsed` INTEGER NOT NULL, `internalHighestUsed` INTEGER NOT NULL, `standardTag` INTEGER NOT NULL, `registrationIndex` INTEGER NOT NULL, `keyClass` INTEGER NOT NULL, `userIdentityId` BLOB NOT NULL, `friendIdentityId` BLOB NOT NULL, `accountExtendedPubKeyBytes` BLOB, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, FOREIGN KEY(`walletId`) REFERENCES `wallets`(`walletId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountType", + "columnName": "accountType", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountTypeName", + "columnName": "accountTypeName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "balanceConfirmed", + "columnName": "balanceConfirmed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "balanceUnconfirmed", + "columnName": "balanceUnconfirmed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "externalHighestUsed", + "columnName": "externalHighestUsed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "internalHighestUsed", + "columnName": "internalHighestUsed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "standardTag", + "columnName": "standardTag", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "registrationIndex", + "columnName": "registrationIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keyClass", + "columnName": "keyClass", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "userIdentityId", + "columnName": "userIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "friendIdentityId", + "columnName": "friendIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountExtendedPubKeyBytes", + "columnName": "accountExtendedPubKeyBytes", + "affinity": "BLOB" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_accounts_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_accounts_walletId` ON `${TABLE_NAME}` (`walletId`)" + }, + { + "name": "index_accounts_walletId_accountType_accountIndex_standardTag_registrationIndex_keyClass_userIdentityId_friendIdentityId", + "unique": true, + "columnNames": [ + "walletId", + "accountType", + "accountIndex", + "standardTag", + "registrationIndex", + "keyClass", + "userIdentityId", + "friendIdentityId" + ], + "orders": [], + "createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_accounts_walletId_accountType_accountIndex_standardTag_registrationIndex_keyClass_userIdentityId_friendIdentityId` ON `${TABLE_NAME}` (`walletId`, `accountType`, `accountIndex`, `standardTag`, `registrationIndex`, `keyClass`, `userIdentityId`, `friendIdentityId`)" + }, + { + "name": "index_accounts_accountExtendedPubKeyBytes", + "unique": true, + "columnNames": [ + "accountExtendedPubKeyBytes" + ], + "orders": [], + "createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_accounts_accountExtendedPubKeyBytes` ON `${TABLE_NAME}` (`accountExtendedPubKeyBytes`)" + } + ], + "foreignKeys": [ + { + "table": "wallets", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "walletId" + ], + "referencedColumns": [ + "walletId" + ] + } + ] + }, + { + "tableName": "transactions", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`txid` BLOB NOT NULL, `transactionData` BLOB NOT NULL, `context` INTEGER NOT NULL, `blockHeight` INTEGER NOT NULL, `blockHash` BLOB, `blockTimestamp` INTEGER NOT NULL, `blockPosition` INTEGER NOT NULL, `hasBlockPosition` INTEGER NOT NULL, `direction` INTEGER NOT NULL, `transactionType` TEXT NOT NULL, `transactionTypeKind` INTEGER NOT NULL, `netAmount` INTEGER NOT NULL, `fee` INTEGER, `label` TEXT NOT NULL, `firstSeen` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`txid`))", + "fields": [ + { + "fieldPath": "txid", + "columnName": "txid", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "transactionData", + "columnName": "transactionData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "context", + "columnName": "context", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "blockHeight", + "columnName": "blockHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "blockHash", + "columnName": "blockHash", + "affinity": "BLOB" + }, + { + "fieldPath": "blockTimestamp", + "columnName": "blockTimestamp", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "blockPosition", + "columnName": "blockPosition", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasBlockPosition", + "columnName": "hasBlockPosition", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "direction", + "columnName": "direction", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "transactionType", + "columnName": "transactionType", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "transactionTypeKind", + "columnName": "transactionTypeKind", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "netAmount", + "columnName": "netAmount", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "fee", + "columnName": "fee", + "affinity": "INTEGER" + }, + { + "fieldPath": "label", + "columnName": "label", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "firstSeen", + "columnName": "firstSeen", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "txid" + ] + }, + "indices": [ + { + "name": "index_transactions_firstSeen", + "unique": false, + "columnNames": [ + "firstSeen" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_transactions_firstSeen` ON `${TABLE_NAME}` (`firstSeen`)" + } + ] + }, + { + "tableName": "transaction_account_involvements", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`transactionTxid` BLOB NOT NULL, `accountId` INTEGER NOT NULL, PRIMARY KEY(`transactionTxid`, `accountId`), FOREIGN KEY(`transactionTxid`) REFERENCES `transactions`(`txid`) ON UPDATE NO ACTION ON DELETE CASCADE , FOREIGN KEY(`accountId`) REFERENCES `accounts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "transactionTxid", + "columnName": "transactionTxid", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountId", + "columnName": "accountId", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "transactionTxid", + "accountId" + ] + }, + "indices": [ + { + "name": "index_transaction_account_involvements_accountId", + "unique": false, + "columnNames": [ + "accountId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_transaction_account_involvements_accountId` ON `${TABLE_NAME}` (`accountId`)" + } + ], + "foreignKeys": [ + { + "table": "transactions", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "transactionTxid" + ], + "referencedColumns": [ + "txid" + ] + }, + { + "table": "accounts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "accountId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "txos", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`outpoint` BLOB NOT NULL, `vout` INTEGER NOT NULL, `amount` INTEGER NOT NULL, `address` TEXT NOT NULL, `scriptPubKey` BLOB NOT NULL, `height` INTEGER NOT NULL, `isCoinbase` INTEGER NOT NULL, `isConfirmed` INTEGER NOT NULL, `isInstantLocked` INTEGER NOT NULL, `isLocked` INTEGER NOT NULL, `isSpent` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, `walletId` BLOB NOT NULL, `txid` BLOB, `spendingTxid` BLOB, `spendingInputIndex` INTEGER, `accountId` INTEGER, `coreAddressId` TEXT, `supersededByTxid` BLOB, PRIMARY KEY(`outpoint`), FOREIGN KEY(`txid`) REFERENCES `transactions`(`txid`) ON UPDATE NO ACTION ON DELETE CASCADE , FOREIGN KEY(`spendingTxid`) REFERENCES `transactions`(`txid`) ON UPDATE NO ACTION ON DELETE SET NULL , FOREIGN KEY(`accountId`) REFERENCES `accounts`(`id`) ON UPDATE NO ACTION ON DELETE SET NULL , FOREIGN KEY(`coreAddressId`) REFERENCES `core_addresses`(`address`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "outpoint", + "columnName": "outpoint", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "vout", + "columnName": "vout", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "amount", + "columnName": "amount", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "address", + "columnName": "address", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "scriptPubKey", + "columnName": "scriptPubKey", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "height", + "columnName": "height", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isCoinbase", + "columnName": "isCoinbase", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isConfirmed", + "columnName": "isConfirmed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isInstantLocked", + "columnName": "isInstantLocked", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isLocked", + "columnName": "isLocked", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isSpent", + "columnName": "isSpent", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "txid", + "columnName": "txid", + "affinity": "BLOB" + }, + { + "fieldPath": "spendingTxid", + "columnName": "spendingTxid", + "affinity": "BLOB" + }, + { + "fieldPath": "spendingInputIndex", + "columnName": "spendingInputIndex", + "affinity": "INTEGER" + }, + { + "fieldPath": "accountId", + "columnName": "accountId", + "affinity": "INTEGER" + }, + { + "fieldPath": "coreAddressId", + "columnName": "coreAddressId", + "affinity": "TEXT" + }, + { + "fieldPath": "supersededByTxid", + "columnName": "supersededByTxid", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "outpoint" + ] + }, + "indices": [ + { + "name": "index_txos_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_txos_walletId` ON `${TABLE_NAME}` (`walletId`)" + }, + { + "name": "index_txos_txid", + "unique": false, + "columnNames": [ + "txid" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_txos_txid` ON `${TABLE_NAME}` (`txid`)" + }, + { + "name": "index_txos_spendingTxid", + "unique": false, + "columnNames": [ + "spendingTxid" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_txos_spendingTxid` ON `${TABLE_NAME}` (`spendingTxid`)" + }, + { + "name": "index_txos_accountId", + "unique": false, + "columnNames": [ + "accountId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_txos_accountId` ON `${TABLE_NAME}` (`accountId`)" + }, + { + "name": "index_txos_coreAddressId", + "unique": false, + "columnNames": [ + "coreAddressId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_txos_coreAddressId` ON `${TABLE_NAME}` (`coreAddressId`)" + } + ], + "foreignKeys": [ + { + "table": "transactions", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "txid" + ], + "referencedColumns": [ + "txid" + ] + }, + { + "table": "transactions", + "onDelete": "SET NULL", + "onUpdate": "NO ACTION", + "columns": [ + "spendingTxid" + ], + "referencedColumns": [ + "txid" + ] + }, + { + "table": "accounts", + "onDelete": "SET NULL", + "onUpdate": "NO ACTION", + "columns": [ + "accountId" + ], + "referencedColumns": [ + "id" + ] + }, + { + "table": "core_addresses", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "coreAddressId" + ], + "referencedColumns": [ + "address" + ] + } + ] + }, + { + "tableName": "core_addresses", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`address` TEXT NOT NULL, `publicKey` BLOB NOT NULL, `poolTypeTag` INTEGER NOT NULL, `addressIndex` INTEGER NOT NULL, `derivationPath` TEXT NOT NULL, `isUsed` INTEGER NOT NULL, `firstSeenHeight` INTEGER NOT NULL, `lastSeenHeight` INTEGER NOT NULL, `balance` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, `accountId` INTEGER, PRIMARY KEY(`address`), FOREIGN KEY(`accountId`) REFERENCES `accounts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "address", + "columnName": "address", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "publicKey", + "columnName": "publicKey", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "poolTypeTag", + "columnName": "poolTypeTag", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "addressIndex", + "columnName": "addressIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "derivationPath", + "columnName": "derivationPath", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "isUsed", + "columnName": "isUsed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "firstSeenHeight", + "columnName": "firstSeenHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSeenHeight", + "columnName": "lastSeenHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "balance", + "columnName": "balance", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountId", + "columnName": "accountId", + "affinity": "INTEGER" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "address" + ] + }, + "indices": [ + { + "name": "index_core_addresses_accountId", + "unique": false, + "columnNames": [ + "accountId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_core_addresses_accountId` ON `${TABLE_NAME}` (`accountId`)" + } + ], + "foreignKeys": [ + { + "table": "accounts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "accountId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "asset_locks", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`outPointHex` TEXT NOT NULL, `walletId` BLOB NOT NULL, `transactionBytes` BLOB NOT NULL, `fundingTypeRaw` INTEGER NOT NULL, `identityIndexRaw` INTEGER NOT NULL, `accountIndexRaw` INTEGER NOT NULL, `amountDuffs` INTEGER NOT NULL, `statusRaw` INTEGER NOT NULL, `proofBytes` BLOB, `recipientPlatformAddressHash` BLOB, `recipientPlatformAddressType` INTEGER, `createdAt` INTEGER NOT NULL, `updatedAt` INTEGER NOT NULL, PRIMARY KEY(`outPointHex`))", + "fields": [ + { + "fieldPath": "outPointHex", + "columnName": "outPointHex", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "transactionBytes", + "columnName": "transactionBytes", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "fundingTypeRaw", + "columnName": "fundingTypeRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "identityIndexRaw", + "columnName": "identityIndexRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountIndexRaw", + "columnName": "accountIndexRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "amountDuffs", + "columnName": "amountDuffs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "statusRaw", + "columnName": "statusRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "proofBytes", + "columnName": "proofBytes", + "affinity": "BLOB" + }, + { + "fieldPath": "recipientPlatformAddressHash", + "columnName": "recipientPlatformAddressHash", + "affinity": "BLOB" + }, + { + "fieldPath": "recipientPlatformAddressType", + "columnName": "recipientPlatformAddressType", + "affinity": "INTEGER" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "updatedAt", + "columnName": "updatedAt", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "outPointHex" + ] + }, + "indices": [ + { + "name": "index_asset_locks_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_asset_locks_walletId` ON `${TABLE_NAME}` (`walletId`)" + } + ] + }, + { + "tableName": "invitations", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`outPointHex` TEXT NOT NULL, `rawOutPoint` BLOB NOT NULL, `walletId` BLOB NOT NULL, `fundingIndexRaw` INTEGER NOT NULL, `amountDuffs` INTEGER NOT NULL, `expiryUnix` INTEGER NOT NULL, `createdAtSecs` INTEGER NOT NULL, `hasInviter` INTEGER NOT NULL, `statusRaw` INTEGER NOT NULL, `reclaimInFlight` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `updatedAt` INTEGER NOT NULL, PRIMARY KEY(`outPointHex`))", + "fields": [ + { + "fieldPath": "outPointHex", + "columnName": "outPointHex", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "rawOutPoint", + "columnName": "rawOutPoint", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "fundingIndexRaw", + "columnName": "fundingIndexRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "amountDuffs", + "columnName": "amountDuffs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "expiryUnix", + "columnName": "expiryUnix", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAtSecs", + "columnName": "createdAtSecs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasInviter", + "columnName": "hasInviter", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "statusRaw", + "columnName": "statusRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "reclaimInFlight", + "columnName": "reclaimInFlight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "updatedAt", + "columnName": "updatedAt", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "outPointHex" + ] + }, + "indices": [ + { + "name": "index_invitations_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_invitations_walletId` ON `${TABLE_NAME}` (`walletId`)" + } + ] + }, + { + "tableName": "identities", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`identityId` BLOB NOT NULL, `balance` INTEGER NOT NULL, `revision` INTEGER NOT NULL, `isLocal` INTEGER NOT NULL, `alias` TEXT, `dpnsName` TEXT, `mainDpnsName` TEXT, `identityType` TEXT NOT NULL, `votingPrivateKeyIdentifier` TEXT, `ownerPrivateKeyIdentifier` TEXT, `payoutPrivateKeyIdentifier` TEXT, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, `lastSyncedAt` INTEGER, `networkRaw` INTEGER NOT NULL, `walletId` BLOB, `identityIndex` INTEGER NOT NULL, PRIMARY KEY(`identityId`), FOREIGN KEY(`walletId`) REFERENCES `wallets`(`walletId`) ON UPDATE NO ACTION ON DELETE SET NULL )", + "fields": [ + { + "fieldPath": "identityId", + "columnName": "identityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "balance", + "columnName": "balance", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "revision", + "columnName": "revision", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isLocal", + "columnName": "isLocal", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "alias", + "columnName": "alias", + "affinity": "TEXT" + }, + { + "fieldPath": "dpnsName", + "columnName": "dpnsName", + "affinity": "TEXT" + }, + { + "fieldPath": "mainDpnsName", + "columnName": "mainDpnsName", + "affinity": "TEXT" + }, + { + "fieldPath": "identityType", + "columnName": "identityType", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "votingPrivateKeyIdentifier", + "columnName": "votingPrivateKeyIdentifier", + "affinity": "TEXT" + }, + { + "fieldPath": "ownerPrivateKeyIdentifier", + "columnName": "ownerPrivateKeyIdentifier", + "affinity": "TEXT" + }, + { + "fieldPath": "payoutPrivateKeyIdentifier", + "columnName": "payoutPrivateKeyIdentifier", + "affinity": "TEXT" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSyncedAt", + "columnName": "lastSyncedAt", + "affinity": "INTEGER" + }, + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB" + }, + { + "fieldPath": "identityIndex", + "columnName": "identityIndex", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "identityId" + ] + }, + "indices": [ + { + "name": "index_identities_networkRaw", + "unique": false, + "columnNames": [ + "networkRaw" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_identities_networkRaw` ON `${TABLE_NAME}` (`networkRaw`)" + }, + { + "name": "index_identities_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_identities_walletId` ON `${TABLE_NAME}` (`walletId`)" + } + ], + "foreignKeys": [ + { + "table": "wallets", + "onDelete": "SET NULL", + "onUpdate": "NO ACTION", + "columns": [ + "walletId" + ], + "referencedColumns": [ + "walletId" + ] + } + ] + }, + { + "tableName": "public_keys", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `keyId` INTEGER NOT NULL, `purpose` TEXT NOT NULL, `securityLevel` TEXT NOT NULL, `keyType` TEXT NOT NULL, `readOnly` INTEGER NOT NULL, `disabledAt` INTEGER, `totalBudget` INTEGER, `expiresAt` INTEGER, `publicKeyData` BLOB NOT NULL, `contractBoundsData` BLOB, `contractBoundsDocumentTypeName` TEXT, `contractBoundsKind` INTEGER, `privateKeyKeychainIdentifier` TEXT, `derivationIdentityIndex` INTEGER, `derivationKeyIndex` INTEGER, `identityId` TEXT NOT NULL, `createdAt` INTEGER NOT NULL, `lastAccessed` INTEGER, `identityIdData` BLOB, FOREIGN KEY(`identityIdData`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keyId", + "columnName": "keyId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "purpose", + "columnName": "purpose", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "securityLevel", + "columnName": "securityLevel", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "keyType", + "columnName": "keyType", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "readOnly", + "columnName": "readOnly", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "disabledAt", + "columnName": "disabledAt", + "affinity": "INTEGER" + }, + { + "fieldPath": "totalBudget", + "columnName": "totalBudget", + "affinity": "INTEGER" + }, + { + "fieldPath": "expiresAt", + "columnName": "expiresAt", + "affinity": "INTEGER" + }, + { + "fieldPath": "publicKeyData", + "columnName": "publicKeyData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contractBoundsData", + "columnName": "contractBoundsData", + "affinity": "BLOB" + }, + { + "fieldPath": "contractBoundsDocumentTypeName", + "columnName": "contractBoundsDocumentTypeName", + "affinity": "TEXT" + }, + { + "fieldPath": "contractBoundsKind", + "columnName": "contractBoundsKind", + "affinity": "INTEGER" + }, + { + "fieldPath": "privateKeyKeychainIdentifier", + "columnName": "privateKeyKeychainIdentifier", + "affinity": "TEXT" + }, + { + "fieldPath": "derivationIdentityIndex", + "columnName": "derivationIdentityIndex", + "affinity": "INTEGER" + }, + { + "fieldPath": "derivationKeyIndex", + "columnName": "derivationKeyIndex", + "affinity": "INTEGER" + }, + { + "fieldPath": "identityId", + "columnName": "identityId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastAccessed", + "columnName": "lastAccessed", + "affinity": "INTEGER" + }, + { + "fieldPath": "identityIdData", + "columnName": "identityIdData", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_public_keys_identityId_keyId", + "unique": false, + "columnNames": [ + "identityId", + "keyId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_public_keys_identityId_keyId` ON `${TABLE_NAME}` (`identityId`, `keyId`)" + }, + { + "name": "index_public_keys_identityIdData", + "unique": false, + "columnNames": [ + "identityIdData" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_public_keys_identityIdData` ON `${TABLE_NAME}` (`identityIdData`)" + }, + { + "name": "index_public_keys_publicKeyData", + "unique": false, + "columnNames": [ + "publicKeyData" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_public_keys_publicKeyData` ON `${TABLE_NAME}` (`publicKeyData`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "identityIdData" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "dpns_names", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `label` TEXT NOT NULL, `normalizedLabel` TEXT NOT NULL, `parentDomainName` TEXT NOT NULL, `normalizedParentDomainName` TEXT NOT NULL, `acquiredAt` INTEGER NOT NULL, `identityId` BLOB NOT NULL, `documentId` BLOB, `isOwned` INTEGER NOT NULL, `priceCredits` INTEGER, `saleStatusRaw` INTEGER NOT NULL, `counterpartyIdentityId` BLOB, `documentCreatedAtMs` INTEGER NOT NULL, `documentUpdatedAtMs` INTEGER NOT NULL, `documentTransferredAtMs` INTEGER NOT NULL, `marketplaceUpdatedAt` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`, `normalizedParentDomainName`, `normalizedLabel`), FOREIGN KEY(`identityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "label", + "columnName": "label", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "normalizedLabel", + "columnName": "normalizedLabel", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "parentDomainName", + "columnName": "parentDomainName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "normalizedParentDomainName", + "columnName": "normalizedParentDomainName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "acquiredAt", + "columnName": "acquiredAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "identityId", + "columnName": "identityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "documentId", + "columnName": "documentId", + "affinity": "BLOB" + }, + { + "fieldPath": "isOwned", + "columnName": "isOwned", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "priceCredits", + "columnName": "priceCredits", + "affinity": "INTEGER" + }, + { + "fieldPath": "saleStatusRaw", + "columnName": "saleStatusRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "counterpartyIdentityId", + "columnName": "counterpartyIdentityId", + "affinity": "BLOB" + }, + { + "fieldPath": "documentCreatedAtMs", + "columnName": "documentCreatedAtMs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentUpdatedAtMs", + "columnName": "documentUpdatedAtMs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentTransferredAtMs", + "columnName": "documentTransferredAtMs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "marketplaceUpdatedAt", + "columnName": "marketplaceUpdatedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw", + "normalizedParentDomainName", + "normalizedLabel" + ] + }, + "indices": [ + { + "name": "index_dpns_names_identityId", + "unique": false, + "columnNames": [ + "identityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dpns_names_identityId` ON `${TABLE_NAME}` (`identityId`)" + }, + { + "name": "index_dpns_names_documentId", + "unique": false, + "columnNames": [ + "documentId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dpns_names_documentId` ON `${TABLE_NAME}` (`documentId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "identityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "dashpay_profiles", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `identityId` BLOB NOT NULL, `displayName` TEXT, `publicMessage` TEXT, `bio` TEXT, `avatarUrl` TEXT, `avatarHash` BLOB, `avatarFingerprint` BLOB, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`, `identityId`), FOREIGN KEY(`identityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "identityId", + "columnName": "identityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "displayName", + "columnName": "displayName", + "affinity": "TEXT" + }, + { + "fieldPath": "publicMessage", + "columnName": "publicMessage", + "affinity": "TEXT" + }, + { + "fieldPath": "bio", + "columnName": "bio", + "affinity": "TEXT" + }, + { + "fieldPath": "avatarUrl", + "columnName": "avatarUrl", + "affinity": "TEXT" + }, + { + "fieldPath": "avatarHash", + "columnName": "avatarHash", + "affinity": "BLOB" + }, + { + "fieldPath": "avatarFingerprint", + "columnName": "avatarFingerprint", + "affinity": "BLOB" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw", + "identityId" + ] + }, + "indices": [ + { + "name": "index_dashpay_profiles_identityId", + "unique": false, + "columnNames": [ + "identityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dashpay_profiles_identityId` ON `${TABLE_NAME}` (`identityId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "identityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "dashpay_contact_requests", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `ownerIdentityId` BLOB NOT NULL, `contactIdentityId` BLOB NOT NULL, `isOutgoing` INTEGER NOT NULL, `senderKeyIndex` INTEGER NOT NULL, `recipientKeyIndex` INTEGER NOT NULL, `accountReference` INTEGER NOT NULL, `encryptedPublicKey` BLOB NOT NULL, `encryptedAccountLabel` BLOB, `autoAcceptProof` BLOB, `coreHeightCreatedAt` INTEGER NOT NULL, `createdAtMillis` INTEGER NOT NULL, `paymentChannelBroken` INTEGER NOT NULL DEFAULT 0, `contactAlias` TEXT, `contactNote` TEXT, `contactHidden` INTEGER NOT NULL DEFAULT 0, `contactAccountLabel` TEXT, `contactAcceptedAccounts` BLOB, `externalAccountReference` INTEGER, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`, `ownerIdentityId`, `contactIdentityId`, `isOutgoing`), FOREIGN KEY(`ownerIdentityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "ownerIdentityId", + "columnName": "ownerIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contactIdentityId", + "columnName": "contactIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "isOutgoing", + "columnName": "isOutgoing", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "senderKeyIndex", + "columnName": "senderKeyIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "recipientKeyIndex", + "columnName": "recipientKeyIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountReference", + "columnName": "accountReference", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "encryptedPublicKey", + "columnName": "encryptedPublicKey", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "encryptedAccountLabel", + "columnName": "encryptedAccountLabel", + "affinity": "BLOB" + }, + { + "fieldPath": "autoAcceptProof", + "columnName": "autoAcceptProof", + "affinity": "BLOB" + }, + { + "fieldPath": "coreHeightCreatedAt", + "columnName": "coreHeightCreatedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAtMillis", + "columnName": "createdAtMillis", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "paymentChannelBroken", + "columnName": "paymentChannelBroken", + "affinity": "INTEGER", + "notNull": true, + "defaultValue": "0" + }, + { + "fieldPath": "contactAlias", + "columnName": "contactAlias", + "affinity": "TEXT" + }, + { + "fieldPath": "contactNote", + "columnName": "contactNote", + "affinity": "TEXT" + }, + { + "fieldPath": "contactHidden", + "columnName": "contactHidden", + "affinity": "INTEGER", + "notNull": true, + "defaultValue": "0" + }, + { + "fieldPath": "contactAccountLabel", + "columnName": "contactAccountLabel", + "affinity": "TEXT" + }, + { + "fieldPath": "contactAcceptedAccounts", + "columnName": "contactAcceptedAccounts", + "affinity": "BLOB" + }, + { + "fieldPath": "externalAccountReference", + "columnName": "externalAccountReference", + "affinity": "INTEGER" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw", + "ownerIdentityId", + "contactIdentityId", + "isOutgoing" + ] + }, + "indices": [ + { + "name": "index_dashpay_contact_requests_ownerIdentityId", + "unique": false, + "columnNames": [ + "ownerIdentityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dashpay_contact_requests_ownerIdentityId` ON `${TABLE_NAME}` (`ownerIdentityId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "ownerIdentityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "dashpay_ignored_senders", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `ownerIdentityId` BLOB NOT NULL, `ignoredSenderId` BLOB NOT NULL, `ignoredAt` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`, `ownerIdentityId`, `ignoredSenderId`), FOREIGN KEY(`ownerIdentityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "ownerIdentityId", + "columnName": "ownerIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "ignoredSenderId", + "columnName": "ignoredSenderId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "ignoredAt", + "columnName": "ignoredAt", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw", + "ownerIdentityId", + "ignoredSenderId" + ] + }, + "indices": [ + { + "name": "index_dashpay_ignored_senders_ownerIdentityId", + "unique": false, + "columnNames": [ + "ownerIdentityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dashpay_ignored_senders_ownerIdentityId` ON `${TABLE_NAME}` (`ownerIdentityId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "ownerIdentityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "dashpay_contact_profiles", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `ownerIdentityId` BLOB NOT NULL, `contactIdentityId` BLOB NOT NULL, `displayName` TEXT, `publicMessage` TEXT, `bio` TEXT, `avatarUrl` TEXT, `avatarHash` BLOB, `avatarFingerprint` BLOB, `checkedAtMs` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`, `ownerIdentityId`, `contactIdentityId`), FOREIGN KEY(`ownerIdentityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "ownerIdentityId", + "columnName": "ownerIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contactIdentityId", + "columnName": "contactIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "displayName", + "columnName": "displayName", + "affinity": "TEXT" + }, + { + "fieldPath": "publicMessage", + "columnName": "publicMessage", + "affinity": "TEXT" + }, + { + "fieldPath": "bio", + "columnName": "bio", + "affinity": "TEXT" + }, + { + "fieldPath": "avatarUrl", + "columnName": "avatarUrl", + "affinity": "TEXT" + }, + { + "fieldPath": "avatarHash", + "columnName": "avatarHash", + "affinity": "BLOB" + }, + { + "fieldPath": "avatarFingerprint", + "columnName": "avatarFingerprint", + "affinity": "BLOB" + }, + { + "fieldPath": "checkedAtMs", + "columnName": "checkedAtMs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw", + "ownerIdentityId", + "contactIdentityId" + ] + }, + "indices": [ + { + "name": "index_dashpay_contact_profiles_ownerIdentityId", + "unique": false, + "columnNames": [ + "ownerIdentityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dashpay_contact_profiles_ownerIdentityId` ON `${TABLE_NAME}` (`ownerIdentityId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "ownerIdentityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "dashpay_payments", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `ownerIdentityId` BLOB NOT NULL, `counterpartyIdentityId` BLOB NOT NULL, `amountDuffs` INTEGER NOT NULL, `directionRaw` INTEGER NOT NULL, `statusRaw` INTEGER NOT NULL, `txid` TEXT NOT NULL, `memo` TEXT, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`, `ownerIdentityId`, `txid`), FOREIGN KEY(`ownerIdentityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "ownerIdentityId", + "columnName": "ownerIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "counterpartyIdentityId", + "columnName": "counterpartyIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "amountDuffs", + "columnName": "amountDuffs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "directionRaw", + "columnName": "directionRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "statusRaw", + "columnName": "statusRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "txid", + "columnName": "txid", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "memo", + "columnName": "memo", + "affinity": "TEXT" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw", + "ownerIdentityId", + "txid" + ] + }, + "indices": [ + { + "name": "index_dashpay_payments_ownerIdentityId", + "unique": false, + "columnNames": [ + "ownerIdentityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dashpay_payments_ownerIdentityId` ON `${TABLE_NAME}` (`ownerIdentityId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "ownerIdentityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "data_contracts", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` BLOB NOT NULL, `name` TEXT NOT NULL, `serializedContract` BLOB NOT NULL, `createdAt` INTEGER NOT NULL, `lastAccessedAt` INTEGER NOT NULL, `binarySerialization` BLOB, `version` INTEGER, `ownerId` BLOB, `contractDescription` TEXT, `schemaData` BLOB NOT NULL, `documentTypesData` BLOB NOT NULL, `groupsData` BLOB, `networkRaw` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, `lastSyncedAt` INTEGER, `canBeDeleted` INTEGER NOT NULL, `readonly` INTEGER NOT NULL, `keepsHistory` INTEGER NOT NULL, `schemaDefs` INTEGER, `documentsKeepHistoryContractDefault` INTEGER NOT NULL, `documentsMutableContractDefault` INTEGER NOT NULL, `documentsCanBeDeletedContractDefault` INTEGER NOT NULL, `hasTokens` INTEGER NOT NULL, `tokensData` BLOB, `ownerIdentityId` BLOB, PRIMARY KEY(`id`), FOREIGN KEY(`ownerIdentityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE SET NULL )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "serializedContract", + "columnName": "serializedContract", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastAccessedAt", + "columnName": "lastAccessedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "binarySerialization", + "columnName": "binarySerialization", + "affinity": "BLOB" + }, + { + "fieldPath": "version", + "columnName": "version", + "affinity": "INTEGER" + }, + { + "fieldPath": "ownerId", + "columnName": "ownerId", + "affinity": "BLOB" + }, + { + "fieldPath": "contractDescription", + "columnName": "contractDescription", + "affinity": "TEXT" + }, + { + "fieldPath": "schemaData", + "columnName": "schemaData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "documentTypesData", + "columnName": "documentTypesData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "groupsData", + "columnName": "groupsData", + "affinity": "BLOB" + }, + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSyncedAt", + "columnName": "lastSyncedAt", + "affinity": "INTEGER" + }, + { + "fieldPath": "canBeDeleted", + "columnName": "canBeDeleted", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "readonly", + "columnName": "readonly", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsHistory", + "columnName": "keepsHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "schemaDefs", + "columnName": "schemaDefs", + "affinity": "INTEGER" + }, + { + "fieldPath": "documentsKeepHistoryContractDefault", + "columnName": "documentsKeepHistoryContractDefault", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentsMutableContractDefault", + "columnName": "documentsMutableContractDefault", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentsCanBeDeletedContractDefault", + "columnName": "documentsCanBeDeletedContractDefault", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasTokens", + "columnName": "hasTokens", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "tokensData", + "columnName": "tokensData", + "affinity": "BLOB" + }, + { + "fieldPath": "ownerIdentityId", + "columnName": "ownerIdentityId", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_data_contracts_networkRaw", + "unique": false, + "columnNames": [ + "networkRaw" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_data_contracts_networkRaw` ON `${TABLE_NAME}` (`networkRaw`)" + }, + { + "name": "index_data_contracts_ownerIdentityId", + "unique": false, + "columnNames": [ + "ownerIdentityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_data_contracts_ownerIdentityId` ON `${TABLE_NAME}` (`ownerIdentityId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "SET NULL", + "onUpdate": "NO ACTION", + "columns": [ + "ownerIdentityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "document_types", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` BLOB NOT NULL, `contractId` BLOB NOT NULL, `name` TEXT NOT NULL, `schemaJSON` BLOB NOT NULL, `propertiesJSON` BLOB NOT NULL, `documentsKeepHistory` INTEGER NOT NULL, `documentsMutable` INTEGER NOT NULL, `documentsCanBeDeleted` INTEGER NOT NULL, `documentsTransferable` INTEGER NOT NULL, `requiredFieldsJSON` BLOB, `securityLevel` INTEGER NOT NULL, `tradeMode` INTEGER NOT NULL, `creationRestrictionMode` INTEGER NOT NULL, `requiresIdentityEncryptionBoundedKey` INTEGER NOT NULL, `requiresIdentityDecryptionBoundedKey` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastAccessedAt` INTEGER NOT NULL, PRIMARY KEY(`id`), FOREIGN KEY(`contractId`) REFERENCES `data_contracts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contractId", + "columnName": "contractId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "schemaJSON", + "columnName": "schemaJSON", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "propertiesJSON", + "columnName": "propertiesJSON", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "documentsKeepHistory", + "columnName": "documentsKeepHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentsMutable", + "columnName": "documentsMutable", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentsCanBeDeleted", + "columnName": "documentsCanBeDeleted", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentsTransferable", + "columnName": "documentsTransferable", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "requiredFieldsJSON", + "columnName": "requiredFieldsJSON", + "affinity": "BLOB" + }, + { + "fieldPath": "securityLevel", + "columnName": "securityLevel", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "tradeMode", + "columnName": "tradeMode", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "creationRestrictionMode", + "columnName": "creationRestrictionMode", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "requiresIdentityEncryptionBoundedKey", + "columnName": "requiresIdentityEncryptionBoundedKey", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "requiresIdentityDecryptionBoundedKey", + "columnName": "requiresIdentityDecryptionBoundedKey", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastAccessedAt", + "columnName": "lastAccessedAt", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_document_types_contractId", + "unique": false, + "columnNames": [ + "contractId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_document_types_contractId` ON `${TABLE_NAME}` (`contractId`)" + } + ], + "foreignKeys": [ + { + "table": "data_contracts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "contractId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "documents", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`documentId` TEXT NOT NULL, `documentType` TEXT NOT NULL, `revision` INTEGER NOT NULL, `data` BLOB NOT NULL, `contractId` TEXT NOT NULL, `ownerId` TEXT NOT NULL, `contractIdData` BLOB NOT NULL, `ownerIdData` BLOB NOT NULL, `createdAt` INTEGER NOT NULL, `updatedAt` INTEGER NOT NULL, `transferredAt` INTEGER, `createdAtBlockHeight` INTEGER, `updatedAtBlockHeight` INTEGER, `transferredAtBlockHeight` INTEGER, `createdAtCoreBlockHeight` INTEGER, `updatedAtCoreBlockHeight` INTEGER, `transferredAtCoreBlockHeight` INTEGER, `networkRaw` INTEGER NOT NULL, `isDeleted` INTEGER NOT NULL, `localCreatedAt` INTEGER NOT NULL, `localUpdatedAt` INTEGER NOT NULL, `documentTypeRelationId` BLOB, `dataContractId` BLOB, `ownerIdentityId` BLOB, PRIMARY KEY(`documentId`), FOREIGN KEY(`documentTypeRelationId`) REFERENCES `document_types`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE , FOREIGN KEY(`dataContractId`) REFERENCES `data_contracts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE , FOREIGN KEY(`ownerIdentityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "documentId", + "columnName": "documentId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "documentType", + "columnName": "documentType", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "revision", + "columnName": "revision", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "data", + "columnName": "data", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contractId", + "columnName": "contractId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "ownerId", + "columnName": "ownerId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "contractIdData", + "columnName": "contractIdData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "ownerIdData", + "columnName": "ownerIdData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "updatedAt", + "columnName": "updatedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "transferredAt", + "columnName": "transferredAt", + "affinity": "INTEGER" + }, + { + "fieldPath": "createdAtBlockHeight", + "columnName": "createdAtBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "updatedAtBlockHeight", + "columnName": "updatedAtBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "transferredAtBlockHeight", + "columnName": "transferredAtBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "createdAtCoreBlockHeight", + "columnName": "createdAtCoreBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "updatedAtCoreBlockHeight", + "columnName": "updatedAtCoreBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "transferredAtCoreBlockHeight", + "columnName": "transferredAtCoreBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isDeleted", + "columnName": "isDeleted", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "localCreatedAt", + "columnName": "localCreatedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "localUpdatedAt", + "columnName": "localUpdatedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentTypeRelationId", + "columnName": "documentTypeRelationId", + "affinity": "BLOB" + }, + { + "fieldPath": "dataContractId", + "columnName": "dataContractId", + "affinity": "BLOB" + }, + { + "fieldPath": "ownerIdentityId", + "columnName": "ownerIdentityId", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "documentId" + ] + }, + "indices": [ + { + "name": "index_documents_networkRaw", + "unique": false, + "columnNames": [ + "networkRaw" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_documents_networkRaw` ON `${TABLE_NAME}` (`networkRaw`)" + }, + { + "name": "index_documents_contractId", + "unique": false, + "columnNames": [ + "contractId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_documents_contractId` ON `${TABLE_NAME}` (`contractId`)" + }, + { + "name": "index_documents_ownerId", + "unique": false, + "columnNames": [ + "ownerId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_documents_ownerId` ON `${TABLE_NAME}` (`ownerId`)" + }, + { + "name": "index_documents_documentTypeRelationId", + "unique": false, + "columnNames": [ + "documentTypeRelationId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_documents_documentTypeRelationId` ON `${TABLE_NAME}` (`documentTypeRelationId`)" + }, + { + "name": "index_documents_dataContractId", + "unique": false, + "columnNames": [ + "dataContractId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_documents_dataContractId` ON `${TABLE_NAME}` (`dataContractId`)" + }, + { + "name": "index_documents_ownerIdentityId", + "unique": false, + "columnNames": [ + "ownerIdentityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_documents_ownerIdentityId` ON `${TABLE_NAME}` (`ownerIdentityId`)" + } + ], + "foreignKeys": [ + { + "table": "document_types", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "documentTypeRelationId" + ], + "referencedColumns": [ + "id" + ] + }, + { + "table": "data_contracts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "dataContractId" + ], + "referencedColumns": [ + "id" + ] + }, + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "ownerIdentityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "indices", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` BLOB NOT NULL, `contractId` BLOB NOT NULL, `documentTypeName` TEXT NOT NULL, `name` TEXT NOT NULL, `unique` INTEGER NOT NULL, `nullSearchable` INTEGER NOT NULL, `contested` INTEGER NOT NULL, `propertiesJSON` BLOB NOT NULL, `contestedDetailsJSON` BLOB, `createdAt` INTEGER NOT NULL, `documentTypeId` BLOB, PRIMARY KEY(`id`), FOREIGN KEY(`documentTypeId`) REFERENCES `document_types`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contractId", + "columnName": "contractId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "documentTypeName", + "columnName": "documentTypeName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "unique", + "columnName": "unique", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "nullSearchable", + "columnName": "nullSearchable", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "contested", + "columnName": "contested", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "propertiesJSON", + "columnName": "propertiesJSON", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contestedDetailsJSON", + "columnName": "contestedDetailsJSON", + "affinity": "BLOB" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentTypeId", + "columnName": "documentTypeId", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_indices_documentTypeId", + "unique": false, + "columnNames": [ + "documentTypeId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_indices_documentTypeId` ON `${TABLE_NAME}` (`documentTypeId`)" + } + ], + "foreignKeys": [ + { + "table": "document_types", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "documentTypeId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "keywords", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `keyword` TEXT NOT NULL, `contractId` TEXT NOT NULL, `dataContractId` BLOB, PRIMARY KEY(`id`), FOREIGN KEY(`dataContractId`) REFERENCES `data_contracts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "keyword", + "columnName": "keyword", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "contractId", + "columnName": "contractId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "dataContractId", + "columnName": "dataContractId", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_keywords_contractId", + "unique": false, + "columnNames": [ + "contractId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_keywords_contractId` ON `${TABLE_NAME}` (`contractId`)" + }, + { + "name": "index_keywords_dataContractId", + "unique": false, + "columnNames": [ + "dataContractId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_keywords_dataContractId` ON `${TABLE_NAME}` (`dataContractId`)" + } + ], + "foreignKeys": [ + { + "table": "data_contracts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "dataContractId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "properties", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` BLOB NOT NULL, `contractId` BLOB NOT NULL, `documentTypeName` TEXT NOT NULL, `name` TEXT NOT NULL, `type` TEXT NOT NULL, `format` TEXT, `contentMediaType` TEXT, `byteArray` INTEGER NOT NULL, `minItems` INTEGER, `maxItems` INTEGER, `pattern` TEXT, `minLength` INTEGER, `maxLength` INTEGER, `minValue` INTEGER, `maxValue` INTEGER, `fieldDescription` TEXT, `transient` INTEGER NOT NULL, `isRequired` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `documentTypeId` BLOB, PRIMARY KEY(`id`), FOREIGN KEY(`documentTypeId`) REFERENCES `document_types`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contractId", + "columnName": "contractId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "documentTypeName", + "columnName": "documentTypeName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "type", + "columnName": "type", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "format", + "columnName": "format", + "affinity": "TEXT" + }, + { + "fieldPath": "contentMediaType", + "columnName": "contentMediaType", + "affinity": "TEXT" + }, + { + "fieldPath": "byteArray", + "columnName": "byteArray", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "minItems", + "columnName": "minItems", + "affinity": "INTEGER" + }, + { + "fieldPath": "maxItems", + "columnName": "maxItems", + "affinity": "INTEGER" + }, + { + "fieldPath": "pattern", + "columnName": "pattern", + "affinity": "TEXT" + }, + { + "fieldPath": "minLength", + "columnName": "minLength", + "affinity": "INTEGER" + }, + { + "fieldPath": "maxLength", + "columnName": "maxLength", + "affinity": "INTEGER" + }, + { + "fieldPath": "minValue", + "columnName": "minValue", + "affinity": "INTEGER" + }, + { + "fieldPath": "maxValue", + "columnName": "maxValue", + "affinity": "INTEGER" + }, + { + "fieldPath": "fieldDescription", + "columnName": "fieldDescription", + "affinity": "TEXT" + }, + { + "fieldPath": "transient", + "columnName": "transient", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isRequired", + "columnName": "isRequired", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentTypeId", + "columnName": "documentTypeId", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_properties_documentTypeId", + "unique": false, + "columnNames": [ + "documentTypeId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_properties_documentTypeId` ON `${TABLE_NAME}` (`documentTypeId`)" + } + ], + "foreignKeys": [ + { + "table": "document_types", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "documentTypeId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "pending_inputs", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `outpoint` BLOB NOT NULL, `inputIndex` INTEGER NOT NULL, `spendingTxid` BLOB NOT NULL, `spendingTransactionTxid` BLOB, `walletId` BLOB NOT NULL, `createdAt` INTEGER NOT NULL, `isSweptTombstone` INTEGER NOT NULL DEFAULT 0, `winnerMinedHeight` INTEGER, FOREIGN KEY(`spendingTransactionTxid`) REFERENCES `transactions`(`txid`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "outpoint", + "columnName": "outpoint", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "inputIndex", + "columnName": "inputIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "spendingTxid", + "columnName": "spendingTxid", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "spendingTransactionTxid", + "columnName": "spendingTransactionTxid", + "affinity": "BLOB" + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isSweptTombstone", + "columnName": "isSweptTombstone", + "affinity": "INTEGER", + "notNull": true, + "defaultValue": "0" + }, + { + "fieldPath": "winnerMinedHeight", + "columnName": "winnerMinedHeight", + "affinity": "INTEGER" + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_pending_inputs_outpoint", + "unique": false, + "columnNames": [ + "outpoint" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_pending_inputs_outpoint` ON `${TABLE_NAME}` (`outpoint`)" + }, + { + "name": "index_pending_inputs_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_pending_inputs_walletId` ON `${TABLE_NAME}` (`walletId`)" + }, + { + "name": "index_pending_inputs_spendingTransactionTxid", + "unique": false, + "columnNames": [ + "spendingTransactionTxid" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_pending_inputs_spendingTransactionTxid` ON `${TABLE_NAME}` (`spendingTransactionTxid`)" + }, + { + "name": "index_pending_inputs_spendingTxid", + "unique": false, + "columnNames": [ + "spendingTxid" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_pending_inputs_spendingTxid` ON `${TABLE_NAME}` (`spendingTxid`)" + }, + { + "name": "index_pending_inputs_walletId_isSweptTombstone_winnerMinedHeight", + "unique": false, + "columnNames": [ + "walletId", + "isSweptTombstone", + "winnerMinedHeight" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_pending_inputs_walletId_isSweptTombstone_winnerMinedHeight` ON `${TABLE_NAME}` (`walletId`, `isSweptTombstone`, `winnerMinedHeight`)" + } + ], + "foreignKeys": [ + { + "table": "transactions", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "spendingTransactionTxid" + ], + "referencedColumns": [ + "txid" + ] + } + ] + }, + { + "tableName": "tokens", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` BLOB NOT NULL, `contractId` BLOB NOT NULL, `position` INTEGER NOT NULL, `name` TEXT NOT NULL, `baseSupply` TEXT NOT NULL, `maxSupply` TEXT, `decimals` INTEGER NOT NULL, `localizations` TEXT, `isPaused` INTEGER NOT NULL, `allowTransferToFrozenBalance` INTEGER NOT NULL, `keepsTransferHistory` INTEGER NOT NULL, `keepsFreezingHistory` INTEGER NOT NULL, `keepsMintingHistory` INTEGER NOT NULL, `keepsBurningHistory` INTEGER NOT NULL, `keepsDirectPricingHistory` INTEGER NOT NULL, `keepsDirectPurchaseHistory` INTEGER NOT NULL, `conventionsChangeRules` TEXT, `maxSupplyChangeRules` TEXT, `manualMintingRules` TEXT, `manualBurningRules` TEXT, `freezeRules` TEXT, `unfreezeRules` TEXT, `destroyFrozenFundsRules` TEXT, `emergencyActionRules` TEXT, `perpetualDistribution` TEXT, `preProgrammedDistribution` TEXT, `oncePerIdentityDistribution` TEXT, `newTokensDestinationIdentity` BLOB, `mintingAllowChoosingDestination` INTEGER NOT NULL, `distributionChangeRules` TEXT, `tradeMode` TEXT NOT NULL, `tradeModeChangeRules` TEXT, `mainControlGroupPosition` INTEGER, `mainControlGroupCanBeModified` TEXT, `tokenDescription` TEXT, `createdAt` INTEGER NOT NULL, `lastUpdatedAt` INTEGER NOT NULL, `canManuallyMint` INTEGER NOT NULL, `canManuallyBurn` INTEGER NOT NULL, `canFreeze` INTEGER NOT NULL, `canUnfreeze` INTEGER NOT NULL, `canDestroyFrozenFunds` INTEGER NOT NULL, `hasEmergencyActions` INTEGER NOT NULL, `canChangeMaxSupply` INTEGER NOT NULL, `canChangeConventions` INTEGER NOT NULL, `canChangeTradeMode` INTEGER NOT NULL, `hasDistribution` INTEGER NOT NULL, PRIMARY KEY(`id`), FOREIGN KEY(`contractId`) REFERENCES `data_contracts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contractId", + "columnName": "contractId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "position", + "columnName": "position", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "baseSupply", + "columnName": "baseSupply", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "maxSupply", + "columnName": "maxSupply", + "affinity": "TEXT" + }, + { + "fieldPath": "decimals", + "columnName": "decimals", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "localizations", + "columnName": "localizations", + "affinity": "TEXT" + }, + { + "fieldPath": "isPaused", + "columnName": "isPaused", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "allowTransferToFrozenBalance", + "columnName": "allowTransferToFrozenBalance", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsTransferHistory", + "columnName": "keepsTransferHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsFreezingHistory", + "columnName": "keepsFreezingHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsMintingHistory", + "columnName": "keepsMintingHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsBurningHistory", + "columnName": "keepsBurningHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsDirectPricingHistory", + "columnName": "keepsDirectPricingHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsDirectPurchaseHistory", + "columnName": "keepsDirectPurchaseHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "conventionsChangeRules", + "columnName": "conventionsChangeRules", + "affinity": "TEXT" + }, + { + "fieldPath": "maxSupplyChangeRules", + "columnName": "maxSupplyChangeRules", + "affinity": "TEXT" + }, + { + "fieldPath": "manualMintingRules", + "columnName": "manualMintingRules", + "affinity": "TEXT" + }, + { + "fieldPath": "manualBurningRules", + "columnName": "manualBurningRules", + "affinity": "TEXT" + }, + { + "fieldPath": "freezeRules", + "columnName": "freezeRules", + "affinity": "TEXT" + }, + { + "fieldPath": "unfreezeRules", + "columnName": "unfreezeRules", + "affinity": "TEXT" + }, + { + "fieldPath": "destroyFrozenFundsRules", + "columnName": "destroyFrozenFundsRules", + "affinity": "TEXT" + }, + { + "fieldPath": "emergencyActionRules", + "columnName": "emergencyActionRules", + "affinity": "TEXT" + }, + { + "fieldPath": "perpetualDistribution", + "columnName": "perpetualDistribution", + "affinity": "TEXT" + }, + { + "fieldPath": "preProgrammedDistribution", + "columnName": "preProgrammedDistribution", + "affinity": "TEXT" + }, + { + "fieldPath": "oncePerIdentityDistribution", + "columnName": "oncePerIdentityDistribution", + "affinity": "TEXT" + }, + { + "fieldPath": "newTokensDestinationIdentity", + "columnName": "newTokensDestinationIdentity", + "affinity": "BLOB" + }, + { + "fieldPath": "mintingAllowChoosingDestination", + "columnName": "mintingAllowChoosingDestination", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "distributionChangeRules", + "columnName": "distributionChangeRules", + "affinity": "TEXT" + }, + { + "fieldPath": "tradeMode", + "columnName": "tradeMode", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "tradeModeChangeRules", + "columnName": "tradeModeChangeRules", + "affinity": "TEXT" + }, + { + "fieldPath": "mainControlGroupPosition", + "columnName": "mainControlGroupPosition", + "affinity": "INTEGER" + }, + { + "fieldPath": "mainControlGroupCanBeModified", + "columnName": "mainControlGroupCanBeModified", + "affinity": "TEXT" + }, + { + "fieldPath": "tokenDescription", + "columnName": "tokenDescription", + "affinity": "TEXT" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdatedAt", + "columnName": "lastUpdatedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canManuallyMint", + "columnName": "canManuallyMint", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canManuallyBurn", + "columnName": "canManuallyBurn", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canFreeze", + "columnName": "canFreeze", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canUnfreeze", + "columnName": "canUnfreeze", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canDestroyFrozenFunds", + "columnName": "canDestroyFrozenFunds", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasEmergencyActions", + "columnName": "hasEmergencyActions", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canChangeMaxSupply", + "columnName": "canChangeMaxSupply", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canChangeConventions", + "columnName": "canChangeConventions", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canChangeTradeMode", + "columnName": "canChangeTradeMode", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasDistribution", + "columnName": "hasDistribution", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_tokens_contractId", + "unique": false, + "columnNames": [ + "contractId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_tokens_contractId` ON `${TABLE_NAME}` (`contractId`)" + } + ], + "foreignKeys": [ + { + "table": "data_contracts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "contractId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "token_balances", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `tokenId` TEXT NOT NULL, `identityId` BLOB NOT NULL, `balance` BLOB NOT NULL, `frozen` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, `lastSyncedAt` INTEGER, `tokenName` TEXT, `tokenSymbol` TEXT, `tokenDecimals` INTEGER, `networkRaw` INTEGER NOT NULL, `identityRef` BLOB, `tokenRef` BLOB, FOREIGN KEY(`identityRef`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE SET NULL , FOREIGN KEY(`tokenRef`) REFERENCES `tokens`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "tokenId", + "columnName": "tokenId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "identityId", + "columnName": "identityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "balance", + "columnName": "balance", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "frozen", + "columnName": "frozen", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSyncedAt", + "columnName": "lastSyncedAt", + "affinity": "INTEGER" + }, + { + "fieldPath": "tokenName", + "columnName": "tokenName", + "affinity": "TEXT" + }, + { + "fieldPath": "tokenSymbol", + "columnName": "tokenSymbol", + "affinity": "TEXT" + }, + { + "fieldPath": "tokenDecimals", + "columnName": "tokenDecimals", + "affinity": "INTEGER" + }, + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "identityRef", + "columnName": "identityRef", + "affinity": "BLOB" + }, + { + "fieldPath": "tokenRef", + "columnName": "tokenRef", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_token_balances_networkRaw", + "unique": false, + "columnNames": [ + "networkRaw" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_token_balances_networkRaw` ON `${TABLE_NAME}` (`networkRaw`)" + }, + { + "name": "index_token_balances_tokenId_identityId", + "unique": false, + "columnNames": [ + "tokenId", + "identityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_token_balances_tokenId_identityId` ON `${TABLE_NAME}` (`tokenId`, `identityId`)" + }, + { + "name": "index_token_balances_identityId", + "unique": false, + "columnNames": [ + "identityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_token_balances_identityId` ON `${TABLE_NAME}` (`identityId`)" + }, + { + "name": "index_token_balances_identityRef", + "unique": false, + "columnNames": [ + "identityRef" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_token_balances_identityRef` ON `${TABLE_NAME}` (`identityRef`)" + }, + { + "name": "index_token_balances_tokenRef", + "unique": false, + "columnNames": [ + "tokenRef" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_token_balances_tokenRef` ON `${TABLE_NAME}` (`tokenRef`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "SET NULL", + "onUpdate": "NO ACTION", + "columns": [ + "identityRef" + ], + "referencedColumns": [ + "identityId" + ] + }, + { + "table": "tokens", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "tokenRef" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "token_history_events", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `eventType` TEXT NOT NULL, `transactionId` BLOB, `blockHeight` INTEGER, `coreBlockHeight` INTEGER, `fromIdentity` BLOB, `toIdentity` BLOB, `performedByIdentity` BLOB NOT NULL, `amount` TEXT, `balanceBefore` TEXT, `balanceAfter` TEXT, `additionalDataJSON` BLOB, `eventDescription` TEXT, `createdAt` INTEGER NOT NULL, `eventTimestamp` INTEGER NOT NULL, `tokenRef` BLOB, PRIMARY KEY(`id`), FOREIGN KEY(`tokenRef`) REFERENCES `tokens`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "eventType", + "columnName": "eventType", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "transactionId", + "columnName": "transactionId", + "affinity": "BLOB" + }, + { + "fieldPath": "blockHeight", + "columnName": "blockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "coreBlockHeight", + "columnName": "coreBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "fromIdentity", + "columnName": "fromIdentity", + "affinity": "BLOB" + }, + { + "fieldPath": "toIdentity", + "columnName": "toIdentity", + "affinity": "BLOB" + }, + { + "fieldPath": "performedByIdentity", + "columnName": "performedByIdentity", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "amount", + "columnName": "amount", + "affinity": "TEXT" + }, + { + "fieldPath": "balanceBefore", + "columnName": "balanceBefore", + "affinity": "TEXT" + }, + { + "fieldPath": "balanceAfter", + "columnName": "balanceAfter", + "affinity": "TEXT" + }, + { + "fieldPath": "additionalDataJSON", + "columnName": "additionalDataJSON", + "affinity": "BLOB" + }, + { + "fieldPath": "eventDescription", + "columnName": "eventDescription", + "affinity": "TEXT" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "eventTimestamp", + "columnName": "eventTimestamp", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "tokenRef", + "columnName": "tokenRef", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_token_history_events_tokenRef", + "unique": false, + "columnNames": [ + "tokenRef" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_token_history_events_tokenRef` ON `${TABLE_NAME}` (`tokenRef`)" + } + ], + "foreignKeys": [ + { + "table": "tokens", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "tokenRef" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "platform_addresses", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`address` TEXT NOT NULL, `addressType` INTEGER NOT NULL, `addressHash` BLOB NOT NULL, `publicKey` BLOB NOT NULL, `accountIndex` INTEGER NOT NULL, `addressIndex` INTEGER NOT NULL, `derivationPath` TEXT NOT NULL, `isUsed` INTEGER NOT NULL, `balance` INTEGER NOT NULL, `nonce` INTEGER NOT NULL, `firstSeenHeight` INTEGER NOT NULL, `lastSeenHeight` INTEGER NOT NULL, `walletId` BLOB NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, `accountId` INTEGER, PRIMARY KEY(`walletId`, `address`), FOREIGN KEY(`accountId`) REFERENCES `accounts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "address", + "columnName": "address", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "addressType", + "columnName": "addressType", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "addressHash", + "columnName": "addressHash", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "publicKey", + "columnName": "publicKey", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "addressIndex", + "columnName": "addressIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "derivationPath", + "columnName": "derivationPath", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "isUsed", + "columnName": "isUsed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "balance", + "columnName": "balance", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "nonce", + "columnName": "nonce", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "firstSeenHeight", + "columnName": "firstSeenHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSeenHeight", + "columnName": "lastSeenHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountId", + "columnName": "accountId", + "affinity": "INTEGER" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId", + "address" + ] + }, + "indices": [ + { + "name": "index_platform_addresses_walletId_addressHash", + "unique": true, + "columnNames": [ + "walletId", + "addressHash" + ], + "orders": [], + "createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_platform_addresses_walletId_addressHash` ON `${TABLE_NAME}` (`walletId`, `addressHash`)" + }, + { + "name": "index_platform_addresses_accountId", + "unique": false, + "columnNames": [ + "accountId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_platform_addresses_accountId` ON `${TABLE_NAME}` (`accountId`)" + } + ], + "foreignKeys": [ + { + "table": "accounts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "accountId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "platform_addresses_sync_states", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`walletId` BLOB NOT NULL, `networkRaw` INTEGER NOT NULL, `syncHeight` INTEGER NOT NULL, `syncTimestamp` INTEGER NOT NULL, `lastKnownRecentBlock` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`walletId`))", + "fields": [ + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "syncHeight", + "columnName": "syncHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "syncTimestamp", + "columnName": "syncTimestamp", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastKnownRecentBlock", + "columnName": "lastKnownRecentBlock", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId" + ] + }, + "indices": [ + { + "name": "index_platform_addresses_sync_states_networkRaw", + "unique": false, + "columnNames": [ + "networkRaw" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_platform_addresses_sync_states_networkRaw` ON `${TABLE_NAME}` (`networkRaw`)" + } + ] + }, + { + "tableName": "shielded_notes", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`nullifier` BLOB NOT NULL, `walletId` BLOB NOT NULL, `accountIndex` INTEGER NOT NULL, `position` INTEGER NOT NULL, `cmx` BLOB NOT NULL, `blockHeight` INTEGER NOT NULL, `isSpent` INTEGER NOT NULL, `value` INTEGER NOT NULL, `noteData` BLOB NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`nullifier`))", + "fields": [ + { + "fieldPath": "nullifier", + "columnName": "nullifier", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "position", + "columnName": "position", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "cmx", + "columnName": "cmx", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "blockHeight", + "columnName": "blockHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isSpent", + "columnName": "isSpent", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "value", + "columnName": "value", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "noteData", + "columnName": "noteData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "nullifier" + ] + }, + "indices": [ + { + "name": "index_shielded_notes_walletId_accountIndex", + "unique": false, + "columnNames": [ + "walletId", + "accountIndex" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_shielded_notes_walletId_accountIndex` ON `${TABLE_NAME}` (`walletId`, `accountIndex`)" + } + ] + }, + { + "tableName": "shielded_outgoing_notes", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`walletId` BLOB NOT NULL, `accountIndex` INTEGER NOT NULL, `cmx` BLOB NOT NULL, `recipient` BLOB NOT NULL, `value` INTEGER NOT NULL, `memo` BLOB NOT NULL, `blockHeight` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`walletId`, `accountIndex`, `cmx`))", + "fields": [ + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "cmx", + "columnName": "cmx", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "recipient", + "columnName": "recipient", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "value", + "columnName": "value", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "memo", + "columnName": "memo", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "blockHeight", + "columnName": "blockHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId", + "accountIndex", + "cmx" + ] + }, + "indices": [ + { + "name": "index_shielded_outgoing_notes_walletId_accountIndex", + "unique": false, + "columnNames": [ + "walletId", + "accountIndex" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_shielded_outgoing_notes_walletId_accountIndex` ON `${TABLE_NAME}` (`walletId`, `accountIndex`)" + } + ] + }, + { + "tableName": "shielded_activities", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`walletId` BLOB NOT NULL, `accountIndex` INTEGER NOT NULL, `entryId` BLOB NOT NULL, `kindTag` INTEGER NOT NULL, `direction` INTEGER NOT NULL, `status` INTEGER NOT NULL, `amount` INTEGER NOT NULL, `fee` INTEGER NOT NULL, `hasFee` INTEGER NOT NULL, `blockHeight` INTEGER NOT NULL, `hasBlockHeight` INTEGER NOT NULL, `createdAtMs` INTEGER NOT NULL, `identityId` BLOB NOT NULL, `counterparty` BLOB NOT NULL, `memo` BLOB NOT NULL, `noteCmxs` BLOB NOT NULL, `spentNullifiers` BLOB NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`walletId`, `accountIndex`, `entryId`))", + "fields": [ + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "entryId", + "columnName": "entryId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "kindTag", + "columnName": "kindTag", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "direction", + "columnName": "direction", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "status", + "columnName": "status", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "amount", + "columnName": "amount", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "fee", + "columnName": "fee", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasFee", + "columnName": "hasFee", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "blockHeight", + "columnName": "blockHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasBlockHeight", + "columnName": "hasBlockHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAtMs", + "columnName": "createdAtMs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "identityId", + "columnName": "identityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "counterparty", + "columnName": "counterparty", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "memo", + "columnName": "memo", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "noteCmxs", + "columnName": "noteCmxs", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "spentNullifiers", + "columnName": "spentNullifiers", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId", + "accountIndex", + "entryId" + ] + }, + "indices": [ + { + "name": "index_shielded_activities_walletId_accountIndex", + "unique": false, + "columnNames": [ + "walletId", + "accountIndex" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_shielded_activities_walletId_accountIndex` ON `${TABLE_NAME}` (`walletId`, `accountIndex`)" + } + ] + }, + { + "tableName": "shielded_sync_states", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`walletId` BLOB NOT NULL, `accountIndex` INTEGER NOT NULL, `lastSyncedIndex` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`walletId`, `accountIndex`))", + "fields": [ + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSyncedIndex", + "columnName": "lastSyncedIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId", + "accountIndex" + ] + }, + "indices": [ + { + "name": "index_shielded_sync_states_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_shielded_sync_states_walletId` ON `${TABLE_NAME}` (`walletId`)" + } + ] + }, + { + "tableName": "shielded_viewing_keys", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`walletId` BLOB NOT NULL, `accountIndex` INTEGER NOT NULL, `fvkBytes` BLOB NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`walletId`, `accountIndex`))", + "fields": [ + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "fvkBytes", + "columnName": "fvkBytes", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId", + "accountIndex" + ] + }, + "indices": [ + { + "name": "index_shielded_viewing_keys_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_shielded_viewing_keys_walletId` ON `${TABLE_NAME}` (`walletId`)" + } + ] + }, + { + "tableName": "wallet_manager_metadata", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `combinedSyncHeight` INTEGER NOT NULL, `combinedSyncBlockHash` BLOB, `walletCount` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`))", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "combinedSyncHeight", + "columnName": "combinedSyncHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "combinedSyncBlockHash", + "columnName": "combinedSyncBlockHash", + "affinity": "BLOB" + }, + { + "fieldPath": "walletCount", + "columnName": "walletCount", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw" + ] + } + } + ], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '4cdbf8acfcfa07b5745b6aacd7f9d5bb')" + ] + } +} \ No newline at end of file diff --git a/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseMigrationTest.kt b/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseMigrationTest.kt index 49398a2d13a..9a98201510b 100644 --- a/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseMigrationTest.kt +++ b/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseMigrationTest.kt @@ -613,13 +613,90 @@ class DashDatabaseMigrationTest { db.close() } + /** + * v14 -> v15 adds the DashPay backfill record columns on `wallets` + * (`dashPayBackfillFloor`, `dashPayBackfillRewoundFrom`, + * `dashPayBackfillCovered`), all nullable and additive. A pre-migration + * wallet reads back with no record — native then rewinds once, as it did + * before the record existed — and a row written afterwards keeps all + * three, so a restored wallet resumes its backfill instead of restarting + * it (dashpay/platform#4302). + */ + @Test + fun migrate14To15AddsDashPayBackfillColumns() { + helper.createDatabase(dbName, 14).apply { + execSQL( + "INSERT INTO wallets (walletId, walletGroupId, networkRaw, name, birthHeight, " + + "syncedHeight, lastSynced, isImported, createdAt, lastUpdated) " + + "VALUES (x'01', x'02', 1, 'w', 0, 2309809, 0, 0, 0, 0)", + ) + execSQL( + "INSERT INTO identities (identityId, balance, revision, isLocal, identityType, " + + "createdAt, lastUpdated, networkRaw, identityIndex, walletId) " + + "VALUES (x'0A', 0, 0, 1, 'User', 0, 0, 1, 0, x'01')", + ) + execSQL( + "INSERT INTO dashpay_contact_requests (networkRaw, ownerIdentityId, " + + "contactIdentityId, isOutgoing, senderKeyIndex, recipientKeyIndex, " + + "accountReference, encryptedPublicKey, coreHeightCreatedAt, createdAtMillis, " + + "paymentChannelBroken, contactHidden, createdAt, lastUpdated) " + + "VALUES (1, x'0A', x'0B', 0, 2, 3, 4, x'05', 1226329, 0, 0, 0, 0, 0)", + ) + close() + } + + val db = helper.runMigrationsAndValidate(dbName, 15, true, DashDatabase.MIGRATION_14_15) + + db.query( + "SELECT externalAccountReference FROM dashpay_contact_requests WHERE ownerIdentityId = x'0A'", + ).use { c -> + assertTrue(c.moveToFirst()) + assertTrue("pre-migration contact rows carry no outbound-account marker", c.isNull(0)) + } + db.execSQL( + "UPDATE dashpay_contact_requests SET externalAccountReference = 4 WHERE ownerIdentityId = x'0A'", + ) + db.query( + "SELECT externalAccountReference FROM dashpay_contact_requests WHERE ownerIdentityId = x'0A'", + ).use { c -> + assertTrue(c.moveToFirst()) + assertEquals(4, c.getInt(0)) + } + + db.query( + "SELECT dashPayBackfillFloor, dashPayBackfillRewoundFrom, dashPayBackfillCovered, " + + "syncedHeight FROM wallets WHERE walletId = x'01'", + ).use { c -> + assertTrue(c.moveToFirst()) + assertTrue("pre-migration wallets have no backfill record", c.isNull(0)) + assertTrue(c.isNull(1)) + assertTrue(c.isNull(2)) + assertEquals("the cursor itself is untouched", 2_309_809, c.getInt(3)) + } + db.execSQL( + "UPDATE wallets SET dashPayBackfillFloor = 2167092, " + + "dashPayBackfillRewoundFrom = 2537092, dashPayBackfillCovered = x'AABB' " + + "WHERE walletId = x'01'", + ) + db.query( + "SELECT dashPayBackfillFloor, dashPayBackfillRewoundFrom, dashPayBackfillCovered " + + "FROM wallets WHERE walletId = x'01'", + ).use { c -> + assertTrue(c.moveToFirst()) + assertEquals(2_167_092, c.getInt(0)) + assertEquals(2_537_092, c.getInt(1)) + assertTrue(byteArrayOf(0xAA.toByte(), 0xBB.toByte()).contentEquals(c.getBlob(2))) + } + db.close() + } + /** The requested contiguous path from the pre-u64 v4 schema to latest. */ @Test fun migrate4ToLatest() { helper.createDatabase(dbName, 4).close() helper.runMigrationsAndValidate( dbName, - 14, + 15, true, DashDatabase.MIGRATION_4_5, DashDatabase.MIGRATION_5_6, @@ -631,16 +708,17 @@ class DashDatabaseMigrationTest { DashDatabase.MIGRATION_11_12, DashDatabase.MIGRATION_12_13, DashDatabase.MIGRATION_13_14, + DashDatabase.MIGRATION_14_15, ).close() } - /** The full chain from v1 must also land on a valid v14 schema. */ + /** The full chain from v1 must also land on a valid v15 schema. */ @Test fun migrateAllTheWayFrom1() { helper.createDatabase(dbName, 1).close() helper.runMigrationsAndValidate( dbName, - 14, + 15, true, DashDatabase.MIGRATION_1_2, DashDatabase.MIGRATION_2_3, @@ -655,6 +733,7 @@ class DashDatabaseMigrationTest { DashDatabase.MIGRATION_11_12, DashDatabase.MIGRATION_12_13, DashDatabase.MIGRATION_13_14, + DashDatabase.MIGRATION_14_15, ).close() } } diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt index 76759d63ed3..f662cfae2fe 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt @@ -437,6 +437,40 @@ abstract class NativePersistenceBridge { */ open fun onWalletChangesetChainLockHeight(walletId: ByteArray, height: Int): Int = 0 + /** + * The wallet's DashPay coreHeight backfill record — the durable half of + * the contact rescan guard (dashpay/platform#4302). Fired inside the + * round, after the wallet changeset header, on every round whose + * changeset carries a record; the rescan sweep writes it on the SAME + * round as the lowered `syncedHeight` it belongs with, so a handler that + * stores both holds a record that only ever vouches for a cursor it also + * stored. Descriptor `([BII[BI)I`. + * + * Whole-record semantics: replace what the wallet row holds, never + * merge. [covered] is ONE flat `byte[]` of `72 * coveredCount` bytes — + * per receival account the owner identity id (32), the contact identity + * id (32), the account index (u32) and the height it is covered from + * (u32), both little-endian — the same + * packing the sweep slot uses for its txids. Store it as an opaque blob + * and hand it back unchanged on [WalletRestoreData.dashPayBackfillCovered]; + * only native reads it. + * + * Purely additive: an implementation that ignores it keeps today's + * behaviour — the backfill re-fires on every launch, re-walking every + * filter from the earliest contact's core height. Slow, never lossy. + * + * Delivered only to a bridge that also overrides [onWalletChangesetHeader]: + * the record vouches for the cursor that callback stores, so a bridge + * keeping the inherited no-op header never receives it. + */ + open fun onWalletChangesetDashPayBackfill( + walletId: ByteArray, + floor: Int, + rewoundFrom: Int, + covered: ByteArray, + coveredCount: Int, + ): Int = 0 + // ── Identities ──────────────────────────────────────────────────── /** @@ -562,14 +596,25 @@ abstract class NativePersistenceBridge { // ── Contacts ────────────────────────────────────────────────────── /** - * One `ContactRequestFFI` upsert. Descriptor - * `([B[B[BZIII[B[B[BIJZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;[I)I`. + * One `ContactRequestFFI` upsert, marker-less form (the signature this + * API had before the outbound-account marker). Native calls the + * overload below; this one stays so existing implementations keep + * compiling and keep receiving rows through the delegating default. * * The tail block ([paymentChannelBroken] / [alias] / [note] / * [isHidden] / [contactAccountLabel] / [acceptedAccounts]) is * established-row relationship metadata (contactInfo + DIP-15 * accepted accounts) — null / false / empty on pending rows. * [acceptedAccounts] is never null (empty when absent). + * + * [hasExternalAccountReference] / [externalAccountReference] mirror + * `EstablishedContact::external_account_reference`: the incoming + * `accountReference` the registered outbound (sending) account was + * built from. Store it and hand it back on + * [ContactRequestRestoreData]; a cold start that restores `None` + * treats the account as rotated and rebuilds it on every launch + * (dashpay/platform#4302). Replicated onto both established rows; + * `false` / 0 on pending rows. */ @Suppress("LongParameterList") open fun onPersistContactUpsert( @@ -593,6 +638,45 @@ abstract class NativePersistenceBridge { acceptedAccounts: IntArray, ): Int = 0 + /** + * The overload native calls (descriptor + * `([B[B[BZIII[B[B[BIJZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;[IZI)I`): + * the row above plus `EstablishedContact::external_account_reference` as + * `(hasExternalAccountReference, externalAccountReference)`. The default + * body delegates to the marker-less overload, so an implementation that + * overrides only that one keeps persisting every other field and merely + * loses the marker (native then rebuilds the outbound account once per + * launch, the pre-marker behaviour). Override this one to store it. + */ + @Suppress("LongParameterList") + open fun onPersistContactUpsert( + walletId: ByteArray, + ownerId: ByteArray, + contactId: ByteArray, + isOutgoing: Boolean, + senderKeyIndex: Int, + recipientKeyIndex: Int, + accountReference: Int, + encryptedPublicKey: ByteArray, + encryptedAccountLabel: ByteArray?, + autoAcceptProof: ByteArray?, + coreHeightCreatedAt: Int, + createdAt: Long, + paymentChannelBroken: Boolean, + alias: String?, + note: String?, + isHidden: Boolean, + contactAccountLabel: String?, + acceptedAccounts: IntArray, + hasExternalAccountReference: Boolean, + externalAccountReference: Int, + ): Int = onPersistContactUpsert( + walletId, ownerId, contactId, isOutgoing, senderKeyIndex, recipientKeyIndex, + accountReference, encryptedPublicKey, encryptedAccountLabel, autoAcceptProof, + coreHeightCreatedAt, createdAt, paymentChannelBroken, alias, note, isHidden, + contactAccountLabel, acceptedAccounts, + ) + /** One sent-side `ContactRequestRemovalFFI`. Descriptor `([B[B[B)I`. */ open fun onPersistContactRemovalSent( walletId: ByteArray, @@ -968,7 +1052,41 @@ class WalletRestoreData( * `loadWalletList`. */ @JvmField val lastAppliedChainLockBytes: ByteArray, -) +) { + // The DashPay backfill record rides as body properties, not constructor + // parameters, so this class keeps exactly the JVM constructors it had + // before the record existed: a bridge compiled against the older SDK — + // Java, or Kotlin using the defaulted `providerSpecialTxs` — still links + // (dashpay/platform#4302 review). Set them with `apply { … }`; native + // reads them by field name either way. + + /** + * Whether the wallet row holds a DashPay coreHeight backfill record + * (dashpay/platform#4302). `false` for a wallet that never rewound for a + * contact or a row persisted before the record existed; native then + * ignores the three fields below, and the first rescan sweep behaves as + * it always did and writes one. Mirror of + * `WalletRestoreEntryFFI.has_dashpay_backfill`. + */ + @JvmField var hasDashPayBackfill: Boolean = false + + /** Lowest height the backfill rewound the cursor to. */ + @JvmField var dashPayBackfillFloor: Int = 0 + + /** + * Highest cursor the backfill rewound from — the height the scan climbs + * back to for the backfill to be complete. + */ + @JvmField var dashPayBackfillRewoundFrom: Int = 0 + + /** + * The record's cover set, exactly as [NativePersistenceBridge.onWalletChangesetDashPayBackfill] + * delivered it: `72` bytes per covered receival account. Native re-packs it into + * a `DashPayBackfillCoveredContactFFI` array; a blob that is not a whole + * number of entries is read as no record. + */ + @JvmField var dashPayBackfillCovered: ByteArray = ByteArray(0) +} /** * Kotlin staging row for the unchanged `ProviderSpecialTxRestoreEntryFFI`. @@ -1258,7 +1376,21 @@ class ContactRequestRestoreData( @JvmField val isHidden: Boolean, @JvmField val contactAccountLabel: String?, @JvmField val acceptedAccounts: IntArray, -) +) { + // Body properties, not constructor parameters, for the same reason as + // [WalletRestoreData]'s backfill fields: the class keeps the JVM + // constructor it had before the marker existed. + + /** + * `EstablishedContact::external_account_reference` as a `(present, + * value)` pair, exactly as [NativePersistenceBridge.onPersistContactUpsert] + * delivered it. `false` restores `None`, which makes native rebuild the + * outbound account once on the next sweep. + */ + @JvmField var hasExternalAccountReference: Boolean = false + + @JvmField var externalAccountReference: Int = 0 +} /** * One flat identity-public-key row — mirror of `IdentityKeyRestoreFFI`. diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabase.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabase.kt index e74e3b00954..0283ac2904f 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabase.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabase.kt @@ -161,9 +161,27 @@ import org.dashfoundation.dashsdk.persistence.entities.WalletManagerMetadataEnti * contract's `oncePerIdentityDistribution` block as JSON, so the claim * screen can offer the third distribution kind. NULL for every pre-existing * row; the next contract materialization fills it in. + * + * Version 15 (durable DashPay backfill, dashpay/platform#4302): adds the + * nullable `wallets.dashPayBackfillFloor`, `wallets.dashPayBackfillRewoundFrom` + * and `wallets.dashPayBackfillCovered` columns — the record native writes + * through `onWalletChangesetDashPayBackfill` on the same round as the lowered + * `syncedHeight` it belongs with, and reads back on `loadWalletList`. The + * contact rescan used to guard against re-lowering the cursor in memory only, + * while the cursor it lowers is durable, so every fresh process rewound again + * and re-walked every filter from the earliest contact's core height. All + * three NULL (every pre-migration row) means no record: native rewinds once + * more, writes one, and never again for the contacts it covers. The same + * version adds the nullable `dashpay_contact_requests.externalAccountReference` + * column — `EstablishedContact::external_account_reference`, the marker that + * says which incoming `accountReference` the outbound sending account was + * built from. It was never persisted, so every cold start read it as absent, + * treated the account as rotated, tore it down and rebuilt it — and that + * rebuild was the per-launch cursor rewind. NULL for pre-migration rows: + * native rebuilds once, stamps it, and never again. */ @Database( - version = 14, + version = 15, exportSchema = true, entities = [ WalletEntity::class, @@ -679,6 +697,25 @@ abstract class DashDatabase : RoomDatabase() { } } + /** + * v14 -> v15: additive nullable `wallets.dashPayBackfillFloor`, + * `wallets.dashPayBackfillRewoundFrom`, `wallets.dashPayBackfillCovered` + * and `dashpay_contact_requests.externalAccountReference`, see the + * version-15 class doc above. NULL for every pre-existing row — no + * record on file and no marker, so the next sweep rebuilds the + * outbound account once, rescans once, and writes both. + */ + val MIGRATION_14_15: Migration = object : Migration(14, 15) { + override fun migrate(db: SupportSQLiteDatabase) { + db.execSQL("ALTER TABLE `wallets` ADD COLUMN `dashPayBackfillFloor` INTEGER") + db.execSQL("ALTER TABLE `wallets` ADD COLUMN `dashPayBackfillRewoundFrom` INTEGER") + db.execSQL("ALTER TABLE `wallets` ADD COLUMN `dashPayBackfillCovered` BLOB") + db.execSQL( + "ALTER TABLE `dashpay_contact_requests` ADD COLUMN `externalAccountReference` INTEGER", + ) + } + } + /** * Build the on-disk database. WAL is Room's default journal mode on * API 16+; writes go through the persistence handler inside @@ -708,6 +745,7 @@ abstract class DashDatabase : RoomDatabase() { MIGRATION_11_12, MIGRATION_12_13, MIGRATION_13_14, + MIGRATION_14_15, ) .build() diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt index 2f4ad4d12d5..dbaa458df17 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt @@ -775,6 +775,39 @@ class PlatformWalletPersistenceHandler( * height, no collection), so a chainlock-advancing round collects too, * not only a header round. */ + /** + * The wallet's DashPay coreHeight backfill record (dashpay/platform#4302): + * whole-record replace of the three `wallets.dashPayBackfill*` columns, + * staged into the round so it commits with — or rolls back with — the + * lowered `syncedHeight` the header slot wrote moments earlier in the + * same round. A record that vouches for a cursor the round never + * committed must not survive on its own. + */ + override fun onWalletChangesetDashPayBackfill( + walletId: ByteArray, + floor: Int, + rewoundFrom: Int, + covered: ByteArray, + coveredCount: Int, + ): Int = guarded { + require(covered.size == coveredCount * DASHPAY_BACKFILL_COVERED_ENTRY_SIZE) { + "DashPay backfill cover set must be $DASHPAY_BACKFILL_COVERED_ENTRY_SIZE bytes per contact" + } + stage(walletId) { db -> + // Drop stale post-deletion callbacks (can't resurrect a wallet). + val wallet = db.walletDao().getByWalletId(walletId) ?: return@stage + db.walletDao().upsert( + wallet.copy( + dashPayBackfillFloor = floor, + dashPayBackfillRewoundFrom = rewoundFrom, + dashPayBackfillCovered = covered.copyOf(), + lastUpdated = now(), + ), + ) + } + 0 + } + override fun onWalletChangesetChainLockHeight(walletId: ByteArray, height: Int): Int = guarded { val round = openRound(walletId) round?.finalityAdvanced = true @@ -2095,6 +2128,7 @@ class PlatformWalletPersistenceHandler( // ── Contacts ────────────────────────────────────────────────────── + /** Marker-less form: same row, no outbound-account marker (stored as NULL). */ override fun onPersistContactUpsert( walletId: ByteArray, ownerId: ByteArray, @@ -2114,6 +2148,36 @@ class PlatformWalletPersistenceHandler( isHidden: Boolean, contactAccountLabel: String?, acceptedAccounts: IntArray, + ): Int = onPersistContactUpsert( + walletId, ownerId, contactId, isOutgoing, senderKeyIndex, recipientKeyIndex, + accountReference, encryptedPublicKey, encryptedAccountLabel, autoAcceptProof, + coreHeightCreatedAt, createdAt, paymentChannelBroken, alias, note, isHidden, + contactAccountLabel, acceptedAccounts, + hasExternalAccountReference = false, + externalAccountReference = 0, + ) + + override fun onPersistContactUpsert( + walletId: ByteArray, + ownerId: ByteArray, + contactId: ByteArray, + isOutgoing: Boolean, + senderKeyIndex: Int, + recipientKeyIndex: Int, + accountReference: Int, + encryptedPublicKey: ByteArray, + encryptedAccountLabel: ByteArray?, + autoAcceptProof: ByteArray?, + coreHeightCreatedAt: Int, + createdAt: Long, + paymentChannelBroken: Boolean, + alias: String?, + note: String?, + isHidden: Boolean, + contactAccountLabel: String?, + acceptedAccounts: IntArray, + hasExternalAccountReference: Boolean, + externalAccountReference: Int, ): Int = guarded { stage(walletId) { db -> // Owner identity must exist; skip silently otherwise (replayed @@ -2139,6 +2203,8 @@ class PlatformWalletPersistenceHandler( contactHidden = isHidden, contactAccountLabel = contactAccountLabel, contactAcceptedAccounts = encodeAcceptedAccounts(acceptedAccounts), + externalAccountReference = + if (hasExternalAccountReference) externalAccountReference else null, lastUpdated = now(), ), ) @@ -2673,6 +2739,15 @@ class PlatformWalletPersistenceHandler( // slice. val lastAppliedChainLockBytes = w.lastAppliedChainLockBytes ?: ByteArray(0) + // Persisted DashPay backfill record — the durable half of the + // contact rescan guard. Present only when every column is set + // (one round writes all three); otherwise native treats the + // wallet as never backfilled and rewinds once, as before. + val backfillFloor = w.dashPayBackfillFloor + val backfillRewoundFrom = w.dashPayBackfillRewoundFrom + val backfillCovered = w.dashPayBackfillCovered + val hasBackfill = + backfillFloor != null && backfillRewoundFrom != null && backfillCovered != null out.add( WalletRestoreData( walletId = w.walletId, @@ -2694,7 +2769,14 @@ class PlatformWalletPersistenceHandler( unresolvedAssetLockTxRecords = unresolvedAssetLockTxRecords, providerSpecialTxs = providerSpecialTxs, lastAppliedChainLockBytes = lastAppliedChainLockBytes, - ), + ).apply { + if (hasBackfill) { + hasDashPayBackfill = true + dashPayBackfillFloor = backfillFloor!! + dashPayBackfillRewoundFrom = backfillRewoundFrom!! + dashPayBackfillCovered = backfillCovered!! + } + }, ) } out.toTypedArray() @@ -2935,7 +3017,12 @@ class PlatformWalletPersistenceHandler( isHidden = c.contactHidden, contactAccountLabel = c.contactAccountLabel, acceptedAccounts = decodeAcceptedAccounts(c.contactAcceptedAccounts), - ) + ).apply { + c.externalAccountReference?.let { reference -> + hasExternalAccountReference = true + externalAccountReference = reference + } + } }.toTypedArray() // Ignored senders (per-sender mute) — restores the Rust // `ignored_senders` set so a previously-ignored sender doesn't @@ -4024,6 +4111,13 @@ class PlatformWalletPersistenceHandler( internal const val CAPABILITY_WALLET_RESTORE: Long = 0x80 internal const val CAPABILITY_DPNS_NAME_STATES: Long = 0x100 internal const val CAPABILITY_TRACKED_ASSET_LOCKS: Long = 0x200 + /** + * Bytes per receival account in the opaque cover set + * [onWalletChangesetDashPayBackfill] delivers: owner id (32), + * contact id (32), account index (4), covered-from height (4). + */ + internal const val DASHPAY_BACKFILL_COVERED_ENTRY_SIZE: Int = 32 + 32 + 4 + 4 + internal const val CAPABILITY_CORE_SWEEP_REMOVAL: Long = NativePersistenceBridge.CAPABILITY_CORE_SWEEP_REMOVAL diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/DashpayContactRequestEntity.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/DashpayContactRequestEntity.kt index aae74e5a845..61254e1c234 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/DashpayContactRequestEntity.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/DashpayContactRequestEntity.kt @@ -81,6 +81,16 @@ data class DashpayContactRequestEntity( * `contactAcceptedAccounts` (`[UInt32]`). */ val contactAcceptedAccounts: ByteArray? = null, + /** + * `EstablishedContact::external_account_reference` — the incoming + * `accountReference` the registered outbound (sending) account was + * built from; null when no account is known to be built for the + * current reference. Relationship-level (both established rows), null + * on pending rows. Restoring it is what stops native from tearing the + * outbound account down and rebuilding it on every cold start + * (dashpay/platform#4302). + */ + val externalAccountReference: Int? = null, val createdAt: Date = Date(), val lastUpdated: Date = Date(), ) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/WalletEntity.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/WalletEntity.kt index 200edbe1947..06682b2c724 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/WalletEntity.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/WalletEntity.kt @@ -68,6 +68,21 @@ data class WalletEntity( * has been persisted". */ val lastAppliedChainLockHeight: Int? = null, + /** + * DashPay coreHeight backfill record (dashpay/platform#4302), delivered + * whole by `onWalletChangesetDashPayBackfill` on the same round as the + * lowered `syncedHeight` it belongs with, and handed back unchanged on + * `loadWalletList`. All three NULL (the pre-migration reading) means no + * record: native re-runs the backfill once and writes one. `floor` is the + * lowest height the backfill rewound to, `rewoundFrom` the cursor it + * rewound from — the scan is complete once `syncedHeight` climbs back + * past it — and `covered` the opaque cover set (72 bytes per receival + * account: owner id, contact id, account index, covered-from height) + * only native reads. + */ + val dashPayBackfillFloor: Int? = null, + val dashPayBackfillRewoundFrom: Int? = null, + val dashPayBackfillCovered: ByteArray? = null, val isImported: Boolean = false, val createdAt: Date = Date(), val lastUpdated: Date = Date(), diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt index fb2ba7896b7..41326e84f92 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt @@ -233,15 +233,16 @@ class DashDatabaseTest { } @Test - fun schemaIsAtVersion14WithTheSweepHoldIndexes() = runTest { + fun schemaIsAtVersion15WithTheSweepHoldIndexes() = runTest { // The sweep-hold columns land in ONE migration (10 → 11), with the // two `pending_inputs` indexes the sweep's claimed-row lookup // (`spendingTxid`) and the end-of-round collector // (`walletId, isSweptTombstone, winnerMinedHeight`) rely on. // 11 → 12 adds the identity key usage limits columns on top, - // 12 → 13 the contract bounds kind, and 13 → 14 the token - // once-per-identity distribution block. - assertEquals(14, db.openHelper.readableDatabase.version) + // 12 → 13 the contract bounds kind, 13 → 14 the token + // once-per-identity distribution block, and 14 → 15 the DashPay + // backfill record columns on `wallets`. + assertEquals(15, db.openHelper.readableDatabase.version) val indexes = mutableSetOf() db.openHelper.readableDatabase.query("PRAGMA index_list('pending_inputs')").use { c -> val nameColumn = c.getColumnIndexOrThrow("name") @@ -293,6 +294,53 @@ class DashDatabaseTest { assertTrue(found) } + @Test + fun shouldHaveNullableDashPayBackfillColumnsOnWallets() = runTest { + // Version 15 (14 → 15): all three nullable with no default, so a + // legacy row reads back as "no record" and native rewinds once. + val expected = mapOf( + "dashPayBackfillFloor" to "INTEGER", + "dashPayBackfillRewoundFrom" to "INTEGER", + "dashPayBackfillCovered" to "BLOB", + ) + val found = mutableMapOf() + db.openHelper.readableDatabase.query("PRAGMA table_info('wallets')").use { c -> + val name = c.getColumnIndexOrThrow("name") + val type = c.getColumnIndexOrThrow("type") + val notNull = c.getColumnIndexOrThrow("notnull") + val default = c.getColumnIndexOrThrow("dflt_value") + while (c.moveToNext()) { + val column = c.getString(name) + if (column !in expected) continue + found[column] = c.getString(type) + assertEquals(column, 0, c.getInt(notNull)) + assertTrue(column, c.isNull(default)) + } + } + assertEquals(expected, found) + + db.walletDao().upsert(WalletEntity(walletId = walletId, networkRaw = 1)) + val legacy = db.walletDao().getByWalletId(walletId)!! + assertNull(legacy.dashPayBackfillFloor) + assertNull(legacy.dashPayBackfillRewoundFrom) + assertNull(legacy.dashPayBackfillCovered) + + // Same version: the nullable outbound-account marker on contact rows. + var markerFound = false + db.openHelper.readableDatabase.query("PRAGMA table_info('dashpay_contact_requests')").use { c -> + val name = c.getColumnIndexOrThrow("name") + val type = c.getColumnIndexOrThrow("type") + val notNull = c.getColumnIndexOrThrow("notnull") + while (c.moveToNext()) { + if (c.getString(name) != "externalAccountReference") continue + markerFound = true + assertEquals("INTEGER", c.getString(type)) + assertEquals(0, c.getInt(notNull)) + } + } + assertTrue(markerFound) + } + @Test fun advanceChainLockHeightIsANarrowMonotonicMaxWrite() = runTest { db.walletDao().upsert(WalletEntity(walletId = walletId, networkRaw = 1, name = "w", syncedHeight = 7)) diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt index b7b1bfd63e9..cc6dede2141 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt @@ -4833,6 +4833,115 @@ class PlatformWalletPersistenceHandlerTest { assertTrue(mutedId.contentEquals(identity.ignoredSenders[0])) } + /** + * dashpay/platform#4302: the outbound-account marker + * (`EstablishedContact::external_account_reference`) must round-trip, + * or every cold start rebuilds the outbound account. `false` stores + * NULL and restores as absent; `true` stores the value and restores it. + */ + @Test + fun contactUpsertRoundTripsTheExternalAccountReference() = runTest { + handler.onPersistWalletMetadata(walletId, testnet, groupId, 0) + val xpub = ByteArray(78) { 30 } + handler.onPersistAccountRegistration( + walletId, 0, 0, 0, 0, 0, ByteArray(0), ByteArray(0), xpub, + ) + val ownerId = ByteArray(32) { 23 } + val contactId = ByteArray(32) { 24 } + seedIdentity(ownerId) + + // Absent marker (the default trailing arguments). + persistIncomingContact(ownerId, contactId) + val before = handler.onLoadWalletList().single().identities.single().contacts.single() + assertFalse(before.hasExternalAccountReference) + assertEquals(0, before.externalAccountReference) + assertNull(db.dashpayDao().getContactRequestsByOwner(ownerId).single().externalAccountReference) + + // Stamped marker. + handler.onChangesetBegin(walletId) + handler.onPersistContactUpsert( + walletId = walletId, + ownerId = ownerId, + contactId = contactId, + isOutgoing = false, + senderKeyIndex = 2, + recipientKeyIndex = 3, + accountReference = 4, + encryptedPublicKey = ByteArray(96) { 5 }, + encryptedAccountLabel = null, + autoAcceptProof = null, + coreHeightCreatedAt = 100_000, + createdAt = 1_700_000_000_000, + paymentChannelBroken = false, + alias = null, + note = null, + isHidden = false, + contactAccountLabel = null, + acceptedAccounts = IntArray(0), + hasExternalAccountReference = true, + externalAccountReference = 4, + ) + handler.onChangesetEnd(walletId, success = true) + val after = handler.onLoadWalletList().single().identities.single().contacts.single() + assertTrue(after.hasExternalAccountReference) + assertEquals(4, after.externalAccountReference) + assertEquals(4, db.dashpayDao().getContactRequestsByOwner(ownerId).single().externalAccountReference) + } + + /** + * dashpay/platform#4302 review: adding the backfill record and the + * outbound-account marker must not change the JVM constructors of the two + * restore holders, or a bridge compiled against the previous SDK throws + * `NoSuchMethodError` when it builds restore rows. Pins both the + * full-arity constructor (Java callers) and `WalletRestoreData`'s + * default-argument synthetic (Kotlin callers leaving `providerSpecialTxs` + * out) against the v4.2-dev parameter lists. + */ + @Test + fun restoreHoldersKeepTheirPreviousJvmConstructors() { + fun ctorTypes(cls: Class<*>) = cls.declaredConstructors.map { it.parameterTypes.map { p -> p.name } }.toSet() + + val walletBase = listOf( + "[B", "int", "[Lorg.dashfoundation.dashsdk.ffi.AccountSpecData;", "long", "long", "long", + "int", "int", "int", "long", + "[Lorg.dashfoundation.dashsdk.ffi.IdentityRestoreData;", + "[Lorg.dashfoundation.dashsdk.ffi.PlatformAddressBalanceRestoreData;", + "[Lorg.dashfoundation.dashsdk.ffi.UtxoRestoreData;", + "[Lorg.dashfoundation.dashsdk.ffi.CoreAddressPoolRestoreData;", + "[Lorg.dashfoundation.dashsdk.ffi.TrackedAssetLockRestoreData;", + "[Lorg.dashfoundation.dashsdk.ffi.UnresolvedAssetLockTxRecordData;", + "[Lorg.dashfoundation.dashsdk.ffi.ProviderSpecialTxRestoreData;", + "[B", + ) + val walletCtors = ctorTypes(org.dashfoundation.dashsdk.ffi.WalletRestoreData::class.java) + assertTrue("full-arity constructor unchanged", walletCtors.contains(walletBase)) + assertTrue( + "default-argument constructor unchanged", + walletCtors.contains(walletBase + listOf("int", "kotlin.jvm.internal.DefaultConstructorMarker")), + ) + + val contactBase = listOf( + "[B", "[B", "boolean", "int", "int", "int", "[B", "[B", "[B", "int", "long", "boolean", + "java.lang.String", "java.lang.String", "boolean", "java.lang.String", "[I", + ) + assertTrue( + "contact restore constructor unchanged", + ctorTypes(org.dashfoundation.dashsdk.ffi.ContactRequestRestoreData::class.java).contains(contactBase), + ) + + // The new fields are still plain fields native can read by name. + for ((cls, names) in listOf( + org.dashfoundation.dashsdk.ffi.WalletRestoreData::class.java to listOf( + "hasDashPayBackfill", "dashPayBackfillFloor", "dashPayBackfillRewoundFrom", "dashPayBackfillCovered", + ), + org.dashfoundation.dashsdk.ffi.ContactRequestRestoreData::class.java to listOf( + "hasExternalAccountReference", "externalAccountReference", + ), + )) { + for (name in names) assertNotNull("$name is a JVM field", cls.getField(name)) + } + } + @Test fun loadWalletListScopesToTheHandlerNetwork() = runTest { // A network-scoped handler must never hand the Rust loader a @@ -6727,6 +6836,156 @@ class PlatformWalletPersistenceHandlerTest { chainLockHeightRound(handler, 600) assertEquals(600, db.walletDao().getByWalletId(walletId)!!.lastAppliedChainLockHeight) } + /** One packed cover-set entry: owner id ‖ contact id ‖ covered-from (LE u32). */ + private fun backfillEntry(owner: Byte, contact: Byte, coveredFrom: Int): ByteArray = + ByteArray(32) { owner } + ByteArray(32) { contact } + ByteArray(4) + byteArrayOf( + (coveredFrom and 0xFF).toByte(), + ((coveredFrom shr 8) and 0xFF).toByte(), + ((coveredFrom shr 16) and 0xFF).toByte(), + ((coveredFrom shr 24) and 0xFF).toByte(), + ) + + /** + * dashpay/platform#4302: the DashPay backfill record is a whole-record + * replace of the three `wallets.dashPayBackfill*` columns, written on + * the same round as the lowered `syncedHeight` the rescan carries. A + * later record replaces the earlier one outright — a contact the new + * record no longer lists is gone, never merged back — and the cover set + * is stored byte-for-byte so native reads back exactly what it wrote. + */ + @Test + fun onWalletChangesetDashPayBackfillReplacesTheRecordOnTheWalletRow() = runTest { + handler.onPersistWalletMetadata(walletId, testnet, groupId, 0) + val row = db.walletDao().getByWalletId(walletId)!! + assertNull("no record until the slot fires", row.dashPayBackfillFloor) + assertNull(row.dashPayBackfillRewoundFrom) + assertNull(row.dashPayBackfillCovered) + + val first = backfillEntry(1, 2, 2_167_092) + backfillEntry(1, 3, 2_300_000) + handler.onChangesetBegin(walletId) + handler.onWalletChangesetHeader( + walletId = walletId, + hasSyncedHeight = true, + syncedHeight = 2_167_092, + hasBalance = false, + confirmedDelta = 0, + unconfirmedDelta = 0, + immatureDelta = 0, + lockedDelta = 0, + lastAppliedChainLockBytes = ByteArray(0), + ) + assertEquals( + 0, + handler.onWalletChangesetDashPayBackfill(walletId, 2_167_092, 2_537_092, first, 2), + ) + handler.onChangesetEnd(walletId, success = true) + + val stored = db.walletDao().getByWalletId(walletId)!! + assertEquals("the lowered cursor lands with the record", 2_167_092, stored.syncedHeight) + assertEquals(2_167_092, stored.dashPayBackfillFloor) + assertEquals(2_537_092, stored.dashPayBackfillRewoundFrom) + assertTrue(first.contentEquals(stored.dashPayBackfillCovered)) + + // A later record replaces the whole cover set. + val second = backfillEntry(1, 2, 2_167_092) + handler.onChangesetBegin(walletId) + handler.onWalletChangesetDashPayBackfill(walletId, 2_100_000, 2_600_000, second, 1) + handler.onChangesetEnd(walletId, success = true) + val replaced = db.walletDao().getByWalletId(walletId)!! + assertEquals(2_100_000, replaced.dashPayBackfillFloor) + assertEquals(2_600_000, replaced.dashPayBackfillRewoundFrom) + assertTrue(second.contentEquals(replaced.dashPayBackfillCovered)) + assertEquals("sibling columns are untouched", 2_167_092, replaced.syncedHeight) + } + + /** + * The record vouches for the cursor written in its round, so a round + * that rolls back must take the record with it: a persisted record over + * a cursor still at its high-water would tell native the backfill + * resumed when it never started — the lossy direction. A malformed cover + * set (not a whole number of 68-byte entries) is refused outright. + */ + @Test + fun onWalletChangesetDashPayBackfillRollsBackWithItsRoundAndRefusesATornCoverSet() = runTest { + handler.onPersistWalletMetadata(walletId, testnet, groupId, 0) + handler.onChangesetBegin(walletId) + handler.onWalletChangesetHeader( + walletId = walletId, + hasSyncedHeight = true, + syncedHeight = 2_537_092, + hasBalance = false, + confirmedDelta = 0, + unconfirmedDelta = 0, + immatureDelta = 0, + lockedDelta = 0, + lastAppliedChainLockBytes = ByteArray(0), + ) + handler.onChangesetEnd(walletId, success = true) + + handler.onChangesetBegin(walletId) + handler.onWalletChangesetHeader( + walletId = walletId, + hasSyncedHeight = true, + syncedHeight = 2_167_092, + hasBalance = false, + confirmedDelta = 0, + unconfirmedDelta = 0, + immatureDelta = 0, + lockedDelta = 0, + lastAppliedChainLockBytes = ByteArray(0), + ) + handler.onWalletChangesetDashPayBackfill( + walletId, 2_167_092, 2_537_092, backfillEntry(1, 2, 2_167_092), 1, + ) + handler.onChangesetEnd(walletId, success = false) + + val row = db.walletDao().getByWalletId(walletId)!! + assertEquals("the rolled-back cursor stays at its committed value", 2_537_092, row.syncedHeight) + assertNull("the rolled-back record never lands", row.dashPayBackfillFloor) + assertNull(row.dashPayBackfillRewoundFrom) + assertNull(row.dashPayBackfillCovered) + + handler.onChangesetBegin(walletId) + val rc = handler.onWalletChangesetDashPayBackfill( + walletId, 2_167_092, 2_537_092, backfillEntry(1, 2, 2_167_092).copyOf(67), 1, + ) + handler.onChangesetEnd(walletId, success = rc == 0) + assertTrue("a torn cover set is refused", rc != 0) + assertNull(db.walletDao().getByWalletId(walletId)!!.dashPayBackfillFloor) + } + + /** + * The restore side of dashpay/platform#4302: a stored record comes back + * on `loadWalletList` exactly as written, and a wallet with no record + * (every pre-migration row) reports none, so native rewinds once for it + * as it always did. + */ + @Test + fun loadWalletListRoundTripsTheDashPayBackfillRecord() = runTest { + handler.onPersistWalletMetadata(walletId, testnet, groupId, 0) + val xpub = ByteArray(78) { 30 } + handler.onPersistAccountRegistration( + walletId, 0, 0, 0, 0, 0, ByteArray(0), ByteArray(0), xpub, + ) + + val before = handler.onLoadWalletList().single() + assertFalse("no record on a fresh wallet", before.hasDashPayBackfill) + assertEquals(0, before.dashPayBackfillFloor) + assertEquals(0, before.dashPayBackfillRewoundFrom) + assertEquals(0, before.dashPayBackfillCovered.size) + + val covered = backfillEntry(1, 2, 2_167_092) + backfillEntry(1, 3, 2_300_000) + handler.onChangesetBegin(walletId) + handler.onWalletChangesetDashPayBackfill(walletId, 2_167_092, 2_537_092, covered, 2) + handler.onChangesetEnd(walletId, success = true) + + val after = handler.onLoadWalletList().single() + assertTrue(after.hasDashPayBackfill) + assertEquals(2_167_092, after.dashPayBackfillFloor) + assertEquals(2_537_092, after.dashPayBackfillRewoundFrom) + assertTrue(covered.contentEquals(after.dashPayBackfillCovered)) + } + } /** diff --git a/packages/rs-platform-wallet-ffi/src/contact_persistence.rs b/packages/rs-platform-wallet-ffi/src/contact_persistence.rs index d0b79103821..416e82870dc 100644 --- a/packages/rs-platform-wallet-ffi/src/contact_persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/contact_persistence.rs @@ -149,6 +149,23 @@ pub struct ContactRequestFFI { /// Number of `u32` entries in [`Self::accepted_accounts`]; `0` when the /// pointer is null. pub accepted_accounts_len: usize, + /// Whether [`Self::external_account_reference`] is set — the mirror of + /// `EstablishedContact::external_account_reference` being `Some`. + /// Relationship-level, replicated onto BOTH established rows like + /// [`Self::payment_channel_broken`]; always `false` on pending rows. + /// + /// Without this the marker died with the process: every cold start saw + /// `None`, treated it as a rotation, tore the outbound + /// `DashpayExternalAccount` down and rebuilt it — and the rebuild rewound + /// the filter-scan cursor to the earliest contact's request height on + /// every launch (dashpay/platform#4302). A host that leaves it `false` + /// keeps that rebuild (the cursor no longer moves for it, but the + /// account churn remains). + pub has_external_account_reference: bool, + /// The `incoming_request.account_reference` the registered outbound + /// account was built from; meaningful only when + /// [`Self::has_external_account_reference`]. + pub external_account_reference: u32, } /// Composite identifier for [`ContactChangeSet::removed_sent`] and @@ -228,9 +245,12 @@ pub struct ContactIgnoredSenderFFI { // 176..=183 contact_account_label *const c_char // 184..=191 accepted_accounts *const u32 // 192..=199 accepted_accounts_len usize +// 200 has_external_account_reference bool +// 201..=203 (padding to 4) +// 204..=207 external_account_reference u32 // -// Total size = 200, alignment = 8 (from u64 / pointer fields). -const _: [u8; 200] = [0u8; std::mem::size_of::()]; +// Total size = 208, alignment = 8 (from u64 / pointer fields). +const _: [u8; 208] = [0u8; std::mem::size_of::()]; const _: [u8; 8] = [0u8; std::mem::align_of::()]; // Expected `ContactRequestRemovalFFI` layout: 64 bytes, alignment 1. @@ -297,6 +317,7 @@ impl ContactRequestFFI { false, None, &[], + None, ) } @@ -318,6 +339,7 @@ impl ContactRequestFFI { false, None, &[], + None, ) } @@ -339,6 +361,7 @@ impl ContactRequestFFI { note: Option<&str>, is_hidden: bool, accepted_accounts: &[u32], + external_account_reference: Option, ) -> Self { Self::from_parts( owner_id, @@ -353,6 +376,7 @@ impl ContactRequestFFI { // it is direction-specific (incoming-only). None, accepted_accounts, + external_account_reference, ) } @@ -371,6 +395,7 @@ impl ContactRequestFFI { is_hidden: bool, contact_account_label: Option<&str>, accepted_accounts: &[u32], + external_account_reference: Option, ) -> Self { Self::from_parts( owner_id, @@ -383,6 +408,7 @@ impl ContactRequestFFI { is_hidden, contact_account_label, accepted_accounts, + external_account_reference, ) } @@ -398,6 +424,7 @@ impl ContactRequestFFI { is_hidden: bool, contact_account_label: Option<&str>, accepted_accounts: &[u32], + external_account_reference: Option, ) -> Self { let (encrypted_public_key, encrypted_public_key_len) = allocate_byte_buffer(&request.encrypted_public_key); @@ -434,6 +461,8 @@ impl ContactRequestFFI { contact_account_label: allocate_c_string(contact_account_label), accepted_accounts, accepted_accounts_len, + has_external_account_reference: external_account_reference.is_some(), + external_account_reference: external_account_reference.unwrap_or(0), } } } @@ -692,6 +721,7 @@ mod tests { Some("a note"), true, &[], + None, ); let mut inc = ContactRequestFFI::from_established_incoming( owner, @@ -703,6 +733,7 @@ mod tests { true, None, &[], + None, ); assert!(out.is_outgoing); assert!(!inc.is_outgoing); @@ -726,6 +757,7 @@ mod tests { None, false, &[], + None, ); assert!(!healthy.payment_channel_broken); assert!(healthy.alias.is_null()); @@ -762,6 +794,7 @@ mod tests { None, false, &[], + None, ); let mut inc = ContactRequestFFI::from_established_incoming( owner, @@ -773,6 +806,7 @@ mod tests { false, Some("Main wallet"), &[], + None, ); assert!( @@ -796,6 +830,71 @@ mod tests { ); } + /// `EstablishedContact::external_account_reference` crosses as a + /// `(has, value)` pair on BOTH established rows (it is relationship-level, + /// like the broken-channel flag) and is absent on pending rows. Losing it + /// across a restart is what rebuilt every outbound account per launch + /// (dashpay/platform#4302). + #[test] + fn established_rows_carry_the_external_account_reference_pending_rows_do_not() { + let request = sample_request(); + let owner = [5u8; 32]; + let contact = [6u8; 32]; + + let mut out = ContactRequestFFI::from_established_outgoing( + owner, + contact, + &request, + false, + None, + None, + false, + &[], + Some(4), + ); + let mut inc = ContactRequestFFI::from_established_incoming( + owner, + contact, + &request, + false, + None, + None, + false, + None, + &[], + Some(4), + ); + for row in [&out, &inc] { + assert!(row.has_external_account_reference); + assert_eq!(row.external_account_reference, 4); + } + + let mut unstamped = ContactRequestFFI::from_established_incoming( + owner, + contact, + &request, + false, + None, + None, + false, + None, + &[], + None, + ); + assert!(!unstamped.has_external_account_reference); + assert_eq!(unstamped.external_account_reference, 0); + + let mut pending = ContactRequestFFI::from_incoming(owner, contact, &request); + assert!(!pending.has_external_account_reference); + + unsafe { + free_contact_requests_ffi(&mut out as *mut ContactRequestFFI, 1); + free_contact_requests_ffi(&mut inc as *mut ContactRequestFFI, 1); + free_contact_requests_ffi(&mut unstamped as *mut ContactRequestFFI, 1); + free_contact_requests_ffi(&mut pending as *mut ContactRequestFFI, 1); + } + } + /// `ContactIgnoredSenderFFI::new` must carry the `(owner, sender)` /// suppression key and the insert/remove `is_ignored` bit, so the /// Swift handler can persist an ignore (`true`) or delete an diff --git a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs index 811b4778894..dd56522df75 100644 --- a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs +++ b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs @@ -108,6 +108,50 @@ pub struct UtxoCreditVerdictFFI { pub spent_at_height: u32, } +/// One receival contact the DashPay coreHeight backfill covers, and the +/// height it covers it from — the C mirror of +/// [`DashPayBackfillCoveredContact`](platform_wallet::changeset::DashPayBackfillCoveredContact). +/// +/// Delivered through the persistence extension's +/// `on_persist_wallet_dashpay_backfill_fn` slot alongside the record's +/// `floor` / `rewound_from`, and handed back at restore on +/// `WalletRestoreEntryFFI::dashpay_backfill_covered`. Rides the extension +/// rather than `WalletChangeSetFFI` for the layout reason documented there: +/// that struct crosses by bare pointer and cannot prove a new field present, +/// while the extension's `struct_size` can. +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct DashPayBackfillCoveredContactFFI { + /// Our identity — the owner of the receival account. + pub owner_identity_id: [u8; 32], + /// The contact whose payments the receival account collects. + pub contact_identity_id: [u8; 32], + /// The receival account's index — coverage is per receival account, not + /// per relationship. + pub account_index: u32, + /// Lowest height the scan is guaranteed to test with this contact's + /// addresses watched. + pub covered_from: u32, +} + +/// Project a backfill record's cover set into its C mirrors for the +/// extension slot, in record order (sorted by `(owner, contact, +/// account_index)`). +pub fn build_dashpay_backfill_covered_for_callback( + record: &platform_wallet::changeset::DashPayBackfillRecord, +) -> Vec { + record + .covered() + .iter() + .map(|entry| DashPayBackfillCoveredContactFFI { + owner_identity_id: entry.owner.to_buffer(), + contact_identity_id: entry.contact.to_buffer(), + account_index: entry.account_index, + covered_from: entry.covered_from, + }) + .collect() +} + /// Project a changeset's credit verdicts into their C mirrors for the /// extension slot, in outpoint order (the map's own ordering — stable, /// so a host log of a round is reproducible). diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index 20f0c421b20..e1d4552733c 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -11,8 +11,9 @@ use crate::persistence::{ FFIPersister, FreeTrackedMasternodesFn, LoadIdentityBalanceBlockTimeFn, LoadTrackedMasternodesFn, PersistDpnsNameStatesFn, PersistIdentityBalanceBlockTimeFn, PersistTrackedMasternodesFn, PersistWalletChangesetChainLockHeightFn, - PersistWalletChangesetSweepsFn, PersistWalletChangesetUtxoVerdictsFn, PersistenceCallbacks, - PersistenceCallbacksExtension, PersistenceCapabilitiesFFI, PersistenceExtensionCallbacks, + PersistWalletChangesetSweepsFn, PersistWalletChangesetUtxoVerdictsFn, + PersistWalletDashPayBackfillFn, PersistenceCallbacks, PersistenceCallbacksExtension, + PersistenceCapabilitiesFFI, PersistenceExtensionCallbacks, PLATFORM_WALLET_PERSISTENCE_CALLBACKS_EXTENSION_VERSION, }; use crate::runtime::runtime; @@ -277,6 +278,10 @@ unsafe fn persistence_extension_callbacks( on_load_identity_balance_block_time_fn, LoadIdentityBalanceBlockTimeFn ), + wallet_dashpay_backfill: slot!( + on_persist_wallet_dashpay_backfill_fn, + PersistWalletDashPayBackfillFn + ), } } @@ -903,6 +908,17 @@ mod tests { 0 } + unsafe extern "C" fn persist_wallet_dashpay_backfill( + _context: *mut c_void, + _wallet_id: *const u8, + _floor: u32, + _rewound_from: u32, + _covered: *const crate::core_wallet_types::DashPayBackfillCoveredContactFFI, + _covered_count: usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn persist_tracked_masternodes( _context: *mut c_void, _network: *const std::os::raw::c_char, @@ -1255,12 +1271,43 @@ mod tests { assert!(read_short.wallet_changeset_sweeps.is_none()); assert!(read_short.wallet_changeset_chain_lock_height.is_none()); assert!(read_short.wallet_changeset_utxo_verdicts.is_none()); + assert!(read_short.wallet_dashpay_backfill.is_none()); let read_unknown = unsafe { persistence_extension_callbacks(&unknown) }; assert!(read_unknown.dpns_name_states.is_none()); assert!(read_unknown.load_tracked_masternodes.is_none()); assert!(read_unknown.wallet_changeset_sweeps.is_none()); assert!(read_unknown.wallet_changeset_chain_lock_height.is_none()); assert!(read_unknown.wallet_changeset_utxo_verdicts.is_none()); + assert!(read_unknown.wallet_dashpay_backfill.is_none()); + } + + /// A host whose `struct_size` stops right after the identity + /// balance-block-time slots (built before the DashPay backfill slot + /// existed) keeps every earlier slot and simply never has the backfill + /// slot read; a host declaring the full size yields it. + #[test] + fn dashpay_backfill_slot_is_gated_by_struct_size() { + let without = PersistenceCallbacksExtension { + struct_size: std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_wallet_dashpay_backfill_fn + ), + on_persist_wallet_changeset_utxo_verdicts_fn: Some( + persist_wallet_changeset_utxo_verdicts, + ), + on_persist_wallet_dashpay_backfill_fn: Some(persist_wallet_dashpay_backfill), + ..Default::default() + }; + let read = unsafe { persistence_extension_callbacks(&without) }; + assert!(read.wallet_changeset_utxo_verdicts.is_some()); + assert!(read.wallet_dashpay_backfill.is_none()); + + let with = PersistenceCallbacksExtension { + on_persist_wallet_dashpay_backfill_fn: Some(persist_wallet_dashpay_backfill), + ..Default::default() + }; + let read = unsafe { persistence_extension_callbacks(&with) }; + assert!(read.wallet_dashpay_backfill.is_some()); } #[test] diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index d701411bdb7..aba5e2ed668 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -54,8 +54,9 @@ use crate::contact_persistence::{ }; use crate::core_address_types::{AddressPoolTypeTagFFI, CoreAddressEntryFFI, KeyTypeTagFFI}; use crate::core_wallet_types::{ - build_sweep_batches_for_callback, build_utxo_credit_verdicts_for_callback, - free_wallet_changeset_ffi, SweepBatchFFI, UtxoCreditVerdictFFI, WalletChangeSetFFI, + build_dashpay_backfill_covered_for_callback, build_sweep_batches_for_callback, + build_utxo_credit_verdicts_for_callback, free_wallet_changeset_ffi, + DashPayBackfillCoveredContactFFI, SweepBatchFFI, UtxoCreditVerdictFFI, WalletChangeSetFFI, }; use crate::dashpay_payment::{build_payment_persist_entries, DashpayPaymentPersistEntryFFI}; use crate::dpns_name_state_persistence::{ @@ -276,6 +277,25 @@ pub type LoadIdentityBalanceBlockTimeFn = unsafe extern "C" fn( out_block_time: *mut crate::types::BlockTime, ) -> i32; +/// Carries the wallet's DashPay coreHeight backfill record +/// ([`DashPayBackfillRecord`](platform_wallet::changeset::DashPayBackfillRecord)) +/// whenever a round writes one: the rescan sweep stores it on the same round +/// as the lowered `synced_height` it belongs with, so a host that stores +/// both has a record that vouches only for a cursor it also stored. Fired +/// inside the round's begin/end bracket, after the core changeset callback. +/// Every write is the whole record — `floor`, `rewound_from`, and every +/// covered contact — so the host replaces, never merges. A host without +/// the slot keeps today's behaviour: the backfill re-fires on every launch, +/// slow but never lossy (dashpay/platform#4302). +pub type PersistWalletDashPayBackfillFn = unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + floor: u32, + rewound_from: u32, + covered: *const DashPayBackfillCoveredContactFFI, + covered_count: usize, +) -> i32; + /// Size- and version-tagged additive persistence callbacks. /// /// `context` is the context in the accompanying [`PersistenceCallbacks`] @@ -406,6 +426,22 @@ pub struct PersistenceCallbacksExtension { out_block_time: *mut crate::types::BlockTime, ) -> i32, >, + /// The wallet's DashPay backfill record (see + /// [`PersistWalletDashPayBackfillFn`]). Appended under the same version + /// for the same reason as the slots above: `struct_size` proves whether + /// a host allocated it, and a host that did not simply never has it read + /// — which is exactly today's behaviour, a backfill that re-fires on + /// every launch. + pub on_persist_wallet_dashpay_backfill_fn: Option< + unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + floor: u32, + rewound_from: u32, + covered: *const DashPayBackfillCoveredContactFFI, + covered_count: usize, + ) -> i32, + >, } impl Default for PersistenceCallbacksExtension { @@ -423,6 +459,7 @@ impl Default for PersistenceCallbacksExtension { on_persist_wallet_changeset_utxo_verdicts_fn: None, on_persist_identity_balance_block_time_fn: None, on_load_identity_balance_block_time_fn: None, + on_persist_wallet_dashpay_backfill_fn: None, } } } @@ -441,6 +478,7 @@ pub struct PersistenceExtensionCallbacks { pub wallet_changeset_utxo_verdicts: Option, pub persist_identity_balance_block_time: Option, pub load_identity_balance_block_time: Option, + pub wallet_dashpay_backfill: Option, } /// Return value by which a persistence callback reports a **retryable** @@ -1372,6 +1410,11 @@ pub struct FFIPersister { /// materialises those rows unspent, as every host did before the slot /// existed. wallet_changeset_utxo_verdicts_callback: Option, + /// `Some` only when the host's extension `struct_size` proved the slot + /// was allocated. Carries the DashPay backfill record a round writes; a + /// host without it re-runs the backfill on every launch, as every host + /// did before the slot existed (dashpay/platform#4302). + wallet_dashpay_backfill_callback: Option, /// Additive tracked-masternode persistence trio (persist / load / /// free), likewise extension-negotiated. tracked_masternodes_callbacks: PersistenceExtensionCallbacks, @@ -1496,6 +1539,7 @@ impl FFIPersister { wallet_changeset_chain_lock_height_callback: extensions .wallet_changeset_chain_lock_height, wallet_changeset_utxo_verdicts_callback: extensions.wallet_changeset_utxo_verdicts, + wallet_dashpay_backfill_callback: extensions.wallet_dashpay_backfill, tracked_masternodes_callbacks: extensions, persist_identity_balance_block_time_callback: extensions .persist_identity_balance_block_time, @@ -1788,6 +1832,20 @@ impl PlatformWalletPersistence for FFIPersister { Ok(out) } + fn stores_dashpay_backfill(&self) -> bool { + // The record reaches the host only through its extension slot; a + // host without it cannot store one, nor replace one it holds. And + // coverage vouches for the cursor it rides with, which reaches the + // host only through the changeset callback: `store` withholds every + // covering record from a host without one, so claiming the record + // here would let the owning layer count coverage as stored that the + // host never received. Such a host is treated like any other that + // cannot keep coverage durably, which still sends it the empty + // record that clears coverage it holds. + self.wallet_dashpay_backfill_callback.is_some() + && self.callbacks.on_persist_wallet_changeset_fn.is_some() + } + fn store_commits_inline(&self) -> bool { // The end callback commits (or rolls back) the host transaction before // `store` returns. `flush` is only a later general-purpose notification. @@ -1811,6 +1869,28 @@ impl PlatformWalletPersistence for FFIPersister { // the sync mutex only adds waiting under genuine round contention. let mut round = self.round_lock.lock(); + // An empty backfill record is an invalidation: Rust sends one to + // clear coverage the host handed back on load, before publishing a + // wallet that will scan without it. A host without the backfill slot + // can still hand coverage back — its load path decodes the record + // whether or not the write slot is wired — but it cannot replace it. + // Acknowledging the round would leave that coverage on disk beside + // cursors scanned without its accounts, for a later session with the + // slot to trust. So the round fails before anything is applied, and + // the publication with it (dashpay/platform#4302 review). + if self.wallet_dashpay_backfill_callback.is_none() + && changeset + .dashpay_backfill + .as_ref() + .is_some_and(|record| record.is_empty()) + { + return Err(PersistenceError::backend_with_kind( + PersistenceErrorKind::Fatal, + "the host holds a DashPay backfill record but registered no backfill \ + callback to invalidate it", + )); + } + // Open the round on the Rust side (rejects a nested begin / an // unclean round left open by a prior unwind — error, never // panic). Matched 1:1 with the `round.end_round()` below. @@ -1845,6 +1925,13 @@ impl PlatformWalletPersistence for FFIPersister { } } let mut outcome = RoundOutcome::default(); + // Whether this round's `synced_height` reached the host. A backfill + // record rides the round that lowers the cursor it vouches for, and + // must not reach the host when that cursor did not: a host without + // atomic rounds would keep the coverage over its old cursor, and no + // rollback on this side can take it back (dashpay/platform#4302 + // review). + let mut cursor_delivered = false; // Wallet-registration metadata. Fires at most once per round // (registration emits the entry; subsequent rounds carry @@ -2114,6 +2201,8 @@ impl PlatformWalletPersistence for FFIPersister { result ); outcome.record(result); + } else if core_cs.synced_height.is_some() { + cursor_delivered = true; } } @@ -2178,6 +2267,75 @@ impl PlatformWalletPersistence for FFIPersister { } } + // The DashPay backfill record rides its own size-negotiated slot, + // after the core changeset callback so the lowered cursor a rescan + // round carries is staged before the record that vouches for it. + // Whole-record semantics: the host replaces what it holds. + // + // A host that registered no changeset callback stores no cursor at + // all, so no record can vouch for one it holds: it never receives + // the record, not even on a record-only round. Withholding only the + // cursor-bearing round is not enough — that round still returns + // `Ok`, the reconcile records its cursor as durable, and a later + // record-only round would then deliver coverage beside a host cursor + // that never moved. + // + // Nor does a host whose rounds are not atomic: callbacks that run + // after this one (identities, payments, tokens, shielded) can still + // fail the round, and such a host would keep the cursor and the + // coverage from a round Rust reports as failed — coverage that then + // suppresses the rescan on the next launch. Only a host attesting + // `ATOMIC_CHANGESETS` rolls the whole round back, record included. + // + // Both gates guard COVERAGE. An empty record covers no contact, so it + // vouches for no cursor and is safe beside any, on any host. It is + // how Rust invalidates coverage a host already holds: + // `settle_published_backfill` sends one before publishing a wallet to + // a host that cannot keep coverage durably, so the host cannot + // restore an earlier run's coverage beside cursors this run scanned + // without it. Withholding it here would let `store` return `Ok` with + // the old coverage still on disk. So an empty record goes to every + // host with the slot, and a failing callback fails the round, which + // fails the publication. A host without the slot never received a + // record, so it holds none to invalidate. + let host_stores_cursors = self.callbacks.on_persist_wallet_changeset_fn.is_some() + && self + .persistence_capabilities() + .contains(PersistenceCapabilities::ATOMIC_CHANGESETS); + let backfill_cursor_owed = changeset + .core + .as_ref() + .is_some_and(|core| core.synced_height.is_some()); + if let Some(record) = changeset.dashpay_backfill.as_ref().filter(|record| { + record.is_empty() + || (host_stores_cursors && (!backfill_cursor_owed || cursor_delivered)) + }) { + if let Some(cb) = self.wallet_dashpay_backfill_callback { + let covered = build_dashpay_backfill_covered_for_callback(record); + let result = unsafe { + cb( + self.callbacks.context, + wallet_id.as_ptr(), + record.floor, + record.rewound_from, + if covered.is_empty() { + std::ptr::null() + } else { + covered.as_ptr() + }, + covered.len(), + ) + }; + if result != 0 { + eprintln!( + "Wallet DashPay backfill persistence callback returned error code {}", + result + ); + outcome.record(result); + } + } + } + // Send identity scalar changeset — upserts and removals. // Swift handler maps these onto `PersistentIdentity` row // upserts / tombstones. @@ -2593,6 +2751,7 @@ impl PlatformWalletPersistence for FFIPersister { established.note.as_deref(), established.is_hidden, &established.accepted_accounts, + established.external_account_reference, )); upserts.push(ContactRequestFFI::from_established_incoming( key.owner_id.to_buffer(), @@ -2606,6 +2765,7 @@ impl PlatformWalletPersistence for FFIPersister { // rides only the incoming row. established.contact_account_label.as_deref(), &established.accepted_accounts, + established.external_account_reference, )); } let removed_sent: Vec = contacts_cs @@ -5213,6 +5373,53 @@ fn provider_rebuild_error( /// the configured signer surface (see /// `Wallet::new_external_signable`). Earlier revisions of this code /// path produced a `WatchOnly` wallet — that has been replaced. +/// The persisted DashPay backfill record on a wallet-restore entry, or the +/// empty record when the host holds none. +/// +/// Empty is the fail-safe reading: a contact absent from the record is +/// re-armed by the next rescan sweep (one redundant rewind), whereas a +/// contact wrongly present would never be backfilled again. So a host that +/// reports the flag with a null cover set gets a record that covers no one, +/// and every entry is taken as the host stored it. +fn decode_dashpay_backfill( + entry: &WalletRestoreEntryFFI, +) -> platform_wallet::changeset::DashPayBackfillRecord { + use platform_wallet::changeset::{DashPayBackfillCoveredContact, DashPayBackfillRecord}; + + if !entry.has_dashpay_backfill { + return DashPayBackfillRecord::default(); + } + let covered: &[DashPayBackfillCoveredContactFFI] = + if entry.dashpay_backfill_covered.is_null() || entry.dashpay_backfill_covered_count == 0 { + &[] + } else { + // SAFETY: host-owned array of `count` entries, valid for the load + // callback window (the same contract as every sibling array). + unsafe { + slice::from_raw_parts( + entry.dashpay_backfill_covered, + entry.dashpay_backfill_covered_count, + ) + } + }; + // `from_entries` puts the cover set in canonical form whatever the host + // stored: one entry per account, a duplicate keeping its HIGHEST + // `covered_from`, so an ambiguous record fails closed. + DashPayBackfillRecord::from_entries( + entry.dashpay_backfill_floor, + entry.dashpay_backfill_rewound_from, + covered + .iter() + .map(|c| DashPayBackfillCoveredContact { + owner: Identifier::from(c.owner_identity_id), + contact: Identifier::from(c.contact_identity_id), + account_index: c.account_index, + covered_from: c.covered_from, + }) + .collect(), + ) +} + fn build_wallet_start_state( entry: &WalletRestoreEntryFFI, ) -> Result< @@ -5850,12 +6057,15 @@ fn build_wallet_start_state( // it. let unconfirmed_outgoing_txs = decode_unconfirmed_outgoing(entry); + let dashpay_backfill = decode_dashpay_backfill(entry); + let wallet_state = ClientWalletStartState { wallet, wallet_info, identity_manager, unused_asset_locks, unconfirmed_outgoing_txs, + dashpay_backfill, }; let platform_address_state = if per_account.is_empty() @@ -6376,6 +6586,7 @@ unsafe fn apply_contact_rows( is_hidden: bool, contact_account_label: Option, accepted_accounts: Vec, + external_account_reference: Option, } let opt_string = |ptr: *const std::os::raw::c_char| -> Option { @@ -6448,6 +6659,12 @@ unsafe fn apply_contact_rows( if acc.accepted_accounts.is_empty() { acc.accepted_accounts = u32s(row.accepted_accounts, row.accepted_accounts_len); } + // The outbound-account marker is relationship-level too; a host + // that never stored it hands back `false`, which restores `None` + // and keeps the conservative one-rebuild behaviour. + if acc.external_account_reference.is_none() && row.has_external_account_reference { + acc.external_account_reference = Some(row.external_account_reference); + } } for (contact_id_bytes, acc) in by_contact { @@ -6461,6 +6678,7 @@ unsafe fn apply_contact_rows( contact.payment_channel_broken = acc.payment_channel_broken; contact.contact_account_label = acc.contact_account_label; contact.accepted_accounts = acc.accepted_accounts; + contact.external_account_reference = acc.external_account_reference; managed.apply_established_contact(contact); } (Some(outgoing), None) => { @@ -8210,6 +8428,923 @@ mod tests { drop(persister); } + /// The DashPay backfill record reaches the host through its own + /// size-negotiated extension slot (dashpay/platform#4302): a round that + /// carries one fires it after the changeset callback with the whole + /// record — scalars and every covered contact — a round without one + /// never fires it, and a host without the slot still succeeds, keeping + /// the pre-record behaviour of a rewind per launch. + #[test] + fn store_delivers_the_dashpay_backfill_record_through_the_extension_slot() { + use platform_wallet::changeset::{CoreChangeSet, DashPayBackfillRecord}; + + #[derive(Default)] + struct Sink { + events: std::sync::Mutex>, + } + unsafe extern "C" fn record_changeset( + ctx: *mut c_void, + _wallet_id: *const u8, + _changeset: *const WalletChangeSetFFI, + ) -> i32 { + let sink = &*(ctx as *const Sink); + sink.events.lock().unwrap().push("changeset".into()); + 0 + } + unsafe extern "C" fn record_backfill( + ctx: *mut c_void, + wallet_id: *const u8, + floor: u32, + rewound_from: u32, + covered: *const DashPayBackfillCoveredContactFFI, + covered_count: usize, + ) -> i32 { + let sink = &*(ctx as *const Sink); + let wallet_id = std::slice::from_raw_parts(wallet_id, 32); + let covered: Vec = if covered.is_null() { + assert_eq!(covered_count, 0, "null cover set must carry count 0"); + Vec::new() + } else { + std::slice::from_raw_parts(covered, covered_count) + .iter() + .map(|c| { + format!( + "{}/{}@{}", + c.owner_identity_id[0], c.contact_identity_id[0], c.covered_from + ) + }) + .collect() + }; + sink.events.lock().unwrap().push(format!( + "backfill wallet={} floor={floor} rewound_from={rewound_from} covered=[{}]", + wallet_id[0], + covered.join(",") + )); + 0 + } + + let sink = Sink::default(); + let callbacks = PersistenceCallbacks { + context: &sink as *const Sink as *mut c_void, + on_changeset_begin_fn: Some(noop_begin), + on_changeset_end_fn: Some(noop_end), + on_persist_wallet_changeset_fn: Some(record_changeset), + ..PersistenceCallbacks::default() + }; + let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( + callbacks, + PersistenceCapabilities::ATOMIC_CHANGESETS, + PersistenceExtensionCallbacks { + wallet_dashpay_backfill: Some(record_backfill), + ..Default::default() + }, + ); + + // A round with no record: the slot stays silent. + persister + .store( + [1u8; 32], + PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: Some(500), + ..CoreChangeSet::default() + }), + ..PlatformWalletChangeSet::default() + }, + ) + .expect("round without a record"); + assert_eq!( + sink.events.lock().unwrap().clone(), + vec!["changeset".to_string()] + ); + + // The rescan's round: lowered cursor first, then the whole record. + let mut record = DashPayBackfillRecord::default(); + record.record_pass( + 2_537_092, + Some(2_167_092), + [ + ( + dpp::prelude::Identifier::from([9u8; 32]), + dpp::prelude::Identifier::from([2u8; 32]), + 0, + 2_300_000, + ), + ( + dpp::prelude::Identifier::from([1u8; 32]), + dpp::prelude::Identifier::from([3u8; 32]), + 0, + 2_167_092, + ), + ], + ); + sink.events.lock().unwrap().clear(); + persister + .store( + [7u8; 32], + PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: Some(2_167_092), + ..CoreChangeSet::default() + }), + dashpay_backfill: Some(record), + ..PlatformWalletChangeSet::default() + }, + ) + .expect("rescan round"); + assert_eq!( + sink.events.lock().unwrap().clone(), + vec![ + "changeset".to_string(), + "backfill wallet=7 floor=2167092 rewound_from=2537092 \ + covered=[1/3@2167092,9/2@2300000]" + .to_string(), + ], + "the record follows the changeset callback, in record order" + ); + + // An empty cover set crosses as a null pointer with count 0. + sink.events.lock().unwrap().clear(); + persister + .store( + [7u8; 32], + PlatformWalletChangeSet { + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + }, + ) + .expect("empty record round"); + assert_eq!( + sink.events.lock().unwrap().clone(), + vec!["backfill wallet=7 floor=0 rewound_from=0 covered=[]".to_string()] + ); + + // A host without the slot: a covering record is withheld and the + // round still succeeds — Rust never offers it one, since such a host + // does not store the record. + let sink = Sink::default(); + let callbacks = PersistenceCallbacks { + context: &sink as *const Sink as *mut c_void, + on_persist_wallet_changeset_fn: Some(record_changeset), + ..PersistenceCallbacks::default() + }; + let legacy = FFIPersister::new_with_persistence_capabilities( + callbacks, + PersistenceCapabilities::NONE, + ); + assert!(!legacy.stores_dashpay_backfill()); + let covering = DashPayBackfillRecord::from_entries( + 100, + 1_000, + vec![platform_wallet::changeset::DashPayBackfillCoveredContact { + owner: Identifier::from([0xAA; 32]), + contact: Identifier::from([0xBB; 32]), + account_index: 0, + covered_from: 100, + }], + ); + legacy + .store( + [7u8; 32], + PlatformWalletChangeSet { + dashpay_backfill: Some(covering), + ..PlatformWalletChangeSet::default() + }, + ) + .expect("a host without the slot still stores the round"); + assert!(sink.events.lock().unwrap().is_empty()); + // But an empty record is an invalidation of coverage the host handed + // back, which it cannot perform: the round fails before anything is + // applied, rather than being acknowledged. + let refused = legacy.store( + [7u8; 32], + PlatformWalletChangeSet { + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + }, + ); + assert!(matches!( + refused, + Err(ref error) if error.kind() == Some(PersistenceErrorKind::Fatal) + )); + assert!(sink.events.lock().unwrap().is_empty()); + drop(persister); + } + + /// A backfill record rides the round that lowers the cursor it vouches + /// for, and must not reach the host when that cursor did not — a host + /// without atomic rounds would keep the coverage over its old cursor + /// (dashpay/platform#4302 review). Withheld when the changeset callback + /// fails for a round that carries a cursor, and on every round — + /// record-only ones included — for a host with no changeset callback, + /// which holds no cursor a record could vouch for, or whose rounds are + /// not atomic, so a later callback's failure could not roll the record + /// back. Delivered for a record-only round on an atomic host that stores + /// cursors. + #[test] + fn a_backfill_record_is_withheld_when_its_cursor_did_not_reach_the_host() { + use platform_wallet::changeset::{CoreChangeSet, DashPayBackfillRecord}; + use std::sync::atomic::{AtomicUsize, Ordering}; + + #[derive(Default)] + struct Sink { + backfills: AtomicUsize, + } + unsafe extern "C" fn reject_changeset( + _ctx: *mut c_void, + _wallet_id: *const u8, + _changeset: *const WalletChangeSetFFI, + ) -> i32 { + 1 + } + unsafe extern "C" fn accept_changeset( + _ctx: *mut c_void, + _wallet_id: *const u8, + _changeset: *const WalletChangeSetFFI, + ) -> i32 { + 0 + } + unsafe extern "C" fn count_backfill( + ctx: *mut c_void, + _wallet_id: *const u8, + _floor: u32, + _rewound_from: u32, + _covered: *const DashPayBackfillCoveredContactFFI, + _covered_count: usize, + ) -> i32 { + (*(ctx as *const Sink)) + .backfills + .fetch_add(1, Ordering::SeqCst); + 0 + } + fn persister_with( + sink: &Sink, + changeset: Option, + atomic: bool, + ) -> FFIPersister { + FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + context: sink as *const Sink as *mut c_void, + on_changeset_begin_fn: atomic.then_some(noop_begin as _), + on_changeset_end_fn: atomic.then_some(noop_end as _), + on_persist_wallet_changeset_fn: changeset, + ..PersistenceCallbacks::default() + }, + if atomic { + PersistenceCapabilities::ATOMIC_CHANGESETS + } else { + PersistenceCapabilities::NONE + }, + PersistenceExtensionCallbacks { + wallet_dashpay_backfill: Some(count_backfill), + ..Default::default() + }, + ) + } + let persister = |sink: &Sink, changeset: Option| { + persister_with(sink, changeset, true) + }; + type PersistWalletChangesetFnForTest = + unsafe extern "C" fn(*mut c_void, *const u8, *const WalletChangeSetFFI) -> i32; + // The gates guard coverage, so the rounds below carry a record that + // covers a contact; an empty one is an invalidation (see the end). + let covering = || { + let mut record = DashPayBackfillRecord::default(); + record.record_pass( + 1_000, + Some(100), + [( + Identifier::from([0xAA; 32]), + Identifier::from([0xBB; 32]), + 0, + 100, + )], + ); + record + }; + let rewind_round = || PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: Some(100), + synced_height_is_rewind: true, + ..CoreChangeSet::default() + }), + dashpay_backfill: Some(covering()), + ..PlatformWalletChangeSet::default() + }; + + // The host rejected the cursor: the record must not follow it. + let sink = Sink::default(); + let rejected = persister(&sink, Some(reject_changeset)); + assert!(rejected.store([1u8; 32], rewind_round()).is_err()); + assert_eq!(sink.backfills.load(Ordering::SeqCst), 0); + + let record_only_round = || PlatformWalletChangeSet { + dashpay_backfill: Some(covering()), + ..PlatformWalletChangeSet::default() + }; + + // The host takes no cursors at all: a record over one is meaningless, + // on the rewind round and on every record-only round after it. + let sink = Sink::default(); + let cursorless = persister(&sink, None); + cursorless + .store([1u8; 32], rewind_round()) + .expect("nothing failed"); + cursorless + .store([1u8; 32], record_only_round()) + .expect("record-only round"); + assert_eq!(sink.backfills.load(Ordering::SeqCst), 0); + + // A host that stores cursors gets the record with its cursor, and a + // record-only round (no cursor to pair with) on its own. + let sink = Sink::default(); + let accepting = persister(&sink, Some(accept_changeset)); + accepting + .store([1u8; 32], rewind_round()) + .expect("rewind round"); + accepting + .store([1u8; 32], record_only_round()) + .expect("record-only round"); + assert_eq!(sink.backfills.load(Ordering::SeqCst), 2); + + // A host whose rounds are not atomic cannot roll back a cursor and a + // record whose round a later callback failed: it gets neither round's + // record. + let sink = Sink::default(); + let non_atomic = persister_with(&sink, Some(accept_changeset), false); + non_atomic + .store([1u8; 32], rewind_round()) + .expect("rewind round"); + non_atomic + .store([1u8; 32], record_only_round()) + .expect("record-only round"); + assert_eq!(sink.backfills.load(Ordering::SeqCst), 0); + + // An empty record covers no one, so it vouches for no cursor: it is + // an invalidation and reaches the cursorless and the non-atomic host + // alike, so neither can keep an earlier run's coverage + // (dashpay/platform#4302 review). + let invalidation = || PlatformWalletChangeSet { + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + }; + non_atomic + .store([1u8; 32], invalidation()) + .expect("invalidation"); + assert_eq!(sink.backfills.load(Ordering::SeqCst), 1); + let sink = Sink::default(); + let cursorless = persister(&sink, None); + cursorless + .store([1u8; 32], invalidation()) + .expect("invalidation"); + assert_eq!(sink.backfills.load(Ordering::SeqCst), 1); + } + + /// The lifecycle end of the invalidation above, through the real FFI + /// adapter, across sessions: an earlier session stored coverage the host + /// now hands back, and this session recreates the wallet without the + /// account it covers. The host's record must be cleared before the wallet + /// is published (dashpay/platform#4302 review): + /// - a host with the backfill slot but no atomic rounds gets the empty + /// record, and a failing callback fails the registration; + /// - a host WITHOUT the slot cannot clear it, so the registration fails + /// with a typed persistence error instead of being acknowledged; + /// - a host that hands back no record registers normally either way. + #[test] + fn a_recreated_wallet_must_clear_coverage_the_ffi_host_handed_back() { + use crate::event_handler::{EventHandlerCallbacks, FFIEventHandler}; + use crate::runtime::runtime; + use crate::wallet_restore_types::WalletRestoreEntryFFI; + use key_wallet::wallet::initialization::WalletAccountCreationOptions; + use platform_wallet::changeset::PersistenceErrorKind; + use platform_wallet::{PlatformWalletError, PlatformWalletManager}; + use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; + + struct Host { + wallet_id: [u8; 32], + holds_record: bool, + covered: [DashPayBackfillCoveredContactFFI; 1], + invalidations: AtomicUsize, + covering: AtomicUsize, + fail: AtomicBool, + } + unsafe extern "C" fn load_wallets( + ctx: *mut c_void, + entries: *mut *const WalletRestoreEntryFFI, + count: *mut usize, + ) -> i32 { + let host = &*(ctx as *const Host); + *entries = Box::into_raw(Box::new(WalletRestoreEntryFFI { + wallet_id: host.wallet_id, + has_dashpay_backfill: host.holds_record, + dashpay_backfill_floor: 100, + dashpay_backfill_rewound_from: 1_000, + dashpay_backfill_covered: if host.holds_record { + host.covered.as_ptr() + } else { + std::ptr::null() + }, + dashpay_backfill_covered_count: usize::from(host.holds_record), + ..Default::default() + })); + *count = 1; + 0 + } + unsafe extern "C" fn free_wallets( + _: *mut c_void, + entries: *const WalletRestoreEntryFFI, + _: usize, + ) { + drop(Box::from_raw(entries.cast_mut())); + } + unsafe extern "C" fn on_backfill( + ctx: *mut c_void, + _wallet_id: *const u8, + _floor: u32, + _rewound_from: u32, + _covered: *const DashPayBackfillCoveredContactFFI, + covered_count: usize, + ) -> i32 { + let host = &*(ctx as *const Host); + if covered_count == 0 { + host.invalidations.fetch_add(1, Ordering::SeqCst); + } else { + host.covering.fetch_add(1, Ordering::SeqCst); + } + i32::from(host.fail.load(Ordering::SeqCst)) + } + unsafe extern "C" fn accounts( + _: *mut c_void, + _: *const u8, + _: *const AccountSpecFFI, + _: usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn pools( + _: *mut c_void, + _: *const u8, + _: *const AccountAddressPoolFFI, + _: usize, + ) -> i32 { + 0 + } + // `atomic_accounts`: atomic rounds and both account writers, but no + // changeset callback, so no cursor reaches the host. + fn manager_for( + host: Option<&'static Host>, + slot: bool, + atomic_accounts: bool, + ) -> PlatformWalletManager { + let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + context: host.map_or(std::ptr::null_mut(), |host| { + host as *const Host as *mut c_void + }), + on_load_wallet_list_fn: host.map(|_| load_wallets as _), + on_load_wallet_list_free_fn: host.map(|_| free_wallets as _), + on_changeset_begin_fn: atomic_accounts.then_some(noop_begin as _), + on_changeset_end_fn: atomic_accounts.then_some(noop_end as _), + on_persist_account_registrations_fn: atomic_accounts.then_some(accounts as _), + on_persist_account_address_pools_fn: atomic_accounts.then_some(pools as _), + ..PersistenceCallbacks::default() + }, + if atomic_accounts { + PersistenceCapabilities::ATOMIC_CHANGESETS + .union(PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS) + } else { + PersistenceCapabilities::NONE + }, + PersistenceExtensionCallbacks { + wallet_dashpay_backfill: slot.then_some(on_backfill as _), + ..Default::default() + }, + ); + let events = FFIEventHandler::new( + EventHandlerCallbacks { + context: std::ptr::null_mut(), + on_wallet_event_fn: None, + on_error_fn: None, + on_platform_address_sync_completed_fn: None, + on_shielded_sync_completed_fn: None, + on_shielded_sync_progress_fn: None, + on_shielded_tree_progress_fn: None, + release_fn: None, + }, + None, + ); + let _runtime_guard = runtime().enter(); + PlatformWalletManager::new( + Arc::new( + dash_sdk::SdkBuilder::new_mock() + .with_network(dashcore::Network::Testnet) + .build() + .expect("mock sdk"), + ), + Arc::new(persister), + Arc::new(events), + ) + } + let create = |manager: &PlatformWalletManager| { + runtime().block_on(manager.create_wallet_from_seed_bytes( + dashcore::Network::Testnet, + &[42; 64], + WalletAccountCreationOptions::Default, + Some(0), + )) + }; + let wallet_id = create(&manager_for(None, true, false)) + .expect("the id this seed registers under") + .wallet_id(); + let host = |holds_record: bool| -> &'static Host { + Box::leak(Box::new(Host { + wallet_id, + holds_record, + covered: [DashPayBackfillCoveredContactFFI { + owner_identity_id: [0xAA; 32], + contact_identity_id: [0xBB; 32], + account_index: 0, + covered_from: 200, + }], + invalidations: AtomicUsize::new(0), + covering: AtomicUsize::new(0), + fail: AtomicBool::new(false), + })) + }; + + // The slot, no atomic rounds: the invalidation is delivered, and a + // host that fails it fails the registration. + let with_slot = host(true); + with_slot.fail.store(true, Ordering::SeqCst); + let manager = manager_for(Some(with_slot), true, false); + assert!( + create(&manager).is_err(), + "an invalidation the host failed must fail the registration" + ); + with_slot.fail.store(false, Ordering::SeqCst); + create(&manager).expect("the invalidation lands"); + assert_eq!(with_slot.invalidations.load(Ordering::SeqCst), 2); + assert_eq!( + with_slot.covering.load(Ordering::SeqCst), + 0, + "a host that is not atomic never receives coverage" + ); + + // No slot: the coverage cannot be cleared, so nothing is published. + let manager = manager_for(Some(host(true)), false, false); + match create(&manager) { + Ok(_) => panic!("coverage the host cannot clear must block the registration"), + Err(PlatformWalletError::PersisterStore(source)) => { + assert_eq!(source.kind(), Some(PersistenceErrorKind::Fatal)); + } + Err(other) => panic!("expected a typed store failure, got {other:?}"), + } + assert!(runtime().block_on(manager.get_wallet(&wallet_id)).is_none()); + + // A host that hands back no record keeps the no-record fallback, + // with or without the slot. + for slot in [false, true] { + let clean = host(false); + create(&manager_for(Some(clean), slot, false)).expect("nothing to clear"); + assert_eq!(clean.invalidations.load(Ordering::SeqCst), 0); + } + + // Atomic rounds and both account writers, but no cursor callback: + // the host cannot keep coverage, so it is cleared like on any other + // such host and never offered coverage. + let cursorless = host(true); + create(&manager_for(Some(cursorless), true, true)).expect("the invalidation lands"); + assert_eq!(cursorless.invalidations.load(Ordering::SeqCst), 1); + assert_eq!(cursorless.covering.load(Ordering::SeqCst), 0); + } + + /// Coverage counts as durable on an FFI host only when everything it + /// rides with can reach the host: atomic rounds, both account writers, + /// the backfill slot, AND the changeset callback that carries the cursor + /// the coverage vouches for. `store` withholds every covering record + /// from a host without that callback, so advertising the record there + /// let the owning layer treat suppressed coverage as stored + /// (dashpay/platform#4302 review). + #[test] + fn backfill_coverage_needs_a_host_that_stores_its_cursor() { + use platform_wallet::changeset::persists_backfill_coverage; + + unsafe extern "C" fn begin(_: *mut c_void, _: *const u8) -> i32 { + 0 + } + unsafe extern "C" fn end(_: *mut c_void, _: *const u8, _: bool) -> i32 { + 0 + } + unsafe extern "C" fn accounts( + _: *mut c_void, + _: *const u8, + _: *const AccountSpecFFI, + _: usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn pools( + _: *mut c_void, + _: *const u8, + _: *const AccountAddressPoolFFI, + _: usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn changeset( + _: *mut c_void, + _: *const u8, + _: *const WalletChangeSetFFI, + ) -> i32 { + 0 + } + unsafe extern "C" fn backfill( + _: *mut c_void, + _: *const u8, + _: u32, + _: u32, + _: *const DashPayBackfillCoveredContactFFI, + _: usize, + ) -> i32 { + 0 + } + let host = |cursor_callback: bool, slot: bool| { + FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + on_changeset_begin_fn: Some(begin), + on_changeset_end_fn: Some(end), + on_persist_account_registrations_fn: Some(accounts), + on_persist_account_address_pools_fn: Some(pools), + on_persist_wallet_changeset_fn: cursor_callback.then_some(changeset as _), + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::ATOMIC_CHANGESETS + .union(PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS), + PersistenceExtensionCallbacks { + wallet_dashpay_backfill: slot.then_some(backfill as _), + ..Default::default() + }, + ) + }; + assert!( + !persists_backfill_coverage(&host(false, true)), + "an atomic, account-capable host with the slot but no cursor callback" + ); + assert!(!persists_backfill_coverage(&host(true, false))); + assert!(persists_backfill_coverage(&host(true, true))); + } + + /// An atomic FFI host keeps outbound-account markers on disk only when it + /// also wires both account writers: `ACCOUNT_ADDRESS_POOLS` is derived + /// from them, and a host without them would acknowledge an account it + /// never stored beside a marker naming it (dashpay/platform#4302 review). + #[test] + fn outbound_markers_need_an_atomic_host_with_account_writers() { + use platform_wallet::changeset::persists_outbound_account_markers; + + unsafe extern "C" fn begin(_: *mut c_void, _: *const u8) -> i32 { + 0 + } + unsafe extern "C" fn end(_: *mut c_void, _: *const u8, _: bool) -> i32 { + 0 + } + unsafe extern "C" fn accounts( + _: *mut c_void, + _: *const u8, + _: *const AccountSpecFFI, + _: usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn pools( + _: *mut c_void, + _: *const u8, + _: *const AccountAddressPoolFFI, + _: usize, + ) -> i32 { + 0 + } + let declared = PersistenceCapabilities::ATOMIC_CHANGESETS + .union(PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS); + let host = |registrations: bool, address_pools: bool| { + FFIPersister::new_with_persistence_capabilities( + PersistenceCallbacks { + on_changeset_begin_fn: Some(begin), + on_changeset_end_fn: Some(end), + on_persist_account_registrations_fn: registrations.then_some(accounts as _), + on_persist_account_address_pools_fn: address_pools.then_some(pools as _), + ..PersistenceCallbacks::default() + }, + declared, + ) + }; + assert!(!persists_outbound_account_markers(&host(false, false))); + assert!(!persists_outbound_account_markers(&host(true, false))); + assert!(!persists_outbound_account_markers(&host(false, true))); + assert!(persists_outbound_account_markers(&host(true, true))); + } + + /// Why the outbound-account marker may ride an account's round only on + /// an atomic host: without begin/end brackets `FFIPersister` cannot claim + /// `ATOMIC_CHANGESETS` even when the host declares it, and it carries on + /// to the contacts callback after the account callback failed. A marker + /// in that round would land beside the account the host still holds, so + /// `register_external_contact_account` leaves it out of rounds on such a + /// host (dashpay/platform#4302 review). + #[test] + fn a_non_atomic_host_keeps_contact_rows_after_rejecting_the_account() { + use dashcore::secp256k1::Secp256k1; + use key_wallet::account::AccountType; + use key_wallet::bip32::{ExtendedPrivKey, ExtendedPubKey}; + use key_wallet::mnemonic::Mnemonic; + use platform_wallet::changeset::{ + AccountRegistrationEntry, ContactChangeSet, SentContactRequestKey, + }; + use platform_wallet::wallet::identity::{ContactRequest, EstablishedContact}; + use std::sync::atomic::{AtomicUsize, Ordering}; + + #[derive(Default)] + struct Host { + marked_rows: AtomicUsize, + } + unsafe extern "C" fn reject_accounts( + _: *mut c_void, + _: *const u8, + _: *const AccountSpecFFI, + _: usize, + ) -> i32 { + 1 + } + #[allow(clippy::too_many_arguments)] + unsafe extern "C" fn keep_contacts( + ctx: *mut c_void, + _: *const u8, + upserts: *const ContactRequestFFI, + count: usize, + _: *const ContactRequestRemovalFFI, + _: usize, + _: *const ContactRequestRemovalFFI, + _: usize, + _: *const ContactIgnoredSenderFFI, + _: usize, + ) -> i32 { + let host = &*(ctx as *const Host); + for row in std::slice::from_raw_parts(upserts, count) { + if row.has_external_account_reference { + host.marked_rows.fetch_add(1, Ordering::SeqCst); + } + } + 0 + } + + let host = Host::default(); + let persister = FFIPersister::new_with_persistence_capabilities( + PersistenceCallbacks { + context: &host as *const Host as *mut c_void, + on_persist_account_registrations_fn: Some(reject_accounts), + on_persist_contacts_fn: Some(keep_contacts), + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::ATOMIC_CHANGESETS, + ); + assert!( + !persister + .persistence_capabilities() + .contains(PersistenceCapabilities::ATOMIC_CHANGESETS), + "a declaration without begin/end brackets is not an atomic round" + ); + + let seed = Mnemonic::from_phrase( + "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", + ) + .expect("static BIP-39 vector") + .to_seed(""); + let master = + ExtendedPrivKey::new_master(dashcore::Network::Testnet, &seed).expect("master"); + let xpub = ExtendedPubKey::from_priv(&Secp256k1::new(), &master); + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0x01; 32]); + let mut established = EstablishedContact::new( + contact, + ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0), + ContactRequest::new(contact, owner, 0, 0, 8, vec![0u8; 96], 100, 0), + ); + established.external_account_reference = Some(8); + let mut contacts = ContactChangeSet::default(); + contacts.established.insert( + SentContactRequestKey { + owner_id: owner, + recipient_id: contact, + }, + established, + ); + let round = PlatformWalletChangeSet { + account_registrations: vec![AccountRegistrationEntry { + account_type: AccountType::DashpayExternalAccount { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }, + account_xpub: xpub, + }], + contacts: Some(contacts), + ..PlatformWalletChangeSet::default() + }; + assert!(persister.store([7u8; 32], round).is_err()); + assert!( + host.marked_rows.load(Ordering::SeqCst) > 0, + "the contact rows still reached the host after the account was rejected" + ); + } + + /// The restore side of dashpay/platform#4302: a wallet-restore entry + /// carrying the record decodes into the start state's + /// `dashpay_backfill` in canonical form, and an entry without it — the + /// zero-init default every pre-record host produces — decodes as the + /// empty record, so the rescan sweep rewinds once as it always did. + #[test] + fn wallet_restore_decodes_the_dashpay_backfill_record() { + use platform_wallet::changeset::DashPayBackfillRecord; + + let none = WalletRestoreEntryFFI::default(); + assert!(!none.has_dashpay_backfill); + assert!(super::decode_dashpay_backfill(&none).is_empty()); + + // The flag alone, with a null cover set: a record that covers no + // one, never a torn read. + let bare = WalletRestoreEntryFFI { + has_dashpay_backfill: true, + dashpay_backfill_floor: 10, + dashpay_backfill_rewound_from: 20, + ..Default::default() + }; + let decoded = super::decode_dashpay_backfill(&bare); + assert!(decoded.is_empty()); + assert_eq!((decoded.floor, decoded.rewound_from), (10, 20)); + + // Out of order and duplicated, as a host might hand it back: the + // decoded record is sorted with one entry per account, the highest + // height of a duplicate kept. + let covered = [ + DashPayBackfillCoveredContactFFI { + owner_identity_id: [9u8; 32], + contact_identity_id: [2u8; 32], + account_index: 0, + covered_from: 2_300_000, + }, + DashPayBackfillCoveredContactFFI { + owner_identity_id: [1u8; 32], + contact_identity_id: [3u8; 32], + account_index: 0, + covered_from: 2_167_092, + }, + DashPayBackfillCoveredContactFFI { + owner_identity_id: [9u8; 32], + contact_identity_id: [2u8; 32], + account_index: 0, + covered_from: 2_999_999, + }, + ]; + let entry = WalletRestoreEntryFFI { + has_dashpay_backfill: true, + dashpay_backfill_floor: 2_167_092, + dashpay_backfill_rewound_from: 2_537_092, + dashpay_backfill_covered: covered.as_ptr(), + dashpay_backfill_covered_count: covered.len(), + ..Default::default() + }; + let decoded = super::decode_dashpay_backfill(&entry); + let mut expected = DashPayBackfillRecord::default(); + expected.record_pass( + 2_537_092, + Some(2_167_092), + [ + ( + dpp::prelude::Identifier::from([1u8; 32]), + dpp::prelude::Identifier::from([3u8; 32]), + 0, + 2_167_092, + ), + ( + dpp::prelude::Identifier::from([9u8; 32]), + dpp::prelude::Identifier::from([2u8; 32]), + 0, + 2_999_999, + ), + ], + ); + assert_eq!(decoded, expected); + assert_eq!( + decoded.covered_from( + &dpp::prelude::Identifier::from([9u8; 32]), + &dpp::prelude::Identifier::from([2u8; 32]), + 0 + ), + Some(2_999_999), + "a duplicated account keeps its highest height, so the record fails closed" + ); + } + /// The numeric chainlock height reaches the host through its own /// size-negotiated extension slot: a chainlock-advancing round fires it /// after the changeset callback with the height a non-Rust host cannot @@ -8705,6 +9840,16 @@ mod tests { PersistenceCallbacksExtension, on_load_identity_balance_block_time_fn ) + std::mem::size_of::>(), + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_wallet_dashpay_backfill_fn + ) + ); + assert_eq!( + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_wallet_dashpay_backfill_fn + ) + std::mem::size_of::>(), std::mem::size_of::() ); assert_eq!( @@ -10141,6 +11286,7 @@ mod tests { Some("a note"), true, &[7, 42], + Some(4), ), ContactRequestFFI::from_established_incoming( owner.to_buffer(), @@ -10152,6 +11298,7 @@ mod tests { true, Some("Main wallet"), &[7, 42], + Some(4), ), ]; @@ -10208,6 +11355,11 @@ mod tests { assert_eq!(e.note.as_deref(), Some("a note")); assert!(e.is_hidden); assert!(e.payment_channel_broken); + assert_eq!( + e.external_account_reference, + Some(4), + "the outbound-account marker must survive the restore (dashpay/platform#4302)" + ); assert_eq!( e.contact_account_label.as_deref(), Some("Main wallet"), diff --git a/packages/rs-platform-wallet-ffi/src/wallet_restore_types.rs b/packages/rs-platform-wallet-ffi/src/wallet_restore_types.rs index 22a71ade25b..a835b46b6f0 100644 --- a/packages/rs-platform-wallet-ffi/src/wallet_restore_types.rs +++ b/packages/rs-platform-wallet-ffi/src/wallet_restore_types.rs @@ -718,6 +718,25 @@ pub struct WalletRestoreEntryFFI { /// leaves every existing field where it was. pub unconfirmed_outgoing_tx_records: *const UnconfirmedOutgoingTxRecordFFI, pub unconfirmed_outgoing_tx_records_count: usize, + /// Whether the host holds a persisted DashPay backfill record for this + /// wallet (dashpay/platform#4302). `false` — the zero-init default — for + /// a wallet that never rewound for a contact, a row persisted before the + /// record existed, or a host that does not store the slot; the rescan + /// sweep then behaves as it always did and writes one. The three fields + /// below are read only when this is `true`. + pub has_dashpay_backfill: bool, + /// Lowest height the backfill rewound the cursor to. + pub dashpay_backfill_floor: u32, + /// Highest cursor the backfill rewound from — the height the scan climbs + /// back to for the backfill to be complete. + pub dashpay_backfill_rewound_from: u32, + /// Receival contacts the backfill covers, each with the height it is + /// covered from. Host-owned for the callback window, freed by + /// `LoadWalletListFreeFn`; `null` / `0` when the record covers no one. + /// Appended at the end, like every addition to this `#[repr(C)]` struct, + /// so no existing offset moves. + pub dashpay_backfill_covered: *const crate::core_wallet_types::DashPayBackfillCoveredContactFFI, + pub dashpay_backfill_covered_count: usize, } /// Every field named explicitly so that adding a field to this ABI struct @@ -758,6 +777,11 @@ impl Default for WalletRestoreEntryFFI { last_applied_chain_lock_bytes_len: 0, unconfirmed_outgoing_tx_records: std::ptr::null(), unconfirmed_outgoing_tx_records_count: 0, + has_dashpay_backfill: false, + dashpay_backfill_floor: 0, + dashpay_backfill_rewound_from: 0, + dashpay_backfill_covered: std::ptr::null(), + dashpay_backfill_covered_count: 0, } } } diff --git a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs index 9c2a11f7cd7..82fda2837ca 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs @@ -1859,6 +1859,10 @@ fn load_one_wallet( // replay inert here, which is the behaviour this path had before the // field existed. unconfirmed_outgoing_txs: Vec::new(), + // Not persisted by this backend yet: the backfill record rides the + // FFI hosts' wallet row only. Empty keeps the rescan sweep on its + // pre-record behaviour here (a rewind per launch, never lossy). + dashpay_backfill: Default::default(), }) } diff --git a/packages/rs-platform-wallet-storage/src/sqlite/schema/versions.rs b/packages/rs-platform-wallet-storage/src/sqlite/schema/versions.rs index bfbb9af6486..de2b418cae1 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/schema/versions.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/schema/versions.rs @@ -144,6 +144,10 @@ pub fn touched_domains(cs: &PlatformWalletChangeSet) -> Vec { invitations, dpns_name_states, identity_scan_state, + // Not persisted by this backend yet (see `ClientWalletStartState:: + // dashpay_backfill` on the load side): the record rides the FFI + // hosts' wallet row only, so it touches no domain here. + dashpay_backfill: _, #[cfg(feature = "shielded")] shielded, #[cfg(not(feature = "shielded"))] diff --git a/packages/rs-platform-wallet/src/changeset/changeset.rs b/packages/rs-platform-wallet/src/changeset/changeset.rs index 702b8abd2d0..f2c5a9f1795 100644 --- a/packages/rs-platform-wallet/src/changeset/changeset.rs +++ b/packages/rs-platform-wallet/src/changeset/changeset.rs @@ -34,6 +34,7 @@ use key_wallet::managed_account::address_pool::{AddressPool, AddressPoolType}; use key_wallet::managed_account::transaction_record::TransactionRecord; use key_wallet::{AddressInfo, Network, PlatformP2PKHAddress, Utxo}; +use crate::changeset::dashpay_backfill::DashPayBackfillRecord; use crate::changeset::identity_scan_state::IdentityScanStateEntry; use crate::wallet::platform_wallet::WalletId; @@ -163,6 +164,22 @@ pub struct CoreChangeSet { /// on merge. pub synced_height: Option, + /// Whether [`synced_height`](Self::synced_height) is a REWIND rather than + /// an advance. The DashPay rescan reconcile lowers the durable cursor + /// deliberately and stores the lowered value with the backfill record + /// that vouches for it; that value must replace whatever the round is + /// merged behind instead of losing to the ordinary monotonic max. + /// + /// Merge is an ordered reset/advance composition — `advance(a)·advance(b) + /// = advance(max)`, `x·rewind(b) = rewind(b)`, `rewind(a)·advance(b) = + /// rewind(max(a, b))` — which is associative, so how a backend groups + /// rounds into accumulations never changes the result. Meaningless while + /// `synced_height` is `None`. Hosts that write the cursor they are handed + /// honour it directly; the SQLite backend keeps its monotonic max and + /// does not store the backfill record, so it neither needs nor sees it. + #[cfg_attr(feature = "serde", serde(default))] + pub synced_height_is_rewind: bool, + /// Addresses freshly derived as a side effect of processing the /// records in this batch — i.e. `WalletEvent::TransactionDetected. /// addresses_derived` and `WalletEvent::BlockProcessed. @@ -748,7 +765,21 @@ impl Merge for CoreChangeSet { ); } if let Some(h) = other.synced_height { - self.synced_height = Some(self.synced_height.map_or(h, |existing| existing.max(h))); + if other.synced_height_is_rewind { + // A rewind replaces whatever it is merged behind. + self.synced_height = Some(h); + self.synced_height_is_rewind = true; + } else { + // An advance composes by max, and keeps a rewind a rewind: + // `rewind(a)·advance(b)` still has to replace anything an + // outer accumulation merges this result behind. A flag with + // no height beside it is meaningless, so it must not turn + // this advance into a rewind. + if self.synced_height.is_none() { + self.synced_height_is_rewind = false; + } + self.synced_height = Some(self.synced_height.map_or(h, |existing| existing.max(h))); + } } // Derived-address dedup. Within a single `WalletEvent` the @@ -2155,6 +2186,22 @@ pub struct PlatformWalletChangeSet { /// scan is retried inside its own launch — but honouring the verdict /// across launches needs the host slot. pub identity_scan_state: Option, + /// Durable record of the DashPay coreHeight backfill — which receival + /// contacts the historical rescan covers, from which height each, and + /// the extent of the rescan. Written by `reconcile_dashpay_rescan` on the + /// same round as the lowered `core.synced_height` it belongs with, and + /// read back through + /// [`ClientWalletStartState::dashpay_backfill`](crate::changeset::ClientWalletStartState::dashpay_backfill) + /// so a fresh process does not rewind for a contact the previous one + /// already rewound for (dashpay/platform#4302). Every write carries the + /// whole record, so merge is last-write-wins. The lowered cursor it + /// travels with is marked [`CoreChangeSet::synced_height_is_rewind`], so + /// it survives any accumulation. See [`DashPayBackfillRecord`]. + /// + /// Durability caveat, the same one `identity_scan_state` carries: a host + /// that has not adopted the slot keeps today's behaviour — the backfill + /// re-fires on every launch — which is slow but never lossy. + pub dashpay_backfill: Option, /// Per-account registration entries emitted at registration / on /// later `add_account` calls. See [`AccountRegistrationEntry`] for /// the merge policy (plain `Vec::extend`, dedup is the apply-side @@ -2306,6 +2353,11 @@ impl Merge for PlatformWalletChangeSet { None => scan, }); } + // Backfill record: every write is a whole snapshot, so the later + // one wins outright. + if let Some(record) = other.dashpay_backfill { + self.dashpay_backfill = Some(record); + } // Per-account specs and address-pool snapshots: append-only. // See the type docstrings for the rationale (registration // round emits each key once; snapshots are whole-pool, so @@ -2342,6 +2394,7 @@ impl Merge for PlatformWalletChangeSet { .is_none_or(|m| m.is_empty()) && self.wallet_metadata.is_none() && self.identity_scan_state.is_none() + && self.dashpay_backfill.is_none() && self.account_registrations.is_empty() && self.provider_key_account_registrations.is_empty() && self.account_address_pools.is_empty() @@ -3337,6 +3390,119 @@ mod tests { assert_eq!(cs.addresses_marked_used.len(), 3); } + /// The rewind marker composes associatively: however a backend groups + /// rounds into accumulations, a rewind replaces what precedes it, an + /// advance after a rewind raises it and keeps it a rewind, and a round + /// that carries only a backfill record changes no cursor. The earlier + /// version inferred "rewind" from a record and a cursor riding one round, + /// which grouping could manufacture (dashpay/platform#4302 review). + #[test] + fn the_rewind_marker_composes_associatively() { + use crate::changeset::DashPayBackfillRecord; + + fn advance(h: u32) -> PlatformWalletChangeSet { + PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: Some(h), + ..CoreChangeSet::default() + }), + ..PlatformWalletChangeSet::default() + } + } + fn rewind(h: u32) -> PlatformWalletChangeSet { + PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: Some(h), + synced_height_is_rewind: true, + ..CoreChangeSet::default() + }), + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + } + } + // A changeset carrying the flag but no height, as an external + // constructor could build one: the flag is meaningless there. + fn heightless(flag: bool) -> PlatformWalletChangeSet { + PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: None, + synced_height_is_rewind: flag, + ..CoreChangeSet::default() + }), + ..PlatformWalletChangeSet::default() + } + } + fn record_only() -> PlatformWalletChangeSet { + PlatformWalletChangeSet { + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + } + } + fn fold(parts: Vec) -> PlatformWalletChangeSet { + let mut acc = PlatformWalletChangeSet::default(); + for part in parts { + acc.merge(part); + } + acc + } + fn cursor(cs: &PlatformWalletChangeSet) -> (Option, bool) { + cs.core.as_ref().map_or((None, false), |core| { + (core.synced_height, core.synced_height_is_rewind) + }) + } + // Both parenthesizations of every triple. + type Case = ( + &'static str, + fn() -> Vec, + (Option, bool), + ); + let cases: Vec = vec![ + ( + "advance, lower advance, record-only", + || vec![advance(1_000), advance(100), record_only()], + (Some(1_000), false), + ), + ( + "advance, rewind, later advance", + || vec![advance(1_000), rewind(100), advance(150)], + (Some(150), true), + ), + ( + "rewind, advance, rewind", + || vec![rewind(500), advance(900), rewind(200)], + (Some(200), true), + ), + ( + "advance, advance, rewind", + || vec![advance(1_000), advance(1_200), rewind(100)], + (Some(100), true), + ), + ( + "advance, heightless flagged, lower advance", + || vec![advance(1_000), heightless(true), advance(100)], + (Some(1_000), false), + ), + ( + "advance, heightless unflagged, lower advance", + || vec![advance(1_000), heightless(false), advance(100)], + (Some(1_000), false), + ), + ( + "rewind, heightless flagged, advance", + || vec![rewind(500), heightless(true), advance(900)], + (Some(900), true), + ), + ]; + for (name, parts, expected) in cases { + let [a, b, c]: [PlatformWalletChangeSet; 3] = parts().try_into().ok().unwrap(); + let left = fold(vec![fold(vec![a, b]), c]); + let [a, b, c]: [PlatformWalletChangeSet; 3] = parts().try_into().ok().unwrap(); + let right = fold(vec![a, fold(vec![b, c])]); + assert_eq!(cursor(&left), expected, "{name}: (A·B)·C"); + assert_eq!(cursor(&right), expected, "{name}: A·(B·C)"); + } + } + /// Highest-used watermarks merge monotonic-max per account per /// pool slot: a later batch can only advance a slot, and `None` /// never erases a prior `Some`. diff --git a/packages/rs-platform-wallet/src/changeset/client_wallet_start_state.rs b/packages/rs-platform-wallet/src/changeset/client_wallet_start_state.rs index 6eab3d88780..90f18b08e5a 100644 --- a/packages/rs-platform-wallet/src/changeset/client_wallet_start_state.rs +++ b/packages/rs-platform-wallet/src/changeset/client_wallet_start_state.rs @@ -7,6 +7,7 @@ use std::collections::BTreeMap; +use crate::changeset::dashpay_backfill::DashPayBackfillRecord; use crate::changeset::identity_manager_start_state::IdentityManagerStartState; use crate::wallet::asset_lock::tracked::TrackedAssetLock; use dashcore::{OutPoint, Transaction}; @@ -60,4 +61,11 @@ pub struct ClientWalletStartState { /// permanently. Replaying the record at load restores exactly the /// state the live process held. pub unconfirmed_outgoing_txs: Vec, + /// Durable record of the DashPay coreHeight backfill, as the host last + /// stored it — which receival contacts the historical rescan already + /// covers, from which height each. Empty (the default) when the host + /// never stored one, or does not persist the slot at all; the first + /// rescan sweep then behaves exactly as before and writes one. See + /// [`DashPayBackfillRecord`]. + pub dashpay_backfill: DashPayBackfillRecord, } diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index e70af1d0c47..e205cb788f5 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -182,6 +182,9 @@ struct BatchDiagnostics { wallets: usize, /// Highest watermark the store accepted and committed. persisted: Option, + /// The watermark each wallet's store accepted in this drain — what that + /// wallet's host now holds, recorded into [`DurableCursors`]. + persisted_by_wallet: BTreeMap, /// Highest watermark withheld by the fail-closed guard. frozen: Option, /// Highest watermark the store rejected. @@ -268,6 +271,249 @@ pub fn spawn_wallet_event_adapter

( sync_fault: Arc, cancel: CancellationToken, ) -> JoinHandle<()> +where + P: PlatformWalletPersistence + 'static, +{ + spawn_wallet_event_adapter_with_durable_cursors( + wallet_manager, + persister, + receiver, + sync_fault, + crate::changeset::DurableCursors::default(), + cancel, + ) +} + +/// The durable `synced_height` each wallet's host last accepted, shared by +/// every writer of that cursor and doubling as the lock that orders them. +/// +/// Two writers exist: the wallet-event adapter (advances, one per committed +/// batch) and the DashPay rescan reconcile (a deliberate rewind stored with +/// the backfill record that vouches for it). Without one lock across +/// "decide the height" and "store it", an advance the adapter had already +/// projected could be stored after the rewind and put the durable cursor back +/// above the range still to be re-matched, and a rewind could advance the +/// durable cursor past rows the adapter had not yet persisted +/// (dashpay/platform#4302 review). Both take this lock before they read the +/// wallet's in-memory cursor and hold it until their `store()` returns; the +/// adapter strips, at commit time, an advance a rewind has overtaken (see +/// [`RewindBarrier`]) or one above the in-memory cursor, and the reconcile +/// never writes a cursor at or above the recorded durable one. +/// +/// Lock order is this mutex, then the wallet manager — never the reverse. +/// Seeded with the cursor each wallet was loaded or created with. +/// +/// An entry outlives its wallet: a removed wallet's entry stays what its host +/// row holds, and a same-id successor seeds from it in preference to the +/// snapshot it read before taking the lock, which a predecessor's commit may +/// have overtaken. The same structure carries the removed wallet's +/// [`RetiredBarrier`] for the successor to inherit — and for the adapter to +/// count down while no wallet is registered under the id, since advances the +/// removed wallet emitted keep arriving and are dropped meanwhile. +pub type DurableCursors = Arc; + +/// The shared state behind [`DurableCursors`]. +#[derive(Debug, Default)] +pub struct DurableCursorState { + /// Per wallet, what its host holds. The ordering lock. + pub cursors: Arc>>, + /// Per wallet id with no registered wallet, the barrier state the removed + /// wallet left (see [`RewindBarrier::retire`]). A leaf lock: taken under + /// the manager lock on both sides, never held across an await. + pub retired: std::sync::Mutex>, +} + +impl DurableCursorState { + /// Lock the cursor map for a short, non-blocking critical section. + pub async fn lock(&self) -> tokio::sync::MutexGuard<'_, BTreeMap> { + self.cursors.lock().await + } + + /// The retired-barrier map, poison-tolerant. + pub fn retired(&self) -> std::sync::MutexGuard<'_, BTreeMap> { + self.retired + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + } +} + +/// What a removed wallet leaves under its id for a same-id successor +/// (dashpay/platform#4302 review). +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct RetiredBarrier { + /// Advances the removed wallet emitted that the adapter has not projected + /// yet. Counted down as they arrive while no wallet holds the id (they + /// are dropped), so a successor inherits exactly the ones still queued. + pub in_flight: u64, + /// The removed wallet's rewind epoch. + pub epoch: u64, + /// The removed wallet's final `account_generation`. The successor's + /// starts above it, so a filter batch whose scripts were reconciled + /// against the predecessor's account set never matches the successor's + /// generation and is not certified for it. + pub account_generation: u64, +} + +/// What one wallet's host holds, as [`DurableCursors`] records it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct DurableCursor { + /// The `synced_height` the host last accepted. + pub height: u32, + /// The [`RewindBarrier`] epoch of the scan advance that put `height` + /// there, when it was one; `None` for a cursor seeded at load or + /// creation, or written by a backfill round as a rewind. + /// + /// A cursor owed to the host after an in-memory lowering is paid in full + /// once the host accepts an advance from the lowering's epoch or later: + /// that advance was reached by the scan that restarted from the lowered + /// cursor, so the host already tracks it, and writing the owed height + /// afterwards would only drag the host back below the scan + /// (dashpay/platform#4302 device check). + pub advance_epoch: Option, +} + +impl DurableCursor { + /// A cursor the host holds without a known scan advance behind it. + pub fn at(height: u32) -> Self { + Self { + height, + advance_epoch: None, + } + } +} + +/// Orders one wallet's `SyncHeightAdvanced` events against in-memory rewinds +/// of its scan cursor, so an advance the engine emitted *before* a rewind is +/// never persisted after it. +/// +/// A rewind emits nothing (the engine only reports forward moves), so the +/// persistence channel carries no marker of where one happened, and a +/// height comparison cannot place it either: once the rescan has climbed +/// back past a still-queued pre-rewind advance, that advance looks current, +/// and storing it would put the durable cursor above transaction rows the +/// rescan emitted behind it in the channel (dashpay/platform#4302 review). +/// +/// The barrier counts instead. The engine emits a `SyncHeightAdvanced` only +/// right after calling this wallet's `update_synced_height` hook, under the +/// same manager write lock, so the hook counts emissions exactly +/// ([`note_emitted`](Self::note_emitted)), and the adapter counts them back +/// down as it projects them ([`take_next`](Self::take_next)). A rewind — +/// made under that same write lock — snapshots how many are still in flight +/// ([`arm`](Self::arm)): exactly those were emitted before it, and the +/// adapter drops them as they arrive. Every later advance is tagged with the +/// rewind epoch it was projected in, and a commit strips one whose epoch a +/// rewind has since superseded, which covers an advance the adapter had +/// already projected when the rewind ran. +/// +/// Miscounting fails safe: an extra count only drops a later, genuine +/// advance, leaving the durable cursor behind the scan until the next one. +#[derive(Debug, Default)] +pub struct RewindBarrier { + /// `SyncHeightAdvanced` events emitted for this wallet that the adapter + /// has not projected yet. + in_flight: u64, + /// How many of the next projected advances were emitted before the + /// latest rewind. + stale: u64, + /// Bumped by every rewind. + epoch: u64, + /// Inherited from a removed same-id predecessor: one past its final + /// `account_generation`, folded into this wallet's. + generation_base: u64, + /// Receival accounts platform registered by inserting them directly + /// (not through key-wallet's `add_managed_account*`, which bump the + /// engine's own counter). Folded into the wallet's `account_generation` + /// so the filter pipeline refuses to certify a batch scanned before the + /// account's scripts were watched. + account_registrations: u64, +} + +impl RewindBarrier { + /// The engine is about to emit a `SyncHeightAdvanced` for this wallet. + pub(crate) fn note_emitted(&mut self) { + self.in_flight = self.in_flight.saturating_add(1); + } + + /// The scan cursor was just lowered in memory: every advance still in + /// flight predates it. + pub(crate) fn arm(&mut self) { + self.stale = self.in_flight; + self.epoch = self.epoch.wrapping_add(1); + } + + /// Classify the next advance the adapter projects: `None` when it was + /// emitted before the latest rewind, else the epoch it belongs to. + pub(crate) fn take_next(&mut self) -> Option { + self.in_flight = self.in_flight.saturating_sub(1); + if self.stale > 0 { + self.stale -= 1; + None + } else { + Some(self.epoch) + } + } + + /// The current rewind epoch. + pub(crate) fn epoch(&self) -> u64 { + self.epoch + } + + /// What a removed wallet leaves for a successor registered under the same + /// id: its advances still in flight, its epoch, and its final account + /// generation (`account_generation`, the wallet's full value). + pub(crate) fn retire(&self, account_generation: u64) -> RetiredBarrier { + RetiredBarrier { + in_flight: self.in_flight, + epoch: self.epoch, + account_generation, + } + } + + /// Take over a removed predecessor's [`retire`](Self::retire) state. The + /// shared adapter can still hold advances the predecessor emitted, queued + /// or already projected; all of them predate this wallet's own scan, so + /// the queued ones are counted as stale and the epoch moves past the + /// predecessor's, which strips the projected ones at commit. The account + /// generation moves past the predecessor's too, so no in-flight filter + /// batch scanned against its account set certifies this wallet. + pub(crate) fn inherit(&mut self, retired: RetiredBarrier) { + self.in_flight = self.in_flight.saturating_add(retired.in_flight); + self.stale = self.stale.saturating_add(retired.in_flight); + self.epoch = self.epoch.wrapping_add(retired.epoch).wrapping_add(1); + self.generation_base = self + .generation_base + .max(retired.account_generation.saturating_add(1)); + } + + /// See [`inherit`](Self::inherit): added to this wallet's account + /// generation. + pub(crate) fn generation_base(&self) -> u64 { + self.generation_base + } + + /// A receival account was inserted outside key-wallet's account-add + /// API: invalidate every in-flight scan snapshot of the account set. + pub(crate) fn note_account_registered(&mut self) { + self.account_registrations = self.account_registrations.wrapping_add(1); + } + + /// See [`note_account_registered`](Self::note_account_registered). + pub(crate) fn account_registrations(&self) -> u64 { + self.account_registrations + } +} + +/// [`spawn_wallet_event_adapter`] sharing `durable_cursors` with the other +/// writer of the durable sync cursor (the DashPay rescan reconcile). The +/// manager uses this form; see [`DurableCursors`]. +pub fn spawn_wallet_event_adapter_with_durable_cursors

( + wallet_manager: Arc>>, + persister: Weak

, + receiver: mpsc::UnboundedReceiver, + sync_fault: Arc, + durable_cursors: DurableCursors, + cancel: CancellationToken, +) -> JoinHandle<()> where P: PlatformWalletPersistence + 'static, { @@ -276,10 +522,138 @@ where persister, receiver, sync_fault, + durable_cursors, cancel, )) } +/// Project one event and fold it into its wallet's entry of `batch`, in +/// channel order. +/// +/// A `SyncHeightAdvanced` is first classified against the wallet's +/// [`RewindBarrier`]: one emitted before the latest rewind contributes no +/// height, and the first advance of a newer rewind epoch replaces the +/// batch's height instead of max-merging with it — a pre-rewind height is +/// higher than the rescan's by construction, and the max would keep it. +/// +/// An advance for a wallet id with no wallet registered under it — removed, +/// or rolled back — contributes nothing: nothing may persist a cursor for a +/// wallet that is gone, and a same-id successor must not inherit it. It is +/// counted down from the id's [`RetiredBarrier`] so the successor inherits +/// exactly the advances still queued (dashpay/platform#4302 review). +async fn project_event_into_batch( + wallet_manager: &Arc>>, + durable_cursors: &DurableCursors, + event: &WalletEvent, + batch: &mut BTreeMap, +) { + let wallet_id = event.wallet_id(); + let advance_epoch = match event { + WalletEvent::SyncHeightAdvanced { .. } => Some({ + let mut wm = wallet_manager.write().await; + match wm.get_wallet_info_mut(&wallet_id) { + Some(info) => info.rewind_barrier.take_next(), + None => { + if let Some(retired) = durable_cursors.retired().get_mut(&wallet_id) { + retired.in_flight = retired.in_flight.saturating_sub(1); + } + None + } + } + }), + _ => None, + }; + let mut core = build_core_changeset(wallet_manager, event).await; + let asset_locks = reconstruct_asset_locks_for_event(wallet_manager, event).await; + let payments = sent_payment_verdicts(wallet_manager, event).await; + let entry = batch.entry(wallet_id).or_default(); + match advance_epoch { + Some(None) => { + tracing::debug!( + wallet_id = %hex::encode(wallet_id), + projected = ?core.synced_height, + "wallet-event adapter: dropping a sync-height advance emitted before a rewind" + ); + core.synced_height = None; + core.synced_height_is_rewind = false; + } + Some(Some(epoch)) if core.synced_height.is_some() => { + if entry.advance_epoch.is_some_and(|held| held != epoch) { + entry.core.synced_height = None; + entry.core.synced_height_is_rewind = false; + } + entry.advance_epoch = Some(epoch); + } + _ => {} + } + entry.core.merge(core); + entry.asset_locks.merge(asset_locks); + // Last-write-wins per `(owner, txid)`: a transaction swept and then + // reinstated inside one drain reaches the store as the verdict the drain + // ended on, never as two rows. + merge_payment_overlays(&mut entry.payments, payments); +} + +/// Strip, from each wallet's batch, a sync-height advance a rewind has +/// overtaken: one projected in a [`RewindBarrier`] epoch a rewind has since +/// superseded, one above the wallet's in-memory cursor, or one for a wallet +/// id nothing is registered under any more. Storing either +/// would claim coverage the rescan has not produced. Called with +/// [`DurableCursors`] held, right before commit, so no rewind can slip in +/// between this check and the store. +async fn drop_watermarks_above_the_live_cursor( + wallet_manager: &Arc>>, + batch: &mut BTreeMap, +) { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let wm = wallet_manager.read().await; + for (wallet_id, wallet_batch) in batch.iter_mut() { + let Some(height) = wallet_batch.core.synced_height else { + continue; + }; + if wallet_batch.core.synced_height_is_rewind { + continue; + } + let Some(info) = wm.get_wallet_info(wallet_id) else { + // No wallet holds this id any more: nothing may persist a cursor + // for it, and a same-id successor must not find one. + tracing::debug!( + wallet_id = %hex::encode(wallet_id), + projected = height, + "wallet-event adapter: dropping a sync-height advance for a removed wallet" + ); + wallet_batch.core.synced_height = None; + continue; + }; + // Projected in a rewind epoch that has since been superseded: the + // advance predates the rewind whatever its height, even once the + // rescan has climbed back past it. + if wallet_batch + .advance_epoch + .is_some_and(|epoch| epoch != info.rewind_barrier.epoch()) + { + tracing::debug!( + wallet_id = %hex::encode(wallet_id), + projected = height, + "wallet-event adapter: dropping a sync-height advance a rewind overtook" + ); + wallet_batch.core.synced_height = None; + continue; + } + let live = info.core_wallet.synced_height(); + if height > live { + tracing::debug!( + wallet_id = %hex::encode(wallet_id), + projected = height, + live, + "wallet-event adapter: dropping a sync-height advance projected before a rewind" + ); + wallet_batch.core.synced_height = None; + } + } +} + /// Drain `receiver`, project each [`WalletEvent`] into a /// [`CoreChangeSet`], and forward to the persister. Split out of /// [`spawn_wallet_event_adapter`] so the loop — not just the @@ -336,6 +710,7 @@ async fn run_wallet_event_adapter

( persister: Weak

, mut receiver: mpsc::UnboundedReceiver, sync_fault: Arc, + durable_cursors: DurableCursors, cancel: CancellationToken, ) where P: PlatformWalletPersistence + 'static, @@ -423,18 +798,9 @@ async fn run_wallet_event_adapter

( let mut batch: BTreeMap = BTreeMap::new(); let mut closed = false; - { - let wallet_id = event.wallet_id(); - // For events that need to consult per-wallet state, - // `build_core_changeset` takes a brief read lock on the manager. - let core = build_core_changeset(&wallet_manager, &event).await; - let asset_locks = reconstruct_asset_locks_for_event(&wallet_manager, &event).await; - let payments = sent_payment_verdicts(&wallet_manager, &event).await; - let entry = batch.entry(wallet_id).or_default(); - entry.core.merge(core); - entry.asset_locks.merge(asset_locks); - merge_payment_overlays(&mut entry.payments, payments); - } + // For events that need to consult per-wallet state, + // `build_core_changeset` takes a brief read lock on the manager. + project_event_into_batch(&wallet_manager, &durable_cursors, &event, &mut batch).await; // Fold in whatever else is already buffered. `try_recv` never waits, // so this drains the backlog at projection speed and stops as soon as @@ -443,18 +809,8 @@ async fn run_wallet_event_adapter

( while folded < ADAPTER_STORE_BATCH_LIMIT { match receiver.try_recv() { Ok(event) => { - let wallet_id = event.wallet_id(); - let core = build_core_changeset(&wallet_manager, &event).await; - let asset_locks = - reconstruct_asset_locks_for_event(&wallet_manager, &event).await; - let payments = sent_payment_verdicts(&wallet_manager, &event).await; - let entry = batch.entry(wallet_id).or_default(); - entry.core.merge(core); - entry.asset_locks.merge(asset_locks); - // Last-write-wins per `(owner, txid)`: a transaction swept - // and then reinstated inside one drain reaches the store as - // the verdict the drain ended on, never as two rows. - merge_payment_overlays(&mut entry.payments, payments); + project_event_into_batch(&wallet_manager, &durable_cursors, &event, &mut batch) + .await; folded += 1; } Err(TryRecvError::Empty) => break, @@ -505,6 +861,12 @@ async fn run_wallet_event_adapter

( // it again costs nothing — whereas the reverse error, omitting a wallet // whose store did run, would leave a watermark free to advance past // rows nobody can account for. + // Take the durable-cursor lock for the whole commit and re-check each + // watermark against the live cursor under it: a rewind that ran since + // this batch was projected must win (see [`DurableCursors`]). The + // guard rides into the commit thread and is released when it returns. + let mut cursors = Arc::clone(&durable_cursors.cursors).lock_owned().await; + drop_watermarks_above_the_live_cursor(&wallet_manager, &mut batch).await; let batch_wallet_ids: Vec = batch .iter() .filter(|(_, wallet_batch)| { @@ -523,6 +885,7 @@ async fn run_wallet_event_adapter

( let sync_fault_for_commit = Arc::clone(&sync_fault); let fault_for_commit = Arc::clone(&fault); let freeze_for_commit = Arc::clone(&freeze_logged); + let wallet_ids_for_commit = batch_wallet_ids.clone(); let committed = tokio::task::spawn_blocking(move || { // The lock is uncontended by construction — this task is the only // writer, and one drain commits at a time — so it never blocks; @@ -537,15 +900,18 @@ async fn run_wallet_event_adapter

( // `commit_batch` returns is lost when a later store in the same // batch panics, and the panic branch would then emit the one-shot // marker a second time for a freeze already announced. - let diag = commit_batch( + let diag = commit_batch_under_writer_lock( &*persister_for_commit, batch, folded, + &wallet_ids_for_commit, &mut fault, &sync_fault_for_commit, &freeze_for_commit, &mut settled, + &mut cursors, ); + drop(cursors); // Hand the claim back out: the next chunk of a cancellation drain // inherits it instead of racing a fresh upgrade against the owner's // release. A panicking `commit_batch` drops it instead, and the @@ -674,6 +1040,7 @@ async fn run_wallet_event_adapter

( /// means the rows never reached disk — the same condition that makes us /// fault the wallet — so counting it as persisted would make the trace /// contradict itself. +#[cfg_attr(not(test), allow(dead_code))] fn commit_batch

( persister: &P, batch: BTreeMap, @@ -702,6 +1069,80 @@ where diag } +/// [`commit_batch`] as the adapter runs it: holding the [`DurableCursors`] +/// writer lock, and leaving that lock's state true even when a store panics. +/// +/// Every cursor a store accepted is recorded before the lock can be +/// released, and a panic faults the wallets whose outcome it left unknown +/// before it propagates. Otherwise the unwind would release the lock with +/// an accepted height unrecorded and the fault latch still clear, and a +/// reconcile entering in that window would measure a new contact against a +/// durable cursor lower than the one the host actually holds +/// (dashpay/platform#4302 review). The panic is re-raised afterwards, so +/// the adapter's `JoinError` arm still logs it and freezes the same set. +#[allow(clippy::too_many_arguments)] +fn commit_batch_under_writer_lock

( + persister: &P, + batch: BTreeMap, + folded: usize, + batch_wallet_ids: &[WalletId], + fault: &mut AdapterFaultState, + sync_fault: &AtomicBool, + freeze_logged: &AtomicBool, + settled: &mut Vec, + cursors: &mut BTreeMap, +) -> BatchDiagnostics +where + P: PlatformWalletPersistence + ?Sized, +{ + let mut diag = BatchDiagnostics::new(folded, batch.len()); + // The epoch each wallet's advance was projected in, for the cursor + // entry its accepted height lands in. + let advance_epochs: BTreeMap> = batch + .iter() + .map(|(wallet_id, wallet_batch)| (*wallet_id, wallet_batch.advance_epoch)) + .collect(); + let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + for (wallet_id, wallet_batch) in batch { + commit_wallet( + persister, + wallet_id, + wallet_batch, + &mut diag, + fault, + sync_fault, + freeze_logged, + settled, + ); + } + })); + // What each host now holds, for the next writer to measure against — + // known only where the store is the commit. A buffering backend commits + // at a later `flush` that can still drop the round, so its accepted + // heights are not durable and are not recorded; it gets no backfill + // record either (see `reconcile_dashpay_rescan`), so nothing measures + // against them. + let durable = persister.store_commits_inline(); + for (wallet_id, height) in diag.persisted_by_wallet.iter().filter(|_| durable) { + cursors.insert( + *wallet_id, + DurableCursor { + height: *height, + advance_epoch: advance_epochs.get(wallet_id).copied().flatten(), + }, + ); + } + if let Err(panic) = unwound { + for wallet_id in batch_wallet_ids { + if !settled.contains(wallet_id) { + fault.fault_wallet(*wallet_id, sync_fault); + } + } + std::panic::resume_unwind(panic); + } + diag +} + /// Commit one wallet's folded changeset — the per-wallet unit of /// [`commit_batch`]. /// @@ -728,6 +1169,7 @@ fn commit_wallet

( mut core, asset_locks, payments, + advance_epoch: _, } = wallet_batch; { // Sent-payment verdicts reach a host only through the payment-overlay @@ -829,6 +1271,7 @@ fn commit_wallet

( // the adapter's own progress marker and holding it back would // re-drive work without making anything safer. core.synced_height = None; + core.synced_height_is_rewind = false; } let cs = PlatformWalletChangeSet { @@ -898,6 +1341,7 @@ fn commit_wallet

( Ok(()) => { if let Some(h) = offered_height { diag.record_persisted(h); + diag.persisted_by_wallet.insert(wallet_id, h); } } Err(e) => { @@ -1000,6 +1444,7 @@ fn fault_and_freeze( fn freeze_synced_height_if_faulted(core: &mut CoreChangeSet, persistence_faulted: bool) { if persistence_faulted { core.synced_height = None; + core.synced_height_is_rewind = false; } } @@ -1025,6 +1470,10 @@ struct WalletBatch { /// identity blob is instead patched row-by-row by the persister, inside /// its own write transaction — see the SQLite backend's `dashpay` module. payments: PaymentOverlay, + /// The [`RewindBarrier`] epoch of the advance in `core.synced_height`, + /// checked again at commit: an advance a rewind has since superseded is + /// stripped there. `None` while the batch carries no advance. + advance_epoch: Option, } /// Rebuild missing tracked asset locks from the records an event @@ -1188,6 +1637,8 @@ async fn build_core_changeset( wallet_manager: &Arc>>, event: &WalletEvent, ) -> CoreChangeSet { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + match event { WalletEvent::TransactionDetected { wallet_id, @@ -1403,10 +1854,38 @@ async fn build_core_changeset( ..CoreChangeSet::default() } } - WalletEvent::SyncHeightAdvanced { height, .. } => CoreChangeSet { - synced_height: Some(*height), - ..CoreChangeSet::default() - }, + WalletEvent::SyncHeightAdvanced { wallet_id, height } => { + // A watermark queued before a rewind must not land after it. + // `reconcile_dashpay_rescan` lowers the in-memory cursor and + // stores the lowered value with its coverage record on its own + // round; an advance the engine emitted just before that rewind + // (still in this channel) would otherwise be persisted after + // it and put the durable cursor back above the range the rescan + // has yet to re-match — with coverage on disk vouching for it. + // The in-memory cursor is the wallet's truth at drain time: an + // event above it is stale, so its watermark is dropped and the + // rescan's own advances carry the cursor from the floor up. + let current = { + let wm = wallet_manager.read().await; + wm.get_wallet_info(wallet_id) + .map(|info| info.core_wallet.synced_height()) + }; + match current { + Some(current) if *height > current => { + tracing::debug!( + wallet_id = %hex::encode(wallet_id), + event_height = *height, + current, + "wallet-event adapter: dropping a sync-height advance queued before a rewind" + ); + CoreChangeSet::default() + } + _ => CoreChangeSet { + synced_height: Some(*height), + ..CoreChangeSet::default() + }, + } + } WalletEvent::ChainLockProcessed { chain_lock, .. } => { // The wallet has already promoted the matching records from // `InBlock` to `InChainLockedBlock` by the time this event @@ -3939,6 +4418,8 @@ mod contact_watch_only_projection_tests { identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), observed_input_conflicts: Default::default(), } } @@ -5013,6 +5494,8 @@ mod tests { /// for the block to be in effect rather than sleeping and hoping. blocked: Arc, capabilities: crate::changeset::PersistenceCapabilities, + /// When set, `store` only buffers: the backend commits at `flush`. + buffered: AtomicBool, } impl ProbePersister { @@ -5024,6 +5507,7 @@ mod tests { block_until: Mutex::new(None), blocked: Arc::new(AtomicBool::new(false)), capabilities: crate::changeset::PersistenceCapabilities::NONE, + buffered: AtomicBool::new(false), } } /// A probe that additionally attests `capabilities` — used by the @@ -5054,6 +5538,10 @@ mod tests { } impl PlatformWalletPersistence for ProbePersister { + fn store_commits_inline(&self) -> bool { + !self.buffered.load(Ordering::SeqCst) + } + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { self.capabilities } @@ -5129,6 +5617,573 @@ mod tests { WalletEvent::SyncHeightAdvanced { wallet_id, height } } + /// Register a fresh test wallet (cursor 0) in `manager`. The adapter + /// persists advances only for a wallet the manager holds + /// (dashpay/platform#4302 review), so a test feeding it bare events needs + /// the wallet present. + async fn insert_test_wallet( + manager: &Arc>>, + ) -> WalletId { + use crate::wallet::core::WalletGeneration; + use crate::wallet::identity::IdentityManager; + use key_wallet::test_utils::TestWalletContext; + + let ctx = TestWalletContext::new_random(); + let info = PlatformWalletInfo { + observed_input_conflicts: Default::default(), + core_wallet: ctx.managed_wallet, + generation: Arc::new(WalletGeneration::new()), + identity_manager: IdentityManager::new(), + tracked_asset_locks: BTreeMap::new(), + dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), + }; + manager + .write() + .await + .insert_wallet(ctx.wallet, info) + .expect("insert wallet") + } + + /// Feed `tx` a sync-height advance the way the engine emits one: the + /// in-memory cursor moves and the wallet's barrier counts the emission. + async fn emit_advance( + manager: &Arc>>, + tx: &UnboundedSender, + wallet_id: WalletId, + height: u32, + ) { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + { + let mut wm = manager.write().await; + let info = wm + .get_wallet_info_mut(&wallet_id) + .expect("registered wallet"); + info.update_synced_height(height); + } + tx.send(sync_height_event(wallet_id, height)).unwrap(); + } + + /// A watermark the engine queued BEFORE `reconcile_dashpay_rescan` + /// lowered the wallet's cursor must not be persisted after it: the + /// rescan round stores the lowered cursor with its coverage record, and + /// a stale advance landing afterwards would put the durable cursor back + /// above the range the rescan has yet to re-match, with coverage on + /// disk vouching for it (dashpay/platform#4302 review). The in-memory + /// cursor at drain time decides: an event above it is dropped, one at or + /// below it flows. + #[tokio::test] + async fn a_sync_height_advance_queued_before_a_rewind_is_dropped() { + use crate::wallet::core::WalletGeneration; + use crate::wallet::identity::IdentityManager; + use key_wallet::test_utils::TestWalletContext; + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let ctx = TestWalletContext::new_random(); + let mut info = PlatformWalletInfo { + observed_input_conflicts: Default::default(), + core_wallet: ctx.managed_wallet, + generation: Arc::new(WalletGeneration::new()), + identity_manager: IdentityManager::new(), + tracked_asset_locks: BTreeMap::new(), + dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), + }; + info.core_wallet.update_synced_height(1_000); + let manager = test_manager(); + let wallet_id = manager + .write() + .await + .insert_wallet(ctx.wallet, info) + .expect("insert wallet"); + + // In step with the engine: the advance the engine just applied. + let cs = super::build_core_changeset(&manager, &sync_height_event(wallet_id, 1_000)).await; + assert_eq!(cs.synced_height, Some(1_000)); + + // The rescan reconcile rewinds the wallet to 100 while that advance + // is still queued: it must not land. + manager + .write() + .await + .get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(100); + let stale = + super::build_core_changeset(&manager, &sync_height_event(wallet_id, 1_000)).await; + assert_eq!( + stale.synced_height, None, + "an advance above the rewound cursor was queued before the rewind" + ); + assert!( + crate::changeset::Merge::is_empty(&stale), + "nothing else rides a bare watermark" + ); + + // The rescan's own advances carry the cursor from the floor up. + let fresh = super::build_core_changeset(&manager, &sync_height_event(wallet_id, 100)).await; + assert_eq!(fresh.synced_height, Some(100)); + + // An unknown wallet has no cursor to compare against: unchanged. + let unknown = super::build_core_changeset(&manager, &sync_height_event([9u8; 32], 7)).await; + assert_eq!(unknown.synced_height, Some(7)); + } + + /// The commit-time re-check that closes the projection-to-commit window: + /// the adapter projects an advance to 1000, releases the manager, and a + /// rescan rewinds the wallet to 100 and stores that before the commit. + /// Under the durable-cursor lock the commit re-reads the live cursor and + /// drops the stale advance; a rewind-marked cursor and an advance at or + /// below the live cursor pass through (dashpay/platform#4302 review). + #[tokio::test] + async fn the_commit_drops_an_advance_projected_before_a_rewind() { + use crate::wallet::core::WalletGeneration; + use crate::wallet::identity::IdentityManager; + use key_wallet::test_utils::TestWalletContext; + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let ctx = TestWalletContext::new_random(); + let mut info = PlatformWalletInfo { + observed_input_conflicts: Default::default(), + core_wallet: ctx.managed_wallet, + generation: Arc::new(WalletGeneration::new()), + identity_manager: IdentityManager::new(), + tracked_asset_locks: BTreeMap::new(), + dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), + }; + info.core_wallet.update_synced_height(1_000); + let manager = test_manager(); + let wallet_id = manager + .write() + .await + .insert_wallet(ctx.wallet, info) + .expect("insert wallet"); + + // Projected while the wallet was at 1000. + let projected = + super::build_core_changeset(&manager, &sync_height_event(wallet_id, 1_000)).await; + assert_eq!(projected.synced_height, Some(1_000)); + + // The rescan rewinds before the commit. + manager + .write() + .await + .get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(100); + + let batch_with = |core: CoreChangeSet| { + let mut batch: BTreeMap = BTreeMap::new(); + batch.entry(wallet_id).or_default().core = core; + batch + }; + let mut stale = batch_with(projected); + super::drop_watermarks_above_the_live_cursor(&manager, &mut stale).await; + assert_eq!( + stale[&wallet_id].core.synced_height, None, + "stale advance dropped" + ); + + let mut in_step = batch_with(CoreChangeSet { + synced_height: Some(100), + ..CoreChangeSet::default() + }); + super::drop_watermarks_above_the_live_cursor(&manager, &mut in_step).await; + assert_eq!(in_step[&wallet_id].core.synced_height, Some(100)); + + let mut rewind = batch_with(CoreChangeSet { + synced_height: Some(500), + synced_height_is_rewind: true, + ..CoreChangeSet::default() + }); + super::drop_watermarks_above_the_live_cursor(&manager, &mut rewind).await; + assert_eq!( + rewind[&wallet_id].core.synced_height, + Some(500), + "a rewind is never second-guessed" + ); + } + + /// A wallet at `synced_height` with the manager's lossless persistence + /// receiver installed, so advances go through the engine's real + /// emission path (and the [`RewindBarrier`] hook counting it). + async fn engine_wallet( + synced_height: u32, + ) -> ( + Arc>>, + tokio::sync::mpsc::UnboundedReceiver, + WalletId, + ) { + use crate::wallet::core::WalletGeneration; + use crate::wallet::identity::IdentityManager; + use key_wallet::test_utils::TestWalletContext; + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let ctx = TestWalletContext::new_random(); + let mut info = PlatformWalletInfo { + observed_input_conflicts: Default::default(), + core_wallet: ctx.managed_wallet, + generation: Arc::new(WalletGeneration::new()), + identity_manager: IdentityManager::new(), + tracked_asset_locks: BTreeMap::new(), + dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), + }; + info.core_wallet.update_synced_height(synced_height); + let manager = test_manager(); + let receiver = manager + .write() + .await + .take_persistence_receiver() + .expect("persistence receiver"); + let wallet_id = manager + .write() + .await + .insert_wallet(ctx.wallet, info) + .expect("insert wallet"); + (manager, receiver, wallet_id) + } + + /// The scan cursor as the engine advances it: in memory, and emitted. + async fn engine_advance( + manager: &Arc>>, + wallet_id: WalletId, + height: u32, + ) { + use key_wallet_manager::WalletInterface; + manager + .write() + .await + .update_wallet_synced_height(&wallet_id, height); + } + + /// The scan cursor as `reconcile_dashpay_rescan` lowers it: in memory + /// only, arming the barrier under the same write lock. + async fn rewind( + manager: &Arc>>, + wallet_id: WalletId, + floor: u32, + ) { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + let mut wm = manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.core_wallet.update_synced_height(floor); + info.rewind_barrier.arm(); + } + + async fn project( + manager: &Arc>>, + durable: &super::DurableCursors, + receiver: &mut tokio::sync::mpsc::UnboundedReceiver, + count: usize, + ) -> BTreeMap { + let mut batch = BTreeMap::new(); + for _ in 0..count { + let event = receiver.try_recv().expect("queued event"); + super::project_event_into_batch(manager, durable, &event, &mut batch).await; + } + batch + } + + /// dashpay/platform#4302 review: an advance the engine emitted before a + /// rewind, still queued when the rescan has climbed back past it, looks + /// current by height — the live cursor is above it again — yet storing + /// it would put the durable cursor above transaction rows the rescan + /// emitted behind it. The barrier drops it by emission order instead. + #[tokio::test] + async fn an_advance_queued_before_a_rewind_is_dropped_after_the_rescan_climbs_past_it() { + let (manager, mut receiver, wallet_id) = engine_wallet(0).await; + let durable = super::DurableCursors::default(); + engine_advance(&manager, wallet_id, 1_000).await; // pre-rewind, still queued + rewind(&manager, wallet_id, 100).await; + engine_advance(&manager, wallet_id, 500).await; + engine_advance(&manager, wallet_id, 1_000).await; // the rescan is back at 1000 + + // The adapter drains the pre-rewind advance on its own. + let mut stale = project(&manager, &durable, &mut receiver, 1).await; + assert_eq!(stale[&wallet_id].core.synced_height, None); + super::drop_watermarks_above_the_live_cursor(&manager, &mut stale).await; + assert_eq!(stale[&wallet_id].core.synced_height, None); + + // The rescan's own advances flow, behind the rows they imply. + let mut fresh = project(&manager, &durable, &mut receiver, 2).await; + super::drop_watermarks_above_the_live_cursor(&manager, &mut fresh).await; + assert_eq!(fresh[&wallet_id].core.synced_height, Some(1_000)); + } + + /// The same race with the advance already projected when the rewind + /// runs: by commit the rescan has climbed back past it, so the live + /// cursor no longer exposes it, and the superseded epoch does. + #[tokio::test] + async fn the_commit_drops_an_advance_a_rewind_overtook_after_the_rescan_climbs_past_it() { + let (manager, mut receiver, wallet_id) = engine_wallet(0).await; + let durable = super::DurableCursors::default(); + engine_advance(&manager, wallet_id, 1_000).await; + let mut projected = project(&manager, &durable, &mut receiver, 1).await; + assert_eq!(projected[&wallet_id].core.synced_height, Some(1_000)); + + rewind(&manager, wallet_id, 100).await; + engine_advance(&manager, wallet_id, 1_200).await; + super::drop_watermarks_above_the_live_cursor(&manager, &mut projected).await; + assert_eq!( + projected[&wallet_id].core.synced_height, None, + "projected before the rewind: stripped although the live cursor is above it" + ); + + // The rescan's advance, in the next batch, is stored. + let mut next = project(&manager, &durable, &mut receiver, 1).await; + super::drop_watermarks_above_the_live_cursor(&manager, &mut next).await; + assert_eq!(next[&wallet_id].core.synced_height, Some(1_200)); + } + + /// A wallet removed and registered again under the same id shares the + /// adapter with its predecessor, which can still hold that predecessor's + /// advances — queued, or already projected. While nothing holds the id + /// every one of them is dropped, at projection and at commit, and + /// counted off the retired barrier; the successor inherits what is still + /// queued, so those drop as stale and the projected ones fail the epoch + /// check at commit, even once the new scan has climbed above them — while + /// the successor's own advances flow (dashpay/platform#4302 review). + #[tokio::test] + async fn a_same_id_successor_drops_its_predecessors_queued_and_projected_advances() { + use crate::wallet::core::WalletGeneration; + use crate::wallet::identity::IdentityManager; + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; + + let (manager, mut receiver, wallet_id) = engine_wallet(0).await; + let durable = super::DurableCursors::default(); + engine_advance(&manager, wallet_id, 700).await; + let mut projected = project(&manager, &durable, &mut receiver, 1).await; // in a batch + engine_advance(&manager, wallet_id, 1_000).await; // still queued + engine_advance(&manager, wallet_id, 1_100).await; // still queued + + // Removed: the barrier is retired under the id. + let (wallet, predecessor_generation) = { + let (wallet, info) = manager + .write() + .await + .remove_wallet(&wallet_id) + .expect("remove"); + let generation = WalletInfoInterface::account_generation(&info); + durable + .retired() + .insert(wallet_id, info.rewind_barrier.retire(generation)); + (wallet, generation) + }; + assert_eq!(durable.retired()[&wallet_id].in_flight, 2); + + // While nothing holds the id: the projected advance is stripped at + // commit, and a queued one is dropped at projection and counted off. + super::drop_watermarks_above_the_live_cursor(&manager, &mut projected).await; + assert_eq!( + projected[&wallet_id].core.synced_height, None, + "nothing may persist a cursor for a removed wallet" + ); + let absent = project(&manager, &durable, &mut receiver, 1).await; + assert_eq!(absent[&wallet_id].core.synced_height, None); + assert_eq!( + durable.retired()[&wallet_id].in_flight, + 1, + "the dropped advance no longer counts as queued" + ); + + // Registered again under the same id. + let retired = durable.retired().remove(&wallet_id).expect("retired"); + let mut successor = PlatformWalletInfo { + observed_input_conflicts: Default::default(), + core_wallet: ManagedWalletInfo::from_wallet(&wallet, 0), + generation: Arc::new(WalletGeneration::new()), + identity_manager: IdentityManager::new(), + tracked_asset_locks: BTreeMap::new(), + dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), + }; + successor.rewind_barrier.inherit(retired); + assert!( + WalletInfoInterface::account_generation(&successor) > predecessor_generation, + "a batch reconciled against the predecessor's accounts cannot match the successor" + ); + assert_eq!( + manager + .write() + .await + .insert_wallet(wallet, successor) + .expect("reinsert"), + wallet_id + ); + engine_advance(&manager, wallet_id, 1_200).await; // the successor's own scan + + let mut queued = project(&manager, &durable, &mut receiver, 1).await; + assert_eq!( + queued[&wallet_id].core.synced_height, None, + "the predecessor's last queued advance drops as stale" + ); + super::drop_watermarks_above_the_live_cursor(&manager, &mut queued).await; + let mut own = project(&manager, &durable, &mut receiver, 1).await; + super::drop_watermarks_above_the_live_cursor(&manager, &mut own).await; + assert_eq!( + own[&wallet_id].core.synced_height, + Some(1_200), + "the successor's own advance is not mistaken for a stale one" + ); + } + + /// A store panicking mid-batch must not release the writer lock with the + /// lock's state behind reality: wallet A's accepted cursor is recorded, + /// and wallet B — whose outcome the panic left unknown — is faulted, + /// both before the panic propagates to the adapter's async `JoinError` + /// arm, so the next writer to take the lock already sees them + /// (dashpay/platform#4302 review). + #[test] + fn a_panicking_commit_records_accepted_cursors_and_faults_before_the_lock_is_released() { + let (obs_tx, _obs_rx) = tokio::sync::mpsc::unbounded_channel(); + let persister = ProbePersister::new(obs_tx); + let a: WalletId = [1u8; 32]; + let b: WalletId = [2u8; 32]; + persister.panic_next(b); + let watermark = |height| super::WalletBatch { + core: CoreChangeSet { + synced_height: Some(height), + ..CoreChangeSet::default() + }, + ..Default::default() + }; + let mut batch = BTreeMap::new(); + batch.insert( + a, + super::WalletBatch { + advance_epoch: Some(3), + ..watermark(1_000) + }, + ); + batch.insert(b, watermark(500)); + + let mut fault = super::AdapterFaultState::default(); + let sync_fault = AtomicBool::new(false); + let freeze_logged = AtomicBool::new(false); + let mut settled = Vec::new(); + let mut cursors: BTreeMap = BTreeMap::from([ + (a, super::DurableCursor::at(100)), + (b, super::DurableCursor::at(100)), + ]); + let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + super::commit_batch_under_writer_lock( + &persister, + batch, + 2, + &[a, b], + &mut fault, + &sync_fault, + &freeze_logged, + &mut settled, + &mut cursors, + ) + })); + assert!(unwound.is_err(), "the panic still propagates"); + assert_eq!( + cursors.get(&a), + Some(&super::DurableCursor { + height: 1_000, + advance_epoch: Some(3), + }), + "A's accepted cursor is recorded with the epoch its advance was projected in" + ); + assert_eq!( + cursors.get(&b).map(|cursor| cursor.height), + Some(100), + "B's store never returned" + ); + assert!( + fault.is_faulted(&b), + "B is faulted before the lock is released" + ); + assert!(!fault.is_faulted(&a), "A settled"); + assert!(sync_fault.load(Ordering::Relaxed)); + } + + /// A backend whose `store` only buffers commits at a later `flush`, which + /// can still fail and drop the round. An accepted store there is not a + /// durable cursor, so the adapter records nothing for the next writer to + /// measure against (dashpay/platform#4302 review). + #[test] + fn a_buffering_backends_accepted_advance_is_not_recorded_as_durable() { + let (obs_tx, _obs_rx) = tokio::sync::mpsc::unbounded_channel(); + let persister = ProbePersister::new(obs_tx); + persister.buffered.store(true, Ordering::SeqCst); + let a: WalletId = [1u8; 32]; + let mut batch = BTreeMap::new(); + batch.insert( + a, + super::WalletBatch { + core: CoreChangeSet { + synced_height: Some(1_000), + ..CoreChangeSet::default() + }, + advance_epoch: Some(3), + ..Default::default() + }, + ); + let mut fault = super::AdapterFaultState::default(); + let sync_fault = AtomicBool::new(false); + let freeze_logged = AtomicBool::new(false); + let mut settled = Vec::new(); + let mut cursors: BTreeMap = + BTreeMap::from([(a, super::DurableCursor::at(100))]); + let diag = super::commit_batch_under_writer_lock( + &persister, + batch, + 1, + &[a], + &mut fault, + &sync_fault, + &freeze_logged, + &mut settled, + &mut cursors, + ); + assert_eq!( + diag.persisted_by_wallet.get(&a).copied(), + Some(1_000), + "the store accepted the advance" + ); + assert_eq!( + cursors.get(&a), + Some(&super::DurableCursor::at(100)), + "an accepted store that is not a commit is not a durable cursor" + ); + } + + /// A batch that holds a pre-rewind advance and then the rescan's first + /// one ends on the rescan's height: the newer epoch replaces the older + /// advance rather than max-merging with it, which would keep the higher, + /// pre-rewind height. + #[tokio::test] + async fn a_batch_spanning_a_rewind_keeps_the_rescans_height_not_the_higher_stale_one() { + let (manager, mut receiver, wallet_id) = engine_wallet(0).await; + let durable = super::DurableCursors::default(); + engine_advance(&manager, wallet_id, 1_000).await; + let first = receiver.try_recv().expect("pre-rewind advance"); + let mut batch = BTreeMap::new(); + super::project_event_into_batch(&manager, &durable, &first, &mut batch).await; + + rewind(&manager, wallet_id, 100).await; + engine_advance(&manager, wallet_id, 400).await; + let second = receiver.try_recv().expect("rescan advance"); + super::project_event_into_batch(&manager, &durable, &second, &mut batch).await; + assert_eq!(batch[&wallet_id].core.synced_height, Some(400)); + + super::drop_watermarks_above_the_live_cursor(&manager, &mut batch).await; + assert_eq!(batch[&wallet_id].core.synced_height, Some(400)); + } + /// A record-bearing (non-watermark) event: carries /// `last_processed_height` but no `synced_height`, so it survives the /// freeze untouched. Used both as a sentinel to prove loop progress and @@ -5155,12 +6210,13 @@ mod tests { /// the watermark forever. #[tokio::test] async fn lossless_burst_never_freezes_and_watermark_reaches_tip() { + let manager = test_manager(); const BURST: u32 = 3000; // >> the old broadcast ring (1000) - let wallet_id = [7u8; 32]; + let wallet_id = insert_test_wallet(&manager).await; let (tx, rx) = unbounded_channel::(); // Pre-buffer the whole burst; an unbounded mpsc cannot drop it. for h in 1..=BURST { - tx.send(sync_height_event(wallet_id, h)).unwrap(); + emit_advance(&manager, &tx, wallet_id, h).await; } let (obs_tx, mut obs_rx) = unbounded_channel(); @@ -5168,10 +6224,11 @@ mod tests { let sync_fault = Arc::new(AtomicBool::new(false)); let cancel = CancellationToken::new(); let handle = tokio::spawn(run_wallet_event_adapter( - test_manager(), + Arc::clone(&manager), Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5215,10 +6272,11 @@ mod tests { /// such guarantee; see the `Drop` rustdoc on `PlatformWalletManager`. #[tokio::test] async fn cancellation_commits_the_events_already_buffered() { - let wallet_id = [11u8; 32]; + let manager = test_manager(); + let wallet_id = insert_test_wallet(&manager).await; let (tx, rx) = unbounded_channel::(); - tx.send(sync_height_event(wallet_id, 41)).unwrap(); - tx.send(sync_height_event(wallet_id, 42)).unwrap(); + emit_advance(&manager, &tx, wallet_id, 41).await; + emit_advance(&manager, &tx, wallet_id, 42).await; let (obs_tx, mut obs_rx) = unbounded_channel(); let persister = Arc::new(ProbePersister::new(obs_tx)); @@ -5228,10 +6286,11 @@ mod tests { cancel.cancel(); run_wallet_event_adapter( - test_manager(), + Arc::clone(&manager), Arc::downgrade(&persister), rx, Arc::new(AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), cancel, ) .await; @@ -5265,14 +6324,15 @@ mod tests { /// interleaving that exposes it. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn a_cancelled_drain_holds_the_persister_until_its_backlog_is_committed() { + let manager = test_manager(); use std::time::{Duration, Instant}; - let wallet_id = [0x55u8; 32]; + let wallet_id = insert_test_wallet(&manager).await; // One past the limit: the tail event cannot ride the first chunk. let backlog = ADAPTER_STORE_BATCH_LIMIT as u32 + 1; let (tx, rx) = unbounded_channel::(); for height in 1..=backlog { - tx.send(sync_height_event(wallet_id, height)).unwrap(); + emit_advance(&manager, &tx, wallet_id, height).await; } let (obs_tx, mut obs_rx) = unbounded_channel(); @@ -5282,10 +6342,11 @@ mod tests { let cancel = CancellationToken::new(); cancel.cancel(); let handle = tokio::spawn(run_wallet_event_adapter( - test_manager(), + Arc::clone(&manager), Arc::downgrade(&persister), rx, Arc::new(AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), cancel, )); @@ -5347,6 +6408,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5401,6 +6463,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5449,6 +6512,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5480,11 +6544,12 @@ mod tests { /// awaiting — making the batch boundary deterministic. #[tokio::test] async fn buffered_events_fold_into_one_store_per_wallet() { - let wallet_id = [11u8; 32]; + let manager = test_manager(); + let wallet_id = insert_test_wallet(&manager).await; // Comfortably larger than the burst, so nothing is dropped. let (tx, rx) = unbounded_channel::(); for h in 1..=5u32 { - tx.send(sync_height_event(wallet_id, h)).unwrap(); + emit_advance(&manager, &tx, wallet_id, h).await; } let (obs_tx, mut obs_rx) = unbounded_channel(); @@ -5492,10 +6557,11 @@ mod tests { let sync_fault = Arc::new(AtomicBool::new(false)); let cancel = CancellationToken::new(); let handle = tokio::spawn(run_wallet_event_adapter( - test_manager(), + manager, Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5530,24 +6596,26 @@ mod tests { /// other's id. #[tokio::test] async fn batch_folds_per_wallet_not_across_wallets() { - let wallet_a = [1u8; 32]; - let wallet_b = [2u8; 32]; + let manager = test_manager(); + let wallet_a = insert_test_wallet(&manager).await; + let wallet_b = insert_test_wallet(&manager).await; let (tx, rx) = unbounded_channel::(); // Interleaved on purpose. - tx.send(sync_height_event(wallet_a, 10)).unwrap(); - tx.send(sync_height_event(wallet_b, 20)).unwrap(); - tx.send(sync_height_event(wallet_a, 11)).unwrap(); - tx.send(sync_height_event(wallet_b, 21)).unwrap(); + emit_advance(&manager, &tx, wallet_a, 10).await; + emit_advance(&manager, &tx, wallet_b, 20).await; + emit_advance(&manager, &tx, wallet_a, 11).await; + emit_advance(&manager, &tx, wallet_b, 21).await; let (obs_tx, mut obs_rx) = unbounded_channel(); let persister = Arc::new(ProbePersister::new(obs_tx)); let sync_fault = Arc::new(AtomicBool::new(false)); let cancel = CancellationToken::new(); let handle = tokio::spawn(run_wallet_event_adapter( - test_manager(), + manager, Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5595,6 +6663,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5682,6 +6751,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5743,6 +6813,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5808,12 +6879,20 @@ mod tests { /// faulted every wallet in the drain. #[tokio::test] async fn a_panicking_commit_spares_the_wallets_it_already_stored() { + let manager = test_manager(); // `BTreeMap` order decides who is committed first, so the ids are // chosen to put the healthy wallet ahead of the panicking one. - let healthy = [0x11u8; 32]; - let doomed = [0x22u8; 32]; + let healthy = insert_test_wallet(&manager).await; + let doomed = insert_test_wallet(&manager).await; // Sorts after `doomed`, so the commit unwinds before it is reached. - let unreached = [0x33u8; 32]; + let unreached = insert_test_wallet(&manager).await; + // `BTreeMap` order decides who is committed first: sort the random ids + // so the roles keep that order. + let (healthy, doomed, unreached) = { + let mut ids = [healthy, doomed, unreached]; + ids.sort(); + (ids[0], ids[1], ids[2]) + }; let (tx, rx) = unbounded_channel::(); let (obs_tx, mut obs_rx) = unbounded_channel(); @@ -5822,10 +6901,11 @@ mod tests { let sync_fault = Arc::new(AtomicBool::new(false)); let cancel = CancellationToken::new(); let handle = tokio::spawn(run_wallet_event_adapter( - test_manager(), + Arc::clone(&manager), Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5849,7 +6929,7 @@ mod tests { // The healthy wallet's watermark must still advance: its store // returned, so its rows are accounted for. - tx.send(sync_height_event(healthy, 900)).unwrap(); + emit_advance(&manager, &tx, healthy, 900).await; // Bounded: on a regression the healthy wallet is frozen, its // watermark-only changeset collapses to nothing, and no store is // observed at all — an unbounded `recv` would hang CI instead of @@ -5867,7 +6947,7 @@ mod tests { // The wallet whose store panicked must be frozen. tx.send(block_processed_event(doomed, 60)).unwrap(); - tx.send(sync_height_event(doomed, 900)).unwrap(); + emit_advance(&manager, &tx, doomed, 900).await; let after_doomed = tokio::time::timeout(std::time::Duration::from_secs(5), obs_rx.recv()) .await .expect("a faulted wallet must still persist its rows") @@ -5884,7 +6964,7 @@ mod tests { // wallet's. A handler that faults only the direct casualty leaves this // one free to advance past rows that never landed. tx.send(block_processed_event(unreached, 60)).unwrap(); - tx.send(sync_height_event(unreached, 900)).unwrap(); + emit_advance(&manager, &tx, unreached, 900).await; let after_unreached = tokio::time::timeout(std::time::Duration::from_secs(5), obs_rx.recv()) .await @@ -5927,6 +7007,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -6015,6 +7096,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -6089,6 +7171,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -6124,7 +7207,8 @@ mod tests { /// any other. #[tokio::test] async fn sweep_with_declared_capability_does_not_freeze() { - let wallet_id = [22u8; 32]; + let manager = test_manager(); + let wallet_id = insert_test_wallet(&manager).await; let (tx, rx) = unbounded_channel::(); let (obs_tx, mut obs_rx) = unbounded_channel(); let persister = Arc::new(ProbePersister::with_capabilities( @@ -6134,17 +7218,18 @@ mod tests { let sync_fault = Arc::new(AtomicBool::new(false)); let cancel = CancellationToken::new(); let handle = tokio::spawn(run_wallet_event_adapter( - test_manager(), + Arc::clone(&manager), Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); tx.send(swept_event(wallet_id, 0x61, 0x62)).unwrap(); // A watermark-bearing event right behind it, folded or not — either // way it must reach the store untouched while the capability holds. - tx.send(sync_height_event(wallet_id, 700)).unwrap(); + emit_advance(&manager, &tx, wallet_id, 700).await; let mut last_synced = None; // Drain until a store carries the watermark. Each receive is bounded: @@ -6790,6 +7875,7 @@ mod tests { core: CoreChangeSet::default(), asset_locks, payments: super::PaymentOverlay::default(), + advance_epoch: None, }, ); commit_batch( @@ -6850,6 +7936,7 @@ mod tests { core, asset_locks: AssetLockChangeSet::default(), payments: super::PaymentOverlay::default(), + advance_epoch: None, }, ); batch @@ -6891,6 +7978,7 @@ mod tests { core: watermark_with_rows(700, 700), asset_locks: AssetLockChangeSet::default(), payments: one_verdict_overlay(PaymentStatus::Failed), + advance_epoch: None, }, ); let diag = commit_batch( @@ -6943,6 +8031,7 @@ mod tests { core: watermark_with_rows(700, 700), asset_locks: AssetLockChangeSet::default(), payments: one_verdict_overlay(PaymentStatus::Failed), + advance_epoch: None, }, ); let diag = commit_batch( @@ -6995,6 +8084,7 @@ mod tests { core: CoreChangeSet::default(), asset_locks: AssetLockChangeSet::default(), payments: one_verdict_overlay(PaymentStatus::Failed), + advance_epoch: None, }, ); commit_batch( @@ -7037,6 +8127,7 @@ mod tests { core: CoreChangeSet::default(), asset_locks: AssetLockChangeSet::default(), payments: one_verdict_overlay(PaymentStatus::Confirmed), + advance_epoch: None, }, ); commit_batch( @@ -7459,6 +8550,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::new(AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), CancellationToken::new(), )); handle.await.expect("the adapter exits on a closed channel"); @@ -7856,6 +8948,8 @@ mod utxo_credit_verdict_tests { identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), observed_input_conflicts: Default::default(), }; let mut wm = WalletManager::::new(dashcore::Network::Testnet); @@ -7950,6 +9044,8 @@ mod utxo_credit_verdict_tests { identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), observed_input_conflicts: Default::default(), }; let mut wm = WalletManager::::new(dashcore::Network::Testnet); diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs new file mode 100644 index 00000000000..fdaa8f2facb --- /dev/null +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -0,0 +1,711 @@ +//! Durable record of the DashPay coreHeight backfill (DIP-15 §12.6). +//! +//! `reconcile_dashpay_rescan` lowers the wallet's SPV `synced_height` to the +//! earliest contact-request height among receival contacts whose addresses +//! were not yet watched when those blocks were first scanned, so the filter +//! scanner re-matches that range with the contact's addresses in the set. +//! The guard that stops the recurring sweep from re-lowering the cursor every +//! pass used to live only in memory (`DashPayState::rescan_triggered`), while +//! the cursor it lowers is durable: the host persists every +//! `SyncHeightAdvanced` the engine emits as the rescan climbs. A fresh process +//! therefore restored a cursor *inside* the climb, saw no guard, and rewound +//! again — one full re-walk per launch, forever (dashpay/platform#4302). +//! +//! [`DashPayBackfillRecord`] is the guard's durable half. It travels with the +//! wallet's own changeset round (the one that also carries the lowered +//! cursor), crosses the FFI as its own size-negotiated persistence slot and a +//! wallet-restore field, and comes back through +//! [`ClientWalletStartState::dashpay_backfill`]. +//! +//! [`ClientWalletStartState::dashpay_backfill`]: crate::changeset::ClientWalletStartState::dashpay_backfill + +use dpp::prelude::Identifier; + +use super::{PersistenceCapabilities, PlatformWalletPersistence}; + +/// One receival account the backfill covers, and the height it covers it +/// from. +/// +/// Keyed on the whole receival-account key — `(owner, contact, +/// account_index)` — not just the relationship: a second receiving account +/// for the same pair derives a different xpub, so its scripts were never +/// watched by a scan that covered the first, and it must not inherit that +/// coverage. +/// +/// `covered_from` is the contact's scan checkpoint at the moment its coverage +/// was recorded — the earliest height whose block the scanner is guaranteed to +/// test against this contact's addresses. It is stored per contact rather than +/// as one wallet-wide floor because a contact's checkpoint can *drop* after it +/// was covered (the reciprocal request turns out to be older than ours, or a +/// rotation falls back to wallet birth): a wallet-wide floor would then read +/// as "still covered" for blocks the scan never tested with that contact. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct DashPayBackfillCoveredContact { + /// Our identity — the owner of the receival account. + pub owner: Identifier, + /// The contact whose payments the receival account collects. + pub contact: Identifier, + /// The receival account's index (`DashpayAccountKey::index`). + pub account_index: u32, + /// Lowest height the scan is guaranteed to have tested (or to test, as it + /// climbs) with this contact's addresses watched. + pub covered_from: u32, +} + +impl DashPayBackfillCoveredContact { + /// The receival-account key the record is sorted and looked up by. + pub fn key(&self) -> (Identifier, Identifier, u32) { + (self.owner, self.contact, self.account_index) + } +} + +/// Per-wallet durable record of the DashPay coreHeight backfill. +/// +/// # What a record claims +/// +/// For every entry in [`covered`](Self::covered): from the moment the record +/// was written, the contact's receival addresses are in the filter match set, +/// and the durable `synced_height` was lowered to at most that contact's +/// `covered_from` in the very same persistence round. From then on the +/// persisted cursor tracks the scanner's real progress *with the contact +/// watched* — the pinned engine refuses to certify a batch scanned at the old +/// checkpoint once the cursor sits below it (contiguity guard, +/// dashpay/rust-dashcore#649) — so on any later launch every block in +/// `[covered_from, synced_height]` has been tested against that contact, and +/// everything above the cursor is about to be. Such a contact needs no second +/// rewind unless its checkpoint has since dropped *below* `covered_from`. +/// +/// # What it does not claim +/// +/// Nothing about a receival account the record does not list. A contact whose +/// account was registered after the record was written (or on this device by +/// a path that never recorded it) is re-armed exactly as before, because the +/// scan that produced this record never watched it. Any ambiguity falls back +/// to re-running the backfill: slow, never lossy. +/// +/// # Completion +/// +/// [`floor`](Self::floor) and [`rewound_from`](Self::rewound_from) describe +/// the backfill's extent: the lowest height it rewound to and the cursor it +/// rewound from. The backfill is complete once `synced_height` has climbed +/// back past `rewound_from` ([`is_complete`](Self::is_complete)); while it is +/// still climbing ([`is_pending`](Self::is_pending)) a restart resumes the +/// climb from the persisted cursor rather than restarting it from the floor. +/// Completion is never *marked* — it is read off the cursor, which is the +/// only thing that actually moved. Marking it at trigger time would have made +/// an interrupted backfill look finished; not marking it costs nothing, +/// because the per-contact `covered_from` entries, not the completion state, +/// are what suppress a second rewind. +/// +/// A later rewind folds into the existing record ([`record_pass`](Self::record_pass)): +/// the floor can only go down, `rewound_from` can only go up, and a contact's +/// entry is replaced by the new checkpoint it was covered from. +/// +/// Every way in — [`from_entries`](Self::from_entries), [`from_parts`](Self::from_parts) +/// and serde deserialization — canonicalizes [`covered`](Self::covered), so a +/// restored record never carries an order or a duplicate the lookups would +/// misread. +#[derive(Debug, Clone, PartialEq, Eq, Default)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(from = "DashPayBackfillRecordSerde"))] +pub struct DashPayBackfillRecord { + /// Lowest height a backfill rewound the cursor to. Meaningless while + /// [`has_extent`](Self::has_extent) is false. + pub floor: u32, + /// Highest cursor a backfill rewound from — the height the scan has to + /// climb back to for the backfill to be complete. `0` while no pass has + /// rewound anything ([`has_extent`](Self::has_extent)). + pub rewound_from: u32, + /// Receival accounts the backfill covers, sorted by `(owner, contact, + /// account_index)`, at most one entry per account. Private so nothing + /// outside this type can break that order, which `covered_from`'s binary + /// search relies on; read it through [`covered`](Self::covered). + covered: Vec, + /// In-memory only, never persisted or crossed over the FFI: a cursor a + /// previous record round still owes the host because that round's + /// store failed. The next successful record round carries it as the + /// round's `synced_height`, so coverage never reaches disk over a + /// durable cursor that is still at a height the in-memory rewind left + /// behind. `None` when nothing is owed. + #[cfg_attr(feature = "serde", serde(skip))] + pub unpersisted_cursor: Option, + /// In-memory only: the `RewindBarrier` epoch the latest contribution to + /// [`unpersisted_cursor`](Self::unpersisted_cursor) was recorded in. The + /// debt is paid once the host accepts a scan advance from this epoch or + /// later — see [`owe_cursor`](Self::owe_cursor). + #[cfg_attr(feature = "serde", serde(skip))] + pub unpersisted_cursor_epoch: Option, + /// In-memory only, like [`unpersisted_cursor`](Self::unpersisted_cursor): + /// the `(floor, rewound_from)` a rewind whose record round failed to + /// store still has to contribute. The retry folds it back in, so the + /// stored record keeps the backfill's real extent instead of the empty + /// one a retry with no rewind of its own would produce. + #[cfg_attr(feature = "serde", serde(skip))] + pub unpersisted_extent: Option<(u32, u32)>, + /// In-memory only: the backend permanently refused a record round this + /// session (a non-transient store failure). Coverage is then kept for + /// the session only, beside the debt, and no further record round is + /// attempted: retrying a rejection that will keep failing would only + /// leave every later sweep rewinding to the same contacts again. A + /// relaunch starts with it clear, and with no durable coverage for those + /// contacts re-runs their backfill. + #[cfg_attr(feature = "serde", serde(skip))] + pub store_refused: bool, +} + +/// Whether `persister` can hold a durable [`DashPayBackfillRecord`]. +/// +/// The backend has to store the record at all +/// ([`stores_dashpay_backfill`](PlatformWalletPersistence::stores_dashpay_backfill)), +/// and the receival accounts the coverage is about +/// ([`ACCOUNT_ADDRESS_POOLS`](PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS)): +/// coverage restored without its account vouches for scripts nothing watches. +/// The record vouches for the cursor written in the same round, so both +/// halves have to land together or not at all, and be on disk once `store` +/// returns. That takes a backend whose `store` is the commit +/// ([`store_commits_inline`](PlatformWalletPersistence::store_commits_inline)) +/// AND that attests [`PersistenceCapabilities::ATOMIC_CHANGESETS`]: an +/// inline but non-atomic backend could keep the coverage after rejecting +/// the cursor, and a buffering one could still drop the round at `flush`. +/// Any other backend gets session-only coverage — the pre-record guard. +pub fn persists_backfill_coverage

(persister: &P) -> bool +where + P: PlatformWalletPersistence + ?Sized, +{ + persister.stores_dashpay_backfill() + && persister.store_commits_inline() + && persister.persistence_capabilities().contains( + PersistenceCapabilities::ATOMIC_CHANGESETS + .union(PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS), + ) +} + +/// The persisted fields of a [`DashPayBackfillRecord`] as serde reads them, +/// before [`DashPayBackfillRecord::from_entries`] canonicalizes the cover set. +#[cfg(feature = "serde")] +#[derive(serde::Deserialize)] +struct DashPayBackfillRecordSerde { + floor: u32, + rewound_from: u32, + covered: Vec, +} + +#[cfg(feature = "serde")] +impl From for DashPayBackfillRecord { + fn from(record: DashPayBackfillRecordSerde) -> Self { + Self::from_entries(record.floor, record.rewound_from, record.covered) + } +} + +impl DashPayBackfillRecord { + /// A restored record from its persisted parts, with the cover set put in + /// canonical form whatever order or duplicates the source held: sorted by + /// receival-account key, one entry per account. A duplicated account + /// keeps its HIGHEST `covered_from` — an ambiguous record must fail + /// closed, and the higher height is the one that re-arms more + /// checkpoints. Every decoder of a stored record goes through here, so + /// [`covered_from`](Self::covered_from)'s binary search never lands on a + /// lower duplicate. + pub fn from_entries( + floor: u32, + rewound_from: u32, + mut covered: Vec, + ) -> Self { + covered.sort_by_key(|entry| (entry.key(), std::cmp::Reverse(entry.covered_from))); + covered.dedup_by(|a, b| a.key() == b.key()); + Self { + floor, + rewound_from, + covered, + unpersisted_cursor: None, + unpersisted_cursor_epoch: None, + unpersisted_extent: None, + store_refused: false, + } + } + + /// Owe the host `cursor`, lowered in memory during rewind epoch `epoch`. + /// Folds into any debt already owed: the lower cursor, and the later + /// epoch — the debt is paid only by an advance the host accepts after the + /// latest lowering. + pub fn owe_cursor(&mut self, cursor: u32, epoch: u64) { + self.unpersisted_cursor = Some( + self.unpersisted_cursor + .map_or(cursor, |owed| owed.min(cursor)), + ); + self.unpersisted_cursor_epoch = Some( + self.unpersisted_cursor_epoch + .map_or(epoch, |owed| owed.max(epoch)), + ); + } + + /// Forget the owed cursor once the host has accepted a scan advance from + /// the epoch it was owed in or later (`host_advance_epoch`): the host + /// then tracks the scan that restarted from the lowered cursor, and + /// writing the owed height would only drag it back below that scan. + /// Returns whether a debt was dropped. + pub fn settle_owed_cursor(&mut self, host_advance_epoch: Option) -> bool { + let paid = self.unpersisted_cursor.is_some() + && host_advance_epoch + .zip(self.unpersisted_cursor_epoch) + .is_some_and(|(host, owed)| host >= owed); + if paid { + self.unpersisted_cursor = None; + self.unpersisted_cursor_epoch = None; + } + paid + } + + /// The covered receival accounts, sorted by `(owner, contact, + /// account_index)`, at most one entry per account. + pub fn covered(&self) -> &[DashPayBackfillCoveredContact] { + &self.covered + } + + /// Whether no backfill has ever been recorded for this wallet. + pub fn is_empty(&self) -> bool { + self.covered.is_empty() + } + + /// The height this record covers the receival account `(owner, contact, + /// account_index)` from, if it covers it at all. + pub fn covered_from( + &self, + owner: &Identifier, + contact: &Identifier, + account_index: u32, + ) -> Option { + self.covered + .binary_search_by(|entry| entry.key().cmp(&(*owner, *contact, account_index))) + .ok() + .map(|index| self.covered[index].covered_from) + } + + /// Whether the receival account `(owner, contact, account_index)` is + /// covered at `checkpoint`: the record lists it and its checkpoint has not + /// dropped below the height it was covered from. + pub fn covers( + &self, + owner: &Identifier, + contact: &Identifier, + account_index: u32, + checkpoint: u32, + ) -> bool { + self.covered_from(owner, contact, account_index) + .is_some_and(|covered_from| checkpoint >= covered_from) + } + + /// Whether a backfill ever rewound the cursor, i.e. whether `floor` / + /// `rewound_from` describe anything. A record whose every contact was + /// forward-covered has entries but no extent, and `rewound_from == 0` + /// cannot come from a real rewind (a checkpoint is never below 0). + pub fn has_extent(&self) -> bool { + self.rewound_from > 0 + } + + /// The backfill is still climbing back to the cursor it rewound from. + pub fn is_pending(&self, synced_height: u32) -> bool { + self.has_extent() && synced_height < self.rewound_from + } + + /// The scan has climbed back past the cursor the backfill rewound from, + /// so every block in `[floor, rewound_from]` has been re-matched. + pub fn is_complete(&self, synced_height: u32) -> bool { + self.has_extent() && synced_height >= self.rewound_from + } + + /// Fold one reconcile pass into the record. + /// + /// `synced_height_before` is the cursor before the pass rewound it (the + /// height the scan must climb back to); `floor` is where the pass rewound + /// to, or `None` when every contact it handled was already covered by the + /// forward scan. Each `(owner, contact, account_index, covered_from)` entry + /// replaces any existing entry for that receival account. + /// + /// Returns `true` when the record changed. + pub fn record_pass( + &mut self, + synced_height_before: u32, + floor: Option, + entries: impl IntoIterator, + ) -> bool { + let mut changed = false; + for (owner, contact, account_index, covered_from) in entries { + let entry = DashPayBackfillCoveredContact { + owner, + contact, + account_index, + covered_from, + }; + match self + .covered + .binary_search_by(|existing| existing.key().cmp(&entry.key())) + { + Ok(index) => { + if self.covered[index] != entry { + self.covered[index] = entry; + changed = true; + } + } + Err(index) => { + self.covered.insert(index, entry); + changed = true; + } + } + } + // The extent describes real rewinds only. A pass that rewound nothing + // (every contact forward-covered, or already covered) says nothing + // about how far a backfill reached, so it must not invent one from + // the cursor it happened to run at — on a record that had no extent + // yet that would make `floor == rewound_from == cursor`, which reads + // complete at any later cursor and measures nothing. + let Some(pass_floor) = floor else { + return changed; + }; + // A first rewind takes the pass's extent as-is; a later one can only + // widen it — deeper floor, higher climb target. "First" is read off + // the extent, never off the cover set: `retain` can empty the set + // while the extent of the backfill those entries rode is still + // being climbed. + let (new_floor, new_rewound_from) = if !self.has_extent() { + (pass_floor, synced_height_before) + } else { + ( + self.floor.min(pass_floor), + self.rewound_from.max(synced_height_before), + ) + }; + if new_floor != self.floor || new_rewound_from != self.rewound_from { + self.floor = new_floor; + self.rewound_from = new_rewound_from; + changed = true; + } + changed + } + + /// Drop every entry for which `keep(owner, contact, account_index)` is + /// false — used to forget receival accounts that no longer exist. Returns + /// `true` when the record changed. + pub fn retain(&mut self, mut keep: impl FnMut(&Identifier, &Identifier, u32) -> bool) -> bool { + let before = self.covered.len(); + self.covered + .retain(|entry| keep(&entry.owner, &entry.contact, entry.account_index)); + before != self.covered.len() + } + + /// Wire size of one [`DashPayBackfillCoveredContact`] in + /// [`covered_bytes`](Self::covered_bytes): owner ‖ contact ‖ + /// `account_index` ‖ `covered_from` (both little-endian). + pub const COVERED_ENTRY_LEN: usize = 32 + 32 + 4 + 4; + + /// Flat encoding of [`covered`](Self::covered) for hosts that store the + /// list as one opaque blob: [`COVERED_ENTRY_LEN`](Self::COVERED_ENTRY_LEN) + /// bytes per entry, in record order. + pub fn covered_bytes(&self) -> Vec { + let mut out = Vec::with_capacity(self.covered.len() * Self::COVERED_ENTRY_LEN); + for entry in &self.covered { + out.extend_from_slice(&entry.owner.to_buffer()); + out.extend_from_slice(&entry.contact.to_buffer()); + out.extend_from_slice(&entry.account_index.to_le_bytes()); + out.extend_from_slice(&entry.covered_from.to_le_bytes()); + } + out + } + + /// Inverse of [`covered_bytes`](Self::covered_bytes). + /// + /// Returns `None` when `bytes` is not a whole number of entries: a + /// truncated or foreign blob must not be read as a shorter cover set, + /// because a contact missing from the set is simply re-armed while a + /// contact wrongly *present* in it is never backfilled again. The caller + /// treats `None` as "no record" — the safe direction. + pub fn from_parts(floor: u32, rewound_from: u32, covered_bytes: &[u8]) -> Option { + if !covered_bytes.len().is_multiple_of(Self::COVERED_ENTRY_LEN) { + return None; + } + let (chunks, _) = covered_bytes.as_chunks::<{ Self::COVERED_ENTRY_LEN }>(); + let covered: Vec = chunks + .iter() + .map(|chunk| { + let mut owner = [0u8; 32]; + let mut contact = [0u8; 32]; + let mut account_index = [0u8; 4]; + let mut height = [0u8; 4]; + owner.copy_from_slice(&chunk[..32]); + contact.copy_from_slice(&chunk[32..64]); + account_index.copy_from_slice(&chunk[64..68]); + height.copy_from_slice(&chunk[68..72]); + DashPayBackfillCoveredContact { + owner: Identifier::from(owner), + contact: Identifier::from(contact), + account_index: u32::from_le_bytes(account_index), + covered_from: u32::from_le_bytes(height), + } + }) + .collect(); + Some(Self::from_entries(floor, rewound_from, covered)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn id(byte: u8) -> Identifier { + Identifier::from([byte; 32]) + } + + #[test] + fn a_first_pass_sets_the_extent_and_a_later_one_only_widens_it() { + let mut record = DashPayBackfillRecord::default(); + assert!(record.is_empty()); + assert!(!record.is_pending(0)); + assert!(!record.is_complete(u32::MAX)); + + assert!(record.record_pass(1_000, Some(100), [(id(1), id(2), 0, 100)])); + assert_eq!((record.floor, record.rewound_from), (100, 1_000)); + assert!(record.is_pending(999)); + assert!(record.is_complete(1_000)); + + // A shallower rewind while climbing neither raises the floor nor + // lowers the climb target. + assert!(record.record_pass(400, Some(300), [(id(1), id(3), 0, 300)])); + assert_eq!((record.floor, record.rewound_from), (100, 1_000)); + + // A deeper rewind after completion lowers the floor and raises the + // climb target to the cursor it rewound from. + assert!(record.record_pass(2_000, Some(50), [(id(1), id(4), 0, 50)])); + assert_eq!((record.floor, record.rewound_from), (50, 2_000)); + + // A forward-covered pass (no rewind) records the contact without + // touching the extent it cannot widen. + assert!(record.record_pass(1_500, None, [(id(1), id(5), 0, 1_700)])); + assert_eq!((record.floor, record.rewound_from), (50, 2_000)); + assert_eq!(record.covered_from(&id(1), &id(5), 0), Some(1_700)); + + // An unchanged replay is reported as such. + assert!(!record.record_pass(1_500, None, [(id(1), id(5), 0, 1_700)])); + } + + /// A pass that rewound nothing must not invent an extent from the cursor + /// it ran at: on the device that produced `floor == rewound_from == + /// 1251329` — the cursor of a later pass — which read complete at any + /// later cursor. The first real rewind then sets the extent. + #[test] + fn a_forward_only_pass_on_an_empty_record_leaves_no_extent() { + let mut record = DashPayBackfillRecord::default(); + assert!(record.record_pass(1_251_329, None, [(id(1), id(2), 0, 1_300_000)])); + assert!(!record.has_extent()); + assert!(!record.is_pending(1_000)); + assert!(!record.is_complete(2_000_000)); + assert!(record.covers(&id(1), &id(2), 0, 1_300_000)); + + assert!(record.record_pass(1_560_731, Some(1_226_329), [(id(1), id(3), 0, 1_226_329)])); + assert!(record.has_extent()); + assert_eq!((record.floor, record.rewound_from), (1_226_329, 1_560_731)); + assert!(record.is_pending(1_251_329)); + assert!(record.is_complete(1_560_731)); + } + + #[test] + fn coverage_is_per_contact_and_keyed_on_the_recorded_checkpoint() { + let mut record = DashPayBackfillRecord::default(); + record.record_pass( + 1_000, + Some(100), + [(id(1), id(2), 0, 100), (id(1), id(3), 0, 300)], + ); + + assert!(record.covers(&id(1), &id(2), 0, 100)); + assert!(record.covers(&id(1), &id(2), 0, 250)); + assert!( + !record.covers(&id(1), &id(2), 0, 99), + "a checkpoint below the covered height re-arms" + ); + assert!( + !record.covers(&id(1), &id(9), 0, 500), + "an unlisted contact is never covered" + ); + assert!( + !record.covers(&id(7), &id(2), 0, 500), + "coverage is per owner, too" + ); + + // Re-recording a pair replaces its entry. + assert!(record.record_pass(500, Some(40), [(id(1), id(2), 0, 40)])); + assert_eq!(record.covered_from(&id(1), &id(2), 0), Some(40)); + assert_eq!(record.covered.len(), 2); + } + + #[test] + fn the_flat_encoding_round_trips_and_refuses_a_torn_blob() { + let mut record = DashPayBackfillRecord::default(); + record.record_pass( + 1_000, + Some(100), + [(id(9), id(2), 0, 100), (id(1), id(3), 0, 300)], + ); + let bytes = record.covered_bytes(); + assert_eq!(bytes.len(), 2 * DashPayBackfillRecord::COVERED_ENTRY_LEN); + + let restored = DashPayBackfillRecord::from_parts(record.floor, record.rewound_from, &bytes) + .expect("a whole blob decodes"); + assert_eq!(restored, record); + + assert!( + DashPayBackfillRecord::from_parts(100, 1_000, &bytes[..bytes.len() - 1]).is_none(), + "a torn blob must read as no record, never as a shorter cover set" + ); + let empty = DashPayBackfillRecord::from_parts(0, 0, &[]).expect("empty decodes"); + assert!(empty.is_empty()); + } + + #[test] + fn a_host_stored_blob_is_canonicalised_on_the_way_in() { + let entry = |owner: u8, contact: u8, from: u32| { + let mut bytes = Vec::new(); + bytes.extend_from_slice(&[owner; 32]); + bytes.extend_from_slice(&[contact; 32]); + bytes.extend_from_slice(&0u32.to_le_bytes()); + bytes.extend_from_slice(&from.to_le_bytes()); + bytes + }; + let mut bytes = entry(5, 6, 700); + bytes.extend(entry(1, 2, 100)); + bytes.extend(entry(5, 6, 900)); // duplicate pair + let record = DashPayBackfillRecord::from_parts(100, 1_000, &bytes).expect("decodes"); + assert_eq!(record.covered.len(), 2); + assert_eq!(record.covered[0].owner, id(1)); + assert_eq!( + record.covered_from(&id(5), &id(6), 0), + Some(900), + "a duplicated account keeps its highest height, re-arming more" + ); + assert!(!record.covers(&id(5), &id(6), 0, 800)); + } + + /// Serde is a restore path too: a record serialized with its cover set + /// out of order and holding a conflicting duplicate must come back in + /// canonical form, the duplicate at its highest height — not left for + /// the binary search to land on the lower one and suppress a rewind. + #[cfg(feature = "serde")] + #[test] + fn a_serde_restored_record_is_canonicalised_on_the_way_in() { + let entry = |owner: u8, contact: u8, covered_from: u32| DashPayBackfillCoveredContact { + owner: id(owner), + contact: id(contact), + account_index: 0, + covered_from, + }; + let stored = DashPayBackfillRecord { + floor: 100, + rewound_from: 1_000, + covered: vec![entry(5, 6, 700), entry(1, 2, 100), entry(5, 6, 900)], + ..Default::default() + }; + let json = serde_json::to_string(&stored).expect("serializes"); + let restored: DashPayBackfillRecord = serde_json::from_str(&json).expect("deserializes"); + assert_eq!( + restored, + DashPayBackfillRecord::from_entries( + 100, + 1_000, + vec![entry(1, 2, 100), entry(5, 6, 900)] + ) + ); + assert_eq!( + restored.covered, + vec![entry(1, 2, 100), entry(5, 6, 900)], + "sorted by account, one entry each" + ); + assert!( + !restored.covers(&id(5), &id(6), 0, 800), + "the duplicate keeps its highest height, re-arming a checkpoint below it" + ); + } + + #[test] + fn retain_forgets_contacts_and_reports_whether_anything_went() { + let mut record = DashPayBackfillRecord::default(); + record.record_pass( + 1_000, + Some(100), + [(id(1), id(2), 0, 100), (id(1), id(3), 0, 300)], + ); + assert!(!record.retain(|_, _, _| true)); + assert!(record.retain(|_, contact, _| *contact != id(3))); + assert_eq!(record.covered_from(&id(1), &id(3), 0), None); + assert_eq!(record.covered_from(&id(1), &id(2), 0), Some(100)); + } + + /// A debt folds to the lower cursor and the later epoch, and is settled + /// only by a host advance from that epoch or later. + #[test] + fn an_owed_cursor_is_settled_only_by_a_host_advance_from_its_epoch_or_later() { + let mut record = DashPayBackfillRecord::default(); + assert!(!record.settle_owed_cursor(Some(9)), "nothing owed"); + record.owe_cursor(500, 2); + record.owe_cursor(300, 1); + assert_eq!(record.unpersisted_cursor, Some(300)); + assert_eq!(record.unpersisted_cursor_epoch, Some(2)); + + assert!( + !record.settle_owed_cursor(None), + "a seeded host cursor proves nothing" + ); + assert!( + !record.settle_owed_cursor(Some(1)), + "an advance from before the latest lowering" + ); + assert_eq!(record.unpersisted_cursor, Some(300)); + assert!(record.settle_owed_cursor(Some(2))); + assert_eq!(record.unpersisted_cursor, None); + assert_eq!(record.unpersisted_cursor_epoch, None); + } + + /// `retain` can empty the cover set while the backfill those entries rode + /// is still climbing. The next rewind must widen that extent, not replace + /// it: replacing `(100, 1000)` with `(200, 500)` would read complete at 500 + /// with `[500, 1000]` never re-matched. + #[test] + fn pruning_the_last_covered_contact_keeps_the_extent_the_next_rewind_widens() { + let mut record = DashPayBackfillRecord::default(); + record.record_pass(1_000, Some(100), [(id(1), id(2), 0, 100)]); + assert!(record.retain(|_, _, _| false)); + assert!(record.is_empty()); + assert_eq!((record.floor, record.rewound_from), (100, 1_000)); + + assert!(record.record_pass(500, Some(200), [(id(1), id(3), 0, 200)])); + assert_eq!((record.floor, record.rewound_from), (100, 1_000)); + assert!(record.is_pending(500)); + assert!(!record.is_complete(999)); + } + + /// A second receiving account for the same relationship derives its own + /// xpub, so a scan that watched the first never tested the second's + /// scripts: coverage is per receival account, not per pair. + #[test] + fn coverage_is_per_receival_account_not_per_relationship() { + let mut record = DashPayBackfillRecord::default(); + record.record_pass(1_000, Some(100), [(id(1), id(2), 0, 100)]); + assert!(record.covers(&id(1), &id(2), 0, 100)); + assert!( + !record.covers(&id(1), &id(2), 1, 100), + "account 1 never inherits account 0's coverage" + ); + + record.record_pass(1_000, Some(100), [(id(1), id(2), 1, 100)]); + assert_eq!(record.covered.len(), 2); + let restored = DashPayBackfillRecord::from_parts( + record.floor, + record.rewound_from, + &record.covered_bytes(), + ) + .expect("decodes"); + assert_eq!(restored, record); + assert!(record.retain(|_, _, account_index| account_index == 0)); + assert!(!record.covers(&id(1), &id(2), 1, 100)); + assert!(record.covers(&id(1), &id(2), 0, 100)); + } +} diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index 4125b8df568..f246d3f6056 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -13,6 +13,7 @@ pub mod changeset; pub mod client_start_state; pub mod client_wallet_start_state; pub mod core_bridge; +pub mod dashpay_backfill; pub mod identity_manager_start_state; pub mod identity_scan_state; pub mod merge; @@ -45,7 +46,13 @@ pub use changeset::{ }; pub use client_start_state::ClientStartState; pub use client_wallet_start_state::ClientWalletStartState; -pub use core_bridge::spawn_wallet_event_adapter; +pub use core_bridge::{ + spawn_wallet_event_adapter, spawn_wallet_event_adapter_with_durable_cursors, DurableCursor, + DurableCursorState, DurableCursors, RetiredBarrier, +}; +pub use dashpay_backfill::{ + persists_backfill_coverage, DashPayBackfillCoveredContact, DashPayBackfillRecord, +}; pub use identity_manager_start_state::IdentityManagerStartState; pub use identity_scan_state::IdentityScanStateEntry; pub use merge::Merge; @@ -58,5 +65,6 @@ pub use shielded_changeset_disabled::ShieldedChangeSet; #[cfg(feature = "shielded")] pub use shielded_sync_start_state::{ShieldedSubwalletStartState, ShieldedSyncStartState}; pub use traits::{ - ListedCoreTxid, PersistenceError, PersistenceErrorKind, PlatformWalletPersistence, + persists_outbound_account_markers, ListedCoreTxid, PersistenceError, PersistenceErrorKind, + PlatformWalletPersistence, }; diff --git a/packages/rs-platform-wallet/src/changeset/traits.rs b/packages/rs-platform-wallet/src/changeset/traits.rs index e1538a26263..11e5d2c58a7 100644 --- a/packages/rs-platform-wallet/src/changeset/traits.rs +++ b/packages/rs-platform-wallet/src/changeset/traits.rs @@ -172,6 +172,32 @@ impl PersistenceError { } } +/// Whether a contact's outbound-account marker +/// (`EstablishedContact::external_account_reference`) can be kept on +/// `persister` and trusted after a restart. +/// +/// A restored marker suppresses the cold-start rebuild of the contact's +/// outbound account, so it may only reach disk where the account it names is +/// durably written with it: a backend whose `store` is the commit +/// ([`store_commits_inline`](PlatformWalletPersistence::store_commits_inline)), +/// whose rounds are all-or-nothing +/// ([`ATOMIC_CHANGESETS`](PersistenceCapabilities::ATOMIC_CHANGESETS)), and +/// that persists account registrations and their address pools +/// ([`ACCOUNT_ADDRESS_POOLS`](PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS)). +/// On any other backend the marker is kept in memory only: contact rows are +/// written without it and a restored one is not trusted, so each launch +/// rebuilds the account once — the behaviour before the marker was persisted. +pub fn persists_outbound_account_markers

(persister: &P) -> bool +where + P: PlatformWalletPersistence + ?Sized, +{ + persister.store_commits_inline() + && persister.persistence_capabilities().contains( + PersistenceCapabilities::ATOMIC_CHANGESETS + .union(PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS), + ) +} + /// Storage backend for [`PlatformWalletChangeSet`] deltas. /// /// The persister persists what the changeset carries — nothing more, @@ -271,6 +297,24 @@ pub trait PlatformWalletPersistence: Send + Sync { false } + /// Whether this backend persists the + /// [`DashPayBackfillRecord`](crate::changeset::DashPayBackfillRecord) a + /// changeset carries, and can replace one it holds. + /// + /// Durable backfill coverage also needs inline, atomic rounds (see + /// [`persists_backfill_coverage`](crate::changeset::persists_backfill_coverage)). + /// A backend that answers `false` gets session-only coverage, and a + /// record it still hands back on load can only be invalidated through + /// [`store`](Self::store) — so a backend that cannot write the record + /// must fail such a store rather than acknowledge it. + /// + /// **Fail-closed:** the default is `false`. A backend that ignores the + /// record keeps the pre-record behaviour: the backfill re-runs on every + /// launch, slow but never lossy. + fn stores_dashpay_backfill(&self) -> bool { + false + } + /// Compatibility summary for older invitation callers. It is true when the /// backend attests atomic changesets plus durably persisted invitation rows /// and asset-lock funding indices. This does not attest restart hydration; diff --git a/packages/rs-platform-wallet/src/error.rs b/packages/rs-platform-wallet/src/error.rs index 73955b6604f..cf2aba64e9f 100644 --- a/packages/rs-platform-wallet/src/error.rs +++ b/packages/rs-platform-wallet/src/error.rs @@ -38,6 +38,16 @@ pub enum PlatformWalletError { #[error("failed to persist wallet changeset: {0}")] PersisterStore(#[source] crate::changeset::PersistenceError), + /// A persister `flush` failed: the commit of rounds a backend whose + /// `store` only buffers was still holding. It keeps the flush's own + /// classification, which means something different from a store's: + /// `Transient` says the buffer survived and another `flush` may commit + /// it, `Constraint` that the buffered data was rejected, and `Fatal` that + /// the buffered rounds were discarded. See [`Self::PersisterLoad`] for why + /// the typed cause is carried. + #[error("failed to commit buffered wallet changesets: {0}")] + PersisterFlush(#[source] crate::changeset::PersistenceError), + /// Restoring persisted platform-address state into a freshly registered /// wallet failed. Boxed to break the recursion; the inner variant and its /// `#[source]` chain survive intact. @@ -1012,6 +1022,16 @@ impl PlatformWalletError { Self::PersisterStore(source) } + /// A persister `flush` failed. Unlike + /// [`from_store_failure`](Self::from_store_failure) the classification is + /// kept as the backend reported it: a transient flush failure is retried + /// with another `flush`, which the store-side reissue attestation says + /// nothing about. See [`Self::from_load_failure`] for why no blanket + /// conversion exists. + pub fn from_flush_failure(source: crate::changeset::PersistenceError) -> Self { + Self::PersisterFlush(source) + } + /// Restoring persisted platform-address state failed. Boxes `source`, so /// callers never write `Box::new`. pub fn from_restore_failure(source: PlatformWalletError) -> Self { diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index 1f9ed240c84..a5bfc7822b7 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -836,13 +836,14 @@ impl PlatformWalletManager

{ /// /// `synced_height` may regress here: that is safe because it is the /// filter-scan checkpoint, decoupled from the monotonic - /// `last_processed_height`, and every persisted sync cursor is - /// monotonic-max guarded (see `reconcile_dashpay_rescan`'s note), so a - /// transient rewind cannot corrupt state or persist a lower cursor. + /// `last_processed_height`, so a transient rewind cannot corrupt state. /// /// The rewound checkpoint lives only in the in-memory `WalletManager`; this - /// call does not persist it. If the process dies before the rescan finishes, - /// the host must issue this request again after restart. Requires SPV + /// call does not persist it — though the host will persist every height + /// the rescan then climbs through, so a relaunch resumes the climb from + /// wherever it got to rather than from the high-water. If the process dies + /// before the rescan finishes and the host wants it restarted from + /// `from_height`, it must issue this request again after restart. Requires SPV /// running for an immediate effect; otherwise it takes effect when SPV next /// starts in the same process and its filter loop first ticks. /// @@ -865,6 +866,15 @@ impl PlatformWalletManager

{ return true; } info.core_wallet.update_synced_height(from_height); + // Advances the engine emitted before this reset must not be stored + // after it. + info.rewind_barrier.arm(); + // Not persisted here, so remember it as owed: the next DashPay + // backfill record round carries it as that round's cursor, and no + // coverage reaches disk beside a durable cursor this reset retracted + // in memory only (dashpay/platform#4302 review). + let epoch = info.rewind_barrier.epoch(); + info.dashpay_backfill.owe_cursor(from_height, epoch); tracing::info!( wallet_id = %hex::encode(wallet_id), from_height, @@ -1946,6 +1956,8 @@ mod txo_inventory_tests { identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), observed_input_conflicts: Default::default(), }; let mut wm = WalletManager::::new(dashcore::Network::Testnet); diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 1bbd0330172..a4bde29f0ec 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -13,6 +13,10 @@ use crate::wallet::PlatformWallet; use std::time::Duration; use crate::broadcaster::{BroadcastError, TransactionBroadcaster}; +use dpp::prelude::Identifier; +use key_wallet::account::derivation::AccountDerivation; +use key_wallet::managed_account::address_pool::{AddressPool, AddressPoolType}; +use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; use key_wallet::transaction_checking::transaction_context::TransactionContext; use key_wallet::transaction_checking::wallet_checker::WalletTransactionChecker; @@ -122,13 +126,27 @@ impl PlatformWalletManager

{ Vec::new(); let mut load_error: Option = None; - 'load: for (expected_wallet_id, wallet_state) in wallets { + // Each wallet's start state, and whether it was re-read under the + // durable-cursor lock that `held` still holds (see the publication + // step below). + let mut pending: std::collections::VecDeque<(WalletId, ClientWalletStartState, bool)> = + wallets + .into_iter() + .map(|(wallet_id, state)| (wallet_id, state, false)) + .collect(); + // The durable-cursor lock, kept from a re-read to the publication of + // the wallet processed from it. Nothing between the two takes the + // lock, and it is released before the rollback below. + let mut held = None; + + 'load: while let Some((expected_wallet_id, wallet_state, reread)) = pending.pop_front() { let ClientWalletStartState { mut wallet, mut wallet_info, identity_manager, unused_asset_locks, unconfirmed_outgoing_txs, + dashpay_backfill, } = wallet_state; // Replay the sends the host still holds as unconfirmed, before @@ -222,14 +240,55 @@ impl PlatformWalletManager

{ core_balance.immature(), core_balance.locked(), ); - let platform_info = PlatformWalletInfo { + let mut platform_info = PlatformWalletInfo { observed_input_conflicts: Default::default(), core_wallet: wallet_info, generation: Arc::clone(&generation), identity_manager: IdentityManager::from(identity_manager), tracked_asset_locks, dpns_name_states: std::collections::BTreeMap::new(), + dashpay_backfill, + rewind_barrier: Default::default(), }; + // A restored outbound-account marker says the contact's external + // account was built from its current xpub, which suppresses the + // startup rebuild. It must not vouch for a pool the host kept + // across a rotation: the host replaces the account's xpub but may + // keep the old pool's addresses, and `send_payment` hands out a + // pooled address before deriving a new one. So any external pool + // holding an address its current xpub does not derive loses its + // contact's marker, and the first contact sweep rebuilds it from + // the current xpub, as every cold start did before the marker + // was persisted (dashpay/platform#4302 review). + // + // A backend that cannot keep markers durably never had one written + // by this process, and one it hands back may name an account it + // did not keep: none is trusted, so the sweep rebuilds each + // outbound account once, as before the marker was persisted. Its + // pools are then not validated at all: no result could keep a + // marker that is cleared regardless. + if crate::changeset::persists_outbound_account_markers(&*self.persister) { + let distrusted = + distrust_markers_of_foreign_external_pools(&wallet, &mut platform_info); + if distrusted > 0 { + tracing::warn!( + wallet_id = %hex::encode(expected_wallet_id), + contacts = distrusted, + "load: restored DashPay external pools hold addresses their current \ + xpub does not derive; their accounts will be rebuilt" + ); + } + } else { + let cleared = clear_restored_outbound_markers(&mut platform_info); + if cleared > 0 { + tracing::info!( + wallet_id = %hex::encode(expected_wallet_id), + contacts = cleared, + "load: this backend does not keep outbound-account markers durably; \ + restored markers are not trusted" + ); + } + } // Seed the double-spend screen's session memory from the // freshly restored state: it closes the race where SPV's // chainlock dispatcher promotion-evicts a restored spender @@ -238,6 +297,10 @@ impl PlatformWalletManager

{ &platform_info, ); + // The cursor the host just handed back is the durable one; it + // seeds the shared record of it as the wallet is published below. + let loaded_cursor = platform_info.core_wallet.metadata.synced_height; + if wallet_id != expected_wallet_id { load_error = Some(PlatformWalletError::WalletCreation(format!( "Persisted wallet id {} does not match recomputed id {}", @@ -265,11 +328,109 @@ impl PlatformWalletManager

{ // // The existence check and the insert share one write-lock scope // so a concurrent loader can't slip between them (TOCTOU). + // + // The durable cursor is seeded in that same scope, under the + // cursor lock taken first (its order: cursor lock, then manager). + // The insert makes the wallet visible to the scanner and the + // event adapter; seeding after it would let an adapter commit or + // a reconcile record a newer durable height first, and the seed + // would then overwrite it with the stale loaded one. An entry a + // removed same-id predecessor left behind is what the host holds + // now — a commit of its may have been accepted after the start + // state was read — so it wins over the loaded cursor, and a + // loaded cursor below it is owed to the host like any in-memory + // reset, in this wallet's inherited epoch, so no coverage is + // recorded beside the host's higher cursor without a reset + // (dashpay/platform#4302 review). + // + // The same predecessor can also have changed the host's DashPay + // backfill record after the start state was read: stored coverage + // for a receival account this snapshot does not hold, or + // invalidated coverage this snapshot still carries. So when an + // entry shows a same-id wallet was published in this process, the + // record this wallet carries replaces the host's before the wallet + // is published, paired with the loaded cursor when that is below + // the host's (`settle_published_backfill`). The existence check + // comes first, so a wallet still registered under this id is never + // overwritten, and every publisher holds the cursor lock, so none + // appears before the insert. + // + // A record is not all such a predecessor can have changed. It can + // also have replaced a receival account's xpub under the same + // `(owner, contact, index)` key and stored coverage scanned with + // the new one. Publishing this snapshot would restore the old + // xpub beside that key, and the scan it then runs would vouch for + // scripts the host's account does not derive. Coverage carries no + // xpub to check against, so the snapshot itself is replaced: under + // the lock, where no publisher or adapter commit can move the + // host, the start state is read again, and this wallet is + // processed from that read and published WITHOUT releasing the + // lock in between. A same-id wallet can only appear by being + // published, which takes this lock, so nothing can register, + // rotate an account and be removed between the read and the + // publication. No token stands in for that guarantee: a cursor + // entry does not move on account writes or same-id lifecycle + // changes, so it cannot prove a read is still current + // (dashpay/platform#4302 review). { - let mut wm = self.wallet_manager.write().await; - if wm.get_wallet(&wallet_id).is_some() { + let mut durable_cursors = match held.take() { + Some(guard) => guard, + None => self.durable_cursors.lock().await, + }; + if self + .wallet_manager + .read() + .await + .get_wallet(&wallet_id) + .is_some() + { continue 'load; } + if !reread && durable_cursors.contains_key(&wallet_id) { + let persister = Arc::clone(&self.persister); + let current = match run_blocking_load(move || persister.load()).await { + Ok(mut state) => state.wallets.remove(&expected_wallet_id), + Err(e) => { + load_error = Some(PlatformWalletError::from_load_failure(e)); + break 'load; + } + }; + match current { + Some(state) => { + pending.push_front((expected_wallet_id, state, true)); + held = Some(durable_cursors); + } + None => tracing::warn!( + wallet_id = %hex::encode(expected_wallet_id), + "load: the wallet is no longer on the host; not publishing its \ + stale snapshot" + ), + } + continue 'load; + } + // Coverage for a receival account this wallet did not restore + // vouches for scripts nothing will watch; it is dropped here, + // and from the host before the wallet is published, exactly + // as registration does (dashpay/platform#4302 review). + let dropped = super::retain_coverage_for_held_accounts( + &mut platform_info.dashpay_backfill, + &platform_info.core_wallet, + ); + match self.settle_published_backfill( + &mut durable_cursors, + wallet_id, + std::mem::take(&mut platform_info.dashpay_backfill), + dropped, + loaded_cursor, + loaded_cursor, + ) { + Ok(record) => platform_info.dashpay_backfill = record, + Err(e) => { + load_error = Some(e); + break 'load; + } + } + let mut wm = self.wallet_manager.write().await; if let Err(e) = wm.insert_wallet(wallet, platform_info) { load_error = Some(PlatformWalletError::WalletCreation(format!( "Failed to register persisted wallet in WalletManager: {}", @@ -277,6 +438,19 @@ impl PlatformWalletManager

{ ))); break 'load; } + let host_cursor = durable_cursors + .get(&wallet_id) + .map_or(loaded_cursor, |entry| entry.height); + durable_cursors + .entry(wallet_id) + .or_insert(crate::changeset::DurableCursor::at(loaded_cursor)); + self.inherit_rewind_barrier(&mut wm, &wallet_id); + if loaded_cursor < host_cursor { + if let Some(info) = wm.get_wallet_info_mut(&wallet_id) { + let epoch = info.rewind_barrier.epoch(); + info.dashpay_backfill.owe_cursor(loaded_cursor, epoch); + } + } } inserted_in_manager.push(wallet_id); @@ -340,6 +514,8 @@ impl PlatformWalletManager

{ Arc::clone(&self.lock_notify), Arc::clone(&persister_dyn), broadcaster, + Arc::clone(&self.sync_fault), + Arc::clone(&self.durable_cursors), ); // Initialize the platform-address provider. If the snapshot @@ -402,6 +578,9 @@ impl PlatformWalletManager

{ } } } + // A `break` between a re-read and its publication leaves the lock + // held; the rollback must not run under it. + drop(held); if let Some(err) = load_error { // Walk back every wallet committed in this call so the @@ -465,7 +644,7 @@ impl PlatformWalletManager

{ } continue; } - if let Err(e) = wm.remove_wallet(id) { + if let Err(e) = self.remove_from_wallet_manager(&mut wm, id) { tracing::warn!( wallet_id = %hex::encode(id), error = %e, @@ -718,6 +897,7 @@ mod idempotent_load_tests { identity_manager: IdentityManagerStartState::default(), unused_asset_locks: BTreeMap::new(), unconfirmed_outgoing_txs: self.pending.clone(), + dashpay_backfill: Default::default(), }, ); Ok(ClientStartState { @@ -784,6 +964,7 @@ mod idempotent_load_tests { identity_manager: IdentityManagerStartState::default(), unused_asset_locks: BTreeMap::new(), unconfirmed_outgoing_txs: Vec::new(), + dashpay_backfill: Default::default(), }, ); Ok(ClientStartState { @@ -823,6 +1004,7 @@ mod idempotent_load_tests { identity_manager: IdentityManagerStartState::default(), unused_asset_locks: BTreeMap::new(), unconfirmed_outgoing_txs: Vec::new(), + dashpay_backfill: Default::default(), }; let mut wallets = BTreeMap::new(); wallets.insert(self.wallet.compute_wallet_id(), entry()); @@ -1283,6 +1465,125 @@ mod idempotent_load_tests { } } +/// Clear the outbound-account marker of every established contact whose +/// restored DashPay external pool holds an address the account's current +/// xpub (on `wallet`) does not derive at that index, so the contact sweep +/// rebuilds the account instead of trusting the pool. Returns how many +/// markers were cleared. +fn distrust_markers_of_foreign_external_pools( + wallet: &key_wallet::Wallet, + info: &mut PlatformWalletInfo, +) -> usize { + let foreign: Vec<_> = info + .core_wallet + .accounts + .dashpay_external_accounts + .iter() + // Only a marker can be distrusted, so a relationship without one + // costs no derivation. + .filter(|(key, _)| { + info.identity_manager + .managed_identity(&Identifier::from(key.user_identity_id)) + .and_then(|managed| { + managed + .dashpay() + .established_contacts() + .get(&Identifier::from(key.friend_identity_id)) + }) + .is_some_and(|established| established.external_account_reference.is_some()) + }) + .filter(|(key, managed)| { + wallet + .accounts + .dashpay_external_accounts + .get(key) + .is_some_and(|account| { + managed + .managed_account_type() + .address_pools() + .into_iter() + .any(|pool| !pool_derives_from(pool, account)) + }) + }) + .map(|(key, _)| *key) + .collect(); + let mut cleared = 0; + for key in foreign { + let owner = Identifier::from(key.user_identity_id); + let contact = Identifier::from(key.friend_identity_id); + let marker = info + .identity_manager + .managed_identity_mut(&owner) + .and_then(|managed| managed.established_contact_mut(&contact)) + .and_then(|established| established.external_account_reference.take()); + if marker.is_some() { + cleared += 1; + } + } + cleared +} + +#[cfg(test)] +thread_local! { + /// How many restored pools [`pool_derives_from`] validated on this + /// thread, so a test can show a load did no derivation it did not need. + static POOL_VALIDATIONS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// How many restored pools have been validated on this thread. +#[cfg(test)] +pub(crate) fn pool_validations_on_this_thread() -> usize { + POOL_VALIDATIONS.with(std::cell::Cell::get) +} + +/// Clear every restored outbound-account marker on `info`. Returns how many +/// were set. +fn clear_restored_outbound_markers(info: &mut PlatformWalletInfo) -> usize { + let mut cleared = 0; + for owner in info.identity_manager.identity_ids() { + let Some(managed) = info.identity_manager.managed_identity_mut(&owner) else { + continue; + }; + let contacts: Vec = managed + .dashpay() + .established_contacts() + .keys() + .copied() + .collect(); + for contact in contacts { + if let Some(established) = managed.established_contact_mut(&contact) { + if established.external_account_reference.take().is_some() { + cleared += 1; + } + } + } + } + cleared +} + +/// Whether every address in `pool` is the one `account` derives at its index. +/// +/// Each restored index is derived on its own through key-wallet's +/// [`AccountDerivation::derive_address_at`] — the derivation that generates +/// the pool — so the cost follows the restored rows (a sparse row at a huge +/// index costs one derivation) and the check cannot drift from address +/// generation. The comparison is by script, which is what the pool watches +/// and pays to, so an address encoded for another network still matches its +/// key. A failed derivation counts as a mismatch, and a hardened pool never +/// matches: its children cannot be derived from a public key. +fn pool_derives_from(pool: &AddressPool, account: &key_wallet::Account) -> bool { + #[cfg(test)] + POOL_VALIDATIONS.with(|count| count.set(count.get() + 1)); + if pool.pool_type == AddressPoolType::AbsentHardened { + return pool.addresses.is_empty(); + } + pool.addresses.iter().all(|(index, info)| { + account + .derive_address_at(pool.pool_type, *index, None) + .is_ok_and(|address| address.script_pubkey() == info.script_pubkey) + }) +} + #[cfg(test)] mod tests { use std::sync::atomic::{AtomicUsize, Ordering}; @@ -1531,4 +1832,66 @@ mod tests { idle adapter holds no strong reference to await" ); } + + /// Restored-address validation costs one derivation per restored row: a + /// single sparse row at index 1,000,000 is checked directly, not by + /// deriving every index below it, and a row another xpub derived is still + /// caught. The expected addresses come from key-wallet's own per-index + /// derivation, so the validator cannot drift from it unnoticed + /// (dashpay/platform#4302 review). + #[test] + fn restored_address_validation_is_proportional_to_the_restored_rows() { + use key_wallet::account::derivation::AccountDerivation; + use key_wallet::account::AccountType; + use key_wallet::bip32::{ExtendedPrivKey, ExtendedPubKey}; + use key_wallet::managed_account::address_pool::AddressPoolType; + use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; + use key_wallet::managed_account::ManagedCoreFundsAccount; + + let secp = dashcore::secp256k1::Secp256k1::new(); + let account = |seed: u8| key_wallet::Account { + parent_wallet_id: None, + account_type: AccountType::DashpayExternalAccount { + index: 0, + user_identity_id: [0xAA; 32], + friend_identity_id: [0x01; 32], + }, + network: dashcore::Network::Testnet, + account_xpub: ExtendedPubKey::from_priv( + &secp, + &ExtendedPrivKey::new_master(dashcore::Network::Testnet, &[seed; 32]) + .expect("master"), + ), + is_watch_only: true, + }; + let (current, previous) = (account(1), account(2)); + let managed = ManagedCoreFundsAccount::from_account(¤t); + let pool = managed.managed_account_type().address_pools()[0].clone(); + assert_eq!(pool.pool_type, AddressPoolType::Absent); + assert!(super::pool_derives_from(&pool, ¤t)); + + let sparse = |from: &key_wallet::Account| { + let mut pool = pool.clone(); + let mut info = pool.addresses[&0].clone(); + info.index = 1_000_000; + info.address = from + .derive_address_at(AddressPoolType::Absent, 1_000_000, None) + .expect("key-wallet derives the row"); + info.script_pubkey = info.address.script_pubkey(); + pool.addresses.insert(1_000_000, info); + pool.highest_generated = Some(1_000_000); + pool + }; + + let started = std::time::Instant::now(); + assert!(super::pool_derives_from(&sparse(¤t), ¤t)); + assert!( + !super::pool_derives_from(&sparse(&previous), ¤t), + "a row the current account does not derive is caught" + ); + assert!( + started.elapsed() < std::time::Duration::from_secs(5), + "validation must not derive every index below the sparse row" + ); + } } diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 51a57c32451..f7fae03602c 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -5,6 +5,8 @@ pub mod dashpay_sync; pub mod dpns_sync; pub mod identity_sync; mod load; +#[cfg(test)] +pub(crate) use load::pool_validations_on_this_thread; mod persistence_load; pub mod platform_address_sync; #[cfg(feature = "shielded")] @@ -14,6 +16,7 @@ mod wallet_lifecycle; pub(crate) use persistence_load::run_blocking_load; +use std::collections::BTreeMap; use std::sync::Arc; use std::time::Duration; @@ -22,9 +25,16 @@ use tokio::sync::{Notify, RwLock}; use tokio::task::JoinHandle; use tokio_util::sync::CancellationToken; +use dpp::prelude::Identifier; +use key_wallet::wallet::ManagedWalletInfo; use key_wallet_manager::WalletManager; -use crate::changeset::{spawn_wallet_event_adapter, PlatformWalletPersistence}; +use crate::changeset::{ + persists_backfill_coverage, spawn_wallet_event_adapter_with_durable_cursors, CoreChangeSet, + DashPayBackfillRecord, DurableCursor, DurableCursors, PlatformWalletChangeSet, + PlatformWalletPersistence, +}; +use crate::error::PlatformWalletError; use crate::events::{PlatformEventHandler, PlatformEventManager}; use crate::manager::dashpay_sync::DashPaySyncManager; use crate::manager::dpns_sync::DpnsSyncManager; @@ -439,6 +449,10 @@ pub struct PlatformWalletManager { /// failed / rescan pending" state rather than re-freezing silently on /// the next launch. pub(super) sync_fault: Arc, + /// The durable sync cursor each wallet's host last accepted, shared by + /// the wallet-event adapter and the DashPay rescan reconcile — the lock + /// that orders their cursor writes. See [`DurableCursors`]. + pub(super) durable_cursors: DurableCursors, /// Per-WALLET in-broadcast fence maps, handed to every /// [`WalletGeneration`](crate::wallet::core::WalletGeneration) registered /// under each id. @@ -466,6 +480,39 @@ pub struct PlatformWalletManager { >, } +/// Keep `record`'s coverage only for the DashPay receival accounts `core` +/// actually holds, and report whether anything was dropped. +/// +/// Coverage vouches that an account's scripts were watched; for an account +/// the published wallet does not hold, nothing watches them, and a scan the +/// wallet commits before the account is registered again would sit inside +/// coverage that never saw it. Both publish paths apply this before +/// [`PlatformWalletManager::settle_published_backfill`], which replaces the +/// host's record when it reports a drop. Dropping the last entry also drops +/// the extent: nothing is left for it to describe. +pub(crate) fn retain_coverage_for_held_accounts( + record: &mut DashPayBackfillRecord, + core: &ManagedWalletInfo, +) -> bool { + let held: Vec<(Identifier, Identifier, u32)> = core + .accounts + .dashpay_receival_accounts + .keys() + .map(|key| { + ( + Identifier::from(key.user_identity_id), + Identifier::from(key.friend_identity_id), + key.index, + ) + }) + .collect(); + let dropped = record.retain(|owner, contact, index| held.contains(&(*owner, *contact, index))); + if dropped && record.is_empty() { + *record = DashPayBackfillRecord::default(); + } + dropped +} + impl PlatformWalletManager

{ /// Create a new PlatformWalletManager. /// @@ -500,16 +547,18 @@ impl PlatformWalletManager

{ // Host-visible hard sync-fault latch. The // adapter raises it the first time it freezes a durable watermark. let sync_fault = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let durable_cursors = DurableCursors::default(); // Spawn the wallet-event adapter that translates upstream // `WalletEvent`s into `CoreChangeSet`s and forwards them to // the persister. let event_adapter_cancel = CancellationToken::new(); - let event_adapter_join = spawn_wallet_event_adapter( + let event_adapter_join = spawn_wallet_event_adapter_with_durable_cursors( Arc::clone(&wallet_manager), Arc::downgrade(&persister), event_receiver, Arc::clone(&sync_fault), + Arc::clone(&durable_cursors), event_adapter_cancel.clone(), ); @@ -607,10 +656,163 @@ impl PlatformWalletManager

{ event_adapter_join: tokio::sync::Mutex::new(Some(event_adapter_join)), registry, sync_fault, + durable_cursors, in_broadcast_fences: std::sync::Mutex::new(std::collections::BTreeMap::new()), } } + /// Remove `wallet_id` from the inner manager, keeping its rewind + /// barrier's state under the id in the shared [`DurableCursors`] for a + /// successor registered under the same id (see + /// [`Self::inherit_rewind_barrier`]) — and for the adapter, which drops + /// the removed wallet's advances while nothing holds the id and counts + /// them off this state as it does. Called under the manager write lock, + /// which is what makes the hand-off atomic against the adapter. + pub(super) fn remove_from_wallet_manager( + &self, + wm: &mut WalletManager, + wallet_id: &WalletId, + ) -> Result<(), key_wallet_manager::WalletError> { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + let (_, info) = wm.remove_wallet(wallet_id)?; + let retiring = info + .rewind_barrier + .retire(WalletInfoInterface::account_generation(&info)); + let mut retired = self.durable_cursors.retired(); + let entry = retired.entry(*wallet_id).or_default(); + entry.in_flight = entry.in_flight.saturating_add(retiring.in_flight); + entry.epoch = entry.epoch.max(retiring.epoch); + entry.account_generation = entry.account_generation.max(retiring.account_generation); + Ok(()) + } + + /// Settle the host's DashPay backfill record for a wallet about to be + /// published under `wallet_id`, holding the durable-cursor lock + /// (`durable_cursors`), and return the record the wallet carries in + /// memory. + /// + /// `record` is what the wallet would carry — its snapshot's record, + /// already filtered to the receival accounts it holds — and `own_cursor` + /// the scan cursor it starts from. `snapshot_cursor` is the host cursor + /// the pre-lock snapshot saw; an entry in `durable_cursors` wins over it. + /// `dropped` says the filter removed coverage from the snapshot's record. + /// + /// The host's record is replaced, before the wallet is visible to the + /// scanner, when it may disagree with what the wallet carries: + /// - the filter dropped coverage for accounts this wallet does not hold; + /// - an entry shows a same-id wallet was published in this process — it + /// may have stored or invalidated coverage after the snapshot was read; + /// - the backend cannot hold the record durably and the snapshot held one. + /// + /// A replacement that puts coverage on the host while this wallet's cursor + /// is below the host's carries that cursor, as a rewind, in the same + /// round. Otherwise a crash right after it would restore the coverage + /// beside a cursor the wallet never scanned up to with those accounts + /// watched. The entry follows the cursor only once the round is + /// committed, so a lower cursor owed later is measured against it. + /// + /// A backend that cannot hold the record durably + /// ([`persists_backfill_coverage`](crate::changeset::persists_backfill_coverage): + /// not inline, or not atomic) gets no record at all, the same as the + /// rescan sweep gives it: only an empty one is written — committed with a + /// `flush` where `store` only buffers — before the wallet is published, + /// so a record an earlier run left cannot outlive this one. + #[allow(clippy::too_many_arguments)] + pub(super) fn settle_published_backfill( + &self, + durable_cursors: &mut BTreeMap, + wallet_id: WalletId, + record: DashPayBackfillRecord, + dropped: bool, + own_cursor: u32, + snapshot_cursor: u32, + ) -> Result { + let inline = self.persister.store_commits_inline(); + let durable = persists_backfill_coverage(&*self.persister); + let predecessor = durable_cursors.contains_key(&wallet_id); + let held_on_host = !record.is_empty() || dropped; + let record = if durable { + record + } else { + DashPayBackfillRecord::default() + }; + // A backend that cannot hold the record durably never had one + // written by this process, so only a record it handed back can need + // invalidating; one that can is replaced when the snapshot's record + // may disagree with what the wallet carries. + let replace = if durable { + dropped || predecessor + } else { + held_on_host + }; + if !replace { + return Ok(record); + } + let host_cursor = durable_cursors + .get(&wallet_id) + .map_or(snapshot_cursor, |entry| entry.height); + let cursor = (!record.is_empty() && own_cursor < host_cursor).then_some(own_cursor); + tracing::info!( + wallet_id = %hex::encode(wallet_id), + covered = record.covered().len(), + dropped, + predecessor, + durable, + cursor = ?cursor, + "the host's DashPay backfill record is replaced with the one this wallet \ + carries before the wallet is published" + ); + let changeset = PlatformWalletChangeSet { + core: cursor.map(|cursor| CoreChangeSet { + synced_height: Some(cursor), + synced_height_is_rewind: true, + ..CoreChangeSet::default() + }), + dashpay_backfill: Some(record.clone()), + ..PlatformWalletChangeSet::default() + }; + self.persister.store(wallet_id, changeset).map_err(|e| { + tracing::error!( + wallet_id = %hex::encode(wallet_id), + error = %e, + "failed to replace the host's DashPay backfill record; \ + the wallet was not published" + ); + PlatformWalletError::from_store_failure(&*self.persister, e) + })?; + if !inline { + self.persister.flush(wallet_id).map_err(|e| { + tracing::error!( + wallet_id = %hex::encode(wallet_id), + error = %e, + "failed to commit the replaced DashPay backfill record; \ + the wallet was not published" + ); + PlatformWalletError::from_flush_failure(e) + })?; + } + if let Some(cursor) = cursor { + durable_cursors.insert(wallet_id, DurableCursor::at(cursor)); + } + Ok(record) + } + + /// Hand a newly published wallet the barrier state a removed wallet of + /// the same id left behind, so advances that wallet emitted — still + /// queued in, or already projected by, the shared adapter — are dropped + /// rather than stored against the new wallet's scan. Called inside the + /// critical section that publishes the wallet. + pub(super) fn inherit_rewind_barrier( + &self, + wm: &mut WalletManager, + wallet_id: &WalletId, + ) { + let retired = self.durable_cursors.retired().remove(wallet_id); + if let (Some(retired), Some(info)) = (retired, wm.get_wallet_info_mut(wallet_id)) { + info.rewind_barrier.inherit(retired); + } + } + /// The in-broadcast fence map for `wallet_id`, creating it on first use. /// /// Every [`WalletGeneration`](crate::wallet::core::WalletGeneration) this @@ -1188,6 +1390,8 @@ mod tests { Arc::new(Notify::new()), Arc::new(NoopPersister) as Arc, Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), + Arc::new(std::sync::atomic::AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), )); mgr.wallets.rcu(|wallets| { let mut next = std::collections::BTreeMap::clone(wallets); diff --git a/packages/rs-platform-wallet/src/manager/startup.rs b/packages/rs-platform-wallet/src/manager/startup.rs index 04c07d50832..36f4721bb3a 100644 --- a/packages/rs-platform-wallet/src/manager/startup.rs +++ b/packages/rs-platform-wallet/src/manager/startup.rs @@ -748,6 +748,47 @@ impl PlatformWalletManager ); } + // The DashPay sync above ran its rescan sweep BEFORE the drain created + // the receival accounts, so it saw none of them. Run it once more now: + // with `synced_height == 0` (this sequence runs before SPV starts) the + // sweep marks every registered receival contact as covered by the + // coming full scan, instead of leaving it for the first post-scan + // sweep to rewind for. + // + // Budgeted, because it is not free: it waits for the cursor and + // manager locks, and on a backend that persists backfill coverage it + // stores the record inline. `within_budget` is safe here: the sweep's + // only await points are those two lock acquisitions, taken before it + // changes anything, and everything after them (the store included) + // runs to completion without yielding, so a drop can only land before + // the pass begins. Skipping it costs a scan, never a payment: the + // contact stays uncovered, and the first sweep after SPV starts + // rewinds to its funding height instead. + if drained > 0 { + match within_budget( + deadline, + identity_wallet.dashpay().reconcile_dashpay_rescan(), + ) + .await + { + Some(Ok(_)) => {} + Some(Err(e)) => { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + error = %e, + "startup: post-drain DashPay rescan sweep failed" + ); + } + None => { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + "startup: budget spent before the post-drain DashPay rescan sweep; \ + the first sweep after SPV starts will backfill the drained contacts" + ); + } + } + } + Ok(tally.into_outcome(started.elapsed())) } @@ -1404,12 +1445,26 @@ mod tests { std::sync::Arc>, WalletId, ) { + manager_with_queued_contact_crypto_on(std::sync::Arc::new( + crate::test_support::NoopTestPersister, + )) + .await + } + + /// [`manager_with_queued_contact_crypto`] over a caller-supplied backend. + async fn manager_with_queued_contact_crypto_on

( + backend: std::sync::Arc

, + ) -> (std::sync::Arc>, WalletId) + where + P: crate::changeset::PlatformWalletPersistence + 'static, + { use crate::changeset::{ upsert_pending_contact_crypto, PendingContactCrypto, PendingContactCryptoOp, }; use crate::wallet::persister::{NoPlatformPersistence, WalletPersister}; - let (manager, wallet_id) = crate::test_support::test_platform_wallet_manager().await; + let (manager, wallet_id) = + crate::test_support::test_platform_wallet_manager_with(backend).await; let persister = WalletPersister::new(wallet_id, std::sync::Arc::new(NoPlatformPersistence)); let mut wm = manager.wallet_manager.write().await; @@ -1543,6 +1598,307 @@ mod tests { ); } + /// The drain creates the receival accounts AFTER the DashPay sync has + /// already run its rescan sweep, so that sweep saw none of them. Startup + /// therefore runs the sweep once more once the drain has produced + /// something. This pins the consequence: the freshly drained contact is + /// marked covered while `synced_height` is still 0, so when the full + /// historical scan later advances past its funding height the recurring + /// sweep does not rewind mid-session to backfill a contact the scan had + /// already covered. + #[tokio::test] + async fn the_post_drain_sweep_marks_the_drained_contact_covered() { + use crate::wallet::identity::network::SeedCryptoProvider; + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let owner = Identifier::from([1u8; 32]); + let contact = Identifier::from([2u8; 32]); + let (manager, wallet_id) = manager_with_queued_contact_crypto().await; + establish_queued_contact(&manager, wallet_id).await; + let owning = + SeedCryptoProvider::from_seed(seed_for(OWNING_MNEMONIC), key_wallet::Network::Testnet); + + let outcome = manager + .start_wallet_subsystems( + &wallet_id, + None, + Some(&owning), + None::<&UnusedSigner>, + WalletStartupOptions::default(), + ) + .await + .expect("bring-up reports rather than raises"); + assert_eq!(outcome.contact_accounts_drained, 1); + + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("wallet info"); + assert_eq!( + info.core_wallet.synced_height(), + 0, + "bring-up runs before SPV starts; the sweep must not have rewound anything" + ); + assert!( + info.identity_manager + .managed_identity(&owner) + .expect("managed identity") + .dashpay() + .rescan_triggered + .contains(&contact), + "the post-drain sweep must mark the drained contact covered by the coming scan" + ); + } + + // Advance the scan past the contact's funding height. The mark must + // hold: no rewind, because the full scan already covered it. + { + let mut wm = manager.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("wallet info"); + info.core_wallet.update_synced_height(200); + } + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("sweep"), + None, + "a contact covered by the full historical scan must never trigger a rewind" + ); + let wm = manager.wallet_manager.read().await; + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("wallet info") + .core_wallet + .synced_height(), + 200 + ); + } + + /// Give the queued contact both requests, ours and its reply (funded at + /// 100), so the pair auto-establishes the relationship. The rescan sweep + /// only handles an ESTABLISHED contact, from its request pair's height. + async fn establish_queued_contact

( + manager: &crate::PlatformWalletManager

, + wallet_id: WalletId, + ) where + P: crate::changeset::PlatformWalletPersistence + 'static, + { + use crate::wallet::identity::ContactRequest; + use crate::wallet::persister::{NoPlatformPersistence, WalletPersister}; + + let owner = Identifier::from([1u8; 32]); + let contact = Identifier::from([2u8; 32]); + let persister = WalletPersister::new(wallet_id, std::sync::Arc::new(NoPlatformPersistence)); + let mut wm = manager.wallet_manager.write().await; + let managed = wm + .get_wallet_info_mut(&wallet_id) + .expect("wallet info") + .identity_manager + .managed_identity_mut(&owner) + .expect("managed identity"); + managed + .add_sent_contact_request( + ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0), + &persister, + ) + .expect("sent request persists"); + managed + .add_incoming_contact_request( + ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 100, 0), + &persister, + ) + .expect("incoming request persists"); + assert!( + managed + .dashpay() + .established_contacts() + .contains_key(&contact), + "the request pair establishes the contact the sweep handles" + ); + } + + /// A backend that persists backfill coverage, so the post-drain sweep + /// stores inline rather than staying in memory: inline commits, atomic + /// rounds and account writers, plus the backfill slot. Records the + /// coverage each round carried, and can hold the drain's account round + /// past the startup deadline — a slow host transaction. + #[derive(Default)] + struct DurableStartupBackend { + /// Coverage carried by each stored round that had a backfill record. + backfill_rounds: std::sync::Mutex>, + /// Whether a round carrying an account registration was stored. + account_round_stored: std::sync::atomic::AtomicBool, + /// When set, the next account round blocks this long before + /// committing. + stall_account_round: std::sync::Mutex>, + } + + impl crate::changeset::PlatformWalletPersistence for DurableStartupBackend { + fn store_commits_inline(&self) -> bool { + true + } + + fn stores_dashpay_backfill(&self) -> bool { + true + } + + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { + use crate::changeset::PersistenceCapabilities as C; + C::ATOMIC_CHANGESETS.union(C::ACCOUNT_ADDRESS_POOLS) + } + + fn store( + &self, + _wallet_id: WalletId, + changeset: crate::changeset::PlatformWalletChangeSet, + ) -> Result<(), crate::changeset::PersistenceError> { + if !changeset.account_registrations.is_empty() { + if let Some(stall) = self.stall_account_round.lock().unwrap().take() { + std::thread::sleep(stall); + } + self.account_round_stored + .store(true, std::sync::atomic::Ordering::SeqCst); + } + if let Some(record) = changeset.dashpay_backfill { + self.backfill_rounds.lock().unwrap().push(record); + } + Ok(()) + } + + fn flush(&self, _wallet_id: WalletId) -> Result<(), crate::changeset::PersistenceError> { + Ok(()) + } + + fn load( + &self, + ) -> Result + { + Ok(crate::changeset::ClientStartState::default()) + } + } + + fn rounds_covering(backend: &DurableStartupBackend, contact: Identifier) -> usize { + backend + .backfill_rounds + .lock() + .unwrap() + .iter() + .filter(|record| { + record + .covered() + .iter() + .any(|entry| entry.contact == contact) + }) + .count() + } + + /// On a backend that persists coverage, the post-drain sweep is a real + /// store, not a memory-only pass: the drained contact reaches the host as + /// covered on the same bring-up. + #[tokio::test] + async fn the_post_drain_sweep_stores_coverage_on_a_durable_backend() { + use crate::wallet::identity::network::SeedCryptoProvider; + + let contact = Identifier::from([2u8; 32]); + let backend = std::sync::Arc::new(DurableStartupBackend::default()); + let (manager, wallet_id) = manager_with_queued_contact_crypto_on(backend.clone()).await; + establish_queued_contact(&manager, wallet_id).await; + let owning = + SeedCryptoProvider::from_seed(seed_for(OWNING_MNEMONIC), key_wallet::Network::Testnet); + + let outcome = manager + .start_wallet_subsystems( + &wallet_id, + None, + Some(&owning), + None::<&UnusedSigner>, + WalletStartupOptions::default(), + ) + .await + .expect("bring-up reports rather than raises"); + + assert_eq!(outcome.contact_accounts_drained, 1); + assert_eq!( + rounds_covering(&backend, contact), + 1, + "the post-drain sweep must store the drained contact's coverage" + ); + } + + /// The post-drain sweep stores inline on a durable backend, so it must + /// respect the startup budget like every other step that can block. A + /// drain whose last host write ran past the deadline still completes and + /// is counted, but no further store is started: bring-up returns, and the + /// contact is left for the first sweep after SPV starts. + #[tokio::test] + async fn the_post_drain_sweep_starts_no_store_once_the_budget_is_spent() { + use crate::wallet::identity::network::SeedCryptoProvider; + + let owner = Identifier::from([1u8; 32]); + let contact = Identifier::from([2u8; 32]); + let budget = Duration::from_secs(1); + let backend = std::sync::Arc::new(DurableStartupBackend::default()); + let (manager, wallet_id) = manager_with_queued_contact_crypto_on(backend.clone()).await; + establish_queued_contact(&manager, wallet_id).await; + *backend.stall_account_round.lock().unwrap() = Some(budget + Duration::from_millis(500)); + let owning = + SeedCryptoProvider::from_seed(seed_for(OWNING_MNEMONIC), key_wallet::Network::Testnet); + + let outcome = manager + .start_wallet_subsystems( + &wallet_id, + None, + Some(&owning), + None::<&UnusedSigner>, + WalletStartupOptions { + budget, + ..WalletStartupOptions::default() + }, + ) + .await + .expect("bring-up reports rather than raises"); + + assert!( + backend + .account_round_stored + .load(std::sync::atomic::Ordering::SeqCst), + "the drain's account round is the write held past the deadline" + ); + assert_eq!(outcome.contact_accounts_drained, 1); + assert_eq!( + rounds_covering(&backend, contact), + 0, + "no coverage store may start once the budget is spent" + ); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + { + let wm = manager.wallet_manager.read().await; + assert!( + !wm.get_wallet_info(&wallet_id) + .expect("wallet info") + .identity_manager + .managed_identity(&owner) + .expect("managed identity") + .dashpay() + .rescan_triggered + .contains(&contact), + "a skipped sweep leaves the contact unhandled" + ); + } + + // The next sweep picks it up. + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("sweep"); + assert_eq!(rounds_covering(&backend, contact), 1); + } + /// The gate is paid for only when there is something to protect. An empty /// queue means the drain would derive nothing, so no key material is /// resolved — which is what keeps this affordable on a warm launch. @@ -1842,6 +2198,7 @@ mod tests { identity_manager: crate::changeset::IdentityManagerStartState::default(), unused_asset_locks: std::collections::BTreeMap::new(), unconfirmed_outgoing_txs: Vec::new(), + dashpay_backfill: Default::default(), }, ); Ok(crate::changeset::ClientStartState { diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index bc211ce76cc..9366ca45d7d 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -370,6 +370,8 @@ impl PlatformWalletManager

{ identity_manager: crate::wallet::identity::IdentityManager::new(), tracked_asset_locks: std::collections::BTreeMap::new(), dpns_name_states: std::collections::BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; wallet.downgrade_to_external_signable(); @@ -412,12 +414,37 @@ impl PlatformWalletManager

{ // than sliced here: the authoritative id is the one that call returns, // and it is deliberately not assumed equal to `registration_wallet_id` // (see the divergence branch below). + // + // The same read also yields what the host holds for every persisted + // wallet: a registration that recreates a wallet the host already has + // must measure durable writes against the host's cursor, not the one + // it just built, and keeps the host's DashPay backfill record for the + // receival accounts it holds (see the seed below). let load_persister: Arc = Arc::clone(&self.persister) as _; - let mut persisted_platform_addresses = + let (mut persisted_platform_addresses, mut host_wallets) = match super::run_blocking_load(move || load_persister.load()).await { Ok(crate::changeset::ClientStartState { - platform_addresses, .. - }) => platform_addresses, + platform_addresses, + wallets, + .. + }) => ( + platform_addresses, + wallets + .into_iter() + .map(|(id, state)| { + ( + id, + ( + state.wallet_info.metadata.synced_height, + state.dashpay_backfill, + ), + ) + }) + .collect::>(), + ), Err(e) => { tracing::error!( wallet_id = %hex::encode(registration_wallet_id), @@ -436,16 +463,111 @@ impl PlatformWalletManager

{ // treat re-registering an existing wallet as a benign no-op // instead of substring-matching the error text. Everything else // stays `WalletCreation`. + // + // The durable cursor is seeded in the same scope as the insert, under + // the cursor lock taken first (its order: cursor lock, then manager). + // The insert makes the wallet visible to the scanner and the event + // adapter; seeding after it would let an adapter commit or a + // reconcile record a newer durable height first, only for the seed to + // overwrite it with the creation one. + // + // A registration can recreate a wallet the host already holds (the + // same seed added again on a fresh manager). The host keeps that + // row's cursor — registration writes no core cursor, and a host + // preserves `syncedHeight` on the metadata upsert — so the seed is + // the host's cursor, not the freshly built one. The snapshot above + // was read before this lock, and a same-id predecessor still + // registered then may have had a commit accepted since — or a whole + // same-id registration may have come and gone, creating the row the + // snapshot did not see. The entry those writes left in + // `DurableCursors` is what the host holds now, so it wins over the + // snapshot, row or no row. When the built cursor is lower than the + // host's, the wallet is in effect reset in memory only: that reset is + // owed to the host like any other, in this wallet's inherited epoch, + // and the next DashPay backfill record round carries it, so coverage + // never reaches disk beside the host's higher cursor + // (dashpay/platform#4302 review). + // + // The host's record is kept only for the receival accounts the + // recreated wallet actually holds — none, when it is built from the + // seed. Coverage for an account that is absent while this wallet + // scans is not coverage: the scan does not watch the account's + // scripts, and the account-generation guard only stops batches still + // in flight, not an advance already committed. The host still holds + // the old record and the account rows, and would restore both beside + // this wallet's advanced cursor after a crash — so the filtered + // record is stored BEFORE the wallet is published to the scanner. + // Those accounts re-arm when they are registered, at the cost of one + // more backfill. + // + // Whether the snapshot held anything to prune is not the whole test, + // because the snapshot is older than the lock: a same-id registration + // can have been published after it, established a receival account, + // stored that account's coverage and been removed again, all before + // this one publishes. Nothing in this snapshot shows that coverage. + // An entry in `DurableCursors` is what tells: every publication leaves + // one and removal keeps it. So with an entry present the host's + // record is replaced with the one this wallet carries, whatever the + // snapshot showed (`settle_published_backfill`). The duplicate check + // runs first, so a wallet still registered under this id never has + // its record overwritten; every publisher holds the cursor lock, so + // none can appear between that check and the insert. + let created_cursor = platform_info.core_wallet.metadata.synced_height; + let (snapshot_cursor, mut host_backfill) = + match host_wallets.remove(®istration_wallet_id) { + Some((cursor, record)) => (Some(cursor), record), + None => (None, Default::default()), + }; + let dropped = super::retain_coverage_for_held_accounts( + &mut host_backfill, + &platform_info.core_wallet, + ); let wallet_id = { + let mut durable_cursors = self.durable_cursors.lock().await; + if self + .wallet_manager + .read() + .await + .get_wallet_info(®istration_wallet_id) + .is_some() + { + return Err(already_registered(registration_wallet_id)); + } + let host_backfill = self.settle_published_backfill( + &mut durable_cursors, + registration_wallet_id, + host_backfill, + dropped, + created_cursor, + snapshot_cursor.unwrap_or(created_cursor), + )?; let mut wm = self.wallet_manager.write().await; - wm.insert_wallet(wallet, platform_info) + let wallet_id = wm + .insert_wallet(wallet, platform_info) .map_err(|e| match e { key_wallet_manager::WalletError::WalletExists(id) => already_registered(id), other => PlatformWalletError::WalletCreation(format!( "Failed to register wallet in WalletManager: {}", other )), - })? + })?; + let host_cursor = durable_cursors + .get(&wallet_id) + .map(|entry| entry.height) + .or(snapshot_cursor) + .unwrap_or(created_cursor); + self.inherit_rewind_barrier(&mut wm, &wallet_id); + if let Some(info) = wm.get_wallet_info_mut(&wallet_id) { + info.dashpay_backfill = host_backfill; + if created_cursor < host_cursor { + let epoch = info.rewind_barrier.epoch(); + info.dashpay_backfill.owe_cursor(created_cursor, epoch); + } + } + durable_cursors + .entry(wallet_id) + .or_insert(crate::changeset::DurableCursor::at(host_cursor)); + wallet_id }; // `insert_wallet` recomputes the id from the (now external-signable) @@ -543,7 +665,7 @@ impl PlatformWalletManager

{ "failed to persist wallet registration changeset" ); let mut wm = self.wallet_manager.write().await; - if let Err(remove_err) = wm.remove_wallet(&wallet_id) { + if let Err(remove_err) = self.remove_from_wallet_manager(&mut wm, &wallet_id) { tracing::warn!( wallet_id = %hex::encode(wallet_id), error = %remove_err, @@ -567,6 +689,8 @@ impl PlatformWalletManager

{ Arc::clone(&self.lock_notify), persister_dyn, broadcaster, + Arc::clone(&self.sync_fault), + Arc::clone(&self.durable_cursors), ); // Restore the platform-address provider from the slice read above — @@ -591,7 +715,7 @@ impl PlatformWalletManager

{ "failed to restore persisted platform-address state" ); let mut wm = self.wallet_manager.write().await; - if let Err(remove_err) = wm.remove_wallet(&wallet_id) { + if let Err(remove_err) = self.remove_from_wallet_manager(&mut wm, &wallet_id) { tracing::warn!( wallet_id = %hex::encode(wallet_id), error = %remove_err, @@ -877,7 +1001,7 @@ impl PlatformWalletManager

{ their identity-sync rows are left for the next launch to reconcile" ); } - if let Err(e) = wm.remove_wallet(wallet_id) { + if let Err(e) = self.remove_from_wallet_manager(&mut wm, wallet_id) { tracing::warn!( wallet_id = %hex::encode(wallet_id), error = %e, @@ -1238,6 +1362,155 @@ mod register_wallet_duplicate_tests { ); } + /// Removing a wallet keeps its rewind barrier's state, and a same-id + /// registration inherits it in the critical section that publishes it: + /// advances the removed wallet emitted cannot be stored against its + /// successor, and the successor's account generation starts past the + /// predecessor's, so no filter batch scanned against the predecessor's + /// account set is certified for it (dashpay/platform#4302 review). + #[tokio::test] + async fn a_same_id_registration_inherits_the_removed_wallets_barrier() { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let manager = make_manager(); + let network = Network::Testnet; + let mnemonic = Mnemonic::from_phrase(TEST_MNEMONIC).expect("valid test mnemonic"); + let seed_bytes = mnemonic.to_seed(""); + let create = |manager: Arc>| async move { + manager + .create_wallet_from_seed_bytes( + network, + &seed_bytes, + WalletAccountCreationOptions::Default, + Some(0), + ) + .await + }; + let wallet_id = create(Arc::clone(&manager)) + .await + .expect("first create") + .wallet_id(); + let predecessor_generation = { + // Three advances emitted and not yet drained by the adapter, and + // a receival account registered. + let mut wm = manager.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + for _ in 0..3 { + info.rewind_barrier.note_emitted(); + } + info.rewind_barrier.arm(); + info.rewind_barrier.note_account_registered(); + WalletInfoInterface::account_generation(info) + }; + manager.remove_wallet(&wallet_id).await.expect("remove"); + assert_eq!( + manager.durable_cursors.retired()[&wallet_id].in_flight, + 3, + "removal retires the barrier under the id" + ); + + create(Arc::clone(&manager)).await.expect("second create"); + assert!( + manager.durable_cursors.retired().get(&wallet_id).is_none(), + "the successor took the retired state over" + ); + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("published"); + let retired = info.rewind_barrier.retire(0); + assert_eq!( + retired.in_flight, 3, + "the predecessor's queued advances are carried" + ); + assert!(retired.epoch > 1, "the epoch moves past the predecessor's"); + assert!( + WalletInfoInterface::account_generation(info) > predecessor_generation, + "the successor's account generation starts past the predecessor's" + ); + } + + /// The durable cursor is seeded in the critical section that publishes + /// the wallet. Published first and seeded after, an adapter commit or a + /// reconcile could record a newer durable height in between and the seed + /// would overwrite it with the creation one. While the cursor lock is + /// held the registration must not publish the wallet at all, and once it + /// does, an entry an earlier same-id registration left is what the host + /// holds now, so it is kept and the lower created cursor is owed to it. + #[tokio::test] + async fn a_wallet_is_published_and_its_durable_cursor_seeded_in_one_step() { + let manager = make_manager(); + let network = Network::Testnet; + let mnemonic = Mnemonic::from_phrase(TEST_MNEMONIC).expect("valid test mnemonic"); + let seed_bytes = mnemonic.to_seed(""); + let create = |manager: Arc>| async move { + manager + .create_wallet_from_seed_bytes( + network, + &seed_bytes, + WalletAccountCreationOptions::Default, + Some(0), + ) + .await + }; + + let wallet_id = create(Arc::clone(&manager)) + .await + .expect("first create") + .wallet_id(); + manager.remove_wallet(&wallet_id).await.expect("remove"); + + let mut gate = manager.durable_cursors.lock().await; + // What a removed registration might leave behind. + gate.insert(wallet_id, crate::changeset::DurableCursor::at(777)); + let registering = tokio::spawn(create(Arc::clone(&manager))); + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + assert!( + manager + .wallet_manager + .read() + .await + .get_wallet_info(&wallet_id) + .is_none(), + "no wallet may become visible while the durable-cursor lock is held" + ); + drop(gate); + + registering.await.expect("join").expect("second create"); + let created_cursor = { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + manager + .wallet_manager + .read() + .await + .get_wallet_info(&wallet_id) + .expect("published") + .core_wallet + .synced_height() + }; + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .map(|cursor| cursor.height), + Some(777), + "the entry the earlier registration left is what the host holds" + ); + assert!(created_cursor < 777); + assert_eq!( + manager + .wallet_manager + .read() + .await + .get_wallet_info(&wallet_id) + .expect("published") + .dashpay_backfill + .unpersisted_cursor, + Some(created_cursor), + "the lower created cursor is owed" + ); + } + /// PENDING-SPEND PROTECTION MUST SURVIVE WALLET RECREATION. /// /// If the in-broadcast fence lived in the `WalletGeneration` itself, it diff --git a/packages/rs-platform-wallet/src/test_support.rs b/packages/rs-platform-wallet/src/test_support.rs index b9446432ee0..bf58b45b29f 100644 --- a/packages/rs-platform-wallet/src/test_support.rs +++ b/packages/rs-platform-wallet/src/test_support.rs @@ -256,6 +256,8 @@ pub async fn funded_wallet_manager_with_outputs( identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); @@ -385,6 +387,8 @@ pub(crate) async fn funded_wallet_manager_dual_standard( identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); let wallet_id = wm.insert_wallet(ctx.wallet, info).expect("insert wallet"); @@ -488,6 +492,8 @@ pub(crate) async fn funded_wallet_manager_with_contact( identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); let wallet_id = wm.insert_wallet(ctx.wallet, info).expect("insert wallet"); @@ -565,6 +571,8 @@ pub(crate) async fn funded_coinjoin_wallet_manager() -> ( identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); @@ -668,6 +676,17 @@ pub async fn test_platform_wallet_manager() -> ( Arc>, WalletId, ) { + test_platform_wallet_manager_with(Arc::new(NoopTestPersister)).await +} + +/// [`test_platform_wallet_manager`] over a caller-supplied persister, for tests +/// whose behaviour depends on what the backend attests. +pub async fn test_platform_wallet_manager_with

( + persister: Arc

, +) -> (Arc>, WalletId) +where + P: crate::changeset::PlatformWalletPersistence + 'static, +{ use key_wallet::mnemonic::Mnemonic; use key_wallet::wallet::initialization::WalletAccountCreationOptions; @@ -676,7 +695,6 @@ pub async fn test_platform_wallet_manager() -> ( abandon abandon abandon abandon abandon about"; let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); - let persister = Arc::new(NoopTestPersister); let event_handler: Arc = Arc::new(NoopTestEventHandler); let manager = Arc::new(crate::PlatformWalletManager::new( @@ -770,6 +788,8 @@ pub(crate) async fn mnemonic_wallet_manager( identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); diff --git a/packages/rs-platform-wallet/src/wallet/apply.rs b/packages/rs-platform-wallet/src/wallet/apply.rs index 0f4ec4a8c33..c781c901e43 100644 --- a/packages/rs-platform-wallet/src/wallet/apply.rs +++ b/packages/rs-platform-wallet/src/wallet/apply.rs @@ -109,6 +109,10 @@ impl PlatformWalletInfo { invitations: _, dpns_name_states, identity_scan_state, + // The backfill record is persistence-only here: the reconcile + // sweep mutates `PlatformWalletInfo::dashpay_backfill` directly + // and the start-state path restores it at load. No replay hook. + dashpay_backfill: _, // Registration-round metadata / per-account specs / // per-pool snapshots are persistence-only — the // canonical in-memory wallet state is built up at @@ -442,6 +446,8 @@ mod tests { tracked_asset_locks: BTreeMap::new(), observed_input_conflicts: Default::default(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), } } diff --git a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/reconstruction.rs b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/reconstruction.rs index a14417dba53..6736299079d 100644 --- a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/reconstruction.rs +++ b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/reconstruction.rs @@ -664,6 +664,8 @@ mod tests { identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; assert_eq!( funding_account_index(&info, &txid), diff --git a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs index 7e886ea7da6..ec725024579 100644 --- a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs +++ b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs @@ -2524,6 +2524,8 @@ mod tests { identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; let out_point = OutPoint::new(tx.txid(), 0); let lock = TrackedAssetLock { diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs index 1879f18346c..b4f4813facf 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs @@ -819,6 +819,8 @@ mod tests { Arc::new(crate::test_support::NoopTestPersister) as Arc, Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), + Arc::new(std::sync::atomic::AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), )) } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs index 01f8327bd90..35a5e35e3db 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs @@ -1098,11 +1098,16 @@ pub(crate) fn budget_spent(deadline: Option) -> bool { /// A permanently-broken channel is left alone (the sweep never rebuilds /// broken contacts; they heal on a superseding request). The staleness test /// is `external_account_reference != Some(incoming_request.account_reference)`: -/// a mismatch (or a `None` marker from a cold restart that did not carry it) -/// means the persisted, tombstone-less account row rebuilt the rotated-away -/// xpub while the contact already tracks the new reference — so `send_payment` -/// would derive addresses the contact no longer watches until it is rebuilt. -fn external_account_needs_rebuild(contact: &EstablishedContact, has_external: bool) -> bool { +/// a mismatch (or a `None` marker from a cold restart on a host that did not +/// carry it — the in-tree hosts persist it on the established rows, so a +/// healthy account survives a restart without a rebuild) means the persisted, +/// tombstone-less account row rebuilt the rotated-away xpub while the contact +/// already tracks the new reference — so `send_payment` would derive addresses +/// the contact no longer watches until it is rebuilt. +pub(super) fn external_account_needs_rebuild( + contact: &EstablishedContact, + has_external: bool, +) -> bool { has_external && !contact.payment_channel_broken && contact.external_account_reference != Some(contact.incoming_request.account_reference) @@ -1865,7 +1870,7 @@ impl DashPayView<'_, B> { /// permanently broken — the account-building candidates for this /// sweep. Runs under the caller's write guard; performs no /// awaits and no lock re-acquisition. - fn collect_account_build_candidates( + pub(super) fn collect_account_build_candidates( info: &crate::wallet::platform_wallet::PlatformWalletInfo, identity_id: &Identifier, ) -> Vec { @@ -3209,7 +3214,10 @@ impl DashPayView<'_, B> { /// /// - Stamping `external_account_reference` lets the sweep's rotation /// self-heal skip a healthy account (marker matches the tracked - /// reference) and detect a stale one after a restart (F2). + /// reference) and detect a stale one after a restart (F2). On a + /// backend that keeps markers on disk the registration round already + /// bound it, and this only clears the broken flag while that marker is + /// still current; it never stamps one there. /// - Clearing `payment_channel_broken` heals a channel that a prior /// permanent-fault marked broken but that a successful re-register (via /// the drain or a user re-accept) proves is usable again — otherwise the @@ -3257,6 +3265,18 @@ impl DashPayView<'_, B> { } let current_reference = contact.incoming_request.account_reference; let already_current = contact.external_account_reference == Some(current_reference); + // Where markers are kept on disk, registration is their only writer: + // it binds the marker in the account's own round, to the request the + // account was built from. A completion arriving late — after the + // account was torn down and rebuilt, or from a request that reused + // the same ciphertext under another reference — could otherwise + // stamp the live reference beside an obsolete durable account, and a + // restart would trust the pairing. So it only heals the broken flag, + // and only while the marker registration bound is still the live one + // (dashpay/platform#4302 review). + if self.persister.persists_outbound_account_markers() && !already_current { + return; + } if already_current && !contact.payment_channel_broken { return; } @@ -3376,7 +3396,7 @@ impl DashPayView<'_, B> { /// One established contact that needs its DashPay accounts (re)built /// during a sync sweep. Collected under the write guard, consumed /// after it is dropped. -struct AccountBuildCandidate { +pub(super) struct AccountBuildCandidate { /// The counterparty identity. contact_id: Identifier, /// The counterparty's 96-byte encrypted xpub (from their incoming @@ -4200,6 +4220,8 @@ mod sweep_tests { tracked_asset_locks: BTreeMap::new(), observed_input_conflicts: Default::default(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), } } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs b/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs index a19288f4128..46e6eb487c0 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs @@ -87,6 +87,10 @@ pub enum ExternalAccountRegistration { /// An account row already existed for the `(index, user, friend)` key; /// nothing was decrypted or replaced. AlreadyExisted, + /// The contact's live incoming request no longer carries the supplied + /// encrypted xpub — a rotation landed after the caller read it. Nothing + /// was persisted or inserted; the sweep rebuilds from the live request. + Superseded, } impl RegisterExternalError { @@ -247,6 +251,10 @@ impl DashPayView<'_, B> { "Failed to register contact account: {e}" )) })?; + // A filter batch in flight right now was scanned without this + // account's scripts: bump the account generation so the pipeline does + // not certify it, and the range is rescanned with the account watched. + info.rewind_barrier.note_account_registered(); tracing::info!( our_identity = %our_identity_id, @@ -402,16 +410,21 @@ impl DashPayView<'_, B> { /// tell a real (re)build from an already-existed no-op — only the former /// may stamp the rotation self-heal marker (see the enum docs). /// - /// **Never wrap this call in a timeout.** Unlike its sibling - /// [`Self::register_contact_account`], which persists inside the write lock - /// it already holds, this one persists *before* acquiring the lock — so - /// there is a genuine `.await` between the durable write and the in-memory - /// inserts. A future dropped in that window leaves an account on disk that - /// no in-memory collection knows about for the rest of the process's life; - /// a real crash reloads it from disk, an in-process cancellation does not. - /// (The next drain does re-register it idempotently, so this is a landmine - /// rather than data loss — but the asymmetry with the sibling is not - /// something to rediscover by analogy.) + /// The absence check, the durable write and the in-memory inserts run + /// under ONE wallet-manager write guard, so registrations of the same + /// account are serialized: a registration that decrypted a payload and + /// then lost the race to another one finds the account present and + /// returns [`ExternalAccountRegistration::AlreadyExisted`], instead of + /// overwriting the newer account's durable xpub and pool. Under the same + /// guard the payload is checked against the contact's LIVE incoming + /// request, and a stale one returns + /// [`ExternalAccountRegistration::Superseded`] without writing anything. + /// When the contact is established and the backend's rounds are inline + /// and atomic, the account and the contact's outbound-account marker are + /// written in the same store round, so the durable marker always names + /// the request the durable account was built from. Any other backend + /// gets no marker from this round; the caller stamps it once the account + /// is stored (dashpay/platform#4302 review). pub async fn register_external_contact_account( &self, our_identity_id: &Identifier, @@ -538,22 +551,8 @@ impl DashPayView<'_, B> { // typed `insert_funds` API after the upstream split. let managed = key_wallet::managed_account::ManagedCoreFundsAccount::from_account(&account); - // Persist the registration BEFORE the in-memory inserts (same - // rationale as `register_contact_account`): without this round - // the account vanishes on relaunch and `send_payment` loses its - // xpub + derived-address state until the next sweep rebuilds it. - self.persister - .store(dashpay_account_registration_changeset( - account_type, - contact_xpub, - &managed, - )) - .map_err(|e| { - Transient(PlatformWalletError::InvalidIdentityData(format!( - "Failed to persist external contact account registration: {e}" - ))) - })?; - + #[cfg(test)] + registration_rendezvous::wait(&contact_identity_id).await; let mut wm = self.wallet_manager.write().await; let (wallet, info) = wm .get_wallet_mut_and_info_mut(&self.wallet_id) @@ -563,6 +562,81 @@ impl DashPayView<'_, B> { ))) })?; + // Re-check under the guard that serializes registrations: the early + // check above ran under a read guard that was released for the + // decrypt, and another registration may have built the account since. + let key = key_wallet::account::account_collection::DashpayAccountKey { + index: account_index, + user_identity_id: our_identity_id.to_buffer(), + friend_identity_id: contact_identity_id.to_buffer(), + }; + if info + .core_wallet + .accounts + .dashpay_external_accounts + .contains_key(&key) + { + return Ok(ExternalAccountRegistration::AlreadyExisted); + } + + // The payload must still be the contact's live request. The durable + // marker is bound to it in the same round as the account only where + // the backend keeps markers durably at all + // (`persists_outbound_account_markers`: an inline, atomic round that + // writes accounts and pools). A backend without that guarantee could + // reject or drop the account and pool yet keep the contact row, + // leaving the live request's marker beside the previous account — + // which a restart would trust. There the marker stays in memory only: + // the caller stamps it once this round succeeded, and every contact + // row written to that backend omits it (dashpay/platform#4302 review). + let binds_marker = self.persister.persists_outbound_account_markers(); + let bound_contact = match info + .identity_manager + .managed_identity(our_identity_id) + .and_then(|managed| managed.established_contact(&contact_identity_id)) + { + Some(contact) + if contact.incoming_request.encrypted_public_key != contact_encrypted_xpub => + { + tracing::warn!( + our_identity = %our_identity_id, + contact = %contact_identity_id, + "external account payload superseded by a rotation before registration; \ + nothing written, the sweep rebuilds from the live request" + ); + return Ok(ExternalAccountRegistration::Superseded); + } + Some(contact) if binds_marker => { + let mut bound = contact.clone(); + bound.external_account_reference = Some(contact.incoming_request.account_reference); + Some(bound) + } + Some(_) | None => None, + }; + + // Persist the registration BEFORE the in-memory inserts (same + // rationale as `register_contact_account`): without this round + // the account vanishes on relaunch and `send_payment` loses its + // xpub + derived-address state until the next sweep rebuilds it. + let mut changeset = + dashpay_account_registration_changeset(account_type, contact_xpub, &managed); + if let Some(bound) = &bound_contact { + let mut contacts = crate::changeset::ContactChangeSet::default(); + contacts.established.insert( + crate::changeset::SentContactRequestKey { + owner_id: *our_identity_id, + recipient_id: contact_identity_id, + }, + bound.clone(), + ); + changeset.contacts = Some(contacts); + } + self.persister.store(changeset).map_err(|e| { + Transient(PlatformWalletError::InvalidIdentityData(format!( + "Failed to persist external contact account registration: {e}" + ))) + })?; + // (a) Insert Account into the immutable wallet account collection so the // xpub is accessible by `send_payment`. wallet @@ -585,6 +659,16 @@ impl DashPayView<'_, B> { ))) })?; + if let Some(bound) = bound_contact { + if let Some(contact) = info + .identity_manager + .managed_identity_mut(our_identity_id) + .and_then(|managed| managed.established_contact_mut(&contact_identity_id)) + { + contact.external_account_reference = bound.external_account_reference; + } + } + tracing::info!( our_identity = %our_identity_id, contact = %contact_identity_id, @@ -594,3 +678,40 @@ impl DashPayView<'_, B> { Ok(ExternalAccountRegistration::Built) } } + +/// Test-only rendezvous between [`IdentityWallet::register_external_contact_account`]'s +/// early absence check and its write-guard acquisition, so a test can hold +/// several registrations of one contact at exactly that point and release +/// them together. Keyed by contact id, so registrations of every other +/// contact — and every other test — pass straight through. +#[cfg(test)] +pub(crate) mod registration_rendezvous { + use std::collections::BTreeMap; + use std::sync::{Arc, Mutex}; + + use dpp::prelude::Identifier; + use tokio::sync::Barrier; + + static BARRIERS: Mutex>> = Mutex::new(BTreeMap::new()); + + /// Hold `parties` registrations of `contact` at the rendezvous until all + /// of them have arrived. + pub(crate) fn install(contact: Identifier, parties: usize) { + BARRIERS + .lock() + .unwrap() + .insert(contact, Arc::new(Barrier::new(parties))); + } + + /// Stop holding registrations of `contact`. + pub(crate) fn remove(contact: &Identifier) { + BARRIERS.lock().unwrap().remove(contact); + } + + pub(crate) async fn wait(contact: &Identifier) { + let barrier = BARRIERS.lock().unwrap().get(contact).cloned(); + if let Some(barrier) = barrier { + barrier.wait().await; + } + } +} diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/dpns_marketplace.rs b/packages/rs-platform-wallet/src/wallet/identity/network/dpns_marketplace.rs index 46fab23f519..ed6e607b698 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/dpns_marketplace.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/dpns_marketplace.rs @@ -3916,6 +3916,8 @@ mod tests { wallet_id, asset_locks, persister, + sync_fault: Arc::new(std::sync::atomic::AtomicBool::new(false)), + durable_cursors: crate::changeset::DurableCursors::default(), broadcaster, sdk_writer: Arc::new(super::super::sdk_writer::SdkWriter::new(sdk)), dpns_operation_gate: Arc::new(tokio::sync::Mutex::new(())), diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/identity_handle.rs b/packages/rs-platform-wallet/src/wallet/identity/network/identity_handle.rs index 402b6930737..8f934c5a12c 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/identity_handle.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/identity_handle.rs @@ -312,6 +312,19 @@ pub struct IdentityWallet { pub(crate) asset_locks: Arc>, /// Per-wallet persistence handle for queuing changesets. pub(crate) persister: crate::wallet::persister::WalletPersister, + /// The manager's persistence-fault latch (see + /// [`PlatformWalletManager::sync_fault_detected`](crate::PlatformWalletManager::sync_fault_detected)). + /// While it is set the wallet-event adapter holds the durable + /// `synced_height` back at the last fully persisted height, and a + /// direct cursor write from this side would advance it past rows that + /// never landed. The DashPay rescan reconcile consults it before it + /// stores a backfill record with a lowered cursor. + pub(crate) sync_fault: Arc, + /// The manager's shared record of each wallet's durable sync cursor, + /// also the lock that orders cursor writes between the wallet-event + /// adapter and the DashPay rescan reconcile. See + /// [`DurableCursors`](crate::changeset::DurableCursors). + pub(crate) durable_cursors: crate::changeset::DurableCursors, /// Broadcaster for DashPay payment transactions. Distinct from the /// asset-lock broadcaster — the asset-lock manager is always /// `SpvBroadcaster`-pinned, while this one picks the broadcaster @@ -353,6 +366,8 @@ impl Clone for IdentityWallet { wallet_id: self.wallet_id, asset_locks: Arc::clone(&self.asset_locks), persister: self.persister.clone(), + sync_fault: Arc::clone(&self.sync_fault), + durable_cursors: Arc::clone(&self.durable_cursors), broadcaster: Arc::clone(&self.broadcaster), sdk_writer: Arc::clone(&self.sdk_writer), dpns_operation_gate: Arc::clone(&self.dpns_operation_gate), diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index a5b49b41bc6..e4f688a8e96 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -86,38 +86,99 @@ impl DashPayView<'_, B> { /// `$coreHeightCreatedAt` across established receival contacts that haven't /// been rescanned yet — the filter manager (`dash-spv`) then re-downloads /// nothing it already has, re-matches the now-larger script set, and - /// re-requests the matching blocks. Each contact is recorded in - /// [`DashPayState::rescan_triggered`](crate::wallet::identity::DashPayState) so the recurring sweep does - /// not re-lower the height every pass (which would reset the in-flight - /// backfill and keep it from ever completing). The guard is in-memory, so a - /// relaunch — where `synced_height` is restored at its high-water — safely - /// re-triggers an interrupted backfill. + /// re-requests the matching blocks. + /// + /// # The guard, in memory and on disk + /// + /// Each contact the pass handles is recorded twice. Durably, in the + /// wallet's [`DashPayBackfillRecord`], keyed per contact on the checkpoint + /// it was covered from and written on the same persistence round as the + /// lowered cursor — that record is what decides whether a contact is + /// covered. And in memory, in + /// [`DashPayState::rescan_triggered`](crate::wallet::identity::DashPayState), + /// which only tracks which contacts this process has handled; a mark on + /// its own never counts as coverage, so a contact whose record write + /// failed, or whose checkpoint dropped below the height it was recorded + /// at, is re-armed through the candidate path. + /// + /// The durable half exists because the cursor this pass lowers is itself + /// durable: the host persists every `SyncHeightAdvanced` the engine emits + /// as the rescan climbs, so a relaunch restores `synced_height` *inside* + /// the climb, not at its high-water. With only the in-memory guard, every + /// fresh process rewound again — on a contact-heavy mainnet wallet a + /// re-walk of hundreds of thousands of filters per launch, never finishing + /// (dashpay/platform#4302). Now a contact the record covers at a + /// checkpoint no lower than the one it was covered from is treated as + /// covered: the scan resumes from the restored cursor with the contact's + /// addresses watched, which is exactly where the previous process left it. + /// A contact whose checkpoint has dropped below its recorded height, or + /// one the record never listed (registered since, or by a path that did + /// not record it), is re-armed as before — the record only ever suppresses + /// a rewind it can vouch for. + /// + /// Nothing is marked "complete" at trigger time: the record's extent + /// (`floor`, `rewound_from`) is descriptive, and completion is read off the + /// cursor ([`DashPayBackfillRecord::is_complete`]). An interrupted + /// backfill therefore resumes rather than restarts, and never reads as + /// finished while the cursor still sits below `rewound_from`. /// /// `synced_height` may regress here: that is safe because it is the /// filter-scan checkpoint, decoupled from the monotonic - /// `last_processed_height`, and every persisted sync cursor is monotonic-max - /// guarded, so a transient rewind cannot corrupt state or persist a lower - /// cursor. Only the **receival** account matters — `DashpayExternalAccount` - /// is outbound and never receives. Returns the floor the height was lowered - /// to, or `None`. + /// `last_processed_height`. The pinned engine refuses to certify a batch + /// scanned at the old checkpoint once the cursor sits below it, so an + /// in-flight batch cannot lift the rewound cursor back over the range it + /// never tested with the new addresses. Only the **receival** account + /// matters — `DashpayExternalAccount` is outbound and never receives. + /// Returns the floor the height was lowered to, or `None`. + /// + /// [`DashPayBackfillRecord`]: crate::changeset::DashPayBackfillRecord + /// [`DashPayBackfillRecord::is_complete`]: crate::changeset::DashPayBackfillRecord::is_complete pub async fn reconcile_dashpay_rescan(&self) -> Result, PlatformWalletError> { + use crate::changeset::{CoreChangeSet, PlatformWalletChangeSet}; use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + // The durable-cursor lock first, then the manager (see + // `DurableCursors`): held until this pass's store returns, so no + // adapter commit can interleave between deciding a cursor and storing + // it. + let mut durable_cursors = self.durable_cursors.lock().await; let mut wm = self.wallet_manager.write().await; let Some(info) = wm.get_wallet_info_mut(&self.wallet_id) else { return Ok(None); }; + let durable = durable_cursors.get(&self.wallet_id).copied(); + // A cursor owed since an in-memory lowering is paid once the host + // has accepted a scan advance from the lowering's epoch or later: the + // host then tracks the scan that restarted from the lowered cursor. + // Writing the old height after that would only drag the host back + // below the scan (dashpay/platform#4302 device check). + if info + .dashpay_backfill + .settle_owed_cursor(durable.and_then(|durable| durable.advance_epoch)) + { + tracing::debug!( + wallet_id = %hex::encode(self.wallet_id), + "DashPay rescan: the host has caught up past the owed cursor; dropping the debt" + ); + } + // 0 means "scan from genesis / not yet started": a full historical + // scan is coming, so nothing is backfilled toward it — but every + // candidate is still handled, as forward-covered. Returning early + // instead left contacts the startup drain had just registered + // unrecorded, and the first sweep after the scan had climbed past + // their funding heights rewound mid-session for a range that scan had + // watched them through. No checkpoint is below 0, so the path below + // rewinds nothing here. let synced_height = info.core_wallet.synced_height(); - // 0 means "scan from genesis / not yet started" — already a full - // historical scan, nothing to backfill toward. - if synced_height == 0 { - return Ok(None); - } - // (owner, contact) pairs that have a receival account — we can only - // watch a contact's incoming addresses once its receival account exists. - let receival_pairs: Vec<(Identifier, Identifier)> = info + // (owner, contact, account_index) of every receival account — we can + // only watch a contact's incoming addresses once its receival account + // exists. Coverage is per account, not per pair: a second receiving + // account for a relationship derives its own xpub, and a scan that + // watched the first never tested the second's scripts. A set, because + // every coverage entry is checked against it under both locks. + let receival_accounts: std::collections::BTreeSet<(Identifier, Identifier, u32)> = info .core_wallet .accounts .dashpay_receival_accounts @@ -126,49 +187,113 @@ impl DashPayView<'_, B> { ( Identifier::from(k.user_identity_id), Identifier::from(k.friend_identity_id), + k.index, ) }) .collect(); - // Candidates: established receival contacts not yet rescanned this - // lifetime whose funding height is below our scan tip. The floor is the - // minimum funding height — one rewind covers them all (deeper-funded - // contacts are in the watch set, so the backfill matches them too). The + // Candidates: established receival contacts the durable record does + // not cover at their current checkpoint. The floor is the minimum + // funding height — one rewind covers them all (deeper-funded contacts + // are in the watch set, so the backfill matches them too). The // funding height is `min(outgoing, incoming)` of the pair: the channel - // is payable only once both requests exist, so the earlier of the two is - // the conservative-correct lower bound. + // is payable only once both requests exist, so the earlier of the two + // is the conservative-correct lower bound. + // + // A process-local `rescan_triggered` mark is never taken as coverage + // on its own: the mark may belong to a pass whose record write failed, + // or the contact's checkpoint may since have dropped below the height + // it was recorded at (an older reciprocal request learned later). + // Either way the range the scan never tested with this contact has to + // be rewound to, so an uncovered contact goes through the candidate + // path whether or not it is marked. let mut floor: Option = None; - let mut to_mark: Vec<(Identifier, Identifier)> = Vec::new(); - for (owner, contact) in receival_pairs { + // Receival accounts this pass handles, with the checkpoint each is + // covered from. + let mut to_mark: Vec<(Identifier, Identifier, u32, u32)> = Vec::new(); + // Contacts the durable record vouched for that are not yet marked in + // this process. + let mut restored: Vec<(Identifier, Identifier)> = Vec::new(); + for (owner, contact, account_index) in receival_accounts.iter().copied() { let Some(managed) = info.identity_manager.managed_identity(&owner) else { continue; }; - if managed.dashpay().rescan_triggered.contains(&contact) { - continue; - } let Some(established) = managed.dashpay().established_contacts().get(&contact) else { continue; }; - let funding = established + let checkpoint = established .outgoing_request .core_height_created_at .min(established.incoming_request.core_height_created_at); + // The previous round already rewound for this contact from a + // checkpoint no higher than today's, and the persisted cursor + // tracked that scan with its addresses watched, so nothing below + // the cursor is missing: resume, don't restart. + if info + .dashpay_backfill + .covers(&owner, &contact, account_index, checkpoint) + { + if !managed.dashpay().rescan_triggered.contains(&contact) + && !restored.contains(&(owner, contact)) + { + restored.push((owner, contact)); + } + continue; + } // Contacts funded below the tip need a backfill — their addresses // weren't watched when those blocks were first scanned. Contacts // funded at or after the tip are already covered by the ongoing // forward scan (their addresses are watched from establishment). - // EITHER way the contact is now handled, so mark it: once the + // EITHER way the contact is now handled, so record it: once the // forward pointer later climbs past a still-forward-covered // contact's funding height, the recurring sweep must NOT then // rewind to it and redundantly re-scan an already-scanned range. - if funding < synced_height { - floor = Some(floor.map_or(funding, |cur| cur.min(funding))); + if checkpoint < synced_height { + floor = Some(floor.map_or(checkpoint, |cur| cur.min(checkpoint))); + } + to_mark.push((owner, contact, account_index, checkpoint)); + } + + let restored_count = restored.len(); + for (owner, contact) in restored { + if let Some(managed) = info.identity_manager.managed_identity_mut(&owner) { + managed.dashpay_rescan_triggered_mut().insert(contact); } - to_mark.push((owner, contact)); + } + if restored_count > 0 { + tracing::info!( + wallet_id = %hex::encode(self.wallet_id), + contacts = restored_count, + synced_height, + floor = info.dashpay_backfill.floor, + rewound_from = info.dashpay_backfill.rewound_from, + pending = info.dashpay_backfill.is_pending(synced_height), + "DashPay rescan: persisted backfill record covers these contacts; resuming from the \ + restored cursor instead of rewinding again" + ); } + // No contact needs handling — but the record may still need a round: + // coverage for a receival account that no longer exists must go (its + // scripts stop being watched, so the coverage stops being true), and + // a cursor or extent owed by an earlier reset or failed round must + // reach the host before the process can die with it. Either one runs + // a record-only round below; otherwise there is nothing to store. if to_mark.is_empty() { - return Ok(None); + let record = &info.dashpay_backfill; + let prunes = record.covered().iter().any(|entry| { + !receival_accounts.contains(&(entry.owner, entry.contact, entry.account_index)) + }); + let owes = record.unpersisted_cursor.is_some() || record.unpersisted_extent.is_some(); + // While the fault latch is up, or after the backend refused a + // round, nothing is stored (see below); the debt waits for the + // next launch. A prune still runs, in memory: the session's + // coverage must stop vouching for an account that is gone. + let unstorable = + self.sync_fault.load(std::sync::atomic::Ordering::Relaxed) || record.store_refused; + if !(prunes || owes) || (unstorable && !prunes) { + return Ok(None); + } } // Lower the filter-scan checkpoint only when a contact is funded below @@ -177,21 +302,183 @@ impl DashPayView<'_, B> { // own header/birth floor, so no double-clamp here. if let Some(floor) = floor { info.core_wallet.update_synced_height(floor); + // Advances the engine emitted before this rewind must not be + // stored after it — see `RewindBarrier`. + info.rewind_barrier.arm(); } let triggered = to_mark.len(); - for (owner, contact) in to_mark { - if let Some(managed) = info.identity_manager.managed_identity_mut(&owner) { - managed.dashpay_rescan_triggered_mut().insert(contact); - } - } if let Some(floor) = floor { tracing::info!( wallet_id = %hex::encode(self.wallet_id), floor, + rewound_from = synced_height, contacts = triggered, "DashPay rescan: lowered SPV synced_height to backfill historical contact payments" ); } + + // Durable half of the guard. Every contact handled this pass is covered + // from its own checkpoint: a rewound one because the scan restarts at + // `floor <= checkpoint`, a forward-covered one because its addresses + // are watched from `synced_height <= checkpoint` on. Entries for + // accounts that no longer exist are dropped so the record tracks the + // live receival set. + // + // The record and the cursor it vouches for ride ONE round, and the + // record reaches memory only once that round is on disk: + // - the round carries the floor this pass rewound to, or failing that + // a cursor an earlier failed round still owes, so coverage is never + // stored beside a durable cursor the in-memory rewind left behind; + // - the pass's record is staged on a copy and installed, with the + // pass's marks, only when the store returns `Ok`. The cursor and + // extent the round owes are recorded on the live record BEFORE the + // store runs, so a store that fails transiently — or panics, which + // unwinds through here and releases the manager and cursor locks — + // leaves the previous coverage plus the debt for the next pass to + // carry, never coverage that did not reach disk; + // - a store the backend refuses outright (a non-transient failure) + // would be refused again, and retrying it would leave every later + // sweep rewinding to the same contacts. The record is marked + // `store_refused` and handled like the latched fault below for the + // rest of the session; + // - while the manager's persistence fault is latched the adapter is + // holding the durable cursor back at the last fully persisted + // height, and a cursor written from here could advance it past rows + // that never landed — so nothing is stored at all: the in-memory + // rewind still runs the rescan, and the next launch re-runs it, + // which is the pre-record behaviour. The latch is never cleared, so + // the pass's coverage is still installed in memory for the rest of + // the session, or every later sweep would rewind to the same + // contacts again. It keeps the debt: should anything store the + // record later, the owed cursor rides with it; + // - a backend that cannot hold the record durably gets none at all + // (`persists_backfill_coverage`): one whose `store` only buffers + // commits at a later `flush` that can still drop the round, and one + // that is not atomic can keep the coverage after rejecting the + // cursor it vouches for. The pass then runs in memory only, exactly + // the pre-record guard. + let mut staged = info.dashpay_backfill.clone(); + staged.retain(|owner, contact, account_index| { + receival_accounts.contains(&(*owner, *contact, account_index)) + }); + // This pass's extent, widened by one a failed earlier round still owes. + let extent = match ( + floor.map(|floor| (floor, synced_height)), + staged.unpersisted_extent.take(), + ) { + (Some((f1, r1)), Some((f2, r2))) => Some((f1.min(f2), r1.max(r2))), + (this_pass, owed) => this_pass.or(owed), + }; + match extent { + Some((extent_floor, rewound_from)) => { + staged.record_pass(rewound_from, Some(extent_floor), to_mark.iter().copied()) + } + None => staged.record_pass(synced_height, None, to_mark.iter().copied()), + }; + let owed = staged.unpersisted_cursor.take(); + staged.unpersisted_cursor_epoch = None; + let needed_cursor = match (floor, owed) { + (Some(floor), Some(owed)) => Some(floor.min(owed)), + (Some(floor), None) => Some(floor), + (None, owed) => owed, + }; + // Never write a cursor at or above the one the host already holds: + // coverage is safe beside any lower durable cursor, and writing a + // higher one would claim rows the adapter has not persisted yet. + let cursor_to_write = + needed_cursor.filter(|cursor| durable.is_none_or(|durable| *cursor < durable.height)); + let cursor_now = floor.unwrap_or(synced_height); + // The debt, recorded before the store so no way out of it loses it. + info.dashpay_backfill.unpersisted_cursor = + Some(needed_cursor.map_or(cursor_now, |cursor| cursor.min(cursor_now))); + info.dashpay_backfill.unpersisted_cursor_epoch = Some(info.rewind_barrier.epoch()); + info.dashpay_backfill.unpersisted_extent = extent; + /// What this pass's staged record becomes. + enum Settled { + /// Installed: stored durably, or a backend that keeps it in + /// memory only (and so carries no debt). + Installed { durable: bool }, + /// Not installed: a retryable failure; the next pass retries. + Retry, + /// Installed for the session only, beside the debt. + SessionOnly, + } + let faulted = self.sync_fault.load(std::sync::atomic::Ordering::Relaxed); + let settled = if !self.persister.persists_backfill_coverage() { + Settled::Installed { durable: false } + } else if faulted { + tracing::warn!( + wallet_id = %hex::encode(self.wallet_id), + "DashPay rescan: persistence fault latched; keeping this backfill's coverage \ + for the session only — the next launch re-runs it" + ); + Settled::SessionOnly + } else if staged.store_refused { + Settled::SessionOnly + } else { + let changeset = PlatformWalletChangeSet { + core: cursor_to_write.map(|cursor| CoreChangeSet { + synced_height: Some(cursor), + synced_height_is_rewind: true, + ..CoreChangeSet::default() + }), + dashpay_backfill: Some(staged.clone()), + ..PlatformWalletChangeSet::default() + }; + match self.persister.store(changeset) { + Ok(()) => Settled::Installed { durable: true }, + Err(error) if error.is_transient() => { + tracing::warn!( + wallet_id = %hex::encode(self.wallet_id), + error = %error, + "DashPay rescan: failed to persist the backfill record; the next pass \ + retries and the next launch re-runs this backfill" + ); + Settled::Retry + } + Err(error) => { + // Retrying a rejection that will keep failing would leave + // every later sweep rewinding to these contacts again. + // Stop storing for the session instead. + tracing::error!( + wallet_id = %hex::encode(self.wallet_id), + error = %error, + "DashPay rescan: the backend refused the backfill record; keeping its \ + coverage for the session only and storing no further record round — \ + the next launch re-runs this backfill" + ); + staged.store_refused = true; + Settled::SessionOnly + } + } + }; + match settled { + Settled::Installed { durable } => { + // In memory only (`durable` false), the staged record carries + // no debt: nothing will ever be stored for it to settle. + info.dashpay_backfill = staged; + if let Some(cursor) = cursor_to_write.filter(|_| durable) { + durable_cursors + .insert(self.wallet_id, crate::changeset::DurableCursor::at(cursor)); + } + } + Settled::Retry => return Ok(floor), + Settled::SessionOnly => { + // Coverage for the session, the debt kept beside it: should + // anything store the record later, the owed cursor rides + // with it. + let live = &info.dashpay_backfill; + staged.unpersisted_cursor = live.unpersisted_cursor; + staged.unpersisted_cursor_epoch = live.unpersisted_cursor_epoch; + staged.unpersisted_extent = live.unpersisted_extent; + info.dashpay_backfill = staged; + } + } + for (owner, contact, _, _) in &to_mark { + if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { + managed.dashpay_rescan_triggered_mut().insert(*contact); + } + } Ok(floor) } @@ -1641,6 +1928,7 @@ mod tests { //! 3. Reconcile must be idempotent and never clobber an existing //! entry for the same txid. + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; use std::collections::BTreeMap; use std::sync::{Arc, Mutex}; @@ -1671,19 +1959,126 @@ mod tests { #[derive(Default)] struct RecordingPersister { stores: Mutex>, + /// When set, every `store` fails transiently (and is not recorded). + fail_stores: Mutex, + /// When set, every `store` is refused permanently (a `Fatal` + /// failure, not recorded); each refused attempt is counted. + refuse_stores: Mutex, + refused_attempts: std::sync::atomic::AtomicUsize, + /// When set, the next `store` panics — a backend that dies mid-write. + panic_next_store: Mutex, + /// When set, `store` only buffers and the backend commits at `flush` + /// (`store_commits_inline()` false). + buffered: Mutex, + /// When set, every `flush` fails. + fail_flushes: Mutex, + /// When set, the backend does not attest `ATOMIC_CHANGESETS`. + non_atomic: Mutex, + /// When set, the backend does not attest `ACCOUNT_ADDRESS_POOLS`: it + /// has no account writer (or only a no-op one). + no_account_writer: Mutex, + /// When set, a round carrying both a cursor and a backfill record + /// keeps the record and then fails on the cursor — the partial write + /// a non-atomic backend is allowed to leave. + partial_rounds: Mutex, + /// When set, a round carrying an account registration rejects the + /// account and pool but keeps the round's contact rows, then fails — + /// what `FFIPersister` does on a host without atomic rounds, which + /// carries on to later callbacks after one fails. + reject_account_rounds: Mutex, + /// When set, the next round carrying an account registration raises + /// `gate_entered` and then blocks until the sender side is used or + /// dropped — a slow backend held mid-write. + account_gate: Mutex>>, + gate_entered: std::sync::atomic::AtomicBool, } impl PlatformWalletPersistence for RecordingPersister { + fn store_commits_inline(&self) -> bool { + !*self.buffered.lock().unwrap() + } + + fn stores_dashpay_backfill(&self) -> bool { + true + } + + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { + use crate::changeset::PersistenceCapabilities as C; + let atomic = if *self.non_atomic.lock().unwrap() { + C::NONE + } else { + C::ATOMIC_CHANGESETS + }; + let accounts = if *self.no_account_writer.lock().unwrap() { + C::NONE + } else { + C::ACCOUNT_ADDRESS_POOLS + }; + atomic.union(accounts) + } + fn store( &self, wallet_id: WalletId, changeset: PlatformWalletChangeSet, ) -> Result<(), PersistenceError> { + if *self.fail_stores.lock().unwrap() { + return Err(PersistenceError::backend_with_kind( + crate::changeset::PersistenceErrorKind::Transient, + "injected store failure", + )); + } + if *self.refuse_stores.lock().unwrap() { + self.refused_attempts + .fetch_add(1, std::sync::atomic::Ordering::SeqCst); + return Err(PersistenceError::backend("injected store refusal")); + } + if std::mem::take(&mut *self.panic_next_store.lock().unwrap()) { + panic!("recording persister: injected store panic"); + } + if !changeset.account_registrations.is_empty() { + let gate = self.account_gate.lock().unwrap().take(); + if let Some(gate) = gate { + self.gate_entered + .store(true, std::sync::atomic::Ordering::SeqCst); + let _ = gate.recv(); + } + } + if *self.reject_account_rounds.lock().unwrap() + && !changeset.account_registrations.is_empty() + { + let kept = PlatformWalletChangeSet { + contacts: changeset.contacts, + ..PlatformWalletChangeSet::default() + }; + self.stores.lock().unwrap().push((wallet_id, kept)); + return Err(PersistenceError::backend("injected account write failure")); + } + if *self.partial_rounds.lock().unwrap() + && changeset.dashpay_backfill.is_some() + && changeset + .core + .as_ref() + .is_some_and(|core| core.synced_height.is_some()) + { + let kept = PlatformWalletChangeSet { + dashpay_backfill: changeset.dashpay_backfill, + ..PlatformWalletChangeSet::default() + }; + self.stores.lock().unwrap().push((wallet_id, kept)); + return Err(PersistenceError::backend("injected cursor write failure")); + } self.stores.lock().unwrap().push((wallet_id, changeset)); Ok(()) } fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + if *self.fail_flushes.lock().unwrap() { + return Err(PersistenceError::backend_with_kind( + PersistenceErrorKind::Transient, + "injected flush failure", + )); + } Ok(()) } @@ -2697,6 +3092,9 @@ mod tests { .register_contact_account(&owner, &contact, 0, test_receiving_xpub(&owner, &contact)) .await .expect("register receival account"); + // An account on file that the record does not know (see + // `forget_recorded_coverage`). + forget_recorded_coverage(&manager, wallet_id, owner, contact).await; { let mut wm = iw.wallet_manager.write().await; @@ -2789,20 +3187,64 @@ mod tests { .managed_identity_mut(&owner) .expect("managed") .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + // Registration records its own coverage now. These fixtures exercise + // the reconcile's rewind path, which serves accounts persisted before + // the record existed (and hosts that do not store it), so model that: + // the account is on file, the record does not know it. + info.dashpay_backfill + .retain(|o, c, _| (o, c) != (&owner, &contact)); + } + + /// Drop `(owner, contact)` from the wallet's backfill record — the shape + /// of an account registered before the record existed, or by a host + /// that does not persist it — so the reconcile's own rewind path is what + /// a test exercises. + async fn forget_recorded_coverage( + manager: &Arc>, + wallet_id: WalletId, + owner: Identifier, + contact: Identifier, + ) { + let mut wm = manager.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .dashpay_backfill + .retain(|o, c, _| (o, c) != (&owner, &contact)); } + /// Move the wallet's cursor to `height` the way a scan does: in memory, + /// and — as the event adapter would once it commits the advance — on the + /// host, recorded in the manager's durable-cursor map. async fn set_synced_height( manager: &Arc>, wallet_id: WalletId, height: u32, ) { use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + let mut durable = manager.durable_cursors.lock().await; let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); let mut wm = wallet.identity().wallet_manager.write().await; wm.get_wallet_info_mut(&wallet_id) .expect("info") .core_wallet .update_synced_height(height); + durable.insert(wallet_id, crate::changeset::DurableCursor::at(height)); + } + + /// The wallet's [`RewindBarrier`](crate::changeset::core_bridge::RewindBarrier) + /// epoch: how many times its cursor was lowered in memory. + async fn rewind_epoch( + manager: &Arc>, + wallet_id: WalletId, + ) -> u64 { + manager + .wallet_manager + .read() + .await + .get_wallet_info(&wallet_id) + .expect("info") + .rewind_barrier + .epoch() } async fn synced_height( @@ -2940,8 +3382,10 @@ mod tests { } /// `synced_height == 0` means "scan from genesis / not started" — already a - /// full historical scan, so the rescan is a no-op (the masking path the spec - /// warns about). + /// full historical scan, so the rescan rewinds nothing (the masking path the + /// spec warns about). The contact is still recorded as forward-covered, so + /// the sweep does not rewind for it once that scan has climbed past its + /// funding height. #[tokio::test] async fn rescan_is_a_noop_when_synced_height_is_zero() { let (manager, persister, wallet_id) = make_wallet().await; @@ -2963,6 +3407,3926 @@ mod tests { "synced_height 0 -> no rescan" ); assert_eq!(synced_height(&manager, wallet_id).await, 0); + let wm = manager.wallet_manager.read().await; + assert!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .covers(&owner, &contact, 0, 100), + "recorded as forward-covered by the coming scan" + ); + } + + /// The persisted half of the rescan guard, captured from a live manager + /// the way a host stores it: the wallet, its managed info (accounts and + /// cursor), its identities with the in-memory marks cleared, and the + /// backfill record from the last round the manager stored. + struct ReloadPersister { + wallet: key_wallet::Wallet, + managed: key_wallet::wallet::ManagedWalletInfo, + identities: std::collections::BTreeMap< + crate::wallet::identity::RegistrationIndex, + crate::wallet::identity::ManagedIdentity, + >, + backfill: crate::changeset::DashPayBackfillRecord, + stores: Mutex>, + } + + impl PlatformWalletPersistence for ReloadPersister { + fn store_commits_inline(&self) -> bool { + true + } + + fn stores_dashpay_backfill(&self) -> bool { + true + } + + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { + crate::changeset::PersistenceCapabilities::ATOMIC_CHANGESETS + .union(crate::changeset::PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS) + } + + fn store( + &self, + wallet_id: WalletId, + changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + self.stores.lock().unwrap().push((wallet_id, changeset)); + Ok(()) + } + + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + Ok(()) + } + + fn load(&self) -> Result { + let wallet_id = self.wallet.compute_wallet_id(); + let mut wallet_identities = std::collections::BTreeMap::new(); + wallet_identities.insert(wallet_id, self.identities.clone()); + let mut wallets = std::collections::BTreeMap::new(); + wallets.insert( + wallet_id, + crate::changeset::ClientWalletStartState { + wallet: self.wallet.clone(), + wallet_info: self.managed.clone(), + identity_manager: crate::changeset::IdentityManagerStartState { + wallet_identities, + ..Default::default() + }, + unused_asset_locks: std::collections::BTreeMap::new(), + unconfirmed_outgoing_txs: Vec::new(), + dashpay_backfill: self.backfill.clone(), + }, + ); + Ok(ClientStartState { + wallets, + ..Default::default() + }) + } + } + + /// Snapshot `manager`'s wallet the way a relaunch would restore it: the + /// persisted cursor at `persisted_synced_height`, every in-memory + /// rescan mark gone, and the backfill record as the host last stored it + /// (`backfill`, or the record the manager holds when `None`). + async fn reload_snapshot( + manager: &Arc>, + wallet_id: WalletId, + owner: Identifier, + persisted_synced_height: u32, + backfill: Option, + ) -> ReloadPersister { + let wm = manager.wallet_manager.read().await; + let wallet = wm.get_wallet(&wallet_id).expect("wallet").clone(); + let info = wm.get_wallet_info(&wallet_id).expect("info"); + let mut managed = info.core_wallet.clone(); + managed.metadata.synced_height = persisted_synced_height; + let mut identity = info + .identity_manager + .managed_identity(&owner) + .expect("owner identity") + .clone(); + identity.dashpay_rescan_triggered_mut().clear(); + let mut identities = std::collections::BTreeMap::new(); + identities.insert(0, identity); + ReloadPersister { + wallet, + managed, + identities, + backfill: backfill.unwrap_or_else(|| info.dashpay_backfill.clone()), + stores: Mutex::default(), + } + } + + /// Bring a manager up from `persister` and hand back the restored wallet. + async fn reload( + persister: ReloadPersister, + ) -> ( + Arc>, + Arc, + WalletId, + ) { + let wallet_id = persister.wallet.compute_wallet_id(); + let persister = Arc::new(persister); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new( + sdk, + Arc::clone(&persister), + handler, + )); + manager + .load_from_persistor() + .await + .expect("the snapshot must load"); + (manager, persister, wallet_id) + } + + /// The backfill record the manager last stored, from the newest round + /// that carried one. + fn last_stored_record( + stores: &[(WalletId, PlatformWalletChangeSet)], + ) -> Option<&PlatformWalletChangeSet> { + stores + .iter() + .rev() + .map(|(_, cs)| cs) + .find(|cs| cs.dashpay_backfill.is_some()) + } + + /// dashpay/platform#4302. The cursor a rewind lowers is durable — the + /// host persists every height the rescan climbs through — so a relaunch + /// restores it *inside* the climb. With only the in-memory guard every + /// fresh process rewound again, forever. The record the rewind stored + /// on its own round must make the next process resume from the restored + /// cursor instead; and it is the record doing that, not anything the + /// reload happens to carry: the same snapshot without it rewinds again. + #[tokio::test] + async fn rescan_resumes_from_the_persisted_cursor_after_a_reload_instead_of_rewinding_again() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + persister.stores.lock().unwrap().clear(); + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("first pass"), + Some(100) + ); + + // The record and the lowered cursor ride ONE round: a host that + // stores the record has stored the cursor it vouches for. + { + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the rewind must persist its record"); + assert_eq!( + round.core.as_ref().and_then(|core| core.synced_height), + Some(100), + "the lowered cursor must be on the same round as the record" + ); + let record = round.dashpay_backfill.as_ref().unwrap(); + assert_eq!((record.floor, record.rewound_from), (100, 1_000)); + assert_eq!(record.covered_from(&owner, &contact, 0), Some(100)); + assert!(record.is_pending(100)); + assert!( + !record.is_complete(999), + "not complete until the scan climbs past 1000" + ); + assert!(record.is_complete(1_000)); + } + + // The process dies while the rescan is climbing: the host persisted + // the cursor at 400. + let snapshot = reload_snapshot(&manager, wallet_id, owner, 400, None).await; + let (restarted, restarted_persister, _) = reload(snapshot).await; + let restored = restarted + .get_wallet(&wallet_id) + .await + .expect("restored wallet"); + assert_eq!( + restored + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass after reload"), + None, + "a contact the persisted record covers must not be rewound for again" + ); + { + let wm = restarted.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!( + info.core_wallet.synced_height(), + 400, + "the scan resumes from the restored cursor, mid-climb" + ); + assert!( + info.identity_manager + .managed_identity(&owner) + .unwrap() + .dashpay() + .rescan_triggered + .contains(&contact), + "the record re-seeds the in-memory guard for this process" + ); + assert!(info.dashpay_backfill.is_pending(400)); + } + assert!( + restarted_persister + .stores + .lock() + .unwrap() + .iter() + .all(|(_, cs)| cs.dashpay_backfill.is_none()), + "nothing changed, so nothing is re-stored" + ); + + // The climb completes: still no rewind, and the record reads complete. + { + let mut wm = restarted.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.core_wallet.update_synced_height(1_000); + } + assert_eq!( + restored + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass at completion"), + None + ); + { + let wm = restarted.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert!(info.dashpay_backfill.is_complete(1_000)); + assert_eq!(info.core_wallet.synced_height(), 1_000); + } + + // Mutation check: the identical snapshot with the record withheld — + // the in-memory-only behaviour — rewinds again. Restoring that + // behaviour in the reconcile fails the assertions above, not this one. + let without = reload_snapshot( + &manager, + wallet_id, + owner, + 400, + Some(crate::changeset::DashPayBackfillRecord::default()), + ) + .await; + let (bare, _, _) = reload(without).await; + assert_eq!( + bare.get_wallet(&wallet_id) + .await + .expect("wallet") + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass without a record"), + Some(100), + "without the persisted record the reload rewinds again — the defect" + ); + } + + /// A contact's checkpoint can drop after it was covered: the reciprocal + /// request turns out to be older than ours. The record covers it only + /// from the height it was recorded at, so the reload must still rewind + /// to the new, lower checkpoint — and the record's floor follows it + /// while its climb target stays. + #[tokio::test] + async fn rescan_reload_rewinds_for_a_contact_whose_checkpoint_dropped_below_its_covered_height() + { + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("first pass"), + Some(100) + ); + + // Before the relaunch the persisted contact state learned an older + // incoming request (50), while the record still says "covered from + // 100". + let mut snapshot = reload_snapshot(&manager, wallet_id, owner, 400, None).await; + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 50, 0); + snapshot + .identities + .get_mut(&0) + .unwrap() + .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + assert_eq!( + snapshot.backfill.covered_from(&owner, &contact, 0), + Some(100) + ); + + let (restarted, restarted_persister, _) = reload(snapshot).await; + assert_eq!( + restarted + .get_wallet(&wallet_id) + .await + .expect("wallet") + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass after reload"), + Some(50), + "a checkpoint below the recorded height re-arms the contact" + ); + let wm = restarted.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.synced_height(), 50); + assert_eq!( + info.dashpay_backfill.covered_from(&owner, &contact, 0), + Some(50) + ); + assert_eq!( + ( + info.dashpay_backfill.floor, + info.dashpay_backfill.rewound_from + ), + (50, 1_000), + "the floor lowers, the climb target stays" + ); + let stores = restarted_persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the deeper rewind persists its record"); + assert_eq!( + round.core.as_ref().and_then(|core| core.synced_height), + Some(50) + ); + } + + /// The record covers exactly the contacts whose rewind it was written + /// for. A receival account it never listed — registered after the record + /// was stored, or established on another device and only now built here + /// — is re-armed on reload regardless of how far the scan has climbed: + /// the scan the record vouches for never watched that contact. + #[tokio::test] + async fn rescan_reload_rearms_a_receival_contact_the_record_never_listed() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let covered = Identifier::from([0xBB; 32]); + let newcomer = Identifier::from([0xCC; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, covered, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("first pass"), + Some(100) + ); + let record = { + let wm = manager.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .unwrap() + .dashpay_backfill + .clone() + }; + assert_eq!(record.covered().len(), 1); + + // The newcomer's account exists on this device by the time of the + // relaunch, but the record on disk predates it. + establish_receival_contact(&manager, &persister, wallet_id, owner, newcomer, 300, 300) + .await; + let snapshot = reload_snapshot(&manager, wallet_id, owner, 400, Some(record)).await; + let (restarted, _, _) = reload(snapshot).await; + assert_eq!( + restarted + .get_wallet(&wallet_id) + .await + .expect("wallet") + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass after reload"), + Some(300), + "an unlisted receival contact is never treated as covered" + ); + let wm = restarted.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.synced_height(), 300); + assert_eq!( + info.dashpay_backfill.covered_from(&owner, &covered, 0), + Some(100) + ); + assert_eq!( + info.dashpay_backfill.covered_from(&owner, &newcomer, 0), + Some(300) + ); + assert_eq!( + ( + info.dashpay_backfill.floor, + info.dashpay_backfill.rewound_from + ), + (100, 1_000) + ); + } + + /// Coverage is per receival account, not per relationship: a second + /// receiving account for an already-covered pair derives its own xpub, + /// and the scan that covered the first never tested its scripts. It is + /// rewound for, not waved through on the first account's entry. + #[tokio::test] + async fn a_second_receival_account_for_a_covered_pair_is_rewound_not_inherited() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let dashpay = wallet.identity().dashpay(); + assert_eq!( + dashpay + .reconcile_dashpay_rescan() + .await + .expect("first pass"), + Some(100) + ); + assert_eq!( + rewind_epoch(&manager, wallet_id).await, + 1, + "the rewind arms the barrier" + ); + set_synced_height(&manager, wallet_id, 1_000).await; + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("steady"), + None, + "account 0 is covered" + ); + + dashpay + .register_contact_account( + &owner, + &contact, + 1, + test_receiving_xpub(&owner, &Identifier::from([0xDD; 32])), + ) + .await + .expect("register account 1"); + assert_eq!( + dashpay + .reconcile_dashpay_rescan() + .await + .expect("pass after account 1"), + Some(100), + "account 1 never inherits account 0's coverage" + ); + let wm = manager.wallet_manager.read().await; + let record = &wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill; + assert_eq!(record.covered_from(&owner, &contact, 0), Some(100)); + assert_eq!(record.covered_from(&owner, &contact, 1), Some(100)); + let stores = persister.stores.lock().unwrap(); + assert_eq!( + last_stored_record(&stores) + .and_then(|cs| cs.dashpay_backfill.as_ref()) + .expect("a record round") + .covered() + .len(), + 2, + "both accounts reach the host" + ); + } + + /// A contact's compact receiving xpub encrypted under `shared_key` — the + /// wire shape a contact sends us — so `register_external_contact_account` + /// can be driven without a signer. + fn encrypted_contact_xpub( + owner: &Identifier, + contact: &Identifier, + shared_key: &[u8; 32], + ) -> Vec { + let seed = Mnemonic::from_phrase(TEST_MNEMONIC) + .expect("mnemonic") + .to_seed(""); + let w = key_wallet::wallet::Wallet::from_seed_bytes( + seed, + Network::Testnet, + WalletAccountCreationOptions::None, + ) + .expect("seed wallet"); + let compact = crate::wallet::identity::crypto::dip14::derive_contact_xpub( + &w, + Network::Testnet, + 0, + owner, + contact, + ) + .expect("derive a valid compact xpub") + .compact + .to_bytes(); + platform_encryption::encrypt_extended_public_key(shared_key, &[0x11u8; 16], &compact) + } + + /// The outbound `DashpayExternalAccount` is watch-only and never + /// receives, so registering (or rebuilding) it must not move the + /// filter-scan cursor. Every cold start whose host does not persist + /// `external_account_reference` tears the account down and rebuilds it; + /// on a tree where that rebuild also rewound the cursor it re-walked + /// every filter from the earliest contact's request height per launch + /// (dashpay/platform#4302, the topple relaunch). Pin the invariant so a + /// registration-time rewind (#4740) cannot pick the outbound account up. + #[tokio::test] + async fn external_account_registration_leaves_the_scan_cursor_untouched() { + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + let shared_key = [0x55u8; 32]; + let encrypted = encrypted_contact_xpub(&owner, &contact, &shared_key); + { + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let outgoing = + ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 1_226_329, 0); + let incoming = + ContactRequest::new(contact, owner, 0, 0, 0, encrypted.clone(), 1_226_329, 0); + info.identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + info.core_wallet.update_synced_height(1_560_740); + } + let registration = iw + .dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &encrypted, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register the outbound account"); + assert_eq!( + registration, + crate::wallet::identity::network::contacts::ExternalAccountRegistration::Built + ); + assert_eq!( + synced_height(&manager, wallet_id).await, + 1_560_740, + "an outbound account never receives: building it must not rewind the scan" + ); + } + + /// The observed relaunch, end to end (dashpay/platform#4302, topple on + /// int23): a restore with established contacts, both accounts persisted, + /// the cursor at the tip and a record covering every contact. With the + /// outbound-account marker restored, the contact pass finds nothing to + /// rebuild; without it (a host that does not carry the marker) the + /// rebuild still leaves the cursor alone; and the reconcile pass after + /// either leaves the cursor at the tip. + #[tokio::test] + async fn a_cold_start_with_persisted_accounts_and_record_leaves_the_cursor_at_the_tip() { + use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + use key_wallet::account::account_collection::DashpayAccountKey; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contacts: Vec = (1..=8u8).map(|b| Identifier::from([b; 32])).collect(); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + { + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let managed = info + .identity_manager + .managed_identity_mut(&owner) + .expect("managed"); + for (i, contact) in contacts.iter().enumerate() { + let height = 1_226_329 + (i as u32) * 5_000; + let outgoing = + ContactRequest::new(owner, *contact, 0, 0, 0, vec![0u8; 96], height, 0); + // The incoming request carries the ciphertext the outbound + // account is built from, so the marker stamp accepts it. + let ciphertext = encrypted_contact_xpub(&owner, contact, &[0x55u8; 32]); + let incoming = ContactRequest::new(*contact, owner, 0, 0, 7, ciphertext, height, 0); + managed.apply_established_contact(EstablishedContact::new( + *contact, outgoing, incoming, + )); + } + } + // First session: the scan is at the tip when the contacts arrive. + // Registration never moves the cursor here; the reconcile pass then + // rewinds once to the earliest request height and records every + // contact, every outbound registration stamps the marker, and the scan + // climbs back to the tip. + set_synced_height(&manager, wallet_id, 1_560_740).await; + let shared_key = [0x55u8; 32]; + for contact in &contacts { + iw.dashpay() + .register_contact_account(&owner, contact, 0, test_receiving_xpub(&owner, contact)) + .await + .expect("register receival"); + let encrypted = encrypted_contact_xpub(&owner, contact, &shared_key); + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &encrypted, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register outbound"); + iw.dashpay() + .note_external_account_registered(&owner, contact, &encrypted) + .await; + } + assert_eq!( + synced_height(&manager, wallet_id).await, + 1_560_740, + "registration alone never moves the cursor" + ); + assert_eq!( + iw.dashpay() + .reconcile_dashpay_rescan() + .await + .expect("first reconcile"), + Some(1_226_329) + ); + set_synced_height(&manager, wallet_id, 1_560_740).await; + + // The host persisted everything: accounts, contacts with the marker, + // the record, the cursor at the tip. Cold start. + let snapshot = reload_snapshot(&manager, wallet_id, owner, 1_560_740, None).await; + let (restarted, _, _) = reload(snapshot).await; + { + let wm = restarted.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + let managed = info.identity_manager.managed_identity(&owner).unwrap(); + for contact in &contacts { + let established = managed.dashpay().established_contacts()[contact].clone(); + assert_eq!(established.external_account_reference, Some(7)); + assert!( + !external_account_needs_rebuild(&established, true), + "a restored marker must not read as a rotation" + ); + } + assert!( + crate::wallet::identity::network::dashpay_view::DashPayView::< + crate::broadcaster::SpvBroadcaster, + >::collect_account_build_candidates(info, &owner) + .is_empty(), + "both accounts persisted: the contact pass has nothing to build" + ); + } + let restored = restarted.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + restored + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None + ); + assert_eq!(synced_height_of(&restarted, wallet_id).await, 1_560_740); + + // A host that did not carry the marker: the pass tears the outbound + // accounts down and the drain rebuilds them — and even that must not + // move the cursor any more. + let mut bare = reload_snapshot(&manager, wallet_id, owner, 1_560_740, None).await; + for contact in &contacts { + let established = bare.identities[&0].dashpay().established_contacts()[contact].clone(); + let mut stripped = established.clone(); + stripped.external_account_reference = None; + bare.identities + .get_mut(&0) + .unwrap() + .apply_established_contact(stripped); + assert!(external_account_needs_rebuild( + &bare.identities[&0].dashpay().established_contacts()[contact], + true + )); + } + let (restarted, _, _) = reload(bare).await; + let restored = restarted.get_wallet(&wallet_id).await.expect("wallet"); + for contact in &contacts { + { + let mut wm = restarted.wallet_manager.write().await; + let (wallet, info) = wm.get_wallet_mut_and_info_mut(&wallet_id).unwrap(); + let key = DashpayAccountKey { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }; + wallet.accounts.dashpay_external_accounts.remove(&key); + info.core_wallet + .accounts + .dashpay_external_accounts + .remove(&key); + } + let encrypted = encrypted_contact_xpub(&owner, contact, &shared_key); + assert_eq!( + restored + .identity() + .dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &encrypted, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("rebuild outbound"), + crate::wallet::identity::network::contacts::ExternalAccountRegistration::Built + ); + } + assert_eq!( + restored + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None + ); + assert_eq!( + synced_height_of(&restarted, wallet_id).await, + 1_560_740, + "rebuilding every outbound account on a cold start must leave the cursor at the tip" + ); + } + + /// Contacts discovered only after the scan has passed their request + /// heights. Registration never moves the cursor on this tree; the + /// reconcile pass then rewinds exactly once and writes the record with + /// the real pair — the floor and the cursor it rewound from — on the + /// round that lowers the cursor. A kill mid-climb then resumes from the + /// persisted cursor: the drain's re-registrations are no-ops, the pass + /// has nothing to build, the reconcile does not rewind, and the record + /// still reads pending with its pair intact. + #[tokio::test] + async fn late_discovered_contacts_rewind_once_and_a_mid_climb_kill_resumes_from_the_cursor() { + use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contacts: Vec = (1..=8u8).map(|b| Identifier::from([b; 32])).collect(); + let shared_key = [0x55u8; 32]; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + { + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let managed = info + .identity_manager + .managed_identity_mut(&owner) + .expect("managed"); + for (i, contact) in contacts.iter().enumerate() { + let height = 1_226_329 + (i as u32) * 5_000; + let outgoing = + ContactRequest::new(owner, *contact, 0, 0, 0, vec![0u8; 96], height, 0); + let ciphertext = encrypted_contact_xpub(&owner, contact, &shared_key); + let incoming = ContactRequest::new(*contact, owner, 0, 0, 7, ciphertext, height, 0); + managed.apply_established_contact(EstablishedContact::new( + *contact, outgoing, incoming, + )); + } + } + // The scan is already at the tip when the contacts are discovered. + set_synced_height(&manager, wallet_id, 1_560_731).await; + persister.stores.lock().unwrap().clear(); + + let mut lowering_rounds = 0usize; + for contact in &contacts { + let before = persister.stores.lock().unwrap().len(); + iw.dashpay() + .register_contact_account(&owner, contact, 0, test_receiving_xpub(&owner, contact)) + .await + .expect("register receival"); + let encrypted = encrypted_contact_xpub(&owner, contact, &shared_key); + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &encrypted, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register outbound"); + iw.dashpay() + .note_external_account_registered(&owner, contact, &encrypted) + .await; + let stores = persister.stores.lock().unwrap(); + lowering_rounds += stores[before..] + .iter() + .filter(|(_, cs)| { + cs.core + .as_ref() + .and_then(|core| core.synced_height) + .is_some() + }) + .count(); + } + assert_eq!( + lowering_rounds, 0, + "registration never moves the cursor on this tree" + ); + assert_eq!(synced_height(&manager, wallet_id).await, 1_560_731); + // The reconcile pass is what rewinds: once, to the earliest request + // height, recording every contact on the round that lowers the cursor. + assert_eq!( + iw.dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + Some(1_226_329) + ); + assert_eq!(synced_height(&manager, wallet_id).await, 1_226_329); + { + let stores = persister.stores.lock().unwrap(); + let lowering = stores + .iter() + .find(|(_, cs)| { + cs.core + .as_ref() + .and_then(|core| core.synced_height) + .is_some() + }) + .map(|(_, cs)| cs) + .expect("the lowering round"); + assert_eq!( + lowering.core.as_ref().and_then(|core| core.synced_height), + Some(1_226_329) + ); + let record = lowering + .dashpay_backfill + .as_ref() + .expect("the lowering round carries the record"); + assert_eq!( + (record.floor, record.rewound_from), + (1_226_329, 1_560_731), + "the real pair: the floor and the cursor it rewound from" + ); + // The last record written covers every contact, pair unchanged. + let last = last_stored_record(&stores) + .unwrap() + .dashpay_backfill + .as_ref() + .unwrap(); + assert_eq!((last.floor, last.rewound_from), (1_226_329, 1_560_731)); + for contact in &contacts { + assert!(last.covered_from(&owner, contact, 0).is_some()); + } + } + + // Killed mid-climb: the host persisted the cursor at 1_400_000. + let snapshot = reload_snapshot(&manager, wallet_id, owner, 1_400_000, None).await; + let (restarted, restarted_persister, _) = reload(snapshot).await; + let restored = restarted.get_wallet(&wallet_id).await.expect("wallet"); + { + let wm = restarted.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + let managed = info.identity_manager.managed_identity(&owner).unwrap(); + for contact in &contacts { + assert!(!external_account_needs_rebuild( + &managed.dashpay().established_contacts()[contact], + true + )); + } + assert!( + crate::wallet::identity::network::dashpay_view::DashPayView::< + crate::broadcaster::SpvBroadcaster, + >::collect_account_build_candidates(info, &owner) + .is_empty() + ); + assert!(info.dashpay_backfill.is_pending(1_400_000)); + } + // A drain that re-registers anyway is a no-op on accounts on file. + for contact in &contacts { + restored + .identity() + .dashpay() + .register_contact_account(&owner, contact, 0, test_receiving_xpub(&owner, contact)) + .await + .expect("idempotent re-registration"); + } + assert_eq!( + restored + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None + ); + assert_eq!( + synced_height_of(&restarted, wallet_id).await, + 1_400_000, + "the climb resumes from the persisted cursor" + ); + assert!( + restarted_persister + .stores + .lock() + .unwrap() + .iter() + .all(|(_, cs)| cs + .core + .as_ref() + .and_then(|core| core.synced_height) + .is_none()), + "nothing lowers the cursor on the cold start" + ); + let wm = restarted.wallet_manager.read().await; + let record = &wm.get_wallet_info(&wallet_id).unwrap().dashpay_backfill; + assert_eq!((record.floor, record.rewound_from), (1_226_329, 1_560_731)); + assert!(record.is_pending(1_400_000)); + assert!(record.is_complete(1_560_731)); + } + + /// key-wallet's account-add API rewinds the cursor to just below wallet + /// birth without emitting or persisting anything. That lowering is owed + /// to the host like any other in-memory rewind: a later record-only + /// round (a contact forward-covered at the lowered cursor) must carry it, + /// or the host keeps its high-water beside the new coverage and the next + /// launch skips the rescan (dashpay/platform#4302 review). + #[tokio::test] + async fn an_account_add_rewind_is_owed_to_the_next_record_round() { + use key_wallet::account::{AccountType, StandardAccountType}; + use key_wallet::wallet::managed_wallet_info::managed_account_operations::ManagedAccountOperations; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + set_synced_height(&manager, wallet_id, 1_000).await; + { + let mut wm = manager.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.core_wallet.metadata.birth_height = 101; + ManagedAccountOperations::add_managed_account_from_xpub( + info, + AccountType::Standard { + index: 9, + standard_account_type: StandardAccountType::BIP44Account, + }, + test_receiving_xpub(&owner, &Identifier::from([0xEE; 32])), + ) + .expect("add account"); + assert_eq!( + info.core_wallet.metadata.synced_height, 100, + "rewound to birth - 1" + ); + assert_eq!(info.dashpay_backfill.unpersisted_cursor, Some(100)); + assert_eq!(info.rewind_barrier.epoch(), 1, "and the barrier is armed"); + } + + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 200, 200).await; + persister.stores.lock().unwrap().clear(); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None, + "forward-covered at the lowered cursor" + ); + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the record is stored"); + let core = round + .core + .as_ref() + .expect("the owed cursor rides the round"); + assert_eq!(core.synced_height, Some(100)); + assert!(core.synced_height_is_rewind); + } + + /// A fresh manager recreating, born at 101, the wallet a host holds at + /// cursor 1000 with `backfill` on its row. + async fn recreate_on_host( + backfill: crate::changeset::DashPayBackfillRecord, + ) -> ( + Arc>, + Arc, + Arc, + WalletId, + ) { + recreate_on_host_at(backfill, 1_000, None).await + } + + /// [`recreate_on_host`] with the host row at `host_cursor`, and + /// optionally a `DurableCursors` entry already under the wallet's id + /// before the registration — what a same-id predecessor's commit, + /// accepted after the registration read its snapshot, leaves behind. + async fn recreate_on_host_at( + backfill: crate::changeset::DashPayBackfillRecord, + host_cursor: u32, + leftover_entry: Option, + ) -> ( + Arc>, + Arc, + Arc, + WalletId, + ) { + let (donor, _donor_persister, wallet_id) = make_wallet().await; + let (wallet, mut managed) = { + let wm = donor.wallet_manager.read().await; + ( + wm.get_wallet(&wallet_id).expect("wallet").clone(), + wm.get_wallet_info(&wallet_id) + .expect("info") + .core_wallet + .clone(), + ) + }; + managed.metadata.synced_height = host_cursor; + let host = Arc::new(ReloadPersister { + wallet, + managed, + identities: std::collections::BTreeMap::new(), + backfill, + stores: Mutex::default(), + }); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + if let Some(entry) = leftover_entry { + manager + .durable_cursors + .lock() + .await + .insert(wallet_id, entry); + } + let seed = Mnemonic::from_phrase(TEST_MNEMONIC) + .expect("valid mnemonic") + .to_seed(""); + let recreated = manager + .create_wallet_from_seed_bytes( + Network::Testnet, + &seed, + WalletAccountCreationOptions::Default, + Some(101), + ) + .await + .expect("recreate"); + assert_eq!(recreated.wallet_id(), wallet_id); + (manager, host, recreated, wallet_id) + } + + /// Register `(owner, contact)`'s receival account on a recreated wallet + /// and establish the contact at `checkpoint`. + async fn establish_on_recreated( + manager: &Arc>, + host: &Arc, + wallet: &Arc, + owner: Identifier, + contact: Identifier, + checkpoint: u32, + ) { + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + let wallet_id = wallet.wallet_id(); + wallet + .identity() + .dashpay() + .register_contact_account(&owner, &contact, 0, test_receiving_xpub(&owner, &contact)) + .await + .expect("register receival account"); + let p = WalletPersister::new(wallet_id, Arc::clone(host) as _); + let mut wm = manager.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], checkpoint, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], checkpoint, 0); + info.identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + } + + /// A fresh manager recreating a wallet the host already holds (the same + /// seed added again) builds a cursor below the host's — registration + /// writes no core cursor and the host keeps its row's `syncedHeight`. + /// The durable cursor is seeded from what the host holds, and the lower + /// built cursor is owed like any in-memory reset: the first record round + /// carries it, so a contact forward-covered at the built cursor is never + /// stored beside the host's higher one, and a reload rewinds for nothing + /// it should not (dashpay/platform#4302 review). + #[tokio::test] + async fn recreating_a_persisted_wallet_owes_its_lower_cursor_to_the_host() { + let (manager, host, recreated, wallet_id) = recreate_on_host(Default::default()).await; + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.metadata.synced_height, 100); + assert_eq!( + info.dashpay_backfill.unpersisted_cursor, + Some(100), + "the lower built cursor is owed to the host" + ); + } + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .map(|cursor| cursor.height), + Some(1_000), + "the durable cursor is the host's, not the built one" + ); + + // A contact forward-covered at the built cursor. + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_on_recreated(&manager, &host, &recreated, owner, contact, 200).await; + host.stores.lock().unwrap().clear(); + assert_eq!( + recreated + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None, + "forward-covered at the built cursor" + ); + let stores = host.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the record is stored"); + let core = round + .core + .as_ref() + .expect("the owed reset rides the record round"); + assert_eq!(core.synced_height, Some(100)); + assert!(core.synced_height_is_rewind); + assert_eq!( + round + .dashpay_backfill + .as_ref() + .unwrap() + .covered_from(&owner, &contact, 0), + Some(200) + ); + } + + /// Coverage for a receival account the recreated wallet does not hold is + /// not coverage: the successor's scan does not watch that account's + /// scripts, and the account-generation guard only stops batches still in + /// flight, not an advance already committed. So recreation keeps the + /// host's record only for the accounts it holds — none, from the seed — + /// and the account is rewound for when it is registered, even though the + /// host's record said it was covered (dashpay/platform#4302 review). + #[tokio::test] + async fn a_recreated_wallet_rearms_coverage_for_receival_accounts_it_does_not_hold() { + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let mut host_record = crate::changeset::DashPayBackfillRecord::default(); + host_record.record_pass(900, Some(200), [(owner, contact, 0, 200)]); + let (manager, host, recreated, wallet_id) = recreate_on_host(host_record.clone()).await; + { + let wm = manager.wallet_manager.read().await; + let record = &wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill; + assert!( + record.is_empty(), + "no receival account is held, so nothing is covered" + ); + assert!(!record.has_extent()); + assert_eq!(record.unpersisted_cursor, Some(100)); + } + // The invalidation reached the host before anything else of this + // registration did: the host would otherwise restore the old + // coverage (and A's account rows) beside this wallet's advanced + // cursor after a crash. + let invalidated = first_store_is_an_empty_record(&host.stores.lock().unwrap()); + + // The successor scans 101..1500 before the account exists, and the + // host accepts that advance (which also pays the owed 100). + accept_successor_advance(&manager, wallet_id, 1_500).await; + host.stores.lock().unwrap().clear(); + establish_on_recreated(&manager, &host, &recreated, owner, contact, 200).await; + assert_eq!( + recreated + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + Some(200), + "101..1500 was scanned without the account: the old coverage must not suppress this" + ); + { + let stores = host.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the rewind is recorded"); + let core = round.core.as_ref().expect("the rewind rides the round"); + assert_eq!(core.synced_height, Some(200)); + assert!(core.synced_height_is_rewind); + } + + // The crash case: the host restores A's account rows with the cursor + // the successor persisted (1500) and whatever record it holds. With + // the invalidated record the reload rewinds for A; with the old + // record it would have trusted coverage over a range scanned without + // A — which is what storing the invalidation first prevents. + assert_only_the_replaced_record_rearms_after_a_crash( + &manager, + wallet_id, + owner, + 1_500, + invalidated, + host_record, + ) + .await; + } + + /// Restore `wallet_id` the way a crash would — its accounts, `owner`'s + /// established contact and the cursor at `persisted_cursor` — once with + /// the record the publication stored over the host's (`replaced`) and + /// once with the coverage it replaced (`stale`). Only the stored record + /// may rewind for the contact; the stale one would have skipped the + /// range scanned without its account. + async fn assert_only_the_replaced_record_rearms_after_a_crash( + manager: &Arc>, + wallet_id: WalletId, + owner: Identifier, + persisted_cursor: u32, + replaced: crate::changeset::DashPayBackfillRecord, + stale: crate::changeset::DashPayBackfillRecord, + ) { + let (wallet, managed, identity) = { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + let mut managed = info.core_wallet.clone(); + managed.metadata.synced_height = persisted_cursor; + let mut identity = info + .identity_manager + .managed_identity(&owner) + .expect("owner") + .clone(); + identity.dashpay_rescan_triggered_mut().clear(); + ( + wm.get_wallet(&wallet_id).expect("wallet").clone(), + managed, + identity, + ) + }; + for (record, expected, why) in [ + (replaced, Some(200), "the stored record re-arms the account"), + ( + stale, + None, + "the replaced coverage would have skipped the unwatched range", + ), + ] { + let mut identities = std::collections::BTreeMap::new(); + identities.insert(0, identity.clone()); + let (restarted, _, _) = reload(ReloadPersister { + wallet: wallet.clone(), + managed: managed.clone(), + identities, + backfill: record, + stores: Mutex::default(), + }) + .await; + assert_eq!( + restarted + .get_wallet(&wallet_id) + .await + .expect("wallet") + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile after reload"), + expected, + "{why}" + ); + } + } + + /// The successor scans to `height` and the host accepts that advance in + /// the successor's own epoch, which also pays any cursor it owed. + async fn accept_successor_advance( + manager: &Arc>, + wallet_id: WalletId, + height: u32, + ) { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let mut durable = manager.durable_cursors.lock().await; + let mut wm = manager.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(height); + durable.insert( + wallet_id, + crate::changeset::DurableCursor { + height, + advance_epoch: Some(0), + }, + ); + } + + /// The first thing `stores` received, which must be a record-only round + /// replacing the host's backfill record with an empty one. + fn first_store_is_an_empty_record( + stores: &[(WalletId, PlatformWalletChangeSet)], + ) -> crate::changeset::DashPayBackfillRecord { + let (_, first) = stores.first().expect("the publication stored something"); + assert!(first.core.is_none(), "a record-only round"); + let record = first + .dashpay_backfill + .clone() + .expect("the host's record is replaced before the wallet is published"); + assert!(record.is_empty()); + assert!(!record.has_extent()); + record + } + + /// The snapshot a registration reads is older than its lock. A same-id + /// registration can be published after it, establish a receival account, + /// store that account's coverage, advance and be removed, all before this + /// one publishes — so a snapshot with nothing to prune is no proof the + /// host holds nothing to re-arm. The entry that predecessor left makes + /// the publication replace the host's record anyway; otherwise this + /// wallet scans past the account unwatched and a crash restores the + /// account beside coverage that claims the range was watched + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn a_recreated_wallet_replaces_coverage_a_predecessor_stored_after_the_snapshot() { + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + // What the predecessor stored after this registration's snapshot. + let mut predecessor_record = crate::changeset::DashPayBackfillRecord::default(); + predecessor_record.record_pass(900, Some(200), [(owner, contact, 0, 200)]); + + let (manager, host, recreated, wallet_id) = recreate_on_host_at( + Default::default(), + 1_000, + Some(crate::changeset::DurableCursor { + height: 1_000, + advance_epoch: Some(2), + }), + ) + .await; + let replaced = first_store_is_an_empty_record(&host.stores.lock().unwrap()); + + accept_successor_advance(&manager, wallet_id, 1_500).await; + establish_on_recreated(&manager, &host, &recreated, owner, contact, 200).await; + assert_only_the_replaced_record_rearms_after_a_crash( + &manager, + wallet_id, + owner, + 1_500, + replaced, + predecessor_record, + ) + .await; + } + + /// Load trusts restored coverage only for the receival accounts it + /// actually restored. A host that kept the record but not the account + /// (its account writer missing or failing) would otherwise hand back + /// coverage for account A beside cursor 1000 with no A to watch; the scan + /// can then advance to 1500 before A is registered again, and the old + /// coverage would accept A over a range scanned without it. The load drops + /// that coverage, from the host too, before publishing + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn loading_drops_coverage_for_a_receival_account_the_wallet_did_not_restore() { + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let mut host_record = crate::changeset::DashPayBackfillRecord::default(); + host_record.record_pass(1_000, Some(200), [(owner, contact, 0, 200)]); + + // The host restores the wallet without A's receival account. + let (donor, _, wallet_id) = make_wallet().await; + let (wallet, mut managed) = { + let wm = donor.wallet_manager.read().await; + ( + wm.get_wallet(&wallet_id).expect("wallet").clone(), + wm.get_wallet_info(&wallet_id) + .expect("info") + .core_wallet + .clone(), + ) + }; + drop(donor); + managed.metadata.synced_height = 1_000; + let host = Arc::new(ReloadPersister { + wallet, + managed, + identities: std::collections::BTreeMap::new(), + backfill: host_record, + stores: Mutex::default(), + }); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + manager + .load_from_persistor() + .await + .expect("the snapshot must load"); + + first_store_is_an_empty_record(&host.stores.lock().unwrap()); + { + let wm = manager.wallet_manager.read().await; + assert!(wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .is_empty()); + } + + // The scan advances without A; then A is registered again. + accept_successor_advance(&manager, wallet_id, 1_500).await; + let loaded = manager.get_wallet(&wallet_id).await.expect("wallet"); + establish_on_recreated(&manager, &host, &loaded, owner, contact, 200).await; + assert_eq!( + loaded + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + Some(200), + "A is rewound for: 1001..1500 was scanned without it" + ); + } + + /// The load path's start state is just as old. A predecessor can register + /// a receival account the snapshot does not hold, store its coverage + /// beside a rewind to 100 and be removed before the load publishes. The + /// loaded cursor (1000) is then above the host's, so no reset is owed, + /// and the snapshot holds no coverage to prune. The entry the predecessor + /// left makes the load replace the host's record with the one it loaded + /// before the wallet is published (dashpay/platform#4302 review). + #[tokio::test] + async fn loading_a_wallet_replaces_coverage_a_predecessor_stored_after_the_snapshot() { + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let mut predecessor_record = crate::changeset::DashPayBackfillRecord::default(); + predecessor_record.record_pass(900, Some(200), [(owner, contact, 0, 200)]); + + let (donor, _, wallet_id) = make_wallet().await; + let (wallet, mut managed) = { + let wm = donor.wallet_manager.read().await; + ( + wm.get_wallet(&wallet_id).expect("wallet").clone(), + wm.get_wallet_info(&wallet_id) + .expect("info") + .core_wallet + .clone(), + ) + }; + drop(donor); + managed.metadata.synced_height = 1_000; + let host = Arc::new(ReloadPersister { + wallet, + managed, + identities: std::collections::BTreeMap::new(), + backfill: Default::default(), + stores: Mutex::default(), + }); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + manager.durable_cursors.lock().await.insert( + wallet_id, + crate::changeset::DurableCursor { + height: 100, + advance_epoch: Some(2), + }, + ); + manager + .load_from_persistor() + .await + .expect("the snapshot must load"); + let replaced = first_store_is_an_empty_record(&host.stores.lock().unwrap()); + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.metadata.synced_height, 1_000); + assert_eq!( + info.dashpay_backfill.unpersisted_cursor, None, + "the loaded cursor is above the host's, so no reset is owed" + ); + } + + accept_successor_advance(&manager, wallet_id, 1_500).await; + let loaded = manager.get_wallet(&wallet_id).await.expect("wallet"); + establish_on_recreated(&manager, &host, &loaded, owner, contact, 200).await; + assert_only_the_replaced_record_rearms_after_a_crash( + &manager, + wallet_id, + owner, + 1_500, + replaced, + predecessor_record, + ) + .await; + } + + /// A host whose first `load` returns a stale snapshot and every later + /// one the current state: what a loader sees when a same-id predecessor + /// changed the host after the loader read its start state. + struct SequencedHost { + stale: ReloadPersister, + current: ReloadPersister, + loads: std::sync::atomic::AtomicUsize, + /// When set, the second `load` (the loader's re-read) reports that + /// it has started and waits to be released. + reread_gate: Mutex, std::sync::mpsc::Receiver<()>)>>, + } + + impl PlatformWalletPersistence for SequencedHost { + fn store_commits_inline(&self) -> bool { + self.current.store_commits_inline() + } + + fn stores_dashpay_backfill(&self) -> bool { + self.current.stores_dashpay_backfill() + } + + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { + self.current.persistence_capabilities() + } + + fn store( + &self, + wallet_id: WalletId, + changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + self.current.store(wallet_id, changeset) + } + + fn flush(&self, wallet_id: WalletId) -> Result<(), PersistenceError> { + self.current.flush(wallet_id) + } + + fn load(&self) -> Result { + if self.loads.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0 { + self.stale.load() + } else { + let gate = self.reread_gate.lock().unwrap().take(); + if let Some((entered, release)) = gate { + let _ = entered.send(()); + let _ = release.recv(); + } + self.current.load() + } + } + } + + /// Coverage is keyed by `(owner, contact, index)`, not by xpub. A same-id + /// predecessor can replace a receival account's xpub under an unchanged + /// key and be removed before a loader that read the old snapshot + /// publishes it. Publishing that snapshot would restore the old xpub + /// with coverage scanned for it, and its scan would then vouch for the + /// key while the host holds the new xpub. With a predecessor's entry + /// present, the loader re-reads the host under the cursor lock and + /// publishes the current account and record instead + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn loading_after_a_predecessor_replaced_an_xpub_publishes_the_current_account() { + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let (donor, donor_persister, wallet_id) = make_wallet().await; + establish_receival_contact( + &donor, + &donor_persister, + wallet_id, + owner, + contact, + 200, + 200, + ) + .await; + // The stale snapshot: xpub A, covered from 200. + let mut stale_record = crate::changeset::DashPayBackfillRecord::default(); + stale_record.record_pass(1_000, Some(200), [(owner, contact, 0, 200)]); + let stale = reload_snapshot(&donor, wallet_id, owner, 1_000, Some(stale_record)).await; + // The host now: the same key under xpub B, and no coverage yet. + let mut current = reload_snapshot( + &donor, + wallet_id, + owner, + 1_000, + Some(crate::changeset::DashPayBackfillRecord::default()), + ) + .await; + drop(donor); + let xpub_a = stale + .wallet + .accounts + .dashpay_receival_accounts + .values() + .next() + .expect("the receival account") + .account_xpub; + let xpub_b = current + .wallet + .accounts + .standard_bip44_accounts + .get(&0) + .expect("bip44 account") + .account_xpub; + assert_ne!(xpub_a, xpub_b); + current + .wallet + .accounts + .dashpay_receival_accounts + .values_mut() + .next() + .expect("the receival account") + .account_xpub = xpub_b; + + let host = Arc::new(SequencedHost { + stale, + current, + loads: std::sync::atomic::AtomicUsize::new(0), + reread_gate: Mutex::new(None), + }); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + // The entry the removed predecessor left. + manager + .durable_cursors + .lock() + .await + .insert(wallet_id, crate::changeset::DurableCursor::at(1_000)); + manager + .load_from_persistor() + .await + .expect("the current state must load"); + + let wm = manager.wallet_manager.read().await; + let published = wm + .get_wallet(&wallet_id) + .expect("wallet") + .accounts + .dashpay_receival_accounts + .values() + .next() + .expect("the receival account") + .account_xpub; + assert_eq!(published, xpub_b, "the host's current xpub is published"); + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .covered_from(&owner, &contact, 0), + None, + "coverage scanned for the old xpub must not be published" + ); + assert_eq!( + host.loads.load(std::sync::atomic::Ordering::SeqCst), + 2, + "the stale snapshot is re-read once, under the lock" + ); + } + + /// The re-read is only current while nothing can change the host. A + /// same-id wallet can register, rotate an account's xpub and be removed + /// without moving the durable cursor entry, so no token can prove the + /// read is still current once the lock is released. The loader keeps the + /// cursor lock from the re-read through the wallet's publication instead. + /// A same-id registration needs that lock, so the first moment one could + /// run is after the re-read state is published (dashpay/platform#4302 + /// review). + #[tokio::test] + async fn the_loader_holds_the_cursor_lock_from_its_re_read_to_publication() { + use std::future::Future; + + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let (donor, donor_persister, wallet_id) = make_wallet().await; + establish_receival_contact( + &donor, + &donor_persister, + wallet_id, + owner, + contact, + 200, + 200, + ) + .await; + let stale = reload_snapshot(&donor, wallet_id, owner, 1_000, None).await; + let current = reload_snapshot(&donor, wallet_id, owner, 1_000, None).await; + drop(donor); + let (entered_tx, entered_rx) = std::sync::mpsc::channel(); + let (release_tx, release_rx) = std::sync::mpsc::channel(); + let host = Arc::new(SequencedHost { + stale, + current, + loads: std::sync::atomic::AtomicUsize::new(0), + reread_gate: Mutex::new(Some((entered_tx, release_rx))), + }); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + // The entry a removed same-id predecessor left. + manager + .durable_cursors + .lock() + .await + .insert(wallet_id, crate::changeset::DurableCursor::at(1_000)); + + let loader = tokio::spawn({ + let manager = Arc::clone(&manager); + async move { manager.load_from_persistor().await } + }); + // Bounded inside the closure: a running blocking task cannot be + // cancelled, so a loader that skipped the re-read would otherwise + // hang the test instead of failing it. + tokio::task::spawn_blocking(move || { + entered_rx.recv_timeout(std::time::Duration::from_secs(10)) + }) + .await + .expect("join") + .expect("the loader re-reads the host within the deadline"); + + // A same-id registration queues for the cursor lock while the + // re-read runs. + let mut registration = Box::pin(manager.durable_cursors.lock()); + let mut cx = std::task::Context::from_waker(std::task::Waker::noop()); + assert!( + registration.as_mut().poll(&mut cx).is_pending(), + "the loader holds the cursor lock during its re-read" + ); + release_tx.send(()).expect("release the re-read"); + + let guard = registration.await; + assert!( + manager + .wallet_manager + .read() + .await + .get_wallet(&wallet_id) + .is_some(), + "nothing may take the cursor lock between the re-read and the publication" + ); + drop(guard); + loader + .await + .expect("join") + .expect("the re-read state must load"); + assert_eq!(host.loads.load(std::sync::atomic::Ordering::SeqCst), 2); + } + + /// A backend whose `store` only buffers commits at a later `flush` that + /// can still fail and drop the round, so a "stored" record is not proof + /// of anything on disk. Such a backend gets no record: the pass rewinds + /// and marks in memory only — the pre-record guard — and no accepted + /// store moves the durable cursor. A relaunch then finds no record and + /// rewinds again: slow, never lossy (dashpay/platform#4302 review). + #[tokio::test] + async fn a_buffering_backend_keeps_the_backfill_record_in_memory_only() { + let (manager, persister, wallet_id) = make_wallet().await; + *persister.buffered.lock().unwrap() = true; + *persister.fail_flushes.lock().unwrap() = true; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let durable_before = manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .copied(); + persister.stores.lock().unwrap().clear(); + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let dashpay = wallet.identity().dashpay(); + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("reconcile"), + Some(100), + "the rewind still runs, in memory" + ); + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("reconcile"), + None, + "the in-memory guard stops a second rewind this session" + ); + assert!( + last_stored_record(&persister.stores.lock().unwrap()).is_none(), + "no record is handed to a backend whose store is not the commit" + ); + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .copied(), + durable_before, + "nothing it accepted is taken as durable" + ); + { + let wm = manager.wallet_manager.read().await; + let record = &wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill; + assert_eq!(record.covered_from(&owner, &contact, 0), Some(100)); + assert_eq!( + record.unpersisted_cursor, None, + "no debt for a record never stored" + ); + } + + // A relaunch finds no record on the host and rewinds again. + let snapshot = + reload_snapshot(&manager, wallet_id, owner, 1_000, Some(Default::default())).await; + let (restarted, _, _) = reload(snapshot).await; + assert_eq!( + restarted + .get_wallet(&wallet_id) + .await + .expect("wallet") + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile after reload"), + Some(100) + ); + } + + /// Inline is not enough: a backend that does not attest atomic + /// changesets may keep the coverage of a round and then reject the cursor + /// it vouches for, and a crash before the retry would restore coverage + /// beside the old cursor. Such a backend gets session-only coverage, like + /// a buffering one, so there is never a partial round to leave behind + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn a_non_atomic_backend_keeps_the_backfill_record_in_memory_only() { + let (manager, persister, wallet_id) = make_wallet().await; + *persister.non_atomic.lock().unwrap() = true; + *persister.partial_rounds.lock().unwrap() = true; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + persister.stores.lock().unwrap().clear(); + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let dashpay = wallet.identity().dashpay(); + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("reconcile"), + Some(100) + ); + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("reconcile"), + None, + "the in-memory guard still holds for the session" + ); + assert!( + last_stored_record(&persister.stores.lock().unwrap()).is_none(), + "no record reaches a backend that could keep it without its cursor" + ); + let snapshot = + reload_snapshot(&manager, wallet_id, owner, 1_000, Some(Default::default())).await; + let (restarted, _, _) = reload(snapshot).await; + assert_eq!( + restarted + .get_wallet(&wallet_id) + .await + .expect("wallet") + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile after reload"), + Some(100), + "a relaunch rewinds again: slow, never lossy" + ); + } + + /// [`ReloadPersister`] behind an inline backend that does not attest + /// atomic changesets. + struct NonAtomicHost(Arc); + + impl PlatformWalletPersistence for NonAtomicHost { + fn store_commits_inline(&self) -> bool { + true + } + + fn store( + &self, + wallet_id: WalletId, + changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + self.0.store(wallet_id, changeset) + } + + fn flush(&self, wallet_id: WalletId) -> Result<(), PersistenceError> { + self.0.flush(wallet_id) + } + + fn load(&self) -> Result { + self.0.load() + } + } + + /// A non-atomic backend that still holds a record from an earlier run + /// must not have it trusted, nor left behind: the load carries no record + /// and replaces the host's with an empty one before publishing. + #[tokio::test] + async fn loading_on_a_non_atomic_backend_invalidates_the_hosts_record() { + let (donor, donor_persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact( + &donor, + &donor_persister, + wallet_id, + owner, + contact, + 200, + 200, + ) + .await; + let mut record = crate::changeset::DashPayBackfillRecord::default(); + record.record_pass(800, None, [(owner, contact, 0, 200)]); + let host = Arc::new(reload_snapshot(&donor, wallet_id, owner, 800, Some(record)).await); + drop(donor); + + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new( + sdk, + Arc::new(NonAtomicHost(Arc::clone(&host))), + handler, + )); + manager + .load_from_persistor() + .await + .expect("the snapshot must load"); + first_store_is_an_empty_record(&host.stores.lock().unwrap()); + let wm = manager.wallet_manager.read().await; + assert!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .is_empty(), + "the restored record is not trusted in memory either" + ); + } + + /// Two outbound-account registrations of one contact must not interleave: + /// one that decrypted a payload before a rotation, and lost the race to + /// the build from the rotated request, must not overwrite the newer + /// account's durable xpub and pool and leave the newer marker beside them. + /// The absence check, the live-request check and the durable write run + /// under one guard, and the account and its marker are written in one + /// round (dashpay/platform#4302 review). + #[tokio::test] + async fn a_superseded_outbound_registration_writes_nothing_and_the_marker_stays_bound() { + use crate::wallet::identity::network::contacts::ExternalAccountRegistration; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + use key_wallet::account::account_collection::DashpayAccountKey; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0x01; 32]); + let shared_key = [0x55u8; 32]; + // A: the payload an older build decrypted. B: the rotated request. + let payload_a = encrypted_contact_xpub(&owner, &Identifier::from([0x02; 32]), &shared_key); + let payload_b = encrypted_contact_xpub(&owner, &contact, &shared_key); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + { + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 8, payload_b.clone(), 100, 0); + info.identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + } + let register = |payload: Vec| { + let iw = iw.clone(); + async move { + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &payload, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register") + } + }; + let key = DashpayAccountKey { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }; + let registrations = |stores: &[(WalletId, PlatformWalletChangeSet)]| { + stores + .iter() + .filter(|(_, cs)| !cs.account_registrations.is_empty()) + .count() + }; + + // A's payload against the live B request, account absent: refused, + // nothing written, nothing inserted. + persister.stores.lock().unwrap().clear(); + assert_eq!( + register(payload_a.clone()).await, + ExternalAccountRegistration::Superseded + ); + assert_eq!(registrations(&persister.stores.lock().unwrap()), 0); + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert!(!info + .core_wallet + .accounts + .dashpay_external_accounts + .contains_key(&key)); + } + + // B builds: the account and the marker naming B ride ONE round. + assert_eq!( + register(payload_b.clone()).await, + ExternalAccountRegistration::Built + ); + { + let stores = persister.stores.lock().unwrap(); + let round = stores + .iter() + .map(|(_, cs)| cs) + .find(|cs| !cs.account_registrations.is_empty()) + .expect("the account was stored"); + let bound = round + .contacts + .as_ref() + .and_then(|contacts| contacts.established.values().next()) + .expect("the marker rides the account's round"); + assert_eq!(bound.external_account_reference, Some(8)); + } + let b_xpub = { + let wm = manager.wallet_manager.read().await; + wm.get_wallet(&wallet_id) + .expect("wallet") + .accounts + .dashpay_external_accounts[&key] + .account_xpub + }; + + // A, having passed its early check before B was built, resumes: it + // finds B's account under the guard and writes nothing. + persister.stores.lock().unwrap().clear(); + assert_eq!( + register(payload_a).await, + ExternalAccountRegistration::AlreadyExisted + ); + assert_eq!(registrations(&persister.stores.lock().unwrap()), 0); + + // What a restart restores: B's account, with B's marker, trusted. + let (restarted, _, _) = + reload(reload_snapshot(&manager, wallet_id, owner, 1_000, None).await).await; + let wm = restarted.wallet_manager.read().await; + assert_eq!( + wm.get_wallet(&wallet_id) + .expect("wallet") + .accounts + .dashpay_external_accounts[&key] + .account_xpub, + b_xpub + ); + let established = wm + .get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone(); + assert_eq!(established.external_account_reference, Some(8)); + } + + /// Outbound registrations of one account are serialized: the durable + /// write runs under the wallet-manager write guard, so nothing else can + /// take the manager while it is held, and a competing registration that + /// passed its early read-guard check before the account existed finds it + /// under the guard and writes nothing (dashpay/platform#4302 review). + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] + async fn competing_outbound_registrations_are_serialized_under_the_write_guard() { + use crate::wallet::identity::network::contacts::ExternalAccountRegistration; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + use std::sync::atomic::Ordering; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + // A contact no other test registers, so the rendezvous holds only these. + let contact = Identifier::from([0x7E; 32]); + let shared_key = [0x55u8; 32]; + let payload = encrypted_contact_xpub(&owner, &contact, &shared_key); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + { + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 7, payload.clone(), 100, 0); + info.identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + } + let (release, gate) = std::sync::mpsc::channel::<()>(); + *persister.account_gate.lock().unwrap() = Some(gate); + persister.stores.lock().unwrap().clear(); + + // Both registrations pass the early read-guard check while the + // account is absent, and meet at the rendezvous before either asks + // for the write guard — so the loser can only learn that the account + // exists from the re-check under that guard. + crate::wallet::identity::network::contacts::registration_rendezvous::install(contact, 2); + let register = || { + let iw = iw.clone(); + let payload = payload.clone(); + tokio::spawn(async move { + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &payload, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register") + }) + }; + let first = register(); + let second = register(); + + // One of them holds the write guard inside its durable write. + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); + while !persister.gate_entered.load(Ordering::SeqCst) { + assert!( + std::time::Instant::now() < deadline, + "no registration reached its store" + ); + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + } + assert!( + manager.wallet_manager.try_write().is_err() + && manager.wallet_manager.try_read().is_err(), + "the manager stays locked while the registration's store is in progress" + ); + release.send(()).expect("release the store"); + + let mut outcomes = vec![first.await.expect("first"), second.await.expect("second")]; + crate::wallet::identity::network::contacts::registration_rendezvous::remove(&contact); + outcomes.sort_by_key(|outcome| *outcome == ExternalAccountRegistration::Built); + assert_eq!( + outcomes, + vec![ + ExternalAccountRegistration::AlreadyExisted, + ExternalAccountRegistration::Built + ] + ); + let account_rounds = persister + .stores + .lock() + .unwrap() + .iter() + .filter(|(_, cs)| !cs.account_registrations.is_empty()) + .count(); + assert_eq!(account_rounds, 1, "the losing registration wrote nothing"); + } + + /// The marker rides the account's round only where that round is + /// all-or-nothing. A host without atomic rounds can reject a rotated + /// account and its pool yet keep the round's contact rows; with the + /// marker in that round, the live request's marker would sit on disk + /// beside the previous account the host still holds, and a restart would + /// trust it. On such a host the round carries no marker, and the old one + /// stays stale so the sweep rebuilds (dashpay/platform#4302 review). + #[tokio::test] + async fn a_rejected_rotation_on_a_non_atomic_host_leaves_no_current_marker_on_disk() { + use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + use crate::wallet::identity::network::contacts::ExternalAccountRegistration; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + use key_wallet::account::account_collection::DashpayAccountKey; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0x01; 32]); + let shared_key = [0x55u8; 32]; + let payload_old = encrypted_contact_xpub(&owner, &contact, &shared_key); + let payload_new = + encrypted_contact_xpub(&owner, &Identifier::from([0x02; 32]), &shared_key); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + let establish = |reference: u32, payload: Vec| { + let iw = iw.clone(); + let persister = Arc::clone(&persister); + async move { + let p = WalletPersister::new(wallet_id, persister as _); + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + if info.identity_manager.managed_identity(&owner).is_none() { + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + } + let managed = info + .identity_manager + .managed_identity_mut(&owner) + .expect("managed"); + let marker = managed + .dashpay() + .established_contacts() + .get(&contact) + .and_then(|c| c.external_account_reference); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = + ContactRequest::new(contact, owner, 0, 0, reference, payload, 100, 0); + let mut established = EstablishedContact::new(contact, outgoing, incoming); + established.external_account_reference = marker; + managed.apply_established_contact(established); + } + }; + let register = |payload: Vec| { + let iw = iw.clone(); + async move { + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &payload, + zeroize::Zeroizing::new(shared_key), + ) + .await + } + }; + + // Built on an atomic host from request 7: account and marker together. + establish(7, payload_old.clone()).await; + assert_eq!( + register(payload_old).await.expect("first build"), + ExternalAccountRegistration::Built + ); + + // The contact rotates to request 8 and the sweep tears the account + // down in memory; the host still holds the old one. The host now + // has no atomic rounds and rejects the rebuilt account and pool. + establish(8, payload_new.clone()).await; + { + let key = DashpayAccountKey { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }; + let mut wm = manager.wallet_manager.write().await; + let (wallet, info) = wm.get_wallet_mut_and_info_mut(&wallet_id).expect("wallet"); + wallet.accounts.dashpay_external_accounts.remove(&key); + info.core_wallet + .accounts + .dashpay_external_accounts + .remove(&key); + } + *persister.non_atomic.lock().unwrap() = true; + *persister.reject_account_rounds.lock().unwrap() = true; + persister.stores.lock().unwrap().clear(); + assert!( + register(payload_new).await.is_err(), + "the host rejected the account" + ); + + let reached_disk = persister + .stores + .lock() + .unwrap() + .iter() + .filter_map(|(_, cs)| cs.contacts.as_ref()) + .flat_map(|contacts| contacts.established.values()) + .any(|established| established.external_account_reference == Some(8)); + assert!( + !reached_disk, + "the live request's marker must not reach disk beside the previous account" + ); + let wm = manager.wallet_manager.read().await; + let established = wm + .get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone(); + assert_eq!(established.external_account_reference, Some(7)); + assert!( + external_account_needs_rebuild(&established, true), + "the stale marker keeps the repair armed" + ); + } + + /// Where markers are kept on disk, registration is their only writer. A + /// completion that arrives late — after the account was torn down for a + /// rotation whose request reused the same ciphertext under a new + /// reference — must not stamp the new reference beside the obsolete + /// durable account; it only heals the broken flag while the bound marker + /// is still current (dashpay/platform#4302 review). + #[tokio::test] + async fn a_late_completion_never_stamps_a_durable_marker() { + use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + use crate::wallet::identity::network::contacts::ExternalAccountRegistration; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + use key_wallet::account::account_collection::DashpayAccountKey; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0x01; 32]); + let shared_key = [0x55u8; 32]; + let payload = encrypted_contact_xpub(&owner, &contact, &shared_key); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + let set_request = |reference: u32| { + let iw = iw.clone(); + let persister = Arc::clone(&persister); + let payload = payload.clone(); + async move { + let p = WalletPersister::new(wallet_id, persister as _); + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + if info.identity_manager.managed_identity(&owner).is_none() { + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + } + let managed = info + .identity_manager + .managed_identity_mut(&owner) + .expect("managed"); + let marker = managed + .dashpay() + .established_contacts() + .get(&contact) + .and_then(|c| c.external_account_reference); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = + ContactRequest::new(contact, owner, 0, 0, reference, payload, 100, 0); + let mut established = EstablishedContact::new(contact, outgoing, incoming); + established.external_account_reference = marker; + managed.apply_established_contact(established); + } + }; + let established = || { + let manager = Arc::clone(&manager); + async move { + let wm = manager.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone() + } + }; + + // Built from request 7: registration binds the marker. + set_request(7).await; + assert_eq!( + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &payload, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register"), + ExternalAccountRegistration::Built + ); + assert_eq!(established().await.external_account_reference, Some(7)); + + // A rotation to request 8 reuses the ciphertext; the sweep tears the + // account down in memory while the host still holds the old one. + set_request(8).await; + { + let key = DashpayAccountKey { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }; + let mut wm = manager.wallet_manager.write().await; + let (wallet, info) = wm.get_wallet_mut_and_info_mut(&wallet_id).expect("wallet"); + wallet.accounts.dashpay_external_accounts.remove(&key); + info.core_wallet + .accounts + .dashpay_external_accounts + .remove(&key); + } + persister.stores.lock().unwrap().clear(); + + // The first build's completion arrives now. + iw.dashpay() + .note_external_account_registered(&owner, &contact, &payload) + .await; + let after = established().await; + assert_eq!( + after.external_account_reference, + Some(7), + "the late completion must not stamp request 8" + ); + assert!(external_account_needs_rebuild(&after, true)); + let stamped = persister + .stores + .lock() + .unwrap() + .iter() + .filter_map(|(_, cs)| cs.contacts.as_ref()) + .flat_map(|contacts| contacts.established.values()) + .any(|row| row.external_account_reference == Some(8)); + assert!(!stamped, "nothing naming request 8 reached disk"); + + // Cold restoration keeps the stale marker, so the sweep rebuilds. + let (restarted, _, _) = + reload(reload_snapshot(&manager, wallet_id, owner, 1_000, None).await).await; + let restored = { + let wm = restarted.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone() + }; + assert_eq!(restored.external_account_reference, Some(7)); + assert!(external_account_needs_rebuild(&restored, true)); + + // While the bound marker is current, completion still heals the + // broken flag. + set_request(7).await; + { + let mut wm = manager.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .established_contact_mut(&contact) + .expect("contact") + .payment_channel_broken = true; + } + iw.dashpay() + .note_external_account_registered(&owner, &contact, &payload) + .await; + let healed = established().await; + assert!(!healed.payment_channel_broken); + assert_eq!(healed.external_account_reference, Some(7)); + } + + /// A marker may reach disk only where the account it names is durably + /// written with it. A buffering backend can accept the account and the + /// marker and then drop both at a failed `flush`, while a later contact + /// snapshot re-sends the marker alone; a backend without an account + /// writer acknowledges the account without keeping it. On either, every + /// contact row is written without the marker and a restored one is not + /// trusted, so the sweep rebuilds the account after a restart instead of + /// pairing the live request's marker with the previous account + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn outbound_markers_stay_in_memory_where_the_account_is_not_durably_written() { + use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + use crate::wallet::identity::network::contacts::ExternalAccountRegistration; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + + for buffered in [true, false] { + let (manager, persister, wallet_id) = make_wallet().await; + if buffered { + *persister.buffered.lock().unwrap() = true; + *persister.fail_flushes.lock().unwrap() = true; + } else { + *persister.no_account_writer.lock().unwrap() = true; + } + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0x01; 32]); + let shared_key = [0x55u8; 32]; + let payload = encrypted_contact_xpub(&owner, &contact, &shared_key); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + { + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = + ContactRequest::new(contact, owner, 0, 0, 8, payload.clone(), 100, 0); + info.identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .apply_established_contact(EstablishedContact::new( + contact, outgoing, incoming, + )); + } + persister.stores.lock().unwrap().clear(); + assert_eq!( + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &payload, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register"), + ExternalAccountRegistration::Built + ); + iw.dashpay() + .note_external_account_registered(&owner, &contact, &payload) + .await; + // A flush on the buffering backend fails and drops the round. + if buffered { + assert!(wallet.flush_persist().is_err()); + } + + // Memory keeps the self-heal marker; no row on disk carries it. + let established = { + let wm = manager.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone() + }; + assert_eq!(established.external_account_reference, Some(8)); + let marked_rows = persister + .stores + .lock() + .unwrap() + .iter() + .filter_map(|(_, cs)| cs.contacts.as_ref()) + .flat_map(|contacts| contacts.established.values()) + .filter(|row| row.external_account_reference.is_some()) + .count(); + assert_eq!(marked_rows, 0, "buffered: {buffered}"); + + // A cold reload from such a backend does not trust a marker it + // hands back, even one an earlier configuration wrote. + let snapshot = Arc::new(reload_snapshot(&manager, wallet_id, owner, 1_000, None).await); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let restarted = if buffered { + let host = Arc::new(BufferedHost { + inner: Arc::clone(&snapshot), + fail_flushes: std::sync::atomic::AtomicBool::new(false), + }); + let manager = Arc::new(PlatformWalletManager::new(sdk, host, handler)); + manager.load_from_persistor().await.expect("load"); + let wm = manager.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone() + } else { + let host = Arc::new(NonAtomicHost(Arc::clone(&snapshot))); + let manager = Arc::new(PlatformWalletManager::new(sdk, host, handler)); + manager.load_from_persistor().await.expect("load"); + let wm = manager.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone() + }; + assert_eq!( + restarted.external_account_reference, None, + "buffered: {buffered}" + ); + assert!(external_account_needs_rebuild(&restarted, true)); + } + } + + /// A wallet with one established contact whose outbound account is + /// registered and whose marker (7) is stamped: the state the restored- + /// marker validation runs against. + async fn wallet_with_a_marked_outbound_account() -> ( + Arc>, + WalletId, + Identifier, + Identifier, + key_wallet::account::account_collection::DashpayAccountKey, + ) { + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + use key_wallet::account::account_collection::DashpayAccountKey; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0x01; 32]); + let shared_key = [0x55u8; 32]; + let encrypted = encrypted_contact_xpub(&owner, &contact, &shared_key); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + { + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 7, encrypted.clone(), 100, 0); + info.identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + } + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &encrypted, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register outbound"); + iw.dashpay() + .note_external_account_registered(&owner, &contact, &encrypted) + .await; + let key = DashpayAccountKey { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }; + (manager, wallet_id, owner, contact, key) + } + + /// A restored outbound-account marker suppresses the startup rebuild, so + /// it must not vouch for a pool its account's xpub does not derive. A + /// host that replaced the xpub on rotation but kept the old pool's rows + /// would otherwise hand out pre-rotation addresses on the next send. + /// The load clears the marker for such a pool, and the contact sweep + /// rebuilds the account from the current xpub (dashpay/platform#4302 + /// review). + #[tokio::test] + async fn a_restored_marker_is_not_trusted_over_a_pool_its_current_xpub_does_not_derive() { + use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + + let (manager, wallet_id, owner, contact, key) = + wallet_with_a_marked_outbound_account().await; + + for (rotated, marker) in [(false, Some(7)), (true, None)] { + let mut snapshot = reload_snapshot(&manager, wallet_id, owner, 1_000, None).await; + if rotated { + // The host replaced the account's xpub and kept the old rows. + snapshot + .wallet + .accounts + .dashpay_external_accounts + .get_mut(&key) + .expect("outbound account") + .account_xpub = test_receiving_xpub(&contact, &owner); + } + let (restarted, _, _) = reload(snapshot).await; + let wm = restarted.wallet_manager.read().await; + let established = wm + .get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone(); + assert_eq!( + established.external_account_reference, marker, + "rotated: {rotated}" + ); + assert_eq!( + external_account_needs_rebuild(&established, true), + rotated, + "only a pool the current xpub does not derive is rebuilt" + ); + } + } + + /// Validating a restored pool derives every restored address, and only a + /// marker can be distrusted by it. So the load derives nothing for a + /// relationship restored without a marker, nor on a backend whose markers + /// are all cleared anyway because it cannot keep them durably; it still + /// validates a marker it would otherwise trust (dashpay/platform#4302 + /// review). + #[tokio::test] + async fn restored_pools_are_validated_only_where_a_marker_could_be_trusted() { + use crate::manager::pool_validations_on_this_thread as validations; + + let (manager, wallet_id, owner, contact, key) = + wallet_with_a_marked_outbound_account().await; + // Rotated, so a validation would distrust the marker if it ran. + let snapshot = |marker: bool| { + let manager = Arc::clone(&manager); + async move { + let mut snapshot = reload_snapshot(&manager, wallet_id, owner, 1_000, None).await; + snapshot + .wallet + .accounts + .dashpay_external_accounts + .get_mut(&key) + .expect("outbound account") + .account_xpub = test_receiving_xpub(&contact, &owner); + if !marker { + snapshot + .identities + .get_mut(&0) + .expect("owner") + .established_contact_mut(&contact) + .expect("contact") + .external_account_reference = None; + } + snapshot + } + }; + // No marker restored: nothing to validate. + let before = validations(); + let _ = reload(snapshot(false).await).await; + assert_eq!( + validations(), + before, + "a markerless relationship costs no derivation" + ); + + // A backend that cannot keep markers: all are cleared, none validated. + let before = validations(); + let host = Arc::new(NonAtomicHost(Arc::new(snapshot(true).await))); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let restarted = Arc::new(PlatformWalletManager::new(sdk, host, handler)); + restarted.load_from_persistor().await.expect("load"); + assert_eq!( + validations(), + before, + "an ineligible backend derives nothing" + ); + { + let wm = restarted.wallet_manager.read().await; + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .external_account_reference, + None, + "and its marker is still cleared" + ); + } + + // A marker an eligible backend would trust is still validated. + let before = validations(); + let _ = reload(snapshot(true).await).await; + assert!( + validations() > before, + "a trusted marker's pool is validated" + ); + } + + /// [`ReloadPersister`] behind a backend whose `store` only buffers, with + /// a `flush` that can be made to fail transiently. + struct BufferedHost { + inner: Arc, + fail_flushes: std::sync::atomic::AtomicBool, + } + + impl PlatformWalletPersistence for BufferedHost { + fn stores_dashpay_backfill(&self) -> bool { + true + } + + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { + crate::changeset::PersistenceCapabilities::ATOMIC_CHANGESETS + } + + fn store( + &self, + wallet_id: WalletId, + changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + self.inner.store(wallet_id, changeset) + } + + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + if self.fail_flushes.load(std::sync::atomic::Ordering::SeqCst) { + return Err(PersistenceError::backend_with_kind( + PersistenceErrorKind::Transient, + "injected flush failure", + )); + } + Ok(()) + } + + fn load(&self) -> Result { + self.inner.load() + } + } + + /// A buffering backend that hands back a record must have it replaced + /// with an empty one, committed with a `flush` before the wallet becomes + /// visible. A failed flush fails the load with the flush's own typed + /// error rather than publishing over a replacement that never reached + /// disk (dashpay/platform#4302 review). + #[tokio::test] + async fn a_buffering_backend_flushes_the_replaced_record_before_publishing() { + let (donor, donor_persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact( + &donor, + &donor_persister, + wallet_id, + owner, + contact, + 200, + 200, + ) + .await; + let mut record = crate::changeset::DashPayBackfillRecord::default(); + record.record_pass(800, None, [(owner, contact, 0, 200)]); + let snapshot = Arc::new(reload_snapshot(&donor, wallet_id, owner, 800, Some(record)).await); + drop(donor); + let host = Arc::new(BufferedHost { + inner: Arc::clone(&snapshot), + fail_flushes: std::sync::atomic::AtomicBool::new(true), + }); + let manager_over = |host: &Arc| { + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + Arc::new(PlatformWalletManager::new(sdk, Arc::clone(host), handler)) + }; + + let manager = manager_over(&host); + let error = match manager.load_from_persistor().await { + Ok(_) => panic!("the replacement did not commit, so the wallet is not published"), + Err(error) => error, + }; + assert!( + matches!( + &error, + crate::error::PlatformWalletError::PersisterFlush(source) + if source.kind() == Some(PersistenceErrorKind::Transient) + ), + "the flush failure keeps its own type and kind: {error:?}" + ); + assert!(manager.get_wallet(&wallet_id).await.is_none()); + + host.fail_flushes + .store(false, std::sync::atomic::Ordering::SeqCst); + snapshot.stores.lock().unwrap().clear(); + let manager = manager_over(&host); + manager + .load_from_persistor() + .await + .expect("the replacement commits"); + first_store_is_an_empty_record(&snapshot.stores.lock().unwrap()); + } + + /// The replacement can also put coverage BACK: the loaded record covers + /// account A while a same-id predecessor scanned the host's cursor to + /// 1000 without it. Coverage from the snapshot beside cursor 1000 would + /// claim (800, 1000] was watched for A, and a crash straight after the + /// replacement would restore exactly that. So the replacement carries the + /// loaded cursor (800) as a rewind in the same round, and the durable + /// entry follows it — nothing is left owed in memory only + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn loading_a_covering_record_below_the_hosts_cursor_writes_the_loaded_cursor_with_it() { + let (donor, donor_persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact( + &donor, + &donor_persister, + wallet_id, + owner, + contact, + 200, + 200, + ) + .await; + let mut record = crate::changeset::DashPayBackfillRecord::default(); + record.record_pass(800, None, [(owner, contact, 0, 200)]); + let snapshot = reload_snapshot(&donor, wallet_id, owner, 800, Some(record.clone())).await; + drop(donor); + + let host = Arc::new(snapshot); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + manager.durable_cursors.lock().await.insert( + wallet_id, + crate::changeset::DurableCursor { + height: 1_000, + advance_epoch: Some(2), + }, + ); + manager + .load_from_persistor() + .await + .expect("the snapshot must load"); + + { + let stores = host.stores.lock().unwrap(); + let (_, first) = stores.first().expect("the replacement was stored"); + assert_eq!(first.dashpay_backfill.as_ref(), Some(&record)); + let core = first + .core + .as_ref() + .expect("the loaded cursor rides the replacement round"); + assert_eq!(core.synced_height, Some(800)); + assert!(core.synced_height_is_rewind); + } + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .copied(), + Some(crate::changeset::DurableCursor::at(800)), + "the entry follows the committed cursor" + ); + let wm = manager.wallet_manager.read().await; + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .unpersisted_cursor, + None, + "nothing is owed: the cursor went to the host with the coverage" + ); + } + + /// The host snapshot a registration reads comes before it takes the + /// durable-cursor lock, and a same-id predecessor still registered then + /// can have a commit accepted in between. Under the lock the entry that + /// commit left is what the host holds now, so it wins over the snapshot: + /// the seed is 1000, not the snapshot's 100, and the built cursor is owed + /// against it (dashpay/platform#4302 review). + #[tokio::test] + async fn a_recreated_wallet_seeds_from_the_cursor_a_predecessor_left_not_the_snapshot() { + let (manager, _host, _recreated, wallet_id) = recreate_on_host_at( + Default::default(), + 100, + Some(crate::changeset::DurableCursor { + height: 1_000, + advance_epoch: Some(2), + }), + ) + .await; + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .map(|cursor| cursor.height), + Some(1_000), + "the predecessor's accepted commit, not the stale snapshot" + ); + let wm = manager.wallet_manager.read().await; + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .unpersisted_cursor, + Some(100), + "the built cursor is owed against the host's real cursor" + ); + } + + /// A snapshot with no host row is not proof the host holds nothing: a + /// same-id registration can have created the row, advanced it and been + /// removed between the snapshot and this publication. The entry those + /// writes left wins, and the lower built cursor is owed against it. + #[tokio::test] + async fn a_leftover_cursor_entry_wins_without_a_host_row_and_the_lower_built_cursor_is_owed() { + let (donor, _, wallet_id) = make_wallet().await; + drop(donor); + let persister = Arc::new(RecordingPersister::default()); // `load` returns no rows + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new( + sdk, + Arc::clone(&persister), + handler, + )); + let entry = crate::changeset::DurableCursor { + height: 1_000, + advance_epoch: Some(2), + }; + manager + .durable_cursors + .lock() + .await + .insert(wallet_id, entry); + let seed = Mnemonic::from_phrase(TEST_MNEMONIC) + .expect("valid mnemonic") + .to_seed(""); + let wallet = manager + .create_wallet_from_seed_bytes( + Network::Testnet, + &seed, + WalletAccountCreationOptions::Default, + Some(101), + ) + .await + .expect("create"); + assert_eq!(wallet.wallet_id(), wallet_id); + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .copied(), + Some(entry), + "the accepted entry is what the host holds" + ); + let wm = manager.wallet_manager.read().await; + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .unpersisted_cursor, + Some(100), + "the lower built cursor is owed" + ); + } + + /// The load path has the same window: the start state is read before + /// the lock, and a predecessor still registered then can have a commit + /// accepted since. The entry wins and the lower loaded cursor is owed, + /// so a contact forward-covered at the loaded cursor is never recorded + /// beside the host's higher one without a reset. + #[tokio::test] + async fn loading_a_wallet_owes_a_loaded_cursor_below_the_one_a_predecessor_left() { + let (donor, _, wallet_id) = make_wallet().await; + let (wallet, mut managed) = { + let wm = donor.wallet_manager.read().await; + ( + wm.get_wallet(&wallet_id).expect("wallet").clone(), + wm.get_wallet_info(&wallet_id) + .expect("info") + .core_wallet + .clone(), + ) + }; + drop(donor); + managed.metadata.synced_height = 100; + let host = Arc::new(ReloadPersister { + wallet, + managed, + identities: std::collections::BTreeMap::new(), + backfill: Default::default(), + stores: Mutex::default(), + }); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + manager.durable_cursors.lock().await.insert( + wallet_id, + crate::changeset::DurableCursor { + height: 1_000, + advance_epoch: Some(2), + }, + ); + manager + .load_from_persistor() + .await + .expect("the snapshot must load"); + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .map(|cursor| cursor.height), + Some(1_000) + ); + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.metadata.synced_height, 100); + assert_eq!(info.dashpay_backfill.unpersisted_cursor, Some(100)); + } + + /// Coverage for a receival account that no longer exists stops being + /// true the moment its scripts stop being watched. A pass with no + /// contact to handle must still drop it and store the record, not + /// return early and leave it vouching on disk (dashpay/platform#4302 + /// review). + #[tokio::test] + async fn coverage_for_a_removed_receival_account_is_pruned_and_stored() { + use key_wallet::account::account_collection::DashpayAccountKey; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let dashpay = wallet.identity().dashpay(); + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("rewind"), + Some(100) + ); + + { + let mut wm = manager.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.core_wallet + .accounts + .dashpay_receival_accounts + .remove(&DashpayAccountKey { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }); + } + persister.stores.lock().unwrap().clear(); + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("prune"), + None + ); + { + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the pruned record is stored"); + assert!(round.dashpay_backfill.as_ref().unwrap().is_empty()); + } + let wm = manager.wallet_manager.read().await; + assert!(wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .is_empty()); + } + + /// Registering a receival account bumps the wallet's account generation, + /// which the filter pipeline compares per batch: a batch scanned before + /// the registration can no longer certify coverage for the new account + /// set, so forward coverage recorded for the account is backed by a scan + /// that actually watched it (dashpay/platform#4302 review). + #[tokio::test] + async fn receival_registration_invalidates_in_flight_scans() { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let (manager, _persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let generation = |wm: &key_wallet_manager::WalletManager< + crate::wallet::platform_wallet::PlatformWalletInfo, + >| { + WalletInfoInterface::account_generation(wm.get_wallet_info(&wallet_id).expect("info")) + }; + let before = generation(&*manager.wallet_manager.read().await); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + wallet + .identity() + .dashpay() + .register_contact_account(&owner, &contact, 0, test_receiving_xpub(&owner, &contact)) + .await + .expect("register"); + let wm = manager.wallet_manager.read().await; + assert_ne!(generation(&wm), before); + assert_eq!( + key_wallet_manager::WalletInterface::wallet_account_generation(&*wm, &wallet_id), + generation(&wm), + "the engine sees the forwarded generation, not the trait's constant 0" + ); + } + + /// A backend that panics inside the record round's `store()` unwinds + /// through the reconcile and releases its locks. The pass's coverage + /// was only staged, and the cursor it owed was recorded before the + /// store ran, so the next round — a forward-covered contact with no + /// cursor of its own — carries that cursor beside both contacts' + /// coverage instead of storing coverage over the host's high-water + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn a_panicking_record_store_leaves_no_coverage_and_keeps_the_cursor_it_owed() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let first = Identifier::from([0xBB; 32]); + let later = Identifier::from([0xCC; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, first, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + persister.stores.lock().unwrap().clear(); + *persister.panic_next_store.lock().unwrap() = true; + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let panicking = { + let wallet = Arc::clone(&wallet); + tokio::spawn( + async move { wallet.identity().dashpay().reconcile_dashpay_rescan().await }, + ) + }; + assert!( + panicking.await.expect_err("the store panicked").is_panic(), + "the injected panic propagates" + ); + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert!( + info.dashpay_backfill + .covered_from(&owner, &first, 0) + .is_none(), + "staged coverage never reached memory" + ); + assert_eq!(info.dashpay_backfill.unpersisted_cursor, Some(100)); + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .map(|cursor| cursor.height), + Some(1_000), + "the host still holds its high-water" + ); + } + + establish_receival_contact(&manager, &persister, wallet_id, owner, later, 200, 200).await; + persister.stores.lock().unwrap().clear(); + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("next pass"); + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the record is stored"); + let core = round + .core + .as_ref() + .expect("the owed cursor rides the round"); + assert_eq!(core.synced_height, Some(100)); + assert!(core.synced_height_is_rewind); + let record = round.dashpay_backfill.as_ref().unwrap(); + assert_eq!(record.covered_from(&owner, &first, 0), Some(100)); + assert_eq!(record.covered_from(&owner, &later, 0), Some(200)); + } + + /// A record round that fails to store must not leave its coverage + /// behind in memory: a later round (a forward-covered contact, no + /// cursor of its own) would then store the whole record — this + /// contact included — beside a durable cursor still at its high-water, + /// and the next launch would skip the rescan (dashpay/platform#4302 + /// review). On failure the record is put back, the pass's marks are + /// dropped, and the cursor the round owed rides the next successful + /// round. + #[tokio::test] + async fn a_failed_record_round_is_retried_with_the_cursor_it_owed() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let first = Identifier::from([0xBB; 32]); + let later = Identifier::from([0xCC; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, first, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + persister.stores.lock().unwrap().clear(); + *persister.fail_stores.lock().unwrap() = true; + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass whose store fails"), + Some(100), + "the in-memory rewind still runs the rescan" + ); + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.synced_height(), 100); + assert!( + info.dashpay_backfill + .covered_from(&owner, &first, 0) + .is_none(), + "coverage that never reached disk must not stay in memory" + ); + assert_eq!(info.dashpay_backfill.unpersisted_cursor, Some(100)); + assert!( + !info + .identity_manager + .managed_identity(&owner) + .unwrap() + .dashpay() + .rescan_triggered + .contains(&first), + "the pass's mark is dropped so the next pass re-attempts" + ); + } + + // Another contact turns up, forward-covered at the rewound cursor — + // exactly the round that used to store `first` without a cursor. + *persister.fail_stores.lock().unwrap() = false; + establish_receival_contact(&manager, &persister, wallet_id, owner, later, 200, 200).await; + persister.stores.lock().unwrap().clear(); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("retry pass"), + None, + "nothing is below the rewound cursor, so no new rewind" + ); + { + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the retry stores the record"); + assert_eq!( + round.core.as_ref().and_then(|core| core.synced_height), + Some(100), + "the retry carries the cursor the failed round owed" + ); + let record = round.dashpay_backfill.as_ref().unwrap(); + assert_eq!(record.covered_from(&owner, &first, 0), Some(100)); + assert_eq!(record.covered_from(&owner, &later, 0), Some(200)); + assert_eq!( + (record.floor, record.rewound_from), + (100, 1_000), + "the retry keeps the failed rewind's extent" + ); + assert!( + record.is_pending(100), + "the backfill still reads as climbing" + ); + assert!( + round.core.as_ref().unwrap().synced_height_is_rewind, + "the owed cursor rides as a rewind" + ); + } + let wm = manager.wallet_manager.read().await; + assert_eq!( + wm.get_wallet_info(&wallet_id) + .unwrap() + .dashpay_backfill + .unpersisted_cursor, + None + ); + } + + /// A process-local mark is not coverage. A contact covered from 100 + /// whose checkpoint then drops to 50 (an older reciprocal request) must + /// be rewound to 50 — not have the lower checkpoint recorded as covered + /// while the range 50–100 was never scanned for it + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn a_marked_contact_whose_checkpoint_dropped_is_rewound_not_recorded_lower() { + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("first pass"), + Some(100) + ); + // The scan climbs; then the reciprocal request turns out to be older. + set_synced_height(&manager, wallet_id, 400).await; + { + let mut wm = manager.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 50, 0); + let managed = info + .identity_manager + .managed_identity_mut(&owner) + .expect("managed"); + managed.apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + assert!( + managed.dashpay().rescan_triggered.contains(&contact), + "the mark survives the request change on this tree" + ); + } + persister.stores.lock().unwrap().clear(); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass after the drop"), + Some(50), + "a marked contact below its recorded height re-arms through the candidate path" + ); + assert_eq!(synced_height(&manager, wallet_id).await, 50); + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the deeper rewind stores its record"); + assert_eq!( + round.core.as_ref().and_then(|core| core.synced_height), + Some(50) + ); + let record = round.dashpay_backfill.as_ref().unwrap(); + assert_eq!(record.covered_from(&owner, &contact, 0), Some(50)); + assert_eq!((record.floor, record.rewound_from), (50, 1_000)); + } + + /// While the manager's persistence fault is latched the adapter holds + /// the durable cursor back at the last fully persisted height. A record + /// round written then would advance it past rows that never landed, so + /// the reconcile rewinds in memory but stores nothing — the next launch + /// re-runs the backfill, the pre-record behaviour + /// (dashpay/platform#4302 review). The latch never clears, so the pass's + /// coverage is kept for the session: once the scan climbs back past the + /// contact, later sweeps must not rewind to it again. + #[tokio::test] + async fn a_latched_persistence_fault_keeps_the_record_off_disk() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + wallet + .identity() + .sync_fault + .store(true, std::sync::atomic::Ordering::Relaxed); + persister.stores.lock().unwrap().clear(); + + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass under a latched fault"), + Some(100), + "the in-memory rewind still runs the rescan" + ); + assert!( + persister.stores.lock().unwrap().is_empty(), + "nothing reaches the persister while the fault is latched" + ); + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.synced_height(), 100); + assert_eq!( + info.dashpay_backfill.covered_from(&owner, &contact, 0), + Some(100), + "the pass's coverage is kept for the session" + ); + assert_eq!( + info.dashpay_backfill.unpersisted_cursor, + Some(100), + "beside the cursor it still owes the host" + ); + } + + // The rescan climbs back past the contact in memory; the latched + // adapter records no durable advance. Later sweeps must not rewind to + // the contact again. + { + let mut wm = manager.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(1_000); + } + for _ in 0..2 { + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("later pass under the latched fault"), + None, + "a contact this session already backfilled must not be rewound to again" + ); + } + assert!(persister.stores.lock().unwrap().is_empty()); + let wm = manager.wallet_manager.read().await; + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .core_wallet + .synced_height(), + 1_000 + ); + } + + /// A record round the backend refuses outright (a non-transient failure) + /// would be refused again. Retrying it every sweep left the contact + /// uncovered, so each sweep after the scan climbed back past it rewound + /// to it again (dashpay/platform#4302 review). The coverage is kept for + /// the session beside the cursor it owes, and no further record round is + /// attempted. + #[tokio::test] + async fn a_permanently_refused_record_is_kept_for_the_session_without_rewinding_again() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + persister.stores.lock().unwrap().clear(); + *persister.refuse_stores.lock().unwrap() = true; + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass whose store is refused"), + Some(100), + "the in-memory rewind still runs the rescan" + ); + assert_eq!( + persister + .refused_attempts + .load(std::sync::atomic::Ordering::SeqCst), + 1 + ); + { + let wm = manager.wallet_manager.read().await; + let record = &wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill; + assert_eq!( + record.covered_from(&owner, &contact, 0), + Some(100), + "the refused round's coverage is kept for the session" + ); + assert_eq!( + record.unpersisted_cursor, + Some(100), + "beside the cursor it owes" + ); + assert!(record.store_refused); + } + + // The rescan climbs back past the contact over several advances, + // with a sweep after each. None may rewind again or retry the store. + for height in [400, 700, 1_000] { + { + let mut wm = manager.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(height); + } + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("later pass"), + None, + "no rewind at {height} for a contact this session already backfilled" + ); + } + assert_eq!( + persister + .refused_attempts + .load(std::sync::atomic::Ordering::SeqCst), + 1, + "a refused record round is not retried" + ); + assert!(persister.stores.lock().unwrap().is_empty()); + } + + /// The host lags memory (a healthy backlog: the adapter has not yet + /// committed the advances that took memory to 1000). A rewind to 800 + /// must not write 800 — that would claim the rows between the host's + /// cursor and 800 are on disk when they are still queued. The host's + /// cursor is already below the floor, so the record is stored with no + /// cursor, and nothing records a durable height that was never written + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn a_rewind_above_the_hosts_cursor_writes_no_cursor() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 800, 800).await; + set_synced_height(&manager, wallet_id, 100).await; + // Memory advances to 1000; the host still holds 100. + { + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let mut wm = wallet.identity().wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(1_000); + } + persister.stores.lock().unwrap().clear(); + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + Some(800), + "memory rewinds to the floor" + ); + { + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the record is stored"); + assert!( + round.core.is_none(), + "the host's 100 is already below the floor; 800 must not be written" + ); + assert_eq!( + round + .dashpay_backfill + .as_ref() + .unwrap() + .covered_from(&owner, &contact, 0), + Some(800) + ); + } + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .map(|cursor| cursor.height), + Some(100), + "the recorded durable cursor is still what the host holds" + ); + } + + /// `spv_rescan_filters_blocking` lowers the cursor in memory only. The + /// next record round must carry that reset, or it stores coverage + /// justified by a rewind the host never saw (dashpay/platform#4302 + /// review): here a contact forward-covered at the reset cursor produces + /// a round with no rewind of its own, which now carries the owed 100. + #[tokio::test] + async fn a_manual_rescan_reset_rides_the_next_record_round() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + set_synced_height(&manager, wallet_id, 1_000).await; + { + let manager = Arc::clone(&manager); + tokio::task::spawn_blocking(move || { + assert!(manager.spv_rescan_filters_blocking(&wallet_id, 100)); + }) + .await + .expect("manual rescan"); + } + assert_eq!(synced_height(&manager, wallet_id).await, 100); + assert_eq!( + rewind_epoch(&manager, wallet_id).await, + 1, + "the reset arms the rewind barrier: advances queued before it are not stored after it" + ); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 200, 200).await; + persister.stores.lock().unwrap().clear(); + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None, + "the contact is forward-covered at the reset cursor" + ); + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the record is stored"); + let core = round.core.as_ref().expect("the owed reset rides the round"); + assert_eq!(core.synced_height, Some(100)); + assert!(core.synced_height_is_rewind); + assert_eq!( + round + .dashpay_backfill + .as_ref() + .unwrap() + .covered_from(&owner, &contact, 0), + Some(200) + ); + } + + /// Reset the cursor to `from` the way the app's "rescan blockchain" + /// does, then model the rescan climbing back to `to` with the adapter + /// committing an advance from rewind epoch `host_epoch`. + async fn reset_then_climb( + manager: &Arc>, + wallet_id: WalletId, + from: u32, + to: u32, + host_epoch: u64, + ) { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + { + let manager = Arc::clone(manager); + tokio::task::spawn_blocking(move || { + assert!(manager.spv_rescan_filters_blocking(&wallet_id, from)); + }) + .await + .expect("manual rescan"); + } + let mut durable = manager.durable_cursors.lock().await; + let mut wm = manager.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(to); + durable.insert( + wallet_id, + crate::changeset::DurableCursor { + height: to, + advance_epoch: Some(host_epoch), + }, + ); + } + + /// dashpay/platform#4302 device check: a manual reset owes the host the + /// reset height, but the adapter persists the rescan's own advances as it + /// climbs, so once the host has accepted one from the reset's epoch the + /// debt is paid. A record round after the climb must not write the old + /// reset height — that dragged the host back to it until the next block + /// advance, and a kill in that window re-scanned from it. + #[tokio::test] + async fn an_owed_cursor_the_host_has_climbed_past_is_dropped_not_written() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + set_synced_height(&manager, wallet_id, 1_000).await; + reset_then_climb(&manager, wallet_id, 100, 1_000, 1).await; + assert_eq!(rewind_epoch(&manager, wallet_id).await, 1); + + establish_receival_contact( + &manager, &persister, wallet_id, owner, contact, 1_200, 1_200, + ) + .await; + persister.stores.lock().unwrap().clear(); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None + ); + { + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the contact is recorded"); + assert!( + round.core.is_none(), + "no stale reset cursor rides the round: {:?}", + round.core.as_ref().and_then(|core| core.synced_height) + ); + } + let wm = manager.wallet_manager.read().await; + let record = &wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill; + assert_eq!(record.unpersisted_cursor, None, "the paid debt is gone"); + assert_eq!(record.covered_from(&owner, &contact, 0), Some(1_200)); + } + + /// The other side: an advance the host accepted from BEFORE the reset + /// says nothing about the range the reset re-opened, so the debt stands + /// and rides the next record round. + #[tokio::test] + async fn an_owed_cursor_is_still_paid_when_the_host_only_advanced_before_the_reset() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + set_synced_height(&manager, wallet_id, 1_000).await; + // The host's last accepted advance is from epoch 0, before the reset + // (epoch 1); memory has not moved since the reset. + reset_then_climb(&manager, wallet_id, 100, 100, 0).await; + manager + .durable_cursors + .lock() + .await + .get_mut(&wallet_id) + .expect("seeded") + .height = 1_000; + + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 200, 200).await; + persister.stores.lock().unwrap().clear(); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"); + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the record is stored"); + let core = round.core.as_ref().expect("the owed reset rides the round"); + assert_eq!(core.synced_height, Some(100)); + assert!(core.synced_height_is_rewind); + } + + async fn synced_height_of( + manager: &Arc>, + wallet_id: WalletId, + ) -> u32 { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + let wm = manager.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .expect("info") + .core_wallet + .synced_height() } /// A `Sent` payment must advance `Pending → Confirmed` once its @@ -7324,6 +11688,8 @@ mod tests { wallet_id: real.wallet_id, asset_locks: Arc::clone(&real.asset_locks), persister: real.persister.clone(), + sync_fault: Arc::clone(&real.sync_fault), + durable_cursors: Arc::clone(&real.durable_cursors), broadcaster: Arc::new(GatedRejectingBroadcaster { entered, release }), sdk_writer: Arc::clone(&real.sdk_writer), dpns_operation_gate: Arc::clone(&real.dpns_operation_gate), @@ -7363,6 +11729,8 @@ mod tests { wallet_id: real.wallet_id, asset_locks: Arc::clone(&real.asset_locks), persister: real.persister.clone(), + sync_fault: Arc::clone(&real.sync_fault), + durable_cursors: Arc::clone(&real.durable_cursors), broadcaster: Arc::new(GatedBroadcaster { entered, release }), sdk_writer: Arc::clone(&real.sdk_writer), dpns_operation_gate: Arc::clone(&real.dpns_operation_gate), @@ -7383,6 +11751,8 @@ mod tests { wallet_id: real.wallet_id, asset_locks: Arc::clone(&real.asset_locks), persister: real.persister.clone(), + sync_fault: Arc::clone(&real.sync_fault), + durable_cursors: Arc::clone(&real.durable_cursors), broadcaster: Arc::new(AcceptingBroadcaster), sdk_writer: Arc::clone(&real.sdk_writer), dpns_operation_gate: Arc::clone(&real.dpns_operation_gate), @@ -7418,6 +11788,8 @@ mod tests { wallet_id: real.wallet_id, asset_locks: Arc::clone(&real.asset_locks), persister: real.persister.clone(), + sync_fault: Arc::clone(&real.sync_fault), + durable_cursors: Arc::clone(&real.durable_cursors), broadcaster: Arc::new(RejectingBroadcaster), sdk_writer: Arc::clone(&real.sdk_writer), dpns_operation_gate: Arc::clone(&real.dpns_operation_gate), diff --git a/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/dashpay.rs b/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/dashpay.rs index fdb698f7cbf..ab22da79cd7 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/dashpay.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/dashpay.rs @@ -134,13 +134,19 @@ pub struct DashPayState { /// recurring sweep does not re-lower the height every pass — which would /// reset the in-flight backfill and prevent it from ever completing. /// - /// In-memory only (never persisted): a relaunch clears it, and because - /// `synced_height` is restored at its monotonic high-water, an interrupted - /// backfill is re-triggered on the next launch — self-healing. The cost of - /// that reset is one historical re-match per launch while any contact is - /// funded below the tip; the compact filters are reused from disk (not - /// re-downloaded), so it is cheap. A persisted breadcrumb could make the - /// backfill durable across a crash if that ever becomes necessary. + /// This set is the in-memory half of the guard and is never persisted: a + /// relaunch clears it. Its durable half is the wallet's + /// [`DashPayBackfillRecord`](crate::changeset::DashPayBackfillRecord), + /// which the rescan reconcile writes on the same persistence round as the + /// lowered cursor and consults on the next launch — the cursor it lowers + /// is durable too (the host persists every height the rescan climbs + /// through), so without the record every fresh process restored a cursor + /// inside the climb and rewound again (dashpay/platform#4302). The two + /// compose per contact: a mark here says "handled this process", an entry + /// there says "covered from this height"; the request-state transitions + /// that clear a mark here leave the record alone, and the reconcile + /// re-arms a recorded contact only when its checkpoint has dropped below + /// the height it was recorded at. pub rescan_triggered: BTreeSet, /// DashPay contact-crypto ops the unattended background sweep enqueued for diff --git a/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/mod.rs b/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/mod.rs index a7c3eafaaff..436dbaaabf6 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/mod.rs @@ -156,7 +156,8 @@ impl ManagedIdentity { /// Mutable access to the per-session contact-rescan guard set. /// /// In-memory only — never persisted; see the field docs on - /// [`DashPayState::rescan_triggered`] for the self-healing contract. + /// [`DashPayState::rescan_triggered`] for how it composes with the + /// wallet's durable backfill record. pub fn dashpay_rescan_triggered_mut( &mut self, ) -> &mut std::collections::BTreeSet { diff --git a/packages/rs-platform-wallet/src/wallet/identity/types/dashpay/established_contact.rs b/packages/rs-platform-wallet/src/wallet/identity/types/dashpay/established_contact.rs index 62c74084b8a..114265436f9 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/types/dashpay/established_contact.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/types/dashpay/established_contact.rs @@ -82,9 +82,14 @@ pub struct EstablishedContact { /// lets the sweep detect a stale account and tear it down + rebuild. /// /// Reset to `None` on (re-)establish and on rotation (the account must be - /// rebuilt from the new key material). A cold restart that does not - /// restore this field leaves it `None`, which conservatively forces one - /// rebuild on the next sweep — self-healing either way. + /// rebuilt from the new key material). Persisted on the contact's + /// established rows (`ContactRequestFFI::external_account_reference`, the + /// Kotlin `dashpay_contact_requests.externalAccountReference` column) so + /// a cold restart restores it and a healthy account is NOT rebuilt. A + /// host that does not carry it leaves it `None`, which conservatively + /// forces one rebuild on the next sweep — self-healing, but on every + /// launch; the rebuild used to also rewind the filter-scan cursor, which + /// is the per-launch re-walk of dashpay/platform#4302. #[cfg_attr(feature = "serde", serde(default))] pub external_account_reference: Option, } diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index fbfefcb2f9d..56a28fa4370 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -63,10 +63,35 @@ impl WalletPersister { Self { wallet_id, inner } } - pub(crate) fn store(&self, changeset: PlatformWalletChangeSet) -> Result<(), PersistenceError> { + /// Store `changeset` for this wallet. + /// + /// On a backend that cannot keep outbound-account markers durably (see + /// [`persists_outbound_account_markers`](crate::changeset::persists_outbound_account_markers)), + /// every established-contact row is written without its marker: a marker + /// that reached disk without its account — through a buffered round that + /// was later dropped, or a host that skips account writes — would be + /// trusted on restart beside the previous account. The in-memory marker is + /// untouched, so the running process keeps its rotation self-heal. + pub(crate) fn store( + &self, + mut changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + if !self.persists_outbound_account_markers() { + if let Some(contacts) = changeset.contacts.as_mut() { + for established in contacts.established.values_mut() { + established.external_account_reference = None; + } + } + } self.inner.store(self.wallet_id, changeset) } + /// Whether the backend keeps outbound-account markers durably (see + /// [`persists_outbound_account_markers`](crate::changeset::persists_outbound_account_markers)). + pub(crate) fn persists_outbound_account_markers(&self) -> bool { + crate::changeset::persists_outbound_account_markers(self.inner.as_ref()) + } + /// Preserve backend failure kinds, granting retry only when the backend /// guarantees a failed store retained and committed nothing. pub(crate) fn classify_store_failure(&self, error: PersistenceError) -> PlatformWalletError { @@ -81,6 +106,12 @@ impl WalletPersister { self.inner.store_commits_inline() } + /// Whether the backend can hold a durable DashPay backfill record (see + /// [`persists_backfill_coverage`](crate::changeset::persists_backfill_coverage)). + pub(crate) fn persists_backfill_coverage(&self) -> bool { + crate::changeset::persists_backfill_coverage(self.inner.as_ref()) + } + /// Feature-specific persistence contracts exposed by the backend. pub(crate) fn persistence_capabilities(&self) -> PersistenceCapabilities { self.inner.persistence_capabilities() diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index 7cbe111776d..75a80e98a8f 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -296,6 +296,20 @@ pub struct PlatformWalletInfo { /// host-side persister mirror fed by /// [`DpnsNameStateChangeSet`](crate::changeset::DpnsNameStateChangeSet). pub dpns_name_states: BTreeMap, + /// Durable half of the DashPay coreHeight backfill guard: which receival + /// contacts the historical rescan already covers, from which height each, + /// and how far the rescan reached. Restored at load from + /// [`ClientWalletStartState::dashpay_backfill`](crate::changeset::ClientWalletStartState::dashpay_backfill) + /// and written by `reconcile_dashpay_rescan` on the round that lowers the + /// cursor. The in-memory half is `DashPayState::rescan_triggered`; see + /// [`DashPayBackfillRecord`](crate::changeset::DashPayBackfillRecord) + /// for how the two compose. + pub dashpay_backfill: crate::changeset::DashPayBackfillRecord, + /// Orders this wallet's queued `SyncHeightAdvanced` events against + /// in-memory rewinds of its scan cursor; see + /// [`RewindBarrier`](crate::changeset::core_bridge::RewindBarrier). In + /// memory only. + pub rewind_barrier: crate::changeset::core_bridge::RewindBarrier, } /// A platform wallet that combines core UTXO functionality with identity management. @@ -661,6 +675,9 @@ impl PlatformWallet { /// Construct a PlatformWallet from a WalletManager that already contains /// the wallet. The wallet must have been inserted into the WalletManager /// before calling this. + // Every argument is a shared handle the manager owns; a builder would + // only move the same nine names behind a struct. + #[allow(clippy::too_many_arguments)] pub(crate) fn new( sdk: Arc, wallet_id: WalletId, @@ -669,6 +686,8 @@ impl PlatformWallet { lock_notify: Arc, persister: Arc, broadcaster: Arc, + sync_fault: Arc, + durable_cursors: crate::changeset::DurableCursors, ) -> Self { // Build the per-wallet persister handle once and share it with // the sub-wallets that need to queue their own changesets @@ -714,6 +733,8 @@ impl PlatformWallet { wallet_id, asset_locks: Arc::clone(&asset_locks), persister: wallet_persister.clone(), + sync_fault, + durable_cursors, broadcaster: dashpay_broadcaster, // DashPay write helper: forwards to the live SDK, erasing its // generic write signatures behind concrete by-value methods. diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs index c8ebc059fe7..4e41e573384 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs @@ -42,6 +42,8 @@ impl WalletInfoInterface for PlatformWalletInfo { tracked_asset_locks: std::collections::BTreeMap::new(), observed_input_conflicts: Default::default(), dpns_name_states: std::collections::BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), } } @@ -56,6 +58,8 @@ impl WalletInfoInterface for PlatformWalletInfo { tracked_asset_locks: std::collections::BTreeMap::new(), observed_input_conflicts: Default::default(), dpns_name_states: std::collections::BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), } } @@ -144,6 +148,26 @@ impl WalletInfoInterface for PlatformWalletInfo { self.core_wallet.last_processed_height() } + /// Forwarded, plus the receival accounts platform inserts itself: the + /// filter pipeline snapshots this per batch and refuses to certify a + /// batch whose snapshot predates an account add, because the scan never + /// tested that account's scripts (dashpay/rust-dashcore#649). The trait + /// default is a constant `0`, which turned the guard off for every + /// platform wallet — so a batch in flight across a contact-account + /// registration could certify coverage the DashPay backfill record then + /// vouched for (dashpay/platform#4302 review). + /// + /// Also offset past a removed same-id predecessor's final generation + /// (see `RewindBarrier::inherit`): both counters restart with the wallet, + /// so without the offset a batch reconciled against the predecessor's + /// account set could match this wallet's generation and be certified. + fn account_generation(&self) -> u64 { + self.core_wallet + .account_generation() + .wrapping_add(self.rewind_barrier.account_registrations()) + .wrapping_add(self.rewind_barrier.generation_base()) + } + fn synced_height(&self) -> CoreBlockHeight { self.core_wallet.synced_height() } @@ -153,7 +177,14 @@ impl WalletInfoInterface for PlatformWalletInfo { .update_last_processed_height(current_height); } + /// The engine's only caller is `update_wallet_synced_height`, which emits + /// a `SyncHeightAdvanced` straight after this returns, under the same + /// manager write lock — so this is where emissions are counted for the + /// [`RewindBarrier`](crate::changeset::core_bridge::RewindBarrier). + /// Platform code that moves the cursor calls `core_wallet` directly and + /// emits nothing. fn update_synced_height(&mut self, current_height: u32) { + self.rewind_barrier.note_emitted(); self.core_wallet.update_synced_height(current_height); } @@ -270,7 +301,10 @@ impl ManagedAccountOperations for PlatformWalletInfo { wallet: &Wallet, account_type: AccountType, ) -> key_wallet::Result<()> { - self.core_wallet.add_managed_account(wallet, account_type) + let before = self.core_wallet.metadata.synced_height; + let added = self.core_wallet.add_managed_account(wallet, account_type); + self.note_account_add_rewind(before); + added } fn add_managed_account_from_xpub( @@ -278,8 +312,12 @@ impl ManagedAccountOperations for PlatformWalletInfo { account_type: AccountType, account_xpub: ExtendedPubKey, ) -> key_wallet::Result<()> { - self.core_wallet - .add_managed_account_from_xpub(account_type, account_xpub) + let before = self.core_wallet.metadata.synced_height; + let added = self + .core_wallet + .add_managed_account_from_xpub(account_type, account_xpub); + self.note_account_add_rewind(before); + added } #[cfg(feature = "bls")] @@ -288,8 +326,12 @@ impl ManagedAccountOperations for PlatformWalletInfo { wallet: &Wallet, account_type: AccountType, ) -> key_wallet::Result<()> { - self.core_wallet - .add_managed_bls_account(wallet, account_type) + let before = self.core_wallet.metadata.synced_height; + let added = self + .core_wallet + .add_managed_bls_account(wallet, account_type); + self.note_account_add_rewind(before); + added } #[cfg(feature = "bls")] @@ -298,8 +340,12 @@ impl ManagedAccountOperations for PlatformWalletInfo { account_type: AccountType, bls_public_key: [u8; 48], ) -> key_wallet::Result<()> { - self.core_wallet - .add_managed_bls_account_from_public_key(account_type, bls_public_key) + let before = self.core_wallet.metadata.synced_height; + let added = self + .core_wallet + .add_managed_bls_account_from_public_key(account_type, bls_public_key); + self.note_account_add_rewind(before); + added } #[cfg(feature = "eddsa")] @@ -308,8 +354,12 @@ impl ManagedAccountOperations for PlatformWalletInfo { wallet: &Wallet, account_type: AccountType, ) -> key_wallet::Result<()> { - self.core_wallet - .add_managed_eddsa_account(wallet, account_type) + let before = self.core_wallet.metadata.synced_height; + let added = self + .core_wallet + .add_managed_eddsa_account(wallet, account_type); + self.note_account_add_rewind(before); + added } #[cfg(feature = "eddsa")] @@ -318,8 +368,31 @@ impl ManagedAccountOperations for PlatformWalletInfo { account_type: AccountType, ed25519_public_key: [u8; 32], ) -> key_wallet::Result<()> { - self.core_wallet - .add_managed_eddsa_account_from_public_key(account_type, ed25519_public_key) + let before = self.core_wallet.metadata.synced_height; + let added = self + .core_wallet + .add_managed_eddsa_account_from_public_key(account_type, ed25519_public_key); + self.note_account_add_rewind(before); + added + } +} + +impl PlatformWalletInfo { + /// key-wallet's account-add API rewinds the scan cursor to just below + /// wallet birth (the new account has no filter coverage). That lowering + /// emits nothing and is not persisted here, so treat it like any other + /// in-memory rewind: advances queued before it must not be stored after + /// it, and the next DashPay backfill record round owes the host the + /// lowered cursor — otherwise a record-only round could store coverage + /// beside a host cursor that never came down (dashpay/platform#4302 + /// review). + fn note_account_add_rewind(&mut self, before: u32) { + let after = self.core_wallet.metadata.synced_height; + if after < before { + self.rewind_barrier.arm(); + let epoch = self.rewind_barrier.epoch(); + self.dashpay_backfill.owe_cursor(after, epoch); + } } } diff --git a/packages/rs-platform-wallet/src/wallet/provider_ecdsa_key_tests.rs b/packages/rs-platform-wallet/src/wallet/provider_ecdsa_key_tests.rs index 2e38a340120..841393e7ea8 100644 --- a/packages/rs-platform-wallet/src/wallet/provider_ecdsa_key_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/provider_ecdsa_key_tests.rs @@ -45,6 +45,8 @@ async fn platform_wallet() -> PlatformWallet { Arc::new(tokio::sync::Notify::new()), Arc::new(NoopTestPersister) as Arc, Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), + Arc::new(std::sync::atomic::AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), ) } diff --git a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs index 670eaef352a..b207cacf648 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs @@ -188,6 +188,8 @@ where Arc::new(tokio::sync::Notify::new()), persister as Arc, Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), + Arc::new(std::sync::atomic::AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), ) } diff --git a/packages/rs-unified-sdk-jni/src/persistence.rs b/packages/rs-unified-sdk-jni/src/persistence.rs index 95d71c125b3..da5d40e8f04 100644 --- a/packages/rs-unified-sdk-jni/src/persistence.rs +++ b/packages/rs-unified-sdk-jni/src/persistence.rs @@ -54,13 +54,14 @@ use jni::JNIEnv; use platform_wallet_ffi::{ AccountAddressPoolFFI, AccountChangeSetFFI, AccountSpecFFI, AddressBalanceEntryFFI, AssetLockEntryFFI, ContactIgnoredSenderFFI, ContactProfileRestoreEntryFFI, ContactRequestFFI, - ContactRequestRemovalFFI, CoreAddressEntryFFI, DpnsNameStateFFI, IdentityEntryFFI, - IdentityKeyEntryFFI, IdentityKeyRemovalFFI, IdentityKeyRestoreFFI, IdentityRestoreEntryFFI, - InvitationEntryFFI, OutPointFFI, PaymentRestoreEntryFFI, PersistenceCallbacks, - PersistenceCallbacksExtension, PlatformAddressFFI, ProviderSpecialTxRestoreEntryFFI, - SpentOutPointFFI, SweepBatchFFI, TokenBalanceRemovalFFI, TokenBalanceUpsertFFI, - TransactionRecordFFI, UnresolvedAssetLockTxRecordFFI, UtxoEntryFFI, UtxoRestoreEntryFFI, - WalletChangeSetFFI, WalletRestoreEntryFFI, + ContactRequestRemovalFFI, CoreAddressEntryFFI, DashPayBackfillCoveredContactFFI, + DpnsNameStateFFI, IdentityEntryFFI, IdentityKeyEntryFFI, IdentityKeyRemovalFFI, + IdentityKeyRestoreFFI, IdentityRestoreEntryFFI, InvitationEntryFFI, OutPointFFI, + PaymentRestoreEntryFFI, PersistenceCallbacks, PersistenceCallbacksExtension, + PlatformAddressFFI, ProviderSpecialTxRestoreEntryFFI, SpentOutPointFFI, SweepBatchFFI, + TokenBalanceRemovalFFI, TokenBalanceUpsertFFI, TransactionRecordFFI, + UnresolvedAssetLockTxRecordFFI, UtxoEntryFFI, UtxoRestoreEntryFFI, WalletChangeSetFFI, + WalletRestoreEntryFFI, }; use std::ffi::{c_void, CStr, CString}; use std::os::raw::c_char; @@ -205,11 +206,108 @@ pub(crate) fn build_vtable(context: *mut c_void) -> PersistenceCallbacks { /// sync watermark with it, and the round is refused one layer up instead of /// advancing past a removal that never happened. Wiring the slot for every /// subclass would make "slot present" prove nothing. +/// +/// The DashPay backfill slot is likewise wired only when the bridge +/// OVERRIDES `onWalletChangesetHeader`, the callback that stores the cursor. +/// `on_persist_wallet_changeset_fn` is always wired, so Rust's "cursor +/// delivered" gate cannot tell a stored cursor from one handed to the +/// inherited no-op header. A bridge that persists the backfill record but +/// not the cursor would then commit coverage for a rewind while keeping the +/// old cursor, and that coverage would suppress the recovery scan after a +/// restart (dashpay/platform#4302 review). Without the slot the record is +/// never delivered and every launch re-runs the backfill: slow, never lossy. +/// +/// It also needs the bridge to OVERRIDE `onWalletChangesetDashPayBackfill` +/// itself. The slot's presence is what `FFIPersister` reports as "this host +/// stores the record", and an inherited no-op would acknowledge the empty +/// record Rust sends to clear coverage the host handed back — so the +/// coverage would stay on disk while the wallet is published without it. +/// With the slot unwired that invalidation is refused instead, and the +/// publication fails closed. pub(crate) fn build_extension(env: &mut JNIEnv, bridge: &JObject) -> PersistenceCallbacksExtension { - let sweeps_overridden = bridge_overrides(env, bridge, "onWalletChangesetTransactionsSwept"); + extension_for(BridgeOverrides { + sweeps: bridge_overrides( + env, + bridge, + "onWalletChangesetTransactionsSwept", + WALLET_CHANGESET_SWEEPS_DESCRIPTOR, + ), + header: bridge_overrides( + env, + bridge, + "onWalletChangesetHeader", + WALLET_CHANGESET_HEADER_DESCRIPTOR, + ), + backfill: bridge_overrides( + env, + bridge, + "onWalletChangesetDashPayBackfill", + WALLET_CHANGESET_DASHPAY_BACKFILL_DESCRIPTOR, + ), + }) +} + +/// Leave the account-registration and address-pool slots of `vtable` unwired +/// when the concrete `bridge` inherits `NativePersistenceBridge`'s +/// success-returning no-op for them (see [`bridge_overrides`]). +/// +/// Those slots are how `FFIPersister` derives `ACCOUNT_ADDRESS_POOLS`, and a +/// contact's outbound-account marker is kept on disk only where that +/// capability holds +/// ([`persists_outbound_account_markers`](platform_wallet::changeset::persists_outbound_account_markers)). +/// An inherited no-op would acknowledge an account it never stored, so the +/// marker naming that account could be trusted after a restart beside the +/// previous one (dashpay/platform#4302 review). Unwired, the capability is not +/// derived, account writes are reported as unsupported rather than silently +/// dropped, and markers stay in memory. +pub(crate) fn restrict_account_writers( + env: &mut JNIEnv, + bridge: &JObject, + vtable: &mut PersistenceCallbacks, +) { + let registrations = bridge_overrides( + env, + bridge, + "onPersistAccountRegistration", + ACCOUNT_REGISTRATION_DESCRIPTOR, + ); + let pools = bridge_overrides( + env, + bridge, + "onPersistAccountAddressPoolEntry", + ACCOUNT_ADDRESS_POOL_ENTRY_DESCRIPTOR, + ); + gate_account_writers(vtable, registrations, pools); +} + +/// The slot half of [`restrict_account_writers`]. +fn gate_account_writers(vtable: &mut PersistenceCallbacks, registrations: bool, pools: bool) { + if !registrations { + vtable.on_persist_account_registrations_fn = None; + } + if !pools { + vtable.on_persist_account_address_pools_fn = None; + } +} + +/// Which optional `NativePersistenceBridge` callbacks the concrete bridge +/// supplies its own body for (see [`bridge_overrides`]). +#[derive(Debug, Clone, Copy)] +struct BridgeOverrides { + /// `onWalletChangesetTransactionsSwept`. + sweeps: bool, + /// `onWalletChangesetHeader`, which carries the scan cursor. + header: bool, + /// `onWalletChangesetDashPayBackfill`, which stores the record. + backfill: bool, +} + +/// The extension slots wired for a bridge with `overrides` (see +/// [`build_extension`]). +fn extension_for(overrides: BridgeOverrides) -> PersistenceCallbacksExtension { PersistenceCallbacksExtension { on_persist_dpns_name_states_fn: Some(tramp_persist_dpns_name_states), - on_persist_wallet_changeset_sweeps_fn: if sweeps_overridden { + on_persist_wallet_changeset_sweeps_fn: if overrides.sweeps { Some(tramp_persist_wallet_changeset_sweeps) } else { None @@ -217,20 +315,41 @@ pub(crate) fn build_extension(env: &mut JNIEnv, bridge: &JObject) -> Persistence on_persist_wallet_changeset_chain_lock_height_fn: Some( tramp_persist_wallet_changeset_chain_lock_height, ), + on_persist_wallet_dashpay_backfill_fn: if overrides.header && overrides.backfill { + Some(tramp_persist_wallet_dashpay_backfill) + } else { + None + }, ..Default::default() } } /// Whether `bridge`'s concrete class — or any superclass strictly below -/// `NativePersistenceBridge` — declares a method named `name`. A Kotlin -/// `override fun` is a declared method of the overriding class, so walking -/// `getDeclaredMethods()` up the hierarchy until the abstract bridge answers -/// "did a subclass supply its own body". Any JNI failure counts as "not -/// overridden" (the pending exception is cleared): the consequence is a -/// slot left unwired, which Rust turns into a stripped capability — the -/// safe direction, never a silently swallowed removal. -fn bridge_overrides(env: &mut JNIEnv, bridge: &JObject, name: &str) -> bool { - fn probe(env: &mut JNIEnv, bridge: &JObject, name: &str) -> Result { +/// `NativePersistenceBridge` — declares the method `name` with the JNI +/// `descriptor` the trampoline calls. A Kotlin `override fun` is a declared +/// method of the overriding class, so walking `getDeclaredMethods()` up the +/// hierarchy until the abstract bridge answers "did a subclass supply its own +/// body". The descriptor has to match, not just the name: an overload such +/// as `onWalletChangesetHeader(walletId, height)` leaves the callback the +/// trampoline invokes inherited, and must not count. Any JNI failure counts +/// as "not overridden" (the pending exception is cleared): the consequence is +/// a slot left unwired, which Rust turns into a stripped capability — the +/// safe direction, never a silently swallowed write. +fn bridge_overrides(env: &mut JNIEnv, bridge: &JObject, name: &str, descriptor: &str) -> bool { + fn class_name(env: &mut JNIEnv, class: &JObject) -> Result { + let name: JString = env + .call_method(class, "getName", "()Ljava/lang/String;", &[])? + .l()? + .into(); + let name = env.get_string(&name)?.to_str().map(str::to_owned); + Ok(name.unwrap_or_default()) + } + fn probe( + env: &mut JNIEnv, + bridge: &JObject, + name: &str, + descriptor: &str, + ) -> Result { let base = env.find_class("org/dashfoundation/dashsdk/ffi/NativePersistenceBridge")?; let mut class = env.get_object_class(bridge)?; loop { @@ -253,12 +372,28 @@ fn bridge_overrides(env: &mut JNIEnv, bridge: &JObject, name: &str) -> bool { .call_method(&method, "getName", "()Ljava/lang/String;", &[])? .l()? .into(); - let matches = env + let same_name = env .get_string(&method_name)? .to_str() .map(|s| s == name) .unwrap_or(false); - if matches { + if !same_name { + continue; + } + let params: JObjectArray = env + .call_method(&method, "getParameterTypes", "()[Ljava/lang/Class;", &[])? + .l()? + .into(); + let mut param_names = Vec::new(); + for p in 0..env.get_array_length(¶ms)? { + let param = env.get_object_array_element(¶ms, p)?; + param_names.push(class_name(env, ¶m)?); + } + let return_type = env + .call_method(&method, "getReturnType", "()Ljava/lang/Class;", &[])? + .l()?; + let return_name = class_name(env, &return_type)?; + if method_descriptor(¶m_names, &return_name) == descriptor { return Ok(true); } } @@ -271,7 +406,7 @@ fn bridge_overrides(env: &mut JNIEnv, bridge: &JObject, name: &str) -> bool { class = superclass.into(); } } - match probe(env, bridge, name) { + match probe(env, bridge, name, descriptor) { Ok(overridden) => overridden, Err(_) => { let _ = env.exception_clear(); @@ -280,6 +415,34 @@ fn bridge_overrides(env: &mut JNIEnv, bridge: &JObject, name: &str) -> bool { } } +/// The JNI descriptor of a method whose parameter and return types +/// `Class.getName()` reports as `params` and `return_type` — `int`, `[B`, +/// `java.lang.String`, `[Ljava.lang.String;` and so on. +fn method_descriptor(params: &[String], return_type: &str) -> String { + fn type_descriptor(name: &str) -> String { + match name { + "boolean" => "Z".to_owned(), + "byte" => "B".to_owned(), + "char" => "C".to_owned(), + "short" => "S".to_owned(), + "int" => "I".to_owned(), + "long" => "J".to_owned(), + "float" => "F".to_owned(), + "double" => "D".to_owned(), + "void" => "V".to_owned(), + array if array.starts_with('[') => array.replace('.', "/"), + class => format!("L{};", class.replace('.', "/")), + } + } + let mut descriptor = String::from("("); + for param in params { + descriptor.push_str(&type_descriptor(param)); + } + descriptor.push(')'); + descriptor.push_str(&type_descriptor(return_type)); + descriptor +} + /// `release_fn` for the persistence vtable: frees the boxed /// [`KotlinPersistenceCtx`] when the native manager's last persister /// reference drops. The FFI guarantees exactly one call, which may land @@ -585,7 +748,7 @@ fn call_persist_account_registration( env.call_method( bridge, "onPersistAccountRegistration", - "([BBBIII[B[B[B)I", + ACCOUNT_REGISTRATION_DESCRIPTOR, &[ wid.into(), JValue::Byte(s.type_tag as i8), @@ -629,7 +792,7 @@ unsafe extern "C" fn tramp_persist_account_address_pools( env.call_method( bridge, "onPersistAccountAddressPoolEntry", - "([BBBIII[B[BB[BZBIZJLjava/lang/String;Ljava/lang/String;)I", + ACCOUNT_ADDRESS_POOL_ENTRY_DESCRIPTOR, &[ (&wid).into(), JValue::Byte(spec.type_tag as i8), @@ -690,7 +853,7 @@ unsafe extern "C" fn tramp_persist_wallet_changeset( .call_method( bridge, "onWalletChangesetHeader", - "([BZIZJJJJ[B)I", + WALLET_CHANGESET_HEADER_DESCRIPTOR, &[ (&wid).into(), JValue::Bool(has_synced as u8), @@ -776,6 +939,20 @@ unsafe extern "C" fn tramp_persist_wallet_changeset_sweeps( /// header's `(hasSyncedHeight, syncedHeight)`, not a sentinel. const WALLET_CHANGESET_SWEEPS_DESCRIPTOR: &str = "([B[BI[B[BIZI)I"; +/// `onWalletChangesetHeader`'s descriptor, shared by the `call_method` site, +/// the descriptor table and the override probe that gates the DashPay +/// backfill slot on it, so the three cannot drift apart. +const WALLET_CHANGESET_HEADER_DESCRIPTOR: &str = "([BZIZJJJJ[B)I"; + +/// `onPersistAccountRegistration`'s descriptor, shared by the `call_method` +/// site, the descriptor table and the override probe in +/// [`restrict_account_writers`]. +const ACCOUNT_REGISTRATION_DESCRIPTOR: &str = "([BBBIII[B[B[B)I"; + +/// `onPersistAccountAddressPoolEntry`'s descriptor, shared the same way. +const ACCOUNT_ADDRESS_POOL_ENTRY_DESCRIPTOR: &str = + "([BBBIII[B[BB[BZBIZJLjava/lang/String;Ljava/lang/String;)I"; + unsafe fn persist_changeset_sweep_batch( env: &mut JNIEnv, bridge: &JObject, @@ -850,6 +1027,63 @@ unsafe extern "C" fn tramp_persist_wallet_changeset_chain_lock_height( }) } +/// Wire size of one covered contact in the flat `covered` array handed to +/// `onWalletChangesetDashPayBackfill` and read back from +/// `WalletRestoreData.dashPayBackfillCovered`: owner (32) ‖ contact (32) ‖ +/// `accountIndex` (u32) ‖ `coveredFrom` (u32), both little-endian. The same +/// layout as +/// `DashPayBackfillRecord::covered_bytes`, so the handler can store the array +/// as one opaque blob and hand it straight back at load. +const DASHPAY_BACKFILL_COVERED_ENTRY_LEN: usize = 32 + 32 + 4 + 4; + +/// Descriptor of `NativePersistenceBridge.onWalletChangesetDashPayBackfill`: +/// `(walletId, floor, rewoundFrom, covered, coveredCount)`. The cover set is +/// shipped as ONE flat `byte[]` of `72·N` bytes plus a count, the same packing +/// the sweep slot uses for its txids, rather than one JVM allocation per +/// contact — a contact-heavy wallet records hundreds. +const WALLET_CHANGESET_DASHPAY_BACKFILL_DESCRIPTOR: &str = "([BII[BI)I"; + +/// Deliver the wallet's DashPay backfill record (see +/// `PersistWalletDashPayBackfillFn`) — the durable half of the coreHeight +/// rescan guard (dashpay/platform#4302). Whole-record semantics: the handler +/// replaces what it holds on the wallet row. +unsafe extern "C" fn tramp_persist_wallet_dashpay_backfill( + context: *mut c_void, + wallet_id: *const u8, + floor: u32, + rewound_from: u32, + covered: *const DashPayBackfillCoveredContactFFI, + covered_count: usize, +) -> i32 { + with_bridge(context, |env, bridge| { + let wid = id32(env, wallet_id)?; + let entries = slice_or_empty(covered, covered_count); + let mut packed = Vec::with_capacity(entries.len() * DASHPAY_BACKFILL_COVERED_ENTRY_LEN); + for entry in entries { + packed.extend_from_slice(&entry.owner_identity_id); + packed.extend_from_slice(&entry.contact_identity_id); + packed.extend_from_slice(&entry.account_index.to_le_bytes()); + packed.extend_from_slice(&entry.covered_from.to_le_bytes()); + } + let packed_arr = env.byte_array_from_slice(&packed)?; + let count = i32::try_from(entries.len()) + .map_err(|_| jni::errors::Error::JniCall(jni::errors::JniError::InvalidArguments))?; + env.call_method( + bridge, + "onWalletChangesetDashPayBackfill", + WALLET_CHANGESET_DASHPAY_BACKFILL_DESCRIPTOR, + &[ + (&wid).into(), + JValue::Int(jint_height(floor)?), + JValue::Int(jint_height(rewound_from)?), + (&packed_arr).into(), + JValue::Int(count), + ], + )? + .i() + }) +} + unsafe fn persist_changeset_account( env: &mut JNIEnv, bridge: &JObject, @@ -1500,7 +1734,7 @@ unsafe fn persist_contact_upsert( env.call_method( bridge, "onPersistContactUpsert", - "([B[B[BZIII[B[B[BIJZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;[I)I", + CONTACT_UPSERT_DESCRIPTOR, &[ wid.into(), (&owner).into(), @@ -1520,11 +1754,25 @@ unsafe fn persist_contact_upsert( JValue::Bool(c.is_hidden as u8), (&contact_account_label).into(), (&accepted).into(), + JValue::Bool(c.has_external_account_reference as u8), + JValue::Int(if c.has_external_account_reference { + c.external_account_reference as i32 + } else { + 0 + }), ], )? .i() } +/// Descriptor of `NativePersistenceBridge.onPersistContactUpsert`. The +/// trailing `(Z, I)` pair is `EstablishedContact::external_account_reference` +/// as `(hasExternalAccountReference, externalAccountReference)` — the marker +/// whose loss across restart rebuilt every outbound contact account on every +/// launch (dashpay/platform#4302). +const CONTACT_UPSERT_DESCRIPTOR: &str = + "([B[B[BZIII[B[B[BIJZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;[IZI)I"; + /// Copy `len` `u32`s from `ptr` (or 0 when null) into a JVM `int[]` /// (bit-pattern cast — DIP-15 account indices never exceed `i32::MAX` /// in practice, and the Kotlin side reads them back with the same cast). @@ -1973,6 +2221,11 @@ struct WalletRestoreStaged { /// minted null / 0 at seal (no chainlock persisted). A single flat /// buffer, freed with one `free_raw_bytes`. last_applied_chain_lock: Vec, + /// The DashPay backfill record's cover set. `Copy` POD like the + /// platform-address balances: minted in one shot at seal, freed with a + /// single `free_raw_slice`. The record's scalars and presence flag ride + /// on `entry` directly. + dashpay_backfill_covered: Vec, } /// Staged account spec: FFI struct with a null xpub pointer plus the @@ -2169,6 +2422,7 @@ fn seal_wallet_entries(staged: Vec) -> Vec` mints in one shot and @@ -2291,6 +2545,13 @@ fn seal_wallet_entries(staged: Vec) -> Vec Result<(bool, u32, u32, Vec), jni::errors::Error> { + let present = env.get_field(holder, "hasDashPayBackfill", "Z")?.z()?; + if !present { + return Ok((false, 0, 0, Vec::new())); + } + let floor = env.get_field(holder, "dashPayBackfillFloor", "I")?.i()?; + let rewound_from = env + .get_field(holder, "dashPayBackfillRewoundFrom", "I")? + .i()?; + let blob = read_bytes_field_vec(env, holder, "dashPayBackfillCovered")?; + if floor < 0 + || rewound_from < 0 + || !blob + .len() + .is_multiple_of(DASHPAY_BACKFILL_COVERED_ENTRY_LEN) + { + log::warn!( + "load: malformed DashPay backfill record on the wallet row (floor={floor}, \ + rewound_from={rewound_from}, blob_len={}); reading as no record", + blob.len() + ); + return Ok((false, 0, 0, Vec::new())); + } + let (chunks, _) = blob.as_chunks::(); + let covered = chunks + .iter() + .map(|chunk| { + let mut owner_identity_id = [0u8; 32]; + let mut contact_identity_id = [0u8; 32]; + let mut account_index = [0u8; 4]; + let mut height = [0u8; 4]; + owner_identity_id.copy_from_slice(&chunk[..32]); + contact_identity_id.copy_from_slice(&chunk[32..64]); + account_index.copy_from_slice(&chunk[64..68]); + height.copy_from_slice(&chunk[68..72]); + DashPayBackfillCoveredContactFFI { + owner_identity_id, + contact_identity_id, + account_index: u32::from_le_bytes(account_index), + covered_from: u32::from_le_bytes(height), + } + }) + .collect(); + Ok((true, floor as u32, rewound_from as u32, covered)) +} + /// Read the Kotlin `WalletRestoreData.utxos` array into staged /// [`UtxoRestoreStaged`] rows (CORE-06 restore path). Empty / null field /// → empty vec (staged as `(null, 0)` at seal). @@ -3323,6 +3657,15 @@ fn build_contact_restore( let is_hidden = env.get_field(holder, "isHidden", "Z")?.z()?; let contact_account_label = read_opt_cstring_field(env, holder, "contactAccountLabel")?; let accepted_accounts = read_u32_array_field(env, holder, "acceptedAccounts")?; + let has_external_account_reference = env + .get_field(holder, "hasExternalAccountReference", "Z")? + .z()?; + let external_account_reference = if has_external_account_reference { + env.get_field(holder, "externalAccountReference", "I")? + .i()? as u32 + } else { + 0 + }; let row = ContactRequestFFI { owner_id, @@ -3346,6 +3689,8 @@ fn build_contact_restore( contact_account_label: ptr::null(), accepted_accounts: ptr::null(), accepted_accounts_len: 0, + has_external_account_reference, + external_account_reference, }; Ok(ContactRestoreStaged { row, @@ -3613,6 +3958,9 @@ unsafe extern "C" fn tramp_load_wallet_list_free( e.last_applied_chain_lock_bytes_len, ); + // DashPay backfill cover set — flat POD slice, one mint at seal. + free_raw_slice(e.dashpay_backfill_covered, e.dashpay_backfill_covered_count); + // identities + nested key / contact / ignored-sender / // payment / contact-profile arrays (each key's `data` buffer + // contract-bounds doc-type C-string; each contact's three byte @@ -4516,12 +4864,18 @@ const BRIDGE_METHOD_TABLE: &[(&str, &str)] = &[ ("onPersistAddressBalance", "([BB[BJIIIJ)I"), ("onPersistSyncState", "([BJJJ)I"), ("onPersistWalletMetadata", "([BI[BI)I"), - ("onPersistAccountRegistration", "([BBBIII[B[B[B)I"), + ( + "onPersistAccountRegistration", + ACCOUNT_REGISTRATION_DESCRIPTOR, + ), ( "onPersistAccountAddressPoolEntry", - "([BBBIII[B[BB[BZBIZJLjava/lang/String;Ljava/lang/String;)I", + ACCOUNT_ADDRESS_POOL_ENTRY_DESCRIPTOR, + ), + ( + "onWalletChangesetHeader", + WALLET_CHANGESET_HEADER_DESCRIPTOR, ), - ("onWalletChangesetHeader", "([BZIZJJJJ[B)I"), ("onWalletChangesetAccountBegin", "([BIBBII[B[BIZIZ)I"), ("onWalletChangesetAccountEnd", "([BI)I"), ( @@ -4548,6 +4902,10 @@ const BRIDGE_METHOD_TABLE: &[(&str, &str)] = &[ // literal at the `call_method` site in // `tramp_persist_wallet_changeset_chain_lock_height`. ("onWalletChangesetChainLockHeight", "([BI)I"), + ( + "onWalletChangesetDashPayBackfill", + WALLET_CHANGESET_DASHPAY_BACKFILL_DESCRIPTOR, + ), ( "onPersistIdentityUpsert", "([B[BJJZIBZ[B[Ljava/lang/String;[JZLjava/lang/String;Ljava/lang/String;\ @@ -4564,10 +4922,7 @@ const BRIDGE_METHOD_TABLE: &[(&str, &str)] = &[ ("onPersistTokenBalanceUpsert", "([B[B[BJ)I"), ("onPersistTokenBalanceRemoval", "([B[B[B)I"), ("onPersistContactIgnored", "([B[B[BZ)I"), - ( - "onPersistContactUpsert", - "([B[B[BZIII[B[B[BIJZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;[I)I", - ), + ("onPersistContactUpsert", CONTACT_UPSERT_DESCRIPTOR), ("onPersistContactRemovalSent", "([B[B[B)I"), ("onPersistContactRemovalIncoming", "([B[B[B)I"), ("onPersistAssetLockUpsert", "([B[B[BIBIJB[B)I"), @@ -4723,6 +5078,111 @@ mod tests { /// A height past `i32::MAX` must refuse the call, never wrap into a /// negative `Int` the handler would read as absent or as a bogus /// collection boundary. + /// The override probe compares the reflected descriptor, not the name: an + /// overload of `onWalletChangesetHeader` leaves the callback the + /// trampoline invokes inherited, so it must not wire the backfill slot. + #[test] + fn the_override_probe_matches_the_called_descriptor_not_an_overload() { + let names = |names: &[&str]| names.iter().map(|n| n.to_string()).collect::>(); + let header = names(&[ + "[B", "boolean", "int", "boolean", "long", "long", "long", "long", "[B", + ]); + assert_eq!( + method_descriptor(&header, "int"), + WALLET_CHANGESET_HEADER_DESCRIPTOR, + "the real override resolves to the descriptor the trampoline calls" + ); + assert_ne!( + method_descriptor(&names(&["[B", "int"]), "int"), + WALLET_CHANGESET_HEADER_DESCRIPTOR, + "an overload is not the callback" + ); + let sweeps = names(&["[B", "[B", "int", "[B", "[B", "int", "boolean", "int"]); + assert_eq!( + method_descriptor(&sweeps, "int"), + WALLET_CHANGESET_SWEEPS_DESCRIPTOR + ); + assert_eq!( + method_descriptor(&names(&["java.lang.String", "[Ljava.lang.String;"]), "void"), + "(Ljava/lang/String;[Ljava/lang/String;)V" + ); + } + + /// The backfill slot needs both callbacks a bridge supplies its own body + /// for. One that inherits the no-op `onWalletChangesetHeader` stores no + /// cursor, so coverage would land beside a cursor that never moved. One + /// that inherits the no-op `onWalletChangesetDashPayBackfill` stores no + /// record, yet would acknowledge the invalidation of coverage it handed + /// back on load. Left unwired, `FFIPersister` reports that the host does + /// not store the record and refuses that invalidation, so the publication + /// fails closed (dashpay/platform#4302 review). The sweep slot keeps its + /// own probe. + #[test] + fn the_backfill_slot_is_wired_only_for_a_bridge_that_stores_cursor_and_record() { + for (header, backfill, wired) in [ + (false, false, false), + (false, true, false), + (true, false, false), + (true, true, true), + ] { + let extension = extension_for(BridgeOverrides { + sweeps: false, + header, + backfill, + }); + assert_eq!( + extension.on_persist_wallet_dashpay_backfill_fn.is_some(), + wired, + "header overridden: {header}, backfill overridden: {backfill}" + ); + } + let sweeps_only = extension_for(BridgeOverrides { + sweeps: true, + header: false, + backfill: false, + }); + assert!(sweeps_only.on_persist_wallet_changeset_sweeps_fn.is_some()); + assert!(sweeps_only.on_persist_wallet_dashpay_backfill_fn.is_none()); + } + + /// A bridge inheriting the no-op account writers gets neither account + /// slot, so `FFIPersister` cannot derive `ACCOUNT_ADDRESS_POOLS` and does + /// not keep outbound-account markers on disk; one implementing both keeps + /// both slots. + #[test] + fn account_writer_slots_follow_the_bridges_overrides() { + for (registrations, pools) in [(false, false), (true, false), (false, true), (true, true)] { + let mut vtable = build_vtable(std::ptr::null_mut()); + gate_account_writers(&mut vtable, registrations, pools); + assert_eq!( + vtable.on_persist_account_registrations_fn.is_some(), + registrations + ); + assert_eq!(vtable.on_persist_account_address_pools_fn.is_some(), pools); + // `release_fn` must not run on the null context when this drops. + vtable.release_fn = None; + } + let names = |names: &[&str]| names.iter().map(|n| n.to_string()).collect::>(); + assert_eq!( + method_descriptor( + &names(&["[B", "byte", "byte", "int", "int", "int", "[B", "[B", "[B"]), + "int" + ), + ACCOUNT_REGISTRATION_DESCRIPTOR + ); + } + + /// The probe has to recognise a real `onWalletChangesetDashPayBackfill` + /// override by the descriptor the trampoline calls. + #[test] + fn the_backfill_probe_matches_the_called_descriptor() { + let names = |names: &[&str]| names.iter().map(|n| n.to_string()).collect::>(); + assert_eq!( + method_descriptor(&names(&["[B", "int", "int", "[B", "int"]), "int"), + WALLET_CHANGESET_DASHPAY_BACKFILL_DESCRIPTOR + ); + } + #[test] fn a_height_past_i32_max_is_refused_rather_than_wrapped() { assert_eq!(jint_height(0).unwrap(), 0); diff --git a/packages/rs-unified-sdk-jni/src/wallet_manager.rs b/packages/rs-unified-sdk-jni/src/wallet_manager.rs index 10396a29939..99eba41dbd0 100644 --- a/packages/rs-unified-sdk-jni/src/wallet_manager.rs +++ b/packages/rs-unified-sdk-jni/src/wallet_manager.rs @@ -41,7 +41,9 @@ #![allow(clippy::missing_safety_doc)] use crate::events::{build_event_extension, build_event_vtable, KotlinEventCtx}; -use crate::persistence::{build_extension, build_vtable, KotlinPersistenceCtx}; +use crate::persistence::{ + build_extension, build_vtable, restrict_account_writers, KotlinPersistenceCtx, +}; use crate::support::{guard, take_pwffi_error, throw_pwffi_result, throw_sdk_exception}; use jni::objects::{JByteArray, JClass, JObject, JObjectArray, JString, JValue}; use jni::sys::{ @@ -154,7 +156,8 @@ pub extern "system" fn Java_org_dashfoundation_dashsdk_ffi_WalletManagerNative_n }; let persistence_ctx = Box::into_raw(Box::new(KotlinPersistenceCtx::new(persistence_global))); - let persistence: PersistenceCallbacks = build_vtable(persistence_ctx as *mut c_void); + let mut persistence: PersistenceCallbacks = build_vtable(persistence_ctx as *mut c_void); + restrict_account_writers(env, &persistence_bridge, &mut persistence); let persistence_extension = build_extension(env, &persistence_bridge); let persistence_capabilities = PersistenceCapabilitiesFFI { version: declared_capabilities_version,