Skip to content

Commit

Permalink
fix(api-sync): populate public key column again (#728)
Browse files Browse the repository at this point in the history
* populate public key column again

* empty commit

* improve upsert conflict resolution
  • Loading branch information
oXtxNt9U authored Oct 14, 2024
1 parent e806876 commit 0637bf5
Showing 1 changed file with 47 additions and 20 deletions.
67 changes: 47 additions & 20 deletions packages/api-sync/source/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export class Sync implements Contracts.ApiSync.Service {
@inject(Identifiers.Application.Instance)
private readonly app!: Contracts.Kernel.Application;

@inject(Identifiers.Cryptography.Identity.Address.Factory)
private readonly addressFactory!: Contracts.Crypto.AddressFactory;

@inject(Identifiers.Cryptography.Configuration)
private readonly configuration!: Contracts.Crypto.Configuration;

Expand Down Expand Up @@ -119,29 +122,41 @@ export class Sync implements Contracts.ApiSync.Service {
proof,
} = commit;

const addressToPublicKey: Record<string, string> = {};
const publicKeyToAddress: Record<string, string> = {};
const transactionReceipts: Models.Receipt[] = [];

let receipts: Map<string, Contracts.Evm.TransactionReceipt> | undefined;

if (unit.hasProcessorResult()) {
const processResult = unit.getProcessorResult();
const { receipts } = processResult;

for (const transaction of transactions) {
const receipt = receipts.get(transaction.id);
if (receipt) {
transactionReceipts.push({
blockHeight: header.height.toFixed(),
deployedContractAddress: receipt.deployedContractAddress,
gasRefunded: Number(receipt.gasRefunded),
gasUsed: Number(receipt.gasUsed),
id: transaction.id,
logs: receipt.logs,
output: receipt.output,
success: receipt.success,
});
}
receipts = unit.getProcessorResult().receipts;
}

for (const transaction of transactions) {
const { senderPublicKey } = transaction.data;
if (!publicKeyToAddress[senderPublicKey]) {
const address = await this.addressFactory.fromPublicKey(senderPublicKey);
publicKeyToAddress[senderPublicKey] = address;
addressToPublicKey[address] = senderPublicKey;
}

const receipt = receipts?.get(transaction.id);
if (receipt) {
transactionReceipts.push({
blockHeight: header.height.toFixed(),
deployedContractAddress: receipt.deployedContractAddress,
gasRefunded: Number(receipt.gasRefunded),
gasUsed: Number(receipt.gasUsed),
id: transaction.id,
logs: receipt.logs,
output: receipt.output,
success: receipt.success,
});
}
}

const accountUpdates: Array<Contracts.Evm.AccountUpdate> = unit.getAccountUpdates();

// temporary workaround for API to find validator wallets
const activeValidators = this.validatorSet.getActiveValidators().reduce((accumulator, current) => {
accumulator[current.address] = current;
Expand Down Expand Up @@ -198,10 +213,10 @@ export class Sync implements Contracts.ApiSync.Service {
// temporary workaround
attributes: activeValidators[account.address]
? { validatorPublicKey: activeValidators[account.address].blsPublicKey }
: [],
: {},
balance: Utils.BigNumber.make(account.balance).toFixed(),
nonce: Utils.BigNumber.make(account.nonce).toFixed(),
publicKey: "",
publicKey: addressToPublicKey[account.address] ?? "",
updated_at: header.height.toFixed(),
})),

Expand Down Expand Up @@ -386,7 +401,19 @@ export class Sync implements Contracts.ApiSync.Service {
.execute();
}

await walletRepository.upsert(deferred.wallets, ["address"]);
await walletRepository
.createQueryBuilder()
.insert()
.values(deferred.wallets)
.onConflict(
`("address") DO UPDATE SET
balance = COALESCE(EXCLUDED.balance, "Wallet".balance),
nonce = COALESCE(EXCLUDED.nonce, "Wallet".nonce),
updated_at = COALESCE(EXCLUDED.updated_at, "Wallet".updated_at),
public_key = COALESCE(NULLIF(EXCLUDED.public_key, ''), "Wallet".public_key),
attributes = "Wallet".attributes || EXCLUDED.attributes`,
)
.execute();
});

const t1 = performance.now();
Expand Down

0 comments on commit 0637bf5

Please sign in to comment.