Skip to content
Snippets Groups Projects
Commit e7dbaebe authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

[fix] Delete or update 'indexOfComplexeConditionForPubkeys' when removing a block

parent 5ec7a750
Branches
No related tags found
1 merge request!1420[fix] Optimize access to sources by pubkey, using specific index
...@@ -129,9 +129,9 @@ export class LevelDBSindex extends LevelDBTable<SindexEntry> ...@@ -129,9 +129,9 @@ export class LevelDBSindex extends LevelDBTable<SindexEntry>
pos: number; pos: number;
}[] }[]
> { > {
const forConditions = await this.getForConditions(`SIG(${pubkey})`); const forSimpleConditions = await this.getForConditions(`SIG(${pubkey})`);
const forPubkeys = await this.getForComplexeConditionPubkey(pubkey); const forComplexConditions = await this.getForComplexeConditionPubkey(pubkey);
const reduced = Indexer.DUP_HELPERS.reduceBy(forConditions.concat(forPubkeys), [ const reduced = Indexer.DUP_HELPERS.reduceBy(forSimpleConditions.concat(forComplexConditions), [
"identifier", "identifier",
"pos", "pos",
]); ]);
...@@ -329,24 +329,25 @@ export class LevelDBSindex extends LevelDBTable<SindexEntry> ...@@ -329,24 +329,25 @@ export class LevelDBSindex extends LevelDBTable<SindexEntry>
} }
private async trimConditions(condition: string, id: string) { private async trimConditions(condition: string, id: string) {
// Get all the account's TX sources // Get all the condition's sources
const existing = (await this.indexForConditions.getOrNull(condition)) || []; const existing = (await this.indexForConditions.getOrNull(condition)) || [];
// Prune the source from the account // Prune the source from the condition
const trimmed = arrayPruneAllCopy(existing, id); const trimmed = arrayPruneAllCopy(existing, id);
if (trimmed.length) { if (trimmed.length) {
// If some sources are left for this "account", persist what remains // If some sources are left for this "condition", persist what remains
await this.indexForConditions.put(condition, trimmed); await this.indexForConditions.put(condition, trimmed);
} else { } else {
// Otherwise just delete the "account" // Otherwise just delete the "account"
await this.indexForConditions.del(condition); await this.indexForConditions.del(condition);
} }
// If complex conditions
if (this.isComplexCondition(condition)) {
const pubkeys = this.getDistinctPubkeysFromCondition(condition);
await this.trimComplexeConditionPubkeys(pubkeys, id);
}
} }
/**
* Duplicate with trimConditions?!
* @param writtenOn
* @param id
*/
private async trimWrittenOn(writtenOn: number, id: string) { private async trimWrittenOn(writtenOn: number, id: string) {
const k = LevelDBSindex.trimWrittenOnKey(writtenOn); const k = LevelDBSindex.trimWrittenOnKey(writtenOn);
const existing = await this.getWrittenOnSourceIds(writtenOn); const existing = await this.getWrittenOnSourceIds(writtenOn);
...@@ -369,6 +370,27 @@ export class LevelDBSindex extends LevelDBTable<SindexEntry> ...@@ -369,6 +370,27 @@ export class LevelDBSindex extends LevelDBTable<SindexEntry>
} }
} }
private async trimComplexeConditionPubkeys(pubkeys: string[], id: string) {
if (!pubkeys || !pubkeys.length) return;
for (const p of pubkeys) {
await this.trimComplexeConditionPubkey(p, id);
}
}
private async trimComplexeConditionPubkey(pubkey: string, id: string) {
// Get all the condition's sources
const existing = (await this.indexOfComplexeConditionForPubkeys.getOrNull(pubkey)) || [];
// Prune the source from the condition
const trimmed = arrayPruneAllCopy(existing, id);
if (trimmed.length) {
// If some sources are left for this "condition", persist what remains
await this.indexOfComplexeConditionForPubkeys.put(pubkey, trimmed);
} else {
// Otherwise just delete the "account"
await this.indexOfComplexeConditionForPubkeys.del(pubkey);
}
}
private async getWrittenOnSourceIds(writtenOn: number) { private async getWrittenOnSourceIds(writtenOn: number) {
const indexForTrimmingId = LevelDBSindex.trimWrittenOnKey(writtenOn); const indexForTrimmingId = LevelDBSindex.trimWrittenOnKey(writtenOn);
return (await this.indexForTrimming.getOrNull(indexForTrimmingId)) || []; return (await this.indexForTrimming.getOrNull(indexForTrimmingId)) || [];
...@@ -432,8 +454,7 @@ export class LevelDBSindex extends LevelDBTable<SindexEntry> ...@@ -432,8 +454,7 @@ export class LevelDBSindex extends LevelDBTable<SindexEntry>
arrCN.push(r); arrCN.push(r);
// If complex condition // If complex condition
if (!CommonConstants.TRANSACTION.OUTPUT_CONDITION_SIG_PUBKEY_UNIQUE.test(r.conditions)) { if (this.isComplexCondition(r.conditions)) {
// Fill complex condition by pubkey
const pubkeys = this.getDistinctPubkeysFromCondition(r.conditions); const pubkeys = this.getDistinctPubkeysFromCondition(r.conditions);
pubkeys.forEach((pub) => { pubkeys.forEach((pub) => {
let arrPub = byPubkeys[pub]; let arrPub = byPubkeys[pub];
...@@ -486,6 +507,9 @@ export class LevelDBSindex extends LevelDBTable<SindexEntry> ...@@ -486,6 +507,9 @@ export class LevelDBSindex extends LevelDBTable<SindexEntry>
} }
} }
private isComplexCondition(condition: string): boolean {
return condition && !CommonConstants.TRANSACTION.OUTPUT_CONDITION_SIG_PUBKEY_UNIQUE.test(condition) || false;
}
/** /**
* Get all pubkeys used by an output condition (e.g. 'SIG(A) && SIG(B)' will return ['A', 'B'] * Get all pubkeys used by an output condition (e.g. 'SIG(A) && SIG(B)' will return ['A', 'B']
* @param condition * @param condition
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment