Skip to content

Commit af6b985

Browse files
committed
more tests
1 parent fbe56f1 commit af6b985

File tree

4 files changed

+101
-18
lines changed

4 files changed

+101
-18
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ test-eth: githooks test-evm
150150

151151
.PHONY: test-evm
152152
test-evm: githooks
153-
SKIP_WASM_BUILD= ${cargo_test} -p module-evm --features tracing
153+
SKIP_WASM_BUILD= ${cargo_test} -p module-evm -p module-evm-bridge --features tracing
154154
SKIP_WASM_BUILD= ${cargo_test} --release -p evm-jsontests --features evm-tests
155155

156156
.PHONY: test-runtimes

modules/evm-bridge/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ try-runtime = [
5959
"frame-system/try-runtime",
6060
"module-evm/try-runtime",
6161
]
62+
tracing = ["module-evm/tracing"]

modules/evm-bridge/src/tests.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,81 @@ fn liquidation_err_fails_as_expected() {
353353
);
354354
});
355355
}
356+
357+
#[cfg(feature = "tracing")]
358+
#[test]
359+
fn tracing_should_work() {
360+
use module_evm::runner::tracing;
361+
use primitives::evm::tracing::TracerConfig;
362+
363+
ExtBuilder::default()
364+
.balances(vec![(alice(), 1_000_000_000_000), (bob(), 1_000_000_000_000)])
365+
.build()
366+
.execute_with(|| {
367+
deploy_contracts();
368+
let mut tracer = tracing::Tracer::new(TracerConfig::CallTracer);
369+
tracing::using(&mut tracer, || {
370+
assert_err!(
371+
EVMBridge::<Runtime>::transfer(
372+
InvokeContext {
373+
contract: erc20_address(),
374+
sender: bob_evm_addr(),
375+
origin: bob_evm_addr(),
376+
},
377+
alice_evm_addr(),
378+
10
379+
),
380+
Error::<Runtime>::ExecutionRevert
381+
);
382+
});
383+
let expected = r#"[
384+
{
385+
"type": "CALL",
386+
"from": "0x1000000000000000000000000000000000000002",
387+
"to": "0x5dddfce53ee040d9eb21afbc0ae1bb4dbb0ba643",
388+
"input": "0xa9059cbb0000000000000000000000001000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a",
389+
"value": "0x0",
390+
"gas": 200000,
391+
"gasUsed": 200000,
392+
"output": null,
393+
"error": null,
394+
"revertReason": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002645524332303a207472616e7366657220616d6f756e7420657863656564732062616c616e63650000000000000000000000000000000000000000000000000000",
395+
"depth": 0,
396+
"calls": []
397+
}
398+
]"#;
399+
let expected = serde_json::from_str::<Vec<tracing::CallTrace>>(expected).unwrap();
400+
assert_eq!(tracer.finalize(), tracing::TraceOutcome::Calls(expected));
401+
402+
tracing::using(&mut tracer, || {
403+
assert_ok!(EVMBridge::<Runtime>::transfer(
404+
InvokeContext {
405+
contract: erc20_address(),
406+
sender: alice_evm_addr(),
407+
origin: alice_evm_addr(),
408+
},
409+
bob_evm_addr(),
410+
100
411+
));
412+
});
413+
414+
let expected = r#"[
415+
{
416+
"type": "CALL",
417+
"from": "0x1000000000000000000000000000000000000001",
418+
"to": "0x5dddfce53ee040d9eb21afbc0ae1bb4dbb0ba643",
419+
"input": "0xa9059cbb00000000000000000000000010000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000064",
420+
"value": "0x0",
421+
"gas": 200000,
422+
"gasUsed": 51929,
423+
"output": "0x0000000000000000000000000000000000000000000000000000000000000001",
424+
"error": null,
425+
"revertReason": null,
426+
"depth": 0,
427+
"calls": []
428+
}
429+
]"#;
430+
let expected = serde_json::from_str::<Vec<tracing::CallTrace>>(expected).unwrap();
431+
assert_eq!(tracer.finalize(), tracing::TraceOutcome::Calls(expected));
432+
});
433+
}

primitives/src/evm.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -384,20 +384,24 @@ pub mod tracing {
384384
}
385385

386386
#[cfg(feature = "std")]
387-
fn vec_to_hex<S>(data: &[u8], serializer: S) -> Result<S::Ok, S::Error>
388-
where
389-
S: serde::Serializer,
390-
{
391-
serializer.serialize_str(&sp_core::bytes::to_hex(data, false))
392-
}
387+
mod maybe_hex {
388+
use serde::{Deserialize, Deserializer, Serializer};
389+
pub fn serialize<S: Serializer>(data: &Option<Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error> {
390+
if let Some(data) = data {
391+
sp_core::bytes::serialize(data.as_slice(), serializer)
392+
} else {
393+
serializer.serialize_none()
394+
}
395+
}
393396

394-
#[cfg(feature = "std")]
395-
fn hex_to_vec<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
396-
where
397-
D: serde::Deserializer<'de>,
398-
{
399-
use serde::de::Error;
400-
String::deserialize(deserializer).and_then(|string| sp_core::bytes::from_hex(&string).map_err(Error::custom))
397+
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<Vec<u8>>, D::Error> {
398+
use serde::de::Error;
399+
match Option::deserialize(deserializer) {
400+
Ok(Some(data)) => sp_core::bytes::from_hex(data).map_err(Error::custom).map(Some),
401+
Ok(None) => Ok(None),
402+
Err(e) => Err(e),
403+
}
404+
}
401405
}
402406

403407
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
@@ -408,21 +412,21 @@ pub mod tracing {
408412
pub call_type: CallType,
409413
pub from: H160,
410414
pub to: H160,
411-
#[cfg_attr(
412-
feature = "std",
413-
serde(serialize_with = "vec_to_hex", deserialize_with = "hex_to_vec")
414-
)]
415+
#[cfg_attr(feature = "std", serde(with = "sp_core::bytes"))]
415416
pub input: Vec<u8>,
416417
pub value: U256,
417418
// gas limit
418419
#[codec(compact)]
419420
pub gas: u64,
420421
#[codec(compact)]
421422
pub gas_used: u64,
423+
#[cfg_attr(feature = "std", serde(with = "maybe_hex"))]
422424
// value returned from EVM, if any
423425
pub output: Option<Vec<u8>>,
426+
#[cfg_attr(feature = "std", serde(with = "maybe_hex"))]
424427
// evm error, if any
425428
pub error: Option<Vec<u8>>,
429+
#[cfg_attr(feature = "std", serde(with = "maybe_hex"))]
426430
// revert reason, if any
427431
pub revert_reason: Option<Vec<u8>>,
428432
// depth of the call

0 commit comments

Comments
 (0)