Skip to content

Commit

Permalink
Fixed some codes from SimonJiao's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
harryliisme3 committed Feb 7, 2023
1 parent b602137 commit 5d0d393
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/components/config/src/abci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub struct CheckPointConfig {
// Fix the amount in the delegators that staking did not modify when it punished the validator.
pub fix_delegators_am_height: u64,
pub validators_limit_v2_height: u64,
// eip1559 support switch.
pub enable_eip1559_height: u64,

// https://github.com/FindoraNetwork/platform/pull/707
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
use ethereum::TransactionV0 as LegcayTransaction;
use ethereum::TransactionV2 as Transaction;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Action {
Transact(LegcayTransaction),
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Action2 {
Transact(Transaction),
}
8 changes: 0 additions & 8 deletions src/components/contracts/primitives/types/src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,3 @@ pub enum Action {
XHub(xhub::Action),
Template(template::Action),
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Action2 {
Ethereum(ethereum::Action2),
Evm(evm::Action),
XHub(xhub::Action),
Template(template::Action),
}
5 changes: 1 addition & 4 deletions src/components/contracts/primitives/types/src/assemble.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::actions::ethereum::Action as EtherAction;
use crate::actions::{Action, Action2};
use crate::actions::Action;
use crate::crypto::{Address, Signature};
use crate::transaction;
use ethereum::TransactionV0 as Transaction;
Expand Down Expand Up @@ -32,9 +32,6 @@ impl CheckFee {
pub type UncheckedTransaction<Extra> =
transaction::UncheckedTransaction<Address, Action, Signature, Extra>;

pub type UncheckedTransaction2<Extra> =
transaction::UncheckedTransaction<Address, Action2, Signature, Extra>;

/// Transaction type that has already been checked.
pub type CheckedTransaction<Extra> =
transaction::CheckedTransaction<Address, Action, Extra>;
Expand Down
1 change: 1 addition & 0 deletions src/components/contracts/primitives/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fp-utils = { path = "../../primitives/utils" }
rlp = "0.5"
ruc = "1.0"
sha3 = "0.10"
serde = { version = "1.0.124", features = ["derive"] }
serde_json = "1.0"
wasm-bindgen = { version = "=0.2.73", features = ["serde-serialize"] }

Expand Down
49 changes: 33 additions & 16 deletions src/components/contracts/primitives/wasm/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
use core::fmt::Display;
use ethereum::{
LegacyTransactionMessage, TransactionSignature, TransactionV0 as LegacyTransaction,
TransactionV2,
TransactionV2 as Transaction,
};
use ethereum_types::{H160, H256};
use serde::{Deserialize, Serialize};

use fp_types::{
actions::{ethereum::Action2 as EthAction2, Action2},
assemble::UncheckedTransaction2,
crypto::secp256k1_ecdsa_recover,
actions::{evm, template, xhub},
crypto::{secp256k1_ecdsa_recover, Address, Signature},
transaction,
};
use fp_utils::tx::EvmRawTxWrapper;
use ruc::{d, err::RucResult};
Expand All @@ -20,6 +21,22 @@ use wasm_bindgen::prelude::*;
use baseapp::BaseApp;
use fp_traits::evm::FeeCalculator;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum EthAction {
Transact(Transaction),
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Action {
Ethereum(EthAction),
Evm(evm::Action),
XHub(xhub::Action),
Template(template::Action),
}

pub type UncheckedTransaction<Extra> =
transaction::UncheckedTransaction<Address, Action, Signature, Extra>;

#[inline(always)]
pub(crate) fn error_to_jsvalue<T: Display>(e: T) -> JsValue {
JsValue::from_str(&e.to_string())
Expand All @@ -36,7 +53,7 @@ pub fn recover_signer(transaction: &LegacyTransaction) -> Option<H160> {

let pubkey = secp256k1_ecdsa_recover(&sig, &msg).ok()?;
Some(H160::from(H256::from_slice(
Keccak256::digest(&pubkey).as_slice(),
Keccak256::digest(pubkey).as_slice(),
)))
}

Expand All @@ -49,10 +66,10 @@ pub fn recover_tx_signer(raw_tx: String) -> Result<String, JsValue> {
.c(d!())
.map_err(error_to_jsvalue)?;

let unchecked_tx: UncheckedTransaction2<()> = serde_json::from_slice(raw_tx)
let unchecked_tx: UncheckedTransaction<()> = serde_json::from_slice(raw_tx)
.c(d!())
.map_err(error_to_jsvalue)?;
if let Action2::Ethereum(EthAction2::Transact(tx)) = unchecked_tx.function {
if let Action::Ethereum(EthAction::Transact(tx)) = unchecked_tx.function {
let tx = match new_tx2legcay_tx(tx) {
Some(tx) => tx,
None => return Err(error_to_jsvalue("invalid raw tx")),
Expand All @@ -73,10 +90,10 @@ pub fn evm_tx_hash(raw_tx: String) -> Result<String, JsValue> {
.c(d!())
.map_err(error_to_jsvalue)?;

let unchecked_tx: UncheckedTransaction2<()> = serde_json::from_slice(raw_tx)
let unchecked_tx: UncheckedTransaction<()> = serde_json::from_slice(raw_tx)
.c(d!())
.map_err(error_to_jsvalue)?;
if let Action2::Ethereum(EthAction2::Transact(tx)) = unchecked_tx.function {
if let Action::Ethereum(EthAction::Transact(tx)) = unchecked_tx.function {
let tx = match new_tx2legcay_tx(tx) {
Some(tx) => tx,
None => return Err(error_to_jsvalue("invalid raw tx")),
Expand All @@ -88,7 +105,7 @@ pub fn evm_tx_hash(raw_tx: String) -> Result<String, JsValue> {
}
}

fn new_tx2legcay_tx(tx: TransactionV2) -> Option<LegacyTransaction> {
fn new_tx2legcay_tx(tx: Transaction) -> Option<LegacyTransaction> {
let transaction: LegacyTransaction = match tx {
ethereum::TransactionV2::Legacy(tx) => tx,
ethereum::TransactionV2::EIP1559(tx) => {
Expand Down Expand Up @@ -130,13 +147,13 @@ mod test {
fn recover_signer_works() {
let raw_tx = String::from("eyJzaWduYXR1cmUiOm51bGwsImZ1bmN0aW9uIjp7IkV0aGVyZXVtIjp7IlRyYW5zYWN0Ijp7IkxlZ2FjeSI6eyJub25jZSI6IjB4MCIsImdhc19wcmljZSI6IjB4MjU0MGJlNDAwIiwiZ2FzX2xpbWl0IjoiMHgxMDAwMDAiLCJhY3Rpb24iOnsiQ2FsbCI6IjB4MzMyNWE3ODQyNWYxN2E3ZTQ4N2ViNTY2NmIyYmZkOTNhYmIwNmM3MCJ9LCJ2YWx1ZSI6IjB4YSIsImlucHV0IjpbXSwic2lnbmF0dXJlIjp7InYiOjQzNDAsInIiOiIweDZiMjBjNzIzNTEzOTk4ZThmYTQ4NWM1MmI4ZjlhZTRmZDdiMWUwYmQwZGZiNzk4NTIzMThiMGMxMDBlOTFmNWUiLCJzIjoiMHg0ZDRjOGMxZjJlMTdjMDJjNGE4OTZlMjYyMTI3YjhiZDZlYmZkNWY1YTc1NWEzZTkyMjBjZmM2OGI4YzY5ZDVkIn19fX19fQ==");
let tx_bytes = base64::decode_config(raw_tx, base64::URL_SAFE).unwrap();
let unchecked_tx: UncheckedTransaction2<()> =
let unchecked_tx: UncheckedTransaction<()> =
serde_json::from_slice(tx_bytes.as_slice()).unwrap();
if let Action2::Ethereum(EthAction2::Transact(tx)) = unchecked_tx.function {
if let Action::Ethereum(EthAction::Transact(tx)) = unchecked_tx.function {
let tx: LegacyTransaction = new_tx2legcay_tx(tx).unwrap();
let signer = recover_signer(&tx).unwrap();
assert_eq!(
format!("{:?}", signer),
format!("{signer:?}"),
"0x5050a4f4b3f9338c3472dcc01a87c76a144b3c9c"
);
} else {
Expand All @@ -148,13 +165,13 @@ mod test {
fn evm_tx_hash_works() {
let raw_tx = String::from("eyJzaWduYXR1cmUiOm51bGwsImZ1bmN0aW9uIjp7IkV0aGVyZXVtIjp7IlRyYW5zYWN0Ijp7IkxlZ2FjeSI6eyJub25jZSI6IjB4MCIsImdhc19wcmljZSI6IjB4MjU0MGJlNDAwIiwiZ2FzX2xpbWl0IjoiMHgxMDAwMDAiLCJhY3Rpb24iOnsiQ2FsbCI6IjB4MzMyNWE3ODQyNWYxN2E3ZTQ4N2ViNTY2NmIyYmZkOTNhYmIwNmM3MCJ9LCJ2YWx1ZSI6IjB4YSIsImlucHV0IjpbXSwic2lnbmF0dXJlIjp7InYiOjQzNDAsInIiOiIweDZiMjBjNzIzNTEzOTk4ZThmYTQ4NWM1MmI4ZjlhZTRmZDdiMWUwYmQwZGZiNzk4NTIzMThiMGMxMDBlOTFmNWUiLCJzIjoiMHg0ZDRjOGMxZjJlMTdjMDJjNGE4OTZlMjYyMTI3YjhiZDZlYmZkNWY1YTc1NWEzZTkyMjBjZmM2OGI4YzY5ZDVkIn19fX19fQ==");
let tx_bytes = base64::decode_config(raw_tx, base64::URL_SAFE).unwrap();
let unchecked_tx: UncheckedTransaction2<()> =
let unchecked_tx: UncheckedTransaction<()> =
serde_json::from_slice(tx_bytes.as_slice()).unwrap();
if let Action2::Ethereum(EthAction2::Transact(tx)) = unchecked_tx.function {
if let Action::Ethereum(EthAction::Transact(tx)) = unchecked_tx.function {
let tx: LegacyTransaction = new_tx2legcay_tx(tx).unwrap();
let hash = H256::from_slice(Keccak256::digest(&rlp::encode(&tx)).as_slice());
assert_eq!(
format!("{:?}", hash),
format!("{hash:?}"),
"0x83901d025accca27ee53fdf1ee354f4437418731e0995ee031beb99499405d26"
);
} else {
Expand Down

0 comments on commit 5d0d393

Please sign in to comment.