Skip to content

Commit

Permalink
fix: add moduleAddress to key table
Browse files Browse the repository at this point in the history
  • Loading branch information
Amuhar committed Jul 24, 2023
1 parent eeba71e commit 85a2bbd
Show file tree
Hide file tree
Showing 54 changed files with 133 additions and 3,273 deletions.
1 change: 1 addition & 0 deletions src/common/registry/fetch/interfaces/key.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface RegistryKey {
depositSignature: string;
key: string;
used: boolean;
moduleAddress: string;
}

export type KeyBatchRecord = [string, string, boolean[]] & {
Expand Down
22 changes: 17 additions & 5 deletions src/common/registry/fetch/key-batch.fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ export class RegistryKeyBatchFetchService {
return this.splitMergedRecord(unformattedKeys, KEYS_LENGTH);
}

public formatKeys(operatorIndex: number, unformattedRecords: KeyBatchRecord, startIndex: number): RegistryKey[] {
public formatKeys(
operatorIndex: number,
unformattedRecords: KeyBatchRecord,
startIndex: number,
moduleAddress: string,
): RegistryKey[] {
const keys = this.unformattedKeysToArray(unformattedRecords[0]);
const signatures = this.unformattedSignaturesToArray(unformattedRecords[1]);
const usedStatuses = unformattedRecords[2];
Expand All @@ -65,6 +70,7 @@ export class RegistryKeyBatchFetchService {
key: keys[chunkIndex],
depositSignature: signatures[chunkIndex],
used,
moduleAddress,
};
});
}
Expand All @@ -74,25 +80,31 @@ export class RegistryKeyBatchFetchService {
operatorIndex: number,
fromIndex = 0,
toIndex = -1,
moduleAddress: string,
overrides: CallOverrides = {},
): Promise<RegistryKey[]> {
if (fromIndex > toIndex && toIndex !== -1) {
throw new Error('fromIndex is greater than or equal to toIndex');
}

if (toIndex == null || toIndex === -1) {
const operator = await this.operatorsService.fetchOne(operatorIndex, overrides);
const operator = await this.operatorsService.fetchOne(operatorIndex, moduleAddress, overrides);

toIndex = operator.totalSigningKeys;
}

const [offset, limit] = this.convertIndicesToOffsetAndTotal(fromIndex, toIndex);
const unformattedKeys = await this.fetchSigningKeysInBatches(operatorIndex, offset, limit);
const unformattedKeys = await this.fetchSigningKeysInBatches(operatorIndex, offset, limit, moduleAddress);

return unformattedKeys;
}

public async fetchSigningKeysInBatches(operatorIndex: number, fromIndex: number, totalAmount: number) {
public async fetchSigningKeysInBatches(
operatorIndex: number,
fromIndex: number,
totalAmount: number,
moduleAddress: string,
) {
// TODO: move to constants/config cause this limit depends on eth node
const batchSize = 1100;

Expand All @@ -105,7 +117,7 @@ export class RegistryKeyBatchFetchService {

const promise = (async () => {
const keys = await this.contract.getSigningKeys(operatorIndex, currentFromIndex, currentBatchSize);
return this.formatKeys(operatorIndex, keys, currentFromIndex);
return this.formatKeys(operatorIndex, keys, currentFromIndex, moduleAddress);
})();

promises.push(promise);
Expand Down
13 changes: 10 additions & 3 deletions src/common/registry/fetch/key.fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export class RegistryKeyFetchService {
) {}

/** fetches one key */
public async fetchOne(operatorIndex: number, keyIndex: number, overrides: CallOverrides = {}): Promise<RegistryKey> {
public async fetchOne(
operatorIndex: number,
keyIndex: number,
moduleAddress: string,
overrides: CallOverrides = {},
): Promise<RegistryKey> {
const keyData = await this.contract.getSigningKey(operatorIndex, keyIndex, overrides as any);

const { key, depositSignature, used } = keyData;
Expand All @@ -28,6 +33,7 @@ export class RegistryKeyFetchService {
key,
depositSignature,
used,
moduleAddress,
};
}

Expand All @@ -36,20 +42,21 @@ export class RegistryKeyFetchService {
operatorIndex: number,
fromIndex = 0,
toIndex = -1,
moduleAddress: string,
overrides: CallOverrides = {},
): Promise<RegistryKey[]> {
if (fromIndex > toIndex && toIndex !== -1) {
throw new Error('fromIndex is greater than or equal to toIndex');
}

if (toIndex == null || toIndex === -1) {
const operator = await this.operatorsService.fetchOne(operatorIndex, overrides);
const operator = await this.operatorsService.fetchOne(operatorIndex, moduleAddress, overrides);

toIndex = operator.totalSigningKeys;
}

const fetcher = async (keyIndex: number) => {
return await this.fetchOne(operatorIndex, keyIndex, overrides);
return await this.fetchOne(operatorIndex, keyIndex, moduleAddress, overrides);
};

const batchSize = REGISTRY_KEY_BATCH_SIZE;
Expand Down
3 changes: 2 additions & 1 deletion src/common/registry/fetch/meta.fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export class RegistryMetaFetchService {
constructor(@Inject(REGISTRY_CONTRACT_TOKEN) private registryContract: Registry) {}

/** fetches keys operation index */
public async fetchKeysOpIndex(overrides: CallOverrides = {}): Promise<number> {
public async fetchKeysOpIndex(overrides: CallOverrides = {}, moduleAddress: string): Promise<number> {
// TODO: read data from all contract that implement curated-v1-onchain type
const bigNumber = await this.registryContract.getKeysOpIndex(overrides as any);
return bigNumber.toNumber();
}
Expand Down
15 changes: 12 additions & 3 deletions src/common/registry/fetch/operator.fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export class RegistryOperatorFetchService {
}

/** fetches one operator */
public async fetchOne(operatorIndex: number, overrides: CallOverrides = {}): Promise<RegistryOperator> {
public async fetchOne(
operatorIndex: number,
moduleAddress: string,
overrides: CallOverrides = {},
): Promise<RegistryOperator> {
const fullInfo = true;
const operator = await this.contract.getNodeOperator(operatorIndex, fullInfo, overrides as any);

Expand All @@ -47,7 +51,12 @@ export class RegistryOperatorFetchService {
}

/** fetches operators */
public async fetch(fromIndex = 0, toIndex = -1, overrides: CallOverrides = {}): Promise<RegistryOperator[]> {
public async fetch(
fromIndex = 0,
toIndex = -1,
moduleAddress: string,
overrides: CallOverrides = {},
): Promise<RegistryOperator[]> {
if (fromIndex > toIndex && toIndex !== -1) {
throw new Error('fromIndex is greater than or equal to toIndex');
}
Expand All @@ -57,7 +66,7 @@ export class RegistryOperatorFetchService {
}

const fetcher = async (operatorIndex: number) => {
return await this.fetchOne(operatorIndex, overrides);
return await this.fetchOne(operatorIndex, moduleAddress, overrides);
};

const batchSize = REGISTRY_OPERATORS_BATCH_SIZE;
Expand Down
24 changes: 15 additions & 9 deletions src/common/registry/main/abstract-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ export abstract class AbstractRegistryService {
) {}

/** collects changed data from the contract and store it to the db */
public async update(blockHashOrBlockTag: string | number) {
public async update(blockHashOrBlockTag: string | number, moduleAddress: string) {
// TODO: remove everything before operators fetching ans saving
const prevMeta = await this.getMetaDataFromStorage();
const currMeta = await this.getMetaDataFromContract(blockHashOrBlockTag);
const currMeta = await this.getMetaDataFromContract(blockHashOrBlockTag, moduleAddress);

const isSameContractState = compareMeta(prevMeta, currMeta);

Expand Down Expand Up @@ -80,7 +81,7 @@ export abstract class AbstractRegistryService {
const blockHash = currMeta.blockHash;

const previousOperators = await this.getOperatorsFromStorage();
const currentOperators = await this.getOperatorsFromContract(blockHash);
const currentOperators = await this.getOperatorsFromContract(blockHash, moduleAddress);

this.logger.log('Collected operators', {
previousOperators: previousOperators.length,
Expand All @@ -96,7 +97,7 @@ export abstract class AbstractRegistryService {
currMeta,
});

await this.syncUpdatedKeysWithContract(previousOperators, currentOperators, blockHash);
await this.syncUpdatedKeysWithContract(previousOperators, currentOperators, blockHash, moduleAddress);
},
{ isolationLevel: IsolationLevel.READ_COMMITTED },
);
Expand All @@ -106,13 +107,16 @@ export abstract class AbstractRegistryService {

/** contract */
/** returns the meta data from the contract */
public async getMetaDataFromContract(blockHashOrBlockTag: string | number): Promise<RegistryMeta> {
public async getMetaDataFromContract(
blockHashOrBlockTag: string | number,
moduleAddress: string,
): Promise<RegistryMeta> {
const { provider } = this.registryContract;
const block = await provider.getBlock(blockHashOrBlockTag);
const blockHash = block.hash;
const blockTag = { blockHash };

const keysOpIndex = await this.metaFetch.fetchKeysOpIndex({ blockTag });
const keysOpIndex = await this.metaFetch.fetchKeysOpIndex({ blockTag }, moduleAddress);

return {
blockNumber: block.number,
Expand All @@ -123,9 +127,9 @@ export abstract class AbstractRegistryService {
}

/** returns operators from the contract */
public async getOperatorsFromContract(blockHash: string) {
public async getOperatorsFromContract(blockHash: string, moduleAddress: string) {
const overrides = { blockTag: { blockHash } };
return await this.operatorFetch.fetch(0, -1, overrides);
return await this.operatorFetch.fetch(0, -1, moduleAddress, overrides);
}

/** returns the right border of the update keys range */
Expand All @@ -136,6 +140,7 @@ export abstract class AbstractRegistryService {
previousOperators: RegistryOperator[],
currentOperators: RegistryOperator[],
blockHash: string,
moduleAddress: string,
) {
// TODO: disable console time after testing
console.time('FETCH_OPERATORS');
Expand All @@ -162,7 +167,8 @@ export abstract class AbstractRegistryService {
const operatorIndex = currOperator.index;
const overrides = { blockTag: { blockHash } };
// TODO: use feature flag
const result = await this.keyBatchFetch.fetch(operatorIndex, fromIndex, toIndex, overrides);
const result = await this.keyBatchFetch.fetch(operatorIndex, fromIndex, toIndex, moduleAddress, overrides);
// add moduleAddress
const operatorKeys = result.filter((key) => key);

this.logger.log('Keys fetched', {
Expand Down
4 changes: 4 additions & 0 deletions src/common/registry/storage/key.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class RegistryKey {
this.key = operatorKey.key.toLocaleLowerCase();
this.depositSignature = operatorKey.depositSignature.toLocaleLowerCase();
this.used = operatorKey.used;
this.moduleAddress = operatorKey.moduleAddress;
}

@PrimaryKey()
Expand All @@ -28,4 +29,7 @@ export class RegistryKey {

@Property()
used!: boolean;

@Property({ length: 42 })
moduleAddress!: string;
}
61 changes: 0 additions & 61 deletions src/common/registry/test/fetch/async.spec.ts

This file was deleted.

53 changes: 0 additions & 53 deletions src/common/registry/test/fetch/key-batch.fetch.spec.ts

This file was deleted.

Loading

0 comments on commit 85a2bbd

Please sign in to comment.