Skip to content

Commit

Permalink
Merge pull request #95 from aionnetwork/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
fredericzha authored Nov 29, 2019
2 parents 6e864a3 + 4699555 commit 7fd720c
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 133 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
description = "aion network rust implementation"
name = "aionr"
# NOTE Make sure to update util/version/Cargo.toml as well
version = "1.0.1"
version = "1.0.2"
license = "GPL-3.0"
authors = ["aion foundation <[email protected]>"]

Expand Down
25 changes: 13 additions & 12 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ impl<'x> OpenBlock<'x> {
&mut self,
txs: &[SignedTransaction],
h: Option<H256>,
check_gas: bool,
is_building_block: bool,
) -> Vec<Result<Receipt, Error>>
{
//TODO: deal with AVM parallelism
Expand All @@ -357,7 +357,7 @@ impl<'x> OpenBlock<'x> {
for apply_result in
self.block
.state
.apply_batch(&env_info, self.engine.machine(), txs, check_gas)
.apply_batch(&env_info, self.engine.machine(), txs, is_building_block)
{
let result = match apply_result {
Ok(outcome) => {
Expand All @@ -369,13 +369,13 @@ impl<'x> OpenBlock<'x> {
.header
.add_transaction_fee(&outcome.receipt.transaction_fee);
self.block.receipts.push(outcome.receipt.clone());
idx += 1;
Ok(outcome.receipt)
}
Err(x) => Err(From::from(x)),
};

receipts_results.push(result);
idx += 1;
}

receipts_results
Expand All @@ -392,7 +392,7 @@ impl<'x> OpenBlock<'x> {
&mut self,
t: SignedTransaction,
h: Option<H256>,
check_gas: bool,
is_building_block: bool,
) -> Result<&Receipt, Error>
{
if self.block.transactions_set.contains(&t.hash()) {
Expand All @@ -415,22 +415,23 @@ impl<'x> OpenBlock<'x> {
&env_info,
self.engine.machine(),
&[t.clone()],
check_gas,
is_building_block,
));
} else {
result.push(self.block.state.apply(
&env_info,
self.engine.machine(),
&t,
check_gas,
is_building_block,
));
}
} else {
result.push(
self.block
.state
.apply(&env_info, self.engine.machine(), &t, check_gas),
);
result.push(self.block.state.apply(
&env_info,
self.engine.machine(),
&t,
is_building_block,
));
}

match result.pop().unwrap() {
Expand Down Expand Up @@ -845,7 +846,7 @@ fn push_transactions(
for t in transactions {
let hash = t.hash();
let start = time::Instant::now();
block.push_transaction(t.clone(), None)?;
block.push_transaction(t.clone(), None, false)?;
let took = start.elapsed();
let took_ms = took.as_secs() * 1000 + took.subsec_nanos() as u64 / 1000000;
if took > time::Duration::from_millis(slow_tx) {
Expand Down
28 changes: 28 additions & 0 deletions core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,34 @@ impl Client {
let last_hashes = self.build_last_hashes(header.parent_hash().clone());
let db = self.state_db.read().boxed_clone_canon(&parent_hash);

// check transaction nonce and type
match State::from_existing(
db.boxed_clone(),
parent.state_root().clone(),
engine.machine().account_start_nonce(parent.number() + 1),
self.factories.clone(),
self.db.read().clone(),
) {
Ok(s) => {
let mut nonce_cache = HashMap::<Address, U256>::new();
for t in block.transactions.clone() {
let expected_nonce: U256 = nonce_cache
.get(t.sender())
.unwrap_or(&s.nonce(t.sender()).unwrap_or(U256::zero()))
.clone();
if expected_nonce != t.nonce {
warn!(target: "client", "Stage 4 block verification failed for #{}. Invalid transaction {}: Tx nonce {} != expected nonce {}\n", header.number(), t.hash(), t.nonce, expected_nonce);
return Err(());
}
nonce_cache.insert(t.sender().clone(), t.nonce + U256::from(1u64));
}
}
_ => {
error!(target: "client", "statedb fatal error");
return Err(());
}
}

let enact_result = enact_verified(
block,
engine,
Expand Down
25 changes: 11 additions & 14 deletions core/src/executive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
&'a mut self,
txs: &[SignedTransaction],
is_local_call: bool,
check_gas: bool,
is_building_block: bool,
) -> Vec<Result<Executed, ExecutionError>>
{
let _vm_lock = AVM_LOCK.lock().unwrap();
Expand All @@ -205,7 +205,7 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
.add_balance(&sender, &(needed_balance), CleanupMode::NoEmpty);
}
debug!(target: "vm", "sender: {:?}, balance: {:?}", sender, self.state.balance(&sender).unwrap_or(0.into()));
} else if check_gas && self.info.gas_used + t.gas > self.info.gas_limit {
} else if is_building_block && self.info.gas_used + t.gas > self.info.gas_limit {
// check gas limit
return vec![Err(From::from(ExecutionError::BlockGasLimitReached {
gas_limit: self.info.gas_limit,
Expand Down Expand Up @@ -344,7 +344,7 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
t: &SignedTransaction,
check_nonce: bool,
is_local_call: bool,
check_gas_limit: bool,
is_building_block: bool,
) -> Result<Executed, ExecutionError>
{
let _vm_lock = VM_LOCK.lock().unwrap();
Expand Down Expand Up @@ -385,15 +385,15 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
// Don't check max gas limit for local call.
// Local node has the right (and is free) to execute "big" calls with its own resources.
// NOTE check gas limit during mining, always try vm execution on import
if !is_local_call && check_gas_limit && t.gas > max_gas_limit {
if !is_local_call && t.gas > max_gas_limit {
return Err(From::from(ExecutionError::ExceedMaxGasLimit {
max: max_gas_limit,
got: t.gas,
}));
}

// 2.3 Gas limit should not exceed the remaining gas limit of the current block
if check_gas_limit && self.info.gas_used + t.gas > self.info.gas_limit {
if is_building_block && self.info.gas_used + t.gas > self.info.gas_limit {
return Err(From::from(ExecutionError::BlockGasLimitReached {
gas_limit: self.info.gas_limit,
gas_used: self.info.gas_used,
Expand Down Expand Up @@ -844,11 +844,8 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
for address in &substate.suicides {
self.state.kill_account(address);
}
let gas_left = match result.status_code {
ExecStatus::Success | ExecStatus::Revert => result.gas_left,
_ => 0.into(),
};
let gas_used = t.gas - gas_left;

let gas_used = t.gas - result.gas_left;

//TODO: check whether avm has already refunded
//let refund_value = gas_left * t.gas_price;
Expand All @@ -859,19 +856,19 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
touched.insert(account);
}

total_gas_used = total_gas_used + gas_used;
if total_gas_used + self.info.gas_used > self.info.gas_limit {
if gas_used + total_gas_used + self.info.gas_used > self.info.gas_limit {
final_results.push(Err(ExecutionError::BlockGasLimitReached {
gas_limit: self.info.gas_limit,
gas_used: self.info.gas_used + total_gas_used,
gas: t.gas,
}));
} else {
total_gas_used = total_gas_used + gas_used;
final_results.push(Ok(Executed {
exception: result.exception,
gas: t.gas,
gas_used: gas_used,
refunded: gas_left,
refunded: result.gas_left,
cumulative_gas_used: self.info.gas_used + gas_used,
logs: substate.logs,
contracts_created: substate.contracts_created,
Expand Down Expand Up @@ -926,7 +923,7 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
self.state
.export_kvdb()
.write(batch)
.expect("GRAPH DB write failed");
.expect("EXTRA DB write failed");
}

return final_results;
Expand Down
Loading

0 comments on commit 7fd720c

Please sign in to comment.