Skip to content

Commit

Permalink
Merge pull request #3341 from energywebfoundation/fix/optimize-retrie…
Browse files Browse the repository at this point in the history
…ving-get-logs

fix: optimize issuer and issuer-api transfer and claim
  • Loading branch information
soanvig authored Mar 8, 2022
2 parents eba5c80 + c8954c2 commit ab7c502
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class BatchClaimCertificatesHandler

return {
...claim,
creationTransactionHash: cert.creationTransactionHash,
schemaVersion: cert.schemaVersion
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class BatchTransferCertificatesHandler

return {
...transfer,
creationTransactionHash: cert.creationTransactionHash,
schemaVersion: cert.schemaVersion
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export class ClaimCertificateHandler implements ICommandHandler<ClaimCertificate
certificate.id,
blockchainProperties,
certificate.schemaVersion
).sync();
).sync({
creationTransactionHash: certificate.creationTransactionHash
});

const claimerBalance = BigNumber.from(
(certificate.issuedPrivately
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export class SyncCertificateHandler implements IEventHandler<SyncCertificateEven
certificate.id,
blockchainProperties,
certificate.schemaVersion
).sync();
).sync({
creationTransactionHash: certificate.creationTransactionHash
});

try {
const updateResult = await this.repository.update(certificate.id, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export class TransferCertificateHandler implements ICommandHandler<TransferCerti
certificate.id,
blockchainProperties,
certificate.schemaVersion
).sync();
).sync({
creationTransactionHash: certificate.creationTransactionHash,
});

if (certificate.issuedPrivately) {
const senderBalance = BigNumber.from(
Expand Down
32 changes: 21 additions & 11 deletions packages/traceability/issuer/src/blockchain-facade/Certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export interface ICertificate extends IData {
schemaVersion: CertificateSchemaVersion;
}

export interface ICertificateSyncParams {
creationTransactionHash?: string;
}

export class Certificate implements ICertificate {
public deviceId: string;

Expand Down Expand Up @@ -179,9 +183,10 @@ export class Certificate implements ICertificate {
blockchainProperties,
schemaVersion
);
newCertificate.creationTransactionHash = txHash;

return newCertificate.sync();
return newCertificate.sync({
creationTransactionHash: txHash
});
}

/**
Expand Down Expand Up @@ -241,13 +246,18 @@ export class Certificate implements ICertificate {
* @description Retrieves current data for a Certificate
*
*/
async sync(): Promise<Certificate> {
async sync(params: ICertificateSyncParams = {}): Promise<Certificate> {
if (this.id === null) {
return this;
}

const { registry } = this.blockchainProperties;

if (params.creationTransactionHash) {
// This will speed up getIssuanceTransaction required for starting block
this.creationTransactionHash = params.creationTransactionHash;
}

const issuanceTransaction = await this.getIssuanceTransaction();

const certOnChain = await registry.getCertificate(this.id);
Expand Down Expand Up @@ -501,23 +511,23 @@ export class Certificate implements ICertificate {
return await registry.provider.getTransactionReceipt(this.creationTransactionHash);
}

const allIssuanceLogs = await getEventsFromContract(
const allBatchIssuanceLogs = await getEventsFromContract(
registry,
registry.filters.IssuanceSingle(null, null, null)
registry.filters.IssuanceBatch(null, null, null, null)
);

let issuanceLog = allIssuanceLogs.find(
(event) => event._id.toString() === this.id.toString()
let issuanceLog = allBatchIssuanceLogs.find((event) =>
event._ids.map((id: BigNumber) => id.toString()).includes(this.id.toString())
);

if (!issuanceLog) {
const allBatchIssuanceLogs = await getEventsFromContract(
const allIssuanceLogs = await getEventsFromContract(
registry,
registry.filters.IssuanceBatch(null, null, null, null)
registry.filters.IssuanceSingle(null, null, null)
);

issuanceLog = allBatchIssuanceLogs.find((event) =>
event._ids.map((id: BigNumber) => id.toString()).includes(this.id.toString())
issuanceLog = allIssuanceLogs.find(
(event) => event._id.toString() === this.id.toString()
);

if (!issuanceLog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface BatchCertificateTransfer {
from?: string;
amount?: BigNumber;
schemaVersion: CertificateSchemaVersion;
creationTransactionHash?: string;
}

export interface BatchCertificateClaim {
Expand All @@ -24,6 +25,7 @@ export interface BatchCertificateClaim {
amount?: BigNumber;
claimData: IClaimData;
schemaVersion: CertificateSchemaVersion;
creationTransactionHash?: string;
}

/**
Expand Down Expand Up @@ -100,16 +102,20 @@ export async function transferCertificates(
certificateBatch: BatchCertificateTransfer[],
blockchainProperties: IBlockchainProperties
): Promise<ContractTransaction> {
const certificatesPromises = certificateBatch.map((cert) =>
new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync()
);

const { registry, activeUser } = blockchainProperties;
const registryWithSigner = registry.connect(activeUser);

const activeUserAddress = await activeUser.getAddress();

const certificates = await Promise.all(certificatesPromises);
const certsWithoutAmount = await Promise.all(
certificateBatch
.filter((b) => !b.amount) // we sync only certs that don't have amount configured
.map((cert) =>
new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync({
creationTransactionHash: cert.creationTransactionHash
})
)
);

return await registryWithSigner.safeBatchTransferFromMultiple(
certificateBatch.map((cert) => cert.from ?? activeUserAddress),
Expand All @@ -119,7 +125,7 @@ export async function transferCertificates(
(cert) =>
cert.amount ??
BigNumber.from(
certificates.find((certificate) => certificate.id === cert.id).owners[
certsWithoutAmount.find((certificate) => certificate.id === cert.id).owners[
cert.from ?? activeUserAddress
] ?? 0
)
Expand All @@ -138,16 +144,21 @@ export async function claimCertificates(
certificateBatch: BatchCertificateClaim[],
blockchainProperties: IBlockchainProperties
): Promise<ContractTransaction> {
const certificatesPromises = certificateBatch.map((cert) =>
new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync()
);
const certificates = await Promise.all(certificatesPromises);

const { activeUser, registry } = blockchainProperties;
const activeUserAddress = await activeUser.getAddress();

const registryWithSigner = registry.connect(activeUser);

const certsWithoutAmount = await Promise.all(
certificateBatch
.filter((c) => !c.amount)
.map((cert) =>
new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync({
creationTransactionHash: cert.creationTransactionHash
})
)
);

return await registryWithSigner.safeBatchTransferAndClaimFromMultiple(
certificateBatch.map((cert) => cert.from ?? activeUserAddress),
certificateBatch.map((cert) => cert.to ?? cert.from ?? activeUserAddress),
Expand All @@ -156,7 +167,7 @@ export async function claimCertificates(
(cert) =>
cert.amount ??
BigNumber.from(
certificates.find((certificate) => certificate.id === cert.id).owners[
certsWithoutAmount.find((certificate) => certificate.id === cert.id).owners[
cert.from ?? activeUserAddress
] ?? 0
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ export async function getAllCertificates(
);
}

return new Certificate(certificateId, blockchainProperties, schemaVersion).sync();
return new Certificate(certificateId, blockchainProperties, schemaVersion).sync({
creationTransactionHash: event.transactionHash
});
});

return Promise.all(certificatePromises);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class CertificationRequest implements ICertificationRequestBlockchain {

constructor(public id: number, public blockchainProperties: IBlockchainProperties) {}
/**
*
*
*
* @description Uses Issuer contract to create Certificate request
*
Expand Down Expand Up @@ -84,10 +84,10 @@ export class CertificationRequest implements ICertificationRequestBlockchain {

return newCertificationRequest.sync();
}
/**
*
/**
*
*
* @description Returns all Certification Requests for an Issuer
* @description Returns all Certification Requests for an Issuer
*
*/
public static async getAll(
Expand All @@ -105,8 +105,8 @@ export class CertificationRequest implements ICertificationRequestBlockchain {
);
}

/**
*
/**
*
*
* @description Retrieves Certificate data
*
Expand Down Expand Up @@ -152,8 +152,8 @@ export class CertificationRequest implements ICertificationRequestBlockchain {
return this;
}

/**
*
/**
*
*
* @description Uses Issuer contract to approve a Certificate request
*
Expand All @@ -177,8 +177,8 @@ export class CertificationRequest implements ICertificationRequestBlockchain {
return certificateId;
}

/**
*
/**
*
*
* @description Uses Issuer contract to revoke a Certificate request
*
Expand Down
2 changes: 1 addition & 1 deletion packages/traceability/issuer/test/Certificate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('Certificate tests', () => {
);
};

it('migrates Registry and Issuer', async () => {
before(async () => {
const registry = await migrateRegistry(provider, issuerPK);
const issuer = await migrateIssuer(provider, issuerPK, registry.address);

Expand Down

0 comments on commit ab7c502

Please sign in to comment.