Skip to content

Commit 159bbf3

Browse files
Proto overhaul (#1259)
* chore: create a proto based on eth * chore: further optimize proto types * chore: finish proto serialization * chore: adapt tests to the new proto definitions * lint
1 parent c453e82 commit 159bbf3

13 files changed

+103
-98
lines changed

build.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ fn print_build_directives() {
3636
// Code generation: Proto files
3737
// -----------------------------------------------------------------------------
3838
fn generate_proto_structs() {
39-
tonic_build::compile_protos("static/proto/append_entry.proto").unwrap();
39+
tonic_build::configure()
40+
.protoc_arg("--experimental_allow_proto3_optional")
41+
.compile(&["static/proto/append_entry.proto"], &["static/proto"])
42+
.unwrap();
4043
}
4144

4245
// -----------------------------------------------------------------------------

chaos/experiments/leader-election.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ check_leader() {
8686
local grpc_address=$1
8787

8888
# Send the gRPC request using grpcurl and capture both stdout and stderr
89-
response=$(grpcurl -import-path static/proto -proto append_entry.proto -plaintext -d '{"term": 0, "prevLogIndex": 0, "prevLogTerm": 0, "leader_id": "leader_id_value", "block_entry": {"number": 1, "hash": "hash_value", "transactions_root": "transactions_root_value", "gas_used": "gas_used_value", "gas_limit": "gas_limit_value", "bloom": "bloom_value", "timestamp": 123456789, "parent_hash": "parent_hash_value", "author": "author_value", "extra_data": "ZXh0cmFfZGF0YV92YWx1ZQ==", "miner": "miner_value", "difficulty": "difficulty_value", "receipts_root": "receipts_root_value", "uncle_hash": "uncle_hash_value", "size": 12345, "state_root": "state_root_value", "total_difficulty": "total_difficulty_value", "nonce": "nonce_value", "transaction_hashes": ["tx_hash1", "tx_hash2"]}}' "$grpc_address" append_entry.AppendEntryService/AppendBlockCommit 2>&1)
89+
response=$(grpcurl -import-path static/proto -proto append_entry.proto -plaintext -d '{"term": 5, "prevLogIndex": 0, "prevLogTerm": 4, "leader_id": "leader_id_value", "block_entry": {"number": 1, "hash": "ZXh0cmFfZGF0YV92YWx1ZQ==", "transactions_root": "ZXh0cmFfZGF0YV92YWx1ZQ==", "gas_used": 999, "gas_limit": 999, "bloom": "ZXh0cmFfZGF0YV92YWx1ZQ==", "timestamp": 123456789, "parent_hash": "ZXh0cmFfZGF0YV92YWx1ZQ==", "author": "ZXh0cmFfZGF0YV92YWx1ZQ==", "extra_data": "ZXh0cmFfZGF0YV92YWx1ZQ==", "miner": "ZXh0cmFfZGF0YV92YWx1ZQ==", "receipts_root": "ZXh0cmFfZGF0YV92YWx1ZQ==", "uncle_hash": "ZXh0cmFfZGF0YV92YWx1ZQ==", "size": 12345, "state_root": "ZXh0cmFfZGF0YV92YWx1ZQ==", "transaction_hashes": []}}' "$grpc_address" append_entry.AppendEntryService/AppendBlockCommit 2>&1)
9090

9191
# Check the response for specific strings to determine the node status
9292
if [[ "$response" == *"append_transaction_executions called on leader node"* ]]; then

src/eth/block_miner.rs

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ impl BlockMiner {
9696
self.storage.save_execution(tx_execution.clone())?;
9797

9898
// decide what to do based on mining mode
99+
// FIXME consensus should be synchronous here and wait for the confirmation from the majority
99100
let _ = self.notifier_pending_txs.send(tx_execution);
100101
if self.mode.is_automine() {
101102
self.mine_local_and_commit()?;

src/eth/consensus/append_log_entries_storage.rs

-6
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,11 @@ mod tests {
116116
assert_eq!(block.hash, expected_block.hash);
117117
assert_eq!(block.number, expected_block.number);
118118
assert_eq!(block.parent_hash, expected_block.parent_hash);
119-
assert_eq!(block.nonce, expected_block.nonce);
120119
assert_eq!(block.uncle_hash, expected_block.uncle_hash);
121120
assert_eq!(block.transactions_root, expected_block.transactions_root);
122121
assert_eq!(block.state_root, expected_block.state_root);
123122
assert_eq!(block.receipts_root, expected_block.receipts_root);
124123
assert_eq!(block.miner, expected_block.miner);
125-
assert_eq!(block.difficulty, expected_block.difficulty);
126-
assert_eq!(block.total_difficulty, expected_block.total_difficulty);
127124
assert_eq!(block.extra_data, expected_block.extra_data);
128125
assert_eq!(block.size, expected_block.size);
129126
assert_eq!(block.gas_limit, expected_block.gas_limit);
@@ -171,7 +168,6 @@ mod tests {
171168
assert_eq!(execution.output, expected_execution.output);
172169
assert_eq!(execution.from, expected_execution.from);
173170
assert_eq!(execution.to, expected_execution.to);
174-
assert_eq!(execution.block_hash, expected_execution.block_hash);
175171
assert_eq!(execution.block_number, expected_execution.block_number);
176172
assert_eq!(execution.transaction_index, expected_execution.transaction_index);
177173
assert_eq!(execution.logs.len(), expected_execution.logs.len());
@@ -180,8 +176,6 @@ mod tests {
180176
assert_eq!(log.topics, expected_log.topics);
181177
assert_eq!(log.data, expected_log.data);
182178
assert_eq!(log.log_index, expected_log.log_index);
183-
assert_eq!(log.removed, expected_log.removed);
184-
assert_eq!(log.transaction_log_index, expected_log.transaction_log_index);
185179
}
186180
assert_eq!(execution.gas, expected_execution.gas);
187181
assert_eq!(execution.receipt_cumulative_gas_used, expected_execution.receipt_cumulative_gas_used);

src/eth/consensus/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod discovery;
55
pub mod forward_to;
66
#[allow(dead_code)]
77
mod log_entry;
8+
pub mod utils;
89

910
mod server;
1011

src/eth/consensus/tests/factories.rs

+26-27
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::net::Ipv4Addr;
22
use std::net::SocketAddr;
33
use std::sync::Arc;
44

5+
use ethereum_types::H160;
56
use ethereum_types::H256;
6-
use ethereum_types::U256;
77
use rand::Rng;
88
use tokio::sync::broadcast;
99
use tokio::sync::Mutex;
@@ -12,65 +12,59 @@ use crate::eth::consensus::append_entry::AppendBlockCommitResponse;
1212
use crate::eth::consensus::append_entry::AppendTransactionExecutionsResponse;
1313
use crate::eth::consensus::append_entry::Log;
1414
use crate::eth::consensus::append_entry::RequestVoteResponse;
15+
use crate::eth::consensus::append_entry::TransactionExecutionEntry;
1516
use crate::eth::consensus::log_entry::LogEntry;
1617
use crate::eth::consensus::BlockEntry;
1718
use crate::eth::consensus::Consensus;
1819
use crate::eth::consensus::LogEntryData;
1920
use crate::eth::consensus::Peer;
2021
use crate::eth::consensus::PeerAddress;
2122
use crate::eth::consensus::Role;
22-
use crate::eth::consensus::TransactionExecutionEntry;
2323
use crate::eth::storage::StratusStorage;
2424

25-
pub fn create_mock_block_entry(transaction_hashes: Vec<String>) -> BlockEntry {
25+
pub fn create_mock_block_entry(transaction_hashes: Vec<Vec<u8>>) -> BlockEntry {
2626
BlockEntry {
2727
number: rand::thread_rng().gen(),
28-
hash: H256::random().to_string(),
29-
parent_hash: H256::random().to_string(),
30-
nonce: H256::random().to_string(),
31-
uncle_hash: H256::random().to_string(),
32-
transactions_root: H256::random().to_string(),
33-
state_root: H256::random().to_string(),
34-
receipts_root: H256::random().to_string(),
35-
miner: H256::random().to_string(),
36-
difficulty: U256::from(rand::thread_rng().gen::<u64>()).to_string(),
37-
total_difficulty: U256::from(rand::thread_rng().gen::<u64>()).to_string(),
28+
hash: H256::random().as_bytes().to_vec(),
29+
parent_hash: H256::random().as_bytes().to_vec(),
30+
uncle_hash: H256::random().as_bytes().to_vec(),
31+
transactions_root: H256::random().as_bytes().to_vec(),
32+
state_root: H256::random().as_bytes().to_vec(),
33+
receipts_root: H256::random().as_bytes().to_vec(),
34+
miner: H160::random().as_bytes().to_vec(),
35+
author: H160::random().as_bytes().to_vec(),
3836
extra_data: vec![rand::thread_rng().gen()],
3937
size: rand::thread_rng().gen(),
40-
gas_limit: U256::from(rand::thread_rng().gen::<u64>()).to_string(),
41-
gas_used: U256::from(rand::thread_rng().gen::<u64>()).to_string(),
38+
gas_limit: rand::thread_rng().gen(),
39+
gas_used: rand::thread_rng().gen(),
4240
timestamp: rand::thread_rng().gen(),
43-
bloom: H256::random().to_string(),
44-
author: H256::random().to_string(),
41+
bloom: H256::random().as_bytes().to_vec(),
4542
transaction_hashes,
4643
}
4744
}
4845

4946
pub fn create_mock_transaction_execution_entry() -> TransactionExecutionEntry {
5047
TransactionExecutionEntry {
51-
hash: H256::random().to_string(),
48+
hash: H256::random().as_bytes().to_vec(),
5249
nonce: rand::thread_rng().gen(),
5350
value: vec![rand::thread_rng().gen()],
5451
gas_price: vec![rand::thread_rng().gen()],
5552
input: vec![rand::thread_rng().gen()],
5653
v: rand::thread_rng().gen(),
5754
r: vec![rand::thread_rng().gen()],
5855
s: vec![rand::thread_rng().gen()],
59-
chain_id: rand::thread_rng().gen(),
60-
result: vec![rand::thread_rng().gen()],
56+
chain_id: Some(rand::thread_rng().gen()),
57+
result: "Success".to_string(),
6158
output: vec![rand::thread_rng().gen()],
62-
from: H256::random().to_string(),
63-
to: H256::random().to_string(),
64-
block_hash: H256::random().to_string(),
59+
from: H160::random().as_bytes().to_vec(),
60+
to: Some(H160::random().as_bytes().to_vec()),
6561
block_number: rand::thread_rng().gen(),
6662
transaction_index: rand::thread_rng().gen(),
6763
logs: vec![Log {
68-
address: H256::random().to_string(),
69-
topics: vec![H256::random().to_string()],
64+
address: H160::random().as_bytes().to_vec(),
65+
topics: vec![H256::random().as_bytes().to_vec()],
7066
data: vec![rand::thread_rng().gen()],
7167
log_index: rand::thread_rng().gen(),
72-
removed: rand::thread_rng().gen(),
73-
transaction_log_index: rand::thread_rng().gen(),
7468
}],
7569
gas: vec![rand::thread_rng().gen()],
7670
receipt_cumulative_gas_used: vec![rand::thread_rng().gen()],
@@ -79,6 +73,11 @@ pub fn create_mock_transaction_execution_entry() -> TransactionExecutionEntry {
7973
receipt_status: rand::thread_rng().gen(),
8074
receipt_logs_bloom: vec![rand::thread_rng().gen()],
8175
receipt_effective_gas_price: vec![rand::thread_rng().gen()],
76+
tx_type: Some(rand::thread_rng().gen()),
77+
signer: vec![rand::thread_rng().gen()],
78+
gas_limit: vec![rand::thread_rng().gen()],
79+
receipt_applied: rand::thread_rng().gen(),
80+
deployed_contract_address: Some(vec![rand::thread_rng().gen()]),
8281
}
8382
}
8483

src/eth/consensus/tests/test_simple_blocks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async fn test_append_entries_transaction_executions_and_block() {
5555
}
5656

5757
// Create and append block with transaction hashes
58-
let transaction_hashes: Vec<String> = all_executions.iter().map(|e| e.hash.clone()).collect();
58+
let transaction_hashes: Vec<Vec<u8>> = all_executions.iter().map(|e| e.hash.clone()).collect();
5959

6060
let block_entry = create_mock_block_entry(transaction_hashes.clone());
6161

src/eth/consensus/utils.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub fn u256_to_bytes(u: ethereum_types::U256) -> Vec<u8> {
2+
let mut bytes = [0u8; 32];
3+
u.to_big_endian(&mut bytes);
4+
bytes.to_vec()
5+
}

src/eth/primitives/block_header.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,23 @@ impl BlockHeader {
7373
}
7474
}
7575

76-
pub fn to_append_entry_block_header(&self, transaction_hashes: Vec<String>) -> append_entry::BlockEntry {
76+
pub fn to_append_entry_block_header(&self, transaction_hashes: Vec<Vec<u8>>) -> append_entry::BlockEntry {
7777
append_entry::BlockEntry {
7878
number: self.number.into(),
79-
hash: self.hash.to_string(),
80-
transactions_root: self.transactions_root.to_string(),
81-
gas_used: self.gas_used.to_string(),
82-
gas_limit: self.gas_limit.to_string(),
83-
bloom: self.bloom.to_string(),
79+
hash: self.hash.as_fixed_bytes().to_vec(),
80+
transactions_root: self.transactions_root.as_fixed_bytes().to_vec(),
81+
gas_used: self.gas_used.as_u64(),
82+
gas_limit: self.gas_limit.as_u64(),
83+
bloom: self.bloom.as_bytes().to_vec(),
8484
timestamp: self.timestamp.as_u64(),
85-
parent_hash: self.parent_hash.to_string(),
86-
author: self.author.to_string(),
85+
parent_hash: self.parent_hash.as_fixed_bytes().to_vec(),
86+
author: self.author.to_fixed_bytes().to_vec(),
8787
extra_data: self.extra_data.clone().0,
88-
miner: self.miner.to_string(),
89-
difficulty: self.difficulty.to_string(),
90-
receipts_root: self.receipts_root.to_string(),
91-
uncle_hash: self.uncle_hash.to_string(),
88+
miner: self.miner.to_fixed_bytes().to_vec(),
89+
receipts_root: self.receipts_root.as_fixed_bytes().to_vec(),
90+
uncle_hash: self.uncle_hash.as_fixed_bytes().to_vec(),
9291
size: self.size.into(),
93-
state_root: self.state_root.to_string(),
94-
total_difficulty: self.total_difficulty.to_string(),
95-
nonce: self.nonce.to_string(),
92+
state_root: self.state_root.as_fixed_bytes().to_vec(),
9693
transaction_hashes,
9794
}
9895
}

src/eth/primitives/hash.rs

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ impl Hash {
4343
pub fn inner_value(&self) -> H256 {
4444
self.0
4545
}
46+
47+
pub fn as_fixed_bytes(&self) -> &[u8; 32] {
48+
self.0.as_fixed_bytes()
49+
}
4650
}
4751

4852
impl Display for Hash {

src/eth/primitives/logs_bloom.rs

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ use crate::gen_newtype_from;
99
#[serde(transparent)]
1010
pub struct LogsBloom(Bloom);
1111

12+
impl LogsBloom {
13+
pub fn as_bytes(&self) -> &[u8] {
14+
self.0.as_bytes()
15+
}
16+
}
17+
1218
impl Deref for LogsBloom {
1319
type Target = Bloom;
1420

src/eth/primitives/transaction_execution.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use display_json::DebugAsJson;
22

33
use crate::eth::consensus::append_entry;
4+
use crate::eth::consensus::utils::*;
45
use crate::eth::evm::EvmExecutionResult;
56
use crate::eth::primitives::EvmExecution;
67
use crate::eth::primitives::ExternalReceipt;
@@ -66,40 +67,31 @@ impl TransactionExecution {
6667

6768
/// TODO: use From or TryFrom trait instead of this function
6869
pub fn to_append_entry_transaction(&self) -> append_entry::TransactionExecutionEntry {
69-
fn u256_to_bytes(u: ethereum_types::U256) -> Vec<u8> {
70-
let mut bytes = [0u8; 32];
71-
u.to_big_endian(&mut bytes);
72-
bytes.to_vec()
73-
}
74-
7570
match self {
7671
Self::External(ExternalTransactionExecution { tx, receipt, result }) => append_entry::TransactionExecutionEntry {
77-
hash: tx.hash.to_string(),
72+
hash: tx.hash.to_fixed_bytes().to_vec(),
7873
nonce: tx.nonce.as_u64(),
7974
value: u256_to_bytes(tx.value),
8075
gas_price: tx.gas_price.map_or(vec![], u256_to_bytes),
8176
input: tx.input.to_vec(),
8277
v: tx.v.as_u64(),
8378
r: u256_to_bytes(tx.r),
8479
s: u256_to_bytes(tx.s),
85-
chain_id: tx.chain_id.unwrap_or_default().as_u64(),
86-
result: result.execution.output.to_vec(),
80+
chain_id: Some(tx.chain_id.unwrap_or_default().as_u64()),
81+
result: result.execution.result.to_string(),
8782
output: result.execution.output.to_vec(),
88-
from: tx.from.to_string(),
89-
to: tx.to.unwrap_or_default().to_string(),
90-
block_hash: receipt.block_hash().to_string(),
83+
from: tx.from.as_bytes().to_vec(),
84+
to: tx.to.map(|to| to.as_bytes().to_vec()),
9185
block_number: receipt.block_number().as_u64(),
9286
transaction_index: receipt.transaction_index.as_u64(),
9387
logs: receipt
9488
.logs
9589
.iter()
9690
.map(|log| append_entry::Log {
97-
address: log.address.to_string(),
98-
topics: log.topics.iter().map(|topic| topic.to_string()).collect(),
91+
address: log.address.as_bytes().to_vec(),
92+
topics: log.topics.iter().map(|topic| topic.as_bytes().to_vec()).collect(),
9993
data: log.data.to_vec(),
10094
log_index: log.log_index.unwrap_or_default().as_u64(),
101-
transaction_log_index: log.transaction_log_index.unwrap_or_default().as_u64(),
102-
removed: log.removed.unwrap_or(false),
10395
})
10496
.collect(),
10597
gas: u256_to_bytes(tx.gas),
@@ -109,6 +101,11 @@ impl TransactionExecution {
109101
receipt_status: receipt.status.unwrap_or_default().as_u32(),
110102
receipt_logs_bloom: receipt.logs_bloom.as_bytes().to_vec(),
111103
receipt_effective_gas_price: receipt.effective_gas_price.map_or(vec![], u256_to_bytes),
104+
deployed_contract_address: None,
105+
gas_limit: u256_to_bytes(tx.gas),
106+
signer: vec![],
107+
receipt_applied: true,
108+
tx_type: None,
112109
},
113110
// TODO: no need to panic here, this could be implemented
114111
_ => panic!("Only ExternalTransactionExecution is supported"),

0 commit comments

Comments
 (0)