Skip to content

Commit a595903

Browse files
committed
feat: ink_revive crate
1 parent 4026157 commit a595903

File tree

13 files changed

+445
-17
lines changed

13 files changed

+445
-17
lines changed

Cargo.lock

Lines changed: 25 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ members = [
1212
"crates/ink/ir",
1313
"crates/ink/macro",
1414
"crates/metadata",
15+
"crates/revive",
1516
"crates/prelude",
1617
"crates/primitives",
1718
"crates/storage",
@@ -104,6 +105,7 @@ xcm = { package = "staging-xcm", git = "https://github.com/use-ink/polkadot-sdk.
104105
polkavm-derive = { version = "0.26.0", default-features = false }
105106

106107
# Solidity dependencies
108+
alloy-core = { version = "1.1.0", default-features = false }
107109
alloy-sol-types = { version = "1.3.0", default-features = false }
108110
const_format = { version = "0.2.34", features = ["fmt"] }
109111
keccak-const = "0.2.0"
@@ -121,6 +123,7 @@ ink_macro = { version = "=6.0.0-alpha.4", path = "crates/ink/macro", default-fea
121123
ink_metadata = { version = "=6.0.0-alpha.4", path = "crates/metadata", default-features = false }
122124
ink_prelude = { version = "=6.0.0-alpha.4", path = "crates/prelude", default-features = false }
123125
ink_primitives = { version = "=6.0.0-alpha.4", path = "crates/primitives", default-features = false }
126+
ink_revive = { version = "=6.0.0-alpha.4", path = "crates/revive", default-features = false }
124127
ink_storage = { version = "=6.0.0-alpha.4", path = "crates/storage", default-features = false }
125128
ink_storage_traits = { version = "=6.0.0-alpha.4", path = "crates/storage/traits", default-features = false }
126129

crates/e2e/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ink_e2e_macro = { workspace = true, default-features = true }
1919
ink = { workspace = true, default-features = true }
2020
ink_env = { workspace = true, default-features = true }
2121
ink_primitives = { workspace = true, default-features = true }
22+
ink_revive = { workspace = true, default-features = true }
2223
ink_sandbox = { version = "=6.0.0-alpha.4", path = "./sandbox", optional = true }
2324

2425
cargo_metadata = { workspace = true }
@@ -61,7 +62,6 @@ default = [ "std" ]
6162
std = [
6263
"impl-serde/std",
6364
"ink_e2e_macro/std",
64-
"pallet-revive/std",
6565
"scale-info/std",
6666
"scale/std",
6767
"serde/std",
@@ -72,6 +72,7 @@ std = [
7272
"sp-runtime-interface/std",
7373
"sp-weights/std",
7474
"ink_e2e_macro/std",
75+
"ink_revive/std",
7576
"ink_sandbox?/std",
7677
"frame-support/std",
7778
]

crates/e2e/sandbox/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ sp-externalities = { workspace = true }
2323
sp-runtime = { workspace = true }
2424
sp-io = { workspace = true }
2525
ink_primitives = { workspace = true }
26+
ink_revive = { workspace = true }
2627

2728
paste = { workspace = true }
2829
scale-info = { workspace = true }
@@ -37,6 +38,7 @@ std = [
3738
"frame-system/std",
3839
"frame-metadata/std",
3940
"ink_primitives/std",
41+
"ink_revive/std",
4042
"pallet-balances/std",
4143
"pallet-revive/std",
4244
"pallet-timestamp/std",

crates/revive/Cargo.toml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[package]
2+
name = "ink_revive"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
description = "ink! revive primitives (placeholder)"
8+
repository.workspace = true
9+
homepage.workspace = true
10+
keywords.workspace = true
11+
categories.workspace = true
12+
13+
[lib]
14+
path = "src/lib.rs"
15+
16+
[dependencies]
17+
scale = { workspace = true }
18+
frame-support = { workspace = true }
19+
scale-info = { workspace = true }
20+
serde = { workspace = true, features = ["derive"] }
21+
ink_primitives = { workspace = true }
22+
# sp-weights = { workspace = true }
23+
# sp-core = { workspace = true }
24+
sp-runtime = { workspace = true }
25+
pallet-revive-uapi = { workspace = true, features = ["scale"] }
26+
alloy-core = { workspace = true, features = ["sol-types"] }
27+
28+
[features]
29+
default = ["std"]
30+
std = [
31+
"alloy-core/std",
32+
"scale/std",
33+
"scale-info/std",
34+
"frame-support/std",
35+
"ink_primitives/std",
36+
# "sp-weights/std",
37+
# "sp-core/std",
38+
"sp-runtime/std",
39+
"serde/std",
40+
]
41+

crates/revive/src/evm.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use scale::{Decode, Encode};
2+
use serde::{
3+
Deserialize,
4+
Serialize,
5+
};
6+
use alloc::vec::Vec;
7+
use scale_info::TypeInfo;
8+
use ink_primitives::{
9+
H160, U256, H256
10+
};
11+
12+
13+
/// A smart contract execution call trace.
14+
#[derive(
15+
TypeInfo, Default, Encode, Decode, Serialize, Deserialize, Clone, Debug, Eq, PartialEq,
16+
)]
17+
#[serde(rename_all = "camelCase")]
18+
pub struct CallTrace<Gas = U256> {
19+
/// Address of the sender.
20+
pub from: H160,
21+
/// Amount of gas provided for the call.
22+
pub gas: Gas,
23+
/// Amount of gas used.
24+
pub gas_used: Gas,
25+
/// Address of the receiver.
26+
pub to: H160,
27+
/// Call input data.
28+
pub input: Vec<u8>,
29+
/// Return data.
30+
#[serde(skip_serializing_if = "Vec::is_empty")]
31+
pub output: Vec<u8>,
32+
/// The error message if the call failed.
33+
#[serde(skip_serializing_if = "Option::is_none")]
34+
pub error: Option<String>,
35+
/// The revert reason, if the call reverted.
36+
#[serde(skip_serializing_if = "Option::is_none")]
37+
pub revert_reason: Option<String>,
38+
/// List of sub-calls.
39+
#[serde(skip_serializing_if = "Vec::is_empty")]
40+
pub calls: Vec<CallTrace<Gas>>,
41+
/// List of logs emitted during the call.
42+
#[serde(skip_serializing_if = "Vec::is_empty")]
43+
pub logs: Vec<CallLog>,
44+
/// Amount of value transferred.
45+
#[serde(skip_serializing_if = "Option::is_none")]
46+
pub value: Option<U256>,
47+
/// Type of call.
48+
#[serde(rename = "type")]
49+
pub call_type: CallType,
50+
}
51+
52+
/// A log emitted during a call.
53+
#[derive(
54+
Debug, Default, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, Eq, PartialEq,
55+
)]
56+
pub struct CallLog {
57+
/// The address of the contract that emitted the log.
58+
pub address: H160,
59+
/// The topics used to index the log.
60+
#[serde(default, skip_serializing_if = "Vec::is_empty")]
61+
pub topics: Vec<H256>,
62+
/// The log's data.
63+
pub data: Vec<u8>,
64+
/// Position of the log relative to subcalls within the same trace
65+
/// See <https://github.com/ethereum/go-ethereum/pull/28389> for details
66+
#[serde(with = "super::hex_serde")]
67+
pub position: u32,
68+
}
69+
70+
/// The type of call that was executed.
71+
#[derive(
72+
Default, TypeInfo, Encode, Decode, Serialize, Deserialize, Eq, PartialEq, Clone, Debug,
73+
)]
74+
#[serde(rename_all = "UPPERCASE")]
75+
pub enum CallType {
76+
/// A regular call.
77+
#[default]
78+
Call,
79+
/// A read-only call.
80+
StaticCall,
81+
/// A delegate call.
82+
DelegateCall,
83+
/// A create call.
84+
Create,
85+
/// A create2 call.
86+
Create2,
87+
}

crates/revive/src/hex_serde.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
use alloc::{format, string::String, vec::Vec};
19+
use alloy_core::hex;
20+
use serde::{Deserialize, Deserializer, Serializer};
21+
22+
pub trait HexCodec: Sized {
23+
type Error;
24+
fn to_hex(&self) -> String;
25+
fn from_hex(s: String) -> Result<Self, Self::Error>;
26+
}
27+
28+
macro_rules! impl_hex_codec {
29+
($($t:ty),*) => {
30+
$(
31+
impl HexCodec for $t {
32+
type Error = core::num::ParseIntError;
33+
fn to_hex(&self) -> String {
34+
format!("0x{:x}", self)
35+
}
36+
fn from_hex(s: String) -> Result<Self, Self::Error> {
37+
<$t>::from_str_radix(s.trim_start_matches("0x"), 16)
38+
}
39+
}
40+
)*
41+
};
42+
}
43+
44+
impl_hex_codec!(u8, u32);
45+
46+
impl<const T: usize> HexCodec for [u8; T] {
47+
type Error = hex::FromHexError;
48+
fn to_hex(&self) -> String {
49+
format!("0x{}", hex::encode(self))
50+
}
51+
fn from_hex(s: String) -> Result<Self, Self::Error> {
52+
let data = hex::decode(s.trim_start_matches("0x"))?;
53+
data.try_into().map_err(|_| hex::FromHexError::InvalidStringLength)
54+
}
55+
}
56+
57+
impl HexCodec for Vec<u8> {
58+
type Error = hex::FromHexError;
59+
fn to_hex(&self) -> String {
60+
format!("0x{}", hex::encode(self))
61+
}
62+
fn from_hex(s: String) -> Result<Self, Self::Error> {
63+
hex::decode(s.trim_start_matches("0x"))
64+
}
65+
}
66+
67+
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
68+
where
69+
S: Serializer,
70+
T: HexCodec,
71+
{
72+
let s = value.to_hex();
73+
serializer.serialize_str(&s)
74+
}
75+
76+
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
77+
where
78+
D: Deserializer<'de>,
79+
T: HexCodec,
80+
<T as HexCodec>::Error: core::fmt::Debug,
81+
{
82+
let s = String::deserialize(deserializer)?;
83+
let value = T::from_hex(s).map_err(|e| serde::de::Error::custom(format!("{:?}", e)))?;
84+
Ok(value)
85+
}

crates/revive/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![cfg_attr(not(feature = "std"), no_std)]
2+
extern crate alloc;
3+
4+
mod evm;
5+
mod hex_serde;
6+
mod primitives;
7+
8+
pub use evm::CallTrace;
9+
pub use primitives::{ContractResult, ExecReturnValue, InstantiateReturnValue, StorageDeposit};

0 commit comments

Comments
 (0)