Skip to content

Commit 11117a3

Browse files
authored
540 capture calldata cp (#546)
* 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]>
1 parent abab2db commit 11117a3

File tree

1 file changed

+71
-4
lines changed

1 file changed

+71
-4
lines changed

packages/relay/src/lib/clients/sdkClient.ts

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ import {
3939
Transaction,
4040
TransactionRecord,
4141
Status,
42-
EthereumFlow
42+
FileCreateTransaction,
43+
FileAppendTransaction,
44+
FileInfoQuery,
45+
EthereumTransaction,
46+
EthereumTransactionData,
4347
} from '@hashgraph/sdk';
4448
import { BigNumber } from '@hashgraph/sdk/lib/Transfer';
4549
import { Logger } from "pino";
@@ -192,8 +196,23 @@ export class SDKClient {
192196
}
193197

194198
async submitEthereumTransaction(transactionBuffer: Uint8Array, callerName: string, requestId?: string): Promise<TransactionResponse> {
195-
return this.executeTransaction(new EthereumFlow()
196-
.setEthereumData(transactionBuffer), callerName, requestId);
199+
const ethereumTransactionData: EthereumTransactionData = EthereumTransactionData.fromBytes(transactionBuffer);
200+
const ethereumTransaction = new EthereumTransaction();
201+
202+
if (ethereumTransactionData.toBytes().length <= 5120) {
203+
ethereumTransaction.setEthereumData(ethereumTransactionData.toBytes());
204+
} else {
205+
const fileId = await this.createFile(ethereumTransactionData.callData, this.clientMain, requestId);
206+
207+
if(!fileId) {
208+
const requestIdPrefix = formatRequestIdMessage(requestId);
209+
throw new SDKClientError({}, `${requestIdPrefix} No fileId created for transaction. `);
210+
}
211+
ethereumTransactionData.callData = new Uint8Array();
212+
ethereumTransaction.setEthereumData(ethereumTransactionData.toBytes()).setCallDataFileId(fileId)
213+
}
214+
215+
return this.executeTransaction(ethereumTransaction, callerName, requestId);
197216
}
198217

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

271-
private executeTransaction = async (transaction: Transaction | EthereumFlow, callerName: string, requestId?: string): Promise<TransactionResponse> => {
290+
private executeTransaction = async (transaction: Transaction, callerName: string, requestId?: string): Promise<TransactionResponse> => {
272291
const transactionType = transaction.constructor.name;
273292
const requestIdPrefix = formatRequestIdMessage(requestId);
274293
try {
@@ -351,4 +370,52 @@ export class SDKClient {
351370
.to(HbarUnit.Tinybar)
352371
.multipliedBy(constants.TINYBAR_TO_WEIBAR_COEF);
353372
}
373+
374+
private createFile = async (callData: Uint8Array, client: Client, requestId?: string) => {
375+
const requestIdPrefix = formatRequestIdMessage(requestId);
376+
const hexedCallData = Buffer.from(callData).toString("hex");
377+
378+
const fileId = (
379+
(
380+
await (
381+
await new FileCreateTransaction()
382+
.setContents(hexedCallData.substring(0, 4096))
383+
.setKeys(
384+
client.operatorPublicKey
385+
? [client.operatorPublicKey]
386+
: []
387+
)
388+
.execute(client)
389+
).getReceipt(client)
390+
).fileId
391+
);
392+
393+
if (fileId && callData.length > 4096) {
394+
await (
395+
await new FileAppendTransaction()
396+
.setFileId(fileId)
397+
.setContents(
398+
hexedCallData.substring(4096, hexedCallData.length)
399+
)
400+
.setChunkSize(4096)
401+
.execute(client)
402+
).getReceipt(client);
403+
}
404+
405+
// Ensure that the calldata file is not empty
406+
if(fileId) {
407+
const fileSize = await (
408+
await new FileInfoQuery()
409+
.setFileId(fileId)
410+
.execute(client)
411+
).size;
412+
413+
if(callData.length > 0 && fileSize.isZero()) {
414+
throw new SDKClientError({}, `${requestIdPrefix} Created file is empty. `);
415+
}
416+
this.logger.trace(`${requestIdPrefix} Created file with fileId: ${fileId} and file size ${fileSize}`);
417+
}
418+
419+
return fileId;
420+
}
354421
}

0 commit comments

Comments
 (0)