From 6b239f29a9b3f8fb4f7a8984952737f24905eb90 Mon Sep 17 00:00:00 2001 From: Hjort Date: Fri, 14 Jul 2023 11:41:57 +0200 Subject: [PATCH 1/2] Add sendRawAccountTransaction entrypoint to gRPCClient --- packages/common/CHANGELOG.md | 6 ++++ packages/common/src/GRPCClient.ts | 57 ++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/packages/common/CHANGELOG.md b/packages/common/CHANGELOG.md index 1a6c28353..bfdc6f7e0 100644 --- a/packages/common/CHANGELOG.md +++ b/packages/common/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Added + +- `sendRawAccountTransaction` to the gRPC Client. + ## 9.0.0 ### Breaking changes diff --git a/packages/common/src/GRPCClient.ts b/packages/common/src/GRPCClient.ts index 1569caf23..33b3b9814 100644 --- a/packages/common/src/GRPCClient.ts +++ b/packages/common/src/GRPCClient.ts @@ -309,35 +309,68 @@ export class ConcordiumGRPCClient { transaction: v1.AccountTransaction, signature: v1.AccountTransactionSignature ): Promise { - const rawPayload = serializeAccountTransactionPayload(transaction); - const transactionSignature: v2.AccountTransactionSignature = - translate.accountTransactionSignatureToV2(signature); - - // Energy cost const accountTransactionHandler = getAccountTransactionHandler( transaction.type ); + + const rawPayload = serializeAccountTransactionPayload(transaction); + + // Energy cost const baseEnergyCost = accountTransactionHandler.getBaseEnergyCost( transaction.payload ); + const energyCost = calculateEnergyCost( countSignatures(signature), BigInt(rawPayload.length), baseEnergyCost ); + return this.sendRawAccountTransaction( + transaction.header, + energyCost, + rawPayload, + signature + ); + } + + /** + * Sends an account transaction, with an already serialized payload, to the node to be + * put in a block on the chain. + * + * Note that a transaction can still fail even if it was accepted by the node. + * To keep track of the transaction use getTransactionStatus. + * + * Note that { @link ConcordiumGRPCClient.sendAccountTransaction } is the recommended + * method to send account transactions, as this requires the caller to serialize the payload themselves. + * + * @param header the transactionheader to send to the node + * @param energyAmount the amount of energy allotted for the transaction + * @param payload the payload serialized to a buffer + * @param signature the signatures on the signing digest of the transaction + * @returns The transaction hash as a byte array + */ + async sendRawAccountTransaction( + header: v1.AccountTransactionHeader, + energyAmount: bigint, + payload: Buffer, + signature: v1.AccountTransactionSignature + ): Promise { + const transactionSignature: v2.AccountTransactionSignature = + translate.accountTransactionSignatureToV2(signature); + // Put together sendBlockItemRequest - const header: v2.AccountTransactionHeader = { - sender: { value: transaction.header.sender.decodedAddress }, - sequenceNumber: { value: transaction.header.nonce }, - energyAmount: { value: energyCost }, - expiry: { value: transaction.header.expiry.expiryEpochSeconds }, + const convertedHeader: v2.AccountTransactionHeader = { + sender: { value: header.sender.decodedAddress }, + sequenceNumber: { value: header.nonce }, + energyAmount: { value: energyAmount }, + expiry: { value: header.expiry.expiryEpochSeconds }, }; const accountTransaction: v2.AccountTransaction = { signature: transactionSignature, - header: header, + header: convertedHeader, payload: { - payload: { oneofKind: 'rawPayload', rawPayload: rawPayload }, + payload: { oneofKind: 'rawPayload', rawPayload: payload }, }, }; const sendBlockItemRequest: v2.SendBlockItemRequest = { From 76d03b69963639f5dd61a3b60b82531da60d2fa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Hjort?= <87635671+shjortConcordium@users.noreply.github.com> Date: Mon, 4 Sep 2023 10:16:36 +0200 Subject: [PATCH 2/2] Update packages/common/src/GRPCClient.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Søren Bruus Zeppelin --- packages/common/src/GRPCClient.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/common/src/GRPCClient.ts b/packages/common/src/GRPCClient.ts index 33b3b9814..351b3280f 100644 --- a/packages/common/src/GRPCClient.ts +++ b/packages/common/src/GRPCClient.ts @@ -341,8 +341,8 @@ export class ConcordiumGRPCClient { * Note that a transaction can still fail even if it was accepted by the node. * To keep track of the transaction use getTransactionStatus. * - * Note that { @link ConcordiumGRPCClient.sendAccountTransaction } is the recommended - * method to send account transactions, as this requires the caller to serialize the payload themselves. + * In general, { @link ConcordiumGRPCClient.sendAccountTransaction } is the recommended + * method to send account transactions, as this does not require the caller to serialize the payload themselves. * * @param header the transactionheader to send to the node * @param energyAmount the amount of energy allotted for the transaction