Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

Commit

Permalink
dvm transaction nonce (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
boundless-forest authored Apr 16, 2021
1 parent 3c84402 commit 0340366
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 62 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions frame/dvm/rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ codec = { package = "parity-scale-codec", version = "2.0.1", default-fe
ethereum = { version = "0.7.1", default-features = false, features = ["with-codec"] }
ethereum-types = { version = "0.11.0", default-features = false }
# darwinia
darwinia-evm = { default-features = false, path = "../../../evm" }
dp-evm = { default-features = false, path = "../../../../primitives/evm" }
darwinia-evm = { default-features = false, path = "../../../evm" }
darwinia-support = { default-features = false, path = "../../../support" }
dp-evm = { default-features = false, path = "../../../../primitives/evm" }
# substrate
sp-api = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.10.0" }
sp-core = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.10.0" }
Expand All @@ -35,6 +36,7 @@ std = [

darwinia-std = [
"darwinia-evm/std",
"darwinia-support/std",
"dp-evm/std",
]

Expand Down
44 changes: 44 additions & 0 deletions frame/dvm/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode};
use darwinia_support::evm::INTERNAL_CALLER;
use ethereum::{Block as EthereumBlock, Log};
use ethereum_types::Bloom;
use sp_core::{H160, H256, U256};
Expand Down Expand Up @@ -47,6 +48,49 @@ impl Default for TransactionStatus {
}
}

/// The dvm transaction used by inner pallets, such as ethereum-issuing.
pub struct DVMTransaction {
/// source of the transaction
pub source: H160,
/// gas price wrapped by Option
pub gas_price: Option<U256>,
/// the transaction defined in ethereum lib
pub tx: ethereum::Transaction,
}

impl DVMTransaction {
/// the internal transaction usually used by pallets
/// the source account is specified by INTERNAL_CALLER
/// gas_price is None means no need for gas fee
/// a default signature which will not be verified
pub fn new(nonce: U256, target: H160, input: Vec<u8>) -> Self {
let transaction = ethereum::Transaction {
nonce,
// Not used, and will be overwritten by None later.
gas_price: U256::zero(),
gas_limit: U256::from(0x100000),
action: ethereum::TransactionAction::Call(target),
value: U256::zero(),
input,
signature: ethereum::TransactionSignature::new(
// Reference https://github.com/ethereum/EIPs/issues/155
//
// But this transaction is sent by darwinia-issuing system from `0x0`
// So ignore signature checking, simply set `chain_id` to `1`
1 * 2 + 36,
H256::from_slice(&[55u8; 32]),
H256::from_slice(&[55u8; 32]),
)
.unwrap(),
};
Self {
source: INTERNAL_CALLER,
gas_price: None,
tx: transaction,
}
}
}

sp_api::decl_runtime_apis! {
/// API necessary for Ethereum-compatibility layer.
pub trait EthereumRuntimeRPCApi {
Expand Down
69 changes: 9 additions & 60 deletions frame/dvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@

// --- darwinia ---
use darwinia_evm::{AccountBasic, FeeCalculator, GasWeightMapping, Runner};
use darwinia_support::evm::INTERNAL_CALLER;
use dp_consensus::{PostLog, PreLog, FRONTIER_ENGINE_ID};
use dp_evm::CallOrCreateInfo;
#[cfg(feature = "std")]
use dp_storage::PALLET_ETHEREUM_SCHEMA;
pub use dvm_rpc_runtime_api::TransactionStatus;
pub use dvm_rpc_runtime_api::{DVMTransaction, TransactionStatus};
// --- substrate ---
use frame_support::ensure;
use frame_support::traits::Currency;
Expand Down Expand Up @@ -81,53 +82,6 @@ impl Default for EthereumStorageSchema {
}
}

/// The ethereum transaction include recovered source account
pub struct DVMTransaction {
/// source of the transaction
pub source: H160,
/// nonce wrapped by Option
pub nonce: Option<U256>,
/// gas price wrapped by Option
pub gas_price: Option<U256>,
/// the transaction defined in ethereum lib
pub tx: ethereum::Transaction,
}

impl DVMTransaction {
/// the internal transaction usually used by pallets
/// the source account is specified by 0x0 address
/// nonce is None means nonce automatically increased
/// gas_price is None means no need for gas fee
/// a default signature which will not be verified
pub fn internal_transaction(nonce: u64, target: H160, input: Vec<u8>) -> Self {
let transaction = ethereum::Transaction {
nonce: U256::from(nonce),
// Not used, and will be overwritten by None later.
gas_price: U256::zero(),
gas_limit: U256::from(0x100000),
action: TransactionAction::Call(target),
value: U256::zero(),
input,
signature: TransactionSignature::new(
// Reference https://github.com/ethereum/EIPs/issues/155
//
// But this transaction is sent by darwinia-issuing system from `0x0`
// So ignore signature checking, simply set `chain_id` to `1`
1 * 2 + 36,
H256::from_slice(&[55u8; 32]),
H256::from_slice(&[55u8; 32]),
)
.unwrap(),
};
Self {
source: H160::zero(),
nonce: None,
gas_price: None,
tx: transaction,
}
}
}

/// A type alias for the balance type from this pallet's point of view.
type AccountId<T> = <T as frame_system::Config>::AccountId;
pub type RingCurrency<T> = <T as Config>::RingCurrency;
Expand Down Expand Up @@ -175,8 +129,6 @@ decl_storage! {
RemainingRingBalance get(fn get_ring_remaining_balances): map hasher(blake2_128_concat) T::AccountId => RingBalance<T>;
/// Remaining kton balance for account
RemainingKtonBalance get(fn get_kton_remaining_balances): map hasher(blake2_128_concat) T::AccountId => KtonBalance<T>;
/// The nonce for internal transactions from system internal address(0x0).
pub InternalNonce: u64 = 0;
}
add_extra_genesis {
build(|_config: &GenesisConfig| {
Expand Down Expand Up @@ -324,14 +276,13 @@ impl<T: Config> Module<T> {
)))
}

fn convert_transaction(
fn to_dvm_transaction(
transaction: ethereum::Transaction,
) -> Result<DVMTransaction, DispatchError> {
let source =
Self::recover_signer(&transaction).ok_or_else(|| Error::<T>::InvalidSignature)?;
Ok(DVMTransaction {
source,
nonce: Some(transaction.nonce),
gas_price: Some(transaction.gas_price),
tx: transaction,
})
Expand Down Expand Up @@ -405,11 +356,9 @@ impl<T: Config> Module<T> {
dp_consensus::find_pre_log(&<frame_system::Pallet<T>>::digest()).is_err(),
Error::<T>::PreLogExists,
);
let internal_nonce = InternalNonce::get();
let transaction = DVMTransaction::internal_transaction(internal_nonce, target, input);

// Not check overflow here, it will go back to zero if overflow
InternalNonce::put(internal_nonce + 1);
let nonce =
<T as darwinia_evm::Config>::RingAccountBasic::account_basic(&INTERNAL_CALLER).nonce;
let transaction = DVMTransaction::new(nonce, target, input);

Self::raw_transact(transaction)
}
Expand All @@ -419,13 +368,13 @@ impl<T: Config> Module<T> {
dp_consensus::find_pre_log(&<frame_system::Pallet<T>>::digest()).is_err(),
Error::<T>::PreLogExists,
);
let transaction = Self::convert_transaction(transaction)?;
let transaction = Self::to_dvm_transaction(transaction)?;
Self::raw_transact(transaction)
}

pub fn do_call(contract: H160, input: Vec<u8>) -> Result<Vec<u8>, DispatchError> {
let (_, _, info) = Self::execute(
H160::zero(),
INTERNAL_CALLER,
input.clone(),
U256::zero(),
U256::from(0x100000),
Expand Down Expand Up @@ -455,7 +404,7 @@ impl<T: Config> Module<T> {
transaction.tx.value,
transaction.tx.gas_limit,
transaction.gas_price,
transaction.nonce,
Some(transaction.tx.nonce),
transaction.tx.action,
None,
)?;
Expand Down
2 changes: 2 additions & 0 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub mod balance {
}

pub mod evm {
use ethereum_primitives::H160;
pub const POW_9: u32 = 1_000_000_000;
pub const INTERNAL_CALLER: H160 = H160::zero();
}

#[cfg(test)]
Expand Down

0 comments on commit 0340366

Please sign in to comment.