@nomicfoundation/edr 0.12.0-next.32 → 0.12.0-next.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/index.d.ts +45 -13
  2. package/package.json +8 -10
  3. package/dist/src/ts/solidity_tests.d.ts +0 -17
  4. package/dist/src/ts/solidity_tests.d.ts.map +0 -1
  5. package/dist/src/ts/solidity_tests.js +0 -32
  6. package/dist/src/ts/solidity_tests.js.map +0 -1
  7. package/src/account.rs +0 -124
  8. package/src/block.rs +0 -28
  9. package/src/call_override.rs +0 -116
  10. package/src/cast.rs +0 -165
  11. package/src/chains/generic.rs +0 -59
  12. package/src/chains/l1.rs +0 -275
  13. package/src/chains/op.rs +0 -451
  14. package/src/chains.rs +0 -7
  15. package/src/config.rs +0 -722
  16. package/src/context.rs +0 -447
  17. package/src/contract_decoder.rs +0 -60
  18. package/src/debug_trace.rs +0 -40
  19. package/src/gas_report.rs +0 -100
  20. package/src/instrument.rs +0 -113
  21. package/src/lib.rs +0 -50
  22. package/src/log.rs +0 -28
  23. package/src/logger.rs +0 -120
  24. package/src/mock/time.rs +0 -134
  25. package/src/mock.rs +0 -65
  26. package/src/precompile.rs +0 -50
  27. package/src/provider/factory.rs +0 -22
  28. package/src/provider/response.rs +0 -75
  29. package/src/provider.rs +0 -162
  30. package/src/result.rs +0 -216
  31. package/src/scenarios.rs +0 -53
  32. package/src/serde.rs +0 -57
  33. package/src/solidity_tests/artifact.rs +0 -184
  34. package/src/solidity_tests/cheatcode_errors.rs +0 -37
  35. package/src/solidity_tests/config.rs +0 -1053
  36. package/src/solidity_tests/factory.rs +0 -22
  37. package/src/solidity_tests/l1.rs +0 -72
  38. package/src/solidity_tests/op.rs +0 -72
  39. package/src/solidity_tests/runner.rs +0 -54
  40. package/src/solidity_tests/test_results.rs +0 -799
  41. package/src/solidity_tests.rs +0 -57
  42. package/src/subscription.rs +0 -32
  43. package/src/trace/debug.rs +0 -61
  44. package/src/trace/exit.rs +0 -89
  45. package/src/trace/library_utils.rs +0 -11
  46. package/src/trace/model.rs +0 -59
  47. package/src/trace/return_data.rs +0 -96
  48. package/src/trace/solidity_stack_trace.rs +0 -913
  49. package/src/trace.rs +0 -192
  50. package/src/ts/coverage.ts +0 -23
  51. package/src/ts/solidity_tests.ts +0 -46
  52. package/src/withdrawal.rs +0 -49
package/index.d.ts CHANGED
@@ -264,9 +264,29 @@ export interface IntervalRange {
264
264
  /** Configuration for the provider's miner. */
265
265
  export interface MiningConfig {
266
266
  autoMine: boolean
267
+ /**
268
+ * The block gas limit to use for mining a block.
269
+ *
270
+ * When not set, enforcement of the block gas limit is disabled in the mem
271
+ * pool, miner, and REVM.
272
+ */
273
+ blockGasLimit?: bigint
267
274
  interval?: bigint | IntervalRange
268
275
  memPool: MemPoolConfig
269
276
  }
277
+ /** Configuration for a locally mined blockchain. */
278
+ export interface LocalConfig {
279
+ /**
280
+ * The blob gas used for the genesis block, introduced in [EIP-4844].
281
+ *
282
+ * [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
283
+ */
284
+ genesisBlobGas?: BlobGas
285
+ /** The block gas limit of the genesis block. */
286
+ genesisBlockGasLimit: bigint
287
+ /** The date, in seconds since the Unix epoch, of the genesis block. */
288
+ genesisBlockTime?: bigint
289
+ }
270
290
  /** Configuration for runtime observability. */
271
291
  export interface ObservabilityConfig {
272
292
  /** If present, configures runtime observability to collect code coverage. */
@@ -302,17 +322,15 @@ export interface ProviderConfig {
302
322
  * will be used.
303
323
  */
304
324
  baseFeeConfig?: Array<BaseFeeParamActivation>
305
- /** The gas limit of each block */
306
- blockGasLimit: bigint
307
325
  /** The chain ID of the blockchain */
308
326
  chainId: bigint
309
327
  /** The address of the coinbase */
310
328
  coinbase: Uint8Array
311
329
  /**
312
- * The configuration for forking a blockchain. If not provided, a local
313
- * blockchain will be created
330
+ * The default transaction gas limit to use for RPC call and transaction
331
+ * requests that do not specify a `gas` value.
314
332
  */
315
- fork?: ForkConfig
333
+ defaultTransactionGasLimit: bigint
316
334
  /** The genesis state of the blockchain */
317
335
  genesisState: Array<AccountOverride>
318
336
  /** The hardfork of the blockchain */
@@ -322,10 +340,6 @@ export interface ProviderConfig {
322
340
  * transactions and later
323
341
  */
324
342
  initialBaseFeePerGas?: bigint
325
- /** The initial blob gas of the blockchain. Required for EIP-4844 */
326
- initialBlobGas?: BlobGas
327
- /** The initial date of the blockchain, in seconds since the Unix epoch */
328
- initialDate?: bigint
329
343
  /**
330
344
  * The initial parent beacon block root of the blockchain. Required for
331
345
  * EIP-4788
@@ -335,6 +349,8 @@ export interface ProviderConfig {
335
349
  minGasPrice: bigint
336
350
  /** The configuration for the miner */
337
351
  mining: MiningConfig
352
+ /** The network configuration for the provider. */
353
+ network: ForkConfig | LocalConfig
338
354
  /** The network ID of the blockchain */
339
355
  networkId: bigint
340
356
  /** The configuration for the provider's observability */
@@ -346,11 +362,17 @@ export interface ProviderConfig {
346
362
  /**
347
363
  * Transaction gas cap, introduced in [EIP-7825].
348
364
  *
349
- * When not set, will default to value defined by the used hardfork
365
+ * Integer values should be larger than zero.
366
+ *
367
+ * When `false`, enforcement of the transaction gas cap is disabled and
368
+ * transactions with any `gas` value are accepted by the mempool and
369
+ * executed without REVM's transaction gas cap check.
370
+ *
371
+ * When not set, a hardfork-specific default value will be used.
350
372
  *
351
373
  * [EIP-7825]: https://eips.ethereum.org/EIPS/eip-7825
352
374
  */
353
- transactionGasCap?: bigint
375
+ transactionGasCap?: bigint | false
354
376
  }
355
377
  /** Tracing config for Solidity stack trace generation. */
356
378
  export interface TracingConfigWithBuffers {
@@ -720,10 +742,20 @@ export interface SolidityTestRunnerConfigArgs {
720
742
  */
721
743
  disableBlockGasLimit?: boolean
722
744
  /**
723
- * Whether to enable the EIP-7825 (Osaka) transaction gas limit cap.
745
+ * Transaction gas cap, introduced in [EIP-7825].
746
+ *
747
+ * When not set, defaults to the value defined by the used hardfork.
748
+ *
749
+ * [EIP-7825]: https://eips.ethereum.org/EIPS/eip-7825
750
+ */
751
+ transactionGasCap?: bigint
752
+ /**
753
+ * Whether to disable the [EIP-7825] transaction gas cap.
724
754
  * Defaults to false.
755
+ *
756
+ * [EIP-7825]: https://eips.ethereum.org/EIPS/eip-7825
725
757
  */
726
- enableTxGasLimitCap?: boolean
758
+ disableTransactionGasCap?: boolean
727
759
  /**
728
760
  * The memory limit of the EVM in bytes.
729
761
  * Defaults to `33_554_432` (2^25 = 32MiB).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nomicfoundation/edr",
3
- "version": "0.12.0-next.32",
3
+ "version": "0.12.0-next.33",
4
4
  "devDependencies": {
5
5
  "@napi-rs/cli": "^2.18.4",
6
6
  "@nomicfoundation/ethereumjs-util": "^9.0.4",
@@ -31,13 +31,11 @@
31
31
  },
32
32
  "exports": {
33
33
  ".": "./index.js",
34
- "./solidity-tests": "./dist/src/ts/solidity_tests.js",
35
34
  "./coverage": "./dist/src/ts/coverage.js"
36
35
  },
37
36
  "files": [
38
37
  "index.js",
39
38
  "index.d.ts",
40
- "src/",
41
39
  "dist/src/",
42
40
  "coverage.sol"
43
41
  ],
@@ -61,13 +59,13 @@
61
59
  "repository": "NomicFoundation/edr.git",
62
60
  "types": "index.d.ts",
63
61
  "dependencies": {
64
- "@nomicfoundation/edr-darwin-arm64": "0.12.0-next.32",
65
- "@nomicfoundation/edr-darwin-x64": "0.12.0-next.32",
66
- "@nomicfoundation/edr-linux-arm64-gnu": "0.12.0-next.32",
67
- "@nomicfoundation/edr-linux-arm64-musl": "0.12.0-next.32",
68
- "@nomicfoundation/edr-linux-x64-gnu": "0.12.0-next.32",
69
- "@nomicfoundation/edr-linux-x64-musl": "0.12.0-next.32",
70
- "@nomicfoundation/edr-win32-x64-msvc": "0.12.0-next.32"
62
+ "@nomicfoundation/edr-darwin-arm64": "0.12.0-next.33",
63
+ "@nomicfoundation/edr-darwin-x64": "0.12.0-next.33",
64
+ "@nomicfoundation/edr-linux-arm64-gnu": "0.12.0-next.33",
65
+ "@nomicfoundation/edr-linux-arm64-musl": "0.12.0-next.33",
66
+ "@nomicfoundation/edr-linux-x64-gnu": "0.12.0-next.33",
67
+ "@nomicfoundation/edr-linux-x64-musl": "0.12.0-next.33",
68
+ "@nomicfoundation/edr-win32-x64-msvc": "0.12.0-next.33"
71
69
  },
72
70
  "scripts": {
73
71
  "artifacts": "napi artifacts",
@@ -1,17 +0,0 @@
1
- import { StandardTestKind, FuzzTestKind, InvariantTestKind } from "../../index";
2
- export declare enum SortOrder {
3
- Ascending = 0,
4
- Descending = 1
5
- }
6
- export interface GasUsageFilter {
7
- minThreshold?: bigint;
8
- maxThreshold?: bigint;
9
- }
10
- export declare function extractGasUsage(testResults: {
11
- name: string;
12
- kind: StandardTestKind | FuzzTestKind | InvariantTestKind;
13
- }[], filter?: GasUsageFilter, ordering?: SortOrder): {
14
- name: string;
15
- gas: bigint;
16
- }[];
17
- //# sourceMappingURL=solidity_tests.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"solidity_tests.d.ts","sourceRoot":"","sources":["../../../src/ts/solidity_tests.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhF,oBAAY,SAAS;IACnB,SAAS,IAAA;IACT,UAAU,IAAA;CACX;AAED,MAAM,WAAW,cAAc;IAC7B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,wBAAgB,eAAe,CAC7B,WAAW,EAAE;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,gBAAgB,GAAG,YAAY,GAAG,iBAAiB,CAAC;CAC3D,EAAE,EACH,MAAM,CAAC,EAAE,cAAc,EACvB,QAAQ,CAAC,EAAE,SAAS,GACnB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,EAAE,CA0BjC"}
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SortOrder = void 0;
4
- exports.extractGasUsage = extractGasUsage;
5
- var SortOrder;
6
- (function (SortOrder) {
7
- SortOrder[SortOrder["Ascending"] = 0] = "Ascending";
8
- SortOrder[SortOrder["Descending"] = 1] = "Descending";
9
- })(SortOrder || (exports.SortOrder = SortOrder = {}));
10
- function extractGasUsage(testResults, filter, ordering) {
11
- const gasUsage = [];
12
- for (const result of testResults) {
13
- // Default to zero gas for invariant tests
14
- const gas = "consumedGas" in result.kind
15
- ? result.kind.consumedGas
16
- : "medianGas" in result.kind
17
- ? result.kind.medianGas
18
- : BigInt(0);
19
- if ((!filter?.minThreshold || gas >= filter.minThreshold) &&
20
- (!filter?.maxThreshold || gas <= filter.maxThreshold)) {
21
- gasUsage.push({ name: result.name, gas });
22
- }
23
- }
24
- if (ordering === SortOrder.Ascending) {
25
- gasUsage.sort((a, b) => (a.gas < b.gas ? -1 : a.gas > b.gas ? 1 : 0));
26
- }
27
- else if (ordering === SortOrder.Descending) {
28
- gasUsage.sort((a, b) => (a.gas > b.gas ? -1 : a.gas < b.gas ? 1 : 0));
29
- }
30
- return gasUsage;
31
- }
32
- //# sourceMappingURL=solidity_tests.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"solidity_tests.js","sourceRoot":"","sources":["../../../src/ts/solidity_tests.ts"],"names":[],"mappings":";;;AAYA,0CAiCC;AA3CD,IAAY,SAGX;AAHD,WAAY,SAAS;IACnB,mDAAS,CAAA;IACT,qDAAU,CAAA;AACZ,CAAC,EAHW,SAAS,yBAAT,SAAS,QAGpB;AAOD,SAAgB,eAAe,CAC7B,WAGG,EACH,MAAuB,EACvB,QAAoB;IAEpB,MAAM,QAAQ,GAAoC,EAAE,CAAC;IAErD,KAAK,MAAM,MAAM,IAAI,WAAW,EAAE,CAAC;QACjC,0CAA0C;QAC1C,MAAM,GAAG,GAAG,aAAa,IAAI,MAAM,CAAC,IAAI;YACtC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW;YACzB,CAAC,CAAC,WAAW,IAAI,MAAM,CAAC,IAAI;gBAC1B,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS;gBACvB,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QAEhB,IACE,CAAC,CAAC,MAAM,EAAE,YAAY,IAAI,GAAG,IAAI,MAAM,CAAC,YAAY,CAAC;YACrD,CAAC,CAAC,MAAM,EAAE,YAAY,IAAI,GAAG,IAAI,MAAM,CAAC,YAAY,CAAC,EACrD,CAAC;YACD,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC;QAC5C,CAAC;IACH,CAAC;IAED,IAAI,QAAQ,KAAK,SAAS,CAAC,SAAS,EAAE,CAAC;QACrC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACxE,CAAC;SAAM,IAAI,QAAQ,KAAK,SAAS,CAAC,UAAU,EAAE,CAAC;QAC7C,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACxE,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC"}
package/src/account.rs DELETED
@@ -1,124 +0,0 @@
1
- use derive_more::Debug;
2
- use edr_primitives::{hex, Address, HashMap, U256};
3
- use edr_solidity_tests::{backend::Predeploy, revm::state::AccountInfo};
4
- use edr_state_api::EvmStorageSlot;
5
- use napi::bindgen_prelude::{BigInt, Uint8Array};
6
- use napi_derive::napi;
7
-
8
- use crate::{
9
- cast::TryCast,
10
- serde::{
11
- serialize_bigint_as_struct, serialize_optional_bigint_as_struct,
12
- serialize_optional_uint8array_as_hex, serialize_uint8array_as_hex,
13
- },
14
- };
15
-
16
- /// Specification of overrides for an account and its storage.
17
- #[napi(object)]
18
- #[derive(Clone, Debug, serde::Serialize)]
19
- pub struct AccountOverride {
20
- /// The account's address
21
- #[debug("{}", hex::encode(address))]
22
- #[serde(serialize_with = "serialize_uint8array_as_hex")]
23
- pub address: Uint8Array,
24
- /// If present, the overwriting balance.
25
- #[serde(serialize_with = "serialize_optional_bigint_as_struct")]
26
- pub balance: Option<BigInt>,
27
- /// If present, the overwriting nonce.
28
- #[serde(serialize_with = "serialize_optional_bigint_as_struct")]
29
- pub nonce: Option<BigInt>,
30
- /// If present, the overwriting code.
31
- #[debug("{:?}", code.as_ref().map(hex::encode))]
32
- #[serde(serialize_with = "serialize_optional_uint8array_as_hex")]
33
- pub code: Option<Uint8Array>,
34
- /// BEWARE: This field is not supported yet. See <https://github.com/NomicFoundation/edr/issues/911>
35
- ///
36
- /// If present, the overwriting storage.
37
- pub storage: Option<Vec<StorageSlot>>,
38
- }
39
-
40
- impl TryFrom<AccountOverride> for (Address, edr_provider::AccountOverride) {
41
- type Error = napi::Error;
42
-
43
- fn try_from(value: AccountOverride) -> Result<Self, Self::Error> {
44
- let AccountOverride {
45
- address,
46
- balance,
47
- nonce,
48
- code,
49
- storage,
50
- } = value;
51
- let storage = storage
52
- .map(|storage| {
53
- storage
54
- .into_iter()
55
- .map(|StorageSlot { index, value }| {
56
- let value = value.try_cast()?;
57
- let slot = EvmStorageSlot::new(value, 0);
58
-
59
- let index: U256 = index.try_cast()?;
60
- Ok((index, slot))
61
- })
62
- .collect::<napi::Result<_>>()
63
- })
64
- .transpose()?;
65
-
66
- let account_override = edr_provider::AccountOverride {
67
- balance: balance.map(TryCast::try_cast).transpose()?,
68
- nonce: nonce.map(TryCast::try_cast).transpose()?,
69
- code: code.map(TryCast::try_cast).transpose()?,
70
- storage,
71
- };
72
-
73
- let address: Address = address.try_cast()?;
74
-
75
- Ok((address, account_override))
76
- }
77
- }
78
-
79
- impl TryFrom<AccountOverride> for Predeploy {
80
- type Error = napi::Error;
81
-
82
- fn try_from(value: AccountOverride) -> Result<Self, Self::Error> {
83
- let (address, account_override) = value.try_into()?;
84
-
85
- let storage = account_override.storage.unwrap_or_else(HashMap::default);
86
- let balance = account_override.balance.unwrap_or(U256::ZERO);
87
- let nonce = account_override.nonce.unwrap_or(0);
88
- let code = account_override.code.ok_or_else(|| {
89
- napi::Error::from_reason(format!("Predeploy with address '{address}' must have code"))
90
- })?;
91
-
92
- if code.is_empty() {
93
- return Err(napi::Error::from_reason(
94
- "Predeploy with address '{address}' must have non-empty code",
95
- ));
96
- }
97
- let code_hash = code.hash_slow();
98
-
99
- let account_info = AccountInfo {
100
- balance,
101
- nonce,
102
- code_hash,
103
- code: Some(code),
104
- };
105
-
106
- Ok(Self {
107
- address,
108
- account_info,
109
- storage,
110
- })
111
- }
112
- }
113
-
114
- /// A description of a storage slot's state.
115
- #[napi(object)]
116
- #[derive(Clone, Debug, serde::Serialize)]
117
- pub struct StorageSlot {
118
- /// The storage slot's index
119
- #[serde(serialize_with = "serialize_bigint_as_struct")]
120
- pub index: BigInt,
121
- /// The storage slot's value
122
- #[serde(serialize_with = "serialize_bigint_as_struct")]
123
- pub value: BigInt,
124
- }
package/src/block.rs DELETED
@@ -1,28 +0,0 @@
1
- use napi::bindgen_prelude::BigInt;
2
- use napi_derive::napi;
3
-
4
- use crate::cast::TryCast;
5
-
6
- /// Information about the blob gas used in a block.
7
- #[napi(object)]
8
- pub struct BlobGas {
9
- /// The total amount of blob gas consumed by the transactions within the
10
- /// block.
11
- pub gas_used: BigInt,
12
- /// The running total of blob gas consumed in excess of the target, prior to
13
- /// the block. Blocks with above-target blob gas consumption increase this
14
- /// value, blocks with below-target blob gas consumption decrease it
15
- /// (bounded at 0).
16
- pub excess_gas: BigInt,
17
- }
18
-
19
- impl TryFrom<BlobGas> for edr_block_header::BlobGas {
20
- type Error = napi::Error;
21
-
22
- fn try_from(value: BlobGas) -> Result<Self, Self::Error> {
23
- Ok(Self {
24
- gas_used: BigInt::try_cast(value.gas_used)?,
25
- excess_gas: BigInt::try_cast(value.excess_gas)?,
26
- })
27
- }
28
- }
@@ -1,116 +0,0 @@
1
- use std::sync::mpsc::channel;
2
-
3
- use edr_primitives::{Address, Bytes};
4
- use napi::{
5
- bindgen_prelude::{Promise, Uint8Array},
6
- threadsafe_function::{
7
- ErrorStrategy, ThreadSafeCallContext, ThreadsafeFunction, ThreadsafeFunctionCallMode,
8
- },
9
- tokio::runtime,
10
- Env, JsFunction, Status,
11
- };
12
- use napi_derive::napi;
13
-
14
- use crate::cast::TryCast;
15
-
16
- /// The result of executing a call override.
17
- #[napi(object)]
18
- pub struct CallOverrideResult {
19
- pub result: Uint8Array,
20
- pub should_revert: bool,
21
- }
22
-
23
- impl TryCast<Option<edr_provider::CallOverrideResult>> for Option<CallOverrideResult> {
24
- type Error = napi::Error;
25
-
26
- fn try_cast(self) -> Result<Option<edr_provider::CallOverrideResult>, Self::Error> {
27
- match self {
28
- None => Ok(None),
29
- Some(result) => Ok(Some(edr_provider::CallOverrideResult {
30
- output: Bytes::copy_from_slice(&result.result),
31
- should_revert: result.should_revert,
32
- })),
33
- }
34
- }
35
- }
36
-
37
- struct CallOverrideCall {
38
- contract_address: Address,
39
- data: Bytes,
40
- }
41
-
42
- #[derive(Clone)]
43
- pub struct CallOverrideCallback {
44
- call_override_callback_fn: ThreadsafeFunction<CallOverrideCall, ErrorStrategy::Fatal>,
45
- runtime: runtime::Handle,
46
- }
47
-
48
- impl CallOverrideCallback {
49
- pub fn new(
50
- env: &Env,
51
- call_override_callback: JsFunction,
52
- runtime: runtime::Handle,
53
- ) -> napi::Result<Self> {
54
- let mut call_override_callback_fn = call_override_callback.create_threadsafe_function(
55
- 0,
56
- |ctx: ThreadSafeCallContext<CallOverrideCall>| {
57
- let address = ctx
58
- .env
59
- .create_arraybuffer_with_data(ctx.value.contract_address.to_vec())?
60
- .into_raw();
61
-
62
- let data = ctx
63
- .env
64
- .create_arraybuffer_with_data(ctx.value.data.to_vec())?
65
- .into_raw();
66
-
67
- Ok(vec![address, data])
68
- },
69
- )?;
70
-
71
- // Maintain a weak reference to the function to avoid blocking the event loop
72
- // from exiting.
73
- call_override_callback_fn.unref(env)?;
74
-
75
- Ok(Self {
76
- call_override_callback_fn,
77
- runtime,
78
- })
79
- }
80
-
81
- pub fn call_override(
82
- &self,
83
- contract_address: Address,
84
- data: Bytes,
85
- ) -> Option<edr_provider::CallOverrideResult> {
86
- let (sender, receiver) = channel();
87
-
88
- let runtime = self.runtime.clone();
89
- let status = self.call_override_callback_fn.call_with_return_value(
90
- CallOverrideCall {
91
- contract_address,
92
- data,
93
- },
94
- ThreadsafeFunctionCallMode::Blocking,
95
- move |result: Promise<Option<CallOverrideResult>>| {
96
- runtime.spawn(async move {
97
- let result = result.await?.try_cast();
98
- sender.send(result).map_err(|_error| {
99
- napi::Error::new(
100
- Status::GenericFailure,
101
- "Failed to send result from call_override_callback",
102
- )
103
- })
104
- });
105
- Ok(())
106
- },
107
- );
108
-
109
- assert_eq!(status, Status::Ok, "Call override callback failed");
110
-
111
- receiver
112
- .recv()
113
- .unwrap()
114
- .expect("Failed call to call_override_callback")
115
- }
116
- }
package/src/cast.rs DELETED
@@ -1,165 +0,0 @@
1
- use edr_primitives::{Address, Bytecode, Bytes, B256, B64, U256};
2
- use napi::{
3
- bindgen_prelude::{BigInt, Uint8Array},
4
- Status,
5
- };
6
-
7
- /// An attempted conversion that consumes `self`, which may or may not be
8
- /// expensive. It is identical to [`TryInto`], but it allows us to implement
9
- /// the trait for external types.
10
- pub trait TryCast<T>: Sized {
11
- /// The type returned in the event of a conversion error.
12
- type Error;
13
-
14
- /// Performs the conversion.
15
- fn try_cast(self) -> Result<T, Self::Error>;
16
- }
17
-
18
- impl TryCast<Address> for Uint8Array {
19
- type Error = napi::Error;
20
-
21
- fn try_cast(self) -> std::result::Result<Address, Self::Error> {
22
- if self.len() != 20 {
23
- return Err(napi::Error::new(
24
- Status::InvalidArg,
25
- "Uint8Array was expected to be 20 bytes.".to_string(),
26
- ));
27
- }
28
- Ok(Address::from_slice(&self))
29
- }
30
- }
31
-
32
- impl TryCast<B64> for Uint8Array {
33
- type Error = napi::Error;
34
-
35
- fn try_cast(self) -> std::result::Result<B64, Self::Error> {
36
- if self.len() != 8 {
37
- return Err(napi::Error::new(
38
- Status::InvalidArg,
39
- "Uint8Array was expected to be 8 bytes.".to_string(),
40
- ));
41
- }
42
- Ok(B64::from_slice(&self))
43
- }
44
- }
45
-
46
- impl TryCast<B256> for Uint8Array {
47
- type Error = napi::Error;
48
-
49
- fn try_cast(self) -> std::result::Result<B256, Self::Error> {
50
- if self.len() != 32 {
51
- return Err(napi::Error::new(
52
- Status::InvalidArg,
53
- "Uint8Array was expected to be 32 bytes.".to_string(),
54
- ));
55
- }
56
- Ok(B256::from_slice(&self))
57
- }
58
- }
59
-
60
- impl TryCast<Bytecode> for Uint8Array {
61
- type Error = napi::Error;
62
-
63
- fn try_cast(self) -> std::result::Result<Bytecode, Self::Error> {
64
- let bytes = Bytes::copy_from_slice(&self);
65
- Bytecode::new_raw_checked(bytes).map_err(|error| {
66
- napi::Error::new(
67
- Status::InvalidArg,
68
- format!("Uint8Array was not valid bytecode: {error}"),
69
- )
70
- })
71
- }
72
- }
73
-
74
- impl TryCast<u64> for BigInt {
75
- type Error = napi::Error;
76
-
77
- fn try_cast(self) -> std::result::Result<u64, Self::Error> {
78
- let (signed, value, lossless) = self.get_u64();
79
-
80
- if signed {
81
- return Err(napi::Error::new(
82
- Status::InvalidArg,
83
- "BigInt was expected to be unsigned.".to_string(),
84
- ));
85
- }
86
-
87
- if !lossless {
88
- return Err(napi::Error::new(
89
- Status::InvalidArg,
90
- "BigInt was expected to fit within 64 bits.".to_string(),
91
- ));
92
- }
93
-
94
- Ok(value)
95
- }
96
- }
97
-
98
- impl TryCast<u128> for BigInt {
99
- type Error = napi::Error;
100
-
101
- fn try_cast(self) -> std::result::Result<u128, Self::Error> {
102
- let (signed, value, lossless) = self.get_u128();
103
-
104
- if signed {
105
- return Err(napi::Error::new(
106
- Status::InvalidArg,
107
- "BigInt was expected to be unsigned.".to_string(),
108
- ));
109
- }
110
-
111
- if !lossless {
112
- return Err(napi::Error::new(
113
- Status::InvalidArg,
114
- "BigInt was expected to fit within 128 bits.".to_string(),
115
- ));
116
- }
117
-
118
- Ok(value)
119
- }
120
- }
121
-
122
- impl TryCast<usize> for BigInt {
123
- type Error = napi::Error;
124
-
125
- fn try_cast(self) -> std::result::Result<usize, Self::Error> {
126
- let size: u64 = BigInt::try_cast(self)?;
127
- usize::try_from(size).map_err(|e| napi::Error::new(Status::InvalidArg, e.to_string()))
128
- }
129
- }
130
-
131
- impl TryCast<U256> for BigInt {
132
- type Error = napi::Error;
133
-
134
- fn try_cast(mut self) -> std::result::Result<U256, Self::Error> {
135
- let num_words = self.words.len();
136
- match num_words.cmp(&4) {
137
- std::cmp::Ordering::Less => self.words.append(&mut vec![0u64; 4 - num_words]),
138
- std::cmp::Ordering::Equal => (),
139
- std::cmp::Ordering::Greater => {
140
- return Err(napi::Error::new(
141
- Status::InvalidArg,
142
- "BigInt cannot have more than 4 words.".to_owned(),
143
- ));
144
- }
145
- }
146
-
147
- Ok(U256::from_limbs(self.words.try_into().unwrap()))
148
- }
149
- }
150
-
151
- impl<T> TryCast<T> for T {
152
- type Error = napi::Error;
153
-
154
- fn try_cast(self) -> std::result::Result<T, Self::Error> {
155
- Ok(self)
156
- }
157
- }
158
-
159
- impl TryCast<Option<Bytes>> for Option<Uint8Array> {
160
- type Error = napi::Error;
161
-
162
- fn try_cast(self) -> Result<Option<Bytes>, Self::Error> {
163
- Ok(self.map(|buffer| Bytes::copy_from_slice(&buffer)))
164
- }
165
- }