Skip to content

Commit

Permalink
Add used_gas to receipts. (#139)
Browse files Browse the repository at this point in the history
* Add gas_used to receipts.

* Add gas_used test.
  • Loading branch information
jnaviask authored Sep 27, 2020
1 parent c0908a0 commit 98f9ce8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
20 changes: 10 additions & 10 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl<T: Trait> Module<T> {
)
),
gas_limit: U256::zero(), // TODO: set this using Ethereum's gas limit change algorithm.
gas_used: U256::zero(), // TODO: get this from receipts.
gas_used: receipts.clone().into_iter().fold(U256::zero(), |acc, r| acc + r.used_gas),
timestamp: UniqueSaturatedInto::<u64>::unique_saturated_into(
pallet_timestamp::Module::<T>::get()
),
Expand Down Expand Up @@ -335,9 +335,9 @@ impl<T: Trait> Module<T> {
);
let transaction_index = Pending::get().len() as u32;

let status = match transaction.action {
let (status, gas_used) = match transaction.action {
ethereum::TransactionAction::Call(target) => {
Self::handle_exec(
let (_, _, gas_used) = Self::handle_exec(
pallet_evm::Module::<T>::execute_call(
source,
target,
Expand All @@ -350,18 +350,18 @@ impl<T: Trait> Module<T> {
)?
)?;

TransactionStatus {
(TransactionStatus {
transaction_hash,
transaction_index,
from: source,
to: Some(target),
contract_address: None,
logs: Vec::new(), // TODO: feed in logs.
logs_bloom: Bloom::default(), // TODO: feed in bloom.
}
}, gas_used)
},
ethereum::TransactionAction::Create => {
let contract_address = Self::handle_exec(
let (_, contract_address, gas_used) = Self::handle_exec(
pallet_evm::Module::<T>::execute_create(
source,
transaction.input.clone(),
Expand All @@ -371,23 +371,23 @@ impl<T: Trait> Module<T> {
Some(transaction.nonce),
true,
)?
)?.1;
)?;

TransactionStatus {
(TransactionStatus {
transaction_hash,
transaction_index,
from: source,
to: None,
contract_address: Some(contract_address),
logs: Vec::new(), // TODO: feed in logs.
logs_bloom: Bloom::default(), // TODO: feed in bloom.
}
}, gas_used)
},
};

let receipt = ethereum::Receipt {
state_root: H256::default(), // TODO: should be okay / error status.
used_gas: U256::default(), // TODO: set this.
used_gas: gas_used,
logs_bloom: Bloom::default(), // TODO: set this.
logs: Vec::new(), // TODO: set this.
};
Expand Down
17 changes: 16 additions & 1 deletion frame/ethereum/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ fn invalid_signature_should_be_ignored() {
});
}


#[test]
fn contract_should_be_created_at_given_address() {
let (pairs, mut ext) = new_test_ext(1);
Expand All @@ -203,3 +202,19 @@ fn contract_should_be_created_at_given_address() {
assert_ne!(Evm::account_codes(erc20_address).len(), 0);
});
}

#[test]
fn transaction_should_generate_correct_gas_used() {
let (pairs, mut ext) = new_test_ext(1);
let alice = &pairs[0];

let expected_gas = U256::from(891328);

ext.execute_with(|| {
assert_ok!(Ethereum::execute(
alice.address,
default_erc20_creation_transaction(alice),
));
assert_eq!(Pending::get()[0].2.used_gas, expected_gas);
});
}

0 comments on commit 98f9ce8

Please sign in to comment.