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

Avoid Using Hardcode ChainId for Internal Transaction #1318

Merged
merged 4 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions frame/dvm/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ impl<T: Config> InternalTransactHandler for Pallet<T> {
fn internal_transact(target: H160, input: Vec<u8>) -> DispatchResultWithPostInfo {
let source = T::PalletId::get().derive_ethereum_address();
let nonce = <T as darwinia_evm::Config>::RingAccountBasic::account_basic(&source).nonce;
let transaction = internal_transaction(nonce, target, input);
let transaction = internal_transaction::<T>(nonce, target, input);

Self::raw_transact(source, transaction).map(|(reason, used_gas)| match reason {
// Only when exit_reason is successful, return Ok(...)
Expand All @@ -983,7 +983,7 @@ impl<T: Config> InternalTransactHandler for Pallet<T> {
sp_io::storage::start_transaction();
let source = T::PalletId::get().derive_ethereum_address();
let nonce = <T as darwinia_evm::Config>::RingAccountBasic::account_basic(&source).nonce;
let transaction = internal_transaction(nonce, contract, input);
let transaction = internal_transaction::<T>(nonce, contract, input);

let (_, _, info) = Self::execute(source, &transaction, None)?;
sp_io::storage::rollback_transaction();
Expand Down Expand Up @@ -1064,7 +1064,11 @@ impl From<Transaction> for AdvancedTransaction {
}
}

pub fn internal_transaction(nonce: U256, target: H160, input: Vec<u8>) -> AdvancedTransaction {
pub fn internal_transaction<T: Config>(
nonce: U256,
target: H160,
input: Vec<u8>,
) -> AdvancedTransaction {
let transaction = ethereum::TransactionV0 {
nonce,
// Not used, and will be overwritten by None later.
Expand All @@ -1076,9 +1080,9 @@ pub fn internal_transaction(nonce: U256, target: H160, input: Vec<u8>) -> Advanc
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,
// The internal transaction is sent by specific pallets, no signature
// validation, just create a valid transaction signature is enough.
T::ChainId::get() * 2 + 36,
H256::from_slice(&[55u8; 32]),
H256::from_slice(&[55u8; 32]),
)
Expand Down
4 changes: 2 additions & 2 deletions frame/dvm/ethereum/src/tests/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ fn internal_transaction_should_works() {
System::assert_last_event(Event::Ethereum(crate::Event::Executed(
<Test as self::Config>::PalletId::get().derive_ethereum_address(),
contract_address,
H256::from_str("0xabdebc2d8a79e4c40d6d66c614bafc2be138d4fc0fd21e28d318f3a032cbee39")
H256::from_str("0xad9426a685cbd9077fc6945dfd294c1d42862950e0ac292ea2e9d34ecf7a9007")
.unwrap(),
ExitReason::Succeed(ExitSucceed::Returned),
)));
Expand All @@ -267,7 +267,7 @@ fn internal_transaction_should_works() {
System::assert_last_event(Event::Ethereum(crate::Event::Executed(
<Test as self::Config>::PalletId::get().derive_ethereum_address(),
contract_address,
H256::from_str("0x2028ce5eef8d4531d4f955c9860b28f9e8cd596b17fea2326d2be49a8d3dc7ac")
H256::from_str("0x85a0a4a2620d7adb3d15a4a295ec4e786b8b5ca115e76a2fe89bb90c876ab694")
.unwrap(),
ExitReason::Succeed(ExitSucceed::Returned),
)));
Expand Down