This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
methods.rs
136 lines (118 loc) · 4.5 KB
/
methods.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//! Methods exported to Ekiden clients.
use ethcore::{
rlp,
transaction::{SignedTransaction, UnverifiedTransaction},
types::receipt::TransactionOutcome,
};
use ethereum_types::U256;
use failure::Fallible;
use oasis_core_runtime::{
runtime_context,
transaction::{dispatcher::CheckOnlySuccess, Context as TxnContext},
};
use oasis_runtime_api::{ExecutionResult, LogEntry, TransactionError};
#[cfg_attr(feature = "test", allow(unused))]
use oasis_runtime_common::{
genesis, BLOCK_GAS_LIMIT, MIN_GAS_PRICE_GWEI, TAG_ETH_LOG_ADDRESS, TAG_ETH_LOG_TOPICS,
TAG_ETH_TX_HASH,
};
use crate::block::BlockContext;
/// Check transactions.
pub mod check {
use super::*;
/// Check transaction.
pub fn tx(txn: &[u8], _ctx: &mut TxnContext) -> Fallible<SignedTransaction> {
let decoded: UnverifiedTransaction = rlp::decode(txn)?;
// Check that gas < block gas limit.
if decoded.as_unsigned().gas > BLOCK_GAS_LIMIT.into() {
return Err(TransactionError::TooMuchGas.into());
}
// Check signature.
let signed = SignedTransaction::new(decoded)?;
// Check gas price.
if signed.gas_price < MIN_GAS_PRICE_GWEI.into() {
return Err(TransactionError::GasPrice.into());
}
Ok(signed)
}
}
/// Execute transactions.
pub mod execute {
use super::*;
/// Execute an Ethereum transaction.
pub fn tx(txn: &[u8], ctx: &mut TxnContext) -> Fallible<ExecutionResult> {
// Perform transaction checks.
let txn = super::check::tx(txn, ctx)?;
// If this is a check txn request, return success.
if ctx.check_only {
return Err(CheckOnlySuccess::default().into());
}
let ectx = runtime_context!(ctx, BlockContext);
// Check if current block already contains the transaction. Reject if so.
let txn_hash = txn.hash();
if ectx.transaction_set.contains(&txn_hash) {
return Err(TransactionError::DuplicateTransaction.into());
}
// Check whether the transaction fits in the current block. If not, return
// an error indicating that the client should retry.
let gas_remaining = U256::from(BLOCK_GAS_LIMIT) - ectx.env_info.gas_used;
if txn.gas > gas_remaining {
return Err(TransactionError::BlockGasLimitReached.into());
}
// Create Ethereum state instance and apply the transaction.
let outcome = ectx
.state
.apply(
&ectx.env_info,
genesis::SPEC.engine.machine(),
&txn,
false, /* tracing */
true, /* should_return_value */
)
.map_err(|err| TransactionError::ExecutionFailure {
message: format!("{}", err),
})?;
// Add to set of executed transactions.
ectx.transaction_set.insert(txn_hash);
// Calculate the amount of gas used by this transaction and update the
// cumulative gas used for the batch. Note: receipt.gas_used is the cumulative
// gas used after executing the given transaction.
let gas_used = outcome.receipt.gas_used - ectx.env_info.gas_used;
ectx.env_info.gas_used = outcome.receipt.gas_used;
// Emit the Ekiden transaction hash so that we can query it.
#[cfg(not(feature = "test"))]
{
ctx.emit_txn_tag(TAG_ETH_TX_HASH, txn_hash);
for log in &outcome.receipt.logs {
ctx.emit_txn_tag(TAG_ETH_LOG_ADDRESS, log.address);
log.topics
.iter()
.zip(TAG_ETH_LOG_TOPICS.iter())
.take(4)
.for_each(|(topic, tag)| {
ctx.emit_txn_tag(tag, topic);
})
}
}
Ok(ExecutionResult {
cumulative_gas_used: outcome.receipt.gas_used,
gas_used,
log_bloom: outcome.receipt.log_bloom,
logs: outcome
.receipt
.logs
.into_iter()
.map(|log| LogEntry {
address: log.address,
topics: log.topics,
data: log.data,
})
.collect(),
status_code: match outcome.receipt.outcome {
TransactionOutcome::StatusCode(code) => code,
_ => unreachable!("we always use EIP-658 semantics"),
},
output: outcome.output,
})
}
}