Skip to content

Commit

Permalink
540 capture calldata cp (#546)
Browse files Browse the repository at this point in the history
* Log fileId and file size for callData

Signed-off-by: lukelee-sl <[email protected]>

* make createFile function private

Signed-off-by: lukelee-sl <[email protected]>

* createFile should only take callData

Signed-off-by: lukelee-sl <[email protected]>

* clear callData after file creation

Signed-off-by: lukelee-sl <[email protected]>

* do not create file for contracts <= 5120 bytes

Signed-off-by: lukelee-sl <[email protected]>

* Log fileId and file size for callData (#541)

Log details about the file created for callData when calling a transaction.

Signed-off-by: lukelee-sl <[email protected]>

Signed-off-by: lukelee-sl <[email protected]>
  • Loading branch information
lukelee-sl authored Sep 21, 2022
1 parent abab2db commit 11117a3
Showing 1 changed file with 71 additions and 4 deletions.
75 changes: 71 additions & 4 deletions packages/relay/src/lib/clients/sdkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ import {
Transaction,
TransactionRecord,
Status,
EthereumFlow
FileCreateTransaction,
FileAppendTransaction,
FileInfoQuery,
EthereumTransaction,
EthereumTransactionData,
} from '@hashgraph/sdk';
import { BigNumber } from '@hashgraph/sdk/lib/Transfer';
import { Logger } from "pino";
Expand Down Expand Up @@ -192,8 +196,23 @@ export class SDKClient {
}

async submitEthereumTransaction(transactionBuffer: Uint8Array, callerName: string, requestId?: string): Promise<TransactionResponse> {
return this.executeTransaction(new EthereumFlow()
.setEthereumData(transactionBuffer), callerName, requestId);
const ethereumTransactionData: EthereumTransactionData = EthereumTransactionData.fromBytes(transactionBuffer);
const ethereumTransaction = new EthereumTransaction();

if (ethereumTransactionData.toBytes().length <= 5120) {
ethereumTransaction.setEthereumData(ethereumTransactionData.toBytes());
} else {
const fileId = await this.createFile(ethereumTransactionData.callData, this.clientMain, requestId);

if(!fileId) {
const requestIdPrefix = formatRequestIdMessage(requestId);
throw new SDKClientError({}, `${requestIdPrefix} No fileId created for transaction. `);
}
ethereumTransactionData.callData = new Uint8Array();
ethereumTransaction.setEthereumData(ethereumTransactionData.toBytes()).setCallDataFileId(fileId)
}

return this.executeTransaction(ethereumTransaction, callerName, requestId);
}

async submitContractCallQuery(to: string, data: string, gas: number, from: string, callerName: string, requestId?: string): Promise<ContractFunctionResult> {
Expand Down Expand Up @@ -268,7 +287,7 @@ export class SDKClient {
}
};

private executeTransaction = async (transaction: Transaction | EthereumFlow, callerName: string, requestId?: string): Promise<TransactionResponse> => {
private executeTransaction = async (transaction: Transaction, callerName: string, requestId?: string): Promise<TransactionResponse> => {
const transactionType = transaction.constructor.name;
const requestIdPrefix = formatRequestIdMessage(requestId);
try {
Expand Down Expand Up @@ -351,4 +370,52 @@ export class SDKClient {
.to(HbarUnit.Tinybar)
.multipliedBy(constants.TINYBAR_TO_WEIBAR_COEF);
}

private createFile = async (callData: Uint8Array, client: Client, requestId?: string) => {
const requestIdPrefix = formatRequestIdMessage(requestId);
const hexedCallData = Buffer.from(callData).toString("hex");

const fileId = (
(
await (
await new FileCreateTransaction()
.setContents(hexedCallData.substring(0, 4096))
.setKeys(
client.operatorPublicKey
? [client.operatorPublicKey]
: []
)
.execute(client)
).getReceipt(client)
).fileId
);

if (fileId && callData.length > 4096) {
await (
await new FileAppendTransaction()
.setFileId(fileId)
.setContents(
hexedCallData.substring(4096, hexedCallData.length)
)
.setChunkSize(4096)
.execute(client)
).getReceipt(client);
}

// Ensure that the calldata file is not empty
if(fileId) {
const fileSize = await (
await new FileInfoQuery()
.setFileId(fileId)
.execute(client)
).size;

if(callData.length > 0 && fileSize.isZero()) {
throw new SDKClientError({}, `${requestIdPrefix} Created file is empty. `);
}
this.logger.trace(`${requestIdPrefix} Created file with fileId: ${fileId} and file size ${fileSize}`);
}

return fileId;
}
}

0 comments on commit 11117a3

Please sign in to comment.