Skip to content

Commit c239c9c

Browse files
authored
Compilance fix 7 (#229)
* Compilance fix 7 * Fix an issue in memory gas panic
1 parent dc364d7 commit c239c9c

File tree

12 files changed

+593
-578
lines changed

12 files changed

+593
-578
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
- name: Run tests
3737
run: |
3838
cargo run --release --verbose -p jsontests -- \
39+
jsontests/res/ethtests/GeneralStateTests/stArgsZeroOneBalance/ \
3940
jsontests/res/ethtests/GeneralStateTests/stCodeCopyTest/ \
4041
jsontests/res/ethtests/GeneralStateTests/stExample/ \
4142
jsontests/res/ethtests/GeneralStateTests/stSelfBalance \

jsontests/src/run.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use evm::backend::in_memory::{
66
use evm::standard::{Config, Etable, Gasometer, Invoker, TransactArgs};
77
use evm::utils::u256_to_h256;
88
use evm::Capture;
9-
use precompiles::StandardPrecompileSet;
9+
use precompiles::StandardResolver;
1010
use primitive_types::U256;
1111
use std::collections::{BTreeMap, BTreeSet};
1212

@@ -60,8 +60,8 @@ pub fn run_test(_filename: &str, _test_name: &str, test: Test, debug: bool) -> R
6060
.collect::<BTreeMap<_, _>>();
6161

6262
let etable = Etable::runtime();
63-
let precompiles = StandardPrecompileSet::new(&config);
64-
let invoker = Invoker::<_, Gasometer, _, _, _, _>::new(&config, &precompiles, &etable);
63+
let resolver = StandardResolver::new(&config);
64+
let invoker = Invoker::<_, Gasometer, _, _, _, _>::new(&config, &resolver, &etable);
6565
let args = TransactArgs::Call {
6666
caller: test.transaction.sender,
6767
address: test.transaction.to,

precompiles/src/blake2/mod.rs

Lines changed: 65 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,91 @@
11
mod eip152;
22

3-
use crate::{address, PurePrecompileSet};
3+
use crate::PurePrecompile;
44
use evm::{ExitException, ExitResult, ExitSucceed, RuntimeState, StaticGasometer};
5-
use primitive_types::H160;
65

76
pub struct Blake2F;
87

98
impl Blake2F {
109
const GAS_COST_PER_ROUND: u64 = 1; // https://eips.ethereum.org/EIPS/eip-152#gas-costs-and-benchmarks
1110
}
1211

13-
impl<G: StaticGasometer> PurePrecompileSet<G> for Blake2F {
12+
impl<G: StaticGasometer> PurePrecompile<G> for Blake2F {
1413
/// Format of `input`:
1514
/// [4 bytes for rounds][64 bytes for h][128 bytes for m][8 bytes for t_0][8 bytes for t_1][1 byte for f]
1615
fn execute(
1716
&self,
1817
input: &[u8],
19-
state: &RuntimeState,
18+
_state: &RuntimeState,
2019
gasometer: &mut G,
21-
) -> Option<(ExitResult, Vec<u8>)> {
22-
const ADDRESS: H160 = address(9);
23-
24-
if state.context.address == ADDRESS {
25-
const BLAKE2_F_ARG_LEN: usize = 213;
26-
27-
if input.len() != BLAKE2_F_ARG_LEN {
28-
return Some((
29-
ExitException::Other(
30-
"input length for Blake2 F precompile should be exactly 213 bytes".into(),
31-
)
32-
.into(),
33-
Vec::new(),
34-
));
35-
}
36-
37-
let mut rounds_buf: [u8; 4] = [0; 4];
38-
rounds_buf.copy_from_slice(&input[0..4]);
39-
let rounds: u32 = u32::from_be_bytes(rounds_buf);
40-
41-
let gas_cost: u64 = (rounds as u64) * Blake2F::GAS_COST_PER_ROUND;
42-
try_some!(gasometer.record_cost(gas_cost.into()));
43-
44-
// we use from_le_bytes below to effectively swap byte order to LE if architecture is BE
45-
let mut h_buf: [u8; 64] = [0; 64];
46-
h_buf.copy_from_slice(&input[4..68]);
47-
let mut h = [0u64; 8];
48-
let mut ctr = 0;
49-
for state_word in &mut h {
50-
let mut temp: [u8; 8] = Default::default();
51-
temp.copy_from_slice(&h_buf[(ctr * 8)..(ctr + 1) * 8]);
52-
*state_word = u64::from_le_bytes(temp);
53-
ctr += 1;
54-
}
20+
) -> (ExitResult, Vec<u8>) {
21+
const BLAKE2_F_ARG_LEN: usize = 213;
22+
23+
if input.len() != BLAKE2_F_ARG_LEN {
24+
return (
25+
ExitException::Other(
26+
"input length for Blake2 F precompile should be exactly 213 bytes".into(),
27+
)
28+
.into(),
29+
Vec::new(),
30+
);
31+
}
5532

56-
let mut m_buf: [u8; 128] = [0; 128];
57-
m_buf.copy_from_slice(&input[68..196]);
58-
let mut m = [0u64; 16];
59-
ctr = 0;
60-
for msg_word in &mut m {
61-
let mut temp: [u8; 8] = Default::default();
62-
temp.copy_from_slice(&m_buf[(ctr * 8)..(ctr + 1) * 8]);
63-
*msg_word = u64::from_le_bytes(temp);
64-
ctr += 1;
65-
}
33+
let mut rounds_buf: [u8; 4] = [0; 4];
34+
rounds_buf.copy_from_slice(&input[0..4]);
35+
let rounds: u32 = u32::from_be_bytes(rounds_buf);
36+
37+
let gas_cost: u64 = (rounds as u64) * Blake2F::GAS_COST_PER_ROUND;
38+
try_some!(gasometer.record_cost(gas_cost.into()));
39+
40+
// we use from_le_bytes below to effectively swap byte order to LE if architecture is BE
41+
let mut h_buf: [u8; 64] = [0; 64];
42+
h_buf.copy_from_slice(&input[4..68]);
43+
let mut h = [0u64; 8];
44+
let mut ctr = 0;
45+
for state_word in &mut h {
46+
let mut temp: [u8; 8] = Default::default();
47+
temp.copy_from_slice(&h_buf[(ctr * 8)..(ctr + 1) * 8]);
48+
*state_word = u64::from_le_bytes(temp);
49+
ctr += 1;
50+
}
6651

67-
let mut t_0_buf: [u8; 8] = [0; 8];
68-
t_0_buf.copy_from_slice(&input[196..204]);
69-
let t_0 = u64::from_le_bytes(t_0_buf);
52+
let mut m_buf: [u8; 128] = [0; 128];
53+
m_buf.copy_from_slice(&input[68..196]);
54+
let mut m = [0u64; 16];
55+
ctr = 0;
56+
for msg_word in &mut m {
57+
let mut temp: [u8; 8] = Default::default();
58+
temp.copy_from_slice(&m_buf[(ctr * 8)..(ctr + 1) * 8]);
59+
*msg_word = u64::from_le_bytes(temp);
60+
ctr += 1;
61+
}
7062

71-
let mut t_1_buf: [u8; 8] = [0; 8];
72-
t_1_buf.copy_from_slice(&input[204..212]);
73-
let t_1 = u64::from_le_bytes(t_1_buf);
63+
let mut t_0_buf: [u8; 8] = [0; 8];
64+
t_0_buf.copy_from_slice(&input[196..204]);
65+
let t_0 = u64::from_le_bytes(t_0_buf);
7466

75-
let f = if input[212] == 1 {
76-
true
77-
} else if input[212] == 0 {
78-
false
79-
} else {
80-
return Some((
81-
ExitException::Other("incorrect final block indicator flag".into()).into(),
82-
Vec::new(),
83-
));
84-
};
67+
let mut t_1_buf: [u8; 8] = [0; 8];
68+
t_1_buf.copy_from_slice(&input[204..212]);
69+
let t_1 = u64::from_le_bytes(t_1_buf);
8570

86-
eip152::compress(&mut h, m, [t_0, t_1], f, rounds as usize);
71+
let f = if input[212] == 1 {
72+
true
73+
} else if input[212] == 0 {
74+
false
75+
} else {
76+
return (
77+
ExitException::Other("incorrect final block indicator flag".into()).into(),
78+
Vec::new(),
79+
);
80+
};
8781

88-
let mut output_buf = [0u8; u64::BITS as usize];
89-
for (i, state_word) in h.iter().enumerate() {
90-
output_buf[i * 8..(i + 1) * 8].copy_from_slice(&state_word.to_le_bytes());
91-
}
82+
eip152::compress(&mut h, m, [t_0, t_1], f, rounds as usize);
9283

93-
Some((ExitSucceed::Returned.into(), output_buf.to_vec()))
94-
} else {
95-
None
84+
let mut output_buf = [0u8; u64::BITS as usize];
85+
for (i, state_word) in h.iter().enumerate() {
86+
output_buf[i * 8..(i + 1) * 8].copy_from_slice(&state_word.to_le_bytes());
9687
}
88+
89+
(ExitSucceed::Returned.into(), output_buf.to_vec())
9790
}
9891
}

0 commit comments

Comments
 (0)