@nomicfoundation/edr 0.3.7 → 0.4.0-alpha.0

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.
package/LICENSE CHANGED
@@ -1 +1,21 @@
1
- SEE LICENSE IN EACH PACKAGE'S LICENSE FILE
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Nomic Foundation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/index.d.ts CHANGED
@@ -329,6 +329,8 @@ export interface HaltResult {
329
329
  export interface ExecutionResult {
330
330
  /** The transaction result */
331
331
  result: SuccessResult | RevertResult | HaltResult
332
+ /** Optional contract address if the transaction created a new contract. */
333
+ contractAddress?: Buffer
332
334
  }
333
335
  export interface SubscriptionEvent {
334
336
  filterId: bigint
@@ -339,6 +341,8 @@ export interface TracingMessage {
339
341
  readonly caller: Buffer
340
342
  /** Recipient address. None if it is a Create message. */
341
343
  readonly to?: Buffer
344
+ /** Whether it's a static call */
345
+ readonly isStaticCall: boolean
342
346
  /** Transaction gas limit */
343
347
  readonly gasLimit: bigint
344
348
  /** Depth of the message */
@@ -362,8 +366,14 @@ export interface TracingStep {
362
366
  readonly pc: bigint
363
367
  /** The executed op code */
364
368
  readonly opcode: string
365
- /** The top entry on the stack. None if the stack is empty. */
366
- readonly stackTop?: bigint
369
+ /**
370
+ * The entries on the stack. It only contains the top element unless
371
+ * verbose tracing is enabled. The vector is empty if there are no elements
372
+ * on the stack.
373
+ */
374
+ readonly stack: Array<bigint>
375
+ /** The memory at the step. None if verbose tracing is disabled. */
376
+ readonly memory?: Buffer
367
377
  }
368
378
  export interface TracingMessageResult {
369
379
  /** Execution result */
@@ -390,6 +400,13 @@ export class Provider {
390
400
  /**Handles a JSON-RPC request and returns a JSON-RPC response. */
391
401
  handleRequest(jsonRequest: string): Promise<Response>
392
402
  setCallOverrideCallback(callOverrideCallback: (contract_address: Buffer, data: Buffer) => Promise<CallOverrideResult | undefined>): void
403
+ /**
404
+ * Set to `true` to make the traces returned with `eth_call`,
405
+ * `eth_estimateGas`, `eth_sendRawTransaction`, `eth_sendTransaction`,
406
+ * `evm_mine`, `hardhat_mine` include the full stack and memory. Set to
407
+ * `false` to disable this.
408
+ */
409
+ setVerboseTracing(verboseTracing: boolean): void
393
410
  }
394
411
  export class Response {
395
412
  get json(): string
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nomicfoundation/edr",
3
- "version": "0.3.7",
3
+ "version": "0.4.0-alpha.0",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "files": [
@@ -42,13 +42,13 @@
42
42
  "node": ">= 18"
43
43
  },
44
44
  "optionalDependencies": {
45
- "@nomicfoundation/edr-win32-x64-msvc": "0.3.7",
46
- "@nomicfoundation/edr-darwin-x64": "0.3.7",
47
- "@nomicfoundation/edr-linux-x64-gnu": "0.3.7",
48
- "@nomicfoundation/edr-darwin-arm64": "0.3.7",
49
- "@nomicfoundation/edr-linux-arm64-gnu": "0.3.7",
50
- "@nomicfoundation/edr-linux-arm64-musl": "0.3.7",
51
- "@nomicfoundation/edr-linux-x64-musl": "0.3.7"
45
+ "@nomicfoundation/edr-win32-x64-msvc": "0.4.0-alpha.0",
46
+ "@nomicfoundation/edr-darwin-x64": "0.4.0-alpha.0",
47
+ "@nomicfoundation/edr-linux-x64-gnu": "0.4.0-alpha.0",
48
+ "@nomicfoundation/edr-darwin-arm64": "0.4.0-alpha.0",
49
+ "@nomicfoundation/edr-linux-arm64-gnu": "0.4.0-alpha.0",
50
+ "@nomicfoundation/edr-linux-arm64-musl": "0.4.0-alpha.0",
51
+ "@nomicfoundation/edr-linux-x64-musl": "0.4.0-alpha.0"
52
52
  },
53
53
  "scripts": {
54
54
  "artifacts": "napi artifacts",
@@ -2,10 +2,11 @@ use std::sync::mpsc::channel;
2
2
 
3
3
  use edr_eth::{Address, Bytes};
4
4
  use napi::{
5
- bindgen_prelude::Buffer,
5
+ bindgen_prelude::{Buffer, Promise},
6
6
  threadsafe_function::{
7
7
  ErrorStrategy, ThreadSafeCallContext, ThreadsafeFunction, ThreadsafeFunctionCallMode,
8
8
  },
9
+ tokio::runtime,
9
10
  Env, JsFunction, Status,
10
11
  };
11
12
  use napi_derive::napi;
@@ -41,10 +42,15 @@ struct CallOverrideCall {
41
42
  #[derive(Clone)]
42
43
  pub struct CallOverrideCallback {
43
44
  call_override_callback_fn: ThreadsafeFunction<CallOverrideCall, ErrorStrategy::Fatal>,
45
+ runtime: runtime::Handle,
44
46
  }
45
47
 
46
48
  impl CallOverrideCallback {
47
- pub fn new(env: &Env, call_override_callback: JsFunction) -> napi::Result<Self> {
49
+ pub fn new(
50
+ env: &Env,
51
+ call_override_callback: JsFunction,
52
+ runtime: runtime::Handle,
53
+ ) -> napi::Result<Self> {
48
54
  let mut call_override_callback_fn = call_override_callback.create_threadsafe_function(
49
55
  0,
50
56
  |ctx: ThreadSafeCallContext<CallOverrideCall>| {
@@ -68,6 +74,7 @@ impl CallOverrideCallback {
68
74
 
69
75
  Ok(Self {
70
76
  call_override_callback_fn,
77
+ runtime,
71
78
  })
72
79
  }
73
80
 
@@ -78,21 +85,24 @@ impl CallOverrideCallback {
78
85
  ) -> Option<edr_provider::CallOverrideResult> {
79
86
  let (sender, receiver) = channel();
80
87
 
88
+ let runtime = self.runtime.clone();
81
89
  let status = self.call_override_callback_fn.call_with_return_value(
82
90
  CallOverrideCall {
83
91
  contract_address,
84
92
  data,
85
93
  },
86
94
  ThreadsafeFunctionCallMode::Blocking,
87
- move |result: Option<CallOverrideResult>| {
88
- let result = result.try_cast();
89
-
90
- sender.send(result).map_err(|_error| {
91
- napi::Error::new(
92
- Status::GenericFailure,
93
- "Failed to send result from call_override_callback",
94
- )
95
- })
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(())
96
106
  },
97
107
  );
98
108
 
package/src/logger.rs CHANGED
@@ -1,11 +1,11 @@
1
1
  use std::{fmt::Display, sync::mpsc::channel};
2
2
 
3
3
  use ansi_term::{Color, Style};
4
- use edr_eth::{Bytes, B256, U256};
4
+ use edr_eth::{transaction::Transaction, Bytes, B256, U256};
5
5
  use edr_evm::{
6
6
  blockchain::BlockchainError,
7
7
  precompile::{self, Precompiles},
8
- trace::TraceMessage,
8
+ trace::{AfterMessage, TraceMessage},
9
9
  ExecutableTransaction, ExecutionResult, SyncBlock,
10
10
  };
11
11
  use edr_provider::{ProviderError, TransactionFailure};
@@ -506,7 +506,7 @@ impl LogCollector {
506
506
  result.transaction_traces.iter()
507
507
  )
508
508
  .find(|(block_transaction, _, _)| {
509
- *block_transaction.hash() == *transaction.hash()
509
+ *block_transaction.transaction_hash() == *transaction.transaction_hash()
510
510
  })
511
511
  .map(|(_, transaction_result, trace)| (result, transaction_result, trace))
512
512
  })
@@ -514,7 +514,11 @@ impl LogCollector {
514
514
 
515
515
  if mining_results.len() > 1 {
516
516
  self.log_multiple_blocks_warning();
517
- self.log_auto_mined_block_results(spec_id, mining_results, transaction.hash());
517
+ self.log_auto_mined_block_results(
518
+ spec_id,
519
+ mining_results,
520
+ transaction.transaction_hash(),
521
+ );
518
522
  self.log_currently_sent_transaction(
519
523
  spec_id,
520
524
  sent_block_result,
@@ -526,7 +530,11 @@ impl LogCollector {
526
530
  let transactions = result.block.transactions();
527
531
  if transactions.len() > 1 {
528
532
  self.log_multiple_transactions_warning();
529
- self.log_auto_mined_block_results(spec_id, mining_results, transaction.hash());
533
+ self.log_auto_mined_block_results(
534
+ spec_id,
535
+ mining_results,
536
+ transaction.transaction_hash(),
537
+ );
530
538
  self.log_currently_sent_transaction(
531
539
  spec_id,
532
540
  sent_block_result,
@@ -659,7 +667,7 @@ impl LogCollector {
659
667
  transaction_traces
660
668
  ) {
661
669
  let should_highlight_hash =
662
- *transaction.hash() == *transaction_hash_to_highlight;
670
+ *transaction.transaction_hash() == *transaction_hash_to_highlight;
663
671
  logger.log_block_transaction(
664
672
  spec_id,
665
673
  transaction,
@@ -706,7 +714,7 @@ impl LogCollector {
706
714
  console_log_inputs: &[Bytes],
707
715
  should_highlight_hash: bool,
708
716
  ) {
709
- let transaction_hash = transaction.hash();
717
+ let transaction_hash = transaction.transaction_hash();
710
718
  if should_highlight_hash {
711
719
  self.log_with_title(
712
720
  "Transaction",
@@ -841,8 +849,12 @@ impl LogCollector {
841
849
  }
842
850
  }
843
851
  } else {
844
- let result = if let Some(TraceMessage::After(result)) = trace.messages.last() {
845
- result
852
+ let result = if let Some(TraceMessage::After(AfterMessage {
853
+ execution_result,
854
+ ..
855
+ })) = trace.messages.last()
856
+ {
857
+ execution_result
846
858
  } else {
847
859
  unreachable!("Before messages must have an after message")
848
860
  };
@@ -1090,7 +1102,7 @@ impl LogCollector {
1090
1102
  self.indented(|logger| {
1091
1103
  logger.log_contract_and_function_name::<false>(spec_id, trace);
1092
1104
 
1093
- let transaction_hash = transaction.hash();
1105
+ let transaction_hash = transaction.transaction_hash();
1094
1106
  logger.log_with_title("Transaction", transaction_hash);
1095
1107
 
1096
1108
  logger.log_with_title("From", format!("0x{:x}", transaction.caller()));
package/src/provider.rs CHANGED
@@ -20,6 +20,7 @@ use crate::{
20
20
  #[napi]
21
21
  pub struct Provider {
22
22
  provider: Arc<edr_provider::Provider<LoggerError>>,
23
+ runtime: runtime::Handle,
23
24
  #[cfg(feature = "scenarios")]
24
25
  scenario_file: Option<napi::tokio::sync::Mutex<napi::tokio::fs::File>>,
25
26
  }
@@ -53,7 +54,7 @@ impl Provider {
53
54
  ))?;
54
55
 
55
56
  let result = edr_provider::Provider::new(
56
- runtime,
57
+ runtime.clone(),
57
58
  logger,
58
59
  subscriber_callback,
59
60
  config,
@@ -64,6 +65,7 @@ impl Provider {
64
65
  |provider| {
65
66
  Ok(Provider {
66
67
  provider: Arc::new(provider),
68
+ runtime,
67
69
  #[cfg(feature = "scenarios")]
68
70
  scenario_file,
69
71
  })
@@ -185,7 +187,8 @@ impl Provider {
185
187
  ) -> napi::Result<()> {
186
188
  let provider = self.provider.clone();
187
189
 
188
- let call_override_callback = CallOverrideCallback::new(&env, call_override_callback)?;
190
+ let call_override_callback =
191
+ CallOverrideCallback::new(&env, call_override_callback, self.runtime.clone())?;
189
192
  let call_override_callback =
190
193
  Arc::new(move |address, data| call_override_callback.call_override(address, data));
191
194
 
@@ -193,6 +196,15 @@ impl Provider {
193
196
 
194
197
  Ok(())
195
198
  }
199
+
200
+ /// Set to `true` to make the traces returned with `eth_call`,
201
+ /// `eth_estimateGas`, `eth_sendRawTransaction`, `eth_sendTransaction`,
202
+ /// `evm_mine`, `hardhat_mine` include the full stack and memory. Set to
203
+ /// `false` to disable this.
204
+ #[napi(ts_return_type = "void")]
205
+ pub fn set_verbose_tracing(&self, verbose_tracing: bool) {
206
+ self.provider.set_verbose_tracing(verbose_tracing);
207
+ }
196
208
  }
197
209
 
198
210
  #[napi]
package/src/result.rs CHANGED
@@ -1,3 +1,4 @@
1
+ use edr_evm::trace::AfterMessage;
1
2
  use napi::{
2
3
  bindgen_prelude::{BigInt, Buffer, Either3},
3
4
  Either, Env, JsBuffer, JsBufferValue,
@@ -168,11 +169,18 @@ pub struct HaltResult {
168
169
  pub struct ExecutionResult {
169
170
  /// The transaction result
170
171
  pub result: Either3<SuccessResult, RevertResult, HaltResult>,
172
+ /// Optional contract address if the transaction created a new contract.
173
+ pub contract_address: Option<Buffer>,
171
174
  }
172
175
 
173
176
  impl ExecutionResult {
174
- pub fn new(env: &Env, result: &edr_evm::ExecutionResult) -> napi::Result<Self> {
175
- let result = match result {
177
+ pub fn new(env: &Env, message: &AfterMessage) -> napi::Result<Self> {
178
+ let AfterMessage {
179
+ execution_result,
180
+ contract_address,
181
+ } = message;
182
+
183
+ let result = match execution_result {
176
184
  edr_evm::ExecutionResult::Success {
177
185
  reason,
178
186
  gas_used,
@@ -227,6 +235,11 @@ impl ExecutionResult {
227
235
  }),
228
236
  };
229
237
 
230
- Ok(Self { result })
238
+ let contract_address = contract_address.map(|address| Buffer::from(address.as_slice()));
239
+
240
+ Ok(Self {
241
+ result,
242
+ contract_address,
243
+ })
231
244
  }
232
245
  }
package/src/trace.rs CHANGED
@@ -1,6 +1,6 @@
1
1
  use std::sync::Arc;
2
2
 
3
- use edr_evm::{interpreter::OPCODE_JUMPMAP, trace::BeforeMessage};
3
+ use edr_evm::{interpreter::OpCode, trace::BeforeMessage};
4
4
  use napi::{
5
5
  bindgen_prelude::{BigInt, Buffer, Either3},
6
6
  Env, JsBuffer, JsBufferValue,
@@ -19,6 +19,10 @@ pub struct TracingMessage {
19
19
  #[napi(readonly)]
20
20
  pub to: Option<Buffer>,
21
21
 
22
+ /// Whether it's a static call
23
+ #[napi(readonly)]
24
+ pub is_static_call: bool,
25
+
22
26
  /// Transaction gas limit
23
27
  #[napi(readonly)]
24
28
  pub gas_limit: BigInt,
@@ -47,29 +51,41 @@ pub struct TracingMessage {
47
51
 
48
52
  impl TracingMessage {
49
53
  pub fn new(env: &Env, message: &BeforeMessage) -> napi::Result<Self> {
54
+ // Deconstruct to make sure all fields are handled
55
+ let BeforeMessage {
56
+ depth,
57
+ caller,
58
+ to: _,
59
+ is_static_call,
60
+ gas_limit,
61
+ data,
62
+ value,
63
+ code_address,
64
+ code,
65
+ } = message;
66
+
50
67
  let data = env
51
- .create_buffer_with_data(message.data.to_vec())
68
+ .create_buffer_with_data(data.to_vec())
52
69
  .map(JsBufferValue::into_raw)?;
53
70
 
54
- let code = message.code.as_ref().map_or(Ok(None), |code| {
71
+ let code = code.as_ref().map_or(Ok(None), |code| {
55
72
  env.create_buffer_with_data(code.original_bytes().to_vec())
56
73
  .map(JsBufferValue::into_raw)
57
74
  .map(Some)
58
75
  })?;
59
76
 
60
77
  Ok(TracingMessage {
61
- caller: Buffer::from(message.caller.as_slice()),
78
+ caller: Buffer::from(caller.as_slice()),
62
79
  to: message.to.map(|to| Buffer::from(to.as_slice())),
63
- gas_limit: BigInt::from(message.gas_limit),
64
- depth: message.depth as u8,
80
+ gas_limit: BigInt::from(*gas_limit),
81
+ is_static_call: *is_static_call,
82
+ depth: *depth as u8,
65
83
  data,
66
84
  value: BigInt {
67
85
  sign_bit: false,
68
- words: message.value.into_limbs().to_vec(),
86
+ words: value.into_limbs().to_vec(),
69
87
  },
70
- code_address: message
71
- .code_address
72
- .map(|address| Buffer::from(address.to_vec())),
88
+ code_address: code_address.map(|address| Buffer::from(address.to_vec())),
73
89
  code,
74
90
  })
75
91
  }
@@ -86,27 +102,46 @@ pub struct TracingStep {
86
102
  /// The executed op code
87
103
  #[napi(readonly)]
88
104
  pub opcode: String,
89
- /// The top entry on the stack. None if the stack is empty.
105
+ /// The entries on the stack. It only contains the top element unless
106
+ /// verbose tracing is enabled. The vector is empty if there are no elements
107
+ /// on the stack.
108
+ #[napi(readonly)]
109
+ pub stack: Vec<BigInt>,
110
+ /// The memory at the step. None if verbose tracing is disabled.
90
111
  #[napi(readonly)]
91
- pub stack_top: Option<BigInt>,
112
+ pub memory: Option<Buffer>,
92
113
  }
93
114
 
94
115
  impl TracingStep {
95
116
  pub fn new(step: &edr_evm::trace::Step) -> Self {
117
+ let stack = step.stack.full().map_or_else(
118
+ || {
119
+ step.stack
120
+ .top()
121
+ .map(u256_to_bigint)
122
+ .map_or_else(Vec::default, |top| vec![top])
123
+ },
124
+ |stack| stack.iter().map(u256_to_bigint).collect(),
125
+ );
126
+ let memory = step.memory.as_ref().cloned().map(Buffer::from);
127
+
96
128
  Self {
97
129
  depth: step.depth as u8,
98
130
  pc: BigInt::from(step.pc),
99
- opcode: OPCODE_JUMPMAP[usize::from(step.opcode)]
100
- .unwrap_or("")
101
- .to_string(),
102
- stack_top: step.stack_top.map(|v| BigInt {
103
- sign_bit: false,
104
- words: v.into_limbs().to_vec(),
105
- }),
131
+ opcode: OpCode::name_by_op(step.opcode).to_string(),
132
+ stack,
133
+ memory,
106
134
  }
107
135
  }
108
136
  }
109
137
 
138
+ fn u256_to_bigint(v: &edr_evm::U256) -> BigInt {
139
+ BigInt {
140
+ sign_bit: false,
141
+ words: v.into_limbs().to_vec(),
142
+ }
143
+ }
144
+
110
145
  #[napi(object)]
111
146
  pub struct TracingMessageResult {
112
147
  /// Execution result
@@ -140,7 +175,7 @@ impl RawTrace {
140
175
  TracingMessage::new(&env, message).map(Either3::A)
141
176
  }
142
177
  edr_evm::trace::TraceMessage::Step(step) => Ok(Either3::B(TracingStep::new(step))),
143
- edr_evm::trace::TraceMessage::After(result) => ExecutionResult::new(&env, result)
178
+ edr_evm::trace::TraceMessage::After(message) => ExecutionResult::new(&env, message)
144
179
  .map(|execution_result| Either3::C(TracingMessageResult { execution_result })),
145
180
  })
146
181
  .collect::<napi::Result<_>>()