Skip to content

Commit

Permalink
fix: Tx receipt serialization to JSON (#6711)
Browse files Browse the repository at this point in the history
Thought we had finally got rid of all serialization issues? Never!
spalladino authored May 28, 2024
1 parent 0f86674 commit 1d785fd
Showing 3 changed files with 34 additions and 1 deletion.
9 changes: 9 additions & 0 deletions yarn-project/circuit-types/src/tx/tx_hash.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { randomBytes } from '@aztec/foundation/crypto';
import { BufferReader, deserializeBigInt, serializeBigInt } from '@aztec/foundation/serialize';

/**
@@ -105,4 +106,12 @@ export class TxHash {
public static fromString(str: string): TxHash {
return new TxHash(Buffer.from(str, 'hex'));
}

/**
* Generates a random TxHash.
* @returns A new TxHash object.
*/
public static random(): TxHash {
return new TxHash(Buffer.from(randomBytes(TxHash.SIZE)));
}
}
23 changes: 23 additions & 0 deletions yarn-project/circuit-types/src/tx/tx_receipt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TxHash } from './tx_hash.js';
import { TxReceipt, TxStatus } from './tx_receipt.js';

describe('TxReceipt', () => {
it('serializes and deserializes from json', () => {
const receipt = new TxReceipt(
TxHash.random(),
TxStatus.SUCCESS,
'error',
BigInt(1),
Buffer.from('blockHash'),
undefined,
);

expect(TxReceipt.fromJSON(receipt.toJSON())).toEqual(receipt);
});

it('serializes and deserializes from json with undefined fields', () => {
const receipt = new TxReceipt(TxHash.random(), TxStatus.DROPPED, 'error', undefined, undefined, undefined);

expect(TxReceipt.fromJSON(receipt.toJSON())).toEqual(receipt);
});
});
3 changes: 2 additions & 1 deletion yarn-project/circuit-types/src/tx/tx_receipt.ts
Original file line number Diff line number Diff line change
@@ -66,6 +66,7 @@ export class TxReceipt {
error: this.error,
blockHash: this.blockHash?.toString('hex'),
blockNumber: this.blockNumber,
transactionFee: this.transactionFee?.toString(),
};
}

@@ -78,7 +79,7 @@ export class TxReceipt {
const txHash = TxHash.fromString(obj.txHash);
const status = obj.status as TxStatus;
const error = obj.error;
const transactionFee = obj.transactionFee;
const transactionFee = obj.transactionFee ? BigInt(obj.transactionFee) : undefined;
const blockHash = obj.blockHash ? Buffer.from(obj.blockHash, 'hex') : undefined;
const blockNumber = obj.blockNumber ? Number(obj.blockNumber) : undefined;
return new TxReceipt(txHash, status, error, transactionFee, blockHash, blockNumber);

0 comments on commit 1d785fd

Please sign in to comment.