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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions lib/db/sqlite/firo_cache_coordinator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,38 @@ abstract class FiroCacheCoordinator {
return;
}

final numberOfCoinsToFetch = meta.size - prevSize;
final int effectivePrevSize;
if (prevSize > meta.size) {
Logging.instance.w(
"Spark cache size mismatch for groupId=$groupId: "
"prevSize=$prevSize > meta.size=${meta.size}. "
"Falling back to full refetch for this set.",
);
effectivePrevSize = 0;
} else {
effectivePrevSize = prevSize;
}

final numberOfCoinsToFetch = meta.size - effectivePrevSize;
if (numberOfCoinsToFetch <= 0) {
// Already up to date for this block hash/set hash.
return;
}

final fullSectorCount = numberOfCoinsToFetch ~/ sectorSize;
final remainder = numberOfCoinsToFetch % sectorSize;

final List<dynamic> coins = [];

for (int i = 0; i < fullSectorCount; i++) {
final start = (i * sectorSize);
final start = effectivePrevSize + (i * sectorSize);
final data = await client.getSparkAnonymitySetBySector(
coinGroupId: groupId,
latestBlock: meta.blockHash,
startIndex: start,
endIndex: start + sectorSize,
);
progressUpdated?.call(start + sectorSize, numberOfCoinsToFetch);
progressUpdated?.call(((i + 1) * sectorSize), numberOfCoinsToFetch);

coins.addAll(data);
}
Expand All @@ -133,8 +149,8 @@ abstract class FiroCacheCoordinator {
final data = await client.getSparkAnonymitySetBySector(
coinGroupId: groupId,
latestBlock: meta.blockHash,
startIndex: numberOfCoinsToFetch - remainder,
endIndex: numberOfCoinsToFetch,
startIndex: effectivePrevSize + numberOfCoinsToFetch - remainder,
endIndex: effectivePrevSize + numberOfCoinsToFetch,
);
progressUpdated?.call(numberOfCoinsToFetch, numberOfCoinsToFetch);

Expand Down
35 changes: 30 additions & 5 deletions lib/db/sqlite/firo_cache_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,46 @@ FCResult _updateSparkAnonSetCoinsWith(
for (final coin in coins) {
db.execute(
"""
INSERT INTO SparkCoin (serialized, txHash, context, groupId)
INSERT OR IGNORE INTO SparkCoin (serialized, txHash, context, groupId)
VALUES (?, ?, ?, ?);
""",
[coin.serialized, coin.txHash, coin.context, coin.groupId],
);
final coinId = db.lastInsertRowId;
final coinIdResult = db.select(
"""
SELECT id
FROM SparkCoin
WHERE serialized = ? AND txHash = ? AND context = ? AND groupId = ?
LIMIT 1;
""",
[coin.serialized, coin.txHash, coin.context, coin.groupId],
);
if (coinIdResult.isEmpty) {
throw Exception(
"Failed to resolve SparkCoin id after insert/ignore operation",
);
}
final coinId = coinIdResult.first["id"] as int;

// finally add the row id to the newly added set
db.execute(
final hasSetCoin = db.select(
"""
INSERT INTO SparkSetCoins (setId, coinId)
VALUES (?, ?);
SELECT 1
FROM SparkSetCoins
WHERE setId = ? AND coinId = ?
LIMIT 1;
""",
[setId, coinId],
);
if (hasSetCoin.isEmpty) {
db.execute(
"""
INSERT INTO SparkSetCoins (setId, coinId)
VALUES (?, ?);
""",
[setId, coinId],
);
}
}

db.execute("COMMIT;");
Expand Down
9 changes: 9 additions & 0 deletions lib/pages/masternodes/create_masternode_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ class CreateMasternodeView extends ConsumerStatefulWidget {
const CreateMasternodeView({
super.key,
required this.firoWalletId,
required this.collateralTxid,
required this.collateralVout,
required this.collateralAddress,
this.popTxidOnSuccess = true,
});

static const routeName = "/createMasternodeView";

final String firoWalletId;
final String collateralTxid;
final int collateralVout;
final String collateralAddress;
final bool popTxidOnSuccess;

@override
Expand Down Expand Up @@ -107,6 +113,9 @@ class _CreateMasternodeDialogState extends ConsumerState<CreateMasternodeView> {
),
child: RegisterMasternodeForm(
firoWalletId: widget.firoWalletId,
collateralTxid: widget.collateralTxid,
collateralVout: widget.collateralVout,
collateralAddress: widget.collateralAddress,
onRegistrationSuccess: (txid) {
if (widget.popTxidOnSuccess && mounted) {
Navigator.of(context, rootNavigator: Util.isDesktop).pop(txid);
Expand Down
Loading