Skip to content

Commit 981b1ff

Browse files
sebastijankuznerCopilot
andauthored
feat(crypto-transaction): use worker (#1281)
* Remove comment * Prepare factory * Switch BigInt * Enable worker * Fix crypto-transaction tests * Fix crypto-block tests * Fix crypto-commit tests Co-authored-by: Copilot <copilot@github.com> * Fix database tests Co-authored-by: Copilot <copilot@github.com> * style: resolve style guide violations [ci-lint-fix] * Fix functional tests Co-authored-by: Copilot <copilot@github.com> * Fix sender mempool --------- Co-authored-by: Copilot <copilot@github.com>
1 parent 9adf753 commit 981b1ff

29 files changed

Lines changed: 180 additions & 179 deletions

File tree

packages/api-sync/source/restore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ export class Restore {
493493
? [...new Set(parsedMultiPayments.map((mp) => mp.to))]
494494
: undefined,
495495

496-
nonce: transaction.nonce.toFixed(),
496+
nonce: transaction.nonce.toString(),
497497

498498
output: receipt.output,
499499
senderPublicKey: transaction.senderPublicKey,
@@ -502,7 +502,7 @@ export class Restore {
502502
timestamp: block.timestamp.toFixed(),
503503
to: transaction.to,
504504
transactionIndex: transaction.transactionIndex!,
505-
value: transaction.value.toFixed(),
505+
value: transaction.value.toString(),
506506
});
507507

508508
multiPayments.push(...parsedMultiPayments);

packages/api-sync/source/service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export class Sync implements Contracts.ApiSync.Service {
218218
multiPaymentRecipients:
219219
parsedMultiPayments.length > 0 ? [...new Set(parsedMultiPayments.map((mp) => mp.to))] : undefined,
220220

221-
nonce: transaction.nonce.toFixed(),
221+
nonce: transaction.nonce.toString(),
222222

223223
output: receipt.output,
224224
senderPublicKey: transaction.senderPublicKey,
@@ -227,7 +227,7 @@ export class Sync implements Contracts.ApiSync.Service {
227227
timestamp: header.timestamp.toFixed(),
228228
to: transaction.to,
229229
transactionIndex: transaction.transactionIndex!,
230-
value: transaction.value.toFixed(),
230+
value: transaction.value.toString(),
231231
});
232232

233233
multiPayments.push(...parsedMultiPayments);

packages/configuration-generator/source/generators/genesis-block.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,11 @@ export class GenesisBlockGenerator extends Generator {
288288
from: transaction.from,
289289
gasLimit: BigInt(transaction.gasLimit),
290290
gasPrice: BigInt(transaction.gasPrice),
291-
nonce: transaction.nonce.toBigInt(),
291+
nonce: transaction.nonce,
292292
specId: Enums.Evm.SpecId.SHANGHAI,
293293
to: transaction.to,
294294
txHash: transaction.hash,
295-
value: transaction.value.toBigInt(),
295+
value: transaction.value,
296296
});
297297

298298
totals.fee = totals.fee.plus(transaction.gasPrice);

packages/contracts/source/contracts/crypto/transactions.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { BigNumber } from "@mainsail/utils";
2-
31
import type { TransactionStorageData } from "../evm/storage.js";
42
import type { EcdsaSignature, KeyPair } from "./identities.js";
53
import type { SchemaValidationResult } from "./validator.js";
@@ -8,11 +6,11 @@ export interface TransactionUnsignedSerializable {
86
readonly network: number;
97

108
readonly to?: string;
11-
readonly value: BigNumber;
9+
readonly value: bigint;
1210

1311
readonly gasPrice: number;
1412
readonly gasLimit: number;
15-
readonly nonce: BigNumber;
13+
readonly nonce: bigint;
1614
readonly data: string;
1715
}
1816

@@ -115,6 +113,7 @@ export interface TransactionFactory {
115113
fromJson(json: TransactionJson): Promise<Transaction>;
116114
fromData(data: TransactionSerializable, strict?: boolean): Promise<Transaction>;
117115
fromStorage(data: TransactionStorageDataExtended): Promise<BlockTransaction>;
116+
computeCryptoData(data: TransactionSerializable): Promise<TransactionCryptoData>;
118117
}
119118

120119
export interface TransactionHashFactory {

packages/crypto-block/source/factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ export class BlockFactory implements Contracts.Crypto.BlockFactory {
107107
reward: BigNumber.make(json.reward),
108108
transactions: json.transactions.map((tx) => ({
109109
...tx,
110-
nonce: BigNumber.make(tx.nonce),
111-
value: BigNumber.make(tx.value),
110+
nonce: BigInt(tx.nonce),
111+
value: BigInt(tx.value),
112112
})),
113113
};
114114

packages/crypto-block/test/fixtures/transactions.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Contracts } from "@mainsail/contracts";
2-
import { BigNumber } from "@mainsail/utils";
32

43
const convertToTransactionJson = (data: Contracts.Crypto.TransactionData): Contracts.Crypto.TransactionJson => ({
54
data: data.data,
@@ -45,14 +44,14 @@ export const transaction1Data: Contracts.Crypto.TransactionData = {
4544
gasPrice: 5_000_000_000,
4645
hash: "65d43d2bed464b6bb1df8d6c0136316d3a3559569904fb16d5e3a8d71ebc2ebc",
4746
network: 10_000,
48-
nonce: BigNumber.ZERO,
47+
nonce: 0n,
4948
r: "921101a4583fb153ec00e501f3c2e2636114e1c8c58d2df8a19426cc066a6768",
5049
s: "22db4bce1e0ace485ce0838d178b4d5bcfa9f69b315a14c580d9b01e5c980bdd",
5150
senderLegacyAddress: "DUQKzkR4BP5UcWayJMpRXtpEXuw4zPbWhe",
5251
senderPublicKey: "02daa6f404dbd49c9c74a3d88d65b967a9b51d3465c92833e8e2ede11e7242f014",
5352
to: "0xBe89811e15f611C1db12e59679b6F3DC1F430155",
5453
v: 0,
55-
value: BigNumber.ZERO,
54+
value: 0n,
5655
};
5756

5857
export const transaction1Json: Contracts.Crypto.TransactionJson = convertToTransactionJson(transaction1Data);
@@ -64,14 +63,14 @@ export const transaction2Data: Contracts.Crypto.TransactionData = {
6463
gasPrice: 5_000_000_000,
6564
hash: "7a74379424e07173c137dd89c29f497b35d8013815b03096b51e67f87c453574",
6665
network: 10_000,
67-
nonce: BigNumber.ONE,
66+
nonce: 1n,
6867
r: "6c9842bc78c2f68468cbf8a8f3fec0ae2679707ffb606a4b373dd01a02af55fc",
6968
s: "1a4c4d984d750678fb204ce8b7e97d860c974ac1a423f098dc4921acd2be0c7d",
7069
senderLegacyAddress: "DQJTK7of6bPUfJEuL9gUV4qnUyq72eskKe",
7170
senderPublicKey: "03043bfdf530d59e919323a33d0c8f5ca43f6b50dfe753c9b3e987a4a5233a2a15",
7271
to: "0xBe89811e15f611C1db12e59679b6F3DC1F430155",
7372
v: 0,
74-
value: BigNumber.ZERO,
73+
value: 0n,
7574
};
7675

7776
export const transactionsData = [transaction1Data, transaction2Data];

packages/crypto-commit/test/fixtures/block.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Contracts } from "@mainsail/contracts";
2+
23
import { BigNumber } from "@mainsail/utils";
34

45
export const validatorMnemonic =
@@ -52,15 +53,15 @@ export const tx1 = {
5253
gasPrice: 5_000_000_000,
5354
// hash: "65d43d2bed464b6bb1df8d6c0136316d3a3559569904fb16d5e3a8d71ebc2ebc",
5455
network: 10_000,
55-
nonce: BigNumber.ZERO,
56+
nonce: 0n,
5657
r: "921101a4583fb153ec00e501f3c2e2636114e1c8c58d2df8a19426cc066a6768",
5758
s: "22db4bce1e0ace485ce0838d178b4d5bcfa9f69b315a14c580d9b01e5c980bdd",
5859
// senderLegacyAddress: "DUQKzkR4BP5UcWayJMpRXtpEXuw4zPbWhe",
5960
// senderPublicKey: "02daa6f404dbd49c9c74a3d88d65b967a9b51d3465c92833e8e2ede11e7242f014",
6061
to: "0xBe89811e15f611C1db12e59679b6F3DC1F430155",
6162
// transactionIndex: 0,
6263
v: 0,
63-
value: BigNumber.ZERO,
64+
value: 0n,
6465
};
6566

6667
export const tx1Storage: Contracts.Evm.TransactionStorageData = {
@@ -69,16 +70,16 @@ export const tx1Storage: Contracts.Evm.TransactionStorageData = {
6970
from: "0xE33074cBE63A1f86cc8EAb97b2099732F15284fE",
7071
gasLimit: BigInt(1_000_000),
7172
gasPrice: BigInt(5_000_000_000),
72-
txHash: "65d43d2bed464b6bb1df8d6c0136316d3a3559569904fb16d5e3a8d71ebc2ebc",
73-
nonce: BigInt(0),
73+
index: 0,
74+
nonce: 0n,
7475
r: "921101a4583fb153ec00e501f3c2e2636114e1c8c58d2df8a19426cc066a6768",
7576
s: "22db4bce1e0ace485ce0838d178b4d5bcfa9f69b315a14c580d9b01e5c980bdd",
7677
// senderLegacyAddress: "DUQKzkR4BP5UcWayJMpRXtpEXuw4zPbWhe",
7778
senderPublicKey: "02daa6f404dbd49c9c74a3d88d65b967a9b51d3465c92833e8e2ede11e7242f014",
7879
to: "0xBe89811e15f611C1db12e59679b6F3DC1F430155",
79-
index: 0,
80+
txHash: "65d43d2bed464b6bb1df8d6c0136316d3a3559569904fb16d5e3a8d71ebc2ebc",
8081
v: 0,
81-
value: BigInt(0),
82+
value: 0n,
8283
};
8384

8485
export const tx1Serialized =
@@ -91,15 +92,15 @@ export const tx2 = {
9192
gasPrice: 5_000_000_000,
9293
// hash: "7a74379424e07173c137dd89c29f497b35d8013815b03096b51e67f87c453574",
9394
network: 10_000,
94-
nonce: BigNumber.ONE,
95+
nonce: 1n,
9596
r: "6c9842bc78c2f68468cbf8a8f3fec0ae2679707ffb606a4b373dd01a02af55fc",
9697
s: "1a4c4d984d750678fb204ce8b7e97d860c974ac1a423f098dc4921acd2be0c7d",
9798
// senderLegacyAddress: "DQJTK7of6bPUfJEuL9gUV4qnUyq72eskKe",
9899
// senderPublicKey: "03043bfdf530d59e919323a33d0c8f5ca43f6b50dfe753c9b3e987a4a5233a2a15",
99100
to: "0xBe89811e15f611C1db12e59679b6F3DC1F430155",
100101
// transactionIndex: 1,
101102
v: 0,
102-
value: BigNumber.ZERO,
103+
value: 0n,
103104
};
104105

105106
export const tx2Storage: Contracts.Evm.TransactionStorageData = {
@@ -108,14 +109,14 @@ export const tx2Storage: Contracts.Evm.TransactionStorageData = {
108109
from: "0x8ae872F64bA0731f66F847f4e8Fc0796dF0bCc08",
109110
gasLimit: BigInt(1_000_000),
110111
gasPrice: BigInt(5_000_000_000),
111-
txHash: "7a74379424e07173c137dd89c29f497b35d8013815b03096b51e67f87c453574",
112+
index: 1,
112113
nonce: BigInt(1),
113114
r: "6c9842bc78c2f68468cbf8a8f3fec0ae2679707ffb606a4b373dd01a02af55fc",
114115
s: "1a4c4d984d750678fb204ce8b7e97d860c974ac1a423f098dc4921acd2be0c7d",
115116
// senderLegacyAddress: "DQJTK7of6bPUfJEuL9gUV4qnUyq72eskKe",
116117
senderPublicKey: "03043bfdf530d59e919323a33d0c8f5ca43f6b50dfe753c9b3e987a4a5233a2a15",
117118
to: "0xBe89811e15f611C1db12e59679b6F3DC1F430155",
118-
index: 1,
119+
txHash: "7a74379424e07173c137dd89c29f497b35d8013815b03096b51e67f87c453574",
119120
v: 0,
120121
value: BigInt(0),
121122
};
@@ -135,15 +136,15 @@ export const blockDataJson: Contracts.Crypto.BlockJson = {
135136
transactions: [
136137
{
137138
...tx1,
139+
data: "0x",
138140
gasPrice: Number(tx1.gasPrice),
139141
value: tx1.value.toString(),
140-
data: "0x",
141142
},
142143
{
143144
...tx2,
145+
data: "0x",
144146
gasPrice: Number(tx2.gasPrice),
145147
value: tx2.value.toString(),
146-
data: "0x",
147148
},
148149
],
149150
};

packages/crypto-transaction/source/builder.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { describe } from "@mainsail/test-runner";
33

44
import { TransactionBuilder } from "./builder.js";
55
import { prepareSandbox } from "../test/helpers/prepare-sandbox";
6-
import { BigNumber } from "@mainsail/utils";
76
import { MissingTransactionSignatureError, ValidationFailed } from "@mainsail/exceptions";
87

98
const wallet = {
@@ -33,10 +32,10 @@ describe<{
3332
senderPublicKey: wallet.publicKey,
3433
senderLegacyAddress: wallet.legacyAddress,
3534
to: undefined,
36-
value: BigNumber.ZERO,
35+
value: 0n,
3736
gasPrice: 5000000000,
3837
gasLimit: 1000000,
39-
nonce: BigNumber.ZERO,
38+
nonce: 0n,
4039
data: "0x",
4140
v: 1,
4241
r: "8f0145edea568df2dd39db91be0bff4ebf5b1e54cae49bf2090bf84fa0dd45a2",
@@ -79,10 +78,10 @@ describe<{
7978
senderPublicKey: wallet.publicKey,
8079
senderLegacyAddress: wallet.legacyAddress,
8180
to: undefined,
82-
value: BigNumber.ZERO,
81+
value: 0n,
8382
gasPrice: 5000000000,
8483
gasLimit: 1000000,
85-
nonce: BigNumber.ZERO,
84+
nonce: 0n,
8685
data: "0x",
8786
v: 1,
8887
r: "8f0145edea568df2dd39db91be0bff4ebf5b1e54cae49bf2090bf84fa0dd45a2",
@@ -154,10 +153,10 @@ describe<{
154153
senderPublicKey: wallet.publicKey,
155154
senderLegacyAddress: wallet.legacyAddress,
156155
to: wallet.address,
157-
value: BigNumber.ONE,
156+
value: 1n,
158157
gasPrice: 6000000000,
159158
gasLimit: 2_000_000,
160-
nonce: BigNumber.ONE,
159+
nonce: 1n,
161160
data: "0x001122",
162161
v: 1,
163162
r: "f1f325baf58dcfea7a5b3f9c4bb2e36368e9599a810ab2641089dc455fc6e6e3",

packages/crypto-transaction/source/builder.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { Contracts, Utils } from "@mainsail/contracts";
33
import { Identifiers } from "@mainsail/constants";
44
import { inject, injectable, postConstruct, tagged } from "@mainsail/container";
55
import { MissingTransactionSignatureError, ValidationFailed } from "@mainsail/exceptions";
6-
import { BigNumber } from "@mainsail/utils";
76

87
import { TransactionFactory } from "./factory.js";
98

@@ -45,14 +44,14 @@ export class TransactionBuilder {
4544
gasPrice: 5 * 1e9,
4645
hash: "",
4746
network: this.configuration.getNetwork().chainId,
48-
nonce: BigNumber.ZERO,
47+
nonce: 0n,
4948
r: "",
5049
s: "",
5150
senderLegacyAddress: "",
5251
senderPublicKey: "",
5352
to: undefined,
5453
v: 0,
55-
value: BigNumber.ZERO,
54+
value: 0n,
5655
};
5756
}
5857

@@ -63,7 +62,7 @@ export class TransactionBuilder {
6362

6463
public nonce(nonce: string): TransactionBuilder {
6564
if (nonce) {
66-
this.data.nonce = BigNumber.make(nonce);
65+
this.data.nonce = BigInt(nonce);
6766
}
6867

6968
return this;
@@ -85,7 +84,7 @@ export class TransactionBuilder {
8584
}
8685

8786
public value(value: string): TransactionBuilder {
88-
this.data.value = BigNumber.make(value);
87+
this.data.value = BigInt(value);
8988
return this;
9089
}
9190

packages/crypto-transaction/source/deserializer.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Contracts } from "@mainsail/contracts";
22

33
import { injectable } from "@mainsail/container";
4-
import { BigNumber } from "@mainsail/utils";
54
import { bytesToHex, getAddress, Hex, hexToBigInt } from "viem";
65

76
function decodeListBounds(buffer: Uint8Array): { start: number; end: number } {
@@ -153,14 +152,14 @@ export class Deserializer implements Contracts.Crypto.TransactionDeserializer {
153152
throw new Error("decoded RLP contains too few fields");
154153
}
155154

156-
const nonce = BigNumber.make(this.#parseNumber(fields[0]));
155+
const nonce = this.#parseBigInt(fields[0]);
157156
const gasPrice = this.#parseNumber(fields[1]);
158157
const gasLimit = this.#parseNumber(fields[2]);
159158

160159
const recipientAddressRaw = this.#parseAddress(fields[3]);
161160
const to = recipientAddressRaw ? getAddress(recipientAddressRaw) : undefined;
162161

163-
const value = this.#parseBigNumber(fields[4]);
162+
const value = this.#parseBigInt(fields[4]);
164163
const data = this.#parseData(fields[5]);
165164

166165
// Signature
@@ -208,8 +207,8 @@ export class Deserializer implements Contracts.Crypto.TransactionDeserializer {
208207
return value === "0x" ? 0 : Number(value);
209208
}
210209

211-
#parseBigNumber(value: Hex): BigNumber {
212-
return value === "0x" ? BigNumber.ZERO : BigNumber.make(hexToBigInt(value));
210+
#parseBigInt(value: Hex): bigint {
211+
return value === "0x" ? 0n : hexToBigInt(value);
213212
}
214213

215214
#parseAddress(value: Hex): string | undefined {

0 commit comments

Comments
 (0)